Diagnostic Queries
Symptoms
A PL/pgSQL ASSERT statement evaluated to false (or null). PostgreSQL raises SQLSTATE P0004 (assert_failure).
- An ASSERT condition in a function did not hold.
- Indicates an internal invariant was violated.
- Assertions are enabled 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 during development/testing. When the condition is false or null and assertions are enabled, PostgreSQL raises P0004 to flag the broken assumption.
Common causes
- An invariant assumed by the function did not hold at runtime.
- A logic bug producing unexpected intermediate state.
- Assertions left enabled in an environment hitting an edge case.
How to fix it
- Read the CONTEXT to locate the ASSERT and investigate why the invariant failed.
- Fix the underlying 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.