Skip to content

Commit 78b1c55

Browse files
author
Richard Guo
committed
Fix inconsistent RestrictInfo serial numbers
When we generate multiple clones of the same qual condition to cope with outer join identity 3, we need to ensure that all the clones get the same serial number. To achieve this, we reset the root->last_rinfo_serial counter each time we produce RestrictInfo(s) from the qual list (see deconstruct_distribute_oj_quals). This approach works only if we ensure that we are not changing the qual list in any way that'd affect the number of RestrictInfos built from it. However, with b262ad4, an IS NULL qual on a NOT NULL column might result in an additional constant-FALSE RestrictInfo. And different versions of the same qual clause can lead to different conclusions about whether it can be reduced to constant-FALSE. This would affect the number of RestrictInfos built from the qual list for different versions, causing inconsistent RestrictInfo serial numbers across multiple clones of the same qual. This inconsistency can confuse users of these serial numbers, such as rebuild_joinclause_attr_needed, and lead to planner errors such as "ERROR: variable not found in subplan target lists". To fix, reset the root->last_rinfo_serial counter after generating the additional constant-FALSE RestrictInfo. Back-patch to v17 where the issue crept in. In v17, I failed to make a test case that would expose this bug, so no test case for v17. Author: Richard Guo Discussion: https://postgr.es/m/CAMbWs4-B6kafn+LmPuh-TYFwFyEm-vVj3Qqv7Yo-69CEv14rRg@mail.gmail.com
1 parent e2b5693 commit 78b1c55

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/backend/optimizer/plan/initsplan.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2657,11 +2657,18 @@ add_base_clause_to_rel(PlannerInfo *root, Index relid,
26572657

26582658
/*
26592659
* Substitute the origin qual with constant-FALSE if it is provably
2660-
* always false. Note that we keep the same rinfo_serial.
2660+
* always false.
2661+
*
2662+
* Note that we need to keep the same rinfo_serial, since it is in
2663+
* practice the same condition. We also need to reset the
2664+
* last_rinfo_serial counter, which is essential to ensure that the
2665+
* RestrictInfos for the "same" qual condition get identical serial
2666+
* numbers (see deconstruct_distribute_oj_quals).
26612667
*/
26622668
if (restriction_is_always_false(root, restrictinfo))
26632669
{
26642670
int save_rinfo_serial = restrictinfo->rinfo_serial;
2671+
int save_last_rinfo_serial = root->last_rinfo_serial;
26652672

26662673
restrictinfo = make_restrictinfo(root,
26672674
(Expr *) makeBoolConst(false, false),
@@ -2674,6 +2681,7 @@ add_base_clause_to_rel(PlannerInfo *root, Index relid,
26742681
restrictinfo->incompatible_relids,
26752682
restrictinfo->outer_relids);
26762683
restrictinfo->rinfo_serial = save_rinfo_serial;
2684+
root->last_rinfo_serial = save_last_rinfo_serial;
26772685
}
26782686
}
26792687

src/backend/optimizer/util/joininfo.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,19 @@ add_join_clause_to_rels(PlannerInfo *root,
106106
return;
107107

108108
/*
109-
* Substitute constant-FALSE for the origin qual if it is always false.
110-
* Note that we keep the same rinfo_serial.
109+
* Substitute the origin qual with constant-FALSE if it is provably always
110+
* false.
111+
*
112+
* Note that we need to keep the same rinfo_serial, since it is in
113+
* practice the same condition. We also need to reset the
114+
* last_rinfo_serial counter, which is essential to ensure that the
115+
* RestrictInfos for the "same" qual condition get identical serial
116+
* numbers (see deconstruct_distribute_oj_quals).
111117
*/
112118
if (restriction_is_always_false(root, restrictinfo))
113119
{
114120
int save_rinfo_serial = restrictinfo->rinfo_serial;
121+
int save_last_rinfo_serial = root->last_rinfo_serial;
115122

116123
restrictinfo = make_restrictinfo(root,
117124
(Expr *) makeBoolConst(false, false),
@@ -124,6 +131,7 @@ add_join_clause_to_rels(PlannerInfo *root,
124131
restrictinfo->incompatible_relids,
125132
restrictinfo->outer_relids);
126133
restrictinfo->rinfo_serial = save_rinfo_serial;
134+
root->last_rinfo_serial = save_last_rinfo_serial;
127135
}
128136

129137
cur_relid = -1;

0 commit comments

Comments
 (0)