Re: Simplify the uniqueness proof for self-join elimination

From: Ayoub Kazar <ayoub(dot)kazar(at)data-bene(dot)io>
To: Richard Guo <guofenglinux(at)gmail(dot)com>, wenhui qiu <qiuwenhuifx(at)gmail(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Simplify the uniqueness proof for self-join elimination
Date: 2026-11-26 16:04:37
Message-ID: 01260603-d992-40c3-92a8-9c11bf326c8e@data-bene.io
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 25/11/2026 05:37, Richard Guo wrote:
> On Mon, Nov 23, 2026 at 7:28 PM wenhui qiu <qiuwenhuifx(at)gmail(dot)com> wrote:
>>> Since all relations in remove_self_joins_one_group() share the same
>>> reloid (and have !rte->inh), would it be worth checking
>>> rel_supports_distinctness() once in remove_self_joins_one_group() so that
>>> we can skip the whole group early if the table has no suitable unique
>>> index?
> Thanks for the review!
>
> Good point. I've added the check at the top of
> remove_self_joins_one_group(), so that we skip the whole group if the
> relation has no usable unique index.
>
>>> Perhaps we can drop otherjoinquals and simplify split_selfjoin_quals() to
>>> just return the list of self-join quals (similar to
>>> get_shared_restrictions())?
> Agreed. Nothing uses the other quals anymore, so I've changed the
> function to return just the self-join quals, and renamed it to
> get_selfjoin_quals().
>
> Attached is the updated patch.
>
> - Richard
While looking at  v2 i see that test_plan_advice (001_replan_regress)
fails on
tidscan:

--- a/src/test/regress/expected/tidscan.out
+++ b/src/test/modules/test_plan_advice/tmp_check/results/tidscan.out
@@ -119,7 +119,7 @@
    ->  Seq Scan on tidscan t1
            Filter: (id = 1)
    ->  Tid Scan on tidscan t2
-         TID Cond: (t1.ctid = ctid)
+         TID Cond: (ctid = t1.ctid)

This is new after what you did in v2's rel_supports_distinctness() early
exit in
remove_self_joins_one_group().  tidscan has no unique indexes, so v2
skips calling generate_join_implied_equalities(), leaving ec_derives_list
empty before path generation.  In the normal case,
create_tidscan_paths() we see t1 first so we would put t1.ctid = t2.ctid
in ec.
When pg_plan_advice replans, SEQ_SCAN(t1) clears PGS_TIDSCAN of course
for t1, therefore ec is never touched. Later on t2 we would generate
t2.ctid = t1.ctid.

This looks like a pre-existing order dependency in equivclass.c /
tidpath.c that SJE previously masked by pre-populating ec_derives_list
in (t1, t2) order (and IIUC, unlike indexscans and merge/hash joins, TidScan
does not commute operands to a fixed side).

I don't think we should try setting a specific ordering directly in
equivclass.c (or anywher else),
If one tries to prepare operand order in
create_join_clause() (using counterpart / em_relids) and in
generate_implied_equalities_for_column() (using em_relids).
Both would change the expectations of order in other places (e.g indexed
inner rels).

This only affects pg_plan_advice (or anything that modifies
rel->pgs_mask per-relation).
Thoughts ?

Regards,
Ayoub Kazar

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Laurenz Albe 2026-11-26 16:12:32 Re: passwordcheck: Make the user name check case-insensitive
Previous Message ZizhuanLiu X-MAN 2026-11-26 16:03:28 Normalize RelabelTypes after stripping PlaceHolderVars