SQLSTATE P0004Severity lowLab verified

Incident brief

PL/pgSQL assertion failure

A PL/pgSQL ASSERT whose condition evaluates to false raises this error with the assertion's message; it signals a violated invariant, not ordinary data validation.

Reproduced on PostgreSQL 18.4Verified 2026-07-24 (isolated lab, PostgreSQL 18.4)Reviewed by Verified against PostgreSQL 18.4 in an isolated lab environment

In 10 seconds

What
PL/pgSQL assertion failure
What triggers it
Run a PL/pgSQL block containing ASSERT <condition>, <message> where the condition is false at runtime.
The fix
Make the asserted invariant hold before the ASSERT (fix the data or the preceding logic).
Proof
Reproduced on PostgreSQL 18.4 → A DO block reproduces SQLSTATE P0004; a false ASSERT condition aborts with its custom message and names the ASSERT in the CONTEXT.

The fix

What to do right now

The immediate, application-level response to this error.

  • Make the asserted invariant hold before the ASSERT (fix the data or the preceding logic).
  • Reserve ASSERT for should-never-happen invariants; use RAISE or validation for expected bad input.
Fix — SQL
-- Ensure the asserted invariant holds.
DO $$ BEGIN ASSERT (SELECT count(*) FROM (VALUES (1)) v) = 1, 'ok'; END $$;

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 18.4
Last verified
2026-07-24 (isolated lab, PostgreSQL 18.4)
Reviewed by
Verified against PostgreSQL 18.4 in an isolated lab environment
Audit status
reviewed