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.

What lands in your log

ERROR: null value cannot be assigned to variable "v" declared NOT NULL

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

In 10 seconds

What triggers it
Declare a PL/pgSQL variable with NOT NULL and a default value.
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.

Fix

What to do right now

Application-level steps for 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
DO $$
DECLARE
  v int NOT NULL := 5;
BEGIN
  v := coalesce(NULL::int, 0);
  RAISE NOTICE 'v = %', v;
END $$;

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

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

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops