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