PostgreSQL Extensions in Go jespino.github.io · 165 points · 32 comments · HN th...

@best30

PostgreSQL Extensions in Go jespino.github.io · 165 points · 32 comments · HN thread

TL;DR gopgext is a tool that builds PostgreSQL extensions from plain Go code compiled with TinyGo. Functions marked with a //gopgext:function comment become SQL functions, and the tool generates the C glue, install script and control file. The page claims support for triggers, aggregates, index operator classes, and ports of several contrib modules that pass upstream regression tests.

  • Mark a Go function with //gopgext:function and gopgext generates the C glue, install script and control file to expose it as a SQL function.
  • Standard library packages such as fmt, regexp, encoding/json and crypto work inside extensions; Go types map to SQL types including arrays, NULLs, UUIDs, timestamps and JSON.
  • Supports SPI queries, SQLSTATE error codes, logging, settings, and responsiveness to query cancellation.
  • Also covers triggers, aggregates, GiST/GIN/SP-GiST/BRIN operator classes, index access methods, foreign data wrappers and query hooks.
  • Ports of pgcrypto, hstore, pg_stat_statements and cube are said to pass upstream regression tests.
  • Built with TinyGo and a C runtime compiled via PGXS, using the same approach as the author's gokm project for Linux kernel modules in Go.

Discussion The thread is mostly a mix of skepticism and curiosity. Several commenters question whether Go fits Postgres extension work, which they see as data-plane code where C or Rust is the norm, and some argue that PL/pgSQL, PL/V8 or PL/JS already cover most needs. Others point to technical friction between the Go runtime and Postgres's process model, signals and memory contexts, and a few explain why TinyGo was chosen over full cgo. The tone is measured, with a few jokes about mascots and a practical side discussion about alternatives such as pgrx, plgo and pljs.

  • Go is a poor fit for data-plane extension code (many): Commenters argue Go shines for platform-level software, while Postgres extensions do performance-sensitive work where C or Rust are better, and GC makes Go unsuitable for some tasks.
  • Use PL/pgSQL or JS procedural languages instead (some): Several users suggest that plpgsql is enough for most logic, and that plv8 or pljs cover cases where a more capable language is needed without writing a native extension.
  • Go's runtime clashes with the Postgres process model (some): Commenters note the risks of signal handler conflicts, GC versus palloc memory contexts, and runtime threads in a forked backend; one says TinyGo's single-threaded design avoids much of this at the cost of goroutines.
  • Rust with pgrx is the preferred route (a few): Some commenters say they will stick with pgrx, which has no runtime and integrates directly with Postgres error handling.
  • Welcome easier extension authoring for Go developers (a few): A minority finds it useful that teams with Go codebases can write extensions without learning C, and welcomes the tooling as an addition to the ecosystem.

Links from the comments

#PostgreSQL #Go #TinyGo #databaseExtensions #CGo #pgrx