Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 22c64f1

Browse files
committed
When compiling with --enable-cassert, check for reference count leaks
in the relcache. It's rather silly that we have reference count leak checks in bufmgr and in catcache, but not in relcache which will normally have many fewer entries. Chris K-L would have caught at least one bug in his recent DROP patch if he'd had this.
1 parent 154f26f commit 22c64f1

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

src/backend/access/transam/xact.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.128 2002/06/20 20:29:25 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.129 2002/08/02 22:36:05 tgl Exp $
1212
*
1313
* NOTES
1414
* Transaction aborts can now occur two ways:
@@ -615,6 +615,8 @@ RecordTransactionCommit(void)
615615
static void
616616
AtCommit_Cache(void)
617617
{
618+
/* Check for relcache reference-count leaks */
619+
AtEOXactRelationCache(true);
618620
/*
619621
* Make catalog changes visible to all backends.
620622
*/
@@ -741,7 +743,7 @@ RecordTransactionAbort(void)
741743
static void
742744
AtAbort_Cache(void)
743745
{
744-
RelationCacheAbort();
746+
AtEOXactRelationCache(false);
745747
AtEOXactInvalidationMessages(false);
746748
}
747749

src/backend/bootstrap/bootstrap.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.134 2002/08/02 18:15:04 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.135 2002/08/02 22:36:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -550,7 +550,6 @@ closerel(char *name)
550550
else
551551
elog(ERROR, "closerel: close of '%s' before any relation was opened",
552552
name);
553-
554553
}
555554

556555
if (boot_reldesc == NULL)
@@ -822,8 +821,8 @@ cleanup()
822821
elog(FATAL, "Memory manager fault: cleanup called twice.\n");
823822
proc_exit(1);
824823
}
825-
if (boot_reldesc != (Relation) NULL)
826-
heap_close(boot_reldesc, NoLock);
824+
if (boot_reldesc != NULL)
825+
closerel(NULL);
827826
CommitTransactionCommand();
828827
proc_exit(Warnings);
829828
}

src/backend/utils/cache/relcache.c

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.168 2002/07/20 05:16:58 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.169 2002/08/02 22:36:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1968,34 +1968,61 @@ RelationCacheInvalidate(void)
19681968
}
19691969

19701970
/*
1971-
* RelationCacheAbort
1971+
* AtEOXactRelationCache
19721972
*
1973-
* Clean up the relcache at transaction abort.
1973+
* Clean up the relcache at transaction commit or abort.
19741974
*
1975-
* What we need to do here is reset relcache entry ref counts to
1976-
* their normal not-in-a-transaction state. A ref count may be
1975+
* During transaction abort, we must reset relcache entry ref counts
1976+
* to their normal not-in-a-transaction state. A ref count may be
19771977
* too high because some routine was exited by elog() between
19781978
* incrementing and decrementing the count.
19791979
*
1980-
* XXX Maybe we should do this at transaction commit, too, in case
1981-
* someone forgets to decrement a refcount in a non-error path?
1980+
* During commit, we should not have to do this, but it's useful to
1981+
* check that the counts are correct to catch missed relcache closes.
1982+
* Since that's basically a debugging thing, only pay the cost when
1983+
* assert checking is enabled.
1984+
*
1985+
* In bootstrap mode, forget the debugging checks --- the bootstrap code
1986+
* expects relations to stay open across start/commit transaction calls.
19821987
*/
19831988
void
1984-
RelationCacheAbort(void)
1989+
AtEOXactRelationCache(bool commit)
19851990
{
19861991
HASH_SEQ_STATUS status;
19871992
RelIdCacheEnt *idhentry;
19881993

1994+
#ifdef USE_ASSERT_CHECKING
1995+
if (commit && IsBootstrapProcessingMode())
1996+
return;
1997+
#else
1998+
if (commit)
1999+
return;
2000+
#endif
2001+
19892002
hash_seq_init(&status, RelationIdCache);
19902003

19912004
while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
19922005
{
19932006
Relation relation = idhentry->reldesc;
2007+
int expected_refcnt;
2008+
2009+
expected_refcnt = relation->rd_isnailed ? 1 : 0;
19942010

1995-
if (relation->rd_isnailed)
1996-
RelationSetReferenceCount(relation, 1);
2011+
if (commit)
2012+
{
2013+
if (relation->rd_refcnt != expected_refcnt)
2014+
{
2015+
elog(WARNING, "Relcache reference leak: relation \"%s\" has refcnt %d instead of %d",
2016+
RelationGetRelationName(relation),
2017+
relation->rd_refcnt, expected_refcnt);
2018+
RelationSetReferenceCount(relation, expected_refcnt);
2019+
}
2020+
}
19972021
else
1998-
RelationSetReferenceCount(relation, 0);
2022+
{
2023+
/* abort case, just reset it quietly */
2024+
RelationSetReferenceCount(relation, expected_refcnt);
2025+
}
19992026
}
20002027
}
20012028

src/include/utils/relcache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: relcache.h,v 1.32 2002/06/20 20:29:53 momjian Exp $
10+
* $Id: relcache.h,v 1.33 2002/08/02 22:36:05 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -63,7 +63,7 @@ extern void RelationCacheInvalidate(void);
6363

6464
extern void RelationPurgeLocalRelation(bool xactComitted);
6565

66-
extern void RelationCacheAbort(void);
66+
extern void AtEOXactRelationCache(bool commit);
6767

6868
/*
6969
* Routines to help manage rebuilding of relcache init file

0 commit comments

Comments
 (0)