1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-05 07:21:24 +03:00

Improve castNode notation by introducing list-extraction-specific variants.

This extends the castNode() notation introduced by commit 5bcab1114 to
provide, in one step, extraction of a list cell's pointer and coercion to
a concrete node type.  For example, "lfirst_node(Foo, lc)" is the same
as "castNode(Foo, lfirst(lc))".  Almost half of the uses of castNode
that have appeared so far include a list extraction call, so this is
pretty widely useful, and it saves a few more keystrokes compared to the
old way.

As with the previous patch, back-patch the addition of these macros to
pg_list.h, so that the notation will be available when back-patching.

Patch by me, after an idea of Andrew Gierth's.

Discussion: https://postgr.es/m/14197.1491841216@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2017-04-10 13:51:29 -04:00
parent 56dd8e85c4
commit 8f0530f580
65 changed files with 176 additions and 168 deletions

View File

@ -322,7 +322,7 @@ ExecBuildProjectionInfo(List *targetList,
/* Now compile each tlist column */
foreach(lc, targetList)
{
TargetEntry *tle = castNode(TargetEntry, lfirst(lc));
TargetEntry *tle = lfirst_node(TargetEntry, lc);
Var *variable = NULL;
AttrNumber attnum = 0;
bool isSafeVar = false;

View File

@ -160,7 +160,7 @@ ExecResetTupleTable(List *tupleTable, /* tuple table */
foreach(lc, tupleTable)
{
TupleTableSlot *slot = castNode(TupleTableSlot, lfirst(lc));
TupleTableSlot *slot = lfirst_node(TupleTableSlot, lc);
/* Always release resources and reset the slot to empty */
ExecClearTuple(slot);

View File

@ -978,7 +978,7 @@ ExecCleanTargetListLength(List *targetlist)
foreach(tl, targetlist)
{
TargetEntry *curTle = castNode(TargetEntry, lfirst(tl));
TargetEntry *curTle = lfirst_node(TargetEntry, tl);
if (!curTle->resjunk)
len++;

View File

@ -479,14 +479,14 @@ init_execution_state(List *queryTree_list,
foreach(lc1, queryTree_list)
{
List *qtlist = castNode(List, lfirst(lc1));
List *qtlist = lfirst_node(List, lc1);
execution_state *firstes = NULL;
execution_state *preves = NULL;
ListCell *lc2;
foreach(lc2, qtlist)
{
Query *queryTree = castNode(Query, lfirst(lc2));
Query *queryTree = lfirst_node(Query, lc2);
PlannedStmt *stmt;
execution_state *newes;
@ -707,7 +707,7 @@ init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
flat_query_list = NIL;
foreach(lc, raw_parsetree_list)
{
RawStmt *parsetree = castNode(RawStmt, lfirst(lc));
RawStmt *parsetree = lfirst_node(RawStmt, lc);
List *queryTree_sublist;
queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
@ -1555,7 +1555,7 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
parse = NULL;
foreach(lc, queryTreeList)
{
Query *q = castNode(Query, lfirst(lc));
Query *q = lfirst_node(Query, lc);
if (q->canSetTag)
parse = q;

View File

@ -2866,7 +2866,7 @@ ExecInitAgg(Agg *node, EState *estate, int eflags)
if (phaseidx > 0)
{
aggnode = castNode(Agg, list_nth(node->chain, phaseidx - 1));
aggnode = list_nth_node(Agg, node->chain, phaseidx - 1);
sortnode = castNode(Sort, aggnode->plan.lefttree);
}
else
@ -3360,7 +3360,7 @@ ExecInitAgg(Agg *node, EState *estate, int eflags)
*/
foreach(arg, pertrans->aggref->args)
{
TargetEntry *source_tle = castNode(TargetEntry, lfirst(arg));
TargetEntry *source_tle = lfirst_node(TargetEntry, arg);
TargetEntry *tle;
tle = flatCopyTargetEntry(source_tle);

View File

@ -517,7 +517,7 @@ ExecInitHashJoin(HashJoin *node, EState *estate, int eflags)
hoperators = NIL;
foreach(l, node->hashclauses)
{
OpExpr *hclause = castNode(OpExpr, lfirst(l));
OpExpr *hclause = lfirst_node(OpExpr, l);
lclauses = lappend(lclauses, ExecInitExpr(linitial(hclause->args),
(PlanState *) hjstate));

View File

@ -401,7 +401,7 @@ ExecInitLockRows(LockRows *node, EState *estate, int eflags)
epq_arowmarks = NIL;
foreach(lc, node->rowMarks)
{
PlanRowMark *rc = castNode(PlanRowMark, lfirst(lc));
PlanRowMark *rc = lfirst_node(PlanRowMark, lc);
ExecRowMark *erm;
ExecAuxRowMark *aerm;

View File

@ -1968,7 +1968,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
*/
foreach(l, node->rowMarks)
{
PlanRowMark *rc = castNode(PlanRowMark, lfirst(l));
PlanRowMark *rc = lfirst_node(PlanRowMark, l);
ExecRowMark *erm;
/* ignore "parent" rowmarks; they are irrelevant at runtime */

View File

@ -817,7 +817,7 @@ ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
i = 1;
foreach(l, oplist)
{
OpExpr *opexpr = castNode(OpExpr, lfirst(l));
OpExpr *opexpr = lfirst_node(OpExpr, l);
Expr *expr;
TargetEntry *tle;
Oid rhs_eq_oper;
@ -1148,7 +1148,7 @@ ExecInitAlternativeSubPlan(AlternativeSubPlan *asplan, PlanState *parent)
*/
foreach(lc, asplan->subplans)
{
SubPlan *sp = castNode(SubPlan, lfirst(lc));
SubPlan *sp = lfirst_node(SubPlan, lc);
SubPlanState *sps = ExecInitSubPlan(sp, parent);
asstate->subplans = lappend(asstate->subplans, sps);
@ -1197,8 +1197,8 @@ ExecAlternativeSubPlan(AlternativeSubPlanState *node,
bool *isNull)
{
/* Just pass control to the active subplan */
SubPlanState *activesp = castNode(SubPlanState,
list_nth(node->subplans, node->active));
SubPlanState *activesp = list_nth_node(SubPlanState,
node->subplans, node->active);
return ExecSubPlan(activesp, econtext, isNull);
}

View File

@ -1233,9 +1233,9 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
if (!(portal->cursorOptions & (CURSOR_OPT_SCROLL | CURSOR_OPT_NO_SCROLL)))
{
if (list_length(stmt_list) == 1 &&
castNode(PlannedStmt, linitial(stmt_list))->commandType != CMD_UTILITY &&
castNode(PlannedStmt, linitial(stmt_list))->rowMarks == NIL &&
ExecSupportsBackwardScan(castNode(PlannedStmt, linitial(stmt_list))->planTree))
linitial_node(PlannedStmt, stmt_list)->commandType != CMD_UTILITY &&
linitial_node(PlannedStmt, stmt_list)->rowMarks == NIL &&
ExecSupportsBackwardScan(linitial_node(PlannedStmt, stmt_list)->planTree))
portal->cursorOptions |= CURSOR_OPT_SCROLL;
else
portal->cursorOptions |= CURSOR_OPT_NO_SCROLL;
@ -1249,8 +1249,8 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
if (list_length(stmt_list) == 1 &&
castNode(PlannedStmt, linitial(stmt_list))->commandType != CMD_UTILITY &&
castNode(PlannedStmt, linitial(stmt_list))->rowMarks != NIL)
linitial_node(PlannedStmt, stmt_list)->commandType != CMD_UTILITY &&
linitial_node(PlannedStmt, stmt_list)->rowMarks != NIL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
@ -1274,7 +1274,7 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
foreach(lc, stmt_list)
{
PlannedStmt *pstmt = castNode(PlannedStmt, lfirst(lc));
PlannedStmt *pstmt = lfirst_node(PlannedStmt, lc);
if (!CommandIsReadOnly(pstmt))
{
@ -1770,7 +1770,7 @@ _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
foreach(list_item, raw_parsetree_list)
{
RawStmt *parsetree = castNode(RawStmt, lfirst(list_item));
RawStmt *parsetree = lfirst_node(RawStmt, list_item);
List *stmt_list;
CachedPlanSource *plansource;
@ -1874,7 +1874,7 @@ _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
foreach(list_item, raw_parsetree_list)
{
RawStmt *parsetree = castNode(RawStmt, lfirst(list_item));
RawStmt *parsetree = lfirst_node(RawStmt, list_item);
CachedPlanSource *plansource;
plansource = CreateOneShotCachedPlan(parsetree,
@ -2035,7 +2035,7 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
foreach(lc2, stmt_list)
{
PlannedStmt *stmt = castNode(PlannedStmt, lfirst(lc2));
PlannedStmt *stmt = lfirst_node(PlannedStmt, lc2);
bool canSetTag = stmt->canSetTag;
DestReceiver *dest;