diff --git a/.github/workflows/sqltest.yml b/.github/workflows/sqltest.yml index fb9a9ecd1..68e22d708 100644 --- a/.github/workflows/sqltest.yml +++ b/.github/workflows/sqltest.yml @@ -37,7 +37,7 @@ jobs: - name: Check pg test syntax run: make -C postgres/conformance check - sqltest-run-cli: + sqltest-cli: name: Run SQL tests (CLI backend) runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 30 @@ -57,7 +57,7 @@ jobs: - name: Run tests (MVCC) run: make -C sqlite/conformance run-cli MVCC=1 - sqltest-run-rust: + sqltest-rust: name: Run SQL tests (Rust backend) runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 30 @@ -74,7 +74,7 @@ jobs: - name: Run tests (MVCC) run: make -C sqlite/conformance run-rust MVCC=1 - sqltest-run-js: + sqltest-js: name: Run SQL tests (JS backend, ${{ matrix.mode }}) runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 30 @@ -123,7 +123,7 @@ jobs: working-directory: sqlite/conformance run: make run-js MVCC=1 - sqltest-run-tursopg: + sqltest-tursopg: name: Run SQL tests (tursopg backend) runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 30 @@ -140,6 +140,21 @@ jobs: - name: Run tests run: make -C postgres/conformance run + sqltest-postgres: + name: Run SQL tests (PostgreSQL) + runs-on: blacksmith-4vcpu-ubuntu-2404 + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: useblacksmith/rust-cache@v3 + with: + prefix-key: "v1-rust" + save-if: ${{ github.ref == 'refs/heads/main' }} + - name: Build test runner + run: make -C postgres/conformance build-runner + - name: Run tests against PostgreSQL + run: make -C postgres/conformance run-postgres + sqltest-sqlite: name: Run SQL tests (SQLite) runs-on: blacksmith-4vcpu-ubuntu-2404 diff --git a/AGENTS.md b/AGENTS.md index c9079f11c..d3f550aa8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -29,6 +29,7 @@ scripts/diff.sh "SQL" [label] # compare sqlite3 vs tursodb output - `make test-single TEST=foo.test` - single legacy TCL test - `make -C sqlite/conformance run-rust ARGS='--snapshot-filter __never__'` - preferred `.sqltest` runner for new coverage - `CI=1 make -C sqlite/conformance run-rust` - only when snapshot tests are required +- `make -C postgres/conformance run-postgres` - run the PostgreSQL `.sqltest` corpus against a real PostgreSQL server (needs Docker, or a server at `PG_DSN`) ### Test Organization @@ -37,7 +38,7 @@ Default: add coverage to the narrowest existing test harness that can express th - `sqlite/conformance/sqlite-sqltests/` - preferred for SQL conformance coverage. These tests run the same scenario against both Turso and SQLite, so use them first for parser, planner, executor, and SQL semantics work that fits the `.sqltest` DSL. - `tests/integration/` - primary fallback when the behavior cannot be expressed cleanly in `.sqltest`. Put API-level regressions, multi-connection orchestration, storage assertions, injected failures, timeout behavior, and other Rust-driven scenarios here. - `sqlite/conformance/upstream/` - imported upstream SQLite golden tests. Do not modify these for Turso behavior changes; use them as fixed compatibility coverage, and only touch them for intentional upstream sync or harness maintenance. -- `postgres/conformance/pg-sqltests/` - `.sqltest` coverage for the PostgreSQL frontend, run via `make -C postgres/conformance run` (spawns a tursopg server per test and drives it over the wire protocol). Only assert behavior real PostgreSQL also exhibits, so the corpus stays valid for differential runs. +- `postgres/conformance/pg-sqltests/` - `.sqltest` coverage for the PostgreSQL frontend. - `testing/cli_tests/` - CLI-focused Python coverage for shell behavior and end-to-end command workflows. - `tests/fuzz/` - minimized fuzz regressions and targeted edge cases that are easier to keep as Rust tests. - `testing/simulator/` and `testing/concurrent-simulator/` - deterministic concurrency, scheduling, and failure-injection coverage for state-machine and I/O correctness. diff --git a/postgres/CONTRIBUTING.md b/postgres/CONTRIBUTING.md index 67caa55b7..00f67512d 100644 --- a/postgres/CONTRIBUTING.md +++ b/postgres/CONTRIBUTING.md @@ -6,13 +6,17 @@ 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. -Two things keep that work honest: +Three things keep that work honest: - **[`COMPAT.md`](COMPAT.md)** — feature-by-feature compatibility status, and where Turso intentionally diverges from PostgreSQL. - **[`conformance/upstream/`](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/`](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 @@ -21,7 +25,8 @@ 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, or a Rust test (see below) — so it can't regress. + 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`](COMPAT.md). Pay particular attention to clauses that parse but diff --git a/postgres/conformance/Makefile b/postgres/conformance/Makefile index 3bfb7cbd5..65906ac66 100644 --- a/postgres/conformance/Makefile +++ b/postgres/conformance/Makefile @@ -1,8 +1,6 @@ # Turso PostgreSQL conformance test Makefile # -# The .sqltest corpora live here; the sqltest runner crate lives in -# testing/sqltest. The tursopg backend spawns a tursopg server per database -# instance and drives it over the PostgreSQL wire protocol. +# The .sqltest corpora live here; the sqltest runner crate lives in testing/sqltest. # Paths ROOT_DIR := $(shell cd ../.. && pwd) @@ -25,7 +23,10 @@ SQLTEST := cargo run --manifest-path $(RUNNER_DIR)/Cargo.toml --bin sqltest $(CA # Test corpora (relative to this directory) PG_TESTS := pg-sqltests -.PHONY: all build build-tursopg build-runner check run test run-one run-upstream clean +# 2 * num_cpu (arbitrary multiplier) +POSTGRES_JOBS ?= $(shell expr 2 \* $$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 2)) + +.PHONY: all build build-tursopg build-runner check run test run-one run-postgres run-one-postgres run-upstream clean # Build everything all: build @@ -56,6 +57,16 @@ test: run run-one: build $(SQLTEST) run $(FILE) --backend tursopg --binary $(TURSOPG) +# Run all tests against a real PostgreSQL server. Uses the server at PG_DSN, +# or starts a throwaway Docker container (see with-postgres.sh). +run-postgres: build-runner + ./with-postgres.sh sh -c '$(SQLTEST) run $(PG_TESTS) --jobs $(POSTGRES_JOBS) --backend postgres --postgres-dsn "$$PG_DSN"' + +# Run a single test file against a real PostgreSQL server: +# make run-one-postgres FILE=pg-sqltests/select.sqltest +run-one-postgres: build-runner + ./with-postgres.sh sh -c '$(SQLTEST) run $(FILE) --jobs $(POSTGRES_JOBS) --backend postgres --postgres-dsn "$$PG_DSN"' + # Run the upstream PostgreSQL regress corpus with pass/fail statuses. # Exit 0 means every test matched its status in run.py's STATUS table; # regressions in blessed tests or unblessed new passes fail the run. diff --git a/postgres/conformance/pg-sqltests/catalog.sqltest b/postgres/conformance/pg-sqltests/catalog.sqltest index 93d18ee8a..9d7cf947a 100644 --- a/postgres/conformance/pg-sqltests/catalog.sqltest +++ b/postgres/conformance/pg-sqltests/catalog.sqltest @@ -152,6 +152,7 @@ expect { 1|updated } +@skip-if postgres "PostgreSQL fills pg_init_privs and pg_cast" test pg-function-support-catalogs-are-empty { SET search_path TO ''; SELECT * FROM pg_init_privs; @@ -169,16 +170,19 @@ test pg-function-support-catalogs-are-empty { } expect {} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-init-privs-is-read-only { DELETE FROM pg_init_privs; } expect error {cannot delete from pg_catalog table "pg_init_privs"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-cast-is-read-only { DELETE FROM pg_cast; } expect error {cannot delete from pg_catalog table "pg_cast"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-transform-is-read-only { DELETE FROM pg_transform; } @@ -195,6 +199,7 @@ expect { pg_is_in_recovery|pg_catalog } +@skip-if postgres "PostgreSQL also finds the functions of information_schema" test pg-dump-function-discovery-excludes-builtins { SET search_path TO ''; SELECT p.tableoid, p.oid, p.proname, p.prolang, p.pronargs, p.proargtypes, @@ -215,6 +220,7 @@ test pg-dump-function-discovery-excludes-builtins { } expect {} +@skip-if postgres "PostgreSQL has six abs() functions, and pg_is_in_recovery() has the language internal" test pg-object-native-function-languages { SET search_path TO ''; SELECT p.proname, l.lanname, CASE WHEN l.lanispl THEN 1 ELSE 0 END, l.tableoid @@ -226,6 +232,7 @@ expect { pg_is_in_recovery|c|0|2612 } +@skip-if postgres "PostgreSQL has the plpgsql language" test pg-object-dump-language-discovery-is-empty { SET search_path TO ''; SELECT tableoid, oid, lanname, lanpltrusted, lanplcallfoid, laninline, @@ -234,6 +241,7 @@ test pg-object-dump-language-discovery-is-empty { } expect {} +@skip-if postgres "PostgreSQL fills these catalogs, and only a superuser can read pg_subscription" test pg-object-unsupported-catalogs-are-empty { SET search_path TO ''; SELECT tableoid FROM pg_operator; @@ -301,86 +309,103 @@ test pg-object-unsupported-catalogs-are-empty { } expect {} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-operator-is-read-only { DELETE FROM pg_operator; } expect error {cannot delete from pg_catalog table "pg_operator"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-opclass-is-read-only { DELETE FROM pg_opclass; } expect error {cannot delete from pg_catalog table "pg_opclass"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-opfamily-is-read-only { DELETE FROM pg_opfamily; } expect error {cannot delete from pg_catalog table "pg_opfamily"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-ts-parser-is-read-only { DELETE FROM pg_ts_parser; } expect error {cannot delete from pg_catalog table "pg_ts_parser"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-ts-template-is-read-only { DELETE FROM pg_ts_template; } expect error {cannot delete from pg_catalog table "pg_ts_template"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-ts-dict-is-read-only { DELETE FROM pg_ts_dict; } expect error {cannot delete from pg_catalog table "pg_ts_dict"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-ts-config-is-read-only { DELETE FROM pg_ts_config; } expect error {cannot delete from pg_catalog table "pg_ts_config"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-foreign-data-wrapper-is-read-only { DELETE FROM pg_foreign_data_wrapper; } expect error {cannot delete from pg_catalog table "pg_foreign_data_wrapper"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-foreign-server-is-read-only { DELETE FROM pg_foreign_server; } expect error {cannot delete from pg_catalog table "pg_foreign_server"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-default-acl-is-read-only { DELETE FROM pg_default_acl; } expect error {cannot delete from pg_catalog table "pg_default_acl"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-conversion-is-read-only { DELETE FROM pg_conversion; } expect error {cannot delete from pg_catalog table "pg_conversion"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-range-is-read-only { DELETE FROM pg_range; } expect error {cannot delete from pg_catalog table "pg_range"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-event-trigger-is-read-only { DELETE FROM pg_event_trigger; } expect error {cannot delete from pg_catalog table "pg_event_trigger"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-subscription-is-read-only { DELETE FROM pg_subscription; } expect error {cannot delete from pg_catalog table "pg_subscription"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-largeobject-metadata-is-read-only { DELETE FROM pg_largeobject_metadata; } expect error {cannot delete from pg_catalog table "pg_largeobject_metadata"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-amop-is-read-only { DELETE FROM pg_amop; } expect error {cannot delete from pg_catalog table "pg_amop"} +@skip-if postgres "different error message: PostgreSQL reports permission denied for the table" test pg-object-amproc-is-read-only { DELETE FROM pg_amproc; } @@ -443,6 +468,7 @@ expect { 2608 } +@skip-if postgres "Turso has the ACLs of PostgreSQL 16, without the MAINTAIN privilege (m) of PostgreSQL 17" test pg-depend-for-dump-table-query { CREATE TABLE dump_indexed (id INTEGER PRIMARY KEY, value TEXT UNIQUE); CREATE TABLE dump_plain (value TEXT); diff --git a/postgres/conformance/pg-sqltests/explain.sqltest b/postgres/conformance/pg-sqltests/explain.sqltest index 251c014c3..2bcabf841 100644 --- a/postgres/conformance/pg-sqltests/explain.sqltest +++ b/postgres/conformance/pg-sqltests/explain.sqltest @@ -3,6 +3,8 @@ # Simple EXPLAIN is intentionally mapped to SQLite's EXPLAIN QUERY PLAN. # PostgreSQL EXPLAIN options are rejected until their output semantics exist. +@skip-file-if postgres "EXPLAIN shows the Turso query plan, and Turso rejects EXPLAIN options that PostgreSQL accepts" + @database :memory: setup explain_table { diff --git a/postgres/conformance/pg-sqltests/functions.sqltest b/postgres/conformance/pg-sqltests/functions.sqltest index 845382cf0..839e78c3f 100644 --- a/postgres/conformance/pg-sqltests/functions.sqltest +++ b/postgres/conformance/pg-sqltests/functions.sqltest @@ -203,6 +203,7 @@ test pg-get-expr-null-oid-does-not-hide-invalid-expression { } expect error {} +@skip-if postgres "PostgreSQL prints (val > 0), and also lists the CHECK constraints of the information_schema domains" test pg-get-expr-check-constraint { CREATE TABLE checked (val int CHECK (val > 0)); SELECT pg_get_expr(conbin, conrelid) FROM pg_constraint WHERE contype = 'c'; @@ -229,6 +230,7 @@ expect { 1 } +@skip-if postgres "Turso has the ACLs of PostgreSQL 16, without the MAINTAIN privilege (m) of PostgreSQL 17" test acldefault-object-kinds { SELECT pg_catalog.acldefault('c', 4294967294); SELECT acldefault('r', 4294967294); @@ -271,6 +273,7 @@ expect { 2615|2200|public|{owner=UC/owner} } +@skip-if postgres "Turso has the ACLs of PostgreSQL 16, without the MAINTAIN privilege (m) of PostgreSQL 17" test acldefault-first-byte-selects-object-kind { SELECT acldefault('rr', 4294967294); SELECT acldefault('r ', 4294967294); diff --git a/postgres/conformance/with-postgres.sh b/postgres/conformance/with-postgres.sh new file mode 100755 index 000000000..be416ea2e --- /dev/null +++ b/postgres/conformance/with-postgres.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# Runs a command with PG_DSN set to the URL of a PostgreSQL server. +# +# ./with-postgres.sh sh -c 'echo "$PG_DSN"' +# +# If PG_DSN is already set, the command uses that server. Otherwise this +# script starts a throwaway PostgreSQL container with Docker, runs the +# command, and removes the container. +# +# To test against another PostgreSQL version, set POSTGRES_VERSION, for +# example POSTGRES_VERSION=18. To upgrade the version for everyone, change +# the default below. +set -euo pipefail + +POSTGRES_VERSION="${POSTGRES_VERSION:-17}" +POSTGRES_IMAGE="${POSTGRES_IMAGE:-mirror.gcr.io/library/postgres:$POSTGRES_VERSION}" + +if [[ -n "${PG_DSN:-}" ]]; then + exec "$@" +fi + +if ! docker info >/dev/null 2>&1; then + echo "error: PG_DSN is not set and Docker is not running." >&2 + echo "Start Docker, or set PG_DSN to a PostgreSQL server, for example" >&2 + echo "PG_DSN=postgres://sqltest@127.0.0.1:5432/postgres." >&2 + exit 2 +fi + +container=$(docker run --detach \ + --env POSTGRES_HOST_AUTH_METHOD=trust \ + --env PGDATA=/pgdata/data \ + --tmpfs /pgdata \ + --publish 127.0.0.1::5432 \ + "$POSTGRES_IMAGE" \ + -c fsync=off \ + -c synchronous_commit=off \ + -c full_page_writes=off \ + -c wal_level=minimal \ + -c max_wal_senders=0 \ + -c autovacuum=off \ + -c jit=off \ + -c max_connections=500) +trap 'docker rm --force --volumes "$container" >/dev/null' EXIT + +for _ in $(seq 1 300); do + if docker exec "$container" pg_isready --host 127.0.0.1 --username postgres >/dev/null 2>&1; then + break + fi + sleep 0.1 +done +if ! docker exec "$container" pg_isready --host 127.0.0.1 --username postgres >/dev/null 2>&1; then + echo "error: PostgreSQL in container $container did not start:" >&2 + docker logs "$container" >&2 + exit 1 +fi + +docker exec "$container" psql --quiet --username postgres \ + --command "CREATE ROLE sqltest LOGIN CREATEDB" >/dev/null + +port=$(docker port "$container" 5432/tcp | head -n 1 | sed 's/.*://') +export PG_DSN="postgres://sqltest@127.0.0.1:${port}/postgres" +echo "Using $POSTGRES_IMAGE at $PG_DSN" >&2 +"$@"