BUG #19563: Planner does not eliminate redundant outer DISTINCT over UNION

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: 2320415112(at)qq(dot)com
Subject: BUG #19563: Planner does not eliminate redundant outer DISTINCT over UNION
Date: 2026-07-21 10:46:44
Message-ID: 19563-444007e4ab7c1292@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: 19563
Logged by: cl hl
Email address: 2320415112(at)qq(dot)com
PostgreSQL version: 17.10
Operating system: Linux LAPTOP-2SQAVLB0 6.6.87.2-microsoft-standard-
Description:

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 <= 800
UNION
SELECT a
FROM outer_distinct_union_t
WHERE a >= 200;

-- Mutated query.
-- The outer DISTINCT is redundant.
EXPLAIN
SELECT DISTINCT *
FROM (
SELECT a
FROM outer_distinct_union_t
WHERE a <= 800
UNION
SELECT a
FROM outer_distinct_union_t
WHERE a >= 200
) AS u;
```

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Zsolt Parragi 2026-07-21 10:55:11 Race between datachecksum enablement and create table with the file_copy strategy
Previous Message Japin Li 2026-07-21 10:23:36 Re: 回复: pg_restore error with partitioned table having exclude constraint