floating point exception
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).
- 1Floating-point division by zero (produces infinity in IEEE 754; some contexts treat this as an error)
- 2Floating-point operations producing Inf or NaN that are rejected by the calling context
FP operation producing an exceptional result.
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.
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.
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.
Class 22 — Data Exception (Postgres-specific)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev