Tasks/Software Engineering/Cross-Language Porting

PostgreSQL 18 wire-compatible server on SQLite (Zig)

Reimplement the PostgreSQL server over SQLite storage, in Zig

pg_wire_sqlite Software Engineering Cross-Language Porting
instruction.mdthis is what the agent is given

Implement a Zig program that stands in for PostgreSQL 18's server-side executables, using SQLite as the underlying storage engine.

The verifier baseline is pinned to PostgreSQL 18.4.

After your run ends, the verifier runs PostgreSQL's own regression suite and cluster-lifecycle tests against your implementation.

Required compatibility:

  • A PostgreSQL 18 client must not be able to tell that it is talking to anything other than a real PostgreSQL 18 instance.
  • If a normal client, driver, ORM, migration tool, or test harness can detect that your server is not PostgreSQL 18 through the public API surface it uses, that is a failure in spirit.

Setup

  1. Your Zig workspace is /app/postgres-sqlite.
  2. PostgreSQL 18 documentation is available offline at /reference/postgresql-docs/ (start at index.html; w3m is installed).
  3. PostgreSQL's psql client (18.4) is installed.
  4. A visible test battery lives at /app/tests_dev/ — see "Scoring" below.

Deliverable

A buildable Zig project in /app/postgres-sqlite.

Build is driven by /app/postgres-sqlite/build.sh, which must use zig build-exe directly — do not rely on zig build inside the container. If you need extra compile or link flags, put them in build.sh so the visible tests and the verifier build exactly the same way.

  • visible tests build with: bash ./build.sh -Doptimize=ReleaseSafe
  • the verifier builds with: bash ./build.sh -Doptimize=ReleaseFast

The verifier then locates your executable under zig-out/bin/ and uses it as a multi-call executable, symlinking it to PostgreSQL server utility names:

  • postgres
  • initdb
  • pg_ctl

Design your program to dispatch on argv[0] (or an equivalent mechanism) so all three entry points work from one binary.

Hidden verification

The verifier receives PostgreSQL 18.4 regression tests and cluster-lifecycle tests that you cannot access during the run. It will:

  1. Reconstruct a PostgreSQL 18.4-style test harness from the hidden test bundle and packaged PostgreSQL 18.4 support files.
  2. Use packaged PostgreSQL 18.4 binaries for the client/admin tools (psql, etc.) and build hidden support artifacts as needed.
  3. Replace the server-side entrypoints with your binary.
  4. Run the regression suite — each test is a .sql script fed to psql -X -a -q -d <db> -v HIDE_TABLEAM=on -v HIDE_TOAST_COMPRESSION=on, whose output is compared against PostgreSQL's own expected output.
  5. Run cluster-lifecycle tests, which create temporary clusters using your initdb, pg_ctl, and postgres compatibility surface.

Your score is the combined pass rate across those hidden tests.

Scoring

Your score is the pass rate over a pooled set of graded units from three layers:

score = (compat_passed + regression_files_passed + tap_files_passed)
        ───────────────────────────────────────────────────────────
        (all compat checks + all regression files + all TAP files)
  • compat — many small, individual wire-compatibility checks (does SELECT 1 return 1, does SHOW server_version work, does a bad query raise the right SQLSTATE, …). Each is one graded unit. These are the easiest points and the first thing to make work.
  • regression — PostgreSQL 18.4's own regression test scripts, counted per file: a whole .sql feature area counts only if its entire output matches PostgreSQL's expected output. Getting most of a file right earns nothing on this layer — you must make an entire feature area behave exactly like PostgreSQL.
  • tap — PostgreSQL 18.4's own TAP tests, run with the real prove / PostgreSQL::Test::Cluster framework, counted per test file. These drive your initdb/pg_ctl/postgres to stand up and manage real clusters.

Every unit counts equally. The compat layer is where a partial server scores; the regression and TAP layers are hard and reward complete, exact feature areas.

Visible vs hidden. /app/tests_dev/ lets you self-check with ./selfcheck.sh, using the exact harness the verifier uses: the compat checks (all visible — they are a public smoke layer) and a visible slice of the regression suite, chosen to span every hidden feature area. The graded set is disjoint and much larger (the full hidden regression suite plus the TAP suite). Matching PostgreSQL's actual behavior is the only thing that generalizes.

What you can use

  • Zig and the Zig standard library
  • Your own code inside /app/postgres-sqlite
  • SQLite (libsqlite3 and its headers are installed)
  • The installed psql client for local testing
  • The offline PostgreSQL 18 documentation
  • Basic system libraries (libc, sqlite3)

What you cannot use

  • PostgreSQL source code during the task
  • PostgreSQL's regression or TAP test bundles beyond the visible slice
  • External Zig packages
  • Any dependency that implements PostgreSQL wire compatibility for you
  • Wrapping, embedding, exec'ing, or proxying to a real PostgreSQL server (no PostgreSQL server binary exists in either environment)
  • Network access at verification time

Working notes

  • psql will not give you a prompt until the startup handshake is right: StartupMessage → AuthenticationOk → ParameterStatus (server_version, client_encoding, DateStyle, standard_conforming_strings, integer_datetimes) → BackendKeyData → ReadyForQuery.
  • The regression tests are fed through psql -a, so your protocol messages drive psql's own output formatting — get RowDescription (including correct type OIDs, type lengths and typmods) and DataRow right and formatting follows. Reporting everything as text will diverge from the expected output on alignment and on client-side type handling.
  • Error compatibility is graded: SQLSTATE codes, severity, and message text all appear in expected output.
  • Both the extended query protocol and the simple query protocol are exercised; psql uses extended protocol for parameterized paths.
  • Keep the build green and a server startable at all times. Broad partial compatibility scores; an ambitious rewrite that does not build scores zero.

Work autonomously; do not stop to ask for input.

Metric

pooled pass rate over the hidden graded units · higher is better

75 compat checks + 163 regression files + 120 TAP files; a file counts only if its whole output matches

anchorheld-outreward
no working server (fail-closed)0 / 3580.00
all 75 compat checks, nothing else75 / 3580.21
real PostgreSQL 18.4358 / 3581.00
normalisation
build or run error0
otherwise(C + R + T) / (75 + 163 + 120)

C compat checks, R whole-file regression passes, T whole-file TAP passes. No anchors and no curve: reward is the raw pooled rate.

Rollouts

312 minwall clock
$109.52spend
170.6Mtokens
43versions, 43 kept
0 0.2 0.4 0.6 0.8 $0 $25 $50 $75 $100 cumulative spend on the run visible-slice pooled pass rate (n/116), higher is better v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42
keptrevertedno scoreturning point
  1. v0Inherited placeholder; builds and passes nothing01 min · $0.41
  2. v1Multicall initdb/pg_ctl/postgres, wire protocol, SQLite storage and SQL mappingOne binary serving the whole PostgreSQL surface (protocol, catalog, SQL) over SQLite, written from scratch.no full self-check14 min · $2.17
  3. v2Rewrite PostgreSQL column aliases on VALUES subqueries into SQLite formno full self-check15 min · $2.42
  4. v3SQL_ASCII/no-NUMA capability reporting, function DDL, stack-limit SQLSTATETake one official regression file, make its whole output match exactly, snapshot, repeat. The loop for 30 more versions.no full self-check19 min · $3.64
  5. v4generate_series CTE rewrite, table-option stripping, serial identity, VACUUMno full self-check23 min · $4.89
  6. v5lseg type: OID, parser, canonical output, pg_input_is_validno full self-check26 min · $5.97
  7. v6oid type: unsigned 32-bit parse and wrap, range errors, comparisonsno full self-check28 min · $6.73
  8. v7time type: parsing, typmod precision, 24:00 carry, EXTRACT fieldsno full self-check34 min · $9.42
  9. v8name type: 63-byte truncation and a reusable SQL identifier parserno full self-check38 min · $11.45
  10. v9Narrow the name 'literal' recognizer that swallowed SELECT name FROMno full self-check40 min · $12.55
  11. v10Persistent SQLite role catalog, CREATE/ALTER/DROP ROLE, pg_authidno full self-check42 min · $14.03
  12. v11System views: memory contexts, wait events, locks, WAL; DECLARE/FETCHno full self-check44 min · $14.93
  13. v12Encoding reporting made consistent so SQL_ASCII takes the non-UTF8/no-ICU pathSatisfy encoding-dependent files by making the cluster take PostgreSQL's own skip branch, not by implementing them.no full self-check46 min · $15.94
  14. v13Restrict one-column catalog replies so they stop impersonating psql \d outputno full self-check49 min · $17.98
  15. v14Fix extended Parse/Bind/Describe/Execute: parameter OIDs, real descriptionsno full self-check52 min · $19.54
  16. v15HASH partition DDL rewrite and satisfies_hash_partition validationno full self-check54 min · $20.52
  17. v16char(n) semantics, expression headings, GROUP BY ordinal validationno full self-check60 min · $22.13
  18. v17Catalog projections for dependency, toast, primary-key and storage sanityno full self-check64 min · $23.02
  19. v18Shell and custom type DDL, stateful casts, cast resolution errors, cascade0.77586267 min · $23.82
  20. v19Pinned PostgreSQL 18.4 catalog foreign-key registry, DO-loop notices0.78448383 min · $28.96
  21. v20point type: parse, canonicalise, underflow-safe distance, containment0.79310390 min · $31.72
  22. v21GIN access-method DDL, pending lists, bitmap plans, recheck summaries0.80172495 min · $33.97
  23. v22Transactional text COPY FROM file; populates the onek and tenk1 fixtures0.80172498 min · $36.03
  24. v23DISTINCT ON translated to window functions, with ORDER BY validation0.810345103 min · $38.20
  25. v24Recursive INHERITS, LIST partitions, set operations, LATERAL, temp shadowing0.818966129 min · $44.42
  26. v25Utility-command acknowledgements, inheritance merge notices, FROM ONLY0.818966133 min · $45.33
  27. v26Persisted schemas, transactional search_path, failed-transaction COMMIT0.827586140 min · $47.39
  28. v27Typed relation options for heap, TOAST and btree with bounds diagnostics0.836207146 min · $49.10
  29. v28UUID OID, 20k-row sort fixture, NULL ordering, scroll cursors, mark/restore0.844828155 min · $52.06
  30. v29Variadic num_nulls and num_nonnulls; lexical path canonicalisation0.844828159 min · $53.82
  31. v30Backend memory logging, function privileges, file/stat/directory APIs0.844828162 min · $55.45
  32. v31Planner-support plans, control-file SRFs, WAL filename arithmetic, relpath0.853448166 min · $57.52
  33. v32INCLUDE-index semantics and the PostgreSQL 18 catalog dialogue for psql \d0.862069186 min · $63.21
  34. v33Compound CREATE SCHEMA validation, AUTHORIZATION CURRENT_ROLE, cascade notices0.862069204 min · $67.63
  35. v34CopyIn wire state machine: CopyData, CopyDone, CopyFail, column lists0.862069215 min · $71.82
  36. v35Stateful RETURNING rows for serial defaults, multi-row DML, subplans0.862069222 min · $75.02
  37. v36RETURNING through rewrite rules, updatable views, whole-row values0.862069228 min · $77.26
  38. v37RETURNING OLD/NEW aliases, cross-partition TID mapping, pg_get_functiondef0.87069241 min · $83.87
  39. v38Identity columns: creation, generated-always insert and update, \d dialogue0.87069252 min · $87.00
  40. v39Identity across COPY, views, partitions, MERGE; generalised psql \d+0.87931267 min · $91.70
  41. v40Materialised views: populated state, refresh snapshots, dependency errors0.87931271 min · $93.75
  42. v41Matview refresh edge cases, privileges, psql \d+ definition rendering0.887931285 min · $99.86
  43. v42float8: IEEE specials, degree trigonometry, Ryu decimal rendering0.896552310 min · $108.33

The full self-check ran only at v18, v23 and v32 (90, 94, 100 of 116); the other points are the log's projection, which those three confirmed.

On the hidden set

held-out metricreward
no working server (fail-closed)0 / 3580.00
all 75 compat checks, nothing else75 / 3580.21
real PostgreSQL 18.4358 / 3581.00
this run80 / 3580.2235