Files
turso/postgres/CONTRIBUTING.md
Claude c4ff6b5fdd postgres/conformance: run the pg-sqltests on PostgreSQL in CI
`make -C postgres/conformance run-postgres` runs the corpus with the
postgres backend. It uses the server at PG_DSN. If PG_DSN is not set,
with-postgres.sh starts a throwaway PostgreSQL container with Docker and
gives it a role that has CREATEDB and is not a superuser. A new CI job,
"Run SQL tests (PostgreSQL)", runs the target.

The container keeps its data on tmpfs and runs with fsync,
synchronous_commit, full_page_writes, autovacuum and JIT off, and with
wal_level=minimal. These settings do not change query results. The
target runs two tests per CPU, because a test often waits for the
server. With the LATERAL tests of #9468, 848 tests take about 6 s on 4
CPUs, against about 60 s with the default settings.

The PostgreSQL version is one line in with-postgres.sh, and
POSTGRES_VERSION changes it for one run. The image comes from
mirror.gcr.io, because Docker Hub limits the pull rate of anonymous
clients.

40 of the 187 tests failed on PostgreSQL 17. They check Turso-specific
behavior: the EXPLAIN output, the catalogs that Turso keeps empty, and
the error for a write to a catalog table. Three of them fail because
Turso has the ACLs of PostgreSQL 16, without the MAINTAIN privilege of
PostgreSQL 17. Mark them with @skip-if postgres, with a reason that
tells what PostgreSQL does.

Tests: make -C postgres/conformance run-postgres, which failed 40 tests
before the skips; make -C postgres/conformance run

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R3hzXhfjBgyLPUF7vd3PHW
2026-10-15 03:04:08 +00:00

4.4 KiB

Contributing to the Turso Postgres Front-End

This is a PostgreSQL front-end for Turso: tursopg speaks the PostgreSQL wire protocol, parses SQL with the real PostgreSQL grammar (libpg_query), and runs queries through the turso_core engine. The goal is to behave like a real PostgreSQL server, so most of the work is closing the gap between what real PostgreSQL does and what the front-end does today.

Three things keep that work honest:

  • COMPAT.md — feature-by-feature compatibility status, and where Turso intentionally diverges from PostgreSQL.
  • conformance/upstream/ — regression tests imported verbatim from the PostgreSQL source tree, run against the front-end so divergence can't sneak in unnoticed.
  • conformance/pg-sqltests/ — .sqltest tests that CI runs against the front-end and against a real PostgreSQL server (make -C postgres/conformance run-postgres). Tests of Turso-specific behavior have @skip-if postgres "reason".

Verification workflow

When changing the parser, translator, or query behavior:

  1. Check the relevant page of the PostgreSQL documentation.
  2. Confirm the behavior against a real PostgreSQL server.
  3. Encode the observed behavior in a test — an upstream regression script that now passes, a .sqltest in conformance/pg-sqltests/, or a Rust test (see below) — so it can't regress.

If Turso intentionally diverges from PostgreSQL, document that in COMPAT.md. Pay particular attention to clauses that parse but are silently ignored: the parser accepts the full PostgreSQL grammar, so a feature the translator drops produces wrong results, not an error.

Running the upstream regression suite

The upstream regression suite is the day-to-day test loop. Run it with:

postgres/conformance/run.py                    # whole corpus
postgres/conformance/run.py boolean            # single test by name
postgres/conformance/run.py --max-diff-lines 0 boolean   # full diffs

This builds tursopg and the pgregress runner, starts a server on a fresh temporary database, runs every .sql script under conformance/upstream/, and byte-compares the psql transcript against the paired .out file. Failed tests write the actual transcript and a unified diff to postgres/regress/results/.

To drive an already-running server instead:

cargo run -p tursopg -- --server 127.0.0.1:5432 /tmp/regress.db &
cargo run -p turso_pg_regress -- --dsn 'postgres://127.0.0.1:5432/regression'

The corpus is pristine

Everything under conformance/upstream/ is imported verbatim from the PostgreSQL source tree (src/test/regress/sql/ and expected/). Never edit these files to match Turso behavior — a failing diff is the to-do list, not a bug in the test. Only touch the corpus to import new files or sync with upstream.

To grow the corpus, copy a <name>.sql script and its <name>.out expected transcript from the same PostgreSQL release into conformance/upstream/. New files are picked up automatically — no registration step. Prefer scripts with few dependencies on other tests; the upstream suite shares one database, and some scripts assume tables created by test_setup.sql.

The runner (postgres/regress/) plays the role of upstream's pg_regress: it speaks the wire protocol directly and reproduces psql's transcript output byte-for-byte (echoed input, aligned tables, LINE n: error markers). If a corpus file needs a psql feature the runner doesn't emulate yet (see the module docs in postgres/regress/main.rs for known gaps), extend the runner — never adapt the corpus. If every test suddenly fails with uniform, systematic diffs, suspect the runner's psql emulation before suspecting the server.

Rust tests

Focused tests that don't fit the transcript format live in Rust:

  • postgres/parser/tests/ — parse/translate coverage (valid and invalid SQL).
  • postgres/tests/integration/ — end-to-end coverage by area (catalog, COPY, dialect, sequences, ...), driven through the front-end API.

Run them with:

cargo test -p turso_pg_parser -p turso_pg_tests

Every behavior change needs a test that fails without the change and passes with it. Upstream regression scripts double as tests here: if your change shrinks a .diff under postgres/regress/results/, say so in the commit message.