Skip to content

Commit 2f48711

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 27621cc commit 2f48711

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
@@ -1622,15 +1622,15 @@ LookupOpclassInfo(Oid operatorClassOid,
16221622
/* First time through: initialize the opclass cache */
16231623
HASHCTL ctl;
16241624

1625+
/* Also make sure CacheMemoryContext exists */
1626+
if (!CacheMemoryContext)
1627+
CreateCacheMemoryContext();
1628+
16251629
MemSet(&ctl, 0, sizeof(ctl));
16261630
ctl.keysize = sizeof(Oid);
16271631
ctl.entrysize = sizeof(OpClassCacheEnt);
16281632
OpClassCache = hash_create("Operator class cache", 64,
16291633
&ctl, HASH_ELEM | HASH_BLOBS);
1630-
1631-
/* Also make sure CacheMemoryContext exists */
1632-
if (!CacheMemoryContext)
1633-
CreateCacheMemoryContext();
16341634
}
16351635

16361636
opcentry = (OpClassCacheEnt *) hash_search(OpClassCache,
@@ -1639,39 +1639,42 @@ LookupOpclassInfo(Oid operatorClassOid,
16391639

16401640
if (!found)
16411641
{
1642-
/* Need to allocate memory for new entry */
1642+
/* Initialize new entry */
16431643
opcentry->valid = false; /* until known OK */
16441644
opcentry->numSupport = numSupport;
1645-
1646-
if (numSupport > 0)
1647-
opcentry->supportProcs = (RegProcedure *)
1648-
MemoryContextAllocZero(CacheMemoryContext,
1649-
(numSupport + 1) * sizeof(RegProcedure));
1650-
else
1651-
opcentry->supportProcs = NULL;
1645+
opcentry->supportProcs = NULL; /* filled below */
16521646
}
16531647
else
16541648
{
16551649
Assert(numSupport == opcentry->numSupport);
16561650
}
16571651

16581652
/*
1659-
* When testing for cache-flush hazards, we intentionally disable the
1660-
* operator class cache and force reloading of the info on each call. This
1661-
* is helpful because we want to test the case where a cache flush occurs
1662-
* while we are loading the info, and it's very hard to provoke that if
1663-
* this happens only once per opclass per backend.
1653+
* When aggressively testing cache-flush hazards, we disable the operator
1654+
* class cache and force reloading of the info on each call. This models
1655+
* no real-world behavior, since the cache entries are never invalidated
1656+
* otherwise. However it can be helpful for detecting bugs in the cache
1657+
* loading logic itself, such as reliance on a non-nailed index. Given
1658+
* the limited use-case and the fact that this adds a great deal of
1659+
* expense, we enable it only in CLOBBER_CACHE_RECURSIVELY mode.
16641660
*/
1665-
#if defined(CLOBBER_CACHE_ALWAYS)
1661+
#if defined(CLOBBER_CACHE_RECURSIVELY)
16661662
opcentry->valid = false;
16671663
#endif
16681664

16691665
if (opcentry->valid)
16701666
return opcentry;
16711667

16721668
/*
1673-
* Need to fill in new entry.
1674-
*
1669+
* Need to fill in new entry. First allocate space, unless we already did
1670+
* so in some previous attempt.
1671+
*/
1672+
if (opcentry->supportProcs == NULL && numSupport > 0)
1673+
opcentry->supportProcs = (RegProcedure *)
1674+
MemoryContextAllocZero(CacheMemoryContext,
1675+
numSupport * sizeof(RegProcedure));
1676+
1677+
/*
16751678
* To avoid infinite recursion during startup, force heap scans if we're
16761679
* looking up info for the opclasses used by the indexes we would like to
16771680
* reference here.

0 commit comments

Comments
 (0)