BUG #19741: `avg(double precision)` overflows on cancelling finite inputs whose mean is representable

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: theshallow27(at)gmail(dot)com
Subject: BUG #19741: `avg(double precision)` overflows on cancelling finite inputs whose mean is representable
Date: 2026-10-02 22:38:39
Message-ID: 19741-01386c4f17e36788@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19741
Logged by: Shallow
Email address: theshallow27(at)gmail(dot)com
PostgreSQL version: 18.6
Operating system: Linux
Description:

The arithmetic mean of two finite values, `1e154` and `-1e154`, is exactly
zero. PostgreSQL's `avg(double precision)` raises an overflow instead. The
same input's sum is zero and its numeric average is zero. The aggregate's
shared float8 accumulation state appears to overflow while accumulating
values used for variance, although `avg` needs only the count and sum for
its
final result.

**Reproduction:**

```sql
SELECT avg(x)
FROM unnest(ARRAY[1e154, -1e154]::double precision[]) AS t(x);
```

**Actual result:** SQLSTATE `22003`, `value out of range: overflow`.

Reference calculation:

```sql
SELECT sum(x), avg(x::numeric)
FROM unnest(ARRAY[1e154, -1e154]::double precision[]) AS t(x);
-- 0 | 0
```

**Expected result:** `avg(double precision)` should return `0` for these
finite
inputs rather than failing due to an intermediate value that does not
contribute to the arithmetic mean.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message shihao zhong 2026-10-03 04:05:05 Re: BUG #19628: Uninterruptible vacuum during hash index processing
Previous Message PG Bug reporting form 2026-10-02 22:38:00 BUG #19740: `has_language_privilege` returns TRUE for a nonexistent language OID when the user is a superuser