PG
PRO
2201EERRORTier 2 — Caution✅ HIGH confidence

invalid argument for logarithm

Category: Data ExceptionVersions: All Postgres versions

What this means

SQLSTATE 2201E is raised when a logarithm function (LOG, LN, LOG10) receives an argument that is not in the valid mathematical domain — specifically, zero or a negative number.

Why it happens

  1. 1Calling LOG(), LN(), or LOG(base, x) with a zero or negative argument
  2. 2A computed column value becomes zero or negative before being passed to a log function

How to reproduce

LN of zero.

trigger — this will ERROR
SELECT LN(0);
ERROR: cannot take logarithm of zero

Fix 1: Guard with NULLIF or CASE before calling the logarithm

When input values may be zero or negative.

fix
SELECT LN(NULLIF(value, 0)) FROM measurements;
-- or:
SELECT CASE WHEN value > 0 THEN LN(value) ELSE NULL END FROM measurements;

Why this works

NULLIF returns NULL for zero, causing LN(NULL) = NULL rather than an error. CASE guards against both zero and negative values.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 22 — Data Exception

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for logarithm domain errors. Stable across versions.

See also

📄 Reference pages

Mathematical FunctionsLNLOG
⚙️ 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 →