选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 错误代码 / Class 42 语法错误或访问规则冲突

42601 syntax_error

语法错误

ERROR 已实测 详解 实测通过

条件名
syntax_error
宏名称
ERRCODE_SYNTAX_ERROR
启用版本
7.4
状态
活跃

版本覆盖

速览

SQLSTATE 42601 是 Class 42 中的 syntax_error42601 表示语法错误。选定的解析器路径拒绝 LIMIT 1, 2,主报文为 LIMIT #,# syntax is not supported,提示为 Use separate LIMIT and OFFSET clauses.

含义

42601 表示语法错误。选定的 gram.y 分支在解析阶段拒绝逗号写法,发出 ERROR 主报文 LIMIT #,# syntax is not supported 和提示 Use separate LIMIT and OFFSET clauses. 因此在 PostgreSQL 中逗号写法不是“跳过 1 行再取 2 行”的请求;两个数必须分别写成两个子句。

扫描器和语法分析器共同实现一般的 42601 路径。scanner_yyerror 设置 SQLSTATE:当前位置仍有 token 时报告 syntax error at or near "%s",扫描器到达输入缓冲区末尾时报告 syntax error at end of input,并附带词法位置。语法动作也可以通过同一格式化路径提供更具体的报文。选定的 LIMIT 分支只是其中一个 producer,并不是所有 42601 都必须包含它。

诊断

记录完整语句、解析位置、服务器版本和提示。对于 at or near,检查 POSITION 指向的 token 及其前面的文本;对于 at end of input,检查未闭合的引号、美元引号、括号、注释或生成的子句。保留 query builder 实际发送的字节(包括分隔符和替换片段),先把完整语句交给服务器解析,再改变语义。解析器在执行前拒绝该语法,因此选定的自动提交会话仍为 IDLE,失败语句不会产生行。在显式事务中,同样的语句级 ERROR 会让事务进入失败状态,直到 ROLLBACK 或合适的保存点操作;这与选定的自动提交观察是不同的会话边界。不要把语法错误与对象不存在或“已识别但当前上下文不支持”的功能混淆。

处理

按提示改写语法。对选定的三行有序查询,应把 LIMIT 1, 2 改为 LIMIT 2 OFFSET 1,结果为 [2, 3]LIMIT 1 OFFSET 0 会产生不同结果,不能作为等价修复。生成分页 SQL 的查询构造器应按方言处理。

对于一般扫描器错误,应按位置修复缺失或错放的 token、引号、注释分隔符或 query builder 片段,然后重新解析完整语句。不能因为另一个 42601 也使用同一 SQLSTATE,就套用 LIMIT 改写。

可复现案例

在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。

syntax_limit_comma PG 10 / 18 有 SQL

前置条件

  • A runner-owned disposable target is provisioned with a deterministic three-row ordered query.

触发

Execute the unsupported comma form over ordered rows: SELECT n FROM generate_series(1, 3) AS s(n) ORDER BY n LIMIT 1, 2.

断言

  • SQLSTATE is 42601 with the LIMIT #,# diagnostic
  • The autocommit session remains IDLE
  • Separate LIMIT and OFFSET syntax returns the second and third rows [2, 3]

处置

Use separate LIMIT and OFFSET clauses as indicated by the server hint.

清理

No persistent objects; close the runner connection.

报文

选定解析器分支以明确的 ERROR 发出主报文 LIMIT #,# syntax is not supported 和 HINT Use separate LIMIT and OFFSET clauses. 扫描器的固定一般分支使用 syntax error at or near "%s"syntax error at end of input,位置由词法扫描器生成。LIMIT 主报文/HINT 只能识别选定的分页子路径;只有客户端异常而没有该具体分支的服务器 SQLSTATE 和诊断时,不能识别该子路径,而其他合法 42601 producer 可以有不同主报文。

报文模板

源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。

主消息 LIMIT #,# syntax is not supported
HINT Use separate LIMIT and OFFSET clauses.

来源:src/backend/parser/gram.y @ REL_18_6 · src/backend/parser/gram.y @ REL_10_23

主消息 syntax error at or near "%s"
主消息 syntax error at end of input

来源:src/backend/parser/scan.l @ REL_18_6 · src/backend/parser/scan.l @ REL_10_23

适用范围:The scanner inserts the grammar/lexer message and dynamic token; these variants are source-only in this batch.

代表案例

本页使用与运行器注册表相同的语句。执行时,syntax_schemasyntax_role 等生成名称会替换为一次性实例中的实际值。

SELECT n FROM generate_series(1, 3) AS s(n) ORDER BY n LIMIT 1, 2;
SELECT n FROM generate_series(1, 3) AS s(n) ORDER BY n LIMIT 2 OFFSET 1;

选定的 18.6 运行记录结构化诊断并通过修复断言;10.21 运行通过同一案例的具体检查。可下载的案例和证据投影分别是 42601 案例 JSON作者证据。运行器清单为 verify/cases/42601/cases.json,页面 SQL 会与共享注册表核对。

版本

选定的自然运行范围是 PostgreSQL 18.6 与 10.21,不能据此推断所有中间版本的行为。

来源

证据

断言

每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。

  • 42601 is the syntax_error condition in Class 42.

    核实方式Read fixed errcodes.txt and the locked catalogue metadata.

    不覆盖Directory identity does not identify every backend path.

    来源src/backend/utils/errcodes.txt

  • The fixed source calls for 42601 include the mechanism and message boundary selected for this page.

    核实方式Trace the resolved REL_18_6 and REL_10_23 source call records at the locked commits.

    不覆盖Other calls can use the same SQLSTATE with different context or dynamic fields.

    来源src/backend/parser/gram.y · src/backend/parser/gram.y · src/backend/parser/scan.l · src/backend/parser/scan.l

  • The fixed scanner formats generic syntax errors as at-or-near-token or end-of-input errors and attaches a lexer position.

    核实方式Read scanner_yyerror and scanner_errposition in the fixed REL_18_6 and REL_10_23 scan.l sources.

    不覆盖This source branch is separate from the selected LIMIT runtime; grammar actions can supply more specific primary text.

    来源src/backend/parser/scan.l · src/backend/parser/scan.l

  • The selected syntax_limit_comma case passed on isolated PostgreSQL 18.6 and 10.21 targets.

    核实方式Run the shared registry case and inspect structured diagnostics, state/recovery assertions, and cleanup.

    不覆盖This covers the selected case and targets only; it does not generalize to all drivers or intermediate releases.

    来源verify/cases/42601/cases.json · verify/cases/42601/snippets.json

  • The locked catalogue records 42601; runtime scope is limited to the selected targets.

    核实方式Read generated facts and selected summaries.

    不覆盖Definition presence is not an exact behavioral introduction.

    来源src/backend/utils/errcodes.txt · raw/calls/REL_18_6.jsonl · raw/calls/REL_10_23.jsonl

运行记录

目标服务器版本结果覆盖案例
latest 18.6 (Homebrew) passed
pg10 10.21 (Debian 10.21-1.pgdg90+1) passed

同类错误代码

Class 42 语法错误或访问规则冲突 下的其他成员。