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