Skip to content

fix(parser): locking-clause order and CREATE OR REPLACE in the generic dialect - #970

Open
kshvakov wants to merge 1 commit into
ajitpratap0:mainfrom
kshvakov:feat/postgres-grammar-gaps
Open

kshvakov wants to merge 1 commit into
ajitpratap0:mainfrom
kshvakov:feat/postgres-grammar-gaps

Conversation

@kshvakov

Copy link
Copy Markdown

Two PostgreSQL grammar fixes found by running the dimitri/sqlfmt test
corpus through GoSQLX as the parsing backend of a SQL formatter, each
verified against libpg_query.

FOR UPDATE ... LIMIT: SELECT ... ORDER BY x FOR UPDATE SKIP LOCKED LIMIT 100 is valid PostgreSQL but was rejected with E2002 at LIMIT —
the locking clause was parsed after LIMIT/OFFSET with no re-check for a
trailing limit clause. The SQL-standard order LIMIT ... FOR UPDATE was
already accepted. After the locking clause each of LIMIT / OFFSET /
FETCH FIRST may appear at most once, in any order; OFFSET combines
with either, LIMIT and FETCH are mutually exclusive, and a
limit-clause token on both sides of the locking clause is invalid in any
combination. A duplicate token is left unconsumed for the statement
boundary, matching how a duplicate LIMIT before FOR fails. The
trailing sequence is gated to PostgreSQL plus the permissive generic
dialect: MySQL puts LIMIT before its locking clause, so
FOR UPDATE ... LIMIT keeps failing there.

CREATE OR REPLACE FUNCTION with an AS $$ ... $$ body failed when no
dialect was set (expected semicolon or end of statement, got REPLACE):
alterMask() == dialectAll made implicitStartWords[REPLACE]
(MySQL|SQLite) match, so the generic end-of-statement scan cut the CREATE
header at its own OR REPLACE. A word following OR never starts a new
statement — nothing valid ends an expression with a bare OR — so
startsStatementInContext now continues the statement there. Named
dialects were unaffected; the postgresql dialect parsed these statements
all along.

Also pins, as postgresql-dialect regression tests, the constructs the
corpus triage relies on: table-valued functions in FROM (including
generate_series(1, 3) AS t(x) column aliases, JOIN, subquery and
INSERT ... SELECT positions) and the <-> distance operator in every
clause position.

10 parse.golden cells regenerated (statements containing CREATE OR
REPLACE, whose recorded statement spans changed with the fix).
Changelog fragments: for-update-after-limit.added.md,
create-or-replace-function-default-dialect.added.md.

@vercel

vercel Bot commented Nov 14, 2026

Copy link
Copy Markdown

@kshvakov is attempting to deploy a commit to the coolajitpratapsingh0-9932's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
gosqlx-website Ignored Ignored Preview Nov 14, 2026 10:12pm UTC

…c dialect

Two PostgreSQL grammar fixes found by running the dimitri/sqlfmt test
corpus through GoSQLX as the parsing backend of a SQL formatter, each
verified against libpg_query:

FOR UPDATE ... LIMIT: SELECT ... ORDER BY x FOR UPDATE SKIP LOCKED
LIMIT 100 is valid PostgreSQL but was rejected with E2002 at LIMIT -
the locking clause was parsed after LIMIT/OFFSET with no re-check for a
trailing limit clause. The SQL-standard order LIMIT ... FOR UPDATE was
already accepted. After the locking clause each of LIMIT / OFFSET /
FETCH FIRST may appear at most once, in any order; OFFSET combines with
either, LIMIT and FETCH are mutually exclusive (LIMIT ALL counts as a
LIMIT: it has no count and lives in Extras.LimitAll), and a limit-
clause token on both sides of the locking clause is invalid in any
combination. A duplicate token is left unconsumed for the statement
boundary, matching how a duplicate LIMIT before FOR fails. The
trailing sequence is gated to PostgreSQL plus the permissive generic
dialect: MySQL puts LIMIT before its locking clause, so FOR UPDATE ...
LIMIT keeps failing there.

CREATE OR REPLACE FUNCTION with an AS $$ ... $$ body failed when no
dialect was set ('expected semicolon or end of statement, got
REPLACE', column of REPLACE): alterMask() == dialectAll made
implicitStartWords[REPLACE] (MySQL|SQLite) match, so the generic
end-of-statement scan cut the CREATE header at its own OR REPLACE. A
word following OR never starts a new statement - nothing valid ends an
expression with a bare OR - so startsStatementInContext now continues
the statement there. Named dialects were unaffected (mask
intersection empty); the postgresql dialect parsed these statements
all along.

Also pins, as postgresql-dialect regression tests, the constructs the
corpus triage relies on: table-valued functions in FROM (including
generate_series(1, 3) AS t(x) column aliases, JOIN, subquery and
INSERT ... SELECT positions) and the <-> distance operator in every
clause position.

10 parse.golden cells regenerated (statements containing CREATE OR
REPLACE, whose recorded statement spans changed with the fix).
Changelog fragments: for-update-after-limit.added.md,
create-or-replace-function-default-dialect.added.md.
@kshvakov
kshvakov force-pushed the feat/postgres-grammar-gaps branch from 0d8ca78 to edf116b Compare November 15, 2026 21:47

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant