SQLSTATE 2201E ERROR Class 22: Data Exception

invalid_argument_for_logarithm cannot take logarithm of a negative number — 2201E

PostgreSQL error "cannot take logarithm of a negative number" (SQLSTATE 2201E): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

A logarithm function (ln, log, log10) was passed a negative number. The logarithm of a negative is undefined in reals, so PostgreSQL raises SQLSTATE 2201E (invalid_argument_for_logarithm).

  • A logarithm function received a negative argument.
  • Undefined over the real numbers.
  • Common with signed computed inputs.

What the server log shows

ERROR:  cannot take logarithm of a negative number

Why PostgreSQL raises this — what the manual says

As Section 9.3 Mathematical Functions and Operators explains:

Real-valued logarithms are defined only for positive arguments, so passing a negative number to ln(), log(), or a related function raises this error.

Real-valued logarithms require positive arguments. A negative input has no real logarithm, so PostgreSQL rejects it with 2201E.

Common causes

  • Negative values reaching a logarithm function.
  • Sign errors in computed inputs.
  • Unvalidated data containing negatives.

How to fix it

  1. Restrict inputs to positive values (e.g. WHERE x > 0).
  2. Use abs() only if taking the log of magnitude is intended.
  3. Guard with CASE to handle non-positive inputs.

Related & next steps

Reference: PostgreSQL 18 Section 9.3 “Mathematical Functions”.

Was this helpful?