gopgext 让 Go 直接编写 PostgreSQL 扩展

原标题:PostgreSQL Extensions in Go
58AI 研判分

开源项目 gopgext 基于 TinyGo 自定义目标与运行时层,可将普通 Go 代码编译为 PostgreSQL 扩展,自动生成 C 胶水代码、安装脚本与控制文件。项目支持标准库、SQL 类型映射、SPI 查询与 SQLSTATE 错误码,但目前仍属实验性,官方明确警告不可用于生产环境。

报道正文

本文目录8 个章节

gopgext builds PostgreSQL extensions from plain Go code, using TinyGo. Mark a function with //gopgext:function and it becomes a SQL function: gopgext writes the C glue, the install script and the control file.

Get started Documentation GitHub

The Go gopher in a PostgreSQL elephant costume

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 Go

A project is plain Go files. The standard library works: fmt, regexp, encoding/json, crypto/*, maps, generics, iterators, defer/recover.

Types map to SQL

Strings, 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 errors

Run SQL through SPI, return errors with SQLSTATE codes, log, read settings and stay responsive to query cancellation.

Beyond functions

Write triggers, aggregates, GiST, GIN, SP-GiST and BRIN operator classes, index access methods, foreign data wrappers and query hooks.

Proven on contrib

Ports of PostgreSQL’s contrib modules (pgcrypto, hstore, pg_stat_statements, cube, …) pass upstream’s regression tests.

Safe coexistence

Several Go extensions load in one backend. PostgreSQL errors never unwind through Go frames, and results are copied into PostgreSQL memory.

Hello, world#

GO
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
BASH
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.