Re: Improve statistics estimation considering GROUP-BY as a 'uniqueiser'

From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Vlada Pogozhelskaya <v(dot)pogozhelskaya(at)postgrespro(dot)ru>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Improve statistics estimation considering GROUP-BY as a 'uniqueiser'
Date: 2025-02-19 09:48:04
Message-ID: CAPpHfdsV=_fSq5ONBJQsP9BY_kHbfu+=NAK7G0kAhDZAo3dUcw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi, Vlada.

On Tue, Feb 18, 2025 at 6:56 PM Vlada Pogozhelskaya
<v(dot)pogozhelskaya(at)postgrespro(dot)ru> wrote:
> Following the discussion on improving statistics estimation by considering GROUP BY as a unique constraint, I’ve prepared a patch that integrates GROUP BY into cardinality estimation in a similar way to DISTINCT.
>
> This patch ensures that when a subquery contains a GROUP BY clause, the optimizer recognizes the grouped columns as unique. The logic follows a straightforward approach, comparing the GROUP BY columns with the target list to determine uniqueness.
>
> I’d appreciate any feedback or suggestions for further improvements.

Thank you for your patch, but your message lacking of explanation on
what is your approach and how is it different from previously
published patches on this thread. As I get from the code, you check
if group by clauses are same to targetlist. If that's true, you
assume every column to be unique. But that's just doesn't work this
way. If values are unique in some set of columns, individual columns
might have repeats. See the example.

# select x, y from generate_series(1,3) x, generate_series(1, 3) y
group by x, y;
x | y
---+---
3 | 2
2 | 2
3 | 1
2 | 1
1 | 3
1 | 2
1 | 1
2 | 3
3 | 3
(9 rows)

x and y are unique here as a pair. But individual x and y values have repeats.

------
Regards,
Alexander Korotkov
Supabase

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Benoit Lobréau 2025-02-19 09:51:49 Re: Fix logging for invalid recovery timeline
Previous Message Chiranmoy.Bhattacharya@fujitsu.com 2025-02-19 09:31:50 Re: [PATCH] SVE popcount support