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

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

百科 / 错误代码 / Class 08 连接异常

08006 connection_failure

连接失败

ERROR 源码确认 详解 未实测

类别
Class 08 连接异常
严重等级
ERROR
条件名
connection_failure
宏名称
ERRCODE_CONNECTION_FAILURE
启用版本
7.4
状态
活跃

版本覆盖

速览

SQLSTATE 08006 是 Class 08 中的 connection_failure08006 表示已有连接发生故障。固定路径包括 postgres_fdw 的取消/连接丢失处理以及 COPY 失败处理;它们没有一个适用于所有路径的主报文或严重级别,本批也有意不做普通 socket 的伪案例。

含义

固定源码路径说明了为什么本类没有统一报文。在 postgres_fdw 中,取消请求失败时,在处理已有外部连接的过程中发出 WARNING;这是取消路径的诊断,本身不证明客户端会话已经终止。在 COPY 路径中,带有未完成事务的客户端 EOF 以 ERROR 报告,而较旧的 COPY-to-stdout 路径在输出连接丢失时以 FATAL 报告。严重级别和发生阶段都是含义的一部分,不能把它们合并成“socket 断了”。

ERROR 分支若发生在显式事务中,仍连接着的会话可能进入 INERROR,调用者应先 ROLLBACK 或回到合适的保存点再执行无关 SQL。FATAL 分支会结束后端连接,不存在可以发送 ROLLBACK 的会话。已经读到 EOF 的客户端也可能根本没有 SQLSTATE。这里的证据确认这些源码分支,但没有声称每次普通网络断开都会发出 08006。

分支指引

固定分支 它说明什么 首先检查什么
postgres_fdw 取消 WARNING 已建立的外部连接上,取消请求无法发送或无法读取结果。 保留动态 libpq 文本、FDW 操作和本地事务状态;WARNING 本身不说明本地会话已关闭。
COPY 客户端 EOF ERROR 服务器在 COPY 相关连接仍有未完成事务时检测到 EOF。 触发该分支的客户端 socket 已经到达 EOF,不能在其上发送 ROLLBACK 或复用;应重新连接并核对 COPY 结果。
COPY-to-stdout FATAL 固定的 10.23 路径在 COPY 期间丢失输出连接,并使用 fatal 级别。 丢弃后端连接并核对已经发送或接收的数据;不能执行会话级回滚。
普通客户端 EOF 对端在固定服务器分支之外消失。 结合驱动/socket 和服务器日志;不要仅凭客户端 null SQLSTATE 指定 08006。

诊断

关联后端 PID、操作、服务器日志字段和客户端异常。客户端断 socket 可能得到 null SQLSTATE,而服务器端 FDW 或 COPY 路径可能发出带路径特定文本的 08006。决定是否重试前,区分 08001(尚未建立连接)、08007/40003(完成结果不确定)和语句尚未执行成功的普通错误。

要同时检查严重级别和生命周期。WARNING 表示 FDW 取消分支中本地会话可能仍然存活;ERROR 要检查 INERROR 还是 IDLE,必要时恢复事务;FATAL 表示后端已消失。如果客户端在 ErrorResponse 之前只读到 EOF,没有 SQLSTATE 是线路交换丢失的正常证据,不能据此认定 08006。

处理

丢弃连接池标记为损坏的连接,核对远端或非幂等操作的结果,并在明确幂等策略后重试完整操作。本页只确认固定源码边界;没有用 RAISE、超时或普通断 socket 冒充实测 08006。

对另一个仍保持连接的 ERROR 情形,应先回滚失败的显式事务(或回滚到围绕该操作有意建立的保存点),再决定能否重试完整操作。这个一般事务规则不能证明 COPY 客户端 EOF socket 可复用:源码分支已经检测到该客户端连接的 EOF。FATAL 或已经丢失的 socket 则要建立新会话,并在任何重放前核对结果。自动提交不会让远端或 COPY 操作自动具备幂等性,它只改变本地事务边界。

可复现案例

在一次性实例上执行过的场景。

source_boundary PG 10 / 18

前置条件

  • A runner-owned disposable target is provisioned only if a source-boundary smoke run is requested.

触发

No artificial SQL trigger is executed; this record preserves the fixed source/definition boundary.

处置

Use the concrete source path or component-specific documentation when it applies.

清理

Drop the case schema with an owner connection.

报文

固定分支包含以下不同诊断:

  • postgres_fdw 取消路径的 WARNINGcould not send cancel request: %s,其中 libpq 错误文本是动态值。
  • 同一 FDW 路径的 WARNINGcould not get result of cancel request: %s,返回的连接错误文本是动态值。
  • 固定 10.23 COPY 路径的 FATALconnection lost during COPY to stdout
  • 固定 18.6/10.23 COPY 客户端 EOF 路径的 ERRORunexpected EOF on client connection with an open transaction

证据将这些变体分开记录;普通客户端断 socket 得到的 null SQLSTATE 不能被提升为 08006。

即使报文都提到连接,ERRORFATAL 也有不同的恢复契约。COPY 客户端 EOF 的 ERROR 分支检测到客户端 socket 已经消失;它不建立可复用 socket,也不提供在该连接上发送 ROLLBACK 的路径。后者属于 COPY-to-stdout 路径,会关闭后端。本批没有自然 08006 运行,因此这些差异来自固定源码路径和文档规定的事务边界,而不是合成的 socket 实验。

报文模板

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

主消息 could not send cancel request: %s

来源:contrib/postgres_fdw/connection.c @ REL_18_6

适用范围:The %s value is the dynamic libpq/cancel error text.

主消息 could not get result of cancel request: %s

来源:contrib/postgres_fdw/connection.c @ REL_18_6

适用范围:The %s value is the dynamic error text returned while reading the cancel result; the fixed source has more than one call site.

主消息 connection lost during COPY to stdout

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

适用范围:This exact fixed branch is recorded from REL_10_23; the page does not generalize it to every COPY direction or release.

主消息 unexpected EOF on client connection with an open transaction

来源:src/backend/commands/copyfromparse.c @ REL_18_6 · src/backend/commands/copy.c @ REL_10_23

适用范围:The fixed COPY client-EOF path reports this when an open transaction is present; the source confirms the branch, not a selected natural run in this batch.

代表案例

本页没有选定的自然 SQL 运行。结构化证据记录的是源码或定义边界;客户端 RAISE 不能代表服务器机制。

版本

上面的生成事实表记录锁定的目录快照和最早观察到的定义。本页没有选定的自然 SQL 运行;固定的 REL_18_6/REL_10_23 源码边界不能当作实测结果,也不能据此推断所有中间版本的行为。

来源

证据

断言

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

  • 08006 is the connection_failure condition in Class 08.

    核实方式Read the fixed errcodes.txt definition and the locked catalogue metadata.

    不覆盖Directory identity does not identify every backend or client path.

    来源src/backend/utils/errcodes.txt

  • The fixed source paths resolve 08006 to distinct established-connection failure branches: postgres_fdw cancel warnings, a COPY-to-stdout FATAL in REL_10_23, and COPY client-EOF ERROR paths in REL_18_6 and REL_10_23. The exact diagnostic and severity depend on where the connection loss is detected.

    核实方式Trace the fixed REL_18_6 and REL_10_23 source call scans and the recorded source paths.

    不覆盖This is a bounded source claim; this batch has no selected natural 08006 runtime, and it does not claim that an ordinary client socket failure exposes this SQLSTATE.

    来源contrib/postgres_fdw/connection.c · src/backend/commands/copy.c · src/backend/commands/copyfromparse.c · src/backend/commands/copy.c

  • The fixed 08006 variants have different recovery contracts: the COPY client-EOF ERROR detects that the triggering client socket is already at EOF and therefore does not establish a reusable session for sending ROLLBACK; an independently still-connected ERROR can require rollback or savepoint recovery, while FATAL ends the backend; a client EOF without an ErrorResponse may expose no SQLSTATE.

    核实方式Combine the COPY client-EOF source branch, recorded source severities, and the official transaction and savepoint recovery contract.

    不覆盖This is a source-backed boundary; the COPY EOF branch says nothing about reusing the already-lost client socket, and this batch selected no natural 08006 runtime or ordinary socket failure.

    来源doc/src/sgml/xact.sgml

  • The locked catalogue records 08006 in the listed snapshots; this page compares only the fixed REL_18_6 and REL_10_23 source paths and has no selected natural runtime.

    核实方式Read the generated facts block and fixed source records; no runtime result is used for this source-only page.

    不覆盖Presence in a definition file is not an exact behavioral introduction, and the fixed source paths do not prove every intermediate release or socket failure.

    来源src/backend/utils/errcodes.txt · src/backend/commands/copyfromparse.c · src/backend/commands/copy.c

同类错误代码

Class 08 连接异常 下的其他成员。