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

Commite031995

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 parentc1b0d75 commite031995

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
/*****************************************************************************
@@ -4037,13 +4042,59 @@ do { \
40374042
switch (nodeTag(path))
40384043
{
40394044
caseT_Path:
4045+
4046+
/*
4047+
* If the path's restriction clauses contain lateral references to
4048+
* the other relation, we can't reparameterize, because we must
4049+
* not change the RelOptInfo's contents here. (Doing so would
4050+
* break things if we end up using a non-partitionwise join.)
4051+
*/
4052+
if (ris_contain_references_to(root,
4053+
path->parent->baserestrictinfo,
4054+
child_rel->top_parent_relids))
4055+
returnNULL;
4056+
4057+
/*
4058+
* If it's a SampleScan with tablesample parameters referencing
4059+
* the other relation, we can't reparameterize, because we must
4060+
* not change the RTE's contents here. (Doing so would break
4061+
* things if we end up using a non-partitionwise join.)
4062+
*/
4063+
if (path->pathtype==T_SampleScan)
4064+
{
4065+
Indexscan_relid=path->parent->relid;
4066+
RangeTblEntry*rte;
4067+
4068+
/* it should be a base rel with a tablesample clause... */
4069+
Assert(scan_relid>0);
4070+
rte=planner_rt_fetch(scan_relid,root);
4071+
Assert(rte->rtekind==RTE_RELATION);
4072+
Assert(rte->tablesample!=NULL);
4073+
4074+
if (contain_references_to(root, (Node*)rte->tablesample,
4075+
child_rel->top_parent_relids))
4076+
returnNULL;
4077+
}
4078+
40404079
FLAT_COPY_PATH(new_path,path,Path);
40414080
break;
40424081

40434082
caseT_IndexPath:
40444083
{
40454084
IndexPath*ipath;
40464085

4086+
/*
4087+
* If the path's restriction clauses contain lateral
4088+
* references to the other relation, we can't reparameterize,
4089+
* because we must not change the IndexOptInfo's contents
4090+
* here. (Doing so would break things if we end up using a
4091+
* non-partitionwise join.)
4092+
*/
4093+
if (ris_contain_references_to(root,
4094+
path->parent->baserestrictinfo,
4095+
child_rel->top_parent_relids))
4096+
returnNULL;
4097+
40474098
FLAT_COPY_PATH(ipath,path,IndexPath);
40484099
ADJUST_CHILD_ATTRS(ipath->indexclauses);
40494100
new_path= (Path*)ipath;
@@ -4054,6 +4105,18 @@ do { \
40544105
{
40554106
BitmapHeapPath*bhpath;
40564107

4108+
/*
4109+
* If the path's restriction clauses contain lateral
4110+
* references to the other relation, we can't reparameterize,
4111+
* because we must not change the RelOptInfo's contents here.
4112+
* (Doing so would break things if we end up using a
4113+
* non-partitionwise join.)
4114+
*/
4115+
if (ris_contain_references_to(root,
4116+
path->parent->baserestrictinfo,
4117+
child_rel->top_parent_relids))
4118+
returnNULL;
4119+
40574120
FLAT_COPY_PATH(bhpath,path,BitmapHeapPath);
40584121
REPARAMETERIZE_CHILD_PATH(bhpath->bitmapqual);
40594122
new_path= (Path*)bhpath;
@@ -4085,6 +4148,18 @@ do { \
40854148
ForeignPath*fpath;
40864149
ReparameterizeForeignPathByChild_functionrfpc_func;
40874150

4151+
/*
4152+
* If the path's restriction clauses contain lateral
4153+
* references to the other relation, we can't reparameterize,
4154+
* because we must not change the RelOptInfo's contents here.
4155+
* (Doing so would break things if we end up using a
4156+
* non-partitionwise join.)
4157+
*/
4158+
if (ris_contain_references_to(root,
4159+
path->parent->baserestrictinfo,
4160+
child_rel->top_parent_relids))
4161+
returnNULL;
4162+
40884163
FLAT_COPY_PATH(fpath,path,ForeignPath);
40894164
if (fpath->fdw_outerpath)
40904165
REPARAMETERIZE_CHILD_PATH(fpath->fdw_outerpath);
@@ -4103,6 +4178,18 @@ do { \
41034178
{
41044179
CustomPath*cpath;
41054180

4181+
/*
4182+
* If the path's restriction clauses contain lateral
4183+
* references to the other relation, we can't reparameterize,
4184+
* because we must not change the RelOptInfo's contents here.
4185+
* (Doing so would break things if we end up using a
4186+
* non-partitionwise join.)
4187+
*/
4188+
if (ris_contain_references_to(root,
4189+
path->parent->baserestrictinfo,
4190+
child_rel->top_parent_relids))
4191+
returnNULL;
4192+
41064193
FLAT_COPY_PATH(cpath,path,CustomPath);
41074194
REPARAMETERIZE_CHILD_PATH_LIST(cpath->custom_paths);
41084195
if (cpath->methods&&
@@ -4279,3 +4366,91 @@ reparameterize_pathlist_by_child(PlannerInfo *root,
42794366

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp