# 39000 — external_routine_invocation_exception

> PostgreSQL SQLSTATE 39000 的源码与诊断参考。
---

# 39000

## 速览 {#at-a-glance}

SQLSTATE 39000 是外部例程调用异常类别。固定的 PostgreSQL 18.6 pgcrypto 路径会为加密库失败和输入长度无效报告该 ERROR；具体函数与动态错误文本决定诊断方向。

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| 字段 | 值 |
| --- | --- |
| SQLSTATE | `39000` |
| 条件名 | `external_routine_invocation_exception` |
| 状态 | `有效` |
| 已知存在于 | `7.4` |
| 锁定快照 | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| 宏 | `ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION` |
| 别名 | `—` |

<!-- source facts: data/errcodes/39000.json -->
<!-- END SQLSTATE FACTS -->

## 含义 {#meaning}

代表性消息包括 `crypt(3) returned NULL`、`encrypt error: %s`、`decrypt error: %s`、`encrypt_iv error: %s`、`decrypt_iv error: %s` 和 `Length not in range`。多个主消息会直接插入 `px_strerror(err)` 返回的动态库文本；这里的动态内容属于主消息，不是独立的 DETAIL 字段。

## 消息 {#messages}

代表性消息包括 `crypt(3) returned NULL`、`encrypt error: %s`、`decrypt error: %s`、`encrypt_iv error: %s`、`decrypt_iv error: %s` 和 `Length not in range`。多个变体把 `px_strerror(err)` 返回的动态库文本直接放入主消息；它不是独立的 DETAIL 字段。

## 诊断 {#diagnosis}

确认 pgcrypto 函数和操作（crypt、加解密、IV 形式或长度校验）。保留动态错误字符串，检查输入长度、key/IV 参数和库边界；不要把库错误与远端例程的 SQLSTATE 合并。

## 处理 {#response}

修正点名的输入或库/配置问题，并验证同一 pgcrypto 操作。只有操作可安全重复且没有完成外部可见工作时才重试；加密调用失败不能成为重放外层写入的理由。

## 版本 {#versions}

锁定目录将该条件的已知下界记为 PostgreSQL 7.4；事实块列出已发布快照，源码路径状态仅限于下方固定的 PostgreSQL 18.6 资料。

## 相关 {#related}

[`39001`](../39001/), [`2F000`](../2f000/)

## 来源 {#sources}

可直接阅读固定的 [pgcrypto.c](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/contrib/pgcrypto/pgcrypto.c#L234-L465)、[pgp-cfb.c](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/contrib/pgcrypto/pgp-cfb.c#L237-L239) 和 [px.c](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/contrib/pgcrypto/px.c#L105-L107) 路径；完整范围和未解决的运行边界见结构化[证据记录](../../data/evidence/39000.json)。
