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
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.
-- Add an ELSE branch so unmatched values are handled.
DO $$ BEGIN CASE 5 WHEN 1 THEN NULL; ELSE NULL; END CASE; END $$;For this error
See this error live on the server
Run these against the affected instance to confirm the diagnosis before you act.
CASE not found (no matching branch) comes from PL/pgSQL control flow. Inspect the exact stored function body and line reported in CONTEXT before changing server settings.
Stored PL/pgSQL source for the failing function
Replace the function signature from CONTEXT; this preserves overload identity.
SELECT pg_get_functiondef(to_regprocedure('<schema.function(argument_types)>'));PL/pgSQL assertion setting
Relevant to assertion failures; harmless context for the other PL/pgSQL control-flow errors.
SELECT current_setting('plpgsql.check_asserts', true) AS plpgsql_check_asserts;Why it happens
What PostgreSQL is telling you
The mechanism behind the error, grounded in the official manual, not paraphrased.
PostgreSQL 18 Documentation, Appendix A. PostgreSQL Error Codes (Table A.1, Class 20, Case Not Found)
20000 → case_not_foundRead the full section on postgresql.org →
Running a CASE with no matching branch
No WHEN matched and there was no ELSE, so PL/pgSQL raises 'case not found' and hints that the ELSE part is missing.The session continues normally
The unhandled CASE failed inside a single statement call, not an open transaction, so session B continues normally on its next query.Reproduce & verify
A real, single-session PostgreSQL reproduction
A literal transcript of SQL run against a live PostgreSQL instance in an isolated lab. The commands below are exactly what was executed.
- 1Write a PL/pgSQL CASE statement whose WHEN branches do not cover the runtime value and omit an ELSE.
- 2At execution the selector matches no branch.
- 3PL/pgSQL raises SQLSTATE 20000 with a hint that the ELSE part is missing.
A status-handling function hit an unexpected value that none of its WHEN branches covered, and the missing ELSE turned it into a runtime error.
-- The error is self-contained in one statement; no schema is required.
SELECT 'no schema needed' AS setup_note;DO $$ BEGIN CASE 5 WHEN 1 THEN NULL; WHEN 2 THEN NULL; END CASE; END $$;SELECT 'ok' AS session_after_error;What PostgreSQL actually returned
setup_note
------------------
no schema needed
(1 row)ERROR: case not found
HINT: CASE statement is missing ELSE part.
CONTEXT: PL/pgSQL function inline_code_block line 1 at CASE session_after_error
---------------------
ok
(1 row)The same block runs cleanly once an ELSE branch handles unmatched values.
Without this
Before: an unmatched CASE aborts
With this, tested
After: ELSE NULL handles the value
- A second operational test: exact SQL, raw output, measured result, and engineer notes
Card required. Cancel before day 7 and you are not charged.
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.
Verification
- 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
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.