assertion failed

SQLSTATE P0004 condition assert_failure class P0 — PL/pgSQL Error severity ERROR
Reproduced & verified on PostgreSQL 14.23, 15.18, 16.14, 17.10 and 18.4 — identical message on every version.
Last reviewed 30 May 2025 · Reproduced live with the SQL on this page.

Symptoms

A PL/pgSQL ASSERT statement evaluated to false (or null). PostgreSQL raises SQLSTATE P0004 (assert_failure).

What the server log shows

ERROR:  assertion failed
CONTEXT:  PL/pgSQL function recompute_totals() line 8 at ASSERT

Why PostgreSQL raises this — what the manual says

Section 41.9.1 Checking Assertions:

“If the result is false or null, then an ASSERT_FAILURE exception is raised.”

ASSERT condition verifies an invariant during development/testing. When the condition is false or null and assertions are enabled, PostgreSQL raises P0004 to flag the broken assumption.

Common causes

How to fix it

  1. Read the CONTEXT to locate the ASSERT and investigate why the invariant failed.
  2. Fix the underlying logic/data that violated the assumption.
  3. Disable assertions in production with plpgsql.check_asserts = off if appropriate.

Related & next steps

Reference: PostgreSQL 18 Section 43.9.5 “Checking Assertions”.