@@ -370,6 +370,7 @@ CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
370370return ;/* nothing left to do */
371371}
372372
373+ /* delink from linked list */
373374dlist_delete (& ct -> cache_elem );
374375
375376/* free associated tuple data */
@@ -452,9 +453,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
452453 */
453454slist_foreach (cache_iter ,& CacheHdr -> ch_caches )
454455{
456+ CatCache * ccp = slist_container (CatCache ,cc_next ,cache_iter .cur );
455457Index hashIndex ;
456458dlist_mutable_iter iter ;
457- CatCache * ccp = slist_container (CatCache ,cc_next ,cache_iter .cur );
458459
459460if (cacheId != ccp -> id )
460461continue ;
@@ -555,7 +556,7 @@ AtEOXact_CatCache(bool isCommit)
555556{
556557slist_iter cache_iter ;
557558
558- slist_foreach (cache_iter ,& ( CacheHdr -> ch_caches ) )
559+ slist_foreach (cache_iter ,& CacheHdr -> ch_caches )
559560{
560561CatCache * ccp = slist_container (CatCache ,cc_next ,cache_iter .cur );
561562dlist_iter iter ;
@@ -682,7 +683,7 @@ CatalogCacheFlushCatalog(Oid catId)
682683
683684CACHE2_elog (DEBUG2 ,"CatalogCacheFlushCatalog called for %u" ,catId );
684685
685- slist_foreach (iter ,& ( CacheHdr -> ch_caches ) )
686+ slist_foreach (iter ,& CacheHdr -> ch_caches )
686687{
687688CatCache * cache = slist_container (CatCache ,cc_next ,iter .cur );
688689
@@ -770,8 +771,10 @@ InitCatCache(int id,
770771
771772/*
772773 * allocate a new cache structure
774+ *
775+ * Note: we rely on zeroing to initialize all the dlist headers correctly
773776 */
774- cp = (CatCache * )palloc0 (sizeof (CatCache )+ nbuckets * sizeof (dlist_node ));
777+ cp = (CatCache * )palloc0 (sizeof (CatCache )+ nbuckets * sizeof (dlist_head ));
775778
776779/*
777780 * initialize the cache's relation information for the relation
@@ -790,9 +793,6 @@ InitCatCache(int id,
790793for (i = 0 ;i < nkeys ;++ i )
791794cp -> cc_key [i ]= key [i ];
792795
793- dlist_init (& cp -> cc_lists );
794- MemSet (& cp -> cc_bucket ,0 ,nbuckets * sizeof (dlist_head ));
795-
796796/*
797797 * new cache is initialized as far as we can go for now. print some
798798 * debugging information, if appropriate.
@@ -1060,7 +1060,7 @@ SearchCatCache(CatCache *cache,
10601060ScanKeyData cur_skey [CATCACHE_MAXKEYS ];
10611061uint32 hashValue ;
10621062Index hashIndex ;
1063- dlist_mutable_iter iter ;
1063+ dlist_iter iter ;
10641064dlist_head * bucket ;
10651065CatCTup * ct ;
10661066Relation relation ;
@@ -1094,10 +1094,12 @@ SearchCatCache(CatCache *cache,
10941094
10951095/*
10961096 * scan the hash bucket until we find a match or exhaust our tuples
1097+ *
1098+ * Note: it's okay to use dlist_foreach here, even though we modify the
1099+ * dlist within the loop, because we don't continue the loop afterwards.
10971100 */
10981101bucket = & cache -> cc_bucket [hashIndex ];
1099-
1100- dlist_foreach_modify (iter ,bucket )
1102+ dlist_foreach (iter ,bucket )
11011103{
11021104bool res ;
11031105
@@ -1382,6 +1384,9 @@ SearchCatCacheList(CatCache *cache,
13821384
13831385/*
13841386 * scan the items until we find a match or exhaust our list
1387+ *
1388+ * Note: it's okay to use dlist_foreach here, even though we modify the
1389+ * dlist within the loop, because we don't continue the loop afterwards.
13851390 */
13861391dlist_foreach (iter ,& cache -> cc_lists )
13871392{
@@ -1807,11 +1812,11 @@ PrepareToInvalidateCacheTuple(Relation relation,
18071812 * ----------------
18081813 */
18091814
1810- slist_foreach (iter ,& ( CacheHdr -> ch_caches ) )
1815+ slist_foreach (iter ,& CacheHdr -> ch_caches )
18111816{
1817+ CatCache * ccp = slist_container (CatCache ,cc_next ,iter .cur );
18121818uint32 hashvalue ;
18131819Oid dbid ;
1814- CatCache * ccp = slist_container (CatCache ,cc_next ,iter .cur );
18151820
18161821if (ccp -> cc_reloid != reloid )
18171822continue ;