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 1wm9Vm-000cXb-1r for pgsql-bugs@arkaria.postgresql.org; Tue, 21 Jul 2026 12:23:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wm9Vm-007RMn-0T for pgsql-bugs@arkaria.postgresql.org; Tue, 21 Jul 2026 12:23:49 +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 1wm80H-0071qL-1H for pgsql-bugs@lists.postgresql.org; Tue, 21 Jul 2026 10:47: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 1wm80B-00000000QKk-3wb7 for pgsql-bugs@lists.postgresql.org; Tue, 21 Jul 2026 10:47: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=sG+i/ZgYPbstC6GNv6cDNB7GtNU4a/sTfLfcI+vHZVs=; b=rYBCPUPgWcMxAsKTa2x62Y5dBD VEIkxG86gWlbjz1fkPVHlR7m/f8lXBMoEj9LM96jeAKJr1cdV0Uo2Oi8BiJoUXqojtGR4nDlqrkqC GjOls8wwpi6h9IdMOlyTj9StWU6L5985xQGmIQtiUYal3w7jbKRMjeZ783QCCiPAfvUSRR4Yuv5ba oZtb8WKY8eO0CnnWabt86FYy0QBerWZzCXCjRusxrM3eIjDChhB2xH2syxahFgCD6whaphys7gYZu 6f198jXIVzonRCRtEuLwVYT3q8HkozkN5mkexojpZCoz6sYCDZUzSBYsh+Vw9soIaGH8pum9vZmVf fv6bUUoA==; 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 1wm808-001LoP-26 for pgsql-bugs@lists.postgresql.org; Tue, 21 Jul 2026 10:47:06 +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 1wm807-000000019La-0WSC for pgsql-bugs@lists.postgresql.org; Tue, 21 Jul 2026 10:47:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19563: Planner does not eliminate redundant outer DISTINCT over UNION To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 2320415112@qq.com Reply-To: 2320415112@qq.com, pgsql-bugs@lists.postgresql.org Date: Tue, 21 Jul 2026 10:46:44 +0000 Message-ID: <19563-444007e4ab7c1292@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: 19563 Logged by: cl hl Email address: 2320415112@qq.com PostgreSQL version: 17.10 Operating system: Linux LAPTOP-2SQAVLB0 6.6.87.2-microsoft-standard- Description: =20 PostgreSQL keeps an extra duplicate-removal operator for a redundant outer `DISTINCT` over a `UNION` query. `UNION` already returns a distinct set of rows. Therefore, the following transformation is semantically redundant: ```sql SELECT a FROM t WHERE ... UNION SELECT a FROM t WHERE ... ``` to: ```sql SELECT DISTINCT * FROM ( SELECT a FROM t WHERE ... UNION SELECT a FROM t WHERE ... ) AS u; ``` However, PostgreSQL plans the mutated query with two `HashAggregate` nodes: one for the `UNION` duplicate elimination and another for the outer `DISTINCT`. The outer `HashAggregate` should be removable because its input is already distinct on the same output columns. How to repeat ```sql DROP TABLE IF EXISTS outer_distinct_union_t; CREATE TABLE outer_distinct_union_t ( a INT PRIMARY KEY, b INT ); INSERT INTO outer_distinct_union_t SELECT i, i % 10 FROM generate_series(1, 1000) AS g(i); ANALYZE outer_distinct_union_t; -- Original query. -- UNION already removes duplicates. EXPLAIN SELECT a FROM outer_distinct_union_t WHERE a <=3D 800 UNION SELECT a FROM outer_distinct_union_t WHERE a >=3D 200; -- Mutated query. -- The outer DISTINCT is redundant. EXPLAIN SELECT DISTINCT * FROM ( SELECT a FROM outer_distinct_union_t WHERE a <=3D 800 UNION SELECT a FROM outer_distinct_union_t WHERE a >=3D 200 ) AS u; ```