Ambiguity in VALUES synopsis: LIMIT vs FETCH

From: Cheng Ding <cheng(dot)ding(at)utexas(dot)edu>
To: pgsql-docs(at)lists(dot)postgresql(dot)org
Cc: Milos Gligoric <gligoric(at)utexas(dot)edu>
Subject: Ambiguity in VALUES synopsis: LIMIT vs FETCH
Date: 2025-08-26 20:13:59
Message-ID: CAMfZ+xO5hbrZJu46S5QYgAtDRBr-UXzR_8VOAraQQX+8_VasQg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-docs

Hello docs team,

I believe there’s an ambiguity in the VALUES command synopsis that can
mislead readers into thinking LIMIT and FETCH can be used together.

*What I ran*

VALUES (12, 'nectarine') LIMIT 1 FETCH FIRST 1 ROW ONLY;

*What happened*

ERROR: syntax error at or near "FETCH"

*Why I think the docs are ambiguous*

The current synopsis for VALUES shows:

VALUES ( expression [, ...] ) [, ...][ ORDER BY sort_expression [ ASC
| DESC | USING operator ] [, ...] ][ LIMIT { count | ALL } ][ OFFSET
start [ ROW | ROWS ] ][ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS
} ONLY ]

As written, this looks like LIMIT … and FETCH … are independent optional
clauses that could both appear. In practice (and in the underlying
grammar), FETCH is the SQL-standard alternative to LIMIT; they are mutually
exclusive.

*Suggested fix (one option)*

Adjust the synopsis to make the alternation explicit, for example:

[ { LIMIT { count | ALL } [ OFFSET start [ ROW | ROWS ] ]
| [ OFFSET start [ ROW | ROWS ] ] FETCH { FIRST | NEXT } [ count ] {
ROW | ROWS } ONLY } ]

Alternatively, add a short note right under the synopsis:

Note: FETCH { FIRST | NEXT } … ONLY is an alternative to LIMIT and cannot
be used together with it.

*Environment*
PostgreSQL v17.6

Best regards,
Cheng Ding
The University of Texas at Austin

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message PG Doc comments form 2025-08-27 02:49:15 Small typo in doc
Previous Message Laurenz Albe 2025-08-25 07:58:29 Re: Inaccurate statement about log shipping replication mode