2201E
PostgreSQLERRORNotableData ExceptionHIGH confidence

invalid argument for logarithm

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
trigger — this will error
SELECT LN(0);

expected output

ERROR:  cannot take logarithm of zero

Fix

Guard with NULLIF or CASE before calling the logarithm

WHEN When input values may be zero or negative.

Guard with NULLIF or CASE before calling the logarithm
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 documentation ↗

Class 22 — Data Exception

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

← All PostgreSQL errors