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
- Restrict inputs to positive values (e.g.
WHERE x > 0). - Use
abs()only if taking the log of magnitude is intended. - Guard with
CASEto handle non-positive inputs.
Related & next steps
Reference: PostgreSQL 18 Section 9.3 “Mathematical Functions”.
Thanks — noted. This helps keep the database accurate.