SQLSTATE 2201E ERROR Class 22: Data Exception

invalid_argument_for_logarithm cannot take logarithm of zero — 2201E

PostgreSQL error "cannot take logarithm of zero" (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 call to ln(), log(), or a related logarithm function was passed zero. The logarithm of zero is undefined, so PostgreSQL raises SQLSTATE 2201E (invalid_argument_for_logarithm).

  • A logarithm function received a 0 argument.
  • Mathematically undefined input.
  • Common with computed values that can be zero.

What the server log shows

ERROR:  cannot take logarithm of zero

Why PostgreSQL raises this — what the manual says

As Section 9.3 Mathematical Functions and Operators explains:

The logarithm of zero is undefined (it diverges to negative infinity), so ln(), log(), and related logarithm functions reject a zero argument with this error.

Logarithms are defined only for positive real numbers. A zero argument has no real logarithm, so PostgreSQL rejects it with 2201E rather than returning a meaningless result.

Common causes

  • Passing a column/expression that evaluates to 0.
  • Unfiltered data containing zeros.
  • Computed inputs that can reach zero.

How to fix it

  1. Filter out or guard zero inputs (e.g. WHERE x > 0).
  2. Use a CASE/NULLIF to handle zero specially.
  3. Add a small epsilon only if mathematically justified.

Related & next steps

Reference: PostgreSQL 18 Section 9.3 “Mathematical Functions”.

Was this helpful?