PG
PRO
39004ERRORTier 2 — Caution✅ HIGH confidence

null value not allowed

Category: External Routine Invocation ExceptionVersions: All Postgres versions

What this means

SQLSTATE 39004 is raised when an external routine declared STRICT returns NULL in a context where NULL is not allowed, or passes NULL to a STRICT function argument.

Why it happens

  1. 1An external function declared STRICT is called with a NULL argument (STRICT functions return NULL for any NULL argument without executing — 39004 is raised if this contract is violated)

How to reproduce

STRICT external function receiving a NULL argument explicitly.

trigger — this will ERROR
ERROR: null value not allowed

Fix 1: Filter NULLs before calling STRICT functions

When calling STRICT external functions with nullable data.

fix
SELECT my_strict_fn(col) FROM data WHERE col IS NOT NULL;

Why this works

STRICT functions short-circuit on NULL inputs by convention, but filtering NULLs in SQL prevents the issue entirely.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 39 — External Routine Invocation Exception

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE. Stable across versions.

See also

📄 Reference pages

STRICT FunctionsExternal Language Functions
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →