gopgext builds PostgreSQL extensions from plain Go using TinyGo

gopgext is a tool that builds PostgreSQL extensions from plain Go code, using TinyGo. A developer marks a Go function with a //gopgext:function comment and it becomes a SQL function. The tool writes the C glue, the install script and the control file, so the project itself is just Go files.
The project page lists what works. Parts of the Go standard library are usable inside an extension: fmt, regexp, encoding/json, crypto/*, maps, generics, iterators and defer/recover. Strings, numbers, arrays, NULLs, sets, rows, timestamps, UUIDs and JSON map to SQL types, and developers can define their own data types and composite types in Go. Extensions can run SQL through SPI, return errors with SQLSTATE codes, log, read settings and stay responsive to query cancellation.
The scope goes beyond plain functions. The page says you can write triggers, aggregates, GiST, GIN, SP-GiST and BRIN operator classes, index access methods, foreign data wrappers and query hooks.
As evidence, the page says ports of PostgreSQL's contrib modules (pgcrypto, hstore, pg_stat_statements, cube and others) pass upstream's regression tests. On coexistence, it says several Go extensions load in one backend, PostgreSQL errors never unwind through Go frames, and results are copied into PostgreSQL memory.
The Hello, world example defines two functions. Hello takes a name and returns "Hello, " plus the name and an exclamation mark. Shout upper-cases a string and returns an error through pg.Errorf with the code CodeInvalidParameterValue and the message "nothing to shout" when the string is empty. Both carry the annotation //gopgext:function immutable parallel=safe. An empty main function runs once per backend, when PostgreSQL loads the library. The workflow is two commands: gopgext init hello creates hello/go.mod and hello/main.go, and gopgext install hello builds hello.so, hello.control and hello--1.0.sql and installs them. Then psql runs CREATE EXTENSION hello and SELECT hello('world'), which returns "Hello, world!". The page points to a tutorial that walks through it step by step.
The page also names a sibling project, gokm, which builds Linux kernel modules from Go. gopgext uses the same approach: a custom TinyGo target and runtime layer for the host, a C runtime compiled by the host's own build system (PGXS for PostgreSQL, kbuild for the kernel), and generated glue.
Key facts
- gopgext builds PostgreSQL extensions from plain Go code using TinyGo; a //gopgext:function comment turns a Go function into a SQL function.
- It generates the C glue, the install script and the control file; the workflow is gopgext init, gopgext install, then CREATE EXTENSION in psql.
- Supported extension types include triggers, aggregates, GiST, GIN, SP-GiST and BRIN operator classes, index access methods, foreign data wrappers and query hooks.
- The page says ports of contrib modules (pgcrypto, hstore, pg_stat_statements, cube and others) pass upstream's regression tests.
- It shares its approach with gokm, a sibling project that builds Linux kernel modules from Go, using PGXS here and kbuild there.
Why it matters
PostgreSQL extensions are normally written in C. gopgext offers a route where the extension author writes ordinary Go and the tool produces the C glue, install script and control file. The page's claim that ports of contrib modules such as pgcrypto, hstore, pg_stat_statements and cube pass upstream's regression tests is the main evidence that this reaches beyond toy functions. The same TinyGo-plus-generated-glue approach is also used by gokm for Linux kernel modules.
Who it affects
Go developers who want to add SQL functions, types, triggers, aggregates, index access methods, foreign data wrappers or query hooks to PostgreSQL without writing the extension in C. Database engineers who maintain extensions are the natural audience for the contrib-module ports.
How to use it
Run gopgext init hello to create hello/go.mod and hello/main.go. Write Go functions and mark each with //gopgext:function plus options such as immutable and parallel=safe. Run gopgext install hello to build hello.so, hello.control and hello--1.0.sql and install them. Then run CREATE EXTENSION hello in psql and call the function, for example SELECT hello('world'). The project page links a step-by-step tutorial.
How solid is it
The evidence is the project's own page. It says ports of contrib modules pass upstream's regression tests and that several Go extensions load in one backend. It does not say how many contrib modules were ported or whether every one passes; the list ends with an ellipsis. No reactions or third-party opinions are included.
Risks and caveats
The page gives no performance numbers, benchmarks or binary-size figures. It does not say which PostgreSQL versions are supported. No license is stated. It makes no production-readiness statement and lists no known limitations of TinyGo or adoption figures. The page names no author, maintainer or organisation, and gives no release date or version number.
“gopgext builds PostgreSQL extensions from plain Go code, using TinyGo.”
— gopgext project page