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