SQLSTATE P0004 ERROR Class P0: PL/pgSQL Error

assert_failure assertion failed — P0004

PostgreSQL error “assertion failed — P0004” (SQLSTATE P0004): what it means, common causes, and how to fix it.

PG 9.6, 10, 11, 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed Jun 2026 Grounded in source

Diagnostic Queries

Symptoms

A PL/pgSQL ASSERT condition evaluated to false or null. PostgreSQL raises SQLSTATE P0004 (assert_failure). This is the errors-new counterpart of the assertion-failed message page.

  • An ASSERT in a function did not hold.
  • Indicates a violated internal invariant.
  • Controlled by plpgsql.check_asserts.

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. When the condition is false/null and assertions are enabled, PostgreSQL raises P0004 to flag the broken assumption.

Common causes

  • An invariant assumed by the function did not hold.
  • A logic bug producing unexpected state.
  • Assertions enabled in an environment hitting an edge case.

How to fix it

  1. Read the CONTEXT to locate the ASSERT; investigate the failed invariant.
  2. Fix the 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”.

Was this helpful?