PG
PRO
22P01ERRORTier 2 — Caution✅ HIGH confidence

floating point exception

Category: Data ExceptionVersions: All Postgres versions

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
SELECT 1.0::float / 0.0::float; -- produces Infinity in Postgres (not 22P01)
-- 22P01 appears in contexts that reject Inf/NaN
ERROR: floating-point exception

Fix 1: Check for zero denominators before FP division

When floating-point division may involve zero.

fix
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 FP results must be finite.

fix
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 docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 22 — Data Exception (Postgres-specific)

Confidence assessment

✅ HIGH confidence

Postgres-specific extension. Behaviour depends on platform FP library; generally stable.

See also

📄 Reference pages

Floating-Point TypesMathematical Functions
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →