百科 / 错误代码 / Class 42 语法错误或访问规则冲突
42704 undefined_object
未定义对象
ERROR 已实测 详解 实测通过
- 条件名
undefined_object- 宏名称
ERRCODE_UNDEFINED_OBJECT- 启用版本
- 7.4
- 状态
- 活跃
版本覆盖
速览
SQLSTATE 42704 是 Class 42 中的 undefined_object。42704 表示未定义对象。选定 DROP TRIGGER 路径在真实表上指定不存在的触发器,报告 trigger "%s" for table "%s" does not exist;随后可以创建并删除真实触发器。
含义
42704 表示未定义对象。get_trigger_oid 按已解析关系 OID 和触发器名称扫描 pg_trigger;当 DROP TRIGGER 要求对象而没有匹配行时,会发出 ERROR:trigger "%s" for table "%s" does not exist。选定路径在真实表上指定不存在的触发器,随后用创建并删除真实触发器验证修复生命周期。
对象类型决定查找方式和报文:固定分支包括类型名解析失败的 type "%s" does not exist、角色查找失败的 role "%s" does not exist、按关系 OID 扫描约束失败的 constraint "%s" for table "%s" does not exist,以及在访问方法下解析限定名或 search_path 中 opclass 失败的 operator class "%s" does not exist for access method "%s"。选定路径更具体:get_trigger_oid 按关系 OID 和触发器名查 pg_trigger,当前表没有匹配行时发出 trigger "%s" for table "%s" does not exist;随后案例只为验证修复生命周期而创建并删除真实触发器。
诊断
先按主报文分类。对于选定触发器分支,检查 DDL 实际解析到的带 schema 关系、准确触发器名称(包括带引号大小写)和迁移顺序;触发器可能存在于另一张表,但由于源码按关系 OID 和名称查找,在当前关系上仍不存在。对于类型或角色报文,检查准确限定名以及 type/role 目录;对于约束报文,检查关系 OID/名称组合和迁移顺序;对于 opclass 报文,检查访问方法以及 opclass 查找使用的 schema/search_path。schema 缺失或未限定关系不存在属于另一类 namespace/未定义关系诊断,权限失败则是 42501。未找到对象的 ERROR 后选定自动提交会话仍为 IDLE;显式 BEGIN 中它会让事务进入 INERROR,直到 ROLLBACK 或回滚到合适保存点。它不同于 dblink 句柄错误 08003 和预备语句名称错误 26000。
处理
按报文指出的对象类型修复:改正限定名或 search_path,按依赖顺序应用前置迁移,或在对应 owner 的迁移中创建/重命名预期的 type、role、constraint 或 operator class。仅对选定触发器案例,才应显式操作目标关系,核对 owner、时机/事件、行级或语句级定义及函数,并在迁移要求时删除准确触发器。如果对象本来就是可选的,应有意识地选择幂等迁移策略;不要用 CASCADE、无关对象或通用的“先创建再删除”作为修复。显式事务报错后,先回滚事务或合适保存点,再重试。
可复现案例
在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。
missing_trigger PG 10 / 18 有 SQL
前置条件
- A runner-owned disposable target is provisioned.
- PL/pgSQL is available for the repair trigger function.
触发
Drop a named trigger that does not exist on a real table.
断言
- SQLSTATE is 42704 with a missing-trigger diagnostic
- The session remains IDLE
- Creating and dropping a real trigger completes the repair lifecycle
处置
Inspect the relation and trigger name; create or drop the intended trigger explicitly rather than changing unrelated objects.
清理
Drop the case schema with an owner connection.
报文
选定的 get_trigger_oid 分支以明确的 ERROR 发出主报文 trigger "%s" for table "%s" does not exist;触发器名和表名都是动态值。其他固定未定义对象分支发出 type "%s" does not exist、role "%s" does not exist、constraint "%s" for table "%s" does not exist 或 operator class "%s" does not exist for access method "%s"。只有客户端异常而没有对应对象类型的服务器 SQLSTATE 和主报文时,不能据此识别 42704。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
trigger "%s" for table "%s" does not exist
来源:src/backend/commands/trigger.c @ REL_18_6 · src/backend/commands/trigger.c @ REL_10_23
type "%s" does not exist
来源:src/backend/catalog/objectaddress.c @ REL_18_6 · src/backend/catalog/objectaddress.c @ REL_10_23
适用范围:Source-only type lookup branch; no natural runtime is claimed.
role "%s" does not exist
来源:src/backend/commands/user.c @ REL_18_6 · src/backend/commands/user.c @ REL_10_23
适用范围:Source-only DROP ROLE lookup branch; NOTICE missing_ok paths are not selected here.
constraint "%s" for table "%s" does not exist
来源:src/backend/catalog/pg_constraint.c @ REL_18_6 · src/backend/catalog/pg_constraint.c @ REL_10_23
适用范围:Source-only relation constraint lookup branch; no natural runtime is claimed.
operator class "%s" does not exist for access method "%s"
来源:src/backend/commands/opclasscmds.c @ REL_18_6 · src/backend/commands/opclasscmds.c @ REL_10_23
适用范围:Source-only access-method/operator-class lookup branch; no natural runtime is claimed.
代表案例
本页使用与运行器注册表相同的语句。执行时,syntax_schema、syntax_role 等生成名称会替换为一次性实例中的实际值。
CREATE TABLE syntax_schema.trigger_table (id integer);
DROP TRIGGER missing_trigger ON syntax_schema.trigger_table;
CREATE FUNCTION syntax_schema.trigger_function() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RETURN NEW; END $$;
CREATE TRIGGER valid_trigger BEFORE INSERT ON syntax_schema.trigger_table FOR EACH ROW EXECUTE PROCEDURE syntax_schema.trigger_function();
DROP TRIGGER valid_trigger ON syntax_schema.trigger_table;
SELECT count(*) FROM pg_trigger WHERE tgrelid = 'syntax_schema.trigger_table'::regclass AND NOT tgisinternal;
选定的 18.6 运行记录结构化诊断并通过修复断言;10.21 运行通过同一案例的具体检查。可下载的案例和证据投影分别是 42704 案例 JSON 和 作者证据。运行器清单为 verify/cases/42704/cases.json,页面 SQL 会与共享注册表核对。
版本
选定的自然运行范围是 PostgreSQL 18.6 与 10.21,不能据此推断所有中间版本的行为。
来源
- 上游源码 src/backend/utils/errcodes.txt
- 上游源码 src/backend/commands/trigger.c 第 1383–1386 行
- 上游源码 src/backend/commands/trigger.c 第 1406–1409 行
- 上游源码 src/backend/catalog/pg_constraint.c 第 781–801 行
- 上游源码 src/backend/catalog/pg_constraint.c 第 1218–1238 行
- 上游源码 src/backend/commands/opclasscmds.c 第 145–207 行
- 上游源码 src/backend/commands/opclasscmds.c 第 145–207 行
- 上游源码 src/backend/commands/user.c 第 1017–1023 行
- 上游源码 src/backend/commands/user.c 第 1129–1135 行
- 上游源码 src/backend/catalog/objectaddress.c 第 1493–1509 行
- 上游源码 src/backend/catalog/objectaddress.c 第 1618–1624 行
- 核验材料 verify/cases/42704/cases.json
- 核验材料 verify/cases/42704/snippets.json
- 核验材料 raw/calls/REL_10_23.jsonl
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
42704 is the undefined_object condition in Class 42.
-
The fixed source calls for 42704 include the mechanism and message boundary selected for this page.
-
Fixed 42704 producers resolve different object categories at different lookup points, including type, role, relation constraint, and access-method operator class; the selected runtime covers only the relation-scoped trigger branch.
-
The selected missing_trigger case passed on isolated PostgreSQL 18.6 and 10.21 targets.
-
The locked catalogue records 42704; 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_column42702ambiguous_column42703undefined_column42710duplicate_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