PostgreSQL Error 20000: case_not_found — What It Means and How to Fix It

PostgreSQL error code 20000, known as case_not_found, is a runtime error thrown inside PL/pgSQL when a CASE statement has no matching WHEN clause for the given input value and no ELSE clause is defined. Unlike a plain SQL CASE expression (which simply returns NULL when no condition matches), a PL/pgSQL CASE statement requires a matching branch to exist — otherwise it raises this exception immediately. Understanding the distinction is critical for writing robust database functions and stored procedures.

Top 3 Causes

1. Missing ELSE Clause

The most common cause is simply forgetting to add an ELSE clause to a PL/pgSQL CASE statement. When an unexpected value arrives — even just once — the entire function fails.