LCOV - code coverage report
Current view: top level - src/backend/utils/cache - catcache.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 608 663 91.7 %
Date: 2025-01-18 04:15:08 Functions: 52 55 94.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * catcache.c
       4             :  *    System catalog cache for tuples matching a key.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/utils/cache/catcache.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/genam.h"
      18             : #include "access/heaptoast.h"
      19             : #include "access/relscan.h"
      20             : #include "access/table.h"
      21             : #include "access/xact.h"
      22             : #include "catalog/catalog.h"
      23             : #include "catalog/pg_collation.h"
      24             : #include "catalog/pg_type.h"
      25             : #include "common/hashfn.h"
      26             : #include "common/pg_prng.h"
      27             : #include "miscadmin.h"
      28             : #include "port/pg_bitutils.h"
      29             : #ifdef CATCACHE_STATS
      30             : #include "storage/ipc.h"      /* for on_proc_exit */
      31             : #endif
      32             : #include "storage/lmgr.h"
      33             : #include "utils/builtins.h"
      34             : #include "utils/catcache.h"
      35             : #include "utils/datum.h"
      36             : #include "utils/fmgroids.h"
      37             : #include "utils/injection_point.h"
      38             : #include "utils/inval.h"
      39             : #include "utils/memutils.h"
      40             : #include "utils/rel.h"
      41             : #include "utils/resowner.h"
      42             : #include "utils/syscache.h"
      43             : 
      44             : /*
      45             :  * If a catcache invalidation is processed while we are in the middle of
      46             :  * creating a catcache entry (or list), it might apply to the entry we're
      47             :  * creating, making it invalid before it's been inserted to the catcache.  To
      48             :  * catch such cases, we have a stack of "create-in-progress" entries.  Cache
      49             :  * invalidation marks any matching entries in the stack as dead, in addition
      50             :  * to the actual CatCTup and CatCList entries.
      51             :  */
      52             : typedef struct CatCInProgress
      53             : {
      54             :     CatCache   *cache;          /* cache that the entry belongs to */
      55             :     uint32      hash_value;     /* hash of the entry; ignored for lists */
      56             :     bool        list;           /* is it a list entry? */
      57             :     bool        dead;           /* set when the entry is invalidated */
      58             :     struct CatCInProgress *next;
      59             : } CatCInProgress;
      60             : 
      61             : static CatCInProgress *catcache_in_progress_stack = NULL;
      62             : 
      63             :  /* #define CACHEDEBUG */   /* turns DEBUG elogs on */
      64             : 
      65             : /*
      66             :  * Given a hash value and the size of the hash table, find the bucket
      67             :  * in which the hash value belongs. Since the hash table must contain
      68             :  * a power-of-2 number of elements, this is a simple bitmask.
      69             :  */
      70             : #define HASH_INDEX(h, sz) ((Index) ((h) & ((sz) - 1)))
      71             : 
      72             : 
      73             : /*
      74             :  *      variables, macros and other stuff
      75             :  */
      76             : 
      77             : #ifdef CACHEDEBUG
      78             : #define CACHE_elog(...)             elog(__VA_ARGS__)
      79             : #else
      80             : #define CACHE_elog(...)
      81             : #endif
      82             : 
      83             : /* Cache management header --- pointer is NULL until created */
      84             : static CatCacheHeader *CacheHdr = NULL;
      85             : 
      86             : static inline HeapTuple SearchCatCacheInternal(CatCache *cache,
      87             :                                                int nkeys,
      88             :                                                Datum v1, Datum v2,
      89             :                                                Datum v3, Datum v4);
      90             : 
      91             : static pg_noinline HeapTuple SearchCatCacheMiss(CatCache *cache,
      92             :                                                 int nkeys,
      93             :                                                 uint32 hashValue,
      94             :                                                 Index hashIndex,
      95             :                                                 Datum v1, Datum v2,
      96             :                                                 Datum v3, Datum v4);
      97             : 
      98             : static uint32 CatalogCacheComputeHashValue(CatCache *cache, int nkeys,
      99             :                                            Datum v1, Datum v2, Datum v3, Datum v4);
     100             : static uint32 CatalogCacheComputeTupleHashValue(CatCache *cache, int nkeys,
     101             :                                                 HeapTuple tuple);
     102             : static inline bool CatalogCacheCompareTuple(const CatCache *cache, int nkeys,
     103             :                                             const Datum *cachekeys,
     104             :                                             const Datum *searchkeys);
     105             : 
     106             : #ifdef CATCACHE_STATS
     107             : static void CatCachePrintStats(int code, Datum arg);
     108             : #endif
     109             : static void CatCacheRemoveCTup(CatCache *cache, CatCTup *ct);
     110             : static void CatCacheRemoveCList(CatCache *cache, CatCList *cl);
     111             : static void RehashCatCache(CatCache *cp);
     112             : static void RehashCatCacheLists(CatCache *cp);
     113             : static void CatalogCacheInitializeCache(CatCache *cache);
     114             : static CatCTup *CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp,
     115             :                                         Datum *arguments,
     116             :                                         uint32 hashValue, Index hashIndex);
     117             : 
     118             : static void ReleaseCatCacheWithOwner(HeapTuple tuple, ResourceOwner resowner);
     119             : static void ReleaseCatCacheListWithOwner(CatCList *list, ResourceOwner resowner);
     120             : static void CatCacheFreeKeys(TupleDesc tupdesc, int nkeys, int *attnos,
     121             :                              Datum *keys);
     122             : static void CatCacheCopyKeys(TupleDesc tupdesc, int nkeys, int *attnos,
     123             :                              Datum *srckeys, Datum *dstkeys);
     124             : 
     125             : 
     126             : /*
     127             :  *                  internal support functions
     128             :  */
     129             : 
     130             : /* ResourceOwner callbacks to hold catcache references */
     131             : 
     132             : static void ResOwnerReleaseCatCache(Datum res);
     133             : static char *ResOwnerPrintCatCache(Datum res);
     134             : static void ResOwnerReleaseCatCacheList(Datum res);
     135             : static char *ResOwnerPrintCatCacheList(Datum res);
     136             : 
     137             : static const ResourceOwnerDesc catcache_resowner_desc =
     138             : {
     139             :     /* catcache references */
     140             :     .name = "catcache reference",
     141             :     .release_phase = RESOURCE_RELEASE_AFTER_LOCKS,
     142             :     .release_priority = RELEASE_PRIO_CATCACHE_REFS,
     143             :     .ReleaseResource = ResOwnerReleaseCatCache,
     144             :     .DebugPrint = ResOwnerPrintCatCache
     145             : };
     146             : 
     147             : static const ResourceOwnerDesc catlistref_resowner_desc =
     148             : {
     149             :     /* catcache-list pins */
     150             :     .name = "catcache list reference",
     151             :     .release_phase = RESOURCE_RELEASE_AFTER_LOCKS,
     152             :     .release_priority = RELEASE_PRIO_CATCACHE_LIST_REFS,
     153             :     .ReleaseResource = ResOwnerReleaseCatCacheList,
     154             :     .DebugPrint = ResOwnerPrintCatCacheList
     155             : };
     156             : 
     157             : /* Convenience wrappers over ResourceOwnerRemember/Forget */
     158             : static inline void
     159    76241478 : ResourceOwnerRememberCatCacheRef(ResourceOwner owner, HeapTuple tuple)
     160             : {
     161    76241478 :     ResourceOwnerRemember(owner, PointerGetDatum(tuple), &catcache_resowner_desc);
     162    76241478 : }
     163             : static inline void
     164    76231096 : ResourceOwnerForgetCatCacheRef(ResourceOwner owner, HeapTuple tuple)
     165             : {
     166    76231096 :     ResourceOwnerForget(owner, PointerGetDatum(tuple), &catcache_resowner_desc);
     167    76231096 : }
     168             : static inline void
     169     3354618 : ResourceOwnerRememberCatCacheListRef(ResourceOwner owner, CatCList *list)
     170             : {
     171     3354618 :     ResourceOwnerRemember(owner, PointerGetDatum(list), &catlistref_resowner_desc);
     172     3354618 : }
     173             : static inline void
     174     3354582 : ResourceOwnerForgetCatCacheListRef(ResourceOwner owner, CatCList *list)
     175             : {
     176     3354582 :     ResourceOwnerForget(owner, PointerGetDatum(list), &catlistref_resowner_desc);
     177     3354582 : }
     178             : 
     179             : 
     180             : /*
     181             :  * Hash and equality functions for system types that are used as cache key
     182             :  * fields.  In some cases, we just call the regular SQL-callable functions for
     183             :  * the appropriate data type, but that tends to be a little slow, and the
     184             :  * speed of these functions is performance-critical.  Therefore, for data
     185             :  * types that frequently occur as catcache keys, we hard-code the logic here.
     186             :  * Avoiding the overhead of DirectFunctionCallN(...) is a substantial win, and
     187             :  * in certain cases (like int4) we can adopt a faster hash algorithm as well.
     188             :  */
     189             : 
     190             : static bool
     191     4817954 : chareqfast(Datum a, Datum b)
     192             : {
     193     4817954 :     return DatumGetChar(a) == DatumGetChar(b);
     194             : }
     195             : 
     196             : static uint32
     197     5495458 : charhashfast(Datum datum)
     198             : {
     199     5495458 :     return murmurhash32((int32) DatumGetChar(datum));
     200             : }
     201             : 
     202             : static bool
     203     3594726 : nameeqfast(Datum a, Datum b)
     204             : {
     205     3594726 :     char       *ca = NameStr(*DatumGetName(a));
     206     3594726 :     char       *cb = NameStr(*DatumGetName(b));
     207             : 
     208     3594726 :     return strncmp(ca, cb, NAMEDATALEN) == 0;
     209             : }
     210             : 
     211             : static uint32
     212     8164972 : namehashfast(Datum datum)
     213             : {
     214     8164972 :     char       *key = NameStr(*DatumGetName(datum));
     215             : 
     216     8164972 :     return hash_any((unsigned char *) key, strlen(key));
     217             : }
     218             : 
     219             : static bool
     220     7778140 : int2eqfast(Datum a, Datum b)
     221             : {
     222     7778140 :     return DatumGetInt16(a) == DatumGetInt16(b);
     223             : }
     224             : 
     225             : static uint32
     226    10718750 : int2hashfast(Datum datum)
     227             : {
     228    10718750 :     return murmurhash32((int32) DatumGetInt16(datum));
     229             : }
     230             : 
     231             : static bool
     232    89493846 : int4eqfast(Datum a, Datum b)
     233             : {
     234    89493846 :     return DatumGetInt32(a) == DatumGetInt32(b);
     235             : }
     236             : 
     237             : static uint32
     238   104919728 : int4hashfast(Datum datum)
     239             : {
     240   104919728 :     return murmurhash32((int32) DatumGetInt32(datum));
     241             : }
     242             : 
     243             : static bool
     244         166 : texteqfast(Datum a, Datum b)
     245             : {
     246             :     /*
     247             :      * The use of DEFAULT_COLLATION_OID is fairly arbitrary here.  We just
     248             :      * want to take the fast "deterministic" path in texteq().
     249             :      */
     250         166 :     return DatumGetBool(DirectFunctionCall2Coll(texteq, DEFAULT_COLLATION_OID, a, b));
     251             : }
     252             : 
     253             : static uint32
     254        3544 : texthashfast(Datum datum)
     255             : {
     256             :     /* analogously here as in texteqfast() */
     257        3544 :     return DatumGetInt32(DirectFunctionCall1Coll(hashtext, DEFAULT_COLLATION_OID, datum));
     258             : }
     259             : 
     260             : static bool
     261        2874 : oidvectoreqfast(Datum a, Datum b)
     262             : {
     263        2874 :     return DatumGetBool(DirectFunctionCall2(oidvectoreq, a, b));
     264             : }
     265             : 
     266             : static uint32
     267      358176 : oidvectorhashfast(Datum datum)
     268             : {
     269      358176 :     return DatumGetInt32(DirectFunctionCall1(hashoidvector, datum));
     270             : }
     271             : 
     272             : /* Lookup support functions for a type. */
     273             : static void
     274     1053978 : GetCCHashEqFuncs(Oid keytype, CCHashFN *hashfunc, RegProcedure *eqfunc, CCFastEqualFN *fasteqfunc)
     275             : {
     276     1053978 :     switch (keytype)
     277             :     {
     278       13894 :         case BOOLOID:
     279       13894 :             *hashfunc = charhashfast;
     280       13894 :             *fasteqfunc = chareqfast;
     281       13894 :             *eqfunc = F_BOOLEQ;
     282       13894 :             break;
     283       18506 :         case CHAROID:
     284       18506 :             *hashfunc = charhashfast;
     285       18506 :             *fasteqfunc = chareqfast;
     286       18506 :             *eqfunc = F_CHAREQ;
     287       18506 :             break;
     288      197358 :         case NAMEOID:
     289      197358 :             *hashfunc = namehashfast;
     290      197358 :             *fasteqfunc = nameeqfast;
     291      197358 :             *eqfunc = F_NAMEEQ;
     292      197358 :             break;
     293       61298 :         case INT2OID:
     294       61298 :             *hashfunc = int2hashfast;
     295       61298 :             *fasteqfunc = int2eqfast;
     296       61298 :             *eqfunc = F_INT2EQ;
     297       61298 :             break;
     298       15680 :         case INT4OID:
     299       15680 :             *hashfunc = int4hashfast;
     300       15680 :             *fasteqfunc = int4eqfast;
     301       15680 :             *eqfunc = F_INT4EQ;
     302       15680 :             break;
     303        7014 :         case TEXTOID:
     304        7014 :             *hashfunc = texthashfast;
     305        7014 :             *fasteqfunc = texteqfast;
     306        7014 :             *eqfunc = F_TEXTEQ;
     307        7014 :             break;
     308      725848 :         case OIDOID:
     309             :         case REGPROCOID:
     310             :         case REGPROCEDUREOID:
     311             :         case REGOPEROID:
     312             :         case REGOPERATOROID:
     313             :         case REGCLASSOID:
     314             :         case REGTYPEOID:
     315             :         case REGCOLLATIONOID:
     316             :         case REGCONFIGOID:
     317             :         case REGDICTIONARYOID:
     318             :         case REGROLEOID:
     319             :         case REGNAMESPACEOID:
     320      725848 :             *hashfunc = int4hashfast;
     321      725848 :             *fasteqfunc = int4eqfast;
     322      725848 :             *eqfunc = F_OIDEQ;
     323      725848 :             break;
     324       14380 :         case OIDVECTOROID:
     325       14380 :             *hashfunc = oidvectorhashfast;
     326       14380 :             *fasteqfunc = oidvectoreqfast;
     327       14380 :             *eqfunc = F_OIDVECTOREQ;
     328       14380 :             break;
     329           0 :         default:
     330           0 :             elog(FATAL, "type %u not supported as catcache key", keytype);
     331             :             *hashfunc = NULL;   /* keep compiler quiet */
     332             : 
     333             :             *eqfunc = InvalidOid;
     334             :             break;
     335             :     }
     336     1053978 : }
     337             : 
     338             : /*
     339             :  *      CatalogCacheComputeHashValue
     340             :  *
     341             :  * Compute the hash value associated with a given set of lookup keys
     342             :  */
     343             : static uint32
     344    92506028 : CatalogCacheComputeHashValue(CatCache *cache, int nkeys,
     345             :                              Datum v1, Datum v2, Datum v3, Datum v4)
     346             : {
     347    92506028 :     uint32      hashValue = 0;
     348             :     uint32      oneHash;
     349    92506028 :     CCHashFN   *cc_hashfunc = cache->cc_hashfunc;
     350             : 
     351             :     CACHE_elog(DEBUG2, "CatalogCacheComputeHashValue %s %d %p",
     352             :                cache->cc_relname, nkeys, cache);
     353             : 
     354    92506028 :     switch (nkeys)
     355             :     {
     356     4351492 :         case 4:
     357     4351492 :             oneHash = (cc_hashfunc[3]) (v4);
     358     4351492 :             hashValue ^= pg_rotate_left32(oneHash, 24);
     359             :             /* FALLTHROUGH */
     360    10594672 :         case 3:
     361    10594672 :             oneHash = (cc_hashfunc[2]) (v3);
     362    10594672 :             hashValue ^= pg_rotate_left32(oneHash, 16);
     363             :             /* FALLTHROUGH */
     364    22208436 :         case 2:
     365    22208436 :             oneHash = (cc_hashfunc[1]) (v2);
     366    22208436 :             hashValue ^= pg_rotate_left32(oneHash, 8);
     367             :             /* FALLTHROUGH */
     368    92506028 :         case 1:
     369    92506028 :             oneHash = (cc_hashfunc[0]) (v1);
     370    92506028 :             hashValue ^= oneHash;
     371    92506028 :             break;
     372           0 :         default:
     373           0 :             elog(FATAL, "wrong number of hash keys: %d", nkeys);
     374             :             break;
     375             :     }
     376             : 
     377    92506028 :     return hashValue;
     378             : }
     379             : 
     380             : /*
     381             :  *      CatalogCacheComputeTupleHashValue
     382             :  *
     383             :  * Compute the hash value associated with a given tuple to be cached
     384             :  */
     385             : static uint32
     386     6583676 : CatalogCacheComputeTupleHashValue(CatCache *cache, int nkeys, HeapTuple tuple)
     387             : {
     388     6583676 :     Datum       v1 = 0,
     389     6583676 :                 v2 = 0,
     390     6583676 :                 v3 = 0,
     391     6583676 :                 v4 = 0;
     392     6583676 :     bool        isNull = false;
     393     6583676 :     int        *cc_keyno = cache->cc_keyno;
     394     6583676 :     TupleDesc   cc_tupdesc = cache->cc_tupdesc;
     395             : 
     396             :     /* Now extract key fields from tuple, insert into scankey */
     397     6583676 :     switch (nkeys)
     398             :     {
     399      396746 :         case 4:
     400      396746 :             v4 = fastgetattr(tuple,
     401      396746 :                              cc_keyno[3],
     402             :                              cc_tupdesc,
     403             :                              &isNull);
     404             :             Assert(!isNull);
     405             :             /* FALLTHROUGH */
     406     1135420 :         case 3:
     407     1135420 :             v3 = fastgetattr(tuple,
     408     1135420 :                              cc_keyno[2],
     409             :                              cc_tupdesc,
     410             :                              &isNull);
     411             :             Assert(!isNull);
     412             :             /* FALLTHROUGH */
     413     4784890 :         case 2:
     414     4784890 :             v2 = fastgetattr(tuple,
     415     4784890 :                              cc_keyno[1],
     416             :                              cc_tupdesc,
     417             :                              &isNull);
     418             :             Assert(!isNull);
     419             :             /* FALLTHROUGH */
     420     6583676 :         case 1:
     421     6583676 :             v1 = fastgetattr(tuple,
     422             :                              cc_keyno[0],
     423             :                              cc_tupdesc,
     424             :                              &isNull);
     425             :             Assert(!isNull);
     426     6583676 :             break;
     427           0 :         default:
     428           0 :             elog(FATAL, "wrong number of hash keys: %d", nkeys);
     429             :             break;
     430             :     }
     431             : 
     432     6583676 :     return CatalogCacheComputeHashValue(cache, nkeys, v1, v2, v3, v4);
     433             : }
     434             : 
     435             : /*
     436             :  *      CatalogCacheCompareTuple
     437             :  *
     438             :  * Compare a tuple to the passed arguments.
     439             :  */
     440             : static inline bool
     441    79279252 : CatalogCacheCompareTuple(const CatCache *cache, int nkeys,
     442             :                          const Datum *cachekeys,
     443             :                          const Datum *searchkeys)
     444             : {
     445    79279252 :     const CCFastEqualFN *cc_fastequal = cache->cc_fastequal;
     446             :     int         i;
     447             : 
     448   184966958 :     for (i = 0; i < nkeys; i++)
     449             :     {
     450   105687706 :         if (!(cc_fastequal[i]) (cachekeys[i], searchkeys[i]))
     451           0 :             return false;
     452             :     }
     453    79279252 :     return true;
     454             : }
     455             : 
     456             : 
     457             : #ifdef CATCACHE_STATS
     458             : 
     459             : static void
     460             : CatCachePrintStats(int code, Datum arg)
     461             : {
     462             :     slist_iter  iter;
     463             :     long        cc_searches = 0;
     464             :     long        cc_hits = 0;
     465             :     long        cc_neg_hits = 0;
     466             :     long        cc_newloads = 0;
     467             :     long        cc_invals = 0;
     468             :     long        cc_nlists = 0;
     469             :     long        cc_lsearches = 0;
     470             :     long        cc_lhits = 0;
     471             : 
     472             :     slist_foreach(iter, &CacheHdr->ch_caches)
     473             :     {
     474             :         CatCache   *cache = slist_container(CatCache, cc_next, iter.cur);
     475             : 
     476             :         if (cache->cc_ntup == 0 && cache->cc_searches == 0)
     477             :             continue;           /* don't print unused caches */
     478             :         elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %d lists, %ld lsrch, %ld lhits",
     479             :              cache->cc_relname,
     480             :              cache->cc_indexoid,
     481             :              cache->cc_ntup,
     482             :              cache->cc_searches,
     483             :              cache->cc_hits,
     484             :              cache->cc_neg_hits,
     485             :              cache->cc_hits + cache->cc_neg_hits,
     486             :              cache->cc_newloads,
     487             :              cache->cc_searches - cache->cc_hits - cache->cc_neg_hits - cache->cc_newloads,
     488             :              cache->cc_searches - cache->cc_hits - cache->cc_neg_hits,
     489             :              cache->cc_invals,
     490             :              cache->cc_nlist,
     491             :              cache->cc_lsearches,
     492             :              cache->cc_lhits);
     493             :         cc_searches += cache->cc_searches;
     494             :         cc_hits += cache->cc_hits;
     495             :         cc_neg_hits += cache->cc_neg_hits;
     496             :         cc_newloads += cache->cc_newloads;
     497             :         cc_invals += cache->cc_invals;
     498             :         cc_nlists += cache->cc_nlist;
     499             :         cc_lsearches += cache->cc_lsearches;
     500             :         cc_lhits += cache->cc_lhits;
     501             :     }
     502             :     elog(DEBUG2, "catcache totals: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lists, %ld lsrch, %ld lhits",
     503             :          CacheHdr->ch_ntup,
     504             :          cc_searches,
     505             :          cc_hits,
     506             :          cc_neg_hits,
     507             :          cc_hits + cc_neg_hits,
     508             :          cc_newloads,
     509             :          cc_searches - cc_hits - cc_neg_hits - cc_newloads,
     510             :          cc_searches - cc_hits - cc_neg_hits,
     511             :          cc_invals,
     512             :          cc_nlists,
     513             :          cc_lsearches,
     514             :          cc_lhits);
     515             : }
     516             : #endif                          /* CATCACHE_STATS */
     517             : 
     518             : 
     519             : /*
     520             :  *      CatCacheRemoveCTup
     521             :  *
     522             :  * Unlink and delete the given cache entry
     523             :  *
     524             :  * NB: if it is a member of a CatCList, the CatCList is deleted too.
     525             :  * Both the cache entry and the list had better have zero refcount.
     526             :  */
     527             : static void
     528     1428334 : CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
     529             : {
     530             :     Assert(ct->refcount == 0);
     531             :     Assert(ct->my_cache == cache);
     532             : 
     533     1428334 :     if (ct->c_list)
     534             :     {
     535             :         /*
     536             :          * The cleanest way to handle this is to call CatCacheRemoveCList,
     537             :          * which will recurse back to me, and the recursive call will do the
     538             :          * work.  Set the "dead" flag to make sure it does recurse.
     539             :          */
     540           0 :         ct->dead = true;
     541           0 :         CatCacheRemoveCList(cache, ct->c_list);
     542           0 :         return;                 /* nothing left to do */
     543             :     }
     544             : 
     545             :     /* delink from linked list */
     546     1428334 :     dlist_delete(&ct->cache_elem);
     547             : 
     548             :     /*
     549             :      * Free keys when we're dealing with a negative entry, normal entries just
     550             :      * point into tuple, allocated together with the CatCTup.
     551             :      */
     552     1428334 :     if (ct->negative)
     553      412774 :         CatCacheFreeKeys(cache->cc_tupdesc, cache->cc_nkeys,
     554      412774 :                          cache->cc_keyno, ct->keys);
     555             : 
     556     1428334 :     pfree(ct);
     557             : 
     558     1428334 :     --cache->cc_ntup;
     559     1428334 :     --CacheHdr->ch_ntup;
     560             : }
     561             : 
     562             : /*
     563             :  *      CatCacheRemoveCList
     564             :  *
     565             :  * Unlink and delete the given cache list entry
     566             :  *
     567             :  * NB: any dead member entries that become unreferenced are deleted too.
     568             :  */
     569             : static void
     570      116180 : CatCacheRemoveCList(CatCache *cache, CatCList *cl)
     571             : {
     572             :     int         i;
     573             : 
     574             :     Assert(cl->refcount == 0);
     575             :     Assert(cl->my_cache == cache);
     576             : 
     577             :     /* delink from member tuples */
     578      388780 :     for (i = cl->n_members; --i >= 0;)
     579             :     {
     580      272600 :         CatCTup    *ct = cl->members[i];
     581             : 
     582             :         Assert(ct->c_list == cl);
     583      272600 :         ct->c_list = NULL;
     584             :         /* if the member is dead and now has no references, remove it */
     585      272600 :         if (
     586             : #ifndef CATCACHE_FORCE_RELEASE
     587      272600 :             ct->dead &&
     588             : #endif
     589         144 :             ct->refcount == 0)
     590         144 :             CatCacheRemoveCTup(cache, ct);
     591             :     }
     592             : 
     593             :     /* delink from linked list */
     594      116180 :     dlist_delete(&cl->cache_elem);
     595             : 
     596             :     /* free associated column data */
     597      116180 :     CatCacheFreeKeys(cache->cc_tupdesc, cl->nkeys,
     598      116180 :                      cache->cc_keyno, cl->keys);
     599             : 
     600      116180 :     pfree(cl);
     601             : 
     602      116180 :     --cache->cc_nlist;
     603      116180 : }
     604             : 
     605             : 
     606             : /*
     607             :  *  CatCacheInvalidate
     608             :  *
     609             :  *  Invalidate entries in the specified cache, given a hash value.
     610             :  *
     611             :  *  We delete cache entries that match the hash value, whether positive
     612             :  *  or negative.  We don't care whether the invalidation is the result
     613             :  *  of a tuple insertion or a deletion.
     614             :  *
     615             :  *  We used to try to match positive cache entries by TID, but that is
     616             :  *  unsafe after a VACUUM FULL on a system catalog: an inval event could
     617             :  *  be queued before VACUUM FULL, and then processed afterwards, when the
     618             :  *  target tuple that has to be invalidated has a different TID than it
     619             :  *  did when the event was created.  So now we just compare hash values and
     620             :  *  accept the small risk of unnecessary invalidations due to false matches.
     621             :  *
     622             :  *  This routine is only quasi-public: it should only be used by inval.c.
     623             :  */
     624             : void
     625    19547952 : CatCacheInvalidate(CatCache *cache, uint32 hashValue)
     626             : {
     627             :     Index       hashIndex;
     628             :     dlist_mutable_iter iter;
     629             : 
     630             :     CACHE_elog(DEBUG2, "CatCacheInvalidate: called");
     631             : 
     632             :     /*
     633             :      * We don't bother to check whether the cache has finished initialization
     634             :      * yet; if not, there will be no entries in it so no problem.
     635             :      */
     636             : 
     637             :     /*
     638             :      * Invalidate *all* CatCLists in this cache; it's too hard to tell which
     639             :      * searches might still be correct, so just zap 'em all.
     640             :      */
     641    23045456 :     for (int i = 0; i < cache->cc_nlbuckets; i++)
     642             :     {
     643     3497504 :         dlist_head *bucket = &cache->cc_lbucket[i];
     644             : 
     645     3609540 :         dlist_foreach_modify(iter, bucket)
     646             :         {
     647      112036 :             CatCList   *cl = dlist_container(CatCList, cache_elem, iter.cur);
     648             : 
     649      112036 :             if (cl->refcount > 0)
     650         144 :                 cl->dead = true;
     651             :             else
     652      111892 :                 CatCacheRemoveCList(cache, cl);
     653             :         }
     654             :     }
     655             : 
     656             :     /*
     657             :      * inspect the proper hash bucket for tuple matches
     658             :      */
     659    19547952 :     hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
     660    26504640 :     dlist_foreach_modify(iter, &cache->cc_bucket[hashIndex])
     661             :     {
     662     6956688 :         CatCTup    *ct = dlist_container(CatCTup, cache_elem, iter.cur);
     663             : 
     664     6956688 :         if (hashValue == ct->hash_value)
     665             :         {
     666     1270338 :             if (ct->refcount > 0 ||
     667     1269042 :                 (ct->c_list && ct->c_list->refcount > 0))
     668             :             {
     669        1440 :                 ct->dead = true;
     670             :                 /* list, if any, was marked dead above */
     671        1440 :                 Assert(ct->c_list == NULL || ct->c_list->dead);
     672             :             }
     673             :             else
     674     1268898 :                 CatCacheRemoveCTup(cache, ct);
     675             :             CACHE_elog(DEBUG2, "CatCacheInvalidate: invalidated");
     676             : #ifdef CATCACHE_STATS
     677             :             cache->cc_invals++;
     678             : #endif
     679             :             /* could be multiple matches, so keep looking! */
     680             :         }
     681             :     }
     682             : 
     683             :     /* Also invalidate any entries that are being built */
     684    19655628 :     for (CatCInProgress *e = catcache_in_progress_stack; e != NULL; e = e->next)
     685             :     {
     686      107676 :         if (e->cache == cache)
     687             :         {
     688         570 :             if (e->list || e->hash_value == hashValue)
     689         564 :                 e->dead = true;
     690             :         }
     691             :     }
     692    19547952 : }
     693             : 
     694             : /* ----------------------------------------------------------------
     695             :  *                     public functions
     696             :  * ----------------------------------------------------------------
     697             :  */
     698             : 
     699             : 
     700             : /*
     701             :  * Standard routine for creating cache context if it doesn't exist yet
     702             :  *
     703             :  * There are a lot of places (probably far more than necessary) that check
     704             :  * whether CacheMemoryContext exists yet and want to create it if not.
     705             :  * We centralize knowledge of exactly how to create it here.
     706             :  */
     707             : void
     708       30402 : CreateCacheMemoryContext(void)
     709             : {
     710             :     /*
     711             :      * Purely for paranoia, check that context doesn't exist; caller probably
     712             :      * did so already.
     713             :      */
     714       30402 :     if (!CacheMemoryContext)
     715       30402 :         CacheMemoryContext = AllocSetContextCreate(TopMemoryContext,
     716             :                                                    "CacheMemoryContext",
     717             :                                                    ALLOCSET_DEFAULT_SIZES);
     718       30402 : }
     719             : 
     720             : 
     721             : /*
     722             :  *      ResetCatalogCache
     723             :  *
     724             :  * Reset one catalog cache to empty.
     725             :  *
     726             :  * This is not very efficient if the target cache is nearly empty.
     727             :  * However, it shouldn't need to be efficient; we don't invoke it often.
     728             :  *
     729             :  * If 'debug_discard' is true, we are being called as part of
     730             :  * debug_discard_caches.  In that case, the cache is not reset for
     731             :  * correctness, but just to get more testing of cache invalidation.  We skip
     732             :  * resetting in-progress build entries in that case, or we'd never make any
     733             :  * progress.
     734             :  */
     735             : static void
     736      346216 : ResetCatalogCache(CatCache *cache, bool debug_discard)
     737             : {
     738             :     dlist_mutable_iter iter;
     739             :     int         i;
     740             : 
     741             :     /* Remove each list in this cache, or at least mark it dead */
     742      386984 :     for (i = 0; i < cache->cc_nlbuckets; i++)
     743             :     {
     744       40768 :         dlist_head *bucket = &cache->cc_lbucket[i];
     745             : 
     746       45050 :         dlist_foreach_modify(iter, bucket)
     747             :         {
     748        4282 :             CatCList   *cl = dlist_container(CatCList, cache_elem, iter.cur);
     749             : 
     750        4282 :             if (cl->refcount > 0)
     751           0 :                 cl->dead = true;
     752             :             else
     753        4282 :                 CatCacheRemoveCList(cache, cl);
     754             :         }
     755             :     }
     756             : 
     757             :     /* Remove each tuple in this cache, or at least mark it dead */
     758    10359664 :     for (i = 0; i < cache->cc_nbuckets; i++)
     759             :     {
     760    10013448 :         dlist_head *bucket = &cache->cc_bucket[i];
     761             : 
     762    10171450 :         dlist_foreach_modify(iter, bucket)
     763             :         {
     764      158002 :             CatCTup    *ct = dlist_container(CatCTup, cache_elem, iter.cur);
     765             : 
     766      158002 :             if (ct->refcount > 0 ||
     767      157996 :                 (ct->c_list && ct->c_list->refcount > 0))
     768             :             {
     769           6 :                 ct->dead = true;
     770             :                 /* list, if any, was marked dead above */
     771           6 :                 Assert(ct->c_list == NULL || ct->c_list->dead);
     772             :             }
     773             :             else
     774      157996 :                 CatCacheRemoveCTup(cache, ct);
     775             : #ifdef CATCACHE_STATS
     776             :             cache->cc_invals++;
     777             : #endif
     778             :         }
     779             :     }
     780             : 
     781             :     /* Also invalidate any entries that are being built */
     782      346216 :     if (!debug_discard)
     783             :     {
     784      346218 :         for (CatCInProgress *e = catcache_in_progress_stack; e != NULL; e = e->next)
     785             :         {
     786           2 :             if (e->cache == cache)
     787           0 :                 e->dead = true;
     788             :         }
     789             :     }
     790      346216 : }
     791             : 
     792             : /*
     793             :  *      ResetCatalogCaches
     794             :  *
     795             :  * Reset all caches when a shared cache inval event forces it
     796             :  */
     797             : void
     798           0 : ResetCatalogCaches(void)
     799             : {
     800           0 :     ResetCatalogCachesExt(false);
     801           0 : }
     802             : 
     803             : void
     804        4062 : ResetCatalogCachesExt(bool debug_discard)
     805             : {
     806             :     slist_iter  iter;
     807             : 
     808             :     CACHE_elog(DEBUG2, "ResetCatalogCaches called");
     809             : 
     810      349332 :     slist_foreach(iter, &CacheHdr->ch_caches)
     811             :     {
     812      345270 :         CatCache   *cache = slist_container(CatCache, cc_next, iter.cur);
     813             : 
     814      345270 :         ResetCatalogCache(cache, debug_discard);
     815             :     }
     816             : 
     817             :     CACHE_elog(DEBUG2, "end of ResetCatalogCaches call");
     818        4062 : }
     819             : 
     820             : /*
     821             :  *      CatalogCacheFlushCatalog
     822             :  *
     823             :  *  Flush all catcache entries that came from the specified system catalog.
     824             :  *  This is needed after VACUUM FULL/CLUSTER on the catalog, since the
     825             :  *  tuples very likely now have different TIDs than before.  (At one point
     826             :  *  we also tried to force re-execution of CatalogCacheInitializeCache for
     827             :  *  the cache(s) on that catalog.  This is a bad idea since it leads to all
     828             :  *  kinds of trouble if a cache flush occurs while loading cache entries.
     829             :  *  We now avoid the need to do it by copying cc_tupdesc out of the relcache,
     830             :  *  rather than relying on the relcache to keep a tupdesc for us.  Of course
     831             :  *  this assumes the tupdesc of a cachable system table will not change...)
     832             :  */
     833             : void
     834         728 : CatalogCacheFlushCatalog(Oid catId)
     835             : {
     836             :     slist_iter  iter;
     837             : 
     838             :     CACHE_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
     839             : 
     840       62608 :     slist_foreach(iter, &CacheHdr->ch_caches)
     841             :     {
     842       61880 :         CatCache   *cache = slist_container(CatCache, cc_next, iter.cur);
     843             : 
     844             :         /* Does this cache store tuples of the target catalog? */
     845       61880 :         if (cache->cc_reloid == catId)
     846             :         {
     847             :             /* Yes, so flush all its contents */
     848         946 :             ResetCatalogCache(cache, false);
     849             : 
     850             :             /* Tell inval.c to call syscache callbacks for this cache */
     851         946 :             CallSyscacheCallbacks(cache->id, 0);
     852             :         }
     853             :     }
     854             : 
     855             :     CACHE_elog(DEBUG2, "end of CatalogCacheFlushCatalog call");
     856         728 : }
     857             : 
     858             : /*
     859             :  *      InitCatCache
     860             :  *
     861             :  *  This allocates and initializes a cache for a system catalog relation.
     862             :  *  Actually, the cache is only partially initialized to avoid opening the
     863             :  *  relation.  The relation will be opened and the rest of the cache
     864             :  *  structure initialized on the first access.
     865             :  */
     866             : #ifdef CACHEDEBUG
     867             : #define InitCatCache_DEBUG2 \
     868             : do { \
     869             :     elog(DEBUG2, "InitCatCache: rel=%u ind=%u id=%d nkeys=%d size=%d", \
     870             :          cp->cc_reloid, cp->cc_indexoid, cp->id, \
     871             :          cp->cc_nkeys, cp->cc_nbuckets); \
     872             : } while(0)
     873             : #else
     874             : #define InitCatCache_DEBUG2
     875             : #endif
     876             : 
     877             : CatCache *
     878     2584170 : InitCatCache(int id,
     879             :              Oid reloid,
     880             :              Oid indexoid,
     881             :              int nkeys,
     882             :              const int *key,
     883             :              int nbuckets)
     884             : {
     885             :     CatCache   *cp;
     886             :     MemoryContext oldcxt;
     887             :     int         i;
     888             : 
     889             :     /*
     890             :      * nbuckets is the initial number of hash buckets to use in this catcache.
     891             :      * It will be enlarged later if it becomes too full.
     892             :      *
     893             :      * nbuckets must be a power of two.  We check this via Assert rather than
     894             :      * a full runtime check because the values will be coming from constant
     895             :      * tables.
     896             :      *
     897             :      * If you're confused by the power-of-two check, see comments in
     898             :      * bitmapset.c for an explanation.
     899             :      */
     900             :     Assert(nbuckets > 0 && (nbuckets & -nbuckets) == nbuckets);
     901             : 
     902             :     /*
     903             :      * first switch to the cache context so our allocations do not vanish at
     904             :      * the end of a transaction
     905             :      */
     906     2584170 :     if (!CacheMemoryContext)
     907           0 :         CreateCacheMemoryContext();
     908             : 
     909     2584170 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
     910             : 
     911             :     /*
     912             :      * if first time through, initialize the cache group header
     913             :      */
     914     2584170 :     if (CacheHdr == NULL)
     915             :     {
     916       30402 :         CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
     917       30402 :         slist_init(&CacheHdr->ch_caches);
     918       30402 :         CacheHdr->ch_ntup = 0;
     919             : #ifdef CATCACHE_STATS
     920             :         /* set up to dump stats at backend exit */
     921             :         on_proc_exit(CatCachePrintStats, 0);
     922             : #endif
     923             :     }
     924             : 
     925             :     /*
     926             :      * Allocate a new cache structure, aligning to a cacheline boundary
     927             :      *
     928             :      * Note: we rely on zeroing to initialize all the dlist headers correctly
     929             :      */
     930     2584170 :     cp = (CatCache *) palloc_aligned(sizeof(CatCache), PG_CACHE_LINE_SIZE,
     931             :                                      MCXT_ALLOC_ZERO);
     932     2584170 :     cp->cc_bucket = palloc0(nbuckets * sizeof(dlist_head));
     933             : 
     934             :     /*
     935             :      * Many catcaches never receive any list searches.  Therefore, we don't
     936             :      * allocate the cc_lbuckets till we get a list search.
     937             :      */
     938     2584170 :     cp->cc_lbucket = NULL;
     939             : 
     940             :     /*
     941             :      * initialize the cache's relation information for the relation
     942             :      * corresponding to this cache, and initialize some of the new cache's
     943             :      * other internal fields.  But don't open the relation yet.
     944             :      */
     945     2584170 :     cp->id = id;
     946     2584170 :     cp->cc_relname = "(not known yet)";
     947     2584170 :     cp->cc_reloid = reloid;
     948     2584170 :     cp->cc_indexoid = indexoid;
     949     2584170 :     cp->cc_relisshared = false; /* temporary */
     950     2584170 :     cp->cc_tupdesc = (TupleDesc) NULL;
     951     2584170 :     cp->cc_ntup = 0;
     952     2584170 :     cp->cc_nlist = 0;
     953     2584170 :     cp->cc_nbuckets = nbuckets;
     954     2584170 :     cp->cc_nlbuckets = 0;
     955     2584170 :     cp->cc_nkeys = nkeys;
     956     6749244 :     for (i = 0; i < nkeys; ++i)
     957             :     {
     958             :         Assert(AttributeNumberIsValid(key[i]));
     959     4165074 :         cp->cc_keyno[i] = key[i];
     960             :     }
     961             : 
     962             :     /*
     963             :      * new cache is initialized as far as we can go for now. print some
     964             :      * debugging information, if appropriate.
     965             :      */
     966             :     InitCatCache_DEBUG2;
     967             : 
     968             :     /*
     969             :      * add completed cache to top of group header's list
     970             :      */
     971     2584170 :     slist_push_head(&CacheHdr->ch_caches, &cp->cc_next);
     972             : 
     973             :     /*
     974             :      * back to the old context before we return...
     975             :      */
     976     2584170 :     MemoryContextSwitchTo(oldcxt);
     977             : 
     978     2584170 :     return cp;
     979             : }
     980             : 
     981             : /*
     982             :  * Enlarge a catcache, doubling the number of buckets.
     983             :  */
     984             : static void
     985        5478 : RehashCatCache(CatCache *cp)
     986             : {
     987             :     dlist_head *newbucket;
     988             :     int         newnbuckets;
     989             :     int         i;
     990             : 
     991        5478 :     elog(DEBUG1, "rehashing catalog cache id %d for %s; %d tups, %d buckets",
     992             :          cp->id, cp->cc_relname, cp->cc_ntup, cp->cc_nbuckets);
     993             : 
     994             :     /* Allocate a new, larger, hash table. */
     995        5478 :     newnbuckets = cp->cc_nbuckets * 2;
     996        5478 :     newbucket = (dlist_head *) MemoryContextAllocZero(CacheMemoryContext, newnbuckets * sizeof(dlist_head));
     997             : 
     998             :     /* Move all entries from old hash table to new. */
     999      462150 :     for (i = 0; i < cp->cc_nbuckets; i++)
    1000             :     {
    1001             :         dlist_mutable_iter iter;
    1002             : 
    1003     1375494 :         dlist_foreach_modify(iter, &cp->cc_bucket[i])
    1004             :         {
    1005      918822 :             CatCTup    *ct = dlist_container(CatCTup, cache_elem, iter.cur);
    1006      918822 :             int         hashIndex = HASH_INDEX(ct->hash_value, newnbuckets);
    1007             : 
    1008      918822 :             dlist_delete(iter.cur);
    1009      918822 :             dlist_push_head(&newbucket[hashIndex], &ct->cache_elem);
    1010             :         }
    1011             :     }
    1012             : 
    1013             :     /* Switch to the new array. */
    1014        5478 :     pfree(cp->cc_bucket);
    1015        5478 :     cp->cc_nbuckets = newnbuckets;
    1016        5478 :     cp->cc_bucket = newbucket;
    1017        5478 : }
    1018             : 
    1019             : /*
    1020             :  * Enlarge a catcache's list storage, doubling the number of buckets.
    1021             :  */
    1022             : static void
    1023        1130 : RehashCatCacheLists(CatCache *cp)
    1024             : {
    1025             :     dlist_head *newbucket;
    1026             :     int         newnbuckets;
    1027             :     int         i;
    1028             : 
    1029        1130 :     elog(DEBUG1, "rehashing catalog cache id %d for %s; %d lists, %d buckets",
    1030             :          cp->id, cp->cc_relname, cp->cc_nlist, cp->cc_nlbuckets);
    1031             : 
    1032             :     /* Allocate a new, larger, hash table. */
    1033        1130 :     newnbuckets = cp->cc_nlbuckets * 2;
    1034        1130 :     newbucket = (dlist_head *) MemoryContextAllocZero(CacheMemoryContext, newnbuckets * sizeof(dlist_head));
    1035             : 
    1036             :     /* Move all entries from old hash table to new. */
    1037       43594 :     for (i = 0; i < cp->cc_nlbuckets; i++)
    1038             :     {
    1039             :         dlist_mutable_iter iter;
    1040             : 
    1041      128522 :         dlist_foreach_modify(iter, &cp->cc_lbucket[i])
    1042             :         {
    1043       86058 :             CatCList   *cl = dlist_container(CatCList, cache_elem, iter.cur);
    1044       86058 :             int         hashIndex = HASH_INDEX(cl->hash_value, newnbuckets);
    1045             : 
    1046       86058 :             dlist_delete(iter.cur);
    1047       86058 :             dlist_push_head(&newbucket[hashIndex], &cl->cache_elem);
    1048             :         }
    1049             :     }
    1050             : 
    1051             :     /* Switch to the new array. */
    1052        1130 :     pfree(cp->cc_lbucket);
    1053        1130 :     cp->cc_nlbuckets = newnbuckets;
    1054        1130 :     cp->cc_lbucket = newbucket;
    1055        1130 : }
    1056             : 
    1057             : /*
    1058             :  *      CatalogCacheInitializeCache
    1059             :  *
    1060             :  * This function does final initialization of a catcache: obtain the tuple
    1061             :  * descriptor and set up the hash and equality function links.  We assume
    1062             :  * that the relcache entry can be opened at this point!
    1063             :  */
    1064             : #ifdef CACHEDEBUG
    1065             : #define CatalogCacheInitializeCache_DEBUG1 \
    1066             :     elog(DEBUG2, "CatalogCacheInitializeCache: cache @%p rel=%u", cache, \
    1067             :          cache->cc_reloid)
    1068             : 
    1069             : #define CatalogCacheInitializeCache_DEBUG2 \
    1070             : do { \
    1071             :         if (cache->cc_keyno[i] > 0) { \
    1072             :             elog(DEBUG2, "CatalogCacheInitializeCache: load %d/%d w/%d, %u", \
    1073             :                 i+1, cache->cc_nkeys, cache->cc_keyno[i], \
    1074             :                  TupleDescAttr(tupdesc, cache->cc_keyno[i] - 1)->atttypid); \
    1075             :         } else { \
    1076             :             elog(DEBUG2, "CatalogCacheInitializeCache: load %d/%d w/%d", \
    1077             :                 i+1, cache->cc_nkeys, cache->cc_keyno[i]); \
    1078             :         } \
    1079             : } while(0)
    1080             : #else
    1081             : #define CatalogCacheInitializeCache_DEBUG1
    1082             : #define CatalogCacheInitializeCache_DEBUG2
    1083             : #endif
    1084             : 
    1085             : static void
    1086      664592 : CatalogCacheInitializeCache(CatCache *cache)
    1087             : {
    1088             :     Relation    relation;
    1089             :     MemoryContext oldcxt;
    1090             :     TupleDesc   tupdesc;
    1091             :     int         i;
    1092             : 
    1093             :     CatalogCacheInitializeCache_DEBUG1;
    1094             : 
    1095      664592 :     relation = table_open(cache->cc_reloid, AccessShareLock);
    1096             : 
    1097             :     /*
    1098             :      * switch to the cache context so our allocations do not vanish at the end
    1099             :      * of a transaction
    1100             :      */
    1101             :     Assert(CacheMemoryContext != NULL);
    1102             : 
    1103      664588 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    1104             : 
    1105             :     /*
    1106             :      * copy the relcache's tuple descriptor to permanent cache storage
    1107             :      */
    1108      664588 :     tupdesc = CreateTupleDescCopyConstr(RelationGetDescr(relation));
    1109             : 
    1110             :     /*
    1111             :      * save the relation's name and relisshared flag, too (cc_relname is used
    1112             :      * only for debugging purposes)
    1113             :      */
    1114      664588 :     cache->cc_relname = pstrdup(RelationGetRelationName(relation));
    1115      664588 :     cache->cc_relisshared = RelationGetForm(relation)->relisshared;
    1116             : 
    1117             :     /*
    1118             :      * return to the caller's memory context and close the rel
    1119             :      */
    1120      664588 :     MemoryContextSwitchTo(oldcxt);
    1121             : 
    1122      664588 :     table_close(relation, AccessShareLock);
    1123             : 
    1124             :     CACHE_elog(DEBUG2, "CatalogCacheInitializeCache: %s, %d keys",
    1125             :                cache->cc_relname, cache->cc_nkeys);
    1126             : 
    1127             :     /*
    1128             :      * initialize cache's key information
    1129             :      */
    1130     1718566 :     for (i = 0; i < cache->cc_nkeys; ++i)
    1131             :     {
    1132             :         Oid         keytype;
    1133             :         RegProcedure eqfunc;
    1134             : 
    1135             :         CatalogCacheInitializeCache_DEBUG2;
    1136             : 
    1137     1053978 :         if (cache->cc_keyno[i] > 0)
    1138             :         {
    1139     1053978 :             Form_pg_attribute attr = TupleDescAttr(tupdesc,
    1140     1053978 :                                                    cache->cc_keyno[i] - 1);
    1141             : 
    1142     1053978 :             keytype = attr->atttypid;
    1143             :             /* cache key columns should always be NOT NULL */
    1144             :             Assert(attr->attnotnull);
    1145             :         }
    1146             :         else
    1147             :         {
    1148           0 :             if (cache->cc_keyno[i] < 0)
    1149           0 :                 elog(FATAL, "sys attributes are not supported in caches");
    1150           0 :             keytype = OIDOID;
    1151             :         }
    1152             : 
    1153     1053978 :         GetCCHashEqFuncs(keytype,
    1154             :                          &cache->cc_hashfunc[i],
    1155             :                          &eqfunc,
    1156             :                          &cache->cc_fastequal[i]);
    1157             : 
    1158             :         /*
    1159             :          * Do equality-function lookup (we assume this won't need a catalog
    1160             :          * lookup for any supported type)
    1161             :          */
    1162     1053978 :         fmgr_info_cxt(eqfunc,
    1163             :                       &cache->cc_skey[i].sk_func,
    1164             :                       CacheMemoryContext);
    1165             : 
    1166             :         /* Initialize sk_attno suitably for HeapKeyTest() and heap scans */
    1167     1053978 :         cache->cc_skey[i].sk_attno = cache->cc_keyno[i];
    1168             : 
    1169             :         /* Fill in sk_strategy as well --- always standard equality */
    1170     1053978 :         cache->cc_skey[i].sk_strategy = BTEqualStrategyNumber;
    1171     1053978 :         cache->cc_skey[i].sk_subtype = InvalidOid;
    1172             :         /* If a catcache key requires a collation, it must be C collation */
    1173     1053978 :         cache->cc_skey[i].sk_collation = C_COLLATION_OID;
    1174             : 
    1175             :         CACHE_elog(DEBUG2, "CatalogCacheInitializeCache %s %d %p",
    1176             :                    cache->cc_relname, i, cache);
    1177             :     }
    1178             : 
    1179             :     /*
    1180             :      * mark this cache fully initialized
    1181             :      */
    1182      664588 :     cache->cc_tupdesc = tupdesc;
    1183      664588 : }
    1184             : 
    1185             : /*
    1186             :  * InitCatCachePhase2 -- external interface for CatalogCacheInitializeCache
    1187             :  *
    1188             :  * One reason to call this routine is to ensure that the relcache has
    1189             :  * created entries for all the catalogs and indexes referenced by catcaches.
    1190             :  * Therefore, provide an option to open the index as well as fixing the
    1191             :  * cache itself.  An exception is the indexes on pg_am, which we don't use
    1192             :  * (cf. IndexScanOK).
    1193             :  */
    1194             : void
    1195      279612 : InitCatCachePhase2(CatCache *cache, bool touch_index)
    1196             : {
    1197      279612 :     if (cache->cc_tupdesc == NULL)
    1198      260264 :         CatalogCacheInitializeCache(cache);
    1199             : 
    1200      279608 :     if (touch_index &&
    1201      250674 :         cache->id != AMOID &&
    1202      247724 :         cache->id != AMNAME)
    1203             :     {
    1204             :         Relation    idesc;
    1205             : 
    1206             :         /*
    1207             :          * We must lock the underlying catalog before opening the index to
    1208             :          * avoid deadlock, since index_open could possibly result in reading
    1209             :          * this same catalog, and if anyone else is exclusive-locking this
    1210             :          * catalog and index they'll be doing it in that order.
    1211             :          */
    1212      244774 :         LockRelationOid(cache->cc_reloid, AccessShareLock);
    1213      244774 :         idesc = index_open(cache->cc_indexoid, AccessShareLock);
    1214             : 
    1215             :         /*
    1216             :          * While we've got the index open, let's check that it's unique (and
    1217             :          * not just deferrable-unique, thank you very much).  This is just to
    1218             :          * catch thinkos in definitions of new catcaches, so we don't worry
    1219             :          * about the pg_am indexes not getting tested.
    1220             :          */
    1221             :         Assert(idesc->rd_index->indisunique &&
    1222             :                idesc->rd_index->indimmediate);
    1223             : 
    1224      244774 :         index_close(idesc, AccessShareLock);
    1225      244774 :         UnlockRelationOid(cache->cc_reloid, AccessShareLock);
    1226             :     }
    1227      279608 : }
    1228             : 
    1229             : 
    1230             : /*
    1231             :  *      IndexScanOK
    1232             :  *
    1233             :  *      This function checks for tuples that will be fetched by
    1234             :  *      IndexSupportInitialize() during relcache initialization for
    1235             :  *      certain system indexes that support critical syscaches.
    1236             :  *      We can't use an indexscan to fetch these, else we'll get into
    1237             :  *      infinite recursion.  A plain heap scan will work, however.
    1238             :  *      Once we have completed relcache initialization (signaled by
    1239             :  *      criticalRelcachesBuilt), we don't have to worry anymore.
    1240             :  *
    1241             :  *      Similarly, during backend startup we have to be able to use the
    1242             :  *      pg_authid, pg_auth_members and pg_database syscaches for
    1243             :  *      authentication even if we don't yet have relcache entries for those
    1244             :  *      catalogs' indexes.
    1245             :  */
    1246             : static bool
    1247     5641050 : IndexScanOK(CatCache *cache)
    1248             : {
    1249     5641050 :     switch (cache->id)
    1250             :     {
    1251      554770 :         case INDEXRELID:
    1252             : 
    1253             :             /*
    1254             :              * Rather than tracking exactly which indexes have to be loaded
    1255             :              * before we can use indexscans (which changes from time to time),
    1256             :              * just force all pg_index searches to be heap scans until we've
    1257             :              * built the critical relcaches.
    1258             :              */
    1259      554770 :             if (!criticalRelcachesBuilt)
    1260       32742 :                 return false;
    1261      522028 :             break;
    1262             : 
    1263       54598 :         case AMOID:
    1264             :         case AMNAME:
    1265             : 
    1266             :             /*
    1267             :              * Always do heap scans in pg_am, because it's so small there's
    1268             :              * not much point in an indexscan anyway.  We *must* do this when
    1269             :              * initially building critical relcache entries, but we might as
    1270             :              * well just always do it.
    1271             :              */
    1272       54598 :             return false;
    1273             : 
    1274       98640 :         case AUTHNAME:
    1275             :         case AUTHOID:
    1276             :         case AUTHMEMMEMROLE:
    1277             :         case DATABASEOID:
    1278             : 
    1279             :             /*
    1280             :              * Protect authentication lookups occurring before relcache has
    1281             :              * collected entries for shared indexes.
    1282             :              */
    1283       98640 :             if (!criticalSharedRelcachesBuilt)
    1284        4082 :                 return false;
    1285       94558 :             break;
    1286             : 
    1287     4933042 :         default:
    1288     4933042 :             break;
    1289             :     }
    1290             : 
    1291             :     /* Normal case, allow index scan */
    1292     5549628 :     return true;
    1293             : }
    1294             : 
    1295             : /*
    1296             :  *  SearchCatCache
    1297             :  *
    1298             :  *      This call searches a system cache for a tuple, opening the relation
    1299             :  *      if necessary (on the first access to a particular cache).
    1300             :  *
    1301             :  *      The result is NULL if not found, or a pointer to a HeapTuple in
    1302             :  *      the cache.  The caller must not modify the tuple, and must call
    1303             :  *      ReleaseCatCache() when done with it.
    1304             :  *
    1305             :  * The search key values should be expressed as Datums of the key columns'
    1306             :  * datatype(s).  (Pass zeroes for any unused parameters.)  As a special
    1307             :  * exception, the passed-in key for a NAME column can be just a C string;
    1308             :  * the caller need not go to the trouble of converting it to a fully
    1309             :  * null-padded NAME.
    1310             :  */
    1311             : HeapTuple
    1312     5089292 : SearchCatCache(CatCache *cache,
    1313             :                Datum v1,
    1314             :                Datum v2,
    1315             :                Datum v3,
    1316             :                Datum v4)
    1317             : {
    1318     5089292 :     return SearchCatCacheInternal(cache, cache->cc_nkeys, v1, v2, v3, v4);
    1319             : }
    1320             : 
    1321             : 
    1322             : /*
    1323             :  * SearchCatCacheN() are SearchCatCache() versions for a specific number of
    1324             :  * arguments. The compiler can inline the body and unroll loops, making them a
    1325             :  * bit faster than SearchCatCache().
    1326             :  */
    1327             : 
    1328             : HeapTuple
    1329    62719942 : SearchCatCache1(CatCache *cache,
    1330             :                 Datum v1)
    1331             : {
    1332    62719942 :     return SearchCatCacheInternal(cache, 1, v1, 0, 0, 0);
    1333             : }
    1334             : 
    1335             : 
    1336             : HeapTuple
    1337     5052300 : SearchCatCache2(CatCache *cache,
    1338             :                 Datum v1, Datum v2)
    1339             : {
    1340     5052300 :     return SearchCatCacheInternal(cache, 2, v1, v2, 0, 0);
    1341             : }
    1342             : 
    1343             : 
    1344             : HeapTuple
    1345     4750300 : SearchCatCache3(CatCache *cache,
    1346             :                 Datum v1, Datum v2, Datum v3)
    1347             : {
    1348     4750300 :     return SearchCatCacheInternal(cache, 3, v1, v2, v3, 0);
    1349             : }
    1350             : 
    1351             : 
    1352             : HeapTuple
    1353     3953836 : SearchCatCache4(CatCache *cache,
    1354             :                 Datum v1, Datum v2, Datum v3, Datum v4)
    1355             : {
    1356     3953836 :     return SearchCatCacheInternal(cache, 4, v1, v2, v3, v4);
    1357             : }
    1358             : 
    1359             : /*
    1360             :  * Work-horse for SearchCatCache/SearchCatCacheN.
    1361             :  */
    1362             : static inline HeapTuple
    1363    81565670 : SearchCatCacheInternal(CatCache *cache,
    1364             :                        int nkeys,
    1365             :                        Datum v1,
    1366             :                        Datum v2,
    1367             :                        Datum v3,
    1368             :                        Datum v4)
    1369             : {
    1370             :     Datum       arguments[CATCACHE_MAXKEYS];
    1371             :     uint32      hashValue;
    1372             :     Index       hashIndex;
    1373             :     dlist_iter  iter;
    1374             :     dlist_head *bucket;
    1375             :     CatCTup    *ct;
    1376             : 
    1377             :     /* Make sure we're in an xact, even if this ends up being a cache hit */
    1378             :     Assert(IsTransactionState());
    1379             : 
    1380             :     Assert(cache->cc_nkeys == nkeys);
    1381             : 
    1382             :     /*
    1383             :      * one-time startup overhead for each cache
    1384             :      */
    1385    81565670 :     if (unlikely(cache->cc_tupdesc == NULL))
    1386      337758 :         CatalogCacheInitializeCache(cache);
    1387             : 
    1388             : #ifdef CATCACHE_STATS
    1389             :     cache->cc_searches++;
    1390             : #endif
    1391             : 
    1392             :     /* Initialize local parameter array */
    1393    81565670 :     arguments[0] = v1;
    1394    81565670 :     arguments[1] = v2;
    1395    81565670 :     arguments[2] = v3;
    1396    81565670 :     arguments[3] = v4;
    1397             : 
    1398             :     /*
    1399             :      * find the hash bucket in which to look for the tuple
    1400             :      */
    1401    81565670 :     hashValue = CatalogCacheComputeHashValue(cache, nkeys, v1, v2, v3, v4);
    1402    81565670 :     hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
    1403             : 
    1404             :     /*
    1405             :      * scan the hash bucket until we find a match or exhaust our tuples
    1406             :      *
    1407             :      * Note: it's okay to use dlist_foreach here, even though we modify the
    1408             :      * dlist within the loop, because we don't continue the loop afterwards.
    1409             :      */
    1410    81565670 :     bucket = &cache->cc_bucket[hashIndex];
    1411    86930288 :     dlist_foreach(iter, bucket)
    1412             :     {
    1413    81566458 :         ct = dlist_container(CatCTup, cache_elem, iter.cur);
    1414             : 
    1415    81566458 :         if (ct->dead)
    1416           0 :             continue;           /* ignore dead entries */
    1417             : 
    1418    81566458 :         if (ct->hash_value != hashValue)
    1419     5364618 :             continue;           /* quickly skip entry if wrong hash val */
    1420             : 
    1421    76201840 :         if (!CatalogCacheCompareTuple(cache, nkeys, ct->keys, arguments))
    1422           0 :             continue;
    1423             : 
    1424             :         /*
    1425             :          * We found a match in the cache.  Move it to the front of the list
    1426             :          * for its hashbucket, in order to speed subsequent searches.  (The
    1427             :          * most frequently accessed elements in any hashbucket will tend to be
    1428             :          * near the front of the hashbucket's list.)
    1429             :          */
    1430    76201840 :         dlist_move_head(bucket, &ct->cache_elem);
    1431             : 
    1432             :         /*
    1433             :          * If it's a positive entry, bump its refcount and return it. If it's
    1434             :          * negative, we can report failure to the caller.
    1435             :          */
    1436    76201840 :         if (!ct->negative)
    1437             :         {
    1438    72430204 :             ResourceOwnerEnlarge(CurrentResourceOwner);
    1439    72430204 :             ct->refcount++;
    1440    72430204 :             ResourceOwnerRememberCatCacheRef(CurrentResourceOwner, &ct->tuple);
    1441             : 
    1442             :             CACHE_elog(DEBUG2, "SearchCatCache(%s): found in bucket %d",
    1443             :                        cache->cc_relname, hashIndex);
    1444             : 
    1445             : #ifdef CATCACHE_STATS
    1446             :             cache->cc_hits++;
    1447             : #endif
    1448             : 
    1449    72430204 :             return &ct->tuple;
    1450             :         }
    1451             :         else
    1452             :         {
    1453             :             CACHE_elog(DEBUG2, "SearchCatCache(%s): found neg entry in bucket %d",
    1454             :                        cache->cc_relname, hashIndex);
    1455             : 
    1456             : #ifdef CATCACHE_STATS
    1457             :             cache->cc_neg_hits++;
    1458             : #endif
    1459             : 
    1460     3771636 :             return NULL;
    1461             :         }
    1462             :     }
    1463             : 
    1464     5363830 :     return SearchCatCacheMiss(cache, nkeys, hashValue, hashIndex, v1, v2, v3, v4);
    1465             : }
    1466             : 
    1467             : /*
    1468             :  * Search the actual catalogs, rather than the cache.
    1469             :  *
    1470             :  * This is kept separate from SearchCatCacheInternal() to keep the fast-path
    1471             :  * as small as possible.  To avoid that effort being undone by a helpful
    1472             :  * compiler, try to explicitly forbid inlining.
    1473             :  */
    1474             : static pg_noinline HeapTuple
    1475     5363830 : SearchCatCacheMiss(CatCache *cache,
    1476             :                    int nkeys,
    1477             :                    uint32 hashValue,
    1478             :                    Index hashIndex,
    1479             :                    Datum v1,
    1480             :                    Datum v2,
    1481             :                    Datum v3,
    1482             :                    Datum v4)
    1483             : {
    1484             :     ScanKeyData cur_skey[CATCACHE_MAXKEYS];
    1485             :     Relation    relation;
    1486             :     SysScanDesc scandesc;
    1487             :     HeapTuple   ntp;
    1488             :     CatCTup    *ct;
    1489             :     bool        stale;
    1490             :     Datum       arguments[CATCACHE_MAXKEYS];
    1491             : 
    1492             :     /* Initialize local parameter array */
    1493     5363830 :     arguments[0] = v1;
    1494     5363830 :     arguments[1] = v2;
    1495     5363830 :     arguments[2] = v3;
    1496     5363830 :     arguments[3] = v4;
    1497             : 
    1498             :     /*
    1499             :      * Tuple was not found in cache, so we have to try to retrieve it directly
    1500             :      * from the relation.  If found, we will add it to the cache; if not
    1501             :      * found, we will add a negative cache entry instead.
    1502             :      *
    1503             :      * NOTE: it is possible for recursive cache lookups to occur while reading
    1504             :      * the relation --- for example, due to shared-cache-inval messages being
    1505             :      * processed during table_open().  This is OK.  It's even possible for one
    1506             :      * of those lookups to find and enter the very same tuple we are trying to
    1507             :      * fetch here.  If that happens, we will enter a second copy of the tuple
    1508             :      * into the cache.  The first copy will never be referenced again, and
    1509             :      * will eventually age out of the cache, so there's no functional problem.
    1510             :      * This case is rare enough that it's not worth expending extra cycles to
    1511             :      * detect.
    1512             :      *
    1513             :      * Another case, which we *must* handle, is that the tuple could become
    1514             :      * outdated during CatalogCacheCreateEntry's attempt to detoast it (since
    1515             :      * AcceptInvalidationMessages can run during TOAST table access).  We do
    1516             :      * not want to return already-stale catcache entries, so we loop around
    1517             :      * and do the table scan again if that happens.
    1518             :      */
    1519     5363830 :     relation = table_open(cache->cc_reloid, AccessShareLock);
    1520             : 
    1521             :     /*
    1522             :      * Ok, need to make a lookup in the relation, copy the scankey and fill
    1523             :      * out any per-call fields.
    1524             :      */
    1525     5363830 :     memcpy(cur_skey, cache->cc_skey, sizeof(ScanKeyData) * nkeys);
    1526     5363830 :     cur_skey[0].sk_argument = v1;
    1527     5363830 :     cur_skey[1].sk_argument = v2;
    1528     5363830 :     cur_skey[2].sk_argument = v3;
    1529     5363830 :     cur_skey[3].sk_argument = v4;
    1530             : 
    1531             :     do
    1532             :     {
    1533     5363830 :         scandesc = systable_beginscan(relation,
    1534             :                                       cache->cc_indexoid,
    1535     5363830 :                                       IndexScanOK(cache),
    1536             :                                       NULL,
    1537             :                                       nkeys,
    1538             :                                       cur_skey);
    1539             : 
    1540     5363830 :         ct = NULL;
    1541     5363830 :         stale = false;
    1542             : 
    1543     5363830 :         while (HeapTupleIsValid(ntp = systable_getnext(scandesc)))
    1544             :         {
    1545     3811274 :             ct = CatalogCacheCreateEntry(cache, ntp, NULL,
    1546             :                                          hashValue, hashIndex);
    1547             :             /* upon failure, we must start the scan over */
    1548     3811274 :             if (ct == NULL)
    1549             :             {
    1550           0 :                 stale = true;
    1551           0 :                 break;
    1552             :             }
    1553             :             /* immediately set the refcount to 1 */
    1554     3811274 :             ResourceOwnerEnlarge(CurrentResourceOwner);
    1555     3811274 :             ct->refcount++;
    1556     3811274 :             ResourceOwnerRememberCatCacheRef(CurrentResourceOwner, &ct->tuple);
    1557     3811274 :             break;              /* assume only one match */
    1558             :         }
    1559             : 
    1560     5363828 :         systable_endscan(scandesc);
    1561     5363828 :     } while (stale);
    1562             : 
    1563     5363828 :     table_close(relation, AccessShareLock);
    1564             : 
    1565             :     /*
    1566             :      * If tuple was not found, we need to build a negative cache entry
    1567             :      * containing a fake tuple.  The fake tuple has the correct key columns,
    1568             :      * but nulls everywhere else.
    1569             :      *
    1570             :      * In bootstrap mode, we don't build negative entries, because the cache
    1571             :      * invalidation mechanism isn't alive and can't clear them if the tuple
    1572             :      * gets created later.  (Bootstrap doesn't do UPDATEs, so it doesn't need
    1573             :      * cache inval for that.)
    1574             :      */
    1575     5363828 :     if (ct == NULL)
    1576             :     {
    1577     1552554 :         if (IsBootstrapProcessingMode())
    1578       48870 :             return NULL;
    1579             : 
    1580     1503684 :         ct = CatalogCacheCreateEntry(cache, NULL, arguments,
    1581             :                                      hashValue, hashIndex);
    1582             : 
    1583             :         /* Creating a negative cache entry shouldn't fail */
    1584             :         Assert(ct != NULL);
    1585             : 
    1586             :         CACHE_elog(DEBUG2, "SearchCatCache(%s): Contains %d/%d tuples",
    1587             :                    cache->cc_relname, cache->cc_ntup, CacheHdr->ch_ntup);
    1588             :         CACHE_elog(DEBUG2, "SearchCatCache(%s): put neg entry in bucket %d",
    1589             :                    cache->cc_relname, hashIndex);
    1590             : 
    1591             :         /*
    1592             :          * We are not returning the negative entry to the caller, so leave its
    1593             :          * refcount zero.
    1594             :          */
    1595             : 
    1596     1503684 :         return NULL;
    1597             :     }
    1598             : 
    1599             :     CACHE_elog(DEBUG2, "SearchCatCache(%s): Contains %d/%d tuples",
    1600             :                cache->cc_relname, cache->cc_ntup, CacheHdr->ch_ntup);
    1601             :     CACHE_elog(DEBUG2, "SearchCatCache(%s): put in bucket %d",
    1602             :                cache->cc_relname, hashIndex);
    1603             : 
    1604             : #ifdef CATCACHE_STATS
    1605             :     cache->cc_newloads++;
    1606             : #endif
    1607             : 
    1608     3811274 :     return &ct->tuple;
    1609             : }
    1610             : 
    1611             : /*
    1612             :  *  ReleaseCatCache
    1613             :  *
    1614             :  *  Decrement the reference count of a catcache entry (releasing the
    1615             :  *  hold grabbed by a successful SearchCatCache).
    1616             :  *
    1617             :  *  NOTE: if compiled with -DCATCACHE_FORCE_RELEASE then catcache entries
    1618             :  *  will be freed as soon as their refcount goes to zero.  In combination
    1619             :  *  with aset.c's CLOBBER_FREED_MEMORY option, this provides a good test
    1620             :  *  to catch references to already-released catcache entries.
    1621             :  */
    1622             : void
    1623    76231096 : ReleaseCatCache(HeapTuple tuple)
    1624             : {
    1625    76231096 :     ReleaseCatCacheWithOwner(tuple, CurrentResourceOwner);
    1626    76231096 : }
    1627             : 
    1628             : static void
    1629    76241478 : ReleaseCatCacheWithOwner(HeapTuple tuple, ResourceOwner resowner)
    1630             : {
    1631    76241478 :     CatCTup    *ct = (CatCTup *) (((char *) tuple) -
    1632             :                                   offsetof(CatCTup, tuple));
    1633             : 
    1634             :     /* Safety checks to ensure we were handed a cache entry */
    1635             :     Assert(ct->ct_magic == CT_MAGIC);
    1636             :     Assert(ct->refcount > 0);
    1637             : 
    1638    76241478 :     ct->refcount--;
    1639    76241478 :     if (resowner)
    1640    76231096 :         ResourceOwnerForgetCatCacheRef(CurrentResourceOwner, &ct->tuple);
    1641             : 
    1642    76241478 :     if (
    1643             : #ifndef CATCACHE_FORCE_RELEASE
    1644    76241478 :         ct->dead &&
    1645             : #endif
    1646        1406 :         ct->refcount == 0 &&
    1647        1296 :         (ct->c_list == NULL || ct->c_list->refcount == 0))
    1648        1296 :         CatCacheRemoveCTup(ct->my_cache, ct);
    1649    76241478 : }
    1650             : 
    1651             : 
    1652             : /*
    1653             :  *  GetCatCacheHashValue
    1654             :  *
    1655             :  *      Compute the hash value for a given set of search keys.
    1656             :  *
    1657             :  * The reason for exposing this as part of the API is that the hash value is
    1658             :  * exposed in cache invalidation operations, so there are places outside the
    1659             :  * catcache code that need to be able to compute the hash values.
    1660             :  */
    1661             : uint32
    1662     1002064 : GetCatCacheHashValue(CatCache *cache,
    1663             :                      Datum v1,
    1664             :                      Datum v2,
    1665             :                      Datum v3,
    1666             :                      Datum v4)
    1667             : {
    1668             :     /*
    1669             :      * one-time startup overhead for each cache
    1670             :      */
    1671     1002064 :     if (cache->cc_tupdesc == NULL)
    1672       25286 :         CatalogCacheInitializeCache(cache);
    1673             : 
    1674             :     /*
    1675             :      * calculate the hash value
    1676             :      */
    1677     1002064 :     return CatalogCacheComputeHashValue(cache, cache->cc_nkeys, v1, v2, v3, v4);
    1678             : }
    1679             : 
    1680             : 
    1681             : /*
    1682             :  *  SearchCatCacheList
    1683             :  *
    1684             :  *      Generate a list of all tuples matching a partial key (that is,
    1685             :  *      a key specifying just the first K of the cache's N key columns).
    1686             :  *
    1687             :  *      It doesn't make any sense to specify all of the cache's key columns
    1688             :  *      here: since the key is unique, there could be at most one match, so
    1689             :  *      you ought to use SearchCatCache() instead.  Hence this function takes
    1690             :  *      one fewer Datum argument than SearchCatCache() does.
    1691             :  *
    1692             :  *      The caller must not modify the list object or the pointed-to tuples,
    1693             :  *      and must call ReleaseCatCacheList() when done with the list.
    1694             :  */
    1695             : CatCList *
    1696     3354618 : SearchCatCacheList(CatCache *cache,
    1697             :                    int nkeys,
    1698             :                    Datum v1,
    1699             :                    Datum v2,
    1700             :                    Datum v3)
    1701             : {
    1702     3354618 :     Datum       v4 = 0;         /* dummy last-column value */
    1703             :     Datum       arguments[CATCACHE_MAXKEYS];
    1704             :     uint32      lHashValue;
    1705             :     Index       lHashIndex;
    1706             :     dlist_iter  iter;
    1707             :     dlist_head *lbucket;
    1708             :     CatCList   *cl;
    1709             :     CatCTup    *ct;
    1710             :     List       *volatile ctlist;
    1711             :     ListCell   *ctlist_item;
    1712             :     int         nmembers;
    1713             :     bool        ordered;
    1714             :     HeapTuple   ntp;
    1715             :     MemoryContext oldcxt;
    1716             :     int         i;
    1717             :     CatCInProgress *save_in_progress;
    1718             :     CatCInProgress in_progress_ent;
    1719             : 
    1720             :     /*
    1721             :      * one-time startup overhead for each cache
    1722             :      */
    1723     3354618 :     if (unlikely(cache->cc_tupdesc == NULL))
    1724       32292 :         CatalogCacheInitializeCache(cache);
    1725             : 
    1726             :     Assert(nkeys > 0 && nkeys < cache->cc_nkeys);
    1727             : 
    1728             : #ifdef CATCACHE_STATS
    1729             :     cache->cc_lsearches++;
    1730             : #endif
    1731             : 
    1732             :     /* Initialize local parameter array */
    1733     3354618 :     arguments[0] = v1;
    1734     3354618 :     arguments[1] = v2;
    1735     3354618 :     arguments[2] = v3;
    1736     3354618 :     arguments[3] = v4;
    1737             : 
    1738             :     /*
    1739             :      * If we haven't previously done a list search in this cache, create the
    1740             :      * bucket header array; otherwise, consider whether it's time to enlarge
    1741             :      * it.
    1742             :      */
    1743     3354618 :     if (cache->cc_lbucket == NULL)
    1744             :     {
    1745             :         /* Arbitrary initial size --- must be a power of 2 */
    1746       36370 :         int         nbuckets = 16;
    1747             : 
    1748       36370 :         cache->cc_lbucket = (dlist_head *)
    1749       36370 :             MemoryContextAllocZero(CacheMemoryContext,
    1750             :                                    nbuckets * sizeof(dlist_head));
    1751             :         /* Don't set cc_nlbuckets if we get OOM allocating cc_lbucket */
    1752       36370 :         cache->cc_nlbuckets = nbuckets;
    1753             :     }
    1754             :     else
    1755             :     {
    1756             :         /*
    1757             :          * If the hash table has become too full, enlarge the buckets array.
    1758             :          * Quite arbitrarily, we enlarge when fill factor > 2.
    1759             :          */
    1760     3318248 :         if (cache->cc_nlist > cache->cc_nlbuckets * 2)
    1761        1130 :             RehashCatCacheLists(cache);
    1762             :     }
    1763             : 
    1764             :     /*
    1765             :      * Find the hash bucket in which to look for the CatCList.
    1766             :      */
    1767     3354618 :     lHashValue = CatalogCacheComputeHashValue(cache, nkeys, v1, v2, v3, v4);
    1768     3354618 :     lHashIndex = HASH_INDEX(lHashValue, cache->cc_nlbuckets);
    1769             : 
    1770             :     /*
    1771             :      * scan the items until we find a match or exhaust our list
    1772             :      *
    1773             :      * Note: it's okay to use dlist_foreach here, even though we modify the
    1774             :      * dlist within the loop, because we don't continue the loop afterwards.
    1775             :      */
    1776     3354618 :     lbucket = &cache->cc_lbucket[lHashIndex];
    1777     3703816 :     dlist_foreach(iter, lbucket)
    1778             :     {
    1779     3426610 :         cl = dlist_container(CatCList, cache_elem, iter.cur);
    1780             : 
    1781     3426610 :         if (cl->dead)
    1782           0 :             continue;           /* ignore dead entries */
    1783             : 
    1784     3426610 :         if (cl->hash_value != lHashValue)
    1785      349198 :             continue;           /* quickly skip entry if wrong hash val */
    1786             : 
    1787             :         /*
    1788             :          * see if the cached list matches our key.
    1789             :          */
    1790     3077412 :         if (cl->nkeys != nkeys)
    1791           0 :             continue;
    1792             : 
    1793     3077412 :         if (!CatalogCacheCompareTuple(cache, nkeys, cl->keys, arguments))
    1794           0 :             continue;
    1795             : 
    1796             :         /*
    1797             :          * We found a matching list.  Move the list to the front of the list
    1798             :          * for its hashbucket, so as to speed subsequent searches.  (We do not
    1799             :          * move the members to the fronts of their hashbucket lists, however,
    1800             :          * since there's no point in that unless they are searched for
    1801             :          * individually.)
    1802             :          */
    1803     3077412 :         dlist_move_head(lbucket, &cl->cache_elem);
    1804             : 
    1805             :         /* Bump the list's refcount and return it */
    1806     3077412 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    1807     3077412 :         cl->refcount++;
    1808     3077412 :         ResourceOwnerRememberCatCacheListRef(CurrentResourceOwner, cl);
    1809             : 
    1810             :         CACHE_elog(DEBUG2, "SearchCatCacheList(%s): found list",
    1811             :                    cache->cc_relname);
    1812             : 
    1813             : #ifdef CATCACHE_STATS
    1814             :         cache->cc_lhits++;
    1815             : #endif
    1816             : 
    1817     3077412 :         return cl;
    1818             :     }
    1819             : 
    1820             :     /*
    1821             :      * List was not found in cache, so we have to build it by reading the
    1822             :      * relation.  For each matching tuple found in the relation, use an
    1823             :      * existing cache entry if possible, else build a new one.
    1824             :      *
    1825             :      * We have to bump the member refcounts temporarily to ensure they won't
    1826             :      * get dropped from the cache while loading other members. We use a PG_TRY
    1827             :      * block to ensure we can undo those refcounts if we get an error before
    1828             :      * we finish constructing the CatCList.  ctlist must be valid throughout
    1829             :      * the PG_TRY block.
    1830             :      */
    1831      277206 :     ctlist = NIL;
    1832             : 
    1833             :     /*
    1834             :      * Cache invalidation can happen while we're building the list.
    1835             :      * CatalogCacheCreateEntry() handles concurrent invalidation of individual
    1836             :      * tuples, but it's also possible that a new entry is concurrently added
    1837             :      * that should be part of the list we're building.  Register an
    1838             :      * "in-progress" entry that will receive the invalidation, until we have
    1839             :      * built the final list entry.
    1840             :      */
    1841      277206 :     save_in_progress = catcache_in_progress_stack;
    1842      277206 :     in_progress_ent.next = catcache_in_progress_stack;
    1843      277206 :     in_progress_ent.cache = cache;
    1844      277206 :     in_progress_ent.hash_value = lHashValue;
    1845      277206 :     in_progress_ent.list = true;
    1846      277206 :     in_progress_ent.dead = false;
    1847      277206 :     catcache_in_progress_stack = &in_progress_ent;
    1848             : 
    1849      277206 :     PG_TRY();
    1850             :     {
    1851             :         ScanKeyData cur_skey[CATCACHE_MAXKEYS];
    1852             :         Relation    relation;
    1853             :         SysScanDesc scandesc;
    1854      277206 :         bool        first_iter = true;
    1855             : 
    1856      277206 :         relation = table_open(cache->cc_reloid, AccessShareLock);
    1857             : 
    1858             :         /*
    1859             :          * Ok, need to make a lookup in the relation, copy the scankey and
    1860             :          * fill out any per-call fields.
    1861             :          */
    1862      277206 :         memcpy(cur_skey, cache->cc_skey, sizeof(ScanKeyData) * cache->cc_nkeys);
    1863      277206 :         cur_skey[0].sk_argument = v1;
    1864      277206 :         cur_skey[1].sk_argument = v2;
    1865      277206 :         cur_skey[2].sk_argument = v3;
    1866      277206 :         cur_skey[3].sk_argument = v4;
    1867             : 
    1868             :         /*
    1869             :          * Scan the table for matching entries.  If an invalidation arrives
    1870             :          * mid-build, we will loop back here to retry.
    1871             :          */
    1872             :         do
    1873             :         {
    1874             :             /*
    1875             :              * If we are retrying, release refcounts on any items created on
    1876             :              * the previous iteration.  We dare not try to free them if
    1877             :              * they're now unreferenced, since an error while doing that would
    1878             :              * result in the PG_CATCH below doing extra refcount decrements.
    1879             :              * Besides, we'll likely re-adopt those items in the next
    1880             :              * iteration, so it's not worth complicating matters to try to get
    1881             :              * rid of them.
    1882             :              */
    1883      277222 :             foreach(ctlist_item, ctlist)
    1884             :             {
    1885           2 :                 ct = (CatCTup *) lfirst(ctlist_item);
    1886             :                 Assert(ct->c_list == NULL);
    1887             :                 Assert(ct->refcount > 0);
    1888           2 :                 ct->refcount--;
    1889             :             }
    1890             :             /* Reset ctlist in preparation for new try */
    1891      277220 :             ctlist = NIL;
    1892      277220 :             in_progress_ent.dead = false;
    1893             : 
    1894      554440 :             scandesc = systable_beginscan(relation,
    1895             :                                           cache->cc_indexoid,
    1896      277220 :                                           IndexScanOK(cache),
    1897             :                                           NULL,
    1898             :                                           nkeys,
    1899             :                                           cur_skey);
    1900             : 
    1901             :             /* The list will be ordered iff we are doing an index scan */
    1902      277220 :             ordered = (scandesc->irel != NULL);
    1903             : 
    1904             :             /* Injection point to help testing the recursive invalidation case */
    1905      277220 :             if (first_iter)
    1906             :             {
    1907      277206 :                 INJECTION_POINT("catcache-list-miss-systable-scan-started");
    1908      277206 :                 first_iter = false;
    1909             :             }
    1910             : 
    1911     1123122 :             while (HeapTupleIsValid(ntp = systable_getnext(scandesc)) &&
    1912      845914 :                    !in_progress_ent.dead)
    1913             :             {
    1914             :                 uint32      hashValue;
    1915             :                 Index       hashIndex;
    1916      845902 :                 bool        found = false;
    1917             :                 dlist_head *bucket;
    1918             : 
    1919             :                 /*
    1920             :                  * See if there's an entry for this tuple already.
    1921             :                  */
    1922      845902 :                 ct = NULL;
    1923      845902 :                 hashValue = CatalogCacheComputeTupleHashValue(cache, cache->cc_nkeys, ntp);
    1924      845902 :                 hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
    1925             : 
    1926      845902 :                 bucket = &cache->cc_bucket[hashIndex];
    1927     1174954 :                 dlist_foreach(iter, bucket)
    1928             :                 {
    1929      454656 :                     ct = dlist_container(CatCTup, cache_elem, iter.cur);
    1930             : 
    1931      454656 :                     if (ct->dead || ct->negative)
    1932         894 :                         continue;   /* ignore dead and negative entries */
    1933             : 
    1934      453762 :                     if (ct->hash_value != hashValue)
    1935      311508 :                         continue;   /* quickly skip entry if wrong hash val */
    1936             : 
    1937      142254 :                     if (!ItemPointerEquals(&(ct->tuple.t_self), &(ntp->t_self)))
    1938           0 :                         continue;   /* not same tuple */
    1939             : 
    1940             :                     /*
    1941             :                      * Found a match, but can't use it if it belongs to
    1942             :                      * another list already
    1943             :                      */
    1944      142254 :                     if (ct->c_list)
    1945       16650 :                         continue;
    1946             : 
    1947      125604 :                     found = true;
    1948      125604 :                     break;      /* A-OK */
    1949             :                 }
    1950             : 
    1951      845902 :                 if (!found)
    1952             :                 {
    1953             :                     /* We didn't find a usable entry, so make a new one */
    1954      720298 :                     ct = CatalogCacheCreateEntry(cache, ntp, NULL,
    1955             :                                                  hashValue, hashIndex);
    1956             : 
    1957             :                     /* upon failure, we must start the scan over */
    1958      720298 :                     if (ct == NULL)
    1959             :                     {
    1960           0 :                         in_progress_ent.dead = true;
    1961           0 :                         break;
    1962             :                     }
    1963             :                 }
    1964             : 
    1965             :                 /* Careful here: add entry to ctlist, then bump its refcount */
    1966             :                 /* This way leaves state correct if lappend runs out of memory */
    1967      845902 :                 ctlist = lappend(ctlist, ct);
    1968      845902 :                 ct->refcount++;
    1969             :             }
    1970             : 
    1971      277220 :             systable_endscan(scandesc);
    1972      277220 :         } while (in_progress_ent.dead);
    1973             : 
    1974      277206 :         table_close(relation, AccessShareLock);
    1975             : 
    1976             :         /* Make sure the resource owner has room to remember this entry. */
    1977      277206 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    1978             : 
    1979             :         /* Now we can build the CatCList entry. */
    1980      277206 :         oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    1981      277206 :         nmembers = list_length(ctlist);
    1982             :         cl = (CatCList *)
    1983      277206 :             palloc(offsetof(CatCList, members) + nmembers * sizeof(CatCTup *));
    1984             : 
    1985             :         /* Extract key values */
    1986      277206 :         CatCacheCopyKeys(cache->cc_tupdesc, nkeys, cache->cc_keyno,
    1987      277206 :                          arguments, cl->keys);
    1988      277206 :         MemoryContextSwitchTo(oldcxt);
    1989             : 
    1990             :         /*
    1991             :          * We are now past the last thing that could trigger an elog before we
    1992             :          * have finished building the CatCList and remembering it in the
    1993             :          * resource owner.  So it's OK to fall out of the PG_TRY, and indeed
    1994             :          * we'd better do so before we start marking the members as belonging
    1995             :          * to the list.
    1996             :          */
    1997             :     }
    1998           0 :     PG_CATCH();
    1999             :     {
    2000             :         Assert(catcache_in_progress_stack == &in_progress_ent);
    2001           0 :         catcache_in_progress_stack = save_in_progress;
    2002             : 
    2003           0 :         foreach(ctlist_item, ctlist)
    2004             :         {
    2005           0 :             ct = (CatCTup *) lfirst(ctlist_item);
    2006             :             Assert(ct->c_list == NULL);
    2007             :             Assert(ct->refcount > 0);
    2008           0 :             ct->refcount--;
    2009           0 :             if (
    2010             : #ifndef CATCACHE_FORCE_RELEASE
    2011           0 :                 ct->dead &&
    2012             : #endif
    2013           0 :                 ct->refcount == 0 &&
    2014           0 :                 (ct->c_list == NULL || ct->c_list->refcount == 0))
    2015           0 :                 CatCacheRemoveCTup(cache, ct);
    2016             :         }
    2017             : 
    2018           0 :         PG_RE_THROW();
    2019             :     }
    2020      277206 :     PG_END_TRY();
    2021             :     Assert(catcache_in_progress_stack == &in_progress_ent);
    2022      277206 :     catcache_in_progress_stack = save_in_progress;
    2023             : 
    2024      277206 :     cl->cl_magic = CL_MAGIC;
    2025      277206 :     cl->my_cache = cache;
    2026      277206 :     cl->refcount = 0;            /* for the moment */
    2027      277206 :     cl->dead = false;
    2028      277206 :     cl->ordered = ordered;
    2029      277206 :     cl->nkeys = nkeys;
    2030      277206 :     cl->hash_value = lHashValue;
    2031      277206 :     cl->n_members = nmembers;
    2032             : 
    2033      277206 :     i = 0;
    2034     1123106 :     foreach(ctlist_item, ctlist)
    2035             :     {
    2036      845900 :         cl->members[i++] = ct = (CatCTup *) lfirst(ctlist_item);
    2037             :         Assert(ct->c_list == NULL);
    2038      845900 :         ct->c_list = cl;
    2039             :         /* release the temporary refcount on the member */
    2040             :         Assert(ct->refcount > 0);
    2041      845900 :         ct->refcount--;
    2042             :         /* mark list dead if any members already dead */
    2043      845900 :         if (ct->dead)
    2044           0 :             cl->dead = true;
    2045             :     }
    2046             :     Assert(i == nmembers);
    2047             : 
    2048             :     /*
    2049             :      * Add the CatCList to the appropriate bucket, and count it.
    2050             :      */
    2051      277206 :     dlist_push_head(lbucket, &cl->cache_elem);
    2052             : 
    2053      277206 :     cache->cc_nlist++;
    2054             : 
    2055             :     /* Finally, bump the list's refcount and return it */
    2056      277206 :     cl->refcount++;
    2057      277206 :     ResourceOwnerRememberCatCacheListRef(CurrentResourceOwner, cl);
    2058             : 
    2059             :     CACHE_elog(DEBUG2, "SearchCatCacheList(%s): made list of %d members",
    2060             :                cache->cc_relname, nmembers);
    2061             : 
    2062      277206 :     return cl;
    2063             : }
    2064             : 
    2065             : /*
    2066             :  *  ReleaseCatCacheList
    2067             :  *
    2068             :  *  Decrement the reference count of a catcache list.
    2069             :  */
    2070             : void
    2071     3354582 : ReleaseCatCacheList(CatCList *list)
    2072             : {
    2073     3354582 :     ReleaseCatCacheListWithOwner(list, CurrentResourceOwner);
    2074     3354582 : }
    2075             : 
    2076             : static void
    2077     3354618 : ReleaseCatCacheListWithOwner(CatCList *list, ResourceOwner resowner)
    2078             : {
    2079             :     /* Safety checks to ensure we were handed a cache entry */
    2080             :     Assert(list->cl_magic == CL_MAGIC);
    2081             :     Assert(list->refcount > 0);
    2082     3354618 :     list->refcount--;
    2083     3354618 :     if (resowner)
    2084     3354582 :         ResourceOwnerForgetCatCacheListRef(CurrentResourceOwner, list);
    2085             : 
    2086     3354618 :     if (
    2087             : #ifndef CATCACHE_FORCE_RELEASE
    2088     3354618 :         list->dead &&
    2089             : #endif
    2090           6 :         list->refcount == 0)
    2091           6 :         CatCacheRemoveCList(list->my_cache, list);
    2092     3354618 : }
    2093             : 
    2094             : 
    2095             : /*
    2096             :  * CatalogCacheCreateEntry
    2097             :  *      Create a new CatCTup entry, copying the given HeapTuple and other
    2098             :  *      supplied data into it.  The new entry initially has refcount 0.
    2099             :  *
    2100             :  * To create a normal cache entry, ntp must be the HeapTuple just fetched
    2101             :  * from scandesc, and "arguments" is not used.  To create a negative cache
    2102             :  * entry, pass NULL for ntp; then "arguments" is the cache keys to use.
    2103             :  * In either case, hashValue/hashIndex are the hash values computed from
    2104             :  * the cache keys.
    2105             :  *
    2106             :  * Returns NULL if we attempt to detoast the tuple and observe that it
    2107             :  * became stale.  (This cannot happen for a negative entry.)  Caller must
    2108             :  * retry the tuple lookup in that case.
    2109             :  */
    2110             : static CatCTup *
    2111     6035256 : CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp, Datum *arguments,
    2112             :                         uint32 hashValue, Index hashIndex)
    2113             : {
    2114             :     CatCTup    *ct;
    2115             :     MemoryContext oldcxt;
    2116             : 
    2117     6035256 :     if (ntp)
    2118             :     {
    2119             :         int         i;
    2120     4531572 :         HeapTuple   dtp = NULL;
    2121             : 
    2122             :         /*
    2123             :          * The invalidation of the in-progress entry essentially never happens
    2124             :          * during our regression tests, and there's no easy way to force it to
    2125             :          * fail for testing purposes.  To ensure we have test coverage for the
    2126             :          * retry paths in our callers, make debug builds randomly fail about
    2127             :          * 0.1% of the times through this code path, even when there's no
    2128             :          * toasted fields.
    2129             :          */
    2130             : #ifdef USE_ASSERT_CHECKING
    2131             :         if (pg_prng_uint32(&pg_global_prng_state) <= (PG_UINT32_MAX / 1000))
    2132             :             return NULL;
    2133             : #endif
    2134             : 
    2135             :         /*
    2136             :          * If there are any out-of-line toasted fields in the tuple, expand
    2137             :          * them in-line.  This saves cycles during later use of the catcache
    2138             :          * entry, and also protects us against the possibility of the toast
    2139             :          * tuples being freed before we attempt to fetch them, in case of
    2140             :          * something using a slightly stale catcache entry.
    2141             :          */
    2142     4531572 :         if (HeapTupleHasExternal(ntp))
    2143             :         {
    2144             :             CatCInProgress *save_in_progress;
    2145             :             CatCInProgress in_progress_ent;
    2146             : 
    2147             :             /*
    2148             :              * The tuple could become stale while we are doing toast table
    2149             :              * access (since AcceptInvalidationMessages can run then).  The
    2150             :              * invalidation will mark our in-progress entry as dead.
    2151             :              */
    2152        3468 :             save_in_progress = catcache_in_progress_stack;
    2153        3468 :             in_progress_ent.next = catcache_in_progress_stack;
    2154        3468 :             in_progress_ent.cache = cache;
    2155        3468 :             in_progress_ent.hash_value = hashValue;
    2156        3468 :             in_progress_ent.list = false;
    2157        3468 :             in_progress_ent.dead = false;
    2158        3468 :             catcache_in_progress_stack = &in_progress_ent;
    2159             : 
    2160        3468 :             PG_TRY();
    2161             :             {
    2162        3468 :                 dtp = toast_flatten_tuple(ntp, cache->cc_tupdesc);
    2163             :             }
    2164           0 :             PG_FINALLY();
    2165             :             {
    2166             :                 Assert(catcache_in_progress_stack == &in_progress_ent);
    2167        3468 :                 catcache_in_progress_stack = save_in_progress;
    2168             :             }
    2169        3468 :             PG_END_TRY();
    2170             : 
    2171        3468 :             if (in_progress_ent.dead)
    2172             :             {
    2173           0 :                 heap_freetuple(dtp);
    2174           0 :                 return NULL;
    2175             :             }
    2176             :         }
    2177             :         else
    2178     4528104 :             dtp = ntp;
    2179             : 
    2180             :         /* Allocate memory for CatCTup and the cached tuple in one go */
    2181     4531572 :         oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    2182             : 
    2183     9063144 :         ct = (CatCTup *) palloc(sizeof(CatCTup) +
    2184     4531572 :                                 MAXIMUM_ALIGNOF + dtp->t_len);
    2185     4531572 :         ct->tuple.t_len = dtp->t_len;
    2186     4531572 :         ct->tuple.t_self = dtp->t_self;
    2187     4531572 :         ct->tuple.t_tableOid = dtp->t_tableOid;
    2188     4531572 :         ct->tuple.t_data = (HeapTupleHeader)
    2189     4531572 :             MAXALIGN(((char *) ct) + sizeof(CatCTup));
    2190             :         /* copy tuple contents */
    2191     4531572 :         memcpy((char *) ct->tuple.t_data,
    2192     4531572 :                (const char *) dtp->t_data,
    2193     4531572 :                dtp->t_len);
    2194     4531572 :         MemoryContextSwitchTo(oldcxt);
    2195             : 
    2196     4531572 :         if (dtp != ntp)
    2197        3468 :             heap_freetuple(dtp);
    2198             : 
    2199             :         /* extract keys - they'll point into the tuple if not by-value */
    2200    13092684 :         for (i = 0; i < cache->cc_nkeys; i++)
    2201             :         {
    2202             :             Datum       atp;
    2203             :             bool        isnull;
    2204             : 
    2205     8561112 :             atp = heap_getattr(&ct->tuple,
    2206             :                                cache->cc_keyno[i],
    2207             :                                cache->cc_tupdesc,
    2208             :                                &isnull);
    2209             :             Assert(!isnull);
    2210     8561112 :             ct->keys[i] = atp;
    2211             :         }
    2212             :     }
    2213             :     else
    2214             :     {
    2215             :         /* Set up keys for a negative cache entry */
    2216     1503684 :         oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    2217     1503684 :         ct = (CatCTup *) palloc(sizeof(CatCTup));
    2218             : 
    2219             :         /*
    2220             :          * Store keys - they'll point into separately allocated memory if not
    2221             :          * by-value.
    2222             :          */
    2223     1503684 :         CatCacheCopyKeys(cache->cc_tupdesc, cache->cc_nkeys, cache->cc_keyno,
    2224     1503684 :                          arguments, ct->keys);
    2225     1503684 :         MemoryContextSwitchTo(oldcxt);
    2226             :     }
    2227             : 
    2228             :     /*
    2229             :      * Finish initializing the CatCTup header, and add it to the cache's
    2230             :      * linked list and counts.
    2231             :      */
    2232     6035256 :     ct->ct_magic = CT_MAGIC;
    2233     6035256 :     ct->my_cache = cache;
    2234     6035256 :     ct->c_list = NULL;
    2235     6035256 :     ct->refcount = 0;            /* for the moment */
    2236     6035256 :     ct->dead = false;
    2237     6035256 :     ct->negative = (ntp == NULL);
    2238     6035256 :     ct->hash_value = hashValue;
    2239             : 
    2240     6035256 :     dlist_push_head(&cache->cc_bucket[hashIndex], &ct->cache_elem);
    2241             : 
    2242     6035256 :     cache->cc_ntup++;
    2243     6035256 :     CacheHdr->ch_ntup++;
    2244             : 
    2245             :     /*
    2246             :      * If the hash table has become too full, enlarge the buckets array. Quite
    2247             :      * arbitrarily, we enlarge when fill factor > 2.
    2248             :      */
    2249     6035256 :     if (cache->cc_ntup > cache->cc_nbuckets * 2)
    2250        5478 :         RehashCatCache(cache);
    2251             : 
    2252     6035256 :     return ct;
    2253             : }
    2254             : 
    2255             : /*
    2256             :  * Helper routine that frees keys stored in the keys array.
    2257             :  */
    2258             : static void
    2259      528954 : CatCacheFreeKeys(TupleDesc tupdesc, int nkeys, int *attnos, Datum *keys)
    2260             : {
    2261             :     int         i;
    2262             : 
    2263     1627690 :     for (i = 0; i < nkeys; i++)
    2264             :     {
    2265     1098736 :         int         attnum = attnos[i];
    2266             :         Form_pg_attribute att;
    2267             : 
    2268             :         /* system attribute are not supported in caches */
    2269             :         Assert(attnum > 0);
    2270             : 
    2271     1098736 :         att = TupleDescAttr(tupdesc, attnum - 1);
    2272             : 
    2273     1098736 :         if (!att->attbyval)
    2274      466740 :             pfree(DatumGetPointer(keys[i]));
    2275             :     }
    2276      528954 : }
    2277             : 
    2278             : /*
    2279             :  * Helper routine that copies the keys in the srckeys array into the dstkeys
    2280             :  * one, guaranteeing that the datums are fully allocated in the current memory
    2281             :  * context.
    2282             :  */
    2283             : static void
    2284     1780890 : CatCacheCopyKeys(TupleDesc tupdesc, int nkeys, int *attnos,
    2285             :                  Datum *srckeys, Datum *dstkeys)
    2286             : {
    2287             :     int         i;
    2288             : 
    2289             :     /*
    2290             :      * XXX: memory and lookup performance could possibly be improved by
    2291             :      * storing all keys in one allocation.
    2292             :      */
    2293             : 
    2294     5564216 :     for (i = 0; i < nkeys; i++)
    2295             :     {
    2296     3783326 :         int         attnum = attnos[i];
    2297     3783326 :         Form_pg_attribute att = TupleDescAttr(tupdesc, attnum - 1);
    2298     3783326 :         Datum       src = srckeys[i];
    2299             :         NameData    srcname;
    2300             : 
    2301             :         /*
    2302             :          * Must be careful in case the caller passed a C string where a NAME
    2303             :          * is wanted: convert the given argument to a correctly padded NAME.
    2304             :          * Otherwise the memcpy() done by datumCopy() could fall off the end
    2305             :          * of memory.
    2306             :          */
    2307     3783326 :         if (att->atttypid == NAMEOID)
    2308             :         {
    2309      776928 :             namestrcpy(&srcname, DatumGetCString(src));
    2310      776928 :             src = NameGetDatum(&srcname);
    2311             :         }
    2312             : 
    2313     3783326 :         dstkeys[i] = datumCopy(src,
    2314     3783326 :                                att->attbyval,
    2315     3783326 :                                att->attlen);
    2316             :     }
    2317     1780890 : }
    2318             : 
    2319             : /*
    2320             :  *  PrepareToInvalidateCacheTuple()
    2321             :  *
    2322             :  *  This is part of a rather subtle chain of events, so pay attention:
    2323             :  *
    2324             :  *  When a tuple is inserted or deleted, it cannot be flushed from the
    2325             :  *  catcaches immediately, for reasons explained at the top of cache/inval.c.
    2326             :  *  Instead we have to add entry(s) for the tuple to a list of pending tuple
    2327             :  *  invalidations that will be done at the end of the command or transaction.
    2328             :  *
    2329             :  *  The lists of tuples that need to be flushed are kept by inval.c.  This
    2330             :  *  routine is a helper routine for inval.c.  Given a tuple belonging to
    2331             :  *  the specified relation, find all catcaches it could be in, compute the
    2332             :  *  correct hash value for each such catcache, and call the specified
    2333             :  *  function to record the cache id and hash value in inval.c's lists.
    2334             :  *  SysCacheInvalidate will be called later, if appropriate,
    2335             :  *  using the recorded information.
    2336             :  *
    2337             :  *  For an insert or delete, tuple is the target tuple and newtuple is NULL.
    2338             :  *  For an update, we are called just once, with tuple being the old tuple
    2339             :  *  version and newtuple the new version.  We should make two list entries
    2340             :  *  if the tuple's hash value changed, but only one if it didn't.
    2341             :  *
    2342             :  *  Note that it is irrelevant whether the given tuple is actually loaded
    2343             :  *  into the catcache at the moment.  Even if it's not there now, it might
    2344             :  *  be by the end of the command, or there might be a matching negative entry
    2345             :  *  to flush --- or other backends' caches might have such entries --- so
    2346             :  *  we have to make list entries to flush it later.
    2347             :  *
    2348             :  *  Also note that it's not an error if there are no catcaches for the
    2349             :  *  specified relation.  inval.c doesn't know exactly which rels have
    2350             :  *  catcaches --- it will call this routine for any tuple that's in a
    2351             :  *  system relation.
    2352             :  */
    2353             : void
    2354     2939182 : PrepareToInvalidateCacheTuple(Relation relation,
    2355             :                               HeapTuple tuple,
    2356             :                               HeapTuple newtuple,
    2357             :                               void (*function) (int, uint32, Oid, void *),
    2358             :                               void *context)
    2359             : {
    2360             :     slist_iter  iter;
    2361             :     Oid         reloid;
    2362             : 
    2363             :     CACHE_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
    2364             : 
    2365             :     /*
    2366             :      * sanity checks
    2367             :      */
    2368             :     Assert(RelationIsValid(relation));
    2369             :     Assert(HeapTupleIsValid(tuple));
    2370             :     Assert(PointerIsValid(function));
    2371             :     Assert(CacheHdr != NULL);
    2372             : 
    2373     2939182 :     reloid = RelationGetRelid(relation);
    2374             : 
    2375             :     /* ----------------
    2376             :      *  for each cache
    2377             :      *     if the cache contains tuples from the specified relation
    2378             :      *         compute the tuple's hash value(s) in this cache,
    2379             :      *         and call the passed function to register the information.
    2380             :      * ----------------
    2381             :      */
    2382             : 
    2383   252769652 :     slist_foreach(iter, &CacheHdr->ch_caches)
    2384             :     {
    2385   249830470 :         CatCache   *ccp = slist_container(CatCache, cc_next, iter.cur);
    2386             :         uint32      hashvalue;
    2387             :         Oid         dbid;
    2388             : 
    2389   249830470 :         if (ccp->cc_reloid != reloid)
    2390   244463316 :             continue;
    2391             : 
    2392             :         /* Just in case cache hasn't finished initialization yet... */
    2393     5367154 :         if (ccp->cc_tupdesc == NULL)
    2394        8992 :             CatalogCacheInitializeCache(ccp);
    2395             : 
    2396     5367154 :         hashvalue = CatalogCacheComputeTupleHashValue(ccp, ccp->cc_nkeys, tuple);
    2397     5367154 :         dbid = ccp->cc_relisshared ? (Oid) 0 : MyDatabaseId;
    2398             : 
    2399     5367154 :         (*function) (ccp->id, hashvalue, dbid, context);
    2400             : 
    2401     5367154 :         if (newtuple)
    2402             :         {
    2403             :             uint32      newhashvalue;
    2404             : 
    2405      370620 :             newhashvalue = CatalogCacheComputeTupleHashValue(ccp, ccp->cc_nkeys, newtuple);
    2406             : 
    2407      370620 :             if (newhashvalue != hashvalue)
    2408        6020 :                 (*function) (ccp->id, newhashvalue, dbid, context);
    2409             :         }
    2410             :     }
    2411     2939182 : }
    2412             : 
    2413             : /* ResourceOwner callbacks */
    2414             : 
    2415             : static void
    2416       10382 : ResOwnerReleaseCatCache(Datum res)
    2417             : {
    2418       10382 :     ReleaseCatCacheWithOwner((HeapTuple) DatumGetPointer(res), NULL);
    2419       10382 : }
    2420             : 
    2421             : static char *
    2422           0 : ResOwnerPrintCatCache(Datum res)
    2423             : {
    2424           0 :     HeapTuple   tuple = (HeapTuple) DatumGetPointer(res);
    2425           0 :     CatCTup    *ct = (CatCTup *) (((char *) tuple) -
    2426             :                                   offsetof(CatCTup, tuple));
    2427             : 
    2428             :     /* Safety check to ensure we were handed a cache entry */
    2429             :     Assert(ct->ct_magic == CT_MAGIC);
    2430             : 
    2431           0 :     return psprintf("cache %s (%d), tuple %u/%u has count %d",
    2432           0 :                     ct->my_cache->cc_relname, ct->my_cache->id,
    2433           0 :                     ItemPointerGetBlockNumber(&(tuple->t_self)),
    2434           0 :                     ItemPointerGetOffsetNumber(&(tuple->t_self)),
    2435             :                     ct->refcount);
    2436             : }
    2437             : 
    2438             : static void
    2439          36 : ResOwnerReleaseCatCacheList(Datum res)
    2440             : {
    2441          36 :     ReleaseCatCacheListWithOwner((CatCList *) DatumGetPointer(res), NULL);
    2442          36 : }
    2443             : 
    2444             : static char *
    2445           0 : ResOwnerPrintCatCacheList(Datum res)
    2446             : {
    2447           0 :     CatCList   *list = (CatCList *) DatumGetPointer(res);
    2448             : 
    2449           0 :     return psprintf("cache %s (%d), list %p has count %d",
    2450           0 :                     list->my_cache->cc_relname, list->my_cache->id,
    2451             :                     list, list->refcount);
    2452             : }

Generated by: LCOV version 1.14