百科 / 错误代码 / Class 42 语法错误或访问规则冲突
42702 ambiguous_column
列引用歧义
ERROR 已实测 详解 实测通过
- 条件名
ambiguous_column- 宏名称
ERRCODE_AMBIGUOUS_COLUMN- 启用版本
- 7.4
- 状态
- 活跃
版本覆盖
速览
SQLSTATE 42702 是 Class 42 中的 ambiguous_column。42702 表示列引用歧义。选定 join 使用两个真实表的 id,未限定的 SELECT id 报告 column reference "%s" is ambiguous。
含义
42702 表示列引用歧义。在 colNameToVar 中,PostgreSQL 会扫描当前可见 namespace 的未限定名称。如果同一次查找中有两个可见 range-table 条目都匹配,就会在执行前以 ERROR 发出 column reference "%s" is ambiguous;选定 join 的两张真实表都暴露了 id。这个分支属于名称解析,行值是否相同不能消除歧义。
诊断
检查别名、CTE、连接输入、lateral 可见性,以及当前查询范围内提供该名称的每个关系。即使两列值相同,未限定名称仍可能有歧义,因为 PostgreSQL 必须先解析来源再执行查询。这是解析阶段的名称解析,因此选定自动提交会话仍为 IDLE;它不同于没有匹配列的 42703。显式事务仍遵循语句级 ERROR 的通常事务状态规则。
处理
使用稳定的表别名限定预期列(或从范围中移除非预期关系),然后核对返回行。不要依赖连接顺序或相同值“解决”歧义。如果生成 SQL 会引入别名或 CTE,应把限定写进查询构造器契约,并测试选定的来源列。
可复现案例
在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。
ambiguous_column PG 10 / 18 有 SQL
前置条件
- A runner-owned disposable target is provisioned.
触发
Join two tables that both expose id and select id without qualification.
断言
- SQLSTATE is 42702 with the ambiguous-reference diagnostic
- The session remains IDLE
- Qualifying the column returns the intended row
处置
Qualify the column with its table or alias and verify the intended source.
清理
Drop the case schema with an owner connection.
报文
选定的 parse_relation.c 分支以明确的 ERROR 发出主报文 column reference "%s" is ambiguous;%s 是未解析的列名,解析位置随语句上下文变化。只有客户端异常而没有服务器 SQLSTATE 和主报文时,不能据此认定 42702。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
column reference "%s" is ambiguous
来源:src/backend/parser/parse_relation.c @ REL_18_6 · src/backend/parser/parse_relation.c @ REL_10_23
代表案例
本页使用与运行器注册表相同的语句。执行时,syntax_schema、syntax_role 等生成名称会替换为一次性实例中的实际值。
CREATE TABLE syntax_schema.left_table (id integer);
CREATE TABLE syntax_schema.right_table (id integer);
INSERT INTO syntax_schema.left_table VALUES (1);
INSERT INTO syntax_schema.right_table VALUES (1);
SELECT id FROM syntax_schema.left_table, syntax_schema.right_table;
SELECT syntax_schema.left_table.id FROM syntax_schema.left_table, syntax_schema.right_table;
选定的 18.6 运行记录结构化诊断并通过修复断言;10.21 运行通过同一案例的具体检查。可下载的案例和证据投影分别是 42702 案例 JSON 和 作者证据。运行器清单为 verify/cases/42702/cases.json,页面 SQL 会与共享注册表核对。
版本
选定的自然运行范围是 PostgreSQL 18.6 与 10.21,不能据此推断所有中间版本的行为。
来源
- 上游源码 src/backend/parser/parse_relation.c 第 787–791 行
- 上游源码 src/backend/parser/parse_relation.c 第 960–964 行
- 上游源码 src/backend/utils/errcodes.txt
- 核验材料 verify/cases/42702/cases.json
- 核验材料 verify/cases/42702/snippets.json
- 核验材料 raw/calls/REL_10_23.jsonl
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
42702 is the ambiguous_column condition in Class 42.
-
The fixed source calls for 42702 include the mechanism and message boundary selected for this page.
-
The selected ambiguous_column case passed on isolated PostgreSQL 18.6 and 10.21 targets.
-
The locked catalogue records 42702; 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_privilege42601syntax_error42602invalid_name42611invalid_column_definition42622name_too_long42701duplicate_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