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

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

文档 / SQL 状态码 / Class 08 连接异常

08001 sqlclient_unable_to_establish_sqlconnection

无法建立 SQL 连接

ERROR 已实测 详解 实测通过

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

版本覆盖

速览

SQLSTATE 08001 是 Class 08 中的 sqlclient_unable_to_establish_sqlconnection08001 是服务器端无法建立客户端连接时使用的 SQLSTATE。选定的 dblink_connect 路径 ERROR 主报文为 could not establish connection,拒绝端口原因放在动态 DETAIL 中;这与 dblink 句柄不存在的 08003,以及启动阶段的 28000、28P01、3D000 不同。

含义

服务器端 dblink_connect 路径在 libpq 无法打开目标远端端点时发出此码。固定诊断为 ERROR,主报文是 could not establish connection,端点和操作系统原因放在动态 DETAIL 中。它描述的是建立命名远端句柄,与句柄不存在的 08003,以及会话尚未建立就被拒绝的 28000 或 3D000 不同。

源码并不自行格式化 DETAIL:dblink.c 通过 errdetail_internal("%s", msg) 传递 libpq 已经组装好的错误字符串。因此,下面选定运行中的 18.6 拒绝端口文本(含主机、端口和 Connection refused)以及 10.21 的 libpq 文本都只是运行观察,不是 PostgreSQL 固定的 08001 模板。

诊断

记录目标主机、端口、认证参数和完整 DETAIL。选定的 18.6/10.21 运行中,拒绝连接后本地自动提交会话保持 IDLE;随后用真实 dblink 句柄连到运行器实例,执行远端 SELECT 1,再显式断开。它证明本地恢复和句柄清理,不证明任何远端业务事务已经完成。

先判断失败阶段再重试。TCP 拒绝、DNS/TLS 失败和认证拒绝都可能由 libpq 通过这个 dblink 路径返回,必须查看动态 DETAIL,不能只按代码分类。若调用位于显式本地事务中,ERROR 可能使事务在 ROLLBACK(或 ROLLBACK TO SAVEPOINT)前不可继续;选定案例中的自动提交 IDLE 不能推广到显式事务。dblink 句柄属于创建它的后端,应在同一会话或同一个连接池成员中检查和修复。

处理

修正端点或连接参数,建立新句柄并先验证无副作用的远端探针,再发送业务操作。若失败请求可能已经越过远端边界,重试前先对账;单凭 08001 不能重放非幂等操作。

自动提交时,修正端点后可以把建连当作新的语句重试,所有者会话仍可能可用。显式事务中应先恢复本地事务,再建立并探测新句柄;只有周围工作明确设计为可继续时,保存点才适合使用。若远端操作可能已经到达目标而本地只收到错误,重放前先核对结果。

可复现案例

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

dblink_connect_failure PG 10 / 18 有 SQL

前置条件

  • A runner-owned disposable target is provisioned.

触发

Connect to a runner-local refused port through server-side dblink.

断言

  • The selected server diagnostic has the expected SQLSTATE
  • The selected recovery/probe assertions pass
  • Runner-owned resources are cleaned up

处置

Follow the case-specific repair statements and verify the resulting state.

清理

Drop the case schema with an owner connection.

实测诊断

固定的 dblink_connect 路径以 ERROR 发出主报文 could not establish connection,并通过 errdetail_internal("%s", msg) 传递动态 libpq 字符串。选定的 18.6 运行中该值为 connection to server at "127.0.0.1", port 1 failed: Connection refused 及 libpq 的后续提示;10.21 运行中则以 could not connect to server: Connection refused 开头。这些是运行特定值,不是固定 SQLSTATE 模板。其他 producer 可能使用不同文本;只有客户端异常而没有服务器诊断,不能据此认定此 SQLSTATE。

这个 dblink 路径的严重级别固定为 ERROR。选定本地会话使用自动提交,所以错误后仍为 IDLE;不要把这个状态推广到外层显式事务,或推广到未能确认结果的远端事务。

报文模板

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

主消息 could not establish connection
DETAIL %s

来源:contrib/dblink/dblink.c @ REL_18_6 · contrib/dblink/dblink.c @ REL_10_23

代表案例

此 SQL 块创建 dblink,尝试连接运行器本地的拒绝端口,然后探测所有者会话。触发点是服务器端 dblink 建连,不会在远端事务中执行工作。

runner_hostrunner_portrunner_dbrunner_user 是运行器占位参数,手工复制时不能照字面使用。应替换为可访问的目标和有权连接它的登录角色;安装/使用 dblink 以及远端登录都需要相应权限。若要复现选定的恢复观察,应在同一个所有者后端中按案例的自动提交边界执行这些语句。

CREATE EXTENSION IF NOT EXISTS dblink;
SELECT dblink_connect('missing_remote', 'host=127.0.0.1 port=1 dbname=postgres connect_timeout=1');
SELECT dblink_connect('working_remote', 'host=runner_host port=runner_port dbname=runner_db user=runner_user connect_timeout=5');
SELECT * FROM dblink('working_remote', 'SELECT 1') AS result(value integer);
SELECT dblink_disconnect('working_remote');
SELECT 1;

18.6 运行记录 SQLSTATE 为 08001,主报文 could not establish connection;错误后所有者会话为 IDLE。修复打开句柄返回 OK,远端返回 1,断开返回 OK;最终探针返回 1,状态 IDLE。10.21 也通过同样断言。

可下载的案例与证据投影分别是 08001 案例 JSON作者证据。运行器清单为 verify/cases/08001/cases.json;发布前会将页面 SQL 与共享注册表比对。

版本

上面的生成事实表记录锁定的目录快照和最早观察到的定义。本页自然运行范围是 PostgreSQL 18.6 与 10.21,不能据此推断所有中间版本的行为。

来源

证据

断言

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

  • 08001 is the sqlclient_unable_to_establish_sqlconnection 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 associated with 08001 report the condition in the mechanism selected for this page.

    核实方式Trace the resolved source call records at the fixed release commits and compare their dynamic message fields.

    不覆盖Other calls can retain the same SQLSTATE with different context or text.

    来源contrib/dblink/dblink.c · contrib/dblink/dblink.c

  • The selected dblink_connect_failure case passed with the expected structured diagnostics, recovery assertions, and cleanup on PostgreSQL 18.6 and 10.21.

    核实方式Run the shared registry case on isolated runner-owned latest and PG10 targets; inspect the final summaries and raw results.

    不覆盖This covers the selected case and versions only; it does not generalize to every driver, proxy, or intermediate release.

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

  • The selected dblink ERROR is observed under autocommit, where the owner session remains IDLE; when the same ERROR occurs inside an explicit transaction, PostgreSQL's transaction recovery rules require rollback or an intentional savepoint before unrelated work.

    核实方式Combine the fixed dblink ERROR source path with the official transaction and savepoint recovery contract.

    不覆盖No explicit-transaction dblink runtime was selected in this batch; the transaction guidance is the general ERROR boundary, not a claim about a specific remote topology.

    来源contrib/dblink/dblink.c · doc/src/sgml/xact.sgml

  • The locked catalogue records 08001 in the listed snapshots; the runtime comparison here is limited to PostgreSQL 18.6 and 10.21.

    核实方式Read the generated facts block and locked manifest, then compare the selected target summaries.

    不覆盖Presence in a definition file is not an exact behavioral introduction; the two runtime targets do not prove all middle versions.

    来源src/backend/utils/errcodes.txt · raw/calls/REL_18_6.jsonl · raw/calls/REL_10_23.jsonl

运行记录

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

同类 SQL 状态码

状态码 条件名 宏名称 严重等级 版本
Class 08 连接异常 Connection Exception 7 个
08000 connection_exception ERRCODE_CONNECTION_EXCEPTION ERROR 7.4
连接管理异常的通用条件,如 FDW 连接不可用。 活跃
08001 sqlclient_unable_to_establish_sqlconnection ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION ERROR 7.4
无法建立到目标服务器的新连接。 活跃
08003 connection_does_not_exist ERRCODE_CONNECTION_DOES_NOT_EXIST ERROR 7.4
指定名称的连接句柄在会话中不存在。 活跃
08004 sqlserver_rejected_establishment_of_sqlconnection ERRCODE_SQLSERVER_REJECTED_ESTABLISHMENT_OF_SQLCONNECTION ERROR 7.4
服务器拒绝建立 SQL 连接。 活跃
08006 connection_failure ERRCODE_CONNECTION_FAILURE ERROR 7.4
与服务器的连接在会话中途失败。 活跃
08007 transaction_resolution_unknown ERRCODE_TRANSACTION_RESOLUTION_UNKNOWN ERROR 7.4
连接中断导致事务提交结果未知。 活跃
08P01 protocol_violation ERRCODE_PROTOCOL_VIOLATION ERROR 7.4
前后端消息违反线路协议,通常是 FATAL。 活跃