LCOV - differential code coverage report
Current view: top level - src/backend/utils/cache - catcache.c (source / functions) Coverage Total Hit LBC UBC GNC CBC DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 91.7 % 702 644 2 56 1 643 1
Current Date: 2025-09-06 07:49:51 +0900 Functions: 94.6 % 56 53 3 2 51
Baseline: lcov-20250906-005545-baseline Branches: 71.2 % 393 280 2 111 280
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 90.5 % 74 67 7 1 66
(360..) days: 91.9 % 628 577 2 49 577
Function coverage date bins:
(30,360] days: 100.0 % 4 4 4
(360..) days: 94.2 % 52 49 3 2 47
Branch coverage date bins:
(30,360] days: 72.2 % 54 39 15 39
(360..) days: 71.1 % 339 241 2 96 241

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

Generated by: LCOV version 2.4-beta