SQLSTATE 22004Severity lowLab verified

Incident brief

Null value not allowed

A PL/pgSQL variable declared NOT NULL was assigned a null value. PostgreSQL rejected the assignment instead of silently storing null.

Reproduced on PostgreSQL 16.14Verified 2026-07-16 (Docker lab, PostgreSQL 16.14)Reviewed by Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What
Null value not allowed
What triggers it
Declare a PL/pgSQL variable with NOT NULL and a default value.
The fix
Don't assign NULL to a variable declared NOT NULL — check for null first with an IF, or use COALESCE on the source value before assigning.
Proof
Reproduced on PostgreSQL 16.14 → Assigning NULL to a NOT NULL PL/pgSQL variable was rejected with SQLSTATE 22004. The session was unaffected afterward.

The fix

What to do right now

The immediate, application-level response to this error.

  • Don't assign NULL to a variable declared NOT NULL — check for null first with an IF, or use COALESCE on the source value before assigning.
  • If null is a valid outcome, drop NOT NULL from the declaration instead of fighting it.
Fix — SQL
DECLARE
  v int NOT NULL := 5;
  incoming int := NULL;
BEGIN
  v := COALESCE(incoming, v); -- keeps v's last good value instead of assigning null
END;

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 16.14
Last verified
2026-07-16 (Docker lab, PostgreSQL 16.14)
Reviewed by
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed