agora inbox for pgsql-bugs@postgresql.org  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: malis@pgrust.com
Subject: BUG #19619: WHERE scale(n) = 1 pushed past GROUP BY / window PARTITION BY, wrong COUNT
Date: Fri, 14 Aug 2026 17:41:02 +0000
Message-ID: <19619-fc646db1c6b2dc90@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19619
Logged by:          Michael Malis
Email address:      malis@pgrust.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;







view thread (3+ messages)  latest in thread

Message-ID: <19619-fc646db1c6b2dc90@postgresql.org>
Permalink:  ../19619-fc646db1c6b2dc90@postgresql.org/
Also on:    postgresql.org/message-id/19619-fc646db1c6b2dc90@postgresql.org

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, malis@pgrust.com
  Subject: Re: BUG #19619: WHERE scale(n) = 1 pushed past GROUP BY / window PARTITION BY, wrong COUNT
  In-Reply-To: <19619-fc646db1c6b2dc90@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox