Re: Tracking per-RelOptInfo uniqueness during planning

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Ilia Evdokimov <ilidock95(at)gmail(dot)com>
Cc: Antonin Houska <ah(at)cybertec(dot)at>, Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Andy Fan <zhihuifan1213(at)163(dot)com>, David Rowley <dgrowleyml(at)gmail(dot)com>
Subject: Re: Tracking per-RelOptInfo uniqueness during planning
Date: 2026-11-24 08:16:27
Message-ID: CAMbWs4-Vr1NM3pAnt7vpfDwLKYPGvCDcg0AN663TnJFwJ6yq8g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Nov 18, 2026 at 4:00 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> I've split the implementation into small steps. Attached are the
> first three I've got so far.
>
> 0001 detects unique inner sides of joins when the inner side is a
> join, for plain tables and inner joins only.
>
> 0002 skips DISTINCT and GROUP BY steps whose input is already
> distinct. A UniqueKey is marked null_aware if it also holds with
> NULLs considered equal.
>
> 0003 skips a DISTINCT step that follows grouping or window functions.
>
> There is still a lot of work to do, including outer joins, semi/anti
> joins, child relations, partially aggregated relations and subqueries.
> But I'd like to hear what people think of the approach first.

Here is v6. v5 only had the first three patches. v6 adds the to-do
items mentioned upthread, as 0004 to 0008. 0001 to 0003 are the same
as in v5 except for some comment changes.

0004 teaches the proof for join relations about outer joins, and 0005
does the same for semijoins and antijoins. The rules for outer joins
took me the longest to get right, full joins in particular, so that is
where I'd most appreciate another pair of eyes.

0006 handles child joins. It doesn't build anything for them; a child
join is distinct if its parent is, so we just ask about the parent.

0007 is for eager aggregation. A partially aggregated relation emits
one row per group, so it is unique for join clauses that cover its
grouping expressions. This one doesn't actually need UniqueKeys.

0008 builds UniqueKeys for subqueries that can't be pulled up, from
their DISTINCT or GROUP BY clauses or set operations, much like we
build them for a plain table from its unique indexes.

On planning time: I asked Claude Opus 5.5 to benchmark this, on -O2
builds without assertions, patched against master. What it reported
is that for the usual shapes (chains of FK/PK joins up to 16 tables,
stars, outer joins, partitionwise joins, eager aggregation) the
overhead is somewhere between 0 and 5%, and around 1% in total over a
few thousand randomly generated queries. The noise is about 5% per
query, so much of that is hard to tell apart from nothing. perf
attributes 0.5-1.3% of planning time to the UniqueKeys code itself.

The worst case it found is a clique of 8 or 10 tables all joined on
their primary keys, which takes 12-15% longer to plan and ends up with
the same plan. The time doesn't go into the proofs. When an inner
side that is a join is proven unique, nestloop paths over it get a
lower cost estimate, so more of them pass add_path_precheck and get
fully costed. We already pay this today when the inner side is a base
relation, but I thought it was worth mentioning.

There are quite a few other things we could do once we know a rel,
base or join, is distinct. The ones I have in mind, none of them in
this series:

- reduce a semijoin to an inner join when its right-hand side is a
join that is unique for the join clauses, or at least skip
unique-ifying a right-hand side that is already unique
- remove a left join whose right-hand side is a join
- drop sort keys that come after a set of keys the input is already
distinct on
- drop the DISTINCT in agg(DISTINCT x) when x is already distinct in
the aggregate's input
- better estimates for the number of groups and for join sizes

Some of these need to ask about uniqueness before the join search
starts, which nothing in this series does yet. That is also why I
haven't touched the existing checks for an inner side that is a single
base relation (relation_has_unique_index_for and
query_is_distinct_for). I think they can be answered from UniqueKeys
as well, and I plan to look at that along with the follow-ups, so that
we don't end up with two mechanisms for good.

Ilia, about the enable_uniquekeys GUC you asked for: I haven't added
one so far. Nothing here is a cost-based choice, so the only use for
it would be as a workaround if a proof turns out to be wrong. I don't
feel strongly about it though, and I'm happy to add it as a separate
patch if people would like to have it.

- Richard

Attachment Content-Type Size
v6-0001-Detect-unique-inner-sides-of-joins-when-the-inner.patch application/octet-stream 38.1 KB
v6-0002-Skip-DISTINCT-and-GROUP-BY-steps-when-the-input-i.patch application/octet-stream 43.8 KB
v6-0003-Skip-DISTINCT-steps-over-grouped-or-windowed-inpu.patch application/octet-stream 17.1 KB
v6-0004-Teach-UniqueKeys-about-outer-joins.patch application/octet-stream 45.3 KB
v6-0005-Teach-UniqueKeys-about-semijoins-and-antijoins.patch application/octet-stream 15.4 KB
v6-0006-Detect-unique-inner-sides-of-joins-for-child-rela.patch application/octet-stream 14.0 KB
v6-0007-Detect-unique-inner-sides-of-joins-for-partially-.patch application/octet-stream 39.9 KB
v6-0008-Build-UniqueKeys-for-subquery-relations.patch application/octet-stream 17.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Daniel Gustafsson 2026-11-24 08:09:26 Re: [PATCH] Fix typo "Querys" in a pquery.c comment