P0004ERRORTier 2 — Caution✅ HIGH confidenceassert_failure
What this means
A PL/pgSQL ASSERT statement evaluated its condition as false, causing the function to abort. Used for invariant checking and developer assertions in PL/pgSQL code.
Why it happens
- 1ASSERT condition evaluated to false or NULL
- 2Invariant assumed by the developer is violated by actual data or state
- 3ASSERT used as a debugging tool and left in production code with an edge case not anticipated
How to reproduce
PL/pgSQL function or DO block containing an ASSERT statement whose condition fails
DO $
DECLARE
total int;
BEGIN
SELECT count(*) INTO total FROM orders;
ASSERT total > 0, 'orders table must not be empty';
END;
$;Fix 1: Fix the condition that caused the assertion to fail
The assertion represents a genuine invariant that is now violated
-- Investigate and correct the data or logic that violated the invariantWhy this works
Address the root cause rather than removing or disabling the assertion
Fix 2: Disable assertions for production if they are development-only checks
Assertions are not appropriate for production workloads
-- In postgresql.conf:
-- plpgsql.check_asserts = offWhy this works
Setting plpgsql.check_asserts=off disables all ASSERT checks without removing them from code
What not to do
Do not remove ASSERT statements to fix the error without investigating why the assertion failed
Why it's wrong: Assertions exist to catch bugs; removing them hides the underlying problem
Version notes
9.5ASSERT statement added in PostgreSQL 9.5Sources
📚 Official docs: https://www.postgresql.org/docs/current/plpgsql-errors-and-messages.html#PLPGSQL-STATEMENTS-ASSERT
🔧 Source ref: https://www.postgresql.org/docs/current/errcodes-appendix.html
Confidence assessment
✅ HIGH confidence
Standard PL/pgSQL error SQLSTATE from official PostgreSQL appendix.
See also
🔗 Related errors
📄 Reference pages