Skip to content

Commit 4ca7276

Browse files
JelteFCommitfest Bot
authored andcommitted
Use foreach_hash macro throughout the codebase
This starts using the new foreach_hash macro throughout the codebase. This makes code easier to read, but obviously does introduce backpatching problems. We can choose not to do this refactor to avoid that. Or we could instead choose to do the refactor and then backpatch these new macros so they can be used in backpatched code. At the very least we should choose a few places where we use the new macros to make sure they have coverage.
1 parent 46bd266 commit 4ca7276

File tree

35 files changed

+131
-490
lines changed

35 files changed

+131
-490
lines changed

contrib/dblink/dblink.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,14 +1275,11 @@ PG_FUNCTION_INFO_V1(dblink_get_connections);
12751275
Datum
12761276
dblink_get_connections(PG_FUNCTION_ARGS)
12771277
{
1278-
HASH_SEQ_STATUS status;
1279-
remoteConnHashEnt *hentry;
12801278
ArrayBuildState *astate = NULL;
12811279

12821280
if (remoteConnHash)
12831281
{
1284-
hash_seq_init(&status, remoteConnHash);
1285-
while ((hentry = (remoteConnHashEnt *) hash_seq_search(&status)) != NULL)
1282+
foreach_hash(remoteConnHashEnt, hentry, remoteConnHash)
12861283
{
12871284
/* ignore it if it's not an open connection */
12881285
if (hentry->rconn.conn == NULL)

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,7 @@ pgss_shmem_shutdown(int code, Datum arg)
741741
FILE *file;
742742
char *qbuffer = NULL;
743743
Size qbuffer_size = 0;
744-
HASH_SEQ_STATUS hash_seq;
745744
int32 num_entries;
746-
pgssEntry *entry;
747745

748746
/* Don't try to dump during a crash. */
749747
if (code)
@@ -777,8 +775,7 @@ pgss_shmem_shutdown(int code, Datum arg)
777775
* When serializing to disk, we store query texts immediately after their
778776
* entry data. Any orphaned query texts are thereby excluded.
779777
*/
780-
hash_seq_init(&hash_seq, pgss_hash);
781-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
778+
foreach_hash(pgssEntry, entry, pgss_hash)
782779
{
783780
int len = entry->query_len;
784781
char *qstr = qtext_fetch(entry->query_offset, len,
@@ -790,8 +787,8 @@ pgss_shmem_shutdown(int code, Datum arg)
790787
if (fwrite(entry, sizeof(pgssEntry), 1, file) != 1 ||
791788
fwrite(qstr, 1, len + 1, file) != len + 1)
792789
{
793-
/* note: we assume hash_seq_term won't change errno */
794-
hash_seq_term(&hash_seq);
790+
/* note: we assume foreach_hash_term won't change errno */
791+
foreach_hash_term(entry);
795792
goto error;
796793
}
797794
}
@@ -1695,8 +1692,6 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
16951692
Size qbuffer_size = 0;
16961693
Size extent = 0;
16971694
int gc_count = 0;
1698-
HASH_SEQ_STATUS hash_seq;
1699-
pgssEntry *entry;
17001695

17011696
/*
17021697
* Superusers or roles with the privileges of pg_read_all_stats members
@@ -1825,8 +1820,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
18251820
}
18261821
}
18271822

1828-
hash_seq_init(&hash_seq, pgss_hash);
1829-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
1823+
foreach_hash(pgssEntry, entry, pgss_hash)
18301824
{
18311825
Datum values[PG_STAT_STATEMENTS_COLS];
18321826
bool nulls[PG_STAT_STATEMENTS_COLS];
@@ -2170,9 +2164,7 @@ entry_cmp(const void *lhs, const void *rhs)
21702164
static void
21712165
entry_dealloc(void)
21722166
{
2173-
HASH_SEQ_STATUS hash_seq;
21742167
pgssEntry **entries;
2175-
pgssEntry *entry;
21762168
int nvictims;
21772169
int i;
21782170
Size tottextlen;
@@ -2196,8 +2188,7 @@ entry_dealloc(void)
21962188
tottextlen = 0;
21972189
nvalidtexts = 0;
21982190

2199-
hash_seq_init(&hash_seq, pgss_hash);
2200-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2191+
foreach_hash(pgssEntry, entry, pgss_hash)
22012192
{
22022193
entries[i++] = entry;
22032194
/* "Sticky" entries get a different usage decay rate. */
@@ -2509,8 +2500,6 @@ gc_qtexts(void)
25092500
char *qbuffer;
25102501
Size qbuffer_size;
25112502
FILE *qfile = NULL;
2512-
HASH_SEQ_STATUS hash_seq;
2513-
pgssEntry *entry;
25142503
Size extent;
25152504
int nentries;
25162505

@@ -2552,8 +2541,7 @@ gc_qtexts(void)
25522541
extent = 0;
25532542
nentries = 0;
25542543

2555-
hash_seq_init(&hash_seq, pgss_hash);
2556-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2544+
foreach_hash(pgssEntry, entry, pgss_hash)
25572545
{
25582546
int query_len = entry->query_len;
25592547
char *qry = qtext_fetch(entry->query_offset,
@@ -2576,7 +2564,7 @@ gc_qtexts(void)
25762564
(errcode_for_file_access(),
25772565
errmsg("could not write file \"%s\": %m",
25782566
PGSS_TEXT_FILE)));
2579-
hash_seq_term(&hash_seq);
2567+
foreach_hash_term(entry);
25802568
goto gc_fail;
25812569
}
25822570

@@ -2643,8 +2631,7 @@ gc_qtexts(void)
26432631
* Since the contents of the external file are now uncertain, mark all
26442632
* hashtable entries as having invalid texts.
26452633
*/
2646-
hash_seq_init(&hash_seq, pgss_hash);
2647-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2634+
foreach_hash(pgssEntry, entry, pgss_hash)
26482635
{
26492636
entry->query_offset = 0;
26502637
entry->query_len = -1;
@@ -2708,8 +2695,6 @@ if (e) { \
27082695
static TimestampTz
27092696
entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
27102697
{
2711-
HASH_SEQ_STATUS hash_seq;
2712-
pgssEntry *entry;
27132698
FILE *qfile;
27142699
int64 num_entries;
27152700
int64 num_remove = 0;
@@ -2729,6 +2714,8 @@ entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
27292714
if (userid != 0 && dbid != 0 && queryid != INT64CONST(0))
27302715
{
27312716
/* If all the parameters are available, use the fast path. */
2717+
pgssEntry *entry;
2718+
27322719
memset(&key, 0, sizeof(pgssHashKey));
27332720
key.userid = userid;
27342721
key.dbid = dbid;
@@ -2752,8 +2739,7 @@ entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
27522739
else if (userid != 0 || dbid != 0 || queryid != INT64CONST(0))
27532740
{
27542741
/* Reset entries corresponding to valid parameters. */
2755-
hash_seq_init(&hash_seq, pgss_hash);
2756-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2742+
foreach_hash(pgssEntry, entry, pgss_hash)
27572743
{
27582744
if ((!userid || entry->key.userid == userid) &&
27592745
(!dbid || entry->key.dbid == dbid) &&
@@ -2766,8 +2752,7 @@ entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
27662752
else
27672753
{
27682754
/* Reset all entries. */
2769-
hash_seq_init(&hash_seq, pgss_hash);
2770-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2755+
foreach_hash(pgssEntry, entry, pgss_hash)
27712756
{
27722757
SINGLE_ENTRY_RESET(entry);
27732758
}

contrib/pg_trgm/trgm_regexp.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,10 +1449,8 @@ prefixContains(TrgmPrefix *prefix1, TrgmPrefix *prefix2)
14491449
static bool
14501450
selectColorTrigrams(TrgmNFA *trgmNFA)
14511451
{
1452-
HASH_SEQ_STATUS scan_status;
14531452
int arcsCount = trgmNFA->arcsCount,
14541453
i;
1455-
TrgmState *state;
14561454
ColorTrgmInfo *colorTrgms;
14571455
int64 totalTrgmCount;
14581456
float4 totalTrgmPenalty;
@@ -1463,8 +1461,7 @@ selectColorTrigrams(TrgmNFA *trgmNFA)
14631461
trgmNFA->colorTrgms = colorTrgms;
14641462

14651463
i = 0;
1466-
hash_seq_init(&scan_status, trgmNFA->states);
1467-
while ((state = (TrgmState *) hash_seq_search(&scan_status)) != NULL)
1464+
foreach_hash(TrgmState, state, trgmNFA->states)
14681465
{
14691466
ListCell *cell;
14701467

@@ -1926,17 +1923,14 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
19261923
int snumber = 2,
19271924
arcIndex,
19281925
arcsCount;
1929-
HASH_SEQ_STATUS scan_status;
1930-
TrgmState *state;
19311926
TrgmPackArcInfo *arcs;
19321927
TrgmPackedArc *packedArcs;
19331928
TrgmPackedGraph *result;
19341929
int i,
19351930
j;
19361931

19371932
/* Enumerate surviving states, giving init and fin reserved numbers */
1938-
hash_seq_init(&scan_status, trgmNFA->states);
1939-
while ((state = (TrgmState *) hash_seq_search(&scan_status)) != NULL)
1933+
foreach_hash(TrgmState, state, trgmNFA->states)
19401934
{
19411935
while (state->parent)
19421936
state = state->parent;
@@ -1958,8 +1952,7 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
19581952
/* Collect array of all arcs */
19591953
arcs = palloc_array(TrgmPackArcInfo, trgmNFA->arcsCount);
19601954
arcIndex = 0;
1961-
hash_seq_init(&scan_status, trgmNFA->states);
1962-
while ((state = (TrgmState *) hash_seq_search(&scan_status)) != NULL)
1955+
foreach_hash(TrgmState, state, trgmNFA->states)
19631956
{
19641957
TrgmState *source = state;
19651958
ListCell *cell;
@@ -2202,16 +2195,13 @@ static void
22022195
printTrgmNFA(TrgmNFA *trgmNFA)
22032196
{
22042197
StringInfoData buf;
2205-
HASH_SEQ_STATUS scan_status;
2206-
TrgmState *state;
22072198
TrgmState *initstate = NULL;
22082199

22092200
initStringInfo(&buf);
22102201

22112202
appendStringInfoString(&buf, "\ndigraph transformedNFA {\n");
22122203

2213-
hash_seq_init(&scan_status, trgmNFA->states);
2214-
while ((state = (TrgmState *) hash_seq_search(&scan_status)) != NULL)
2204+
foreach_hash(TrgmState, state, trgmNFA->states)
22152205
{
22162206
ListCell *cell;
22172207

contrib/postgres_fdw/connection.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,6 @@ pgfdw_report_internal(int elevel, PGresult *res, PGconn *conn,
10441044
static void
10451045
pgfdw_xact_callback(XactEvent event, void *arg)
10461046
{
1047-
HASH_SEQ_STATUS scan;
1048-
ConnCacheEntry *entry;
10491047
List *pending_entries = NIL;
10501048
List *cancel_requested = NIL;
10511049

@@ -1057,8 +1055,7 @@ pgfdw_xact_callback(XactEvent event, void *arg)
10571055
* Scan all connection cache entries to find open remote transactions, and
10581056
* close them.
10591057
*/
1060-
hash_seq_init(&scan, ConnectionHash);
1061-
while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
1058+
foreach_hash(ConnCacheEntry, entry, ConnectionHash)
10621059
{
10631060
PGresult *res;
10641061

@@ -1195,8 +1192,6 @@ static void
11951192
pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
11961193
SubTransactionId parentSubid, void *arg)
11971194
{
1198-
HASH_SEQ_STATUS scan;
1199-
ConnCacheEntry *entry;
12001195
int curlevel;
12011196
List *pending_entries = NIL;
12021197
List *cancel_requested = NIL;
@@ -1215,8 +1210,7 @@ pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
12151210
* of the current level, and close them.
12161211
*/
12171212
curlevel = GetCurrentTransactionNestLevel();
1218-
hash_seq_init(&scan, ConnectionHash);
1219-
while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
1213+
foreach_hash(ConnCacheEntry, entry, ConnectionHash)
12201214
{
12211215
char sql[100];
12221216

@@ -1307,14 +1301,10 @@ pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
13071301
static void
13081302
pgfdw_inval_callback(Datum arg, int cacheid, uint32 hashvalue)
13091303
{
1310-
HASH_SEQ_STATUS scan;
1311-
ConnCacheEntry *entry;
1312-
13131304
Assert(cacheid == FOREIGNSERVEROID || cacheid == USERMAPPINGOID);
13141305

13151306
/* ConnectionHash must exist already, if we're registered */
1316-
hash_seq_init(&scan, ConnectionHash);
1317-
while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
1307+
foreach_hash(ConnCacheEntry, entry, ConnectionHash)
13181308
{
13191309
/* Ignore invalid entries */
13201310
if (entry->conn == NULL)
@@ -2165,8 +2155,6 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo,
21652155
enum pgfdwVersion api_version)
21662156
{
21672157
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
2168-
HASH_SEQ_STATUS scan;
2169-
ConnCacheEntry *entry;
21702158

21712159
InitMaterializedSRF(fcinfo, 0);
21722160

@@ -2189,8 +2177,7 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo,
21892177
elog(ERROR, "incorrect number of output arguments");
21902178
}
21912179

2192-
hash_seq_init(&scan, ConnectionHash);
2193-
while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
2180+
foreach_hash(ConnCacheEntry, entry, ConnectionHash)
21942181
{
21952182
ForeignServer *server;
21962183
Datum values[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0};
@@ -2392,8 +2379,6 @@ postgres_fdw_disconnect_all(PG_FUNCTION_ARGS)
23922379
static bool
23932380
disconnect_cached_connections(Oid serverid)
23942381
{
2395-
HASH_SEQ_STATUS scan;
2396-
ConnCacheEntry *entry;
23972382
bool all = !OidIsValid(serverid);
23982383
bool result = false;
23992384

@@ -2404,8 +2389,7 @@ disconnect_cached_connections(Oid serverid)
24042389
if (!ConnectionHash)
24052390
return false;
24062391

2407-
hash_seq_init(&scan, ConnectionHash);
2408-
while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
2392+
foreach_hash(ConnCacheEntry, entry, ConnectionHash)
24092393
{
24102394
/* Ignore cache entry if no open connection right now. */
24112395
if (!entry->conn)

contrib/postgres_fdw/shippable.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,13 @@ typedef struct
6565
static void
6666
InvalidateShippableCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
6767
{
68-
HASH_SEQ_STATUS status;
69-
ShippableCacheEntry *entry;
70-
7168
/*
7269
* In principle we could flush only cache entries relating to the
7370
* pg_foreign_server entry being outdated; but that would be more
7471
* complicated, and it's probably not worth the trouble. So for now, just
7572
* flush all entries.
7673
*/
77-
hash_seq_init(&status, ShippableCacheHash);
78-
while ((entry = (ShippableCacheEntry *) hash_seq_search(&status)) != NULL)
74+
foreach_hash(ShippableCacheEntry, entry, ShippableCacheHash)
7975
{
8076
if (hash_search(ShippableCacheHash,
8177
&entry->key,

src/backend/access/heap/rewriteheap.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,11 @@ begin_heap_rewrite(Relation old_heap, Relation new_heap, TransactionId oldest_xm
290290
void
291291
end_heap_rewrite(RewriteState state)
292292
{
293-
HASH_SEQ_STATUS seq_status;
294-
UnresolvedTup unresolved;
295-
296293
/*
297294
* Write any remaining tuples in the UnresolvedTups table. If we have any
298295
* left, they should in fact be dead, but let's err on the safe side.
299296
*/
300-
hash_seq_init(&seq_status, state->rs_unresolved_tups);
301-
302-
while ((unresolved = hash_seq_search(&seq_status)) != NULL)
297+
foreach_hash(UnresolvedTupData, unresolved, state->rs_unresolved_tups)
303298
{
304299
ItemPointerSetInvalid(&unresolved->tuple->t_data->t_ctid);
305300
raw_heap_insert(state, unresolved->tuple);
@@ -794,8 +789,6 @@ logical_begin_heap_rewrite(RewriteState state)
794789
static void
795790
logical_heap_rewrite_flush_mappings(RewriteState state)
796791
{
797-
HASH_SEQ_STATUS seq_status;
798-
RewriteMappingFile *src;
799792
dlist_mutable_iter iter;
800793

801794
Assert(state->rs_logical_rewrite);
@@ -807,8 +800,7 @@ logical_heap_rewrite_flush_mappings(RewriteState state)
807800
elog(DEBUG1, "flushing %u logical rewrite mapping entries",
808801
state->rs_num_rewrite_mappings);
809802

810-
hash_seq_init(&seq_status, state->rs_logical_mappings);
811-
while ((src = (RewriteMappingFile *) hash_seq_search(&seq_status)) != NULL)
803+
foreach_hash(RewriteMappingFile, src, state->rs_logical_mappings)
812804
{
813805
char *waldata;
814806
char *waldata_start;
@@ -892,9 +884,6 @@ logical_heap_rewrite_flush_mappings(RewriteState state)
892884
static void
893885
logical_end_heap_rewrite(RewriteState state)
894886
{
895-
HASH_SEQ_STATUS seq_status;
896-
RewriteMappingFile *src;
897-
898887
/* done, no logical rewrite in progress */
899888
if (!state->rs_logical_rewrite)
900889
return;
@@ -904,8 +893,7 @@ logical_end_heap_rewrite(RewriteState state)
904893
logical_heap_rewrite_flush_mappings(state);
905894

906895
/* Iterate over all mappings we have written and fsync the files. */
907-
hash_seq_init(&seq_status, state->rs_logical_mappings);
908-
while ((src = (RewriteMappingFile *) hash_seq_search(&seq_status)) != NULL)
896+
foreach_hash(RewriteMappingFile, src, state->rs_logical_mappings)
909897
{
910898
if (FileSync(src->vfd, WAIT_EVENT_LOGICAL_REWRITE_SYNC) != 0)
911899
ereport(data_sync_elevel(ERROR),

0 commit comments

Comments
 (0)