Skip to content

Commit 5069017

Browse files
committed
Remove AtEOXact_CatCache().
The sole useful effect of this function, to check that no catcache entries have positive refcounts at transaction end, has really been obsolete since we introduced ResourceOwners in PG 8.1. We reduced the checks to assertions years ago, so that the function was a complete no-op in production builds. There have been previous discussions about removing it entirely, but consensus up to now was that it had some small value as a cross-check for bugs in the ResourceOwner logic. However, it now emerges that it's possible to trigger these assertions if you hit an assert-enabled backend with SIGTERM during a call to SearchCatCacheList, because that function temporarily increases the refcounts of entries it's intending to add to a catcache list construct. In a normal ERROR scenario, the extra refcounts are cleaned up by SearchCatCacheList's PG_CATCH block; but in a FATAL exit we do a transaction abort and exit without ever executing PG_CATCH handlers. There's a case to be made that this is a generic hazard and we should consider restructuring elog(FATAL) handling so that pending PG_CATCH handlers do get run. That's pretty scary though: it could easily create more problems than it solves. Preliminary stress testing by Andreas Seltenreich suggests that there are not many live problems of this ilk, so we rejected that idea. There are more-localized ways to fix the problem; the most principled one would be to use PG_ENSURE_ERROR_CLEANUP instead of plain PG_TRY. But adding cycles to SearchCatCacheList isn't very appealing. We could also weaken the assertions in AtEOXact_CatCache in some more or less ad-hoc way, but that just makes its raison d'etre even less compelling. In the end, the most reasonable solution seems to be to just remove AtEOXact_CatCache altogether, on the grounds that it's not worth trying to fix it. It hasn't found any bugs for us in many years. Per report from Jeevan Chalke. Back-patch to all supported branches. Discussion: https://postgr.es/m/CAM2+6=VEE30YtRQCZX7_sCFsEpoUkFBV1gZazL70fqLn8rcvBA@mail.gmail.com
1 parent 4e704aa commit 5069017

File tree

3 files changed

+0
-59
lines changed

3 files changed

+0
-59
lines changed

src/backend/access/transam/xact.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,9 +1987,6 @@ CommitTransaction(void)
19871987
RESOURCE_RELEASE_AFTER_LOCKS,
19881988
true, true);
19891989

1990-
/* Check we've released all catcache entries */
1991-
AtEOXact_CatCache(true);
1992-
19931990
AtCommit_Notify();
19941991
AtEOXact_GUC(true, 1);
19951992
AtEOXact_SPI(true);
@@ -2252,9 +2249,6 @@ PrepareTransaction(void)
22522249
*/
22532250
PostPrepare_Twophase();
22542251

2255-
/* Check we've released all catcache entries */
2256-
AtEOXact_CatCache(true);
2257-
22582252
/* PREPARE acts the same as COMMIT as far as GUC is concerned */
22592253
AtEOXact_GUC(true, 1);
22602254
AtEOXact_SPI(true);
@@ -2402,7 +2396,6 @@ AbortTransaction(void)
24022396
ResourceOwnerRelease(TopTransactionResourceOwner,
24032397
RESOURCE_RELEASE_AFTER_LOCKS,
24042398
false, true);
2405-
AtEOXact_CatCache(false);
24062399

24072400
AtEOXact_GUC(false, 1);
24082401
AtEOXact_SPI(false);

src/backend/utils/cache/catcache.c

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -542,57 +542,6 @@ CreateCacheMemoryContext(void)
542542
}
543543

544544

545-
/*
546-
* AtEOXact_CatCache
547-
*
548-
* Clean up catcaches at end of main transaction (either commit or abort)
549-
*
550-
* As of PostgreSQL 8.1, catcache pins should get released by the
551-
* ResourceOwner mechanism. This routine is just a debugging
552-
* cross-check that no pins remain.
553-
*/
554-
void
555-
AtEOXact_CatCache(bool isCommit)
556-
{
557-
#ifdef USE_ASSERT_CHECKING
558-
if (assert_enabled)
559-
{
560-
CatCache *ccp;
561-
562-
for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
563-
{
564-
Dlelem *elt;
565-
int i;
566-
567-
/* Check CatCLists */
568-
for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt))
569-
{
570-
CatCList *cl = (CatCList *) DLE_VAL(elt);
571-
572-
Assert(cl->cl_magic == CL_MAGIC);
573-
Assert(cl->refcount == 0);
574-
Assert(!cl->dead);
575-
}
576-
577-
/* Check individual tuples */
578-
for (i = 0; i < ccp->cc_nbuckets; i++)
579-
{
580-
for (elt = DLGetHead(&ccp->cc_bucket[i]);
581-
elt;
582-
elt = DLGetSucc(elt))
583-
{
584-
CatCTup *ct = (CatCTup *) DLE_VAL(elt);
585-
586-
Assert(ct->ct_magic == CT_MAGIC);
587-
Assert(ct->refcount == 0);
588-
Assert(!ct->dead);
589-
}
590-
}
591-
}
592-
}
593-
#endif
594-
}
595-
596545
/*
597546
* ResetCatalogCache
598547
*

src/include/utils/catcache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ typedef struct catcacheheader
162162
extern PGDLLIMPORT MemoryContext CacheMemoryContext;
163163

164164
extern void CreateCacheMemoryContext(void);
165-
extern void AtEOXact_CatCache(bool isCommit);
166165

167166
extern CatCache *InitCatCache(int id, Oid reloid, Oid indexoid,
168167
int nkeys, const int *key,

0 commit comments

Comments
 (0)