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.

What lands in your log

ERROR: ledger must have exactly one open row

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

In 10 seconds

What triggers it
Run a PL/pgSQL block containing ASSERT <condition>, <message> where the condition is false at runtime.
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.

Fix

What to do right now

Application-level steps for 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 $$;

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

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

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops