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

Commit12ec16d

Browse files
committed
Apply band-aid fix for an oversight in reparameterize_path_by_child.
The path we wish to reparameterize is not a standalone object:in particular, it implicitly references baserestrictinfo clausesin the associated RelOptInfo, and if it's a SampleScan path thenthere is also the TableSampleClause in the RTE to worry about.Both of those could contain lateral references to the join partnerrelation, which would need to be modified to refer to its child.Since we aren't doing that, affected queries can give wrong answers,or odd failures such as "variable not found in subplan target list",or executor crashes. But we can't just summarily modify thoseexpressions, because they are shared with other paths for the rel.We'd break things if we modify them and then end up using somenon-partitioned-join path.In HEAD, we plan to fix this by postponing reparameterizationuntil create_plan(), when we know that those other paths areno longer of interest, and then adjusting those expressions alongwith the ones in the path itself. That seems like too big a changefor stable branches however. In the back branches, let's just detectwhether any troublesome lateral references actually exist in thoseexpressions, and fail reparameterization if so. This will result innot performing a partitioned join in such cases. Given the lack offield complaints, nobody's likely to miss the optimization.Report and patch by Richard Guo. Apply to 12-16 only, sincethe intended fix for HEAD looks quite different. We're not quiteready to push the HEAD fix, but with back-branch releases comingup soon, it seems wise to get this stopgap fix in place there.Discussion:https://postgr.es/m/CAMbWs496+N=UAjOc=rcD3P7B6oJe4rZw08e_TZRUsWbPxZW3Tw@mail.gmail.com
1 parent76fd779 commit12ec16d

File tree

3 files changed

+390
-0
lines changed

3 files changed

+390
-0
lines changed

‎src/backend/optimizer/util/pathnode.c

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include"optimizer/optimizer.h"
2727
#include"optimizer/pathnode.h"
2828
#include"optimizer/paths.h"
29+
#include"optimizer/placeholder.h"
2930
#include"optimizer/planmain.h"
3031
#include"optimizer/prep.h"
3132
#include"optimizer/restrictinfo.h"
@@ -56,6 +57,10 @@ static intappend_startup_cost_compare(const ListCell *a, const ListCell *b);
5657
staticList*reparameterize_pathlist_by_child(PlannerInfo*root,
5758
List*pathlist,
5859
RelOptInfo*child_rel);
60+
staticboolcontain_references_to(PlannerInfo*root,Node*clause,
61+
Relidsrelids);
62+
staticboolris_contain_references_to(PlannerInfo*root,List*rinfos,
63+
Relidsrelids);
5964

6065

6166
/*****************************************************************************
@@ -4052,13 +4057,59 @@ do { \
40524057
switch (nodeTag(path))
40534058
{
40544059
caseT_Path:
4060+
4061+
/*
4062+
* If the path's restriction clauses contain lateral references to
4063+
* the other relation, we can't reparameterize, because we must
4064+
* not change the RelOptInfo's contents here. (Doing so would
4065+
* break things if we end up using a non-partitionwise join.)
4066+
*/
4067+
if (ris_contain_references_to(root,
4068+
path->parent->baserestrictinfo,
4069+
child_rel->top_parent_relids))
4070+
returnNULL;
4071+
4072+
/*
4073+
* If it's a SampleScan with tablesample parameters referencing
4074+
* the other relation, we can't reparameterize, because we must
4075+
* not change the RTE's contents here. (Doing so would break
4076+
* things if we end up using a non-partitionwise join.)
4077+
*/
4078+
if (path->pathtype==T_SampleScan)
4079+
{
4080+
Indexscan_relid=path->parent->relid;
4081+
RangeTblEntry*rte;
4082+
4083+
/* it should be a base rel with a tablesample clause... */
4084+
Assert(scan_relid>0);
4085+
rte=planner_rt_fetch(scan_relid,root);
4086+
Assert(rte->rtekind==RTE_RELATION);
4087+
Assert(rte->tablesample!=NULL);
4088+
4089+
if (contain_references_to(root, (Node*)rte->tablesample,
4090+
child_rel->top_parent_relids))
4091+
returnNULL;
4092+
}
4093+
40554094
FLAT_COPY_PATH(new_path,path,Path);
40564095
break;
40574096

40584097
caseT_IndexPath:
40594098
{
40604099
IndexPath*ipath;
40614100

4101+
/*
4102+
* If the path's restriction clauses contain lateral
4103+
* references to the other relation, we can't reparameterize,
4104+
* because we must not change the IndexOptInfo's contents
4105+
* here. (Doing so would break things if we end up using a
4106+
* non-partitionwise join.)
4107+
*/
4108+
if (ris_contain_references_to(root,
4109+
path->parent->baserestrictinfo,
4110+
child_rel->top_parent_relids))
4111+
returnNULL;
4112+
40624113
FLAT_COPY_PATH(ipath,path,IndexPath);
40634114
ADJUST_CHILD_ATTRS(ipath->indexclauses);
40644115
new_path= (Path*)ipath;
@@ -4069,6 +4120,18 @@ do { \
40694120
{
40704121
BitmapHeapPath*bhpath;
40714122

4123+
/*
4124+
* If the path's restriction clauses contain lateral
4125+
* references to the other relation, we can't reparameterize,
4126+
* because we must not change the RelOptInfo's contents here.
4127+
* (Doing so would break things if we end up using a
4128+
* non-partitionwise join.)
4129+
*/
4130+
if (ris_contain_references_to(root,
4131+
path->parent->baserestrictinfo,
4132+
child_rel->top_parent_relids))
4133+
returnNULL;
4134+
40724135
FLAT_COPY_PATH(bhpath,path,BitmapHeapPath);
40734136
REPARAMETERIZE_CHILD_PATH(bhpath->bitmapqual);
40744137
new_path= (Path*)bhpath;
@@ -4100,6 +4163,18 @@ do { \
41004163
ForeignPath*fpath;
41014164
ReparameterizeForeignPathByChild_functionrfpc_func;
41024165

4166+
/*
4167+
* If the path's restriction clauses contain lateral
4168+
* references to the other relation, we can't reparameterize,
4169+
* because we must not change the RelOptInfo's contents here.
4170+
* (Doing so would break things if we end up using a
4171+
* non-partitionwise join.)
4172+
*/
4173+
if (ris_contain_references_to(root,
4174+
path->parent->baserestrictinfo,
4175+
child_rel->top_parent_relids))
4176+
returnNULL;
4177+
41034178
FLAT_COPY_PATH(fpath,path,ForeignPath);
41044179
if (fpath->fdw_outerpath)
41054180
REPARAMETERIZE_CHILD_PATH(fpath->fdw_outerpath);
@@ -4118,6 +4193,18 @@ do { \
41184193
{
41194194
CustomPath*cpath;
41204195

4196+
/*
4197+
* If the path's restriction clauses contain lateral
4198+
* references to the other relation, we can't reparameterize,
4199+
* because we must not change the RelOptInfo's contents here.
4200+
* (Doing so would break things if we end up using a
4201+
* non-partitionwise join.)
4202+
*/
4203+
if (ris_contain_references_to(root,
4204+
path->parent->baserestrictinfo,
4205+
child_rel->top_parent_relids))
4206+
returnNULL;
4207+
41214208
FLAT_COPY_PATH(cpath,path,CustomPath);
41224209
REPARAMETERIZE_CHILD_PATH_LIST(cpath->custom_paths);
41234210
if (cpath->methods&&
@@ -4296,3 +4383,91 @@ reparameterize_pathlist_by_child(PlannerInfo *root,
42964383

42974384
returnresult;
42984385
}
4386+
4387+
/*
4388+
* contain_references_to
4389+
*Detect whether any Vars or PlaceHolderVars in the given clause contain
4390+
*lateral references to the given 'relids'.
4391+
*/
4392+
staticbool
4393+
contain_references_to(PlannerInfo*root,Node*clause,Relidsrelids)
4394+
{
4395+
boolret= false;
4396+
List*vars;
4397+
ListCell*lc;
4398+
4399+
/*
4400+
* Examine all Vars and PlaceHolderVars used in the clause.
4401+
*
4402+
* By omitting the relevant flags, this also gives us a cheap sanity check
4403+
* that no aggregates or window functions appear in the clause. We don't
4404+
* expect any of those in scan-level restrictions or tablesamples.
4405+
*/
4406+
vars=pull_var_clause(clause,PVC_INCLUDE_PLACEHOLDERS);
4407+
foreach(lc,vars)
4408+
{
4409+
Node*node= (Node*)lfirst(lc);
4410+
4411+
if (IsA(node,Var))
4412+
{
4413+
Var*var= (Var*)node;
4414+
4415+
if (bms_is_member(var->varno,relids))
4416+
{
4417+
ret= true;
4418+
break;
4419+
}
4420+
}
4421+
elseif (IsA(node,PlaceHolderVar))
4422+
{
4423+
PlaceHolderVar*phv= (PlaceHolderVar*)node;
4424+
PlaceHolderInfo*phinfo=find_placeholder_info(root,phv, false);
4425+
4426+
/*
4427+
* We should check both ph_eval_at (in case the PHV is to be
4428+
* computed at the other relation and then laterally referenced
4429+
* here) and ph_lateral (in case the PHV is to be evaluated here
4430+
* but contains lateral references to the other relation). The
4431+
* former case should not occur in baserestrictinfo clauses, but
4432+
* it can occur in tablesample clauses.
4433+
*/
4434+
if (bms_overlap(phinfo->ph_eval_at,relids)||
4435+
bms_overlap(phinfo->ph_lateral,relids))
4436+
{
4437+
ret= true;
4438+
break;
4439+
}
4440+
}
4441+
else
4442+
Assert(false);
4443+
}
4444+
4445+
list_free(vars);
4446+
4447+
returnret;
4448+
}
4449+
4450+
/*
4451+
* ris_contain_references_to
4452+
*Apply contain_references_to() to a list of RestrictInfos.
4453+
*
4454+
* We need extra code for this because pull_var_clause() can't descend
4455+
* through RestrictInfos.
4456+
*/
4457+
staticbool
4458+
ris_contain_references_to(PlannerInfo*root,List*rinfos,Relidsrelids)
4459+
{
4460+
ListCell*lc;
4461+
4462+
foreach(lc,rinfos)
4463+
{
4464+
RestrictInfo*rinfo=lfirst_node(RestrictInfo,lc);
4465+
4466+
/* Pseudoconstant clauses can't contain any Vars or PHVs */
4467+
if (rinfo->pseudoconstant)
4468+
continue;
4469+
if (contain_references_to(root, (Node*)rinfo->clause,relids))
4470+
return true;
4471+
}
4472+
return false;
4473+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp