百科 / 错误代码 / Class 42 语法错误或访问规则冲突
42601 syntax_error
语法错误
ERROR 已实测 详解 实测通过
- 条件名
syntax_error- 宏名称
ERRCODE_SYNTAX_ERROR- 启用版本
- 7.4
- 状态
- 活跃
版本覆盖
速览
SQLSTATE 42601 是 Class 42 中的 syntax_error。42601 表示语法错误。选定的解析器路径拒绝 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
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_schema、syntax_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,不能据此推断所有中间版本的行为。
来源
- 上游源码 src/backend/utils/errcodes.txt
- 上游源码 src/backend/parser/gram.y 第 11200–11204 行
- 上游源码 src/backend/parser/gram.y 第 13313–13317 行
- 上游源码 src/backend/parser/scan.l 第 1115–1127 行
- 上游源码 src/backend/parser/scan.l 第 1228–1240 行
- 核验材料 verify/cases/42601/cases.json
- 核验材料 verify/cases/42601/snippets.json
- 核验材料 raw/calls/REL_10_23.jsonl
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
42601 is the syntax_error condition in Class 42.
-
The fixed source calls for 42601 include the mechanism and message boundary selected for this page.
-
The fixed scanner formats generic syntax errors as at-or-near-token or end-of-input errors and attaches a lexer position.
-
The selected syntax_limit_comma case passed on isolated PostgreSQL 18.6 and 10.21 targets.
-
The locked catalogue records 42601; runtime scope is limited to the selected targets.
运行记录
| 目标 | 服务器版本 | 结果 | 覆盖案例 |
|---|---|---|---|
| latest | 18.6 (Homebrew) | passed | |
| pg10 | 10.21 (Debian 10.21-1.pgdg90+1) | passed |
同类错误代码
Class 42 语法错误或访问规则冲突 下的其他成员。
42000syntax_error_or_access_rule_violation42501insufficient_privilege42602invalid_name42611invalid_column_definition42622name_too_long42701duplicate_column42702ambiguous_column42703undefined_column42704undefined_object42710duplicate_object42712duplicate_alias42723duplicate_function42725ambiguous_function42803grouping_error42804datatype_mismatch42809wrong_object_type42830invalid_foreign_key42846cannot_coerce42883undefined_function428C9generated_always42939reserved_name42P01undefined_table42P02undefined_parameter42P03duplicate_cursor42P04duplicate_database42P05duplicate_prepared_statement42P06duplicate_schema42P07duplicate_table42P08ambiguous_parameter42P09ambiguous_alias42P10invalid_column_reference42P11invalid_cursor_definition42P12invalid_database_definition42P13invalid_function_definition42P14invalid_prepared_statement_definition42P15invalid_schema_definition42P16invalid_table_definition42P17invalid_object_definition42P18indeterminate_datatype42P19invalid_recursion42P20windowing_error42P21collation_mismatch42P22indeterminate_collation