文档 / SQL 状态码 / Class 55 前置状态不满足
55006 object_in_use
对象正在使用
ERROR 已实测 详解 实测通过
- 条件名
object_in_use- 宏名称
ERRCODE_OBJECT_IN_USE- 启用版本
- 7.4
- 状态
- 活跃
版本覆盖
速览
55006 表示请求的对象操作与正在进行的使用冲突。本页选择显式事务中同一会话的活动 cursor 占用表;数据库用户、逻辑复制和待处理触发器事件是不同路径。
含义
选定的表命令模板为 cannot %s "%s" because it is being used by active queries in this session。关系引用计数指向同一 backend 的活动 portal 或扫描,不是一般权限问题。同一个表使用检查还有当前事务排队的 AFTER trigger 分支:cannot %s "%s" because it has pending trigger events。其他 55006 报文可能指明数据库用户或逻辑复制槽等不同阻塞者。
诊断
确认对象和持有使用状态的具体会话或服务器状态。本案例检查显式事务与命名 cursor;若是待处理 trigger event,应完成或回滚排队它们的事务。遇到数据库或复制路径时,应在终止任何会话前检查 pg_stat_activity、复制槽、订阅和维护归属。当前 backend 的 cursor 与其他用户 backend 或逻辑复制槽不是同一种阻塞。
处理
回滚失败的显式事务,让事务拥有的 cursor 和待处理 trigger event 释放,再在明确的维护边界重做 DDL。数据库用户阻塞要协调持有会话,逻辑复制槽则处理订阅/slot 生命周期。不要把 CASCADE 或终止无关会话当作通用处理。
可复现案例
在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。
active_cursor_drop PG 10 / 18 有 SQL
前置条件
- A runner-owned empty table exists; a cursor can still hold the relation in active use.
- The table cursor and DROP run on one backend session.
触发
Declare and use a cursor on a table in an explicit transaction, then attempt to DROP that table from the same session.
断言
- SQLSTATE is 55006 with ERROR severity
- The diagnostic says the table is used by active queries in this session
- The failed DROP leaves the explicit transaction INERROR
- ROLLBACK releases the transaction-owned cursor and the same session can DROP the table
处置
Rollback the failed transaction to release its cursor and then repeat the DDL; do not hide an active-session dependency with CASCADE.
清理
Close all runner connections and drop the case schema with an owner connection.
实测诊断
选定源码组为 ERROR,操作和关系名是动态字段。实测主报文为 cannot DROP TABLE "cursor_drop_table" because it is being used by active queries in this session;事务进入 INERROR,回滚后恢复为 IDLE。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
source database "%s" is being accessed by other users
database "%s" is used by an active logical replication slot
cannot %s "%s" because it is being used by active queries in this session
cannot %s "%s" because it has pending trigger events
来源:src/backend/commands/tablecmds.c @ REL_18_6
适用范围:This is a separate current-transaction branch from the active-query reference-count branch.
cannot %s "%s" because it is being used by active queries in this session
cannot %s "%s" because it is being used by active queries in this session
代表案例
注册表创建一张空表,在同一后端开启事务、声明并 FETCH 命名 cursor,再尝试 DROP TABLE;即使没有行,活动 cursor 仍会触发同会话 active-query 分支。真实 55006 后,处理程序在同一连接回滚,再次删除表并确认关系已消失。本案例不代替其他数据库用户、逻辑复制槽或待处理 AFTER-trigger event 路径。
CREATE TABLE cursor_drop_table(id integer PRIMARY KEY);
BEGIN;
DECLARE active_cursor CURSOR FOR SELECT id FROM cursor_drop_table;
FETCH active_cursor;
DROP TABLE cursor_drop_table;
ROLLBACK;
DROP TABLE cursor_drop_table;
SELECT to_regclass('cursor_drop_table_regclass')
选定的 PostgreSQL 18.6 与 10.21 运行均通过 SQLSTATE、严重级别、状态或断开恢复、修复、清理和一次性实例停止断言。详见 案例 JSON 与 作者证据。
版本
锁定目录从 7.4 起记录 55006。18.6 与 10.21 的活动 cursor DDL 案例均通过;这不代表数据库、复制或待处理触发器路径。
来源
- 上游源码 src/backend/commands/tablecmds.c 第 3072–3077 行
- 上游源码 src/backend/commands/tablecmds.c 第 4431–4435 行
- 上游源码 src/backend/utils/errcodes.txt 第 425 行
- 上游源码 src/backend/commands/dbcommands.c 第 1407 行
- 上游源码 src/backend/commands/dbcommands.c 第 1752 行
- 上游源码 src/backend/commands/tablecmds.c 第 4431 行
- 核验材料 verify/cases/55006/cases.json
- 核验材料 verify/cases/55006/snippets.json
- 核验材料 raw/calls/REL_10_23.jsonl
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
55006 is object_in_use in SQLSTATE Class 55.
-
The cited PostgreSQL 18.6 source paths support the representative resource and object-state mechanisms described.
-
The locked catalogue records 55006 from 7.4; that boundary does not prove exact implementation introduction.
-
The selected table-command path reports 55006 when the same session attempts DROP while its transaction-owned cursor still uses the table.
-
The active_cursor_drop case passed on isolated PostgreSQL 18.6 and 10.21; rollback released the cursor transaction and the same session then dropped the table.
运行记录
| 目标 | 服务器版本 | 结果 | 覆盖案例 |
|---|---|---|---|
| latest | 18.6 (Homebrew) | passed | active_cursor_drop |
| pg10 | 10.21 (Debian 10.21-1.pgdg90+1) | passed | active_cursor_drop |
同类 SQL 状态码
| 状态码 | 条件名 | 宏名称 | 严重等级 | 版本 |
|---|---|---|---|---|
| 55000 | object_not_in_prerequisite_state | ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE |
ERROR | 7.4 |
| 对象或服务器未处于操作所需的前置状态。 | 活跃 | |||
| 55006 | object_in_use | ERRCODE_OBJECT_IN_USE |
ERROR | 7.4 |
| 对象正被使用,操作与进行中的占用冲突。 | 活跃 | |||
| 55P02 | cant_change_runtime_param | ERRCODE_CANT_CHANGE_RUNTIME_PARAM |
ERROR | 7.4 |
| 当前上下文或阶段不允许修改该运行时参数。 | 活跃 | |||
| 55P03 | lock_not_available | ERRCODE_LOCK_NOT_AVAILABLE |
ERROR | 8.0 |
| 当前等待策略下取不到锁,含 lock_timeout 到期。 | 活跃 | |||
| 55P04 | unsafe_new_enum_value_usage | ERRCODE_UNSAFE_NEW_ENUM_VALUE_USAGE |
ERROR | 12 |
| 同一事务中使用了尚未提交的新增 enum 值。 | 活跃 | |||