Re: Simplify the uniqueness proof for self-join elimination

From: wenhui qiu <qiuwenhuifx(at)gmail(dot)com>
To: Ayoub Kazar <ayoub(dot)kazar(at)data-bene(dot)io>
Cc: Richard Guo <guofenglinux(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Simplify the uniqueness proof for self-join elimination
Date: 2026-11-27 03:41:22
Message-ID: CAGjGUAJmHikRN=7jBcebj1r_L0LJqLB9p62V8YAXNd-Uw35y6Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

HI Ayoub

> 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),
> ...
> > This only affects pg_plan_advice (or anything that modifies
> > rel->pgs_mask per-relation).
> > Thoughts ?
>
> Agreed, thanks for tracking this down.
>
> This is indeed pre-existing on master whenever SJE does not run on the
> join -- for instance, joining two different tables on ctid:
>
> CREATE TABLE tidscan2 (id int);
> EXPLAIN (COSTS OFF)
> SELECT * FROM tidscan t1 JOIN tidscan2 t2 ON t1.ctid = t2.ctid
> WHERE t1.id = 1;
>
> or running the existing query in tidscan.sql with
> enable_self_join_elimination = off produces the exact same diff under
> test_plan_advice. SJE only masked it for the self-join case because it
> previously called generate_join_implied_equalities() even when the
> relation had no unique indexes. So we shouldn't drop the
> rel_supports_distinctness() check in SJE just to hide this, and I also
> agree we shouldn't try to enforce a specific operand order in
> equivclass.c.
>
> It seems to me there are two ways we could address this on the tidscan
> side (probably as a separate preparatory patch):
>
> 1. Canonicalize parameterized TidScan quals in
> BuildParameterizedTidPaths() so that the scanned relation's ctid is
> always on the left (via commute_restrictinfo(rinfo, TIDEqualOperator),
> similar to what match_opclause_to_indexcol() does for index scans).
> That way, parameterized TidScans consistently produce "TID Cond:
> (ctid = t1.ctid)" regardless of which relation populated
> ec_derives_list first.
>
> Doing this only in BuildParameterizedTidPaths() leaves single-rel cases
> like "WHERE '(0,1)' = ctid" alone (preserving test coverage for the
> commuted OpExpr branch in nodeTidscan.c and avoiding breaking the
> OR-clause list_difference() check in create_tidscan_plan()), and only
> changes the two join plans in expected/tidscan.out. Note that
> create_tidscan_plan() would also need to check rinfo_serial rather than
> list_member_ptr(tidquals, rinfo) when removing duplicate scan_clauses,
> otherwise a commuted non-EC join clause (such as the LEFT JOIN test in
> tidscan.sql) would be left behind as a redundant Filter.
>
> 2. Alternatively, handle PGS_TIDSCAN via disabled_nodes in
> cost_tidscan() / cost_tidrangescan() instead of skipping path
> generation in create_tidscan_paths(), matching PGS_SEQSCAN,
> PGS_INDEXSCAN, and PGS_BITMAPSCAN. That avoids changing tidscan.out,
> though as noted in pathnodes.h, skipping path generation was chosen to
> preserve the historical behavior of enable_tidscan = off (e.g., when
> enable_seqscan is also off).
>
> Between the two, (1) feels cleaner to me since it removes the
> EXPLAIN output's sensitivity to EC derivation order altogether, but I'd
> be interested to hear what others think.
>
> Thanks

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message shveta malik 2026-11-27 03:45:51 Re: Proposal: Conflict log history table for Logical Replication
Previous Message Chao Li 2026-11-27 03:28:52 Re: Use option_parse_int() for pg_createsubscriber --recovery-timeout and pgbench --max-tries