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

Reply via email to