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

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

百科 / 错误代码 / Class 55 前置状态不满足

55000 object_not_in_prerequisite_state

对象未处于前置状态

ERROR 已实测 详解 实测通过

类别
Class 55 前置状态不满足
严重等级
ERROR
条件名
object_not_in_prerequisite_state
宏名称
ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
启用版本
7.4
状态
活跃

版本覆盖

速览

55000 表示请求的操作缺少某个必要的对象或服务器状态。本页选择 currval 所需的会话级序列前提;预加载、恢复和无效索引是不同机制。

含义

currval 按后端会话记录状态。创建序列,或在另一个会话调用 nextval,都不会初始化当前会话。固定序列模板为 currval of sequence "%s" is not yet defined in this session,严重级别为 ERROR

其他固定的 55000 路径有不同前提:pg_stat_statementspg_stat_statements must be loaded via "shared_preload_libraries",应按正常配置流程加入启动参数并重启服务;pg_surgeryrecovery is in progress,detail 为 Heap surgery functions cannot be executed during recovery.,应等待恢复完成,或在合适的可写 primary 上执行;pgstattupleindex "%s" is not valid,应先检查索引,并按适当的 REINDEX 或迁移流程重建后再调用。它们都是对象或服务器状态检查,不是权限错误,也不能与会话级序列案例互相替代。

诊断

先确认对象状态以及对象所属的后端会话。对本路径,确认序列并检查这个 backend 是否调用过 nextval;连接池中另一个连接调用过也不满足当前会话前提。在显式事务中,失败的 currval 会使事务中止,因此修复前要执行 ROLLBACKROLLBACK TO SAVEPOINT。其他 55000 路径还要检查恢复状态、预加载设置、索引有效性或对象自身前提。

处理

在同一 backend 先调用 nextval 再调用 currval;如果业务契约如此设计,也可以传入明确已知的值。自动提交时失败语句的事务已结束,可以重新发起下一条语句;显式事务中则先回滚或回滚到保存点,再调用 nextval。不要把它当作权限错误,也不要假设连接池中另一个连接的成功调用会初始化当前连接。

可复现案例

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

currval_before_nextval PG 10 / 18 有 SQL

前置条件

  • A runner-owned sequence exists.
  • The trigger and repair use one backend session.

触发

Call currval for a sequence before this backend has called nextval, then initialize and read it.

断言

  • SQLSTATE is 55000 with ERROR severity
  • The diagnostic says currval is not yet defined in this session
  • The failed autocommit session remains IDLE
  • nextval initializes currval on the same backend and both values are 10

处置

Use nextval in the same session before currval, or pass an explicitly known value; creating the sequence in another session does not initialize this session’s currval state.

清理

Close all runner connections and drop the case schema with an owner connection.

实测诊断

选定源码组为 ERROR,使用上述动态序列名模板。实测诊断中的序列名为 currval_sequence;错误后自动提交会话仍为 IDLE,同一会话随后通过 nextvalcurrval 返回 10。

报文模板

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

主消息 pg_stat_statements must be loaded via "shared_preload_libraries"

来源:contrib/pg_stat_statements/pg_stat_statements.c @ REL_18_6

主消息 recovery is in progress
DETAIL Heap surgery functions cannot be executed during recovery.

来源:contrib/pg_surgery/heap_surgery.c @ REL_18_6

主消息 index "%s" is not valid

来源:contrib/pgstattuple/pgstatindex.c @ REL_18_6

主消息 currval of sequence "%s" is not yet defined in this session

来源:src/backend/commands/sequence.c @ REL_18_6

主消息 currval of sequence "%s" is not yet defined in this session

来源:src/backend/commands/sequence.c @ REL_10_23

代表案例

选定注册表在一个自动提交 backend 上创建序列,然后在尚未调用 nextval 时调用 currval,再用 nextval(返回 10)初始化并再次读取 currval(返回 10)。失败调用、修复和验证属于同一会话状态转换;本案例不测试连接池切换。

CREATE SEQUENCE sequence_identifier START WITH 10;
SELECT currval('sequence_regclass');
SELECT nextval('sequence_regclass');
SELECT currval('sequence_regclass')

选定的 PostgreSQL 18.6 与 10.21 运行均通过 SQLSTATE、严重级别、状态或断开恢复、修复、清理和一次性实例停止断言。详见 案例 JSON作者证据

版本

锁定目录从 7.4 起记录 55000。18.6 与 10.21 的序列会话案例均通过;这不覆盖其他 55000 分支,也不推断所有中间版本。

来源

证据

断言

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

  • 55000 is object_not_in_prerequisite_state in SQLSTATE Class 55.

    核实方式Read fixed definition.

    来源src/backend/utils/errcodes.txt

  • The cited PostgreSQL 18.6 source paths support the representative resource and object-state mechanisms described.

    核实方式Read complete fixed source contexts.

    不覆盖Source confirmation is not natural runtime.

    来源contrib/pg_stat_statements/pg_stat_statements.c · contrib/pg_surgery/heap_surgery.c · contrib/pgstattuple/pgstatindex.c

  • The locked catalogue records 55000 from 7.4; that boundary does not prove exact implementation introduction.

    核实方式Use catalogue boundary.

    来源src/backend/utils/errcodes.txt

  • The sequence currval path requires a prior nextval call in the same backend session and reports 55000 before that prerequisite exists; creating the sequence does not initialize the session-local last-value state.

    核实方式Read fixed sequence.c source-call records and compare same-session registry assertions.

    不覆盖Other 55000 paths represent different object or server states.

    来源src/backend/commands/sequence.c · src/backend/commands/sequence.c

  • The currval_before_nextval case passed on isolated PostgreSQL 18.6 and 10.21 targets; nextval initialized currval on the same session and returned 10.

    核实方式Execute the shared registry and inspect SQLSTATE, severity, session state, repair, cleanup, and target stop.

    不覆盖Scope is limited to sequence session state and does not cover recovery, preload, or invalid-index branches.

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

运行记录

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

同类错误代码

Class 55 前置状态不满足 下的其他成员。