Skip to content
HN On Hacker News ↗

gopgext — PostgreSQL extensions in Go

▲ 169 points • 37 comments • by valyala • 2d ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this entire text is AI.

100 %

AI likelihood · overall

AI
0% human-written 100% AI-generated
SEGMENTS · HUMAN 0 of 5
SEGMENTS · AI 5 of 5
WORD COUNT 356
PEAK AI % 100% · §1
Analyzed
Nov 21
backend: pangram/v3.3
Segments scanned
5 windows
avg 71 words each
Distribution
0 / 100%
human / AI fraction
Verdict
AI
Pangram v3.3

Article text · 356 words · 5 segments analyzed

Human AI-generated
§1 AI · 100%

gopgext builds PostgreSQL extensions from plain Go code, using TinyGo.

§2 AI · 100%

Mark a function with //gopgext:function and it becomes a SQL function: gopgext writes the C glue, the install script and the control file.Experimentalgopgext is experimental. Don’t use it in production: a bug in the generated code or the Go runtime runs inside the server, where it can crash it or corrupt data, and the API and generated SQL may change without notice.Just GoA project is plain Go files. The standard library works: fmt, regexp, encoding/json, crypto/*, maps, generics, iterators, defer/recover.Types map to SQLStrings, numbers, arrays, NULLs, sets, rows, timestamps, UUIDs and JSON map to SQL types. Define your own data types and composite types in Go.Queries and errorsRun SQL through SPI, return errors with SQLSTATE codes, log, read settings and stay responsive to query cancellation.Beyond functionsWrite triggers, aggregates, GiST, GIN, SP-GiST and BRIN operator classes, index access methods, foreign data wrappers and query hooks.Proven on contribPorts of PostgreSQL’s contrib modules (pgcrypto, hstore, pg_stat_statements, cube, …) pass upstream’s regression tests.Safe coexistenceSeveral Go extensions load in one backend. PostgreSQL errors never unwind through Go frames, and results are copied into PostgreSQL memory.Hello, world#package main import ( "strings" "gopgext/pg" ) // Hello returns a greeting for name.

§3 AI · 100%

// //gopgext:function immutable parallel=safe func Hello(name string) string { return "Hello, " + name + "!"

§4 AI · 100%

} // Shout upper-cases s. An empty string raises an error.

§5 AI · 100%

// //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.