pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrei Lepikhov <lepihov@gmail.com>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Zhang Mingli <zmlpostgres@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Cc: jian he <jian.universality@gmail.com>
Subject: Re: Remove useless GROUP BY columns considering unique index
Date: Mon, 2 Dec 2024 11:18:42 +0700
Message-ID: <480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com> (raw)
In-Reply-To: <CAApHDvo=+emQvjvRKzbDHY3L7Mm_dKZU7qmy=6cpCtWU+jYs=Q@mail.gmail.com>
References: <f389a811-f427-4448-997e-b639343ecdb1@Spark>
	<327990c8-b9b2-4b0c-bffb-462249f82de0@Spark>
	<CAApHDvqHW0wo8rFzrtMgYa=bkxjfKOxSjiz_QqO5najWHjoRRg@mail.gmail.com>
	<CACJufxFZn3VAuAPnvVWZ3pk6Kn_zHMJh=n0dpw6gu7B0B-Y3vg@mail.gmail.com>
	<CACJufxGtnCUSAF3R4gX_E8yCm6T7MQAcLguwa50uQJDKs9LF4Q@mail.gmail.com>
	<CAApHDvpbZ2Njs=m-8+rnEr_UjyAyECVH__kjab8SqRcGA0S=Ew@mail.gmail.com>
	<CACJufxE-MzvN0qba1KriQzj5OXJBB32mYdfL4YK4KiJVwi+ofA@mail.gmail.com>
	<CAApHDvp6xkkq7YCGk94jGguxSdq7ewD7VDn3-i-AyJdyhK-Etw@mail.gmail.com>
	<CACJufxENUcVLs0qmpG0JOV7KmDNYQY_qOMQqh_VwOct-SL0ovg@mail.gmail.com>
	<CAApHDvqLezKwoEBBQd0dp4Y9MDkFBDbny0f3SzEeqOFoU7Z5+A@mail.gmail.com>
	<CAApHDvrJjL3fLGoNh5QnPKx4b8ckDbAXeY1H+E6smFj8cULORQ@mail.gmail.com>
	<1507cbb9-937f-48f5-9931-b67014511f9b@gmail.com>
	<CAApHDvo=+emQvjvRKzbDHY3L7Mm_dKZU7qmy=6cpCtWU+jYs=Q@mail.gmail.com>

On 29/11/2024 14:28, David Rowley wrote:
> On Fri, 29 Nov 2024 at 19:36, Andrei Lepikhov <lepihov@gmail.com> wrote:
>> 1. Thread reference in the patch comment doesn't work.
>> 2. May it be possible to move remove_useless_groupby_columns in the
>> second commit? It would be easier to review the differences.
> 
> For #1, I rewrote the commit message.
> 
> I've split into two patches for ease of review. See attached.
Thanks!
Patch 0001 seems OK. The differentiation of 'planmain' and 'initsplan' 
is not clear for me, but code movement seems correct. I should say that 
comment in initsplan.c:
'... initialization routines'
looks strange. Is this module about initialisation now?

Patch 0002 looks helpful and performant. I propose to check 'relid > 0' 
to avoid diving into 'foreach(lc, parse->rtable)' at all if nothing has 
been found.
It may restrict future optimisations like [1], where the upper query 
block can employ the 'uniqueness' of grouped clauses in selectivity 
estimations. But I think it is OK for now.

NOTES:
1. Uniqueness is proved by a UNIQUE index. It might be a bit more 
sophisticated to probe not only grouping qual but also employ 
equivalence classes. In that case, we could process something like that:

CREATE TABLE test (
   x int NOT NULL, y int NOT NULL, z int NOT NULL, w int);
CREATE UNIQUE INDEX ON test(y,z);
SELECT t2.z FROM test t2, test t1 WHERE t1.y=t2.y
GROUP BY t1.y,t2.z,t2.w;

2. The same idea could be used with DISTINCT statement. Right now we have:
SELECT DISTINCT y,z,w FROM test;

  HashAggregate
    Output: y, z, w
    Group Key: test.y, test.z, test.w
    ->  Seq Scan on public.test
          Output: x, y, z, w

[1] 
https://www.postgresql.org/message-id/flat/50fe6779-ee2d-4256-bc64-cd661bc4029a%40gmail.com

-- 
regards, Andrei Lepikhov






view thread (24+ messages)  latest in thread

Message-ID: <480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com>
Permalink:  ../480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com/
Also on:    postgresql.org/message-id/480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: lepihov@gmail.com, dgrowleyml@gmail.com, zmlpostgres@gmail.com, jian.universality@gmail.com
  Subject: Re: Remove useless GROUP BY columns considering unique index
  In-Reply-To: <480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com>
  References: <f389a811-f427-4448-997e-b639343ecdb1@Spark> <327990c8-b9b2-4b0c-bffb-462249f82de0@Spark> <CAApHDvqHW0wo8rFzrtMgYa=bkxjfKOxSjiz_QqO5najWHjoRRg@mail.gmail.com> <CACJufxFZn3VAuAPnvVWZ3pk6Kn_zHMJh=n0dpw6gu7B0B-Y3vg@mail.gmail.com> <CACJufxGtnCUSAF3R4gX_E8yCm6T7MQAcLguwa50uQJDKs9LF4Q@mail.gmail.com> <CAApHDvpbZ2Njs=m-8+rnEr_UjyAyECVH__kjab8SqRcGA0S=Ew@mail.gmail.com> <CACJufxE-MzvN0qba1KriQzj5OXJBB32mYdfL4YK4KiJVwi+ofA@mail.gmail.com> <CAApHDvp6xkkq7YCGk94jGguxSdq7ewD7VDn3-i-AyJdyhK-Etw@mail.gmail.com> <CACJufxENUcVLs0qmpG0JOV7KmDNYQY_qOMQqh_VwOct-SL0ovg@mail.gmail.com> <CAApHDvqLezKwoEBBQd0dp4Y9MDkFBDbny0f3SzEeqOFoU7Z5+A@mail.gmail.com> <CAApHDvrJjL3fLGoNh5QnPKx4b8ckDbAXeY1H+E6smFj8cULORQ@mail.gmail.com> <1507cbb9-937f-48f5-9931-b67014511f9b@gmail.com> <CAApHDvo=+emQvjvRKzbDHY3L7Mm_dKZU7qmy=6cpCtWU+jYs=Q@mail.gmail.com> <480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox