backend/nodes cleanup: Move loop variables definitions into for statement
Hi Hackers,
While working on the other patch [1]/messages/by-id/CAEoWx2m2E0xE8Kvbkv31ULh_E+5zph-WA_bEdv3UR9CLhw+3vg@mail.gmail.com that touched tbm_add_tuples() under
src/backend/nodes, I moved a loop variable into for statement by the way as
I knew Peter Eisentraut had done some efforts about that, see [2]https://git.postgresql.org/cgit/postgresql.git/commit/?id=03fbb0814c5015ab79e670ab97bb6a3349269e4b.
However, Peter removed that change from the patch and he said that should
belong to a separate cleanup patch. So I am following up and moving loop
variables into for statements wherever possible under src/backend/nodes.
[1]: /messages/by-id/CAEoWx2m2E0xE8Kvbkv31ULh_E+5zph-WA_bEdv3UR9CLhw+3vg@mail.gmail.com
/messages/by-id/CAEoWx2m2E0xE8Kvbkv31ULh_E+5zph-WA_bEdv3UR9CLhw+3vg@mail.gmail.com
[2]: https://git.postgresql.org/cgit/postgresql.git/commit/?id=03fbb0814c5015ab79e670ab97bb6a3349269e4b
https://git.postgresql.org/cgit/postgresql.git/commit/?id=03fbb0814c5015ab79e670ab97bb6a3349269e4b
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v1-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchapplication/octet-stream; name=v1-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchDownload
From 460569ce899062d5000ba921e43e70d4fc1bc311 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Wed, 12 Nov 2025 15:53:15 +0800
Subject: [PATCH v1] backend/nodes cleanup: Move loop variables definitions
into for statement
---
src/backend/nodes/bitmapset.c | 9 +++------
src/backend/nodes/nodeFuncs.c | 4 +---
src/backend/nodes/outfuncs.c | 13 +++++--------
src/backend/nodes/params.c | 6 ++----
src/backend/nodes/tidbitmap.c | 16 ++++++----------
5 files changed, 17 insertions(+), 31 deletions(-)
diff --git a/src/backend/nodes/bitmapset.c b/src/backend/nodes/bitmapset.c
index b4ecf0b0390..7b1e9d94103 100644
--- a/src/backend/nodes/bitmapset.c
+++ b/src/backend/nodes/bitmapset.c
@@ -538,7 +538,6 @@ bms_is_member(int x, const Bitmapset *a)
int
bms_member_index(Bitmapset *a, int x)
{
- int i;
int bitnum;
int wordnum;
int result = 0;
@@ -554,7 +553,7 @@ bms_member_index(Bitmapset *a, int x)
bitnum = BITNUM(x);
/* count bits in preceding words */
- for (i = 0; i < wordnum; i++)
+ for (int i = 0; i < wordnum; i++)
{
bitmapword w = a->words[i];
@@ -1306,7 +1305,6 @@ int
bms_next_member(const Bitmapset *a, int prevbit)
{
int nwords;
- int wordnum;
bitmapword mask;
Assert(bms_is_valid_set(a));
@@ -1316,7 +1314,7 @@ bms_next_member(const Bitmapset *a, int prevbit)
nwords = a->nwords;
prevbit++;
mask = (~(bitmapword) 0) << BITNUM(prevbit);
- for (wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
+ for (int wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
{
bitmapword w = a->words[wordnum];
@@ -1366,7 +1364,6 @@ bms_next_member(const Bitmapset *a, int prevbit)
int
bms_prev_member(const Bitmapset *a, int prevbit)
{
- int wordnum;
int ushiftbits;
bitmapword mask;
@@ -1391,7 +1388,7 @@ bms_prev_member(const Bitmapset *a, int prevbit)
ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(prevbit) + 1);
mask = (~(bitmapword) 0) >> ushiftbits;
- for (wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
+ for (int wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
{
bitmapword w = a->words[wordnum];
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index ede838cd40c..d228318dc72 100644
--- a/src/backend/nodes/nodeFuncs.c
+++ b/src/backend/nodes/nodeFuncs.c
@@ -4826,9 +4826,7 @@ planstate_walk_members(PlanState **planstates, int nplans,
planstate_tree_walker_callback walker,
void *context)
{
- int j;
-
- for (j = 0; j < nplans; j++)
+ for (int j = 0; j < nplans; j++)
{
if (PSWALK(planstates[j]))
return true;
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index d9fe21a07e0..1ba590b71d6 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -346,8 +346,7 @@ outBitmapset(StringInfo str, const Bitmapset *bms)
void
outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
- Size length,
- i;
+ Size length;
char *s;
length = datumGetSize(value, typbyval, typlen);
@@ -356,7 +355,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
s = (char *) (&value);
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (int i = 0; i < (Size) sizeof(Datum); i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -368,7 +367,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
else
{
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < length; i++)
+ for (int i = 0; i < length; i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -434,8 +433,6 @@ _outBoolExpr(StringInfo str, const BoolExpr *node)
static void
_outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
{
- int i;
-
WRITE_NODE_TYPE("FOREIGNKEYOPTINFO");
WRITE_UINT_FIELD(con_relid);
@@ -450,10 +447,10 @@ _outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
WRITE_INT_FIELD(nmatched_ri);
/* for compactness, just print the number of matches per column: */
appendStringInfoString(str, " :eclass");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", (node->eclass[i] != NULL));
appendStringInfoString(str, " :rinfos");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", list_length(node->rinfos[i]));
}
diff --git a/src/backend/nodes/params.c b/src/backend/nodes/params.c
index ec5946c5777..aeb8ace2c54 100644
--- a/src/backend/nodes/params.c
+++ b/src/backend/nodes/params.c
@@ -166,13 +166,12 @@ paramlist_param_ref(ParseState *pstate, ParamRef *pref)
Size
EstimateParamListSpace(ParamListInfo paramLI)
{
- int i;
Size sz = sizeof(int);
if (paramLI == NULL || paramLI->numParams <= 0)
return sz;
- for (i = 0; i < paramLI->numParams; i++)
+ for (int i = 0; i < paramLI->numParams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
@@ -229,7 +228,6 @@ void
SerializeParamList(ParamListInfo paramLI, char **start_address)
{
int nparams;
- int i;
/* Write number of parameters. */
if (paramLI == NULL || paramLI->numParams <= 0)
@@ -240,7 +238,7 @@ SerializeParamList(ParamListInfo paramLI, char **start_address)
*start_address += sizeof(int);
/* Write each parameter in turn. */
- for (i = 0; i < nparams; i++)
+ for (int i = 0; i < nparams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 23d97b3a6c8..1f83d0d55f5 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -369,10 +369,9 @@ tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids,
{
BlockNumber currblk = InvalidBlockNumber;
PagetableEntry *page = NULL; /* only valid when currblk is valid */
- int i;
Assert(tbm->iterating == TBM_NOT_ITERATING);
- for (i = 0; i < ntids; i++)
+ for (int i = 0; i < ntids; i++)
{
BlockNumber blk = ItemPointerGetBlockNumber(tids + i);
OffsetNumber off = ItemPointerGetOffsetNumber(tids + i);
@@ -471,12 +470,11 @@ static void
tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
{
PagetableEntry *apage;
- int wordnum;
if (bpage->ischunk)
{
/* Scan b's chunk, mark each indicated page lossy in a */
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = bpage->words[wordnum];
@@ -511,7 +509,7 @@ tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
else
{
/* Both pages are exact, merge at the bit level */
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
apage->words[wordnum] |= bpage->words[wordnum];
apage->recheck |= bpage->recheck;
}
@@ -579,14 +577,13 @@ static bool
tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
const PagetableEntry *bpage;
- int wordnum;
if (apage->ischunk)
{
/* Scan each bit in chunk, try to clear */
bool candelete = true;
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = apage->words[wordnum];
@@ -640,7 +637,7 @@ tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
/* Both pages are exact, merge at the bit level */
Assert(!bpage->ischunk);
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
apage->words[wordnum] &= bpage->words[wordnum];
if (apage->words[wordnum] != 0)
@@ -904,10 +901,9 @@ tbm_extract_page_tuple(TBMIterateResult *iteritem,
uint32 max_offsets)
{
PagetableEntry *page = iteritem->internal_page;
- int wordnum;
int ntuples = 0;
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
bitmapword w = page->words[wordnum];
--
2.39.5 (Apple Git-154)
On Wed, Nov 12, 2025 at 4:00 PM Chao Li <li.evan.chao@gmail.com> wrote:
Hi Hackers,
While working on the other patch [1] that touched tbm_add_tuples() under
src/backend/nodes, I moved a loop variable into for statement by the way as
I knew Peter Eisentraut had done some efforts about that, see [2].However, Peter removed that change from the patch and he said that should
belong to a separate cleanup patch. So I am following up and moving loop
variables into for statements wherever possible under src/backend/nodes.[1]
/messages/by-id/CAEoWx2m2E0xE8Kvbkv31ULh_E+5zph-WA_bEdv3UR9CLhw+3vg@mail.gmail.com
[2]
https://git.postgresql.org/cgit/postgresql.git/commit/?id=03fbb0814c5015ab79e670ab97bb6a3349269e4b
Rebased to V2.
Best regards,
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v2-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchapplication/octet-stream; name=v2-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchDownload
From 70937ae52f12e861a9bfa6e43823779b7d757f1d Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Wed, 12 Nov 2025 15:53:15 +0800
Subject: [PATCH v2] backend/nodes cleanup: Move loop variables definitions
into for statement
---
src/backend/nodes/bitmapset.c | 9 +++------
src/backend/nodes/nodeFuncs.c | 4 +---
src/backend/nodes/outfuncs.c | 13 +++++--------
src/backend/nodes/params.c | 6 ++----
src/backend/nodes/readfuncs.c | 7 +++----
src/backend/nodes/tidbitmap.c | 16 ++++++----------
6 files changed, 20 insertions(+), 35 deletions(-)
diff --git a/src/backend/nodes/bitmapset.c b/src/backend/nodes/bitmapset.c
index b4ecf0b0390..7b1e9d94103 100644
--- a/src/backend/nodes/bitmapset.c
+++ b/src/backend/nodes/bitmapset.c
@@ -538,7 +538,6 @@ bms_is_member(int x, const Bitmapset *a)
int
bms_member_index(Bitmapset *a, int x)
{
- int i;
int bitnum;
int wordnum;
int result = 0;
@@ -554,7 +553,7 @@ bms_member_index(Bitmapset *a, int x)
bitnum = BITNUM(x);
/* count bits in preceding words */
- for (i = 0; i < wordnum; i++)
+ for (int i = 0; i < wordnum; i++)
{
bitmapword w = a->words[i];
@@ -1306,7 +1305,6 @@ int
bms_next_member(const Bitmapset *a, int prevbit)
{
int nwords;
- int wordnum;
bitmapword mask;
Assert(bms_is_valid_set(a));
@@ -1316,7 +1314,7 @@ bms_next_member(const Bitmapset *a, int prevbit)
nwords = a->nwords;
prevbit++;
mask = (~(bitmapword) 0) << BITNUM(prevbit);
- for (wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
+ for (int wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
{
bitmapword w = a->words[wordnum];
@@ -1366,7 +1364,6 @@ bms_next_member(const Bitmapset *a, int prevbit)
int
bms_prev_member(const Bitmapset *a, int prevbit)
{
- int wordnum;
int ushiftbits;
bitmapword mask;
@@ -1391,7 +1388,7 @@ bms_prev_member(const Bitmapset *a, int prevbit)
ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(prevbit) + 1);
mask = (~(bitmapword) 0) >> ushiftbits;
- for (wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
+ for (int wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
{
bitmapword w = a->words[wordnum];
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index ede838cd40c..d228318dc72 100644
--- a/src/backend/nodes/nodeFuncs.c
+++ b/src/backend/nodes/nodeFuncs.c
@@ -4826,9 +4826,7 @@ planstate_walk_members(PlanState **planstates, int nplans,
planstate_tree_walker_callback walker,
void *context)
{
- int j;
-
- for (j = 0; j < nplans; j++)
+ for (int j = 0; j < nplans; j++)
{
if (PSWALK(planstates[j]))
return true;
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index d9fe21a07e0..1ba590b71d6 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -346,8 +346,7 @@ outBitmapset(StringInfo str, const Bitmapset *bms)
void
outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
- Size length,
- i;
+ Size length;
char *s;
length = datumGetSize(value, typbyval, typlen);
@@ -356,7 +355,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
s = (char *) (&value);
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (int i = 0; i < (Size) sizeof(Datum); i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -368,7 +367,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
else
{
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < length; i++)
+ for (int i = 0; i < length; i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -434,8 +433,6 @@ _outBoolExpr(StringInfo str, const BoolExpr *node)
static void
_outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
{
- int i;
-
WRITE_NODE_TYPE("FOREIGNKEYOPTINFO");
WRITE_UINT_FIELD(con_relid);
@@ -450,10 +447,10 @@ _outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
WRITE_INT_FIELD(nmatched_ri);
/* for compactness, just print the number of matches per column: */
appendStringInfoString(str, " :eclass");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", (node->eclass[i] != NULL));
appendStringInfoString(str, " :rinfos");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", list_length(node->rinfos[i]));
}
diff --git a/src/backend/nodes/params.c b/src/backend/nodes/params.c
index ec5946c5777..aeb8ace2c54 100644
--- a/src/backend/nodes/params.c
+++ b/src/backend/nodes/params.c
@@ -166,13 +166,12 @@ paramlist_param_ref(ParseState *pstate, ParamRef *pref)
Size
EstimateParamListSpace(ParamListInfo paramLI)
{
- int i;
Size sz = sizeof(int);
if (paramLI == NULL || paramLI->numParams <= 0)
return sz;
- for (i = 0; i < paramLI->numParams; i++)
+ for (int i = 0; i < paramLI->numParams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
@@ -229,7 +228,6 @@ void
SerializeParamList(ParamListInfo paramLI, char **start_address)
{
int nparams;
- int i;
/* Write number of parameters. */
if (paramLI == NULL || paramLI->numParams <= 0)
@@ -240,7 +238,7 @@ SerializeParamList(ParamListInfo paramLI, char **start_address)
*start_address += sizeof(int);
/* Write each parameter in turn. */
- for (i = 0; i < nparams; i++)
+ for (int i = 0; i < nparams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index 2f933e95cb9..609da808f32 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -599,8 +599,7 @@ parseNodeString(void)
Datum
readDatum(bool typbyval)
{
- Size length,
- i;
+ Size length;
int tokenLength;
const char *token;
Datum res;
@@ -623,7 +622,7 @@ readDatum(bool typbyval)
elog(ERROR, "byval datum but length = %zu", length);
res = (Datum) 0;
s = (char *) (&res);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (int i = 0; i < (Size) sizeof(Datum); i++)
{
token = pg_strtok(&tokenLength);
s[i] = (char) atoi(token);
@@ -634,7 +633,7 @@ readDatum(bool typbyval)
else
{
s = (char *) palloc(length);
- for (i = 0; i < length; i++)
+ for (int i = 0; i < length; i++)
{
token = pg_strtok(&tokenLength);
s[i] = (char) atoi(token);
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 23d97b3a6c8..1f83d0d55f5 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -369,10 +369,9 @@ tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids,
{
BlockNumber currblk = InvalidBlockNumber;
PagetableEntry *page = NULL; /* only valid when currblk is valid */
- int i;
Assert(tbm->iterating == TBM_NOT_ITERATING);
- for (i = 0; i < ntids; i++)
+ for (int i = 0; i < ntids; i++)
{
BlockNumber blk = ItemPointerGetBlockNumber(tids + i);
OffsetNumber off = ItemPointerGetOffsetNumber(tids + i);
@@ -471,12 +470,11 @@ static void
tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
{
PagetableEntry *apage;
- int wordnum;
if (bpage->ischunk)
{
/* Scan b's chunk, mark each indicated page lossy in a */
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = bpage->words[wordnum];
@@ -511,7 +509,7 @@ tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
else
{
/* Both pages are exact, merge at the bit level */
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
apage->words[wordnum] |= bpage->words[wordnum];
apage->recheck |= bpage->recheck;
}
@@ -579,14 +577,13 @@ static bool
tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
const PagetableEntry *bpage;
- int wordnum;
if (apage->ischunk)
{
/* Scan each bit in chunk, try to clear */
bool candelete = true;
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = apage->words[wordnum];
@@ -640,7 +637,7 @@ tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
/* Both pages are exact, merge at the bit level */
Assert(!bpage->ischunk);
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
apage->words[wordnum] &= bpage->words[wordnum];
if (apage->words[wordnum] != 0)
@@ -904,10 +901,9 @@ tbm_extract_page_tuple(TBMIterateResult *iteritem,
uint32 max_offsets)
{
PagetableEntry *page = iteritem->internal_page;
- int wordnum;
int ntuples = 0;
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
bitmapword w = page->words[wordnum];
--
2.39.5 (Apple Git-154)
Added to CF: https://commitfest.postgresql.org/patch/6227/
On Nov 14, 2025, at 21:52, Chao Li <li.evan.chao@gmail.com> wrote:
On Wed, Nov 12, 2025 at 4:00 PM Chao Li <li.evan.chao@gmail.com> wrote:
Hi Hackers,While working on the other patch [1] that touched tbm_add_tuples() under src/backend/nodes, I moved a loop variable into for statement by the way as I knew Peter Eisentraut had done some efforts about that, see [2].
However, Peter removed that change from the patch and he said that should belong to a separate cleanup patch. So I am following up and moving loop variables into for statements wherever possible under src/backend/nodes.
[1] /messages/by-id/CAEoWx2m2E0xE8Kvbkv31ULh_E+5zph-WA_bEdv3UR9CLhw+3vg@mail.gmail.com
[2] https://git.postgresql.org/cgit/postgresql.git/commit/?id=03fbb0814c5015ab79e670ab97bb6a3349269e4bRebased to V2.
Best regards,
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/<v2-0001-backend-nodes-cleanup-Move-loop-variables-definit.patch>
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
On 12.11.25 09:00, Chao Li wrote:
While working on the other patch [1] that touched tbm_add_tuples() under
src/backend/nodes, I moved a loop variable into for statement by the way
as I knew Peter Eisentraut had done some efforts about that, see [2].However, Peter removed that change from the patch and he said that
should belong to a separate cleanup patch. So I am following up and
moving loop variables into for statements wherever possible under src/
backend/nodes.
In a couple of cases, you are changing the type of the loop variable
from Size to int. I would not expect such a change in a patch of this
nature. If you have a reason to change it, please explain it (but I
doubt the change is correct).
On Wed, Nov 19, 2025 at 5:36 PM Peter Eisentraut <peter@eisentraut.org>
wrote:
In a couple of cases, you are changing the type of the loop variable
from Size to int. I would not expect such a change in a patch of this
nature. If you have a reason to change it, please explain it (but I
doubt the change is correct).
That was a mistake. I fixed it in v3.
Best regards,
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v3-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchapplication/octet-stream; name=v3-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchDownload
From 19dd35a44725059d4a41793d86fcd839b3b1a70a Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Wed, 12 Nov 2025 15:53:15 +0800
Subject: [PATCH v3] backend/nodes cleanup: Move loop variables definitions
into for statement
---
src/backend/nodes/bitmapset.c | 9 +++------
src/backend/nodes/nodeFuncs.c | 4 +---
src/backend/nodes/outfuncs.c | 13 +++++--------
src/backend/nodes/params.c | 6 ++----
src/backend/nodes/readfuncs.c | 7 +++----
src/backend/nodes/tidbitmap.c | 16 ++++++----------
6 files changed, 20 insertions(+), 35 deletions(-)
diff --git a/src/backend/nodes/bitmapset.c b/src/backend/nodes/bitmapset.c
index b4ecf0b0390..7b1e9d94103 100644
--- a/src/backend/nodes/bitmapset.c
+++ b/src/backend/nodes/bitmapset.c
@@ -538,7 +538,6 @@ bms_is_member(int x, const Bitmapset *a)
int
bms_member_index(Bitmapset *a, int x)
{
- int i;
int bitnum;
int wordnum;
int result = 0;
@@ -554,7 +553,7 @@ bms_member_index(Bitmapset *a, int x)
bitnum = BITNUM(x);
/* count bits in preceding words */
- for (i = 0; i < wordnum; i++)
+ for (int i = 0; i < wordnum; i++)
{
bitmapword w = a->words[i];
@@ -1306,7 +1305,6 @@ int
bms_next_member(const Bitmapset *a, int prevbit)
{
int nwords;
- int wordnum;
bitmapword mask;
Assert(bms_is_valid_set(a));
@@ -1316,7 +1314,7 @@ bms_next_member(const Bitmapset *a, int prevbit)
nwords = a->nwords;
prevbit++;
mask = (~(bitmapword) 0) << BITNUM(prevbit);
- for (wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
+ for (int wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
{
bitmapword w = a->words[wordnum];
@@ -1366,7 +1364,6 @@ bms_next_member(const Bitmapset *a, int prevbit)
int
bms_prev_member(const Bitmapset *a, int prevbit)
{
- int wordnum;
int ushiftbits;
bitmapword mask;
@@ -1391,7 +1388,7 @@ bms_prev_member(const Bitmapset *a, int prevbit)
ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(prevbit) + 1);
mask = (~(bitmapword) 0) >> ushiftbits;
- for (wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
+ for (int wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
{
bitmapword w = a->words[wordnum];
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index ede838cd40c..d228318dc72 100644
--- a/src/backend/nodes/nodeFuncs.c
+++ b/src/backend/nodes/nodeFuncs.c
@@ -4826,9 +4826,7 @@ planstate_walk_members(PlanState **planstates, int nplans,
planstate_tree_walker_callback walker,
void *context)
{
- int j;
-
- for (j = 0; j < nplans; j++)
+ for (int j = 0; j < nplans; j++)
{
if (PSWALK(planstates[j]))
return true;
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index d9fe21a07e0..1ba590b71d6 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -346,8 +346,7 @@ outBitmapset(StringInfo str, const Bitmapset *bms)
void
outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
- Size length,
- i;
+ Size length;
char *s;
length = datumGetSize(value, typbyval, typlen);
@@ -356,7 +355,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
s = (char *) (&value);
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (int i = 0; i < (Size) sizeof(Datum); i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -368,7 +367,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
else
{
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < length; i++)
+ for (int i = 0; i < length; i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -434,8 +433,6 @@ _outBoolExpr(StringInfo str, const BoolExpr *node)
static void
_outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
{
- int i;
-
WRITE_NODE_TYPE("FOREIGNKEYOPTINFO");
WRITE_UINT_FIELD(con_relid);
@@ -450,10 +447,10 @@ _outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
WRITE_INT_FIELD(nmatched_ri);
/* for compactness, just print the number of matches per column: */
appendStringInfoString(str, " :eclass");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", (node->eclass[i] != NULL));
appendStringInfoString(str, " :rinfos");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", list_length(node->rinfos[i]));
}
diff --git a/src/backend/nodes/params.c b/src/backend/nodes/params.c
index ec5946c5777..aeb8ace2c54 100644
--- a/src/backend/nodes/params.c
+++ b/src/backend/nodes/params.c
@@ -166,13 +166,12 @@ paramlist_param_ref(ParseState *pstate, ParamRef *pref)
Size
EstimateParamListSpace(ParamListInfo paramLI)
{
- int i;
Size sz = sizeof(int);
if (paramLI == NULL || paramLI->numParams <= 0)
return sz;
- for (i = 0; i < paramLI->numParams; i++)
+ for (int i = 0; i < paramLI->numParams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
@@ -229,7 +228,6 @@ void
SerializeParamList(ParamListInfo paramLI, char **start_address)
{
int nparams;
- int i;
/* Write number of parameters. */
if (paramLI == NULL || paramLI->numParams <= 0)
@@ -240,7 +238,7 @@ SerializeParamList(ParamListInfo paramLI, char **start_address)
*start_address += sizeof(int);
/* Write each parameter in turn. */
- for (i = 0; i < nparams; i++)
+ for (int i = 0; i < nparams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index 2f933e95cb9..9a8ca27ec10 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -599,8 +599,7 @@ parseNodeString(void)
Datum
readDatum(bool typbyval)
{
- Size length,
- i;
+ Size length;
int tokenLength;
const char *token;
Datum res;
@@ -623,7 +622,7 @@ readDatum(bool typbyval)
elog(ERROR, "byval datum but length = %zu", length);
res = (Datum) 0;
s = (char *) (&res);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (Size i = 0; i < (Size) sizeof(Datum); i++)
{
token = pg_strtok(&tokenLength);
s[i] = (char) atoi(token);
@@ -634,7 +633,7 @@ readDatum(bool typbyval)
else
{
s = (char *) palloc(length);
- for (i = 0; i < length; i++)
+ for (Size i = 0; i < length; i++)
{
token = pg_strtok(&tokenLength);
s[i] = (char) atoi(token);
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 23d97b3a6c8..1f83d0d55f5 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -369,10 +369,9 @@ tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids,
{
BlockNumber currblk = InvalidBlockNumber;
PagetableEntry *page = NULL; /* only valid when currblk is valid */
- int i;
Assert(tbm->iterating == TBM_NOT_ITERATING);
- for (i = 0; i < ntids; i++)
+ for (int i = 0; i < ntids; i++)
{
BlockNumber blk = ItemPointerGetBlockNumber(tids + i);
OffsetNumber off = ItemPointerGetOffsetNumber(tids + i);
@@ -471,12 +470,11 @@ static void
tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
{
PagetableEntry *apage;
- int wordnum;
if (bpage->ischunk)
{
/* Scan b's chunk, mark each indicated page lossy in a */
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = bpage->words[wordnum];
@@ -511,7 +509,7 @@ tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
else
{
/* Both pages are exact, merge at the bit level */
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
apage->words[wordnum] |= bpage->words[wordnum];
apage->recheck |= bpage->recheck;
}
@@ -579,14 +577,13 @@ static bool
tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
const PagetableEntry *bpage;
- int wordnum;
if (apage->ischunk)
{
/* Scan each bit in chunk, try to clear */
bool candelete = true;
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = apage->words[wordnum];
@@ -640,7 +637,7 @@ tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
/* Both pages are exact, merge at the bit level */
Assert(!bpage->ischunk);
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
apage->words[wordnum] &= bpage->words[wordnum];
if (apage->words[wordnum] != 0)
@@ -904,10 +901,9 @@ tbm_extract_page_tuple(TBMIterateResult *iteritem,
uint32 max_offsets)
{
PagetableEntry *page = iteritem->internal_page;
- int wordnum;
int ntuples = 0;
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
bitmapword w = page->words[wordnum];
--
2.39.5 (Apple Git-154)
On 19.11.25 11:32, Chao Li wrote:
On Wed, Nov 19, 2025 at 5:36 PM Peter Eisentraut <peter@eisentraut.org
<mailto:peter@eisentraut.org>> wrote:In a couple of cases, you are changing the type of the loop variable
from Size to int. I would not expect such a change in a patch of this
nature. If you have a reason to change it, please explain it (but I
doubt the change is correct).That was a mistake. I fixed it in v3.
Still not correct in outDatum().
On Mon, Nov 24, 2025 at 11:00 PM Peter Eisentraut <peter@eisentraut.org>
wrote:
On 19.11.25 11:32, Chao Li wrote:
On Wed, Nov 19, 2025 at 5:36 PM Peter Eisentraut <peter@eisentraut.org
<mailto:peter@eisentraut.org>> wrote:In a couple of cases, you are changing the type of the loop variable
from Size to int. I would not expect such a change in a patch ofthis
nature. If you have a reason to change it, please explain it (but I
doubt the change is correct).That was a mistake. I fixed it in v3.
Still not correct in outDatum().
Oops! Fixed in v4.
Best regards,
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v4-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchapplication/octet-stream; name=v4-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchDownload
From d11a41943be3e768c0ff7115fc92eb985df91f29 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Wed, 12 Nov 2025 15:53:15 +0800
Subject: [PATCH v4] backend/nodes cleanup: Move loop variables definitions
into for statement
---
src/backend/nodes/bitmapset.c | 9 +++------
src/backend/nodes/nodeFuncs.c | 4 +---
src/backend/nodes/outfuncs.c | 13 +++++--------
src/backend/nodes/params.c | 6 ++----
src/backend/nodes/readfuncs.c | 7 +++----
src/backend/nodes/tidbitmap.c | 16 ++++++----------
6 files changed, 20 insertions(+), 35 deletions(-)
diff --git a/src/backend/nodes/bitmapset.c b/src/backend/nodes/bitmapset.c
index b4ecf0b0390..7b1e9d94103 100644
--- a/src/backend/nodes/bitmapset.c
+++ b/src/backend/nodes/bitmapset.c
@@ -538,7 +538,6 @@ bms_is_member(int x, const Bitmapset *a)
int
bms_member_index(Bitmapset *a, int x)
{
- int i;
int bitnum;
int wordnum;
int result = 0;
@@ -554,7 +553,7 @@ bms_member_index(Bitmapset *a, int x)
bitnum = BITNUM(x);
/* count bits in preceding words */
- for (i = 0; i < wordnum; i++)
+ for (int i = 0; i < wordnum; i++)
{
bitmapword w = a->words[i];
@@ -1306,7 +1305,6 @@ int
bms_next_member(const Bitmapset *a, int prevbit)
{
int nwords;
- int wordnum;
bitmapword mask;
Assert(bms_is_valid_set(a));
@@ -1316,7 +1314,7 @@ bms_next_member(const Bitmapset *a, int prevbit)
nwords = a->nwords;
prevbit++;
mask = (~(bitmapword) 0) << BITNUM(prevbit);
- for (wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
+ for (int wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
{
bitmapword w = a->words[wordnum];
@@ -1366,7 +1364,6 @@ bms_next_member(const Bitmapset *a, int prevbit)
int
bms_prev_member(const Bitmapset *a, int prevbit)
{
- int wordnum;
int ushiftbits;
bitmapword mask;
@@ -1391,7 +1388,7 @@ bms_prev_member(const Bitmapset *a, int prevbit)
ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(prevbit) + 1);
mask = (~(bitmapword) 0) >> ushiftbits;
- for (wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
+ for (int wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
{
bitmapword w = a->words[wordnum];
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index ede838cd40c..d228318dc72 100644
--- a/src/backend/nodes/nodeFuncs.c
+++ b/src/backend/nodes/nodeFuncs.c
@@ -4826,9 +4826,7 @@ planstate_walk_members(PlanState **planstates, int nplans,
planstate_tree_walker_callback walker,
void *context)
{
- int j;
-
- for (j = 0; j < nplans; j++)
+ for (int j = 0; j < nplans; j++)
{
if (PSWALK(planstates[j]))
return true;
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index d9fe21a07e0..0abca9f803b 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -346,8 +346,7 @@ outBitmapset(StringInfo str, const Bitmapset *bms)
void
outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
- Size length,
- i;
+ Size length;
char *s;
length = datumGetSize(value, typbyval, typlen);
@@ -356,7 +355,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
s = (char *) (&value);
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (Size i = 0; i < (Size) sizeof(Datum); i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -368,7 +367,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
else
{
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < length; i++)
+ for (Size i = 0; i < length; i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -434,8 +433,6 @@ _outBoolExpr(StringInfo str, const BoolExpr *node)
static void
_outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
{
- int i;
-
WRITE_NODE_TYPE("FOREIGNKEYOPTINFO");
WRITE_UINT_FIELD(con_relid);
@@ -450,10 +447,10 @@ _outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
WRITE_INT_FIELD(nmatched_ri);
/* for compactness, just print the number of matches per column: */
appendStringInfoString(str, " :eclass");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", (node->eclass[i] != NULL));
appendStringInfoString(str, " :rinfos");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", list_length(node->rinfos[i]));
}
diff --git a/src/backend/nodes/params.c b/src/backend/nodes/params.c
index ec5946c5777..aeb8ace2c54 100644
--- a/src/backend/nodes/params.c
+++ b/src/backend/nodes/params.c
@@ -166,13 +166,12 @@ paramlist_param_ref(ParseState *pstate, ParamRef *pref)
Size
EstimateParamListSpace(ParamListInfo paramLI)
{
- int i;
Size sz = sizeof(int);
if (paramLI == NULL || paramLI->numParams <= 0)
return sz;
- for (i = 0; i < paramLI->numParams; i++)
+ for (int i = 0; i < paramLI->numParams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
@@ -229,7 +228,6 @@ void
SerializeParamList(ParamListInfo paramLI, char **start_address)
{
int nparams;
- int i;
/* Write number of parameters. */
if (paramLI == NULL || paramLI->numParams <= 0)
@@ -240,7 +238,7 @@ SerializeParamList(ParamListInfo paramLI, char **start_address)
*start_address += sizeof(int);
/* Write each parameter in turn. */
- for (i = 0; i < nparams; i++)
+ for (int i = 0; i < nparams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index 2f933e95cb9..9a8ca27ec10 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -599,8 +599,7 @@ parseNodeString(void)
Datum
readDatum(bool typbyval)
{
- Size length,
- i;
+ Size length;
int tokenLength;
const char *token;
Datum res;
@@ -623,7 +622,7 @@ readDatum(bool typbyval)
elog(ERROR, "byval datum but length = %zu", length);
res = (Datum) 0;
s = (char *) (&res);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (Size i = 0; i < (Size) sizeof(Datum); i++)
{
token = pg_strtok(&tokenLength);
s[i] = (char) atoi(token);
@@ -634,7 +633,7 @@ readDatum(bool typbyval)
else
{
s = (char *) palloc(length);
- for (i = 0; i < length; i++)
+ for (Size i = 0; i < length; i++)
{
token = pg_strtok(&tokenLength);
s[i] = (char) atoi(token);
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 23d97b3a6c8..1f83d0d55f5 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -369,10 +369,9 @@ tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids,
{
BlockNumber currblk = InvalidBlockNumber;
PagetableEntry *page = NULL; /* only valid when currblk is valid */
- int i;
Assert(tbm->iterating == TBM_NOT_ITERATING);
- for (i = 0; i < ntids; i++)
+ for (int i = 0; i < ntids; i++)
{
BlockNumber blk = ItemPointerGetBlockNumber(tids + i);
OffsetNumber off = ItemPointerGetOffsetNumber(tids + i);
@@ -471,12 +470,11 @@ static void
tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
{
PagetableEntry *apage;
- int wordnum;
if (bpage->ischunk)
{
/* Scan b's chunk, mark each indicated page lossy in a */
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = bpage->words[wordnum];
@@ -511,7 +509,7 @@ tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
else
{
/* Both pages are exact, merge at the bit level */
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
apage->words[wordnum] |= bpage->words[wordnum];
apage->recheck |= bpage->recheck;
}
@@ -579,14 +577,13 @@ static bool
tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
const PagetableEntry *bpage;
- int wordnum;
if (apage->ischunk)
{
/* Scan each bit in chunk, try to clear */
bool candelete = true;
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = apage->words[wordnum];
@@ -640,7 +637,7 @@ tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
/* Both pages are exact, merge at the bit level */
Assert(!bpage->ischunk);
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
apage->words[wordnum] &= bpage->words[wordnum];
if (apage->words[wordnum] != 0)
@@ -904,10 +901,9 @@ tbm_extract_page_tuple(TBMIterateResult *iteritem,
uint32 max_offsets)
{
PagetableEntry *page = iteritem->internal_page;
- int wordnum;
int ntuples = 0;
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
bitmapword w = page->words[wordnum];
--
2.39.5 (Apple Git-154)
On Tue, Nov 25, 2025 at 7:38 AM Chao Li <li.evan.chao@gmail.com> wrote:
On Mon, Nov 24, 2025 at 11:00 PM Peter Eisentraut <peter@eisentraut.org>
wrote:On 19.11.25 11:32, Chao Li wrote:
On Wed, Nov 19, 2025 at 5:36 PM Peter Eisentraut <peter@eisentraut.org
<mailto:peter@eisentraut.org>> wrote:In a couple of cases, you are changing the type of the loop variable
from Size to int. I would not expect such a change in a patch ofthis
nature. If you have a reason to change it, please explain it (but I
doubt the change is correct).That was a mistake. I fixed it in v3.
Still not correct in outDatum().
Oops! Fixed in v4.
Rebased to v5.
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v5-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchapplication/octet-stream; name=v5-0001-backend-nodes-cleanup-Move-loop-variables-definit.patchDownload
From ba4fa56bff961ddf35bb26147d92c097c46b0bda Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Wed, 12 Nov 2025 15:53:15 +0800
Subject: [PATCH v5] backend/nodes cleanup: Move loop variables definitions
into for statement
---
src/backend/nodes/bitmapset.c | 9 +++------
src/backend/nodes/nodeFuncs.c | 4 +---
src/backend/nodes/outfuncs.c | 13 +++++--------
src/backend/nodes/params.c | 6 ++----
src/backend/nodes/readfuncs.c | 7 +++----
src/backend/nodes/tidbitmap.c | 16 ++++++----------
6 files changed, 20 insertions(+), 35 deletions(-)
diff --git a/src/backend/nodes/bitmapset.c b/src/backend/nodes/bitmapset.c
index b4ecf0b0390..7b1e9d94103 100644
--- a/src/backend/nodes/bitmapset.c
+++ b/src/backend/nodes/bitmapset.c
@@ -538,7 +538,6 @@ bms_is_member(int x, const Bitmapset *a)
int
bms_member_index(Bitmapset *a, int x)
{
- int i;
int bitnum;
int wordnum;
int result = 0;
@@ -554,7 +553,7 @@ bms_member_index(Bitmapset *a, int x)
bitnum = BITNUM(x);
/* count bits in preceding words */
- for (i = 0; i < wordnum; i++)
+ for (int i = 0; i < wordnum; i++)
{
bitmapword w = a->words[i];
@@ -1306,7 +1305,6 @@ int
bms_next_member(const Bitmapset *a, int prevbit)
{
int nwords;
- int wordnum;
bitmapword mask;
Assert(bms_is_valid_set(a));
@@ -1316,7 +1314,7 @@ bms_next_member(const Bitmapset *a, int prevbit)
nwords = a->nwords;
prevbit++;
mask = (~(bitmapword) 0) << BITNUM(prevbit);
- for (wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
+ for (int wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
{
bitmapword w = a->words[wordnum];
@@ -1366,7 +1364,6 @@ bms_next_member(const Bitmapset *a, int prevbit)
int
bms_prev_member(const Bitmapset *a, int prevbit)
{
- int wordnum;
int ushiftbits;
bitmapword mask;
@@ -1391,7 +1388,7 @@ bms_prev_member(const Bitmapset *a, int prevbit)
ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(prevbit) + 1);
mask = (~(bitmapword) 0) >> ushiftbits;
- for (wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
+ for (int wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
{
bitmapword w = a->words[wordnum];
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index ede838cd40c..d228318dc72 100644
--- a/src/backend/nodes/nodeFuncs.c
+++ b/src/backend/nodes/nodeFuncs.c
@@ -4826,9 +4826,7 @@ planstate_walk_members(PlanState **planstates, int nplans,
planstate_tree_walker_callback walker,
void *context)
{
- int j;
-
- for (j = 0; j < nplans; j++)
+ for (int j = 0; j < nplans; j++)
{
if (PSWALK(planstates[j]))
return true;
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index d9fe21a07e0..0abca9f803b 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -346,8 +346,7 @@ outBitmapset(StringInfo str, const Bitmapset *bms)
void
outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
- Size length,
- i;
+ Size length;
char *s;
length = datumGetSize(value, typbyval, typlen);
@@ -356,7 +355,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{
s = (char *) (&value);
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (Size i = 0; i < (Size) sizeof(Datum); i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -368,7 +367,7 @@ outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
else
{
appendStringInfo(str, "%u [ ", (unsigned int) length);
- for (i = 0; i < length; i++)
+ for (Size i = 0; i < length; i++)
appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfoChar(str, ']');
}
@@ -434,8 +433,6 @@ _outBoolExpr(StringInfo str, const BoolExpr *node)
static void
_outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
{
- int i;
-
WRITE_NODE_TYPE("FOREIGNKEYOPTINFO");
WRITE_UINT_FIELD(con_relid);
@@ -450,10 +447,10 @@ _outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
WRITE_INT_FIELD(nmatched_ri);
/* for compactness, just print the number of matches per column: */
appendStringInfoString(str, " :eclass");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", (node->eclass[i] != NULL));
appendStringInfoString(str, " :rinfos");
- for (i = 0; i < node->nkeys; i++)
+ for (int i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", list_length(node->rinfos[i]));
}
diff --git a/src/backend/nodes/params.c b/src/backend/nodes/params.c
index ec5946c5777..aeb8ace2c54 100644
--- a/src/backend/nodes/params.c
+++ b/src/backend/nodes/params.c
@@ -166,13 +166,12 @@ paramlist_param_ref(ParseState *pstate, ParamRef *pref)
Size
EstimateParamListSpace(ParamListInfo paramLI)
{
- int i;
Size sz = sizeof(int);
if (paramLI == NULL || paramLI->numParams <= 0)
return sz;
- for (i = 0; i < paramLI->numParams; i++)
+ for (int i = 0; i < paramLI->numParams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
@@ -229,7 +228,6 @@ void
SerializeParamList(ParamListInfo paramLI, char **start_address)
{
int nparams;
- int i;
/* Write number of parameters. */
if (paramLI == NULL || paramLI->numParams <= 0)
@@ -240,7 +238,7 @@ SerializeParamList(ParamListInfo paramLI, char **start_address)
*start_address += sizeof(int);
/* Write each parameter in turn. */
- for (i = 0; i < nparams; i++)
+ for (int i = 0; i < nparams; i++)
{
ParamExternData *prm;
ParamExternData prmdata;
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index 2f933e95cb9..9a8ca27ec10 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -599,8 +599,7 @@ parseNodeString(void)
Datum
readDatum(bool typbyval)
{
- Size length,
- i;
+ Size length;
int tokenLength;
const char *token;
Datum res;
@@ -623,7 +622,7 @@ readDatum(bool typbyval)
elog(ERROR, "byval datum but length = %zu", length);
res = (Datum) 0;
s = (char *) (&res);
- for (i = 0; i < (Size) sizeof(Datum); i++)
+ for (Size i = 0; i < (Size) sizeof(Datum); i++)
{
token = pg_strtok(&tokenLength);
s[i] = (char) atoi(token);
@@ -634,7 +633,7 @@ readDatum(bool typbyval)
else
{
s = (char *) palloc(length);
- for (i = 0; i < length; i++)
+ for (Size i = 0; i < length; i++)
{
token = pg_strtok(&tokenLength);
s[i] = (char) atoi(token);
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 23d97b3a6c8..1f83d0d55f5 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -369,10 +369,9 @@ tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids,
{
BlockNumber currblk = InvalidBlockNumber;
PagetableEntry *page = NULL; /* only valid when currblk is valid */
- int i;
Assert(tbm->iterating == TBM_NOT_ITERATING);
- for (i = 0; i < ntids; i++)
+ for (int i = 0; i < ntids; i++)
{
BlockNumber blk = ItemPointerGetBlockNumber(tids + i);
OffsetNumber off = ItemPointerGetOffsetNumber(tids + i);
@@ -471,12 +470,11 @@ static void
tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
{
PagetableEntry *apage;
- int wordnum;
if (bpage->ischunk)
{
/* Scan b's chunk, mark each indicated page lossy in a */
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = bpage->words[wordnum];
@@ -511,7 +509,7 @@ tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
else
{
/* Both pages are exact, merge at the bit level */
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
apage->words[wordnum] |= bpage->words[wordnum];
apage->recheck |= bpage->recheck;
}
@@ -579,14 +577,13 @@ static bool
tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
const PagetableEntry *bpage;
- int wordnum;
if (apage->ischunk)
{
/* Scan each bit in chunk, try to clear */
bool candelete = true;
- for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
{
bitmapword w = apage->words[wordnum];
@@ -640,7 +637,7 @@ tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
{
/* Both pages are exact, merge at the bit level */
Assert(!bpage->ischunk);
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
apage->words[wordnum] &= bpage->words[wordnum];
if (apage->words[wordnum] != 0)
@@ -904,10 +901,9 @@ tbm_extract_page_tuple(TBMIterateResult *iteritem,
uint32 max_offsets)
{
PagetableEntry *page = iteritem->internal_page;
- int wordnum;
int ntuples = 0;
- for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
+ for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
{
bitmapword w = page->words[wordnum];
--
2.39.5 (Apple Git-154)
On 25.11.25 04:46, Chao Li wrote:
On Tue, Nov 25, 2025 at 7:38 AM Chao Li <li.evan.chao@gmail.com
<mailto:li.evan.chao@gmail.com>> wrote:On Mon, Nov 24, 2025 at 11:00 PM Peter Eisentraut
<peter@eisentraut.org <mailto:peter@eisentraut.org>> wrote:On 19.11.25 11:32, Chao Li wrote:
On Wed, Nov 19, 2025 at 5:36 PM Peter Eisentraut
<peter@eisentraut.org <mailto:peter@eisentraut.org>
<mailto:peter@eisentraut.org <mailto:peter@eisentraut.org>>>
wrote:
In a couple of cases, you are changing the type of the
loop variable
from Size to int. I would not expect such a change in a
patch of this
nature. If you have a reason to change it, please
explain it (but I
doubt the change is correct).
That was a mistake. I fixed it in v3.
Still not correct in outDatum().
Oops! Fixed in v4.
Rebased to v5.
committed