Skip to content

Commit f00ab1f

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 41b98dd commit f00ab1f

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

src/backend/optimizer/plan/initsplan.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -2767,11 +2767,18 @@ add_base_clause_to_rel(PlannerInfo *root, Index relid,
27672767

27682768
/*
27692769
* Substitute the origin qual with constant-FALSE if it is provably
2770-
* always false. Note that we keep the same rinfo_serial.
2770+
* always false.
2771+
*
2772+
* Note that we need to keep the same rinfo_serial, since it is in
2773+
* practice the same condition. We also need to reset the
2774+
* last_rinfo_serial counter, which is essential to ensure that the
2775+
* RestrictInfos for the "same" qual condition get identical serial
2776+
* numbers (see deconstruct_distribute_oj_quals).
27712777
*/
27722778
if (restriction_is_always_false(root, restrictinfo))
27732779
{
27742780
int save_rinfo_serial = restrictinfo->rinfo_serial;
2781+
int save_last_rinfo_serial = root->last_rinfo_serial;
27752782

27762783
restrictinfo = make_restrictinfo(root,
27772784
(Expr *) makeBoolConst(false, false),
@@ -2784,6 +2791,7 @@ add_base_clause_to_rel(PlannerInfo *root, Index relid,
27842791
restrictinfo->incompatible_relids,
27852792
restrictinfo->outer_relids);
27862793
restrictinfo->rinfo_serial = save_rinfo_serial;
2794+
root->last_rinfo_serial = save_last_rinfo_serial;
27872795
}
27882796
}
27892797

src/backend/optimizer/util/joininfo.c

+10-2
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;

src/test/regress/expected/predicate.out

+28
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,31 @@ SELECT * FROM pred_parent WHERE a IS NULL;
290290
(2 rows)
291291

292292
DROP TABLE pred_parent, pred_child;
293+
-- Validate the additional constant-FALSE qual does not cause inconsistent
294+
-- RestrictInfo serial numbers
295+
CREATE TABLE pred_tab (a int PRIMARY KEY, b int);
296+
INSERT INTO pred_tab SELECT i, i FROM generate_series(1, 10)i;
297+
ANALYZE pred_tab;
298+
EXPLAIN (COSTS OFF)
299+
SELECT 1 FROM pred_tab t1
300+
LEFT JOIN
301+
(pred_tab t2 LEFT JOIN pred_tab t3 ON t2.a = t3.a) ON TRUE
302+
LEFT JOIN pred_tab t4 ON t1.a IS NULL AND t1.b = 1
303+
RIGHT JOIN pred_tab t5 ON t1.b = t5.b;
304+
QUERY PLAN
305+
---------------------------------------------------
306+
Hash Right Join
307+
Hash Cond: (t1.b = t5.b)
308+
-> Nested Loop Left Join
309+
-> Nested Loop Left Join
310+
Join Filter: (false AND (t1.b = 1))
311+
-> Seq Scan on pred_tab t1
312+
-> Result
313+
One-Time Filter: false
314+
-> Materialize
315+
-> Seq Scan on pred_tab t2
316+
-> Hash
317+
-> Seq Scan on pred_tab t5
318+
(12 rows)
319+
320+
DROP TABLE pred_tab;

src/test/regress/sql/predicate.sql

+15
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,18 @@ EXPLAIN (COSTS OFF)
147147
SELECT * FROM pred_parent WHERE a IS NULL;
148148

149149
DROP TABLE pred_parent, pred_child;
150+
151+
-- Validate the additional constant-FALSE qual does not cause inconsistent
152+
-- RestrictInfo serial numbers
153+
CREATE TABLE pred_tab (a int PRIMARY KEY, b int);
154+
INSERT INTO pred_tab SELECT i, i FROM generate_series(1, 10)i;
155+
ANALYZE pred_tab;
156+
157+
EXPLAIN (COSTS OFF)
158+
SELECT 1 FROM pred_tab t1
159+
LEFT JOIN
160+
(pred_tab t2 LEFT JOIN pred_tab t3 ON t2.a = t3.a) ON TRUE
161+
LEFT JOIN pred_tab t4 ON t1.a IS NULL AND t1.b = 1
162+
RIGHT JOIN pred_tab t5 ON t1.b = t5.b;
163+
164+
DROP TABLE pred_tab;

0 commit comments

Comments
 (0)