diff --git a/src/backend/distributed/deparser/deparse_statistics_stmts.c b/src/backend/distributed/deparser/deparse_statistics_stmts.c index 558c242df..e0f1ecaef 100644 --- a/src/backend/distributed/deparser/deparse_statistics_stmts.c +++ b/src/backend/distributed/deparser/deparse_statistics_stmts.c @@ -220,14 +220,15 @@ AppendStatTypes(StringInfo buf, CreateStatsStmt *stmt) appendStringInfoString(buf, " ("); Value *statType = NULL; - foreach_ptr(statType, stmt->stat_types) + int statTypeIndex = 0; + foreach_ptr_with_index(statType, stmt->stat_types, statTypeIndex) { - appendStringInfoString(buf, strVal(statType)); - - if (statType != llast(stmt->stat_types)) + if (statTypeIndex != 0) { appendStringInfoString(buf, ", "); } + + appendStringInfoString(buf, strVal(statType)); } appendStringInfoString(buf, ")"); @@ -240,7 +241,8 @@ AppendColumnNames(StringInfo buf, CreateStatsStmt *stmt) { StatsElem *column = NULL; - foreach_ptr(column, stmt->exprs) + int columnIndex = 0; + foreach_ptr_with_index(column, stmt->exprs, columnIndex) { if (!column->name) { @@ -250,14 +252,13 @@ AppendColumnNames(StringInfo buf, CreateStatsStmt *stmt) "only simple column references are allowed in CREATE STATISTICS"))); } - const char *columnName = quote_identifier(column->name); - - appendStringInfoString(buf, columnName); - - if (column != llast(stmt->exprs)) + if (columnIndex != 0) { appendStringInfoString(buf, ", "); } + + const char *columnName = quote_identifier(column->name); + appendStringInfoString(buf, columnName); } } diff --git a/src/backend/distributed/executor/distributed_intermediate_results.c b/src/backend/distributed/executor/distributed_intermediate_results.c index 8a29e633d..bce8f8b91 100644 --- a/src/backend/distributed/executor/distributed_intermediate_results.c +++ b/src/backend/distributed/executor/distributed_intermediate_results.c @@ -637,26 +637,24 @@ QueryStringForFragmentsTransfer(NodeToNodeFragmentsTransfer *fragmentsTransfer) { StringInfo queryString = makeStringInfo(); StringInfo fragmentNamesArrayString = makeStringInfo(); - int fragmentCount = 0; NodePair *nodePair = &fragmentsTransfer->nodes; WorkerNode *sourceNode = LookupNodeByNodeIdOrError(nodePair->sourceNodeId); appendStringInfoString(fragmentNamesArrayString, "ARRAY["); DistributedResultFragment *fragment = NULL; - foreach_ptr(fragment, fragmentsTransfer->fragmentList) + int fragmentIndex = 0; + foreach_ptr_with_index(fragment, fragmentsTransfer->fragmentList, fragmentIndex) { const char *fragmentName = fragment->resultId; - if (fragmentCount > 0) + if (fragmentIndex != 0) { appendStringInfoString(fragmentNamesArrayString, ","); } appendStringInfoString(fragmentNamesArrayString, quote_literal_cstr(fragmentName)); - - fragmentCount++; } appendStringInfoString(fragmentNamesArrayString, "]::text[]"); diff --git a/src/backend/distributed/executor/insert_select_executor.c b/src/backend/distributed/executor/insert_select_executor.c index 338b03075..116c851a5 100644 --- a/src/backend/distributed/executor/insert_select_executor.c +++ b/src/backend/distributed/executor/insert_select_executor.c @@ -580,20 +580,18 @@ static int PartitionColumnIndexFromColumnList(Oid relationId, List *columnNameList) { Var *partitionColumn = PartitionColumn(relationId, 0); - int partitionColumnIndex = 0; const char *columnName = NULL; - foreach_ptr(columnName, columnNameList) + int columnNameIndex = 0; + foreach_ptr_with_index(columnName, columnNameList, columnNameIndex) { AttrNumber attrNumber = get_attnum(relationId, columnName); /* check whether this is the partition column */ if (partitionColumn != NULL && attrNumber == partitionColumn->varattno) { - return partitionColumnIndex; + return columnNameIndex; } - - partitionColumnIndex++; } return -1; @@ -727,15 +725,13 @@ static int PartitionColumnIndex(List *insertTargetList, Var *partitionColumn) { TargetEntry *insertTargetEntry = NULL; - int targetEntryIndex = 0; - foreach_ptr(insertTargetEntry, insertTargetList) + int insertTargetEntryIndex = 0; + foreach_ptr_with_index(insertTargetEntry, insertTargetList, insertTargetEntryIndex) { if (insertTargetEntry->resno == partitionColumn->varattno) { - return targetEntryIndex; + return insertTargetEntryIndex; } - - targetEntryIndex++; } return -1; @@ -803,11 +799,11 @@ static void WrapTaskListForProjection(List *taskList, List *projectedTargetEntries) { StringInfo projectedColumnsString = makeStringInfo(); - int entryIndex = 0; TargetEntry *targetEntry = NULL; - foreach_ptr(targetEntry, projectedTargetEntries) + int targetEntryIndex = 0; + foreach_ptr_with_index(targetEntry, projectedTargetEntries, targetEntryIndex) { - if (entryIndex != 0) + if (targetEntryIndex != 0) { appendStringInfoChar(projectedColumnsString, ','); } @@ -815,8 +811,6 @@ WrapTaskListForProjection(List *taskList, List *projectedTargetEntries) char *columnName = targetEntry->resname; Assert(columnName != NULL); appendStringInfoString(projectedColumnsString, quote_identifier(columnName)); - - entryIndex++; } Task *task = NULL; diff --git a/src/backend/distributed/executor/multi_executor.c b/src/backend/distributed/executor/multi_executor.c index 5c36d5bba..12672bf56 100644 --- a/src/backend/distributed/executor/multi_executor.c +++ b/src/backend/distributed/executor/multi_executor.c @@ -460,14 +460,13 @@ SortTupleStore(CitusScanState *scanState) Oid *collations = (Oid *) palloc(numberOfSortKeys * sizeof(Oid)); bool *nullsFirst = (bool *) palloc(numberOfSortKeys * sizeof(bool)); - int sortKeyIndex = 0; - /* * Iterate on the returning target list and generate the necessary information * for sorting the tuples. */ TargetEntry *returningEntry = NULL; - foreach_ptr(returningEntry, targetList) + int returningEntryIndex = 0; + foreach_ptr_with_index(returningEntry, targetList, returningEntryIndex) { Oid sortop = InvalidOid; @@ -477,12 +476,10 @@ SortTupleStore(CitusScanState *scanState) &sortop, NULL, NULL, NULL); - sortColIdx[sortKeyIndex] = sortKeyIndex + 1; - sortOperators[sortKeyIndex] = sortop; - collations[sortKeyIndex] = exprCollation((Node *) returningEntry->expr); - nullsFirst[sortKeyIndex] = false; - - sortKeyIndex++; + sortColIdx[returningEntryIndex] = returningEntryIndex + 1; + sortOperators[returningEntryIndex] = sortop; + collations[returningEntryIndex] = exprCollation((Node *) returningEntry->expr); + nullsFirst[returningEntryIndex] = false; } Tuplesortstate *tuplesortstate = diff --git a/src/backend/distributed/metadata/metadata_cache.c b/src/backend/distributed/metadata/metadata_cache.c index 789601d44..c5f8fdf12 100644 --- a/src/backend/distributed/metadata/metadata_cache.c +++ b/src/backend/distributed/metadata/metadata_cache.c @@ -1477,7 +1477,6 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry) { Relation distShardRelation = table_open(DistShardRelationId(), AccessShareLock); TupleDesc distShardTupleDesc = RelationGetDescr(distShardRelation); - int arrayIndex = 0; shardIntervalArray = MemoryContextAllocZero(MetadataCacheMemoryContext, shardIntervalArrayLength * @@ -1493,7 +1492,8 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry) sizeof(int)); HeapTuple shardTuple = NULL; - foreach_ptr(shardTuple, distShardTupleList) + int shardTupleIndex = 0; + foreach_ptr_with_index(shardTuple, distShardTupleList, shardTupleIndex) { ShardInterval *shardInterval = TupleToShardInterval(shardTuple, distShardTupleDesc, @@ -1501,13 +1501,11 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry) intervalTypeMod); MemoryContext oldContext = MemoryContextSwitchTo(MetadataCacheMemoryContext); - shardIntervalArray[arrayIndex] = CopyShardInterval(shardInterval); + shardIntervalArray[shardTupleIndex] = CopyShardInterval(shardInterval); MemoryContextSwitchTo(oldContext); heap_freetuple(shardTuple); - - arrayIndex++; } table_close(distShardRelation, AccessShareLock); @@ -1603,7 +1601,6 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry) { ShardInterval *shardInterval = sortedShardIntervalArray[shardIndex]; int64 shardId = shardInterval->shardId; - int placementOffset = 0; /* * Enable quick lookups of this shard ID by adding it to ShardIdCacheHash @@ -1631,10 +1628,10 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry) GroupShardPlacement *placementArray = palloc0(numberOfPlacements * sizeof(GroupShardPlacement)); GroupShardPlacement *srcPlacement = NULL; - foreach_ptr(srcPlacement, placementList) + int srcPlacementIndex = 0; + foreach_ptr_with_index(srcPlacement, placementList, srcPlacementIndex) { - placementArray[placementOffset] = *srcPlacement; - placementOffset++; + placementArray[srcPlacementIndex] = *srcPlacement; } MemoryContextSwitchTo(oldContext); diff --git a/src/backend/distributed/metadata/metadata_sync.c b/src/backend/distributed/metadata/metadata_sync.c index 95a061510..ff465326a 100644 --- a/src/backend/distributed/metadata/metadata_sync.c +++ b/src/backend/distributed/metadata/metadata_sync.c @@ -754,7 +754,6 @@ NodeListInsertCommand(List *workerNodeList) { StringInfo nodeListInsertCommand = makeStringInfo(); int workerCount = list_length(workerNodeList); - int processedWorkerNodeCount = 0; Oid primaryRole = PrimaryNodeRoleId(); /* if there are no workers, return NULL */ @@ -779,7 +778,8 @@ NodeListInsertCommand(List *workerNodeList) /* iterate over the worker nodes, add the values */ WorkerNode *workerNode = NULL; - foreach_ptr(workerNode, workerNodeList) + int workerNodeIndex = 0; + foreach_ptr_with_index(workerNode, workerNodeList, workerNodeIndex) { char *hasMetadataString = workerNode->hasMetadata ? "TRUE" : "FALSE"; char *metadataSyncedString = workerNode->metadataSynced ? "TRUE" : "FALSE"; @@ -790,6 +790,11 @@ NodeListInsertCommand(List *workerNodeList) Datum nodeRoleStringDatum = DirectFunctionCall1(enum_out, nodeRoleOidDatum); char *nodeRoleString = DatumGetCString(nodeRoleStringDatum); + if (workerNodeIndex != 0) + { + appendStringInfo(nodeListInsertCommand, ","); + } + appendStringInfo(nodeListInsertCommand, "(%d, %d, %s, %d, %s, %s, %s, %s, '%s'::noderole, %s, %s)", workerNode->nodeId, @@ -803,12 +808,6 @@ NodeListInsertCommand(List *workerNodeList) nodeRoleString, quote_literal_cstr(workerNode->nodeCluster), shouldHaveShards); - - processedWorkerNodeCount++; - if (processedWorkerNodeCount != workerCount) - { - appendStringInfo(nodeListInsertCommand, ","); - } } return nodeListInsertCommand->data; @@ -963,7 +962,8 @@ ShardListInsertCommand(List *shardIntervalList) "WITH shard_data(relationname, shardid, storagetype, " "shardminvalue, shardmaxvalue) AS (VALUES "); - foreach_ptr(shardInterval, shardIntervalList) + int shardIntervalIndex = 0; + foreach_ptr_with_index(shardInterval, shardIntervalList, shardIntervalIndex) { uint64 shardId = shardInterval->shardId; Oid distributedRelationId = shardInterval->relationId; @@ -992,6 +992,11 @@ ShardListInsertCommand(List *shardIntervalList) appendStringInfo(maxHashToken, "NULL"); } + if (shardIntervalIndex != 0) + { + appendStringInfo(insertShardCommand, ", "); + } + appendStringInfo(insertShardCommand, "(%s::regclass, %ld, '%c'::\"char\", %s, %s)", quote_literal_cstr(qualifiedRelationName), @@ -999,11 +1004,6 @@ ShardListInsertCommand(List *shardIntervalList) shardInterval->storageType, minHashToken->data, maxHashToken->data); - - if (llast(shardIntervalList) != shardInterval) - { - appendStringInfo(insertShardCommand, ", "); - } } appendStringInfo(insertShardCommand, ") "); diff --git a/src/backend/distributed/operations/shard_rebalancer.c b/src/backend/distributed/operations/shard_rebalancer.c index 773c9e8b6..7b56ce721 100644 --- a/src/backend/distributed/operations/shard_rebalancer.c +++ b/src/backend/distributed/operations/shard_rebalancer.c @@ -280,12 +280,12 @@ CheckRebalanceStateInvariants(const RebalanceState *state) { NodeFillState *fillState = NULL; NodeFillState *prevFillState = NULL; - int fillStateIndex = 0; int fillStateLength = list_length(state->fillStateListAsc); Assert(state != NULL); Assert(list_length(state->fillStateListAsc) == list_length(state->fillStateListDesc)); - foreach_ptr(fillState, state->fillStateListAsc) + int fillStateIndex = 0; + foreach_ptr_with_index(fillState, state->fillStateListAsc, fillStateIndex) { float4 totalCost = 0; ShardCost *shardCost = NULL; @@ -333,7 +333,6 @@ CheckRebalanceStateInvariants(const RebalanceState *state) 1000); prevFillState = fillState; - fillStateIndex++; } } diff --git a/src/backend/distributed/test/distribution_metadata.c b/src/backend/distributed/test/distribution_metadata.c index 97809c02f..3c5abd215 100644 --- a/src/backend/distributed/test/distribution_metadata.c +++ b/src/backend/distributed/test/distribution_metadata.c @@ -62,7 +62,6 @@ Datum load_shard_id_array(PG_FUNCTION_ARGS) { Oid distributedTableId = PG_GETARG_OID(0); - int shardIdIndex = 0; Oid shardIdTypeId = INT8OID; List *shardList = LoadShardIntervalList(distributedTableId); @@ -71,12 +70,12 @@ load_shard_id_array(PG_FUNCTION_ARGS) Datum *shardIdDatumArray = palloc0(shardIdCount * sizeof(Datum)); ShardInterval *shardInterval = NULL; - foreach_ptr(shardInterval, shardList) + int shardIntervalIndex = 0; + foreach_ptr_with_index(shardInterval, shardList, shardIntervalIndex) { Datum shardIdDatum = Int64GetDatum(shardInterval->shardId); - shardIdDatumArray[shardIdIndex] = shardIdDatum; - shardIdIndex++; + shardIdDatumArray[shardIntervalIndex] = shardIdDatum; } ArrayType *shardIdArrayType = DatumArrayToArrayType(shardIdDatumArray, shardIdCount, @@ -122,7 +121,6 @@ load_shard_placement_array(PG_FUNCTION_ARGS) int64 shardId = PG_GETARG_INT64(0); bool onlyActive = PG_GETARG_BOOL(1); List *placementList = NIL; - int placementIndex = 0; Oid placementTypeId = TEXTOID; StringInfo placementInfo = makeStringInfo(); @@ -141,13 +139,13 @@ load_shard_placement_array(PG_FUNCTION_ARGS) Datum *placementDatumArray = palloc0(placementCount * sizeof(Datum)); ShardPlacement *placement = NULL; - foreach_ptr(placement, placementList) + int placementIndex = 0; + foreach_ptr_with_index(placement, placementList, placementIndex) { appendStringInfo(placementInfo, "%s:%d", placement->nodeName, placement->nodePort); placementDatumArray[placementIndex] = CStringGetTextDatum(placementInfo->data); - placementIndex++; resetStringInfo(placementInfo); } diff --git a/src/backend/distributed/test/metadata_sync.c b/src/backend/distributed/test/metadata_sync.c index 0c8622a83..5b5e3e3fd 100644 --- a/src/backend/distributed/test/metadata_sync.c +++ b/src/backend/distributed/test/metadata_sync.c @@ -44,7 +44,6 @@ master_metadata_snapshot(PG_FUNCTION_ARGS) List *dropSnapshotCommands = MetadataDropCommands(); List *createSnapshotCommands = MetadataCreateCommands(); List *snapshotCommandList = NIL; - int snapshotCommandIndex = 0; Oid ddlCommandTypeId = TEXTOID; snapshotCommandList = list_concat(snapshotCommandList, dropSnapshotCommands); @@ -54,12 +53,14 @@ master_metadata_snapshot(PG_FUNCTION_ARGS) Datum *snapshotCommandDatumArray = palloc0(snapshotCommandCount * sizeof(Datum)); const char *metadataSnapshotCommand = NULL; - foreach_ptr(metadataSnapshotCommand, snapshotCommandList) + int metadataSnapshotCommandIndex = 0; + foreach_ptr_with_index(metadataSnapshotCommand, snapshotCommandList, + metadataSnapshotCommandIndex) { Datum metadataSnapshotCommandDatum = CStringGetTextDatum(metadataSnapshotCommand); - snapshotCommandDatumArray[snapshotCommandIndex] = metadataSnapshotCommandDatum; - snapshotCommandIndex++; + snapshotCommandDatumArray[metadataSnapshotCommandIndex] = + metadataSnapshotCommandDatum; } ArrayType *snapshotCommandArrayType = DatumArrayToArrayType(snapshotCommandDatumArray, diff --git a/src/backend/distributed/test/prune_shard_list.c b/src/backend/distributed/test/prune_shard_list.c index 98aa8e7ff..db2d4b483 100644 --- a/src/backend/distributed/test/prune_shard_list.c +++ b/src/backend/distributed/test/prune_shard_list.c @@ -207,7 +207,6 @@ MakeTextPartitionExpression(Oid distributedTableId, text *value) static ArrayType * PrunedShardIdsForTable(Oid distributedTableId, List *whereClauseList) { - int shardIdIndex = 0; Oid shardIdTypeId = INT8OID; Index tableId = 1; @@ -218,12 +217,12 @@ PrunedShardIdsForTable(Oid distributedTableId, List *whereClauseList) Datum *shardIdDatumArray = palloc0(shardIdCount * sizeof(Datum)); ShardInterval *shardInterval = NULL; - foreach_ptr(shardInterval, shardList) + int shardIntervalIndex = 0; + foreach_ptr_with_index(shardInterval, shardList, shardIntervalIndex) { Datum shardIdDatum = Int64GetDatum(shardInterval->shardId); - shardIdDatumArray[shardIdIndex] = shardIdDatum; - shardIdIndex++; + shardIdDatumArray[shardIntervalIndex] = shardIdDatum; } ArrayType *shardIdArrayType = DatumArrayToArrayType(shardIdDatumArray, shardIdCount, diff --git a/src/backend/distributed/transaction/distributed_deadlock_detection.c b/src/backend/distributed/transaction/distributed_deadlock_detection.c index f9e4adca0..9706d868c 100644 --- a/src/backend/distributed/transaction/distributed_deadlock_detection.c +++ b/src/backend/distributed/transaction/distributed_deadlock_detection.c @@ -669,9 +669,10 @@ WaitsForToString(List *waitsFor) StringInfo transactionIdStr = makeStringInfo(); TransactionNode *waitingNode = NULL; - foreach_ptr(waitingNode, waitsFor) + int waitingNodeIndex = 0; + foreach_ptr_with_index(waitingNode, waitsFor, waitingNodeIndex) { - if (transactionIdStr->len != 0) + if (waitingNodeIndex != 0) { appendStringInfoString(transactionIdStr, ","); } diff --git a/src/backend/distributed/utils/colocation_utils.c b/src/backend/distributed/utils/colocation_utils.c index 4edc9e424..a0f6951d2 100644 --- a/src/backend/distributed/utils/colocation_utils.c +++ b/src/backend/distributed/utils/colocation_utils.c @@ -182,17 +182,17 @@ get_colocated_shard_array(PG_FUNCTION_ARGS) int colocatedShardCount = list_length(colocatedShardList); Datum *colocatedShardsDatumArray = palloc0(colocatedShardCount * sizeof(Datum)); Oid arrayTypeId = OIDOID; - int colocatedShardIndex = 0; ShardInterval *colocatedShardInterval = NULL; - foreach_ptr(colocatedShardInterval, colocatedShardList) + int colocatedShardIntervalIndex = 0; + foreach_ptr_with_index(colocatedShardInterval, colocatedShardList, + colocatedShardIntervalIndex) { uint64 colocatedShardId = colocatedShardInterval->shardId; Datum colocatedShardDatum = Int64GetDatum(colocatedShardId); - colocatedShardsDatumArray[colocatedShardIndex] = colocatedShardDatum; - colocatedShardIndex++; + colocatedShardsDatumArray[colocatedShardIntervalIndex] = colocatedShardDatum; } ArrayType *colocatedShardsArrayType = DatumArrayToArrayType(colocatedShardsDatumArray, diff --git a/src/backend/distributed/utils/listutils.c b/src/backend/distributed/utils/listutils.c index 836a4bff6..8620b63af 100644 --- a/src/backend/distributed/utils/listutils.c +++ b/src/backend/distributed/utils/listutils.c @@ -41,11 +41,10 @@ SortList(List *pointerList, int (*comparisonFunction)(const void *, const void * void **array = (void **) palloc0(arraySize * sizeof(void *)); void *pointer = NULL; - foreach_ptr(pointer, pointerList) + int pointerIndex = 0; + foreach_ptr_with_index(pointer, pointerList, pointerIndex) { - array[arrayIndex] = pointer; - - arrayIndex++; + array[pointerIndex] = pointer; } /* sort the array of pointers using the comparison function */ @@ -77,13 +76,12 @@ PointerArrayFromList(List *pointerList) { int pointerCount = list_length(pointerList); void **pointerArray = (void **) palloc0(pointerCount * sizeof(void *)); - int pointerIndex = 0; void *pointer = NULL; - foreach_ptr(pointer, pointerList) + int pointerIndex = 0; + foreach_ptr_with_index(pointer, pointerList, pointerIndex) { pointerArray[pointerIndex] = pointer; - pointerIndex += 1; } return pointerArray; @@ -188,15 +186,14 @@ StringJoin(List *stringList, char delimiter) StringInfo joinedString = makeStringInfo(); const char *command = NULL; - int curIndex = 0; - foreach_ptr(command, stringList) + int commandIndex = 0; + foreach_ptr_with_index(command, stringList, commandIndex) { - if (curIndex > 0) + if (commandIndex != 0) { appendStringInfoChar(joinedString, delimiter); } appendStringInfoString(joinedString, command); - curIndex++; } return joinedString->data; @@ -212,14 +209,12 @@ List * ListTake(List *pointerList, int size) { List *result = NIL; - int listIndex = 0; void *pointer = NULL; foreach_ptr(pointer, pointerList) { result = lappend(result, pointer); - listIndex++; - if (listIndex >= size) + if (list_length(result) >= size) { break; } diff --git a/src/backend/distributed/utils/resource_lock.c b/src/backend/distributed/utils/resource_lock.c index 885ce39ac..1ad3dd908 100644 --- a/src/backend/distributed/utils/resource_lock.c +++ b/src/backend/distributed/utils/resource_lock.c @@ -216,8 +216,6 @@ static void LockShardListResourcesOnFirstWorker(LOCKMODE lockmode, List *shardIntervalList) { StringInfo lockCommand = makeStringInfo(); - int processedShardIntervalCount = 0; - int totalShardIntervalCount = list_length(shardIntervalList); WorkerNode *firstWorkerNode = GetFirstPrimaryWorkerNode(); int connectionFlags = 0; const char *superuser = CurrentUserName(); @@ -225,17 +223,16 @@ LockShardListResourcesOnFirstWorker(LOCKMODE lockmode, List *shardIntervalList) appendStringInfo(lockCommand, "SELECT lock_shard_resources(%d, ARRAY[", lockmode); ShardInterval *shardInterval = NULL; - foreach_ptr(shardInterval, shardIntervalList) + int shardIntervalIndex = 0; + foreach_ptr_with_index(shardInterval, shardIntervalList, shardIntervalIndex) { - int64 shardId = shardInterval->shardId; - - appendStringInfo(lockCommand, "%lu", shardId); - - processedShardIntervalCount++; - if (processedShardIntervalCount != totalShardIntervalCount) + if (shardIntervalIndex != 0) { appendStringInfo(lockCommand, ", "); } + + int64 shardId = shardInterval->shardId; + appendStringInfo(lockCommand, "%lu", shardId); } appendStringInfo(lockCommand, "])"); @@ -303,8 +300,6 @@ void LockShardListMetadataOnWorkers(LOCKMODE lockmode, List *shardIntervalList) { StringInfo lockCommand = makeStringInfo(); - int processedShardIntervalCount = 0; - int totalShardIntervalCount = list_length(shardIntervalList); if (list_length(shardIntervalList) == 0) { @@ -314,17 +309,16 @@ LockShardListMetadataOnWorkers(LOCKMODE lockmode, List *shardIntervalList) appendStringInfo(lockCommand, "SELECT lock_shard_metadata(%d, ARRAY[", lockmode); ShardInterval *shardInterval = NULL; - foreach_ptr(shardInterval, shardIntervalList) + int shardIntervalIndex = 0; + foreach_ptr_with_index(shardInterval, shardIntervalList, shardIntervalIndex) { - int64 shardId = shardInterval->shardId; - - appendStringInfo(lockCommand, "%lu", shardId); - - processedShardIntervalCount++; - if (processedShardIntervalCount != totalShardIntervalCount) + if (shardIntervalIndex != 0) { appendStringInfo(lockCommand, ", "); } + + int64 shardId = shardInterval->shardId; + appendStringInfo(lockCommand, "%lu", shardId); } appendStringInfo(lockCommand, "])"); diff --git a/src/include/distributed/listutils.h b/src/include/distributed/listutils.h index 3a177f7d9..e2032e089 100644 --- a/src/include/distributed/listutils.h +++ b/src/include/distributed/listutils.h @@ -33,6 +33,13 @@ typedef struct ListCellAndListWrapper ListCell *listCell; } ListCellAndListWrapper; + +typedef struct ListCellAndIndex +{ + ListCell *listCell; + int index; +} ListCellAndIndex; + /* * foreach_ptr - * a convenience macro which loops through a pointer list without needing a @@ -52,9 +59,17 @@ typedef struct ListCellAndListWrapper for (ListCell *(var ## CellDoNotUse) = list_head(l); \ (var ## CellDoNotUse) != NULL && \ (((var) = lfirst(var ## CellDoNotUse)) || true); \ - var ## CellDoNotUse = lnext_compat(l, var ## CellDoNotUse)) + (var ## CellDoNotUse) = lnext_compat(l, var ## CellDoNotUse)) +#define foreach_ptr_with_index(var, l, index) \ + (index) = 0; \ + for (ListCell *(var ## CellDoNotUse) = list_head(l); \ + (var ## CellDoNotUse) != NULL && \ + (((var) = lfirst(var ## CellDoNotUse)) || true); \ + (var ## CellDoNotUse) = lnext_compat(l, var ## CellDoNotUse), \ + (index)++) + /* * foreach_int - * a convenience macro which loops through an int list without needing a @@ -113,6 +128,7 @@ typedef struct ListCellAndListWrapper #define foreach_ptr_append(var, l) foreach_ptr(var, l) #endif + /* utility functions declaration shared within this module */ extern List * SortList(List *pointerList, int (*ComparisonFunction)(const void *, const void *));