· via Hacker News – Front Page (native)
gopgext turns ordinary Go code into PostgreSQL extensions via TinyGo
gopgext compiles ordinary Go functions into loadable PostgreSQL extensions using TinyGo, generating the C bindings and install files automatically — but its own documentation warns it is not production-ready.

A project called gopgext has drawn attention on the Hacker News front page with an unusual pitch: write PostgreSQL extensions in ordinary Go, and let the toolchain turn them into loadable server modules. According to the project's documentation, a developer marks a Go function with a //gopgext:function comment, and gopgext handles everything else — the C shim, the SQL install script and the extension control file. TinyGo, the compact Go compiler, provides the compilation step.
How it works
A project is just Go source files. The developer annotates exported functions with the directive, optionally tagging properties such as volatility and parallel safety, and gopgext produces the artifacts PostgreSQL expects from an extension. Two commands drive the workflow: one scaffolds a new extension with a go.mod and an entry file, the other builds the shared library plus its control and SQL files and installs them. From there, CREATE EXTENSION in psql exposes the functions to SQL.
The documentation's worked example defines two functions: one that returns a greeting for a given name, and one that upper-cases a string, returning a structured error with a SQLSTATE code attached when the input is empty. Apart from the comment above each function, nothing in the code is extension-specific.
Standard Go, mapped to SQL types
Because compilation goes through TinyGo, the project says the Go standard library largely works inside the server, including fmt, regexp, encoding/json, the crypto packages, generics, iterators and defer/recover. Value types translate to SQL counterparts: strings, numbers, arrays, NULL handling, sets, rows, timestamps, UUIDs and JSON, and developers can define their own SQL data types and composite types in Go.
Extensions can also execute SQL through PostgreSQL's Server Programming Interface, raise errors carrying SQLSTATE codes, write log messages, read server settings and remain responsive when a query is cancelled, the documentation says.
More than scalar functions
The scope goes well beyond simple functions. The project lists support for triggers, aggregate functions, operator classes for the GiST, GIN, SP-GiST and BRIN index methods, custom index access methods, foreign data wrappers and query hooks — essentially the full surface that C extensions traditionally occupy.
To validate the approach, several of PostgreSQL's own contrib modules have been ported to it, including pgcrypto, hstore, pg_stat_statements and cube, and the documentation reports that these ports satisfy the regression test suites those modules ship with upstream.
Coexistence and safety
Multiple Go extensions can be loaded into the same backend at once. The runtime is built so PostgreSQL errors never propagate through Go stack frames, and results are copied out into PostgreSQL-managed memory rather than shared in place.
The project is blunt about maturity, though. gopgext is experimental and should not run in production: a defect in generated code or in the Go runtime executes inside the server process, where it can crash the server or corrupt data, and both the API and the generated SQL are subject to change without notice.
A shared technique with kernel work
gopgext also has a sibling project, gokm, which applies the same recipe to Linux kernel modules written in Go. Both share one architecture, the documentation explains: a custom TinyGo target plus a runtime layer tailored to the host environment, a C-side runtime built with the host's native build machinery — PGXS for PostgreSQL, kbuild for the kernel — and generated binding code tying the two worlds together.
Why it matters
PostgreSQL extension development has been effectively a C-only discipline, with all the memory-management pitfalls and boilerplate that implies, while the safer alternatives such as PL/pgSQL cannot reach the parts of the server where triggers, index methods and foreign data wrappers live. gopgext sketches a middle route: a garbage-collected, statically typed language with a large standard library, compiled into a native module that can touch those internals. The contrib ports passing upstream regression tests suggest the idea is credible rather than a stunt. But the project's own production warning is the honest caveat — the hardest problems in this design are the safety ones, and until the runtime proves itself, gopgext is best read as a promising proof of concept for how database extensibility could look.
- #postgresql
- #golang
- #tinygo
- #database
- #developer-tools