From 7fb42d6747c417a9199f0b51037ced96ee135039 Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Mon, 30 Nov 2026 10:26:55 +0900 Subject: [PATCH v3 1/2] Put the scanned relation's CTID first in parameterized TID quals A parameterized TID scan uses a join clause such as "t1.ctid = t2.ctid" as its TID qual, in whatever operand order the clause happens to have. For EquivalenceClass-derived clauses, that order depends on which side first asked for the clause to be built, since create_join_clause() returns a cached clause for either order. So the TID Cond shown by EXPLAIN can change for reasons that have nothing to do with the chosen plan. test_plan_advice can trip over this, since the advice it supplies may disable TID scans on one side of the join. Index scans don't have this problem, because match_opclause_to_indexcol() commutes the clause to put the index column on the left. To fix, do the same in BuildParameterizedTidPaths(): if the scanned relation's CTID is on the right, commute the clause. Since the TID qual may now be a copy of the RestrictInfo in the path's ppi_clauses, create_tidscan_plan() must match it by rinfo_serial rather than by pointer when removing duplicates from scan_clauses. This changes the TID Cond shown for parameterized TID scans in the regression tests. Single-relation TID quals such as "'(0,1)' = ctid" are left as written. Reported-by: Ayoub Kazar Suggested-by: wenhui qiu Author: Richard Guo Discussion: https://postgr.es/m/01260603-d992-40c3-92a8-9c11bf326c8e@data-bene.io --- src/backend/optimizer/path/tidpath.c | 4 ++++ src/backend/optimizer/plan/createplan.c | 7 ++++--- src/test/regress/expected/tidscan.out | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/backend/optimizer/path/tidpath.c b/src/backend/optimizer/path/tidpath.c index 18a3654720b..94dc33f46e5 100644 --- a/src/backend/optimizer/path/tidpath.c +++ b/src/backend/optimizer/path/tidpath.c @@ -460,6 +460,10 @@ BuildParameterizedTidPaths(PlannerInfo *root, RelOptInfo *rel, List *clauses) if (!join_clause_is_movable_to(rinfo, rel)) continue; + /* Put our rel's CTID on the left, as is done for index quals */ + if (!bms_is_member(rel->relid, rinfo->left_relids)) + rinfo = commute_restrictinfo(rinfo, TIDEqualOperator); + /* OK, make list of clauses for this path */ tidquals = list_make1(rinfo); diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index bc991d31254..3d68acd91fa 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -3457,8 +3457,8 @@ create_tidscan_plan(PlannerInfo *root, TidPath *best_path, * still in RestrictInfo form, and drop any that are redundant with the * tidqual. * - * In normal cases simple pointer equality checks will be enough to spot - * duplicate RestrictInfos, so we try that first. + * In normal cases matching rinfo_serial will be enough to spot duplicate + * RestrictInfos, including commuted copies, so we try that first. * * Another common case is that a scan_clauses entry is generated from the * same EquivalenceClass as some tidqual, and is therefore redundant with @@ -3469,6 +3469,7 @@ create_tidscan_plan(PlannerInfo *root, TidPath *best_path, */ if (list_length(tidquals) == 1) { + RestrictInfo *tidqual = linitial_node(RestrictInfo, tidquals); List *qpqual = NIL; ListCell *l; @@ -3478,7 +3479,7 @@ create_tidscan_plan(PlannerInfo *root, TidPath *best_path, if (rinfo->pseudoconstant) continue; /* we may drop pseudoconstants here */ - if (list_member_ptr(tidquals, rinfo)) + if (rinfo->rinfo_serial == tidqual->rinfo_serial) continue; /* simple duplicate */ if (is_redundant_derived_clause(rinfo, tidquals)) continue; /* derived from same EquivalenceClass */ diff --git a/src/test/regress/expected/tidscan.out b/src/test/regress/expected/tidscan.out index 52250e09c95..78b6f51ea9a 100644 --- a/src/test/regress/expected/tidscan.out +++ b/src/test/regress/expected/tidscan.out @@ -119,7 +119,7 @@ FROM tidscan t1 JOIN tidscan t2 ON t1.ctid = t2.ctid WHERE t1.id = 1; -> Seq Scan on tidscan t1 Filter: (id = 1) -> Tid Scan on tidscan t2 - TID Cond: (t1.ctid = ctid) + TID Cond: (ctid = t1.ctid) (5 rows) SELECT t1.ctid, t1.*, t2.ctid, t2.* @@ -138,7 +138,7 @@ FROM tidscan t1 LEFT JOIN tidscan t2 ON t1.ctid = t2.ctid WHERE t1.id = 1; -> Seq Scan on tidscan t1 Filter: (id = 1) -> Tid Scan on tidscan t2 - TID Cond: (t1.ctid = ctid) + TID Cond: (ctid = t1.ctid) (5 rows) SELECT t1.ctid, t1.*, t2.ctid, t2.* -- 2.53.0