SQLSTATE 22012Severity lowLab verified

Incident brief

Division by zero

An expression divided a number by zero. PostgreSQL raises an error instead of returning infinity or an undefined result.

What lands in your log

ERROR: division by zero

Reproduced on PostgreSQL 16.14Verified 2026-07-15 (Docker lab, PostgreSQL 16.14)Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What triggers it
Run any expression that divides by a literal or computed zero.
Fix
Guard the divisor with NULLIF(divisor, 0) so a zero divisor produces NULL instead of an error.
Proof
Reproduced on PostgreSQL 16.14 → 10 / 0 was rejected with SQLSTATE 22012. Outside a transaction block, the session was unaffected, the very next statement ran and returned a normal result.

Fix

What to do right now

Application-level steps for this error.

  • Guard the divisor with NULLIF(divisor, 0) so a zero divisor produces NULL instead of an error.
  • Validate the divisor in application code before sending the query, if a specific fallback value is required instead of NULL.
  • Inside a transaction, remember this error aborts the transaction just like any other, see SQLSTATE 25P02.
Fix SQL
-- NULLIF turns a zero divisor into NULL instead of raising an error
SELECT 10 / NULLIF(0, 0) AS guarded_result;

Verification

PG 16.14
Last verified
2026-07-15 (Docker lab, PostgreSQL 16.14)
Verification scope
Verified against PostgreSQL 16.14 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