Add macros for looping through a List without a ListCell.
Nathan Bossart <nathan@postgresql.org>
Add macros for looping through a List without a ListCell.
Many foreach loops only use the ListCell pointer to retrieve the
content of the cell, like so:
ListCell *lc;
foreach(lc, mylist)
{
int myint = lfirst_int(lc);
...
}
This commit adds a few convenience macros that automatically
declare the loop variable and retrieve the current cell's contents.
This allows us to rewrite the previous loop like this:
foreach_int(myint, mylist)
{
...
}
This commit also adjusts a few existing loops in order to add
coverage for the new/adjusted macros. There is presently no plan
to bulk update all foreach loops, as that could introduce a
significant amount of back-patching pain. Instead, these macros
are primarily intended for use in new code.
Author: Jelte Fennema-Nio
Reviewed-by: David Rowley, Alvaro Herrera, Vignesh C, Tom Lane
Discussion: https://postgr.es/m/CAGECzQSwXKnxGwW1_Q5JE%2B8Ja20kyAbhBHO04vVrQsLcDciwXA%40mail.gmail.com
Files
| Path | Change | +/− |
|---|---|---|
| src/backend/executor/execExpr.c | modified | +3 −6 |
| src/backend/replication/logical/relation.c | modified | +1 −3 |
| src/backend/replication/logical/tablesync.c | modified | +3 −3 |
| src/backend/replication/pgoutput/pgoutput.c | modified | +3 −4 |
| src/include/nodes/pg_list.h | modified | +61 −10 |
Discussion
- Add new for_each macros for iterating over a List that do not require ListCell pointer 24 messages · 2023-10-24 → 2024-01-04