Skip to content

Commit 8c3e36f

Browse files
committed
Reduce overhead of cache-clobber testing in LookupOpclassInfo().
Commit 03ffc4d added logic to bypass all caching behavior in LookupOpclassInfo when CLOBBER_CACHE_ALWAYS is enabled. It doesn't look like I stopped to think much about what that would cost, but recent investigation shows that the cost is enormous: it roughly doubles the time needed for cache-clobber test runs. There does seem to be value in this behavior when trying to test the opclass-cache loading logic itself, but for other purposes the cost is excessive. Hence, let's back off to doing this only when debug_invalidate_system_caches_always is at least 3; or in older branches, when CLOBBER_CACHE_RECURSIVELY is defined. While here, clean up some other minor issues in LookupOpclassInfo. Re-order the code so we aren't left with broken cache entries (leading to later core dumps) in the unlikely case that we suffer OOM while trying to allocate space for a new entry. (That seems to be my oversight in 03ffc4d.) Also, in >= v13, stop allocating one array entry too many. That's evidently left over from sloppy reversion in 851b14b. Back-patch to all supported branches, mainly to reduce the runtime of cache-clobbering buildfarm animals. Discussion: https://postgr.es/m/1370856.1625428625@sss.pgh.pa.us
1 parent 9dd560c commit 8c3e36f

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/backend/utils/cache/relcache.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,15 +1701,15 @@ LookupOpclassInfo(Oid operatorClassOid,
17011701
/* First time through: initialize the opclass cache */
17021702
HASHCTL ctl;
17031703

1704+
/* Also make sure CacheMemoryContext exists */
1705+
if (!CacheMemoryContext)
1706+
CreateCacheMemoryContext();
1707+
17041708
MemSet(&ctl, 0, sizeof(ctl));
17051709
ctl.keysize = sizeof(Oid);
17061710
ctl.entrysize = sizeof(OpClassCacheEnt);
17071711
OpClassCache = hash_create("Operator class cache", 64,
17081712
&ctl, HASH_ELEM | HASH_BLOBS);
1709-
1710-
/* Also make sure CacheMemoryContext exists */
1711-
if (!CacheMemoryContext)
1712-
CreateCacheMemoryContext();
17131713
}
17141714

17151715
opcentry = (OpClassCacheEnt *) hash_search(OpClassCache,
@@ -1718,39 +1718,42 @@ LookupOpclassInfo(Oid operatorClassOid,
17181718

17191719
if (!found)
17201720
{
1721-
/* Need to allocate memory for new entry */
1721+
/* Initialize new entry */
17221722
opcentry->valid = false; /* until known OK */
17231723
opcentry->numSupport = numSupport;
1724-
1725-
if (numSupport > 0)
1726-
opcentry->supportProcs = (RegProcedure *)
1727-
MemoryContextAllocZero(CacheMemoryContext,
1728-
numSupport * sizeof(RegProcedure));
1729-
else
1730-
opcentry->supportProcs = NULL;
1724+
opcentry->supportProcs = NULL; /* filled below */
17311725
}
17321726
else
17331727
{
17341728
Assert(numSupport == opcentry->numSupport);
17351729
}
17361730

17371731
/*
1738-
* When testing for cache-flush hazards, we intentionally disable the
1739-
* operator class cache and force reloading of the info on each call. This
1740-
* is helpful because we want to test the case where a cache flush occurs
1741-
* while we are loading the info, and it's very hard to provoke that if
1742-
* this happens only once per opclass per backend.
1732+
* When aggressively testing cache-flush hazards, we disable the operator
1733+
* class cache and force reloading of the info on each call. This models
1734+
* no real-world behavior, since the cache entries are never invalidated
1735+
* otherwise. However it can be helpful for detecting bugs in the cache
1736+
* loading logic itself, such as reliance on a non-nailed index. Given
1737+
* the limited use-case and the fact that this adds a great deal of
1738+
* expense, we enable it only in CLOBBER_CACHE_RECURSIVELY mode.
17431739
*/
1744-
#if defined(CLOBBER_CACHE_ALWAYS)
1740+
#if defined(CLOBBER_CACHE_RECURSIVELY)
17451741
opcentry->valid = false;
17461742
#endif
17471743

17481744
if (opcentry->valid)
17491745
return opcentry;
17501746

17511747
/*
1752-
* Need to fill in new entry.
1753-
*
1748+
* Need to fill in new entry. First allocate space, unless we already did
1749+
* so in some previous attempt.
1750+
*/
1751+
if (opcentry->supportProcs == NULL && numSupport > 0)
1752+
opcentry->supportProcs = (RegProcedure *)
1753+
MemoryContextAllocZero(CacheMemoryContext,
1754+
numSupport * sizeof(RegProcedure));
1755+
1756+
/*
17541757
* To avoid infinite recursion during startup, force heap scans if we're
17551758
* looking up info for the opclasses used by the indexes we would like to
17561759
* reference here.

0 commit comments

Comments
 (0)