# 39000 — external_routine_invocation_exception

> Source-backed reference for PostgreSQL SQLSTATE 39000.
---

# 39000

## At a glance {#at-a-glance}

Fixed PostgreSQL 18.6 paths in `contrib/pgcrypto` report this ERROR for cryptographic library failures and invalid input lengths. The category can also carry other external routine invocation errors, so the exact library function and dynamic error text matter.

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

| Field | Value |
| --- | --- |
| SQLSTATE | `39000` |
| Condition | `external_routine_invocation_exception` |
| Status | `active` |
| Known present by | `7.4` |
| Locked snapshots | `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` |
| Macros | `ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION` |
| Aliases | `—` |

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

## Meaning {#meaning}

Representative messages are `crypt(3) returned NULL`, `encrypt error: %s`, `decrypt error: %s`, `encrypt_iv error: %s`, `decrypt_iv error: %s`, and `Length not in range`. Several primary messages interpolate the dynamic library text returned by `px_strerror(err)`; it is part of the primary message, not a separate DETAIL field.

## Messages {#messages}

Representative messages include `crypt(3) returned NULL`, `encrypt error: %s`, `decrypt error: %s`, `encrypt_iv error: %s`, `decrypt_iv error: %s`, and `Length not in range`. Several variants put the dynamic library text from `px_strerror(err)` directly in the primary message; it is not a separate DETAIL field.

## Diagnosis {#diagnosis}

Identify the pgcrypto function and operation (crypt, encrypt/decrypt, IV form, or length validation). Preserve the dynamic error string and check input lengths, key/IV parameters, and library boundary; do not merge a library error with a SQLSTATE from a remote routine.

## Response {#response}

Correct the named input or library/configuration problem and validate the same pgcrypto operation. Retry only when the operation is safe and no externally visible work was completed; a failed encryption call does not justify replaying a surrounding write.

## Versions {#versions}

The locked catalogue places this condition at least by PostgreSQL 7.4. The facts block lists the published snapshots; source-path status is limited to the fixed PostgreSQL 18.6 evidence below.

## Related {#related}

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

## Sources {#sources}

Read the fixed [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), and [px.c](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/contrib/pgcrypto/px.c#L105-L107) paths alongside the structured [evidence record](../data/evidence/39000.json).
