diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2023-11-08 13:30:50 +0200 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2023-11-08 13:30:50 +0200 |
commit | b8bff07daa85c837a2747b4d35cd5a27e73fb7b2 (patch) | |
tree | b9c98f5071e676c3bb7f94a6a6909406e3d9531b /src/backend/utils/cache/relcache.c | |
parent | b70c2143bbbe291fe2b444150772972fa53972f1 (diff) | |
download | postgresql-b8bff07daa85c837a2747b4d35cd5a27e73fb7b2.tar.gz postgresql-b8bff07daa85c837a2747b4d35cd5a27e73fb7b2.zip |
Make ResourceOwners more easily extensible.
Instead of having a separate array/hash for each resource kind, use a
single array and hash to hold all kinds of resources. This makes it
possible to introduce new resource "kinds" without having to modify
the ResourceOwnerData struct. In particular, this makes it possible
for extensions to register custom resource kinds.
The old approach was to have a small array of resources of each kind,
and if it fills up, switch to a hash table. The new approach also uses
an array and a hash, but now the array and the hash are used at the
same time. The array is used to hold the recently added resources, and
when it fills up, they are moved to the hash. This keeps the access to
recent entries fast, even when there are a lot of long-held resources.
All the resource-specific ResourceOwnerEnlarge*(),
ResourceOwnerRemember*(), and ResourceOwnerForget*() functions have
been replaced with three generic functions that take resource kind as
argument. For convenience, we still define resource-specific wrapper
macros around the generic functions with the old names, but they are
now defined in the source files that use those resource kinds.
The release callback no longer needs to call ResourceOwnerForget on
the resource being released. ResourceOwnerRelease unregisters the
resource from the owner before calling the callback. That needed some
changes in bufmgr.c and some other files, where releasing the
resources previously always called ResourceOwnerForget.
Each resource kind specifies a release priority, and
ResourceOwnerReleaseAll releases the resources in priority order. To
make that possible, we have to restrict what you can do between
phases. After calling ResourceOwnerRelease(), you are no longer
allowed to remember any more resources in it or to forget any
previously remembered resources by calling ResourceOwnerForget. There
was one case where that was done previously. At subtransaction commit,
AtEOSubXact_Inval() would handle the invalidation messages and call
RelationFlushRelation(), which temporarily increased the reference
count on the relation being flushed. We now switch to the parent
subtransaction's resource owner before calling AtEOSubXact_Inval(), so
that there is a valid ResourceOwner to temporarily hold that relcache
reference.
Other end-of-xact routines make similar calls to AtEOXact_Inval()
between release phases, but I didn't see any regression test failures
from those, so I'm not sure if they could reach a codepath that needs
remembering extra resources.
There were two exceptions to how the resource leak WARNINGs on commit
were printed previously: llvmjit silently released the context without
printing the warning, and a leaked buffer io triggered a PANIC. Now
everything prints a WARNING, including those cases.
Add tests in src/test/modules/test_resowner.
Reviewed-by: Aleksander Alekseev, Michael Paquier, Julien Rouhaud
Reviewed-by: Kyotaro Horiguchi, Hayato Kuroda, Álvaro Herrera, Zhihong Yu
Reviewed-by: Peter Eisentraut, Andres Freund
Discussion: https://www.postgresql.org/message-id/cbfabeb0-cd3c-e951-a572-19b365ed314d%40iki.fi
Diffstat (limited to 'src/backend/utils/cache/relcache.c')
-rw-r--r-- | src/backend/utils/cache/relcache.c | 64 |
1 files changed, 62 insertions, 2 deletions
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index a49ab465b36..b3faccbefe5 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -80,13 +80,14 @@ #include "storage/smgr.h" #include "utils/array.h" #include "utils/builtins.h" +#include "utils/catcache.h" #include "utils/datum.h" #include "utils/fmgroids.h" #include "utils/inval.h" #include "utils/lsyscache.h" #include "utils/memutils.h" #include "utils/relmapper.h" -#include "utils/resowner_private.h" +#include "utils/resowner.h" #include "utils/snapmgr.h" #include "utils/syscache.h" @@ -273,6 +274,7 @@ static HTAB *OpClassCache = NULL; /* non-export function prototypes */ +static void RelationCloseCleanup(Relation relation); static void RelationDestroyRelation(Relation relation, bool remember_tupdesc); static void RelationClearRelation(Relation relation, bool rebuild); @@ -2115,6 +2117,31 @@ RelationIdGetRelation(Oid relationId) * ---------------------------------------------------------------- */ +/* ResourceOwner callbacks to track relcache references */ +static void ResOwnerReleaseRelation(Datum res); +static char *ResOwnerPrintRelCache(Datum res); + +static const ResourceOwnerDesc relref_resowner_desc = +{ + .name = "relcache reference", + .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS, + .release_priority = RELEASE_PRIO_RELCACHE_REFS, + .ReleaseResource = ResOwnerReleaseRelation, + .DebugPrint = ResOwnerPrintRelCache +}; + +/* Convenience wrappers over ResourceOwnerRemember/Forget */ +static inline void +ResourceOwnerRememberRelationRef(ResourceOwner owner, Relation rel) +{ + ResourceOwnerRemember(owner, PointerGetDatum(rel), &relref_resowner_desc); +} +static inline void +ResourceOwnerForgetRelationRef(ResourceOwner owner, Relation rel) +{ + ResourceOwnerForget(owner, PointerGetDatum(rel), &relref_resowner_desc); +} + /* * RelationIncrementReferenceCount * Increments relation reference count. @@ -2126,7 +2153,7 @@ RelationIdGetRelation(Oid relationId) void RelationIncrementReferenceCount(Relation rel) { - ResourceOwnerEnlargeRelationRefs(CurrentResourceOwner); + ResourceOwnerEnlarge(CurrentResourceOwner); rel->rd_refcnt += 1; if (!IsBootstrapProcessingMode()) ResourceOwnerRememberRelationRef(CurrentResourceOwner, rel); @@ -2162,6 +2189,12 @@ RelationClose(Relation relation) /* Note: no locking manipulations needed */ RelationDecrementReferenceCount(relation); + RelationCloseCleanup(relation); +} + +static void +RelationCloseCleanup(Relation relation) +{ /* * If the relation is no longer open in this session, we can clean up any * stale partition descriptors it has. This is unlikely, so check to see @@ -6813,3 +6846,30 @@ unlink_initfile(const char *initfilename, int elevel) initfilename))); } } + +/* + * ResourceOwner callbacks + */ +static char * +ResOwnerPrintRelCache(Datum res) +{ + Relation rel = (Relation) DatumGetPointer(res); + + return psprintf("relation \"%s\"", RelationGetRelationName(rel)); +} + +static void +ResOwnerReleaseRelation(Datum res) +{ + Relation rel = (Relation) DatumGetPointer(res); + + /* + * This reference has already been removed from the resource owner, so + * just decrement reference count without calling + * ResourceOwnerForgetRelationRef. + */ + Assert(rel->rd_refcnt > 0); + rel->rd_refcnt -= 1; + + RelationCloseCleanup((Relation) res); +} |