39001
PostgreSQLERRORNotableExternal Routine Invocation ExceptionHIGH confidence

invalid SQLSTATE returned

What this means

SQLSTATE 39001 is raised when an external routine returns or raises a SQLSTATE code that is not a valid 5-character SQLSTATE string.

Why it happens
  1. 1An external function raises an error with an invalid SQLSTATE code (not 5 alphanumeric characters)
How to reproduce

External function raising an invalid SQLSTATE.

trigger — this will error
trigger — this will error
-- PL/Python example:
CREATE FUNCTION bad_sqlstate() RETURNS VOID AS $
plpy.error("bad state", sqlstate="XXXXX99") -- invalid format
$ LANGUAGE plpython3u;

expected output

ERROR:  invalid SQLSTATE code

Fix

Use a valid 5-character SQLSTATE code when raising errors in external functions

WHEN When raising custom errors from PL/Python, PL/Perl, or C functions.

Why this works

SQLSTATE codes must be exactly 5 alphanumeric characters. Use a user-defined exception code like P0001 or raise with an existing standard code.

Sources
Official documentation ↗

Class 39 — External Routine Invocation Exception

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors