PostgreSQL Extensions in Go
Hello, world# package main import ( "strings" "gopgext/pg" ) // Hello returns a greeting for name. // //gopgext:function immutable parallel=safe func Hello(name string) string { return "Hello, " + name + "!" } // Shout upper-cases s. An empty string raises an error. // //gopgext:function immutable parallel=safe func Shout(s string) (string, error) { if s == "" { return "", pg.Errorf(pg.CodeInvalidParameterValue, "nothing to shout") } return strings.ToUpper(s) + "!", nil } func main() {} // runs once per backend, when PostgreSQL loads the library gopgext init hello # creates hello/go.mod and hello/main.go gopgext install hello # builds hello.so, hello.control, hello--1.0.sql and installs them psql -c 'CREATE EXTENSION hello' -c "SELECT hello('world')" # hello # --------------- # Hello, world! The tutorial walks through it step by step. A sibling project# gopgext uses the same approach as gokm, which builds Linux kernel modules from Go: a custom TinyGo target and runtime layer for the host, a C runtime compiled by the host’s own build system (PGXS here, kbuild there), and generated glue.