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: Thu, 12 Dec 2024 10:38:57 +0700
Message-ID: <f358f934-44d6-4c17-83fe-d61c5c89e191@gmail.com> (raw)
In-Reply-To: <CAApHDvp-vfvkhpkkdcWADwSPNvDZN29Qxrb+x6A1775-thoW0A@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>
	<480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com>
	<CAApHDvp-vfvkhpkkdcWADwSPNvDZN29Qxrb+x6A1775-thoW0A@mail.gmail.com>

On 12/12/24 10:09, David Rowley wrote:
> On Mon, 2 Dec 2024 at 17:18, Andrei Lepikhov <lepihov@gmail.com> wrote:
>> 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.
> 
> I did end up adding another fast path there, but I felt like checking
> relid > 0 wasn't as good as it could be as that would have only
> short-circuited when we don't see any Vars of level 0 in the GROUP BY.
> It seemed cheap enough to short-circuit when none of the relations
> mentioned in the GROUP BY have multiple columns mentioned.
Your solution seems much better my proposal. Thanks to apply it!

> when how do you decide if the GROUP BY should become t1.a,t1.b or
> t2.x,t2.y? It's not clear to me that using t1's columns is always
> better than using t2's. I imagine using a mix is never better, but I'm
> unsure how you'd decide which ones to use.
Depends on how to calculate that 'better'. Right now, GROUP-BY employs 
two strategies to reduce path cost: 1) ORDER-BY statement (avoid final 
sorting); 2) To fit incoming subtree pathkeys (avoid grouping presorting).
My idea comes close with [1], where the cost depends on the estimated 
number of groups in the first grouping column because cost_sort predicts 
the number of comparison operator calls based on statistics. In this 
case, the choice between (x,y) and (a,b) will depend on the ndistinct of 
'x' and 'a'.
In general, it was the idea to debate, more for further development than 
for the patch in this thread.

[1] Consider the number of columns in the sort cost model
https://www.postgresql.org/message-id/flat/8742aaa8-9519-4a1f-91bd-364aec65f5cf%40gmail.com

-- 
regards, Andrei Lepikhov





view thread (24+ messages)

Message-ID: <f358f934-44d6-4c17-83fe-d61c5c89e191@gmail.com>
Permalink:  ../f358f934-44d6-4c17-83fe-d61c5c89e191@gmail.com/
Also on:    postgresql.org/message-id/f358f934-44d6-4c17-83fe-d61c5c89e191@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: <f358f934-44d6-4c17-83fe-d61c5c89e191@gmail.com>
  References: <f389a811-f427-4448-997e-b639343ecdb1@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> <CAApHDvp-vfvkhpkkdcWADwSPNvDZN29Qxrb+x6A1775-thoW0A@mail.gmail.com> <f358f934-44d6-4c17-83fe-d61c5c89e191@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