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

Commitce76c0b

Browse files
committed
Add a reverse-translation column number array to struct AppendRelInfo.
This provides for cheaper mapping of child columns back to parentcolumns. The one existing use-case in examine_simple_variable()would hardly justify this by itself; but an upcoming bug fix willmake use of this array in a mainstream code path, and it seemslikely that we'll find other uses for it as we continue to buildout the partitioning infrastructure.Discussion:https://postgr.es/m/12424.1575168015@sss.pgh.pa.us
1 parent4526951 commitce76c0b

File tree

8 files changed

+62
-40
lines changed

8 files changed

+62
-40
lines changed

‎src/backend/nodes/copyfuncs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,6 +2327,8 @@ _copyAppendRelInfo(const AppendRelInfo *from)
23272327
COPY_SCALAR_FIELD(parent_reltype);
23282328
COPY_SCALAR_FIELD(child_reltype);
23292329
COPY_NODE_FIELD(translated_vars);
2330+
COPY_SCALAR_FIELD(num_child_cols);
2331+
COPY_POINTER_FIELD(parent_colnos,from->num_child_cols*sizeof(AttrNumber));
23302332
COPY_SCALAR_FIELD(parent_reloid);
23312333

23322334
returnnewnode;

‎src/backend/nodes/equalfuncs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@ _equalAppendRelInfo(const AppendRelInfo *a, const AppendRelInfo *b)
900900
COMPARE_SCALAR_FIELD(parent_reltype);
901901
COMPARE_SCALAR_FIELD(child_reltype);
902902
COMPARE_NODE_FIELD(translated_vars);
903+
COMPARE_SCALAR_FIELD(num_child_cols);
904+
COMPARE_POINTER_FIELD(parent_colnos,a->num_child_cols*sizeof(AttrNumber));
903905
COMPARE_SCALAR_FIELD(parent_reloid);
904906

905907
return true;

‎src/backend/nodes/nodeFuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,6 +3103,7 @@ expression_tree_mutator(Node *node,
31033103

31043104
FLATCOPY(newnode,appinfo,AppendRelInfo);
31053105
MUTATE(newnode->translated_vars,appinfo->translated_vars,List*);
3106+
/* Assume nothing need be done with parent_colnos[] */
31063107
return (Node*)newnode;
31073108
}
31083109
break;

‎src/backend/nodes/outfuncs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,6 +2509,8 @@ _outAppendRelInfo(StringInfo str, const AppendRelInfo *node)
25092509
WRITE_OID_FIELD(parent_reltype);
25102510
WRITE_OID_FIELD(child_reltype);
25112511
WRITE_NODE_FIELD(translated_vars);
2512+
WRITE_INT_FIELD(num_child_cols);
2513+
WRITE_ATTRNUMBER_ARRAY(parent_colnos,node->num_child_cols);
25122514
WRITE_OID_FIELD(parent_reloid);
25132515
}
25142516

‎src/backend/optimizer/prep/prepjointree.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root,
8181
intparentRTindex,Query*setOpQuery,
8282
intchildRToffset);
8383
staticvoidmake_setop_translation_list(Query*query,Indexnewvarno,
84-
List**translated_vars);
84+
AppendRelInfo*appinfo);
8585
staticboolis_simple_subquery(Query*subquery,RangeTblEntry*rte,
8686
JoinExpr*lowest_outer_join);
8787
staticNode*pull_up_simple_values(PlannerInfo*root,Node*jtnode,
@@ -1313,8 +1313,7 @@ pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex,
13131313
appinfo->child_relid=childRTindex;
13141314
appinfo->parent_reltype=InvalidOid;
13151315
appinfo->child_reltype=InvalidOid;
1316-
make_setop_translation_list(setOpQuery,childRTindex,
1317-
&appinfo->translated_vars);
1316+
make_setop_translation_list(setOpQuery,childRTindex,appinfo);
13181317
appinfo->parent_reloid=InvalidOid;
13191318
root->append_rel_list=lappend(root->append_rel_list,appinfo);
13201319

@@ -1356,14 +1355,22 @@ pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex,
13561355
* a UNION ALL member. (At this point it's just a simple list of
13571356
* referencing Vars, but if we succeed in pulling up the member
13581357
* subquery, the Vars will get replaced by pulled-up expressions.)
1358+
* Also create the rather trivial reverse-translation array.
13591359
*/
13601360
staticvoid
13611361
make_setop_translation_list(Query*query,Indexnewvarno,
1362-
List**translated_vars)
1362+
AppendRelInfo*appinfo)
13631363
{
13641364
List*vars=NIL;
1365+
AttrNumber*pcolnos;
13651366
ListCell*l;
13661367

1368+
/* Initialize reverse-translation array with all entries zero */
1369+
/* (entries for resjunk columns will stay that way) */
1370+
appinfo->num_child_cols=list_length(query->targetList);
1371+
appinfo->parent_colnos=pcolnos=
1372+
(AttrNumber*)palloc0(appinfo->num_child_cols*sizeof(AttrNumber));
1373+
13671374
foreach(l,query->targetList)
13681375
{
13691376
TargetEntry*tle= (TargetEntry*)lfirst(l);
@@ -1372,9 +1379,10 @@ make_setop_translation_list(Query *query, Index newvarno,
13721379
continue;
13731380

13741381
vars=lappend(vars,makeVarFromTargetEntry(newvarno,tle));
1382+
pcolnos[tle->resno-1]=tle->resno;
13751383
}
13761384

1377-
*translated_vars=vars;
1385+
appinfo->translated_vars=vars;
13781386
}
13791387

13801388
/*

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typedef struct
3434
staticvoidmake_inh_translation_list(Relationoldrelation,
3535
Relationnewrelation,
3636
Indexnewvarno,
37-
List**translated_vars);
37+
AppendRelInfo*appinfo);
3838
staticNode*adjust_appendrel_attrs_mutator(Node*node,
3939
adjust_appendrel_attrs_context*context);
4040
staticList*adjust_inherited_tlist(List*tlist,
@@ -55,8 +55,7 @@ make_append_rel_info(Relation parentrel, Relation childrel,
5555
appinfo->child_relid=childRTindex;
5656
appinfo->parent_reltype=parentrel->rd_rel->reltype;
5757
appinfo->child_reltype=childrel->rd_rel->reltype;
58-
make_inh_translation_list(parentrel,childrel,childRTindex,
59-
&appinfo->translated_vars);
58+
make_inh_translation_list(parentrel,childrel,childRTindex,appinfo);
6059
appinfo->parent_reloid=RelationGetRelid(parentrel);
6160

6261
returnappinfo;
@@ -65,16 +64,23 @@ make_append_rel_info(Relation parentrel, Relation childrel,
6564
/*
6665
* make_inh_translation_list
6766
* Build the list of translations from parent Vars to child Vars for
68-
* an inheritance child.
67+
* an inheritance child, as well as a reverse-translation array.
68+
*
69+
* The reverse-translation array has an entry for each child relation
70+
* column, which is either the 1-based index of the corresponding parent
71+
* column, or 0 if there's no match (that happens for dropped child columns,
72+
* as well as child columns beyond those of the parent, which are allowed in
73+
* traditional inheritance though not partitioning).
6974
*
7075
* For paranoia's sake, we match type/collation as well as attribute name.
7176
*/
7277
staticvoid
7378
make_inh_translation_list(Relationoldrelation,Relationnewrelation,
7479
Indexnewvarno,
75-
List**translated_vars)
80+
AppendRelInfo*appinfo)
7681
{
7782
List*vars=NIL;
83+
AttrNumber*pcolnos;
7884
TupleDescold_tupdesc=RelationGetDescr(oldrelation);
7985
TupleDescnew_tupdesc=RelationGetDescr(newrelation);
8086
Oidnew_relid=RelationGetRelid(newrelation);
@@ -83,6 +89,11 @@ make_inh_translation_list(Relation oldrelation, Relation newrelation,
8389
intold_attno;
8490
intnew_attno=0;
8591

92+
/* Initialize reverse-translation array with all entries zero */
93+
appinfo->num_child_cols=newnatts;
94+
appinfo->parent_colnos=pcolnos=
95+
(AttrNumber*)palloc0(newnatts*sizeof(AttrNumber));
96+
8697
for (old_attno=0;old_attno<oldnatts;old_attno++)
8798
{
8899
Form_pg_attributeatt;
@@ -115,6 +126,7 @@ make_inh_translation_list(Relation oldrelation, Relation newrelation,
115126
atttypmod,
116127
attcollation,
117128
0));
129+
pcolnos[old_attno]=old_attno+1;
118130
continue;
119131
}
120132

@@ -138,6 +150,7 @@ make_inh_translation_list(Relation oldrelation, Relation newrelation,
138150
elog(ERROR,"could not find inherited attribute \"%s\" of relation \"%s\"",
139151
attname,RelationGetRelationName(newrelation));
140152
new_attno= ((Form_pg_attribute)GETSTRUCT(newtup))->attnum-1;
153+
Assert(new_attno >=0&&new_attno<newnatts);
141154
ReleaseSysCache(newtup);
142155

143156
att=TupleDescAttr(new_tupdesc,new_attno);
@@ -157,10 +170,11 @@ make_inh_translation_list(Relation oldrelation, Relation newrelation,
157170
atttypmod,
158171
attcollation,
159172
0));
173+
pcolnos[new_attno]=old_attno+1;
160174
new_attno++;
161175
}
162176

163-
*translated_vars=vars;
177+
appinfo->translated_vars=vars;
164178
}
165179

166180
/*

‎src/backend/utils/adt/selfuncs.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,29 +4769,17 @@ examine_simple_variable(PlannerInfo *root, Var *var,
47694769
root)->rtekind==RTE_RELATION)
47704770
{
47714771
intparent_varattno;
4772-
ListCell*l;
47734772

4774-
parent_varattno=1;
47754773
found= false;
4776-
foreach(l,appinfo->translated_vars)
4777-
{
4778-
Var*childvar=lfirst_node(Var,l);
4779-
4780-
/* Ignore dropped attributes of the parent. */
4781-
if (childvar!=NULL&&
4782-
varattno==childvar->varattno)
4783-
{
4784-
found= true;
4785-
break;
4786-
}
4787-
parent_varattno++;
4788-
}
4789-
4790-
if (!found)
4791-
break;
4774+
if (varattno <=0||varattno>appinfo->num_child_cols)
4775+
break;/* safety check */
4776+
parent_varattno=appinfo->parent_colnos[varattno-1];
4777+
if (parent_varattno==0)
4778+
break;/* Var is local to child */
47924779

47934780
varno=appinfo->parent_relid;
47944781
varattno=parent_varattno;
4782+
found= true;
47954783

47964784
/* If the parent is itself a child, continue up. */
47974785
appinfo=root->append_rel_array[varno];

‎src/include/nodes/pathnodes.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,17 +2151,13 @@ struct SpecialJoinInfo
21512151
* "append relation" (essentially, a list of child RTEs), we build an
21522152
* AppendRelInfo for each child RTE. The list of AppendRelInfos indicates
21532153
* which child RTEs must be included when expanding the parent, and each node
2154-
* carries information needed to translate Vars referencing the parent into
2155-
* Vars referencing that child.
2156-
*
2157-
* These structs are kept in the PlannerInfo node's append_rel_list.
2158-
* Note that we just throw all the structs into one list, and scan the
2159-
* whole list when desiring to expand any one parent. We could have used
2160-
* a more complex data structure (eg, one list per parent), but this would
2161-
* be harder to update during operations such as pulling up subqueries,
2162-
* and not really any easier to scan. Considering that typical queries
2163-
* will not have many different append parents, it doesn't seem worthwhile
2164-
* to complicate things.
2154+
* carries information needed to translate between columns of the parent and
2155+
* columns of the child.
2156+
*
2157+
* These structs are kept in the PlannerInfo node's append_rel_list, with
2158+
* append_rel_array[] providing a convenient lookup method for the struct
2159+
* associated with a particular child relid (there can be only one, though
2160+
* parent rels may have many entries in append_rel_list).
21652161
*
21662162
* Note: after completion of the planner prep phase, any given RTE is an
21672163
* append parent having entries in append_rel_list if and only if its
@@ -2218,6 +2214,15 @@ typedef struct AppendRelInfo
22182214
*/
22192215
List*translated_vars;/* Expressions in the child's Vars */
22202216

2217+
/*
2218+
* This array simplifies translations in the reverse direction, from
2219+
* child's column numbers to parent's. The entry at [ccolno - 1] is the
2220+
* 1-based parent column number for child column ccolno, or zero if that
2221+
* child column is dropped or doesn't exist in the parent.
2222+
*/
2223+
intnum_child_cols;/* length of array */
2224+
AttrNumber*parent_colnos;/* array of parent attnos, or zeroes */
2225+
22212226
/*
22222227
* We store the parent table's OID here for inheritance, or InvalidOid for
22232228
* UNION ALL. This is only needed to help in generating error messages if

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp