Skip to content

PG18: tolerate new libpg_query location fields and deparse record declaration defaults - #395

Merged
pyramation merged 2 commits into
mainfrom
devin/1795318716-pg18-new-location-fields
Nov 22, 2026
Merged

pyramation merged 2 commits into
mainfrom
devin/1795318716-pg18-new-location-fields

Conversation

@pyramation

@pyramation pyramation commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Prepares pgsql-parser for the next libpg_query PG 18 bump (constructive-io/libpg_query#15 core sync + constructive-io/libpg_query#16 patch series). With a WASM built from #16, 221 tests failed (81 deparser, 127 transform-ast, 6 plpgsql-parser, 6 transform, 1 plpgsql-deparser). None of them came from a parse bug. They came from two AST additions:

  1. New source-offset fields from upstream (pganalyze 18-latest, "Normalize: Take constant locations from the parser"): DefElem.arg_location, NotifyStmt.payload_location, Create/AlterSubscriptionStmt.conninfo_location. A diff of all 304 kitchen-sink fixtures, published WASM vs. new WASM, shows these three keys are the only differences. Round-trip comparisons stripped location but not these fields, so every function AS/LANGUAGE option mismatched after deparse. These are now added to every cleanTree / volatile-key set (deparser, transform-ast, transform CLEAN_TREE_VOLATILE_KEYS, plpgsql-deparser, plpgsql-parser tests).
  2. PLpgSQL_rec now carries declaration properties (when we upgrade to PG 12/13, add these test cases #16 record-datatype patch): isconst, notnull, default_val, datatype. Unresolved custom types (int_nn, booltrue, composite test1) and record become PLpgSQL_rec. deparseRec dropped := row(1,2,3) / NOT NULL / CONSTANT. It now emits them like deparseVar. walkPlpgsqlAst also walks PLpgSQL_rec.datatype and default_val.
x record := row(1,2,3);            -- was: x record;
x CONSTANT int_nn NOT NULL := 42;  -- was: x int_nn;

Both changes are backward compatible: the published libpg-query WASM omits these fields.

Testing: pnpm -r test passes fully with both the published WASM (CI) and a locally built WASM from libpg_query#16. Added unit tests use hand-built ASTs for record declarations and the traverse walk, so they don't depend on the WASM version.

Not included: the 18-latest.proto / generated types refresh (new fields arg_location, payload_location, payload_param, conninfo_location, CreateStatsStmt.owner) and deparsing NOTIFY chan, $1 (payload_param). These belong with the libpg-query version bump.


libpg-query and parser opened this PR for Dan
Implemented in session Combine PG 18 patch branches into one PR
Review with Devin · Open in Devin Desktop


Summary by CodeRabbit

  • Bug Fixes
    • Record declarations now preserve supported attributes, including CONSTANT, NOT NULL, declared types, and default expressions. Declarations without a type use RECORD.
    • AST traversal now includes record declaration types and default expressions.
  • Improvements
    • Round-trip comparisons ignore additional location metadata, reducing differences caused by parser location fields.

…AST comparisons

libpg_query 18-latest (Normalize: take constant locations from the parser)
adds DefElem.arg_location, NotifyStmt.payload_location and
CreateSubscriptionStmt/AlterSubscriptionStmt.conninfo_location. They are
source offsets like location, so round-trip comparisons must strip them.
… declarations

PG18 libpg_query serializes PLpgSQL_rec with isconst/notnull/default_val/datatype
(record, composite and unresolved custom types). Emit them like PLpgSQL_var and
walk PLpgSQL_rec datatype/default_val in walkPlpgsqlAst.
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@luxor-code-review

luxor-code-review Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


The change extends the volatile-key cleaning used by round-trip validation and fixes PLpgSQL_rec deparse and traversal behavior. Cleaners in several packages now strip the newer arg_location, payload_location, and conninfo_location fields, and the PLpgSQL_rec datum is visited as a non-leaf with its datatype/default children.

Files Change
packages/transform/src/round-trip-core.ts, packages/transform-ast/test-utils/clean-tree.ts, packages/deparser/scripts/clean-utils.ts, packages/plpgsql-deparser/test-utils/index.ts, packages/plpgsql-parser/__tests__/round-trip.test.ts Extend the volatile-key sets used for round-trip cleaning/comparison with the new location fields.
packages/plpgsql-deparser/src/plpgsql-deparser.ts, packages/plpgsql-deparser/src/types.ts, packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts Rewrite deparseRec to emit CONSTANT/NOT NULL/default clauses and fall back to RECORD when the datatype is missing, with matching type definitions and tests.
packages/traverse/src/plpgsql.ts, packages/traverse/__tests__/plpgsql-rec.test.ts Visit PLpgSQL_rec children (datatype, default_val) instead of treating it as a leaf.
packages/transform/__tests__/round-trip-core.test.ts Round-trip test coverage for the updated cleaning.

Two low-severity observations stayed below the reporting threshold: the RECORD fallback branch in deparseRec may still emit CONSTANT/DEFAULT clauses that the PL/pgSQL grammar cannot re-parse, and the CLI round-trip test's positionKeys regex was not updated to include the new location fields.

Reviewed commit: 725b65a

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

PL/pgSQL record declarations now support additional metadata during traversal and deparsing. Tree-cleaning and round-trip normalization now omit three additional location fields.

Changes: PL/pgSQL record handling

Layer / File(s) Summary
Record metadata and child traversal
packages/plpgsql-deparser/src/types.ts, packages/traverse/src/plpgsql.ts, packages/traverse/__tests__/plpgsql-rec.test.ts
PLpgSQL_rec adds optional datatype, default expression, constant, and not-null fields. The walker visits datatype and default-expression children when present.
Record declaration output
packages/plpgsql-deparser/src/plpgsql-deparser.ts, packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts
deparseRec emits CONSTANT, a declared type or RECORD fallback, NOT NULL, and an optional default expression. Tests cover record declarations with and without a datatype.

Tree normalization

Layer / File(s) Summary
Location-field normalization
packages/transform/src/round-trip-core.ts, packages/transform/__tests__/round-trip-core.test.ts, packages/deparser/scripts/clean-utils.ts, packages/transform-ast/test-utils/clean-tree.ts, packages/plpgsql-deparser/test-utils/index.ts, packages/plpgsql-parser/__tests__/round-trip.test.ts
Tree-cleaning helpers and round-trip normalization omit arg_location, payload_location, and conninfo_location. The volatile-key test includes these fields.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: luxor-code-review

Merge Risk: Minimal · up to 725b6

This change preserves record declaration attributes when deparsing and traversing PL/pgSQL, and it ignores three PG 18 source-offset fields in AST comparisons. No concrete merge-blocking risk was found.

Security architecture risk: Minimal · up to 725b6

The changes preserve record declaration semantics and extend existing traversal behavior. The inspected changes establish no introduced or worsened risk to security boundaries, privileges, failure containment, or state ownership.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The directly affected surface is caller-supplied AST processing and generated SQL, including downstream qualification and reference analysis. The inspected deparser returns SQL text rather than executing it; authority held by external execution or deployment consumers was not established.

Trust Boundaries and Controls

  • observed — Record defaults now use the existing deparseExpr helper, which returns expression query text verbatim. Variable defaults already used that behavior before this PR. This is SQL generation, not an input-sanitization boundary; the inspected change does not replace or bypass an existing validation control.

Resilience and Maintainability Implications

  • observed — The added traversal branch reads child fields and creates copied key paths without itself mutating the AST or acquiring resources. Parent-first callbacks and false-return pruning remain intact. Visitor mutations and exceptions retain existing caller-owned semantics rather than gaining a new atomicity or rollback guarantee.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names two real changes in the PR: tolerating the new libpg_query PG18 location fields (arg_location, payload_location, conninfo_location) in AST cleanup, and deparsing record declaration def…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names two real changes in the PR: tolerating the new libpg_query PG18 location fields (arg_location, payload_location, conninfo_location) in AST cleanup, and deparsing record declaration defaults and attributes in PL/pgSQL. Both are present in the raw summary. The title is long (86 characters, 11 words) and joins two topics with a colon-free comma-less phrase, but it still describes the primary changes accurately.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 11
  • Waived: $2.75 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 23ea127 and 725b65a.

Files selected for processing: 11

  • packages/deparser/scripts/clean-utils.ts
  • packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts
  • packages/plpgsql-deparser/src/plpgsql-deparser.ts
  • packages/plpgsql-deparser/src/types.ts
  • packages/plpgsql-deparser/test-utils/index.ts
  • packages/plpgsql-parser/__tests__/round-trip.test.ts
  • packages/transform-ast/test-utils/clean-tree.ts
  • packages/transform/__tests__/round-trip-core.test.ts
  • packages/transform/src/round-trip-core.ts
  • packages/traverse/__tests__/plpgsql-rec.test.ts
  • packages/traverse/src/plpgsql.ts

Limit details: You’ve used all 10 included reviews currently available.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant