22P01
PostgreSQLERRORNotableData ExceptionHIGH confidence

floating point exception

What this means

SQLSTATE 22P01 is a Postgres-specific error raised when a floating-point arithmetic operation produces an exception, such as overflow to infinity or an invalid operation (e.g., 0.0/0.0 producing NaN in a strict context).

Why it happens
  1. 1Floating-point division by zero (produces infinity in IEEE 754; some contexts treat this as an error)
  2. 2Floating-point operations producing Inf or NaN that are rejected by the calling context
How to reproduce

FP operation producing an exceptional result.

trigger — this will error
trigger — this will error
SELECT 1.0::float / 0.0::float; -- produces Infinity in Postgres (not 22P01)
-- 22P01 appears in contexts that reject Inf/NaN

expected output

ERROR:  floating-point exception

Fix 1

Check for zero denominators before FP division

WHEN When floating-point division may involve zero.

Check for zero denominators before FP division
SELECT CASE WHEN denom = 0.0 THEN NULL ELSE num / denom END FROM data;

Why this works

A CASE guard prevents the division from occurring when the denominator is zero.

Fix 2

Filter Inf and NaN results

WHEN When FP results must be finite.

Filter Inf and NaN results
SELECT CASE WHEN result IS NOT NULL AND result = result AND ABS(result) < 'Inf'::float
     THEN result ELSE NULL END FROM computed;

Why this works

Checking result = result (NaN != NaN) and ABS(result) < Inf filters out both NaN and infinity.

Sources
Official documentation ↗

Class 22 — Data Exception (Postgres-specific)

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors