Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wpVg2-001gcv-1A for pgsql-bugs@arkaria.postgresql.org; Thu, 30 Jul 2026 18:40:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wpVg0-00CfKO-0r for pgsql-bugs@arkaria.postgresql.org; Thu, 30 Jul 2026 18:40:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wpNVk-00BFTp-0k for pgsql-bugs@lists.postgresql.org; Thu, 30 Jul 2026 09:57:08 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wpNVh-000000013lF-2avj for pgsql-bugs@lists.postgresql.org; Thu, 30 Jul 2026 09:57:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=oXH3z1KFNbKMLuIcBLwcjkpjEvhnLLdBS30PB1MKwnQ=; b=dtpRKCjDcm8owlphWEsnfXLec6 PARPsSpOUjZ1jXr+xkvkt6QRepK9Ause3dDHh4RmNO5KBNkTOE1T9k7VxvF45He8iFtyBWdfx/wT1 zNhZNW9kFJqqB8s90Mt8Dj6d7fni0CzKNko/flqxecKjVDv2HzQFJUNCvQnb10y4dYrF9zT+RJsKT yIDl8gfGuZeyFy+aqr82OaiB4XMsHkqN5PPUCgSUpqYU9Pu0yJ6/EsVjXSP2KprViQEr4os92OFqt nTb8Q0DZlh9nsIa+NzwWsdzGiiiBZWM5kLPMFEWrr+inq8i87E6CGCZU31w6/fGbcvNyEwtqL4PcL j6DpYxPg==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wpNVg-003IcW-2T for pgsql-bugs@lists.postgresql.org; Thu, 30 Jul 2026 09:57:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1wpNVf-00000007ovr-0835 for pgsql-bugs@lists.postgresql.org; Thu, 30 Jul 2026 09:57:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19588: Semantically equivalent DISTINCT ON query returns different result when wrapped in MATERIALIZED CTE. To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: dllggyx@outlook.com Reply-To: dllggyx@outlook.com, pgsql-bugs@lists.postgresql.org Date: Thu, 30 Jul 2026 09:56:45 +0000 Message-ID: <19588-e32b82433b3660ea@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19588 Logged by: Yuxiao Guo Email address: dllggyx@outlook.com PostgreSQL version: 18.4 Operating system: Ubuntu 20.04 x86-64, docker image postgres:18.4 Description: =20 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) =3D 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) =3D 1;