embeddeddsql

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2026 License: MIT Imports: 27 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

embedded-dsql

Aurora DSQL-compatible PostgreSQL for Go tests: no Docker, no cluster.

embedded-dsql starts a real PostgreSQL 16 server via fergusstrange/embedded-postgres and puts an in-process wire-protocol proxy in front of it, built on github.com/jackc/pgx/v5/pgproto3. The proxy enforces Aurora DSQL's PostgreSQL-compatibility subset and emulates CREATE INDEX ASYNC and sys.jobs, so your tests catch DSQL-incompatible SQL before it reaches a real cluster.

The proxy is a Go port of MiniStack's DSQL proxy, and a parity suite keeps the two in agreement.

Install

go get github.com/cepinos/embedded-dsql

Linux and macOS on amd64 and arm64. The first Start downloads the PostgreSQL binaries (about 15 MB) from Maven Central, or the Maven repository you configure; later runs use the cache.

Usage

func TestMain(m *testing.M) {
	ctx := context.Background()
	db, err := embeddeddsql.Start(ctx, embeddeddsql.Options{})
	if err != nil {
		log.Fatal(err)
	}
	dsn = db.DSN()
	code := m.Run()
	if err := db.Stop(); err != nil {
		log.Print(err)
		code = 1
	}
	os.Exit(code)
}

func TestSomething(t *testing.T) {
	ctx := context.Background()
	conn, err := pgx.Connect(ctx, dsn)
	require.NoError(t, err)
	defer conn.Close(ctx)

	_, err = conn.Exec(ctx, `CREATE TABLE t (id serial)`)
	// ERROR: type "serial" is not supported (SQLSTATE 0A000)

	_, err = conn.Exec(ctx, `CREATE TABLE t (id uuid PRIMARY KEY, name text)`)
	require.NoError(t, err)

	var jobID string
	err = conn.QueryRow(ctx, `CREATE INDEX ASYNC t_name ON t (name)`).Scan(&jobID)
	require.NoError(t, err)
	var done bool
	err = conn.QueryRow(ctx, `SELECT sys.wait_for_job($1)`, jobID).Scan(&done)
	require.NoError(t, err)
}

database/sql works the same way through github.com/jackc/pgx/v5/stdlib.

API

func Start(ctx context.Context, opts Options) (*DB, error)

func (db *DB) DSN() string         // postgres://... of the DSQL proxy
func (db *DB) PostgresDSN() string // the PostgreSQL server itself, unvalidated
func (db *DB) Stop() error         // idempotent; leaves no process behind

type Options struct {
	CacheDir            string    // default $GOMODCACHE/../embedded-postgres
	RuntimeDir          string    // default: a fresh temp dir, removed by Stop
	Port                int       // DSN port; 0 picks a free one
	PostgresVersion     string    // default "16.14.0"; any zonky version
	BinaryRepositoryURL string    // default "https://repo1.maven.org/maven2"
	BinarySHA256        string    // hex SHA-256 of the jar; overrides the pin
	Logger              io.Writer // PostgreSQL output and proxy errors; default discarded
	Passthrough         bool      // no proxy: DSN points straight at PostgreSQL
}

The DSN uses user admin and database postgres. The proxy accepts any user and password, so a DSQL IAM auth token works as the password. PostgresDSN is there for fixtures DSQL refuses, such as TRUNCATE between tests; statements sent that way bypass the proxy's transaction and catalog tracking. Strict validation is the default; Passthrough: true turns the proxy off.

What is enforced

Both the simple and the extended query protocol (pgx, database/sql) are validated, with the SQLSTATEs and messages Aurora DSQL uses:

  • Denylist (0A000): CREATE DATABASE/TYPE/EXTENSION/TRIGGER/PROCEDURE/ TABLESPACE/RULE/AGGREGATE/DOMAIN/CAST/COLLATION/OPERATOR/PUBLICATION/ SUBSCRIPTION, materialized views, temporary and unlogged tables, foreign data wrappers, COPY, LISTEN/NOTIFY/UNLISTEN, TRUNCATE, DO, SAVEPOINT, LOCK, VACUUM, CLUSTER, REINDEX, ALTER SYSTEM, prepared transactions, functions in any language but SQL, partitioning, inheritance and EXCLUDE constraints.
  • Column types: only DSQL's types (no serial, arrays, money, inet, geometric types, ...); identity columns on bigint only; sequence and identity CACHE must be 1 or at least 65536.
  • ALTER TABLE subset: no ALTER COLUMN TYPE, no SET NOT NULL, ADD COLUMN without constraints, ADD CONSTRAINT only for CHECK/FOREIGN KEY ... NOT VALID and UNIQUE USING INDEX, ADD GENERATED ... AS IDENTITY needs CACHE, and no dropping a primary key column.
  • DEFERRABLE only on foreign keys.
  • Indexes: CREATE INDEX ASYNC only (no CONCURRENTLY, USING, partial indexes), immutable expressions only (42P17), no expressions in INCLUDE, at most 8 key columns (54011) and 24 indexes per table.
  • Locking clauses: only FOR UPDATE and FOR KEY SHARE.
  • Transactions (25006): one DDL statement per transaction, no DDL mixed with DML, at most 3,000 rows, 10 MiB and 5 minutes. A statement rejected inside a transaction aborts it (25P02 afterwards, COMMIT rolls back).
  • OC001: a transaction whose connection has not seen another session's DDL fails with 40001.
  • Async DDL: CREATE [UNIQUE] INDEX ASYNC and ALTER TABLE ASYNC ... VALIDATE CONSTRAINT return a job_id; sys.jobs and sys.wait_for_job report it. Data that violates the constraint fails the job, not the statement, as on DSQL.
Limitations

Inherited from MiniStack's proxy:

  • Async DDL completes synchronously. Over the extended protocol it runs at Execute; a statement that is parsed but never executed registers no job.
  • Parsing is regular-expression work, not a SQL parser. Statement splitting, comment stripping, parenthesis matching and the locking-clause rule are lexer-aware (literals, dollar quotes, quoted identifiers, nested comments); the rest is pattern matching.
  • The locking-clause rule is syntactic: a share-mode clause DSQL would accept as a no-op (SELECT 1 FOR SHARE) is refused anyway.
  • Foreign keys are enforced by PostgreSQL, so conflicting writes wait on a row lock where DSQL fails the loser optimistically with 40001.
  • Transaction row counting is static (VALUES tuples only); INSERT ... SELECT, UPDATE and DELETE row counts are not tracked.
  • OC001 is optimistic: the catalog version bumps when DDL is forwarded, not when it commits.
  • Isolation is PostgreSQL's (default READ COMMITTED); conflicts surface as PostgreSQL reports them, for example 40001 on a write conflict under REPEATABLE READ, not through DSQL's optimistic commit.
  • The server announces server_version 16.4 like MiniStack, while the backend runs the configured PostgreSQL version.
Known differences from MiniStack

The parity suite (below) finds no difference on its case table. By design, embedded-dsql goes further than MiniStack 1.5.9 in a few places, all outside what the parity suite sends:

  • Prepared statements that clients cache (pgx does) are re-checked at Bind against the transaction rules and a pending abort, not only at Parse.
  • sys.jobs / sys.wait_for_job accept a bound parameter (job_id = $1, sys.wait_for_job($1)); their rows are computed at Execute, so a cached statement sees new jobs; a binary-format bool is encoded as binary.
  • The primary-key probe uses to_regclass, so an unknown relation no longer aborts the client's transaction before the backend answers.
  • Proxy probes wait until the backend owes the client nothing, so a pipelining client cannot have its results swallowed.
  • Client startup parameters (application_name, search_path via options, ...) are forwarded to the backend, and a backend connection failure is reported to the client as 08006.

Binaries, cache and root

  • The PostgreSQL binaries come from the zonky embedded-postgres-binaries-<os>-<arch> jar, fetched from <BinaryRepositoryURL>/io/zonky/test/postgres/embedded-postgres-binaries-<os>-<arch>/<version>/ exactly as embedded-postgres builds it. BinaryRepositoryURL (default Maven Central) can point at a mirror or proxy; a trailing slash is ignored. Downloads use net/http defaults, so HTTPS_PROXY is honoured.

  • Any published PostgresVersion works. The jar is always verified before use, by the first rule that applies:

    1. BinarySHA256 set: the jar must match it.
    2. The version is pinned for this platform (16.14.0 on linux and darwin, amd64 and arm64, glibc and Alpine): the jar and the .txz inside must match the SHA-256s pinned in code, whatever repository served them.
    3. Otherwise: the jar must match <jar URL>.sha256 from the same repository. If that file is missing, not HTTP 200 or not a SHA-256 digest, Start fails before downloading the jar, naming the URL and the version, and asks for BinarySHA256 or a pinned version. (embedded-postgres skips verification in that case; embedded-dsql never does.)

    A mismatch fails loudly with the URL and what the checksum was expected from.

  • The archive is written atomically, under a cross-process file lock, to <CacheDir>/embedded-postgres-binaries-<os>-<arch>-<version>.txz, which is where embedded-postgres looks, so it never downloads by itself. The version is part of the name, so switching versions never reuses another archive. A small .embedded-dsql record next to the archive keeps the SHA-256 of the jar it came from and of the archive, so an unpinned version is reused without a download, and a changed BinarySHA256 downloads again. A corrupt cached archive is replaced. Binaries are extracted once per archive to <CacheDir>/extracted/. The default CacheDir sits next to the Go module cache, which CI caches usually persist; the included workflow caches it explicitly.

  • Every instance gets free ports and its own runtime directory, so parallel test binaries never collide. Stop waits for the server to exit. Start stops a server an earlier run left in an explicit RuntimeDir, and removes temporary instances whose test process died without calling Stop.

  • PostgreSQL refuses to run as root. When the tests run as root (a CI container), the binaries are extracted to a root-owned directory under the temp dir and initdb/pg_ctl are replaced by wrapper scripts that run the real binaries as nobody via setpriv or runuser (util-linux, present on the Debian-based golang images). Without either tool Start fails with a clear error. An explicit RuntimeDir must then be reachable by nobody.

  • embedded-postgres keeps a small log file per start in the temp dir.

Parity suite

parity_test.go (build tag parity) runs one SQL case table, over both wire protocols, against a MiniStack DSQL cluster and against embedded-dsql, and requires identical outcomes: SQLSTATE and message, or command tag and rows.

docker run -d --name ministack -p 4566:4566 -p 25432-25461:25432-25461 \
  -e SERVICES=dsql -e DSQL_STRICT=1 \
  -v /var/run/docker.sock:/var/run/docker.sock ministackorg/ministack:1.5.9
EMBEDDED_DSQL_MINISTACK_URL=http://127.0.0.1:4566 go test -tags parity -run Parity -v .

The suite creates a cluster through MiniStack's DSQL API and deletes it afterwards. Set EMBEDDED_DSQL_PARITY_VERBOSE=1 to log every outcome.

Ported files

These files are ports of MiniStack 1.5.9 (ministackorg/ministack:1.5.9, PyPI ministack==1.5.9) and keep MiniStack's copyright notice:

File Ported from
internal/pgproxy/lexer.go ministack/core/pgproxy.py
internal/pgproxy/validate.go ministack/core/pgproxy.py
internal/pgproxy/jobs.go ministack/core/pgproxy.py
internal/pgproxy/proxy.go ministack/core/pgproxy.py
internal/pgproxy/validate_test.go tests/test_dsql.py
parity_test.go (case table) tests/test_dsql.py

MiniStack's MIT license is reproduced in THIRD_PARTY_NOTICES.

License

MIT — see LICENSE. Files ported from MiniStack keep MiniStack's own MIT copyright notice.

Documentation

Overview

Package embeddeddsql starts a real PostgreSQL server for Go tests, without Docker, and fronts it with an in-process wire-protocol proxy that enforces the Aurora DSQL PostgreSQL-compatibility subset, so tests catch DSQL-incompatible SQL without needing a cluster.

db, err := embeddeddsql.Start(ctx, embeddeddsql.Options{})
if err != nil {
	t.Fatal(err)
}
t.Cleanup(func() {
	if err := db.Stop(); err != nil {
		t.Error(err)
	}
})
conn, err := pgx.Connect(ctx, db.DSN())

The proxy is a Go port of MiniStack's pgproxy.py (MiniStack 1.5.9). It validates both the simple and the extended query protocol and answers with the SQLSTATEs and messages Aurora DSQL uses: the denylist (extensions, triggers, TRUNCATE, temporary tables, ...), supported column types, identity columns, the ALTER TABLE subset, DEFERRABLE, CACHE values, index rules (CREATE INDEX ASYNC only, at most 8 keys, 24 indexes per table), locking clauses, the one-DDL-per-transaction and no-DDL-with-DML rules, the 3,000-row / 10 MiB / 5-minute transaction limits, OC001 schema conflicts, and dropping a primary key column. CREATE INDEX ASYNC and ALTER TABLE ASYNC ... VALIDATE CONSTRAINT return a job id, and sys.jobs and sys.wait_for_job are emulated.

Documented limitations, inherited from MiniStack:

  • CREATE INDEX ASYNC / ALTER TABLE ASYNC sent over the extended protocol run at Execute time on the connection's backend; a statement that is parsed but never executed registers no job. Jobs complete synchronously.
  • Type and name parsing is regular-expression work, not a SQL parser. Statement splitting, comment stripping, parenthesis matching and the locking-clause rule are lexer-aware (literals, dollar quotes, quoted identifiers, nested comments); the rest is pattern matching.
  • The locking-clause rule is syntactic: a share-mode clause that DSQL would accept as a no-op (SELECT 1 FOR SHARE) is refused anyway.
  • Foreign keys are enforced by PostgreSQL, so a write and a concurrent change to the referenced key wait on a row lock where DSQL resolves them optimistically and fails the loser with 40001.
  • Transaction row counting is static (VALUES tuples only); INSERT ... SELECT, UPDATE and DELETE row counts are not tracked.
  • OC001 emulation is optimistic: the catalog version bumps when DDL is forwarded, not when it commits.
  • Isolation is PostgreSQL's: the default is READ COMMITTED, and concurrency conflicts surface as PostgreSQL reports them (for example 40001 under REPEATABLE READ), not through DSQL's optimistic commit.

Linux and macOS on amd64 and arm64 are supported.

Index

Constants

View Source
const DefaultBinaryRepositoryURL = "https://repo1.maven.org/maven2"

DefaultBinaryRepositoryURL is the Maven repository the PostgreSQL binaries come from when Options.BinaryRepositoryURL is empty: Maven Central, which hosts the zonky embedded-postgres-binaries jars that github.com/fergusstrange/embedded-postgres consumes.

View Source
const DefaultPostgresVersion = "16.14.0"

DefaultPostgresVersion is the PostgreSQL version started when Options.PostgresVersion is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is a running embedded DSQL instance.

func Start

func Start(ctx context.Context, opts Options) (*DB, error)

Start downloads (once, verified by checksum) and starts a PostgreSQL server, then fronts it with the DSQL validation proxy. Call Stop when done; Stop leaves no process behind.

func (*DB) DSN

func (db *DB) DSN() string

DSN is the connection string clients use: the DSQL proxy, or PostgreSQL itself when Options.Passthrough is set. Any user and password are accepted by the proxy, so a DSQL IAM token works as the password.

func (*DB) PostgresDSN

func (db *DB) PostgresDSN() string

PostgresDSN points straight at the backing PostgreSQL server, bypassing DSQL validation. Use it for test fixtures DSQL refuses, such as TRUNCATE between tests; statements sent this way do not update the proxy's transaction or catalog tracking.

func (*DB) Stop

func (db *DB) Stop() error

Stop closes the proxy and every client connection, stops PostgreSQL and removes the temporary runtime directory. It is safe to call more than once.

type Options

type Options struct {
	// CacheDir holds the downloaded PostgreSQL archive and the extracted
	// binaries. Default: $GOMODCACHE/../embedded-postgres (for example
	// ~/go/pkg/embedded-postgres), falling back to the user cache dir.
	CacheDir string
	// RuntimeDir holds this instance's data directory. Default: a fresh
	// temporary directory, removed by Stop. An explicit RuntimeDir is reused:
	// Start stops any server an earlier run left running there.
	RuntimeDir string
	// Port is the loopback port of the DSN. 0 picks a free port.
	Port int
	// PostgresVersion is the zonky embedded-postgres-binaries version, any
	// version published to the repository. Default: DefaultPostgresVersion.
	// Versions with a pinned checksum are verified against the pin; others
	// against BinarySHA256 or the repository's <jar>.sha256 file.
	PostgresVersion string
	// BinaryRepositoryURL is the root of the Maven repository serving the
	// zonky jars (a Maven Central mirror or proxy). Default:
	// DefaultBinaryRepositoryURL. A trailing slash is ignored. Pinned
	// versions are still verified against the pin.
	BinaryRepositoryURL string
	// BinarySHA256 is the hex SHA-256 of the binaries jar. When set, the jar
	// is verified against it instead of the pinned checksum or the
	// repository's .sha256 file.
	BinarySHA256 string
	// Logger receives PostgreSQL's output and the proxy's connection errors.
	// Default: discarded.
	Logger io.Writer
	// Passthrough skips the DSQL proxy: DSN points straight at PostgreSQL
	// and nothing is validated. The default is strict DSQL validation.
	Passthrough bool
}

Options configures Start. The zero value is ready to use.

Directories

Path Synopsis
internal
pgproxy
Package pgproxy is a PostgreSQL wire-protocol proxy that enforces the Aurora DSQL PostgreSQL-compatibility subset in front of a real PostgreSQL server.
Package pgproxy is a PostgreSQL wire-protocol proxy that enforces the Aurora DSQL PostgreSQL-compatibility subset in front of a real PostgreSQL server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址