SQLSTATE 2201ESeverity lowLab verified

Incident brief

Logarithm of a negative number

PostgreSQL raises this data exception when ln(), log(), or log(b, x) is asked for the logarithm of a value that is zero or negative, which is undefined for the real logarithm.

What lands in your log

ERROR: cannot take logarithm of a negative number

Reproduced on PostgreSQL 18.4Verified 2026-07-24 (isolated lab, PostgreSQL 18.4)Verified against PostgreSQL 18.4 in an isolated lab environment

In 10 seconds

What triggers it
Call a logarithm function such as ln(x) or log(x) with an argument that is zero or negative.
Fix
Constrain the input so the argument is strictly greater than zero before taking its logarithm.
Proof
Reproduced on PostgreSQL 18.4 → A single SELECT reproduces SQLSTATE 2201E: the server rejects the negative logarithm argument rather than returning a value.

Fix

What to do right now

Application-level steps for this error.

  • Constrain the input so the argument is strictly greater than zero before taking its logarithm.
  • Filter or clamp offending rows (WHERE x > 0), or use a CASE expression to return NULL for non-positive inputs.
Fix SQL
-- Only take the logarithm of a strictly positive value.
SELECT ln(1);

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

PG 18.4
Last verified
2026-07-24 (isolated lab, PostgreSQL 18.4)
Verification scope
Verified against PostgreSQL 18.4 in an isolated lab environment
Audit status
reviewed
ShareLinkedInX

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops