mirror of
https://github.com/postgres/postgres.git
synced 2025-07-15 19:21:59 +03:00
Replace CLOBBER_CACHE_ALWAYS with run-time GUC
Forced cache invalidation (CLOBBER_CACHE_ALWAYS) has been impractical to use for testing in PostgreSQL because it's so slow and because it's toggled on/off only at build time. It is helpful when hunting bugs in any code that uses the sycache/relcache because causes cache invalidations to be injected whenever it would be possible for an invalidation to occur, whether or not one was really pending. Address this by providing run-time control over cache clobber behaviour using the new debug_invalidate_system_caches_always GUC. Support is not compiled in at all unless assertions are enabled or CLOBBER_CACHE_ENABLED is explicitly defined at compile time. It defaults to 0 if compiled in, so it has negligible effect on assert build performance by default. When support is compiled in, test code can now set debug_invalidate_system_caches_always=1 locally to a backend to test specific queries, functions, extensions, etc. Or tests can toggle it globally for a specific test case while retaining normal performance during test setup and teardown. For backwards compatibility with existing test harnesses and scripts, debug_invalidate_system_caches_always defaults to 1 if CLOBBER_CACHE_ALWAYS is defined, and to 3 if CLOBBER_CACHE_RECURSIVE is defined. CLOBBER_CACHE_ENABLED is now visible in pg_config_manual.h, as is the related RECOVER_RELATION_BUILD_MEMORY setting for the relcache. Author: Craig Ringer <craig.ringer@2ndquadrant.com> Discussion: https://www.postgresql.org/message-id/flat/CAMsr+YF=+ctXBZj3ywmvKNUjWpxmuTuUKuv-rgbHGX5i5pLstQ@mail.gmail.com
This commit is contained in:
67
src/backend/utils/cache/relcache.c
vendored
67
src/backend/utils/cache/relcache.c
vendored
@ -91,15 +91,15 @@
|
||||
#define RELCACHE_INIT_FILEMAGIC 0x573266 /* version ID value */
|
||||
|
||||
/*
|
||||
* Default policy for whether to apply RECOVER_RELATION_BUILD_MEMORY:
|
||||
* do so in clobber-cache builds but not otherwise. This choice can be
|
||||
* overridden at compile time with -DRECOVER_RELATION_BUILD_MEMORY=1 or =0.
|
||||
* Whether to bother checking if relation cache memory needs to be freed
|
||||
* eagerly. See also RelationBuildDesc() and pg_config_manual.h.
|
||||
*/
|
||||
#ifndef RECOVER_RELATION_BUILD_MEMORY
|
||||
#if defined(CLOBBER_CACHE_ALWAYS) || defined(CLOBBER_CACHE_RECURSIVELY)
|
||||
#define RECOVER_RELATION_BUILD_MEMORY 1
|
||||
#if defined(RECOVER_RELATION_BUILD_MEMORY) && (RECOVER_RELATION_BUILD_MEMORY != 0)
|
||||
#define MAYBE_RECOVER_RELATION_BUILD_MEMORY 1
|
||||
#else
|
||||
#define RECOVER_RELATION_BUILD_MEMORY 0
|
||||
#ifdef CLOBBER_CACHE_ENABLED
|
||||
#define MAYBE_RECOVER_RELATION_BUILD_MEMORY 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -1040,19 +1040,25 @@ RelationBuildDesc(Oid targetRelId, bool insertIt)
|
||||
* scope, and relcache loads shouldn't happen so often that it's essential
|
||||
* to recover transient data before end of statement/transaction. However
|
||||
* that's definitely not true in clobber-cache test builds, and perhaps
|
||||
* it's not true in other cases. If RECOVER_RELATION_BUILD_MEMORY is not
|
||||
* zero, arrange to allocate the junk in a temporary context that we'll
|
||||
* free before returning. Make it a child of caller's context so that it
|
||||
* will get cleaned up appropriately if we error out partway through.
|
||||
* it's not true in other cases.
|
||||
*
|
||||
* When cache clobbering is enabled or when forced to by
|
||||
* RECOVER_RELATION_BUILD_MEMORY=1, arrange to allocate the junk in a
|
||||
* temporary context that we'll free before returning. Make it a child
|
||||
* of caller's context so that it will get cleaned up appropriately if
|
||||
* we error out partway through.
|
||||
*/
|
||||
#if RECOVER_RELATION_BUILD_MEMORY
|
||||
MemoryContext tmpcxt;
|
||||
MemoryContext oldcxt;
|
||||
#ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
|
||||
MemoryContext tmpcxt = NULL;
|
||||
MemoryContext oldcxt = NULL;
|
||||
|
||||
tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
|
||||
"RelationBuildDesc workspace",
|
||||
ALLOCSET_DEFAULT_SIZES);
|
||||
oldcxt = MemoryContextSwitchTo(tmpcxt);
|
||||
if (RECOVER_RELATION_BUILD_MEMORY || debug_invalidate_system_caches_always > 0)
|
||||
{
|
||||
tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
|
||||
"RelationBuildDesc workspace",
|
||||
ALLOCSET_DEFAULT_SIZES);
|
||||
oldcxt = MemoryContextSwitchTo(tmpcxt);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
@ -1065,10 +1071,13 @@ RelationBuildDesc(Oid targetRelId, bool insertIt)
|
||||
*/
|
||||
if (!HeapTupleIsValid(pg_class_tuple))
|
||||
{
|
||||
#if RECOVER_RELATION_BUILD_MEMORY
|
||||
/* Return to caller's context, and blow away the temporary context */
|
||||
MemoryContextSwitchTo(oldcxt);
|
||||
MemoryContextDelete(tmpcxt);
|
||||
#ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
|
||||
if (tmpcxt)
|
||||
{
|
||||
/* Return to caller's context, and blow away the temporary context */
|
||||
MemoryContextSwitchTo(oldcxt);
|
||||
MemoryContextDelete(tmpcxt);
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
@ -1247,10 +1256,13 @@ RelationBuildDesc(Oid targetRelId, bool insertIt)
|
||||
/* It's fully valid */
|
||||
relation->rd_isvalid = true;
|
||||
|
||||
#if RECOVER_RELATION_BUILD_MEMORY
|
||||
/* Return to caller's context, and blow away the temporary context */
|
||||
MemoryContextSwitchTo(oldcxt);
|
||||
MemoryContextDelete(tmpcxt);
|
||||
#ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
|
||||
if (tmpcxt)
|
||||
{
|
||||
/* Return to caller's context, and blow away the temporary context */
|
||||
MemoryContextSwitchTo(oldcxt);
|
||||
MemoryContextDelete(tmpcxt);
|
||||
}
|
||||
#endif
|
||||
|
||||
return relation;
|
||||
@ -1646,8 +1658,9 @@ LookupOpclassInfo(Oid operatorClassOid,
|
||||
* while we are loading the info, and it's very hard to provoke that if
|
||||
* this happens only once per opclass per backend.
|
||||
*/
|
||||
#if defined(CLOBBER_CACHE_ALWAYS)
|
||||
opcentry->valid = false;
|
||||
#ifdef CLOBBER_CACHE_ENABLED
|
||||
if (debug_invalidate_system_caches_always > 0)
|
||||
opcentry->valid = false;
|
||||
#endif
|
||||
|
||||
if (opcentry->valid)
|
||||
|
Reference in New Issue
Block a user