LCOV - differential code coverage report
Current view: top level - src/backend/utils/cache - catcache.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 92.1 % 699 644 1 54 1 11 632 15
Current Date: 2026-05-05 10:23:31 +0900 Functions: 94.6 % 56 53 3 8 45 2
Baseline: lcov-20260505-025707-baseline Branches: 72.3 % 393 284 1 1 107 2 3 279 1 3
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 2 2 2
(30,360] days: 100.0 % 14 14 11 3
(360..) days: 91.9 % 683 628 1 54 1 627
Function coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 94.4 % 54 51 3 6 45
Branch coverage date bins:
(30,360] days: 75.0 % 4 3 1 3
(360..) days: 72.2 % 389 281 1 107 2 279

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

Generated by: LCOV version 2.5.0-beta