BUG #19588: Semantically equivalent DISTINCT ON query returns different result when wrapped in MATERIALIZED CTE.

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: dllggyx(at)outlook(dot)com
Subject: BUG #19588: Semantically equivalent DISTINCT ON query returns different result when wrapped in MATERIALIZED CTE.
Date: 2026-07-30 09:56:45
Message-ID: 19588-e32b82433b3660ea@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: 19588
Logged by: Yuxiao Guo
Email address: dllggyx(at)outlook(dot)com
PostgreSQL version: 18.4
Operating system: Ubuntu 20.04 x86-64, docker image postgres:18.4
Description:

The two forms are semantically equivalent, but I observed different results.
The first version returned 1.0, while the second version returned empty set.

PoC:

CREATE TABLE t0(c0 numeric);
INSERT INTO t0 VALUES (1.0), (1.00);

-- Query A, result: {1.0}
SELECT * FROM (
SELECT DISTINCT ON (c0) c0
FROM t0
ORDER BY c0, scale(c0) DESC
) t1
WHERE scale(c0) = 1;

-- Query B, result: empty set
WITH t1 AS MATERIALIZED (
SELECT DISTINCT ON (c0) c0
FROM t0
ORDER BY c0, scale(c0) DESC
)
SELECT * FROM t1 WHERE scale(c0) = 1;

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-07-30 10:29:19 BUG #19589: JSON_QUERY rejects domain-over-bytea input when using FORMAT JSON ENCODING UTF8.
Previous Message Tristan Partin 2026-07-30 08:58:38 Re: BUG #19584: tid input acceptance is platform-dependent: '(,5)'::tid yields (0,5) on glibc, errors on macOS