Skip to content

Commit 08890b4

Browse files
committed
Fix resource owner code to generate catcache and relcache leak warnings
when open references remain during normal cleanup of a resource owner. This restores the system's ability to warn about leaks to what it was before 8.0. Not really a user-level bug, but helpful for development.
1 parent 6b7ef07 commit 08890b4

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

src/backend/utils/cache/catcache.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/cache/catcache.c,v 1.118 2004/12/31 22:01:25 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/cache/catcache.c,v 1.119 2005/03/25 18:30:27 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -556,8 +556,7 @@ AtEOXact_CatCache(bool isCommit)
556556
if (cl->refcount != 0)
557557
{
558558
if (isCommit)
559-
elog(WARNING, "cache reference leak: cache %s (%d), list %p has count %d",
560-
ccp->cc_relname, ccp->id, cl, cl->refcount);
559+
PrintCatCacheListLeakWarning(cl);
561560
cl->refcount = 0;
562561
}
563562

@@ -579,10 +578,7 @@ AtEOXact_CatCache(bool isCommit)
579578
if (ct->refcount != 0)
580579
{
581580
if (isCommit)
582-
elog(WARNING, "cache reference leak: cache %s (%d), tuple %u has count %d",
583-
ct->my_cache->cc_relname, ct->my_cache->id,
584-
HeapTupleGetOid(&ct->tuple),
585-
ct->refcount);
581+
PrintCatCacheLeakWarning(&ct->tuple);
586582
ct->refcount = 0;
587583
}
588584

@@ -1807,3 +1803,32 @@ PrepareToInvalidateCacheTuple(Relation relation,
18071803
ccp->cc_relisshared ? (Oid) 0 : MyDatabaseId);
18081804
}
18091805
}
1806+
1807+
1808+
/*
1809+
* Subroutines for warning about reference leaks. These are exported so
1810+
* that resowner.c can call them.
1811+
*/
1812+
void
1813+
PrintCatCacheLeakWarning(HeapTuple tuple)
1814+
{
1815+
CatCTup *ct = (CatCTup *) (((char *) tuple) -
1816+
offsetof(CatCTup, tuple));
1817+
1818+
/* Safety check to ensure we were handed a cache entry */
1819+
Assert(ct->ct_magic == CT_MAGIC);
1820+
1821+
elog(WARNING, "cache reference leak: cache %s (%d), tuple %u/%u has count %d",
1822+
ct->my_cache->cc_relname, ct->my_cache->id,
1823+
ItemPointerGetBlockNumber(&(tuple->t_self)),
1824+
ItemPointerGetOffsetNumber(&(tuple->t_self)),
1825+
ct->refcount);
1826+
}
1827+
1828+
void
1829+
PrintCatCacheListLeakWarning(CatCList *list)
1830+
{
1831+
elog(WARNING, "cache reference leak: cache %s (%d), list %p has count %d",
1832+
list->my_cache->cc_relname, list->my_cache->id,
1833+
list, list->refcount);
1834+
}

src/backend/utils/resowner/resowner.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/utils/resowner/resowner.c,v 1.10 2005/03/04 20:21:06 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/resowner/resowner.c,v 1.11 2005/03/25 18:30:27 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -87,6 +87,7 @@ static void ResourceOwnerReleaseInternal(ResourceOwner owner,
8787
ResourceReleasePhase phase,
8888
bool isCommit,
8989
bool isTopLevel);
90+
static void PrintRelCacheLeakWarning(Relation rel);
9091

9192

9293
/*****************************************************************************
@@ -231,7 +232,11 @@ ResourceOwnerReleaseInternal(ResourceOwner owner,
231232
* iterate till there are none.
232233
*/
233234
while (owner->nrelrefs > 0)
235+
{
236+
if (isCommit)
237+
PrintRelCacheLeakWarning(owner->relrefs[owner->nrelrefs - 1]);
234238
RelationClose(owner->relrefs[owner->nrelrefs - 1]);
239+
}
235240
}
236241
}
237242
else if (phase == RESOURCE_RELEASE_LOCKS)
@@ -284,9 +289,17 @@ ResourceOwnerReleaseInternal(ResourceOwner owner,
284289
* to iterate till there are none. Ditto for catcache lists.
285290
*/
286291
while (owner->ncatrefs > 0)
292+
{
293+
if (isCommit)
294+
PrintCatCacheLeakWarning(owner->catrefs[owner->ncatrefs - 1]);
287295
ReleaseCatCache(owner->catrefs[owner->ncatrefs - 1]);
296+
}
288297
while (owner->ncatlistrefs > 0)
298+
{
299+
if (isCommit)
300+
PrintCatCacheListLeakWarning(owner->catlistrefs[owner->ncatlistrefs - 1]);
289301
ReleaseCatCacheList(owner->catlistrefs[owner->ncatlistrefs - 1]);
302+
}
290303
}
291304
/* Clean up index scans too */
292305
ReleaseResources_gist();
@@ -746,3 +759,13 @@ ResourceOwnerForgetRelationRef(ResourceOwner owner, Relation rel)
746759
elog(ERROR, "relcache reference %s is not owned by resource owner %s",
747760
RelationGetRelationName(rel), owner->name);
748761
}
762+
763+
/*
764+
* Debugging subroutine
765+
*/
766+
static void
767+
PrintRelCacheLeakWarning(Relation rel)
768+
{
769+
elog(WARNING, "relcache reference leak: relation \"%s\" not closed",
770+
RelationGetRelationName(rel));
771+
}

src/include/utils/catcache.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
1414
* Portions Copyright (c) 1994, Regents of the University of California
1515
*
16-
* $PostgreSQL: pgsql/src/include/utils/catcache.h,v 1.52 2004/12/31 22:03:45 pgsql Exp $
16+
* $PostgreSQL: pgsql/src/include/utils/catcache.h,v 1.53 2005/03/25 18:30:28 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -187,4 +187,7 @@ extern void PrepareToInvalidateCacheTuple(Relation relation,
187187
HeapTuple tuple,
188188
void (*function) (int, uint32, ItemPointer, Oid));
189189

190+
extern void PrintCatCacheLeakWarning(HeapTuple tuple);
191+
extern void PrintCatCacheListLeakWarning(CatCList *list);
192+
190193
#endif /* CATCACHE_H */

0 commit comments

Comments
 (0)