↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

Wiki / SQLSTATE

25P02 — 事务处于失败状态(in_failed_sql_transaction)

事务已失败,后续语句被拒绝直到 ROLLBACK。

英文手册覆盖始于 PostgreSQL 8.1. 来源历史与运行验证各自保留独立版本边界。

当前阅读 PG 18·选择有来源记录的版本

aliases
未知
class name
Invalid Transaction State
condition name
in_failed_sql_transaction
lang
en
sqlstate
25P02

版本定义 PG 18

25P02 — 事务处于失败状态(in_failed_sql_transaction)

速览 {#at-a-glance}

25P02 通常是第二个诊断,而不是根因。选定案例先因重复键返回 23505,随后 SELECT 在事务仍为 INERROR 时被 25P02 拒绝;必须先 ROLLBACK 才能有效重试。

含义 {#meaning}

postgres.c 在再次规划普通命令前检查已中止事务状态,并报告 current transaction is aborted, commands ignored until end of transaction block。驱动会暴露对应的失败事务状态;原始错误仍是诊断锚点,25P02 表示该事务块已经不能继续执行普通业务语句。

诊断 {#diagnosis}

先定位 25P02 之前的第一条语句错误,再记录两个诊断和连接事务状态。选定运行显示根错误 23505、随后 25P02;两次之后都为 INERROR,ROLLBACK 后为 IDLE,新的显式 BEGIN/COMMIT 成功提交有效行。检查根错误前是否建立了保存点:ROLLBACK TO SAVEPOINT 可以保留外层事务;没有保存点时必须结束整个失败事务块。连接池不能把 INERROR 会话交给下一个请求。

处理 {#response}

第一条错误后停止发送业务语句,保留根 SQLSTATE 和详情。若事先建立的保存点仍可用,就回滚到该保存点并继续外层事务;否则执行 ROLLBACK,确认连接回到 IDLE,再开启新事务。应重新评估操作,不要盲目重放根语句。

源码报文 {#messages}

固定主报文是 current transaction is aborted, commands ignored until end of transaction block。它描述前一条错误留下的事务状态,不能覆盖根错误的 SQLSTATE、DETAIL 或约束身份。

实测诊断 {#observed}

18.6 (Homebrew) / latest:根 SQLSTATE 23505;次级 SQLSTATE 25P02;根错误/次级诊断后均为 INERROR;回滚后 IDLE;行集 [[1, 'seed'], [2, 'repaired']]。 10.21 (Debian 10.21-1.pgdg90+1) / pg10:根 SQLSTATE 23505;次级 SQLSTATE 25P02;根错误/次级诊断后均为 INERROR;回滚后 IDLE;行集 [[1, 'seed'], [2, 'repaired']]。

代表案例 {#case}

运行器从共享语句清单(registry)读取下列根错误、次级诊断、回滚和有效重试语句;完整断言、环境和清理见 案例导出。

-- create
CREATE TABLE items(id integer PRIMARY KEY, note text UNIQUE NOT NULL);
-- seed
INSERT INTO items VALUES (1, 'seed');
-- begin
BEGIN;
-- trigger
INSERT INTO items VALUES (2, 'seed');
-- followup
SELECT 1 AS ignored;
-- rollback
ROLLBACK;
-- repair_begin
BEGIN;
-- repair
INSERT INTO items VALUES (2, 'repaired');
-- commit
COMMIT;
-- verify
SELECT id, note FROM items ORDER BY id;

上述片段的 SQLSTATE、诊断、状态和修复断言来自共享语句清单(registry)(SHA-256 04da3240dcf3c03fe60d13715f8187350fadf5b8d1f10e5b837acd45289759cd);结构化证据。

作者证据 ID:identity, abort-state, runtime。选定运行记录:runtime.25P02-batch2-latest-20260909.latest, runtime.25P02-batch2-pg10-20260909.pg10。

版本与边界 {#versions}

选定的“重复键后出现 25P02”案例在 PostgreSQL 18.6 与 10.21 通过。它证明事务状态恢复,不表示可以原样重试重复键操作。

相关 {#related}

23505 唯一约束冲突、25P01 没有活动 SQL 事务、40001 可串行化失败。

来源 {#sources}

  • src.errcodes.18.6 (SHA-256 6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba)
  • src.calls.REL_18_6 — raw/calls/REL_18_6.jsonl (SHA-256 9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf)
  • src.postgres.18.6 (SHA-256 9fb62275b1badf94d01ab351337b60410cd9b3ab1fe63fa9f23d6d2185a21061)
  • doc.libpq.18.6 (SHA-256 a91ce8f29dde162245d2f35f839e8b7192f62a57e01020b80564af65edd09440) · official documentation

比较版本

SQLSTATE 证据与已记录的运行版本

cases · failed_transaction_recovery
{
  "assertions": [
    "The root duplicate is 23505",
    "The follow-up command reports 25P02",
    "The failed transaction remains INERROR until ROLLBACK",
    "A new explicit transaction commits the repaired row"
  ],
  "case_id": "failed_transaction_recovery",
  "cleanup": "Drop the case schema with an owner connection.",
  "has_snippet": true,
  "position": 0,
  "preconditions": [
    "A runner-owned disposable target is provisioned."
  ],
  "repair": "Follow the explicit transaction or source boundary described by the case.",
  "trigger": "Raise a root unique violation, then issue a query before ROLLBACK.",
  "versions": [
    "10",
    "18"
  ]
}
claims · identity
{
  "claim_id": "identity",
  "limits": "It describes the transaction state after a prior error; it is often a secondary diagnostic.",
  "method": "Read the fixed condition row and macro.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "25P02 is in_failed_sql_transaction in Class 25."
}
claims · abort-state
{
  "claim_id": "abort-state",
  "limits": "The first error remains the root cause; 25P02 tells the client to stop issuing ordinary work and end the transaction.",
  "method": "Trace IsAbortedTransactionBlockState and the libpq PQTRANS_INERROR state.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.postgres.18.6",
    "src.calls.REL_18_6",
    "doc.libpq.18.6"
  ],
  "statement": "After an error aborts a transaction block, postgres.c rejects subsequent commands with 25P02 until COMMIT/ROLLBACK ends the block."
}
claims · runtime
{
  "claim_id": "runtime",
  "limits": "The repair demonstrates transaction-state recovery; it does not make the original duplicate valid.",
  "method": "Read the selected summaries and structured diagnostics.",
  "position": 2,
  "runtime": [],
  "sources": [
    "runtime",
    "runtime.25P02-batch2-latest-20260909.latest",
    "runtime.25P02-batch2-pg10-20260909.pg10"
  ],
  "statement": "The selected case first raises 23505, then observes 25P02 while INERROR, rolls back to IDLE, and commits a valid row in a new explicit transaction on both targets."
}
messages · aborted
{
  "limits": "The message is secondary to the earlier statement error; errdetail_abort may add context.",
  "message_id": "aborted",
  "path": "",
  "position": 0,
  "raw": {
    "id": "aborted",
    "limits": "The message is secondary to the earlier statement error; errdetail_abort may add context.",
    "primary_template": "current transaction is aborted, commands ignored until end of transaction block",
    "severity_source": "ERROR",
    "sources": [
      "src.postgres.18.6"
    ],
    "sqlstate": "25P02"
  },
  "severity": "ERROR",
  "sources": [
    "src.postgres.18.6"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "current transaction is aborted, commands ignored until end of transaction block",
      "position": 0,
      "role": "",
      "template": "current transaction is aborted, commands ignored until end of transaction block"
    }
  ]
}
runtimes · runtime.25P02-batch2-latest-20260909.latest · 18.6 (Homebrew) · passed
{
  "cases": [
    "failed_transaction_recovery"
  ],
  "limits": "",
  "position": 0,
  "raw": {
    "case_manifest": {
      "path": "verify/cases/25P02/cases.json",
      "sha256": "dc6dea41ff7dc64f413c290151ee8200b11d66050eb5de499c64ef7aaf87f88c"
    },
    "case_status": "passed",
    "cases": [
      "failed_transaction_recovery"
    ],
    "id": "runtime.25P02-batch2-latest-20260909.latest",
    "observed": {
      "after_25P02": "INERROR",
      "after_rollback": "IDLE",
      "after_root_error": "INERROR",
      "final_status": "IDLE",
      "primary": "current transaction is aborted, commands ignored until end of transaction block",
      "root_primary": "duplicate key value violates unique constraint \"items_note_key\"",
      "root_sqlstate": "23505",
      "rows_after_retry": [
        [
          1,
          "seed"
        ],
        [
          2,
          "repaired"
        ]
      ],
      "sqlstate": "25P02"
    },
    "raw": "verify/results/25P02-batch2-latest-20260909/latest/raw.jsonl",
    "raw_sha256": "f03391117a5430c0611c72a1fa03ee6169f72575e69733a98023b0c9fb255788",
    "run_id": "25P02-batch2-latest-20260909",
    "server_version": "18.6 (Homebrew)",
    "server_version_num": 180006,
    "snippet_registry": {
      "path": "verify/cases/25P02/snippets.json",
      "sha256": "04da3240dcf3c03fe60d13715f8187350fadf5b8d1f10e5b837acd45289759cd"
    },
    "status": "passed",
    "summary": "verify/results/25P02-batch2-latest-20260909/latest/summary.json",
    "summary_sha256": "6c06ba72ebf7ce12c171d8a45ceec233b03eedc0db98796221d00c8b0e34732d",
    "target": "latest",
    "target_status": "passed"
  },
  "run_id": "25P02-batch2-latest-20260909",
  "runtime_id": "runtime.25P02-batch2-latest-20260909.latest",
  "server_version": "18.6 (Homebrew)",
  "status": "passed",
  "target": "latest"
}
runtimes · runtime.25P02-batch2-pg10-20260909.pg10 · 10.21 (Debian 10.21-1.pgdg90+1) · passed
{
  "cases": [
    "failed_transaction_recovery"
  ],
  "limits": "",
  "position": 1,
  "raw": {
    "case_manifest": {
      "path": "verify/cases/25P02/cases.json",
      "sha256": "dc6dea41ff7dc64f413c290151ee8200b11d66050eb5de499c64ef7aaf87f88c"
    },
    "case_status": "passed",
    "cases": [
      "failed_transaction_recovery"
    ],
    "id": "runtime.25P02-batch2-pg10-20260909.pg10",
    "observed": {
      "after_25P02": "INERROR",
      "after_rollback": "IDLE",
      "after_root_error": "INERROR",
      "final_status": "IDLE",
      "primary": "current transaction is aborted, commands ignored until end of transaction block",
      "root_primary": "duplicate key value violates unique constraint \"items_note_key\"",
      "root_sqlstate": "23505",
      "rows_after_retry": [
        [
          1,
          "seed"
        ],
        [
          2,
          "repaired"
        ]
      ],
      "sqlstate": "25P02"
    },
    "raw": "verify/results/25P02-batch2-pg10-20260909/pg10/raw.jsonl",
    "raw_sha256": "97d4ce9f816c98c09408bd00921a97e33708ccf52ca10da0682ab7238ef377a6",
    "run_id": "25P02-batch2-pg10-20260909",
    "server_version": "10.21 (Debian 10.21-1.pgdg90+1)",
    "server_version_num": 100021,
    "snippet_registry": {
      "path": "verify/cases/25P02/snippets.json",
      "sha256": "04da3240dcf3c03fe60d13715f8187350fadf5b8d1f10e5b837acd45289759cd"
    },
    "status": "passed",
    "summary": "verify/results/25P02-batch2-pg10-20260909/pg10/summary.json",
    "summary_sha256": "722e8d8c8ddc1301c1c7acb98d9e6442dde6f3a2020246b30775dec8c98a3847",
    "target": "pg10",
    "target_status": "passed"
  },
  "run_id": "25P02-batch2-pg10-20260909",
  "runtime_id": "runtime.25P02-batch2-pg10-20260909.pg10",
  "server_version": "10.21 (Debian 10.21-1.pgdg90+1)",
  "status": "passed",
  "target": "pg10"
}
sources · src.errcodes.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/errcodes.txt",
  "position": 0,
  "sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
  "source_id": "src.errcodes.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt"
}
sources · src.calls.REL_18_6
{
  "commit": "",
  "docs_url": "",
  "kind": "local_artifact",
  "location": "",
  "path": "raw/calls/REL_18_6.jsonl",
  "position": 1,
  "sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
  "source_id": "src.calls.REL_18_6",
  "tag": "",
  "url": ""
}
sources · src.postgres.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/tcop/postgres.c",
  "position": 2,
  "sha256": "9fb62275b1badf94d01ab351337b60410cd9b3ab1fe63fa9f23d6d2185a21061",
  "source_id": "src.postgres.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/postgres.c#L1127-L1140"
}
sources · doc.libpq.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "https://www.postgresql.org/docs/18/libpq-status.html",
  "kind": "upstream_source",
  "location": "",
  "path": "doc/src/sgml/libpq.sgml",
  "position": 3,
  "sha256": "a91ce8f29dde162245d2f35f839e8b7192f62a57e01020b80564af65edd09440",
  "source_id": "doc.libpq.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/doc/src/sgml/libpq.sgml#L2818-L2835"
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:71cbabe45a6cf257be8691b85a72a43985b2835a07e2e1234a2922de8a3de492

正文语言: zh-Hans · english-manuals:71cbabe45a6cf257be8691b85a72a43985b2835a07e2e1234a2922de8a3de492