Files
turso/postgres/conformance/with-postgres.sh
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

64 lines
2.1 KiB
Bash
Executable File

#!/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
"$@"