惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
博客园_首页
T
Tailwind CSS Blog
The Cloudflare Blog
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
PostgreSQL 20000 Error: Causes and Solutions Complete Guide
umzzil nng · 2026-06-03 · via DEV Community

umzzil nng

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.

-- ❌ Problematic: No ELSE clause
CREATE OR REPLACE FUNCTION get_label(p_code TEXT)
RETURNS TEXT AS $$
BEGIN
    CASE p_code
        WHEN 'X' THEN RETURN 'Express';
        WHEN 'S' THEN RETURN 'Standard';
        -- Any other value raises ERROR 20000!
    END CASE;
END;
$$ LANGUAGE plpgsql;

-- ✅ Fixed: ELSE clause added
CREATE OR REPLACE FUNCTION get_label(p_code TEXT)
RETURNS TEXT AS $$
BEGIN
    CASE p_code
        WHEN 'X' THEN RETURN 'Express';
        WHEN 'S' THEN RETURN 'Standard';
        ELSE RETURN 'Unknown';  -- Safe fallback
    END CASE;
END;
$$ LANGUAGE plpgsql;

2. New Data Values After Deployment

Business requirements change. New status codes, categories, or types get added to the database, but the PL/pgSQL functions are never updated to handle them. This is one of the most dangerous causes because the function worked perfectly before and suddenly starts failing in production.

-- Suppose 'REFUNDED' was added to order statuses after deployment
CREATE OR REPLACE FUNCTION process_order(p_status TEXT)
RETURNS TEXT AS $$
BEGIN
    CASE p_status
        WHEN 'PENDING'   THEN RETURN 'Awaiting approval';
        WHEN 'APPROVED'  THEN RETURN 'Processing';
        WHEN 'SHIPPED'   THEN RETURN 'On the way';
        -- 'REFUNDED' was added later — triggers ERROR 20000
        ELSE
            -- Log unknown values for monitoring
            RAISE WARNING 'Unhandled status encountered: %', p_status;
            RETURN 'Status: ' || COALESCE(p_status, 'NULL');
    END CASE;
END;
$$ LANGUAGE plpgsql;

3. NULL Values Not Handled

In PL/pgSQL, NULL does not match any WHEN condition because NULL = NULL evaluates to NULL, not TRUE. If a column lacks a NOT NULL constraint, a NULL value will bypass every WHEN clause and trigger case_not_found.

-- ❌ NULL input causes ERROR 20000
CREATE OR REPLACE FUNCTION score_grade(p_score NUMERIC)
RETURNS TEXT AS $$
BEGIN
    CASE
        WHEN p_score >= 90 THEN RETURN 'A';
        WHEN p_score >= 80 THEN RETURN 'B';
        WHEN p_score >= 70 THEN RETURN 'C';
        -- NULL p_score matches NONE of the above!
    END CASE;
END;
$$ LANGUAGE plpgsql;

-- ✅ NULL handled explicitly
CREATE OR REPLACE FUNCTION score_grade(p_score NUMERIC)
RETURNS TEXT AS $$
BEGIN
    CASE
        WHEN p_score IS NULL THEN RETURN 'No Score';
        WHEN p_score >= 90   THEN RETURN 'A';
        WHEN p_score >= 80   THEN RETURN 'B';
        WHEN p_score >= 70   THEN RETURN 'C';
        ELSE                      RETURN 'F';
    END CASE;
END;
$$ LANGUAGE plpgsql;


Quick Fix: EXCEPTION Block

If you can't immediately refactor the function, wrap the logic in an EXCEPTION block to catch case_not_found gracefully:

CREATE OR REPLACE FUNCTION safe_get_label(p_code TEXT)
RETURNS TEXT AS $$
BEGIN
    CASE p_code
        WHEN 'X' THEN RETURN 'Express';
        WHEN 'S' THEN RETURN 'Standard';
    END CASE;
EXCEPTION
    WHEN case_not_found THEN
        RAISE WARNING 'case_not_found for input: %', p_code;
        RETURN 'Fallback Value';
END;
$$ LANGUAGE plpgsql;


Prevention Tips

Always include an ELSE clause. Treat it as a non-negotiable coding standard for every PL/pgSQL CASE statement. Even if you believe all values are covered, an ELSE with a RAISE WARNING or default return value prevents unexpected production failures.

Write boundary and NULL tests before deploying. Use pgTAP or a similar framework to test your functions with out-of-range values, NULL, and empty strings. Automate these tests in your CI/CD pipeline so any new data values that break existing logic are caught before they reach production.

-- Quick pgTAP smoke test example
SELECT plan(3);
SELECT is(safe_get_label('X'),    'Express',        'Known code X works');
SELECT is(safe_get_label(NULL),   'Fallback Value', 'NULL handled safely');
SELECT is(safe_get_label('ZZZZ'), 'Fallback Value', 'Unknown code handled');
SELECT * FROM finish();


Related Errors

Code Name Notes
P0001 raise_exception Explicit exception raised via RAISE EXCEPTION
P0002 no_data_found SELECT INTO returns no rows — common in the same PL/pgSQL context
P0003 too_many_rows SELECT INTO returns multiple rows

📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.