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 1wdSZ0-00475L-2i for pgsql-bugs@arkaria.postgresql.org; Sat, 27 Jun 2026 12:55:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wdSYz-00FIMb-2T for pgsql-bugs@arkaria.postgresql.org; Sat, 27 Jun 2026 12:55:13 +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 1wcOnq-001GWD-02 for pgsql-bugs@lists.postgresql.org; Wed, 24 Jun 2026 14:42:10 +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 1wcOnm-000000002Qy-1AES for pgsql-bugs@lists.postgresql.org; Wed, 24 Jun 2026 14:42:09 +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=Sd2Z+GBsPey9D3eeSrgZdNuQ2DRHw5Jnas02p3CjfRY=; b=YXZI99VXuU5eZmDkpiq6PNDFKH CGszLOpUVEw+VUd8/hrV0HjbKJxcjFzySzeC3yiLuBRl3+fsL2GgwTh+1muK/+wCjYSpoI4ad0l47 oMUOwbzK3fMA3jgdwn2IjcitkpHkh0NMLHOUuWzK5ri23YvQq/4/2tqMjsuvIon5XziR+jQg1/bQ2 oQtUDq11hNew951TXkcqbOf5iuwiYzFe/mQrTIxuzEeKzAR8oJEB2H3XtAL9IWjNL5EouNQrm4aMP VOf8XqpvrpjpobHD8KL0N8R07zeiMlujHBir6ZV7SQHHIjXls76TCD7Fufd/niEbBRR8g88KM4JoE MAWXyQqA==; 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 1wcOnj-005u8U-2u for pgsql-bugs@lists.postgresql.org; Wed, 24 Jun 2026 14:42:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wcOni-000Ogx-1J for pgsql-bugs@lists.postgresql.org; Wed, 24 Jun 2026 14:42:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19533: Wrong results from WindowAgg run-condition pushdown on count() with EXCLUDE CURRENT ROW 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:41:28 +0000 Message-ID: <19533-413a1014e5d0e766@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: 19533 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}`.