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

Commit62f1202

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 parent88be3f0 commit62f1202

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
/*****************************************************************************
@@ -4103,13 +4108,59 @@ do { \
41034108
switch (nodeTag(path))
41044109
{
41054110
caseT_Path:
4111+
4112+
/*
4113+
* If the path's restriction clauses contain lateral references to
4114+
* the other relation, we can't reparameterize, because we must
4115+
* not change the RelOptInfo's contents here. (Doing so would
4116+
* break things if we end up using a non-partitionwise join.)
4117+
*/
4118+
if (ris_contain_references_to(root,
4119+
path->parent->baserestrictinfo,
4120+
child_rel->top_parent_relids))
4121+
returnNULL;
4122+
4123+
/*
4124+
* If it's a SampleScan with tablesample parameters referencing
4125+
* the other relation, we can't reparameterize, because we must
4126+
* not change the RTE's contents here. (Doing so would break
4127+
* things if we end up using a non-partitionwise join.)
4128+
*/
4129+
if (path->pathtype==T_SampleScan)
4130+
{
4131+
Indexscan_relid=path->parent->relid;
4132+
RangeTblEntry*rte;
4133+
4134+
/* it should be a base rel with a tablesample clause... */
4135+
Assert(scan_relid>0);
4136+
rte=planner_rt_fetch(scan_relid,root);
4137+
Assert(rte->rtekind==RTE_RELATION);
4138+
Assert(rte->tablesample!=NULL);
4139+
4140+
if (contain_references_to(root, (Node*)rte->tablesample,
4141+
child_rel->top_parent_relids))
4142+
returnNULL;
4143+
}
4144+
41064145
FLAT_COPY_PATH(new_path,path,Path);
41074146
break;
41084147

41094148
caseT_IndexPath:
41104149
{
41114150
IndexPath*ipath;
41124151

4152+
/*
4153+
* If the path's restriction clauses contain lateral
4154+
* references to the other relation, we can't reparameterize,
4155+
* because we must not change the IndexOptInfo's contents
4156+
* here. (Doing so would break things if we end up using a
4157+
* non-partitionwise join.)
4158+
*/
4159+
if (ris_contain_references_to(root,
4160+
path->parent->baserestrictinfo,
4161+
child_rel->top_parent_relids))
4162+
returnNULL;
4163+
41134164
FLAT_COPY_PATH(ipath,path,IndexPath);
41144165
ADJUST_CHILD_ATTRS(ipath->indexclauses);
41154166
new_path= (Path*)ipath;
@@ -4120,6 +4171,18 @@ do { \
41204171
{
41214172
BitmapHeapPath*bhpath;
41224173

4174+
/*
4175+
* If the path's restriction clauses contain lateral
4176+
* references to the other relation, we can't reparameterize,
4177+
* because we must not change the RelOptInfo's contents here.
4178+
* (Doing so would break things if we end up using a
4179+
* non-partitionwise join.)
4180+
*/
4181+
if (ris_contain_references_to(root,
4182+
path->parent->baserestrictinfo,
4183+
child_rel->top_parent_relids))
4184+
returnNULL;
4185+
41234186
FLAT_COPY_PATH(bhpath,path,BitmapHeapPath);
41244187
REPARAMETERIZE_CHILD_PATH(bhpath->bitmapqual);
41254188
new_path= (Path*)bhpath;
@@ -4151,6 +4214,18 @@ do { \
41514214
ForeignPath*fpath;
41524215
ReparameterizeForeignPathByChild_functionrfpc_func;
41534216

4217+
/*
4218+
* If the path's restriction clauses contain lateral
4219+
* references to the other relation, we can't reparameterize,
4220+
* because we must not change the RelOptInfo's contents here.
4221+
* (Doing so would break things if we end up using a
4222+
* non-partitionwise join.)
4223+
*/
4224+
if (ris_contain_references_to(root,
4225+
path->parent->baserestrictinfo,
4226+
child_rel->top_parent_relids))
4227+
returnNULL;
4228+
41544229
FLAT_COPY_PATH(fpath,path,ForeignPath);
41554230
if (fpath->fdw_outerpath)
41564231
REPARAMETERIZE_CHILD_PATH(fpath->fdw_outerpath);
@@ -4169,6 +4244,18 @@ do { \
41694244
{
41704245
CustomPath*cpath;
41714246

4247+
/*
4248+
* If the path's restriction clauses contain lateral
4249+
* references to the other relation, we can't reparameterize,
4250+
* because we must not change the RelOptInfo's contents here.
4251+
* (Doing so would break things if we end up using a
4252+
* non-partitionwise join.)
4253+
*/
4254+
if (ris_contain_references_to(root,
4255+
path->parent->baserestrictinfo,
4256+
child_rel->top_parent_relids))
4257+
returnNULL;
4258+
41724259
FLAT_COPY_PATH(cpath,path,CustomPath);
41734260
REPARAMETERIZE_CHILD_PATH_LIST(cpath->custom_paths);
41744261
if (cpath->methods&&
@@ -4358,3 +4445,91 @@ reparameterize_pathlist_by_child(PlannerInfo *root,
43584445

43594446
returnresult;
43604447
}
4448+
4449+
/*
4450+
* contain_references_to
4451+
*Detect whether any Vars or PlaceHolderVars in the given clause contain
4452+
*lateral references to the given 'relids'.
4453+
*/
4454+
staticbool
4455+
contain_references_to(PlannerInfo*root,Node*clause,Relidsrelids)
4456+
{
4457+
boolret= false;
4458+
List*vars;
4459+
ListCell*lc;
4460+
4461+
/*
4462+
* Examine all Vars and PlaceHolderVars used in the clause.
4463+
*
4464+
* By omitting the relevant flags, this also gives us a cheap sanity check
4465+
* that no aggregates or window functions appear in the clause. We don't
4466+
* expect any of those in scan-level restrictions or tablesamples.
4467+
*/
4468+
vars=pull_var_clause(clause,PVC_INCLUDE_PLACEHOLDERS);
4469+
foreach(lc,vars)
4470+
{
4471+
Node*node= (Node*)lfirst(lc);
4472+
4473+
if (IsA(node,Var))
4474+
{
4475+
Var*var= (Var*)node;
4476+
4477+
if (bms_is_member(var->varno,relids))
4478+
{
4479+
ret= true;
4480+
break;
4481+
}
4482+
}
4483+
elseif (IsA(node,PlaceHolderVar))
4484+
{
4485+
PlaceHolderVar*phv= (PlaceHolderVar*)node;
4486+
PlaceHolderInfo*phinfo=find_placeholder_info(root,phv);
4487+
4488+
/*
4489+
* We should check both ph_eval_at (in case the PHV is to be
4490+
* computed at the other relation and then laterally referenced
4491+
* here) and ph_lateral (in case the PHV is to be evaluated here
4492+
* but contains lateral references to the other relation). The
4493+
* former case should not occur in baserestrictinfo clauses, but
4494+
* it can occur in tablesample clauses.
4495+
*/
4496+
if (bms_overlap(phinfo->ph_eval_at,relids)||
4497+
bms_overlap(phinfo->ph_lateral,relids))
4498+
{
4499+
ret= true;
4500+
break;
4501+
}
4502+
}
4503+
else
4504+
Assert(false);
4505+
}
4506+
4507+
list_free(vars);
4508+
4509+
returnret;
4510+
}
4511+
4512+
/*
4513+
* ris_contain_references_to
4514+
*Apply contain_references_to() to a list of RestrictInfos.
4515+
*
4516+
* We need extra code for this because pull_var_clause() can't descend
4517+
* through RestrictInfos.
4518+
*/
4519+
staticbool
4520+
ris_contain_references_to(PlannerInfo*root,List*rinfos,Relidsrelids)
4521+
{
4522+
ListCell*lc;
4523+
4524+
foreach(lc,rinfos)
4525+
{
4526+
RestrictInfo*rinfo=lfirst_node(RestrictInfo,lc);
4527+
4528+
/* Pseudoconstant clauses can't contain any Vars or PHVs */
4529+
if (rinfo->pseudoconstant)
4530+
continue;
4531+
if (contain_references_to(root, (Node*)rinfo->clause,relids))
4532+
return true;
4533+
}
4534+
return false;
4535+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp