Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf00ab1f

Browse files
author
Richard Guo
committed
Fix inconsistent RestrictInfo serial numbers
When we generate multiple clones of the same qual condition to copewith outer join identity 3, we need to ensure that all the clones getthe same serial number. To achieve this, we reset theroot->last_rinfo_serial counter each time we produce RestrictInfo(s)from the qual list (see deconstruct_distribute_oj_quals). Thisapproach works only if we ensure that we are not changing the quallist in any way that'd affect the number of RestrictInfos built fromit.However, withb262ad4, an IS NULL qual on a NOT NULL column mightresult in an additional constant-FALSE RestrictInfo. And differentversions of the same qual clause can lead to different conclusionsabout whether it can be reduced to constant-FALSE. This would affectthe number of RestrictInfos built from the qual list for differentversions, causing inconsistent RestrictInfo serial numbers acrossmultiple clones of the same qual. This inconsistency can confuseusers of these serial numbers, such as rebuild_joinclause_attr_needed,and lead to planner errors such as "ERROR: variable not found insubplan target lists".To fix, reset the root->last_rinfo_serial counter after generating theadditional constant-FALSE RestrictInfo.Back-patch to v17 where the issue crept in. In v17, I failed to makea test case that would expose this bug, so no test case for v17.Author: Richard GuoDiscussion:https://postgr.es/m/CAMbWs4-B6kafn+LmPuh-TYFwFyEm-vVj3Qqv7Yo-69CEv14rRg@mail.gmail.com
1 parent41b98dd commitf00ab1f

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

‎src/backend/optimizer/plan/initsplan.c

Lines changed: 9 additions & 1 deletion
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
intsave_rinfo_serial=restrictinfo->rinfo_serial;
2781+
intsave_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

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
intsave_rinfo_serial=restrictinfo->rinfo_serial;
121+
intsave_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

Lines changed: 28 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,18 @@ EXPLAIN (COSTS OFF)
147147
SELECT*FROM pred_parentWHERE a ISNULL;
148148

149149
DROPTABLE pred_parent, pred_child;
150+
151+
-- Validate the additional constant-FALSE qual does not cause inconsistent
152+
-- RestrictInfo serial numbers
153+
CREATETABLEpred_tab (aintPRIMARY KEY, bint);
154+
INSERT INTO pred_tabSELECT i, iFROM generate_series(1,10)i;
155+
ANALYZE pred_tab;
156+
157+
EXPLAIN (COSTS OFF)
158+
SELECT1FROM pred_tab t1
159+
LEFT JOIN
160+
(pred_tab t2LEFT JOIN pred_tab t3ONt2.a=t3.a)ON TRUE
161+
LEFT JOIN pred_tab t4ONt1.a ISNULLANDt1.b=1
162+
RIGHT JOIN pred_tab t5ONt1.b=t5.b;
163+
164+
DROPTABLE pred_tab;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp