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
- Filter out or guard zero inputs (e.g.
WHERE x > 0). - Use a
CASE/NULLIFto handle zero specially. - Add a small epsilon only if mathematically justified.
Related & next steps
Reference: PostgreSQL 18 Section 9.3 “Mathematical Functions”.
Thanks — noted. This helps keep the database accurate.