SQLSTATE 20000Severity lowLab verified

Incident brief

CASE not found (no matching branch)

A PL/pgSQL CASE statement with no matching WHEN and no ELSE raises this at runtime when the selector matches none of the branches.

What lands in your log

ERROR: case not found

Reproduced on PostgreSQL 18.4Verified 2026-07-24 (isolated lab, PostgreSQL 18.4)Verified against PostgreSQL 18.4 in an isolated lab environment

In 10 seconds

What triggers it
Write a PL/pgSQL CASE statement whose WHEN branches do not cover the runtime value and omit an ELSE.
Fix
Add an ELSE branch (even ELSE NULL) so unmatched values are handled.
Proof
Reproduced on PostgreSQL 18.4 → A single DO block reproduces SQLSTATE 20000; a CASE with no matching WHEN and no ELSE fails at runtime with a hint.

Fix

What to do right now

Application-level steps for this error.

  • Add an ELSE branch (even ELSE NULL) so unmatched values are handled.
  • Or ensure the WHEN branches exhaustively cover every possible value.
Fix SQL
-- Add an ELSE branch so unmatched values are handled.
DO $$ BEGIN CASE 5 WHEN 1 THEN NULL; ELSE NULL; END CASE; 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 18.4
Last verified
2026-07-24 (isolated lab, PostgreSQL 18.4)
Verification scope
Verified against PostgreSQL 18.4 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