fix(catalog): synthesize Postgres system columns (xmin, ctid, ...) (#3742)#4459
Open
luongs3 wants to merge 1 commit into
Open
fix(catalog): synthesize Postgres system columns (xmin, ctid, ...) (#3742)#4459luongs3 wants to merge 1 commit into
luongs3 wants to merge 1 commit into
Conversation
…qlc-dev#3742) Problem ------- sqlc errored with "column does not exist" when a PostgreSQL query referenced any of the six system columns (tableoid, xmin, cmin, xmax, cmax, ctid) unless `database.managed: true` was configured. The 2023 fix (sqlc-dev#2871 / sqlc-dev#1745) only added the columns inside the PostgreSQL analyzer, which is initialized only when a live database is attached. Code-only pipelines that build the catalog purely from schema files still hit the same "column \"xmin\" does not exist" failure 17 months later (sqlc-dev#3742). Fix --- Synthesize the six PostgreSQL system columns inside `compiler.QueryCatalog.GetTable` whenever the engine is `postgresql`. The catalog itself is left untouched (so MySQL/SQLite tables are not polluted), and the synthesized columns are marked with a new `Column.IsSystem` flag so `SELECT *` / `RETURNING *` expansion skips them — matching PostgreSQL's own behavior. Explicit references like `SELECT xmin, ctid FROM foo` and aliased forms like `a.xmin` resolve to the correct types (oid/xid/cid -> pgtype.Uint32, tid -> pgtype.TID). Changed files ------------- - internal/compiler/query.go: add `Column.IsSystem`. - internal/compiler/query_catalog.go: track engine on QueryCatalog; append `pgSystemColumns` in GetTable when engine == postgresql. - internal/compiler/expand.go: skip IsSystem columns in `*` expansion. - internal/compiler/output_columns.go: skip IsSystem columns in the output-columns `*` branch (mirror of expand.go). - internal/endtoend/testdata/system_columns_3742/postgresql/pgx/v5/: new fixture exercising all six system columns, a table alias, and SELECT * (which must omit system columns). Golden files committed. Tests ----- - `go test ./internal/compiler/... ./internal/sql/... ./internal/engine/... ./internal/codegen/...` — all pass. - `go test -run TestExamples ./internal/endtoend/` — passes. - `go vet` + `gofmt -l` clean on touched files. Local verification ------------------ Built `/tmp/sqlc-dev-3742` from this branch and ran it against the exact schema + query in the issue. Before fix: `EXIT=1, column "xmin" does not exist`. After fix: `EXIT=0`, generates `pgtype.Uint32` for xmin, `pgtype.TID` for ctid. Confirmed `SELECT *` still expands to only the user columns (id, name, bio) — system columns are not included. What does NOT change -------------------- - MySQL / SQLite catalogs are untouched. - The managed-database analyzer path (sqlc-dev#2871) is untouched and still works for users on `database.managed: true`. - `SELECT *` continues to omit system columns (PG-compatible). - The existing `select_system` endtoend fixture (managed-db) is left alone; the new fixture covers the non-managed code path. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #3742.
sqlc errors with
column "..." does not existwhen a PostgreSQL query references any of the six system columns (tableoid,xmin,cmin,xmax,cmax,ctid) unlessdatabase.managed: trueis configured.The earlier fix (#2871 / #1745) only added these columns inside the PostgreSQL analyzer, which is initialized only when a live database is attached. Code-only pipelines that build the catalog purely from schema files still hit the same failure 17 months later. The issue thread on #3742 confirms this —
swallowstalkeralready diagnosed it, andwerjo-gridfusenoted that requiring a live DB doesn't work for code-gen pipelines.Repro on
main(no live DB):Fix
Synthesize the six PostgreSQL system columns inside
compiler.QueryCatalog.GetTablewhenever the engine ispostgresql. The catalog itself is left untouched (so MySQL/SQLite tables are not polluted), and the synthesized columns are marked with a newColumn.IsSystemflag soSELECT */RETURNING *expansion skips them — matching PostgreSQL's own behavior. Explicit references likeSELECT xmin, ctid FROM fooand aliased forms likea.xminresolve to the correct types (oid/xid/cid->pgtype.Uint32,tid->pgtype.TID).Changed files:
internal/compiler/query.go— addColumn.IsSystem.internal/compiler/query_catalog.go— track engine onQueryCatalog; appendpgSystemColumnsinGetTablewhen engine is postgresql.internal/compiler/expand.go— skipIsSystemcolumns in*expansion.internal/compiler/output_columns.go— skipIsSystemcolumns in the output-columns*branch (mirrorsexpand.go).internal/endtoend/testdata/system_columns_3742/postgresql/pgx/v5/— new fixture exercising all six system columns, a table alias, andSELECT *(which must omit them). Golden files committed.Tests
Local end-to-end verification against the exact schema + query from the issue:
EXIT=1, sql/query.sql:2:8: column "xmin" does not existEXIT=0, generatespgtype.Uint32forxmin,pgtype.TIDforctid.SELECT *still expands toid, name, bioonly.What does NOT change
database.managed: true.SELECT *continues to omit system columns (matches PostgreSQL behavior).select_systemend-to-end fixture (managed-db) is left alone; the new fixture covers the non-managed code path that was broken.