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 1wdRzS-0046iD-24 for pgsql-bugs@arkaria.postgresql.org; Sat, 27 Jun 2026 12:18:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wdRzR-00FB21-1i for pgsql-bugs@arkaria.postgresql.org; Sat, 27 Jun 2026 12:18:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wcOh6-001ESq-2z for pgsql-bugs@lists.postgresql.org; Wed, 24 Jun 2026 14:35:13 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wcOh1-000000002NT-1c3b for pgsql-bugs@lists.postgresql.org; Wed, 24 Jun 2026 14:35:12 +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=/ehrttQ4dKPbaRwALsywY/SmZCm2RFWy5E7D/2DJVgc=; b=kiVJG2w6kik80WGOB43anD/zdq xNQlhfGXVrs6L/4nW7BiDTozLFd1hAKP8mmhkEsXB6VKf1OyaeSJopnMUeGwEqKqnv8Jq11CiOfJp Q1ZeicYb5Z4DpEKIG/lf6x/Pb8F56+lOF90GcwgK9tXGtkgsuz54np1RE/gObpkICeJHlSblV3d2j edZ14XoyNm9SvZp3soN0C2GL5dJIyVPDJNMRjUHzysQVgGZDeg1LcB6SIW+RtwrjShHblJ1fhaCGm zksV+K7uIAL9fgWridFwBq3K7nwaabMqpzUyx6BFRDlTfhp6RSyT+3tFZ/o55N4+k6E3MdLiNK2E1 c+1HJjyA==; 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 1wcOgy-005tzw-0L for pgsql-bugs@lists.postgresql.org; Wed, 24 Jun 2026 14:35:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wcOgv-000O8X-25 for pgsql-bugs@lists.postgresql.org; Wed, 24 Jun 2026 14:35:02 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19532: Window run-condition optimization can produce wrong results for count() window aggregates To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: imchifan@163.com Reply-To: imchifan@163.com, pgsql-bugs@lists.postgresql.org Date: Wed, 24 Jun 2026 14:34:14 +0000 Message-ID: <19532-a8893c9d2a630a30@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: 19532 Logged by: Qifan Liu Email address: imchifan@163.com PostgreSQL version: 18.4 Operating system: Ubuntu 20.04 x86-64, docker image postgres:18.4 Description: =20 ## PoC ```sql DROP TABLE IF EXISTS t; CREATE TABLE t ( id int PRIMARY KEY, k int NOT NULL, v int ); INSERT INTO t(id, k, v) VALUES (1, 1, 1), (2, 1, NULL), (3, 1, 1), (4, 2, 1); WITH source_rows AS ( SELECT id FROM ( SELECT id, count(v) OVER ( ORDER BY k RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW ) AS c FROM t ) s WHERE c <=3D 1 ), reference_rows AS ( SELECT t1.id FROM t AS t1 WHERE ( SELECT count(t2.v) FROM t AS t2 WHERE t2.k <=3D t1.k AND t2.id <> t1.id ) <=3D 1 ), metamorphic_rows AS ( SELECT id FROM ( SELECT id, count(v) OVER ( ORDER BY k RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW ) AS c FROM t ) s WHERE c + 0 <=3D 1 ) SELECT 'source' AS label, coalesce(array_agg(id ORDER BY id), '{}'::int[]) AS ids FROM source_rows UNION ALL SELECT 'reference', coalesce(array_agg(id ORDER BY id), '{}'::int[]) FROM reference_rows UNION ALL SELECT 'metamorphic_no_pushdown', coalesce(array_agg(id ORDER BY id), '{}'::int[]) FROM metamorphic_rows; EXPLAIN (COSTS OFF) SELECT id FROM ( SELECT id, count(v) OVER ( ORDER BY k RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW ) AS c FROM t ) s WHERE c <=3D 1; ``` ## Expected Behavior The source query should return the same rows as the reference query and the semantically equivalent metamorphic_no_pushdown query. ## Actual Behavior The optimized query returns only `{1}`, while the reference and no-pushdown forms return `{1,3}`. `EXPLAIN` shows a `Run Condition` on the window aggregate, indicating that the optimization changed query semantics.