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
- Read the CONTEXT to locate the ASSERT; investigate the failed invariant.
- Fix the logic/data that violated the assumption.
- Disable assertions in production with
plpgsql.check_asserts = offif appropriate.
Related & next steps
Reference: PostgreSQL 18 Section 43.9.5 “Checking Assertions”.
Thanks — noted. This helps keep the database accurate.