BUG #19619: WHERE scale(n) = 1 pushed past GROUP BY / window PARTITION BY, wrong COUNT

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: malis(at)pgrust(dot)com
Subject: BUG #19619: WHERE scale(n) = 1 pushed past GROUP BY / window PARTITION BY, wrong COUNT
Date: 2026-08-14 17:41:02
Message-ID: 19619-fc646db1c6b2dc90@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: 19619
Logged by: Michael Malis
Email address: malis(at)pgrust(dot)com
PostgreSQL version: 18.6
Operating system: MacOS
Description:

The planner pushes WHERE scale(n) = 1 (and the equivalent HAVING) past
GROUP BY n and window PARTITION BY n. numeric values 1.0, 1.00 and
1.000 compare equal, so they form one group / one window partition, but
scale() distinguishes them. Pushing the filter into the scan drops two
of the three rows before the aggregate or window runs, so COUNT(*) is 1
instead of 3. Wrong answers, not a crash.

Version: PostgreSQL 18.6 on aarch64-apple-darwin24.5.0, compiled by
Apple clang version 17.0.0 (clang-1700.0.13.5), 64-bit
Configure: --enable-cassert --enable-debug

Reproducer
----------

CREATE TABLE nums (n numeric);
INSERT INTO nums VALUES (1.0), (1.00), (1.000);
ANALYZE nums;

-- Wrong: filter is pushed below the window
SELECT n, c FROM (
SELECT n, COUNT(*) OVER (PARTITION BY n) AS c
FROM nums
) s
WHERE scale(n) = 1
ORDER BY 1;

-- Correct: OFFSET 0 blocks pushdown; filter stays above the window
SELECT n, c FROM (
SELECT n, COUNT(*) OVER (PARTITION BY n) AS c
FROM nums
OFFSET 0
) s
WHERE scale(n) = 1
ORDER BY 1;

-- Same bug for GROUP BY
SELECT c FROM (
SELECT n, COUNT(*)::int AS c FROM nums GROUP BY n
) s
WHERE scale(n) = 1;

SELECT c FROM (
SELECT n, COUNT(*)::int AS c FROM nums GROUP BY n OFFSET 0
) s
WHERE scale(n) = 1;

-- Same bug for HAVING (moved to WHERE)
SELECT n, COUNT(*)::int AS c FROM nums GROUP BY n HAVING scale(n) = 1;

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andrey Rachitskiy 2026-08-14 18:02:39 Re: BUG #19613: pg_restore: several SEGVs in ReadToc() in pg_backup_archiver.c
Previous Message Tom Lane 2026-08-14 16:33:07 Re: BUG #19613: pg_restore: several SEGVs in ReadToc() in pg_backup_archiver.c