Re: Remove redundant DISTINCT when GROUP BY already guarantees uniqueness

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Remove redundant DISTINCT when GROUP BY already guarantees uniqueness
Date: 2026-06-08 20:55:52
Message-ID: CAN4CZFNxk22cGswV6HBkgws3z=QTc5CULKmdOY3A8_LY=NcSVw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello!

+static bool
+distinct_redundant_by_groupby(Query *parse)

I think this also should check hasTargetSRFs:

CREATE TABLE t (a int, b int, c int);
INSERT INTO t VALUES (1,1,10),(1,2,20),(2,1,30),(2,2,40),(1,1,50);
SELECT DISTINCT a, unnest(ARRAY[1,1]) AS u FROM t GROUP BY a ORDER BY a, u;

Also, query_is_distinct_for already does something similar (and
already handles hasTargetSRFs) - would it be possible to extract the
common part instead of duplicating the logic?

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-06-08 21:12:09 Re: [PATCH] Add pg_get_table_ddl() to reconstruct CREATE TABLE statements
Previous Message Andres Freund 2026-06-08 20:15:55 Re: (SQL/PGQ) cache lookup failed for label