Fix numeric log() with an infinite value and 0<base<=1

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Fix numeric log() with an infinite value and 0<base<=1
Date: 2026-10-14 11:16:38
Message-ID: 08270B8B-634F-4381-9D6F-9BDB18DA82A0@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

The SQL function log() returns incorrect results in these cases:
```
evantest=# SELECT log(0.5::numeric, 'Infinity'::numeric);
log
----------
Infinity
(1 row)

evantest=# SELECT log(1::numeric, 'Infinity'::numeric);
log
----------
Infinity
(1 row)
```

log(base, value) = ln(value) / ln(base)

ln(0.5) is negative, so the first should return negative infinity; ln(1) is zero, so the second should fail with division by zero.

Verifying in Python:
```
>>> print(math.log(math.inf, 0.5))
-inf

>>> print(math.log(math.inf, 1));
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
print(math.log(math.inf, 1));
~~~~~~~~^^^^^^^^^^^^^
ZeroDivisionError: division by zero
```

Also, verifying with PG itself using division:
```
evantest=# select ln('Infinity'::numeric)/ln(0.5::numeric);
?column?
-----------
-Infinity
(1 row)

evantest=# select ln('Infinity'::numeric)/ln(1::numeric);
ERROR: division by zero
```

This issue was introduced in v14.

Please see the attached patch for the fix.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v1-0001-Fix-numeric-log-with-an-infinite-argument-and-a-b.patch application/octet-stream 3.4 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Fujii Masao 2026-10-14 11:00:31 Re: Incremental backups report progress as if they were full backups