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

Commitc94fb8e

Browse files
committed
Standardize some more loops that chase down parallel lists.
We have forboth() and forthree() macros that simplify iteratingthrough several parallel lists, but not everyplace that couldreasonably use those was doing so. Also invent forfour() andforfive() macros to do the same for four or five parallel lists,and use those where applicable.The immediate motivation for doing this is to reduce the numberof ad-hoc lnext() calls, to reduce the footprint of a WIP patch.However, it seems like good cleanup and error-proofing anyway;the places that were combining forthree() with a manually iteratedloop seem particularly illegible and bug-prone.There was some speculation about restructuring related parsetreerepresentations to reduce the need for parallel list chasing ofthis sort. Perhaps that's a win, or perhaps not, but in any caseit would be considerably more invasive than this patch; and it'snot particularly related to my immediate goal of improving theList infrastructure. So I'll leave that question for another day.Patch by me; thanks to David Rowley for review.Discussion:https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
1 parent0a271df commitc94fb8e

File tree

9 files changed

+76
-115
lines changed

9 files changed

+76
-115
lines changed

‎src/backend/access/common/tupdesc.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -902,23 +902,12 @@ BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
902902
desc=CreateTemplateTupleDesc(natts);
903903

904904
attnum=0;
905-
906-
l2=list_head(types);
907-
l3=list_head(typmods);
908-
l4=list_head(collations);
909-
foreach(l1,names)
905+
forfour(l1,names,l2,types,l3,typmods,l4,collations)
910906
{
911907
char*attname=strVal(lfirst(l1));
912-
Oidatttypid;
913-
int32atttypmod;
914-
Oidattcollation;
915-
916-
atttypid=lfirst_oid(l2);
917-
l2=lnext(l2);
918-
atttypmod=lfirst_int(l3);
919-
l3=lnext(l3);
920-
attcollation=lfirst_oid(l4);
921-
l4=lnext(l4);
908+
Oidatttypid=lfirst_oid(l2);
909+
int32atttypmod=lfirst_int(l3);
910+
Oidattcollation=lfirst_oid(l4);
922911

923912
attnum++;
924913

‎src/backend/executor/execExpr.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,6 @@ ExecInitExprRec(Expr *node, ExprState *state,
16831683
*l_opfamily,
16841684
*l_inputcollid;
16851685
ListCell*lc;
1686-
intoff;
16871686

16881687
/*
16891688
* Iterate over each field, prepare comparisons. To handle
@@ -1695,20 +1694,11 @@ ExecInitExprRec(Expr *node, ExprState *state,
16951694
Assert(list_length(rcexpr->opfamilies)==nopers);
16961695
Assert(list_length(rcexpr->inputcollids)==nopers);
16971696

1698-
off=0;
1699-
for (off=0,
1700-
l_left_expr=list_head(rcexpr->largs),
1701-
l_right_expr=list_head(rcexpr->rargs),
1702-
l_opno=list_head(rcexpr->opnos),
1703-
l_opfamily=list_head(rcexpr->opfamilies),
1704-
l_inputcollid=list_head(rcexpr->inputcollids);
1705-
off<nopers;
1706-
off++,
1707-
l_left_expr=lnext(l_left_expr),
1708-
l_right_expr=lnext(l_right_expr),
1709-
l_opno=lnext(l_opno),
1710-
l_opfamily=lnext(l_opfamily),
1711-
l_inputcollid=lnext(l_inputcollid))
1697+
forfive(l_left_expr,rcexpr->largs,
1698+
l_right_expr,rcexpr->rargs,
1699+
l_opno,rcexpr->opnos,
1700+
l_opfamily,rcexpr->opfamilies,
1701+
l_inputcollid,rcexpr->inputcollids)
17121702
{
17131703
Expr*left_expr= (Expr*)lfirst(l_left_expr);
17141704
Expr*right_expr= (Expr*)lfirst(l_right_expr);

‎src/backend/executor/nodeIndexscan.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,12 +1332,12 @@ ExecIndexBuildScanKeys(PlanState *planstate, Relation index,
13321332
{
13331333
/* (indexkey, indexkey, ...) op (expression, expression, ...) */
13341334
RowCompareExpr*rc= (RowCompareExpr*)clause;
1335-
ListCell*largs_cell=list_head(rc->largs);
1336-
ListCell*rargs_cell=list_head(rc->rargs);
1337-
ListCell*opnos_cell=list_head(rc->opnos);
1338-
ListCell*collids_cell=list_head(rc->inputcollids);
13391335
ScanKeyfirst_sub_key;
13401336
intn_sub_key;
1337+
ListCell*largs_cell;
1338+
ListCell*rargs_cell;
1339+
ListCell*opnos_cell;
1340+
ListCell*collids_cell;
13411341

13421342
Assert(!isorderby);
13431343

@@ -1346,19 +1346,22 @@ ExecIndexBuildScanKeys(PlanState *planstate, Relation index,
13461346
n_sub_key=0;
13471347

13481348
/* Scan RowCompare columns and generate subsidiary ScanKey items */
1349-
while (opnos_cell!=NULL)
1349+
forfour(largs_cell,rc->largs,rargs_cell,rc->rargs,
1350+
opnos_cell,rc->opnos,collids_cell,rc->inputcollids)
13501351
{
13511352
ScanKeythis_sub_key=&first_sub_key[n_sub_key];
13521353
intflags=SK_ROW_MEMBER;
13531354
Datumscanvalue;
13541355
Oidinputcollation;
13551356

1357+
leftop= (Expr*)lfirst(largs_cell);
1358+
rightop= (Expr*)lfirst(rargs_cell);
1359+
opno=lfirst_oid(opnos_cell);
1360+
inputcollation=lfirst_oid(collids_cell);
1361+
13561362
/*
13571363
* leftop should be the index key Var, possibly relabeled
13581364
*/
1359-
leftop= (Expr*)lfirst(largs_cell);
1360-
largs_cell=lnext(largs_cell);
1361-
13621365
if (leftop&&IsA(leftop,RelabelType))
13631366
leftop= ((RelabelType*)leftop)->arg;
13641367

@@ -1374,9 +1377,6 @@ ExecIndexBuildScanKeys(PlanState *planstate, Relation index,
13741377
* We have to look up the operator's associated btree support
13751378
* function
13761379
*/
1377-
opno=lfirst_oid(opnos_cell);
1378-
opnos_cell=lnext(opnos_cell);
1379-
13801380
if (index->rd_rel->relam!=BTREE_AM_OID||
13811381
varattno<1||varattno>indnkeyatts)
13821382
elog(ERROR,"bogus RowCompare index qualification");
@@ -1398,15 +1398,9 @@ ExecIndexBuildScanKeys(PlanState *planstate, Relation index,
13981398
elog(ERROR,"missing support function %d(%u,%u) in opfamily %u",
13991399
BTORDER_PROC,op_lefttype,op_righttype,opfamily);
14001400

1401-
inputcollation=lfirst_oid(collids_cell);
1402-
collids_cell=lnext(collids_cell);
1403-
14041401
/*
14051402
* rightop is the constant or variable comparison value
14061403
*/
1407-
rightop= (Expr*)lfirst(rargs_cell);
1408-
rargs_cell=lnext(rargs_cell);
1409-
14101404
if (rightop&&IsA(rightop,RelabelType))
14111405
rightop= ((RelabelType*)rightop)->arg;
14121406

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,17 +1680,14 @@ convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
16801680
*/
16811681
tlist=testlist=paramids=NIL;
16821682
resno=1;
1683-
/* there's no "forfour" so we have to chase one of the lists manually */
1684-
cc=list_head(opcollations);
1685-
forthree(lc,leftargs,rc,rightargs,oc,opids)
1683+
forfour(lc,leftargs,rc,rightargs,oc,opids,cc,opcollations)
16861684
{
16871685
Node*leftarg= (Node*)lfirst(lc);
16881686
Node*rightarg= (Node*)lfirst(rc);
16891687
Oidopid=lfirst_oid(oc);
16901688
Oidopcollation=lfirst_oid(cc);
16911689
Param*param;
16921690

1693-
cc=lnext(cc);
16941691
param=generate_new_exec_param(root,
16951692
exprType(rightarg),
16961693
exprTypmod(rightarg),

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,17 +1130,14 @@ generate_setop_tlist(List *colTypes, List *colCollations,
11301130
TargetEntry*tle;
11311131
Node*expr;
11321132

1133-
/* there's no forfour() so we must chase one list manually */
1134-
rtlc=list_head(refnames_tlist);
1135-
forthree(ctlc,colTypes,cclc,colCollations,itlc,input_tlist)
1133+
forfour(ctlc,colTypes,cclc,colCollations,
1134+
itlc,input_tlist,rtlc,refnames_tlist)
11361135
{
11371136
OidcolType=lfirst_oid(ctlc);
11381137
OidcolColl=lfirst_oid(cclc);
11391138
TargetEntry*inputtle= (TargetEntry*)lfirst(itlc);
11401139
TargetEntry*reftle= (TargetEntry*)lfirst(rtlc);
11411140

1142-
rtlc=lnext(rtlc);
1143-
11441141
Assert(inputtle->resno==resno);
11451142
Assert(reftle->resno==resno);
11461143
Assert(!inputtle->resjunk);

‎src/backend/parser/analyze.c

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -831,18 +831,14 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
831831
*/
832832
rte=pstate->p_target_rangetblentry;
833833
qry->targetList=NIL;
834-
icols=list_head(icolumns);
835-
attnos=list_head(attrnos);
836-
foreach(lc,exprList)
834+
Assert(list_length(exprList) <=list_length(icolumns));
835+
forthree(lc,exprList,icols,icolumns,attnos,attrnos)
837836
{
838837
Expr*expr= (Expr*)lfirst(lc);
839-
ResTarget*col;
840-
AttrNumberattr_num;
838+
ResTarget*col=lfirst_node(ResTarget,icols);
839+
AttrNumberattr_num= (AttrNumber)lfirst_int(attnos);
841840
TargetEntry*tle;
842841

843-
col=lfirst_node(ResTarget,icols);
844-
attr_num= (AttrNumber)lfirst_int(attnos);
845-
846842
tle=makeTargetEntry(expr,
847843
attr_num,
848844
col->name,
@@ -851,9 +847,6 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
851847

852848
rte->insertedCols=bms_add_member(rte->insertedCols,
853849
attr_num-FirstLowInvalidHeapAttributeNumber);
854-
855-
icols=lnext(icols);
856-
attnos=lnext(attnos);
857850
}
858851

859852
/* Process ON CONFLICT, if any. */
@@ -950,19 +943,16 @@ transformInsertRow(ParseState *pstate, List *exprlist,
950943
* Prepare columns for assignment to target table.
951944
*/
952945
result=NIL;
953-
icols=list_head(icolumns);
954-
attnos=list_head(attrnos);
955-
foreach(lc,exprlist)
946+
forthree(lc,exprlist,icols,icolumns,attnos,attrnos)
956947
{
957948
Expr*expr= (Expr*)lfirst(lc);
958-
ResTarget*col;
959-
960-
col=lfirst_node(ResTarget,icols);
949+
ResTarget*col=lfirst_node(ResTarget,icols);
950+
intattno=lfirst_int(attnos);
961951

962952
expr=transformAssignedExpr(pstate,expr,
963953
EXPR_KIND_INSERT_TARGET,
964954
col->name,
965-
lfirst_int(attnos),
955+
attno,
966956
col->indirection,
967957
col->location);
968958

@@ -991,9 +981,6 @@ transformInsertRow(ParseState *pstate, List *exprlist,
991981
}
992982

993983
result=lappend(result,expr);
994-
995-
icols=lnext(icols);
996-
attnos=lnext(attnos);
997984
}
998985

999986
returnresult;
@@ -1699,11 +1686,11 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
16991686
qry->targetList=NIL;
17001687
targetvars=NIL;
17011688
targetnames=NIL;
1702-
left_tlist=list_head(leftmostQuery->targetList);
17031689

1704-
forthree(lct,sostmt->colTypes,
1705-
lcm,sostmt->colTypmods,
1706-
lcc,sostmt->colCollations)
1690+
forfour(lct,sostmt->colTypes,
1691+
lcm,sostmt->colTypmods,
1692+
lcc,sostmt->colCollations,
1693+
left_tlist,leftmostQuery->targetList)
17071694
{
17081695
OidcolType=lfirst_oid(lct);
17091696
int32colTypmod=lfirst_int(lcm);
@@ -1729,7 +1716,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
17291716
qry->targetList=lappend(qry->targetList,tle);
17301717
targetvars=lappend(targetvars,var);
17311718
targetnames=lappend(targetnames,makeString(colName));
1732-
left_tlist=lnext(left_tlist);
17331719
}
17341720

17351721
/*
@@ -2201,10 +2187,9 @@ determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
22012187
* dummy result expressions of the non-recursive term.
22022188
*/
22032189
targetList=NIL;
2204-
left_tlist=list_head(leftmostQuery->targetList);
22052190
next_resno=1;
22062191

2207-
foreach(nrtl,nrtargetlist)
2192+
forboth(nrtl,nrtargetlist,left_tlist,leftmostQuery->targetList)
22082193
{
22092194
TargetEntry*nrtle= (TargetEntry*)lfirst(nrtl);
22102195
TargetEntry*lefttle= (TargetEntry*)lfirst(left_tlist);
@@ -2218,7 +2203,6 @@ determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
22182203
colName,
22192204
false);
22202205
targetList=lappend(targetList,tle);
2221-
left_tlist=lnext(left_tlist);
22222206
}
22232207

22242208
/* Now build CTE's output column info using dummy targetlist */

‎src/backend/parser/parse_func.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,13 +2124,12 @@ LookupFuncWithArgs(ObjectType objtype, ObjectWithArgs *func, bool noError)
21242124
FUNC_MAX_ARGS,
21252125
FUNC_MAX_ARGS)));
21262126

2127-
args_item=list_head(func->objargs);
2128-
for (i=0;i<argcount;i++)
2127+
i=0;
2128+
foreach(args_item,func->objargs)
21292129
{
21302130
TypeName*t= (TypeName*)lfirst(args_item);
21312131

2132-
argoids[i]=LookupTypeNameOid(NULL,t,noError);
2133-
args_item=lnext(args_item);
2132+
argoids[i++]=LookupTypeNameOid(NULL,t,noError);
21342133
}
21352134

21362135
/*

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

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9811,31 +9811,18 @@ get_tablefunc(TableFunc *tf, deparse_context *context, bool showimplicit)
98119811
ListCell*l5;
98129812
intcolnum=0;
98139813

9814-
l2=list_head(tf->coltypes);
9815-
l3=list_head(tf->coltypmods);
9816-
l4=list_head(tf->colexprs);
9817-
l5=list_head(tf->coldefexprs);
9818-
98199814
appendStringInfoString(buf," COLUMNS ");
9820-
foreach(l1,tf->colnames)
9815+
forfive(l1,tf->colnames,l2,tf->coltypes,l3,tf->coltypmods,
9816+
l4,tf->colexprs,l5,tf->coldefexprs)
98219817
{
98229818
char*colname=strVal(lfirst(l1));
9823-
Oidtypid;
9824-
int32typmod;
9825-
Node*colexpr;
9826-
Node*coldefexpr;
9827-
boolordinality=tf->ordinalitycol==colnum;
9819+
Oidtypid=lfirst_oid(l2);
9820+
int32typmod=lfirst_int(l3);
9821+
Node*colexpr= (Node*)lfirst(l4);
9822+
Node*coldefexpr= (Node*)lfirst(l5);
9823+
boolordinality=(tf->ordinalitycol==colnum);
98289824
boolnotnull=bms_is_member(colnum,tf->notnulls);
98299825

9830-
typid=lfirst_oid(l2);
9831-
l2=lnext(l2);
9832-
typmod=lfirst_int(l3);
9833-
l3=lnext(l3);
9834-
colexpr= (Node*)lfirst(l4);
9835-
l4=lnext(l4);
9836-
coldefexpr= (Node*)lfirst(l5);
9837-
l5=lnext(l5);
9838-
98399826
if (colnum>0)
98409827
appendStringInfoString(buf,", ");
98419828
colnum++;
@@ -10349,12 +10336,11 @@ get_from_clause_coldeflist(RangeTblFunction *rtfunc,
1034910336

1035010337
appendStringInfoChar(buf,'(');
1035110338

10352-
/* there's no forfour(), so must chase one list the hard way */
1035310339
i=0;
10354-
l4=list_head(rtfunc->funccolnames);
10355-
forthree(l1,rtfunc->funccoltypes,
10356-
l2,rtfunc->funccoltypmods,
10357-
l3,rtfunc->funccolcollations)
10340+
forfour(l1,rtfunc->funccoltypes,
10341+
l2,rtfunc->funccoltypmods,
10342+
l3,rtfunc->funccolcollations,
10343+
l4,rtfunc->funccolnames)
1035810344
{
1035910345
Oidatttypid=lfirst_oid(l1);
1036010346
int32atttypmod=lfirst_int(l2);
@@ -10378,7 +10364,6 @@ get_from_clause_coldeflist(RangeTblFunction *rtfunc,
1037810364
appendStringInfo(buf," COLLATE %s",
1037910365
generate_collation_name(attcollation));
1038010366

10381-
l4=lnext(l4);
1038210367
i++;
1038310368
}
1038410369

‎src/include/nodes/pg_list.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,32 @@ list_length(const List *l)
205205
(cell1) != NULL && (cell2) != NULL && (cell3) != NULL;\
206206
(cell1) = lnext(cell1), (cell2) = lnext(cell2), (cell3) = lnext(cell3))
207207

208+
/*
209+
* forfour -
210+
* the same for four lists
211+
*/
212+
#defineforfour(cell1,list1,cell2,list2,cell3,list3,cell4,list4) \
213+
for ((cell1) = list_head(list1), (cell2) = list_head(list2), \
214+
(cell3) = list_head(list3), (cell4) = list_head(list4); \
215+
(cell1) != NULL && (cell2) != NULL && \
216+
(cell3) != NULL && (cell4) != NULL; \
217+
(cell1) = lnext(cell1), (cell2) = lnext(cell2), \
218+
(cell3) = lnext(cell3), (cell4) = lnext(cell4))
219+
220+
/*
221+
* forfive -
222+
* the same for five lists
223+
*/
224+
#defineforfive(cell1,list1,cell2,list2,cell3,list3,cell4,list4,cell5,list5) \
225+
for ((cell1) = list_head(list1), (cell2) = list_head(list2), \
226+
(cell3) = list_head(list3), (cell4) = list_head(list4), \
227+
(cell5) = list_head(list5); \
228+
(cell1) != NULL && (cell2) != NULL && (cell3) != NULL && \
229+
(cell4) != NULL && (cell5) != NULL; \
230+
(cell1) = lnext(cell1), (cell2) = lnext(cell2), \
231+
(cell3) = lnext(cell3), (cell4) = lnext(cell4), \
232+
(cell5) = lnext(cell5))
233+
208234
externList*lappend(List*list,void*datum);
209235
externList*lappend_int(List*list,intdatum);
210236
externList*lappend_oid(List*list,Oiddatum);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp