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.

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

In 10 seconds

What
Division by zero
What triggers it
Run any expression that divides by a literal or computed zero.
The 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.

The fix

What to do right now

The immediate, application-level response to 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;

Related & next steps

Follow the thread

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

Verification

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