diff options
author | Heikki Linnakangas | 2023-11-08 11:30:50 +0000 |
---|---|---|
committer | Heikki Linnakangas | 2023-11-08 11:30:50 +0000 |
commit | b8bff07daa85c837a2747b4d35cd5a27e73fb7b2 (patch) | |
tree | b9c98f5071e676c3bb7f94a6a6909406e3d9531b /src/include/utils/resowner.h | |
parent | b70c2143bbbe291fe2b444150772972fa53972f1 (diff) |
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/include/utils/resowner.h')
-rw-r--r-- | src/include/utils/resowner.h | 94 |
1 files changed, 87 insertions, 7 deletions
diff --git a/src/include/utils/resowner.h b/src/include/utils/resowner.h index cb35e9e0904..0735480214e 100644 --- a/src/include/utils/resowner.h +++ b/src/include/utils/resowner.h @@ -37,19 +37,87 @@ extern PGDLLIMPORT ResourceOwner AuxProcessResourceOwner; /* * Resource releasing is done in three phases: pre-locks, locks, and - * post-locks. The pre-lock phase must release any resources that are - * visible to other backends (such as pinned buffers); this ensures that - * when we release a lock that another backend may be waiting on, it will - * see us as being fully out of our transaction. The post-lock phase - * should be used for backend-internal cleanup. + * post-locks. The pre-lock phase must release any resources that are visible + * to other backends (such as pinned buffers); this ensures that when we + * release a lock that another backend may be waiting on, it will see us as + * being fully out of our transaction. The post-lock phase should be used for + * backend-internal cleanup. + * + * Within each phase, resources are released in priority order. Priority is + * just an integer specified in ResourceOwnerDesc. The priorities of built-in + * resource types are given below, extensions may use any priority relative to + * those or RELEASE_PRIO_FIRST/LAST. RELEASE_PRIO_FIRST is a fine choice if + * your resource doesn't depend on any other resources. */ typedef enum { - RESOURCE_RELEASE_BEFORE_LOCKS, + RESOURCE_RELEASE_BEFORE_LOCKS = 1, RESOURCE_RELEASE_LOCKS, RESOURCE_RELEASE_AFTER_LOCKS, } ResourceReleasePhase; +typedef uint32 ResourceReleasePriority; + +/* priorities of built-in BEFORE_LOCKS resources */ +#define RELEASE_PRIO_BUFFER_IOS 100 +#define RELEASE_PRIO_BUFFER_PINS 200 +#define RELEASE_PRIO_RELCACHE_REFS 300 +#define RELEASE_PRIO_DSMS 400 +#define RELEASE_PRIO_JIT_CONTEXTS 500 +#define RELEASE_PRIO_CRYPTOHASH_CONTEXTS 600 +#define RELEASE_PRIO_HMAC_CONTEXTS 700 + +/* priorities of built-in AFTER_LOCKS resources */ +#define RELEASE_PRIO_CATCACHE_REFS 100 +#define RELEASE_PRIO_CATCACHE_LIST_REFS 200 +#define RELEASE_PRIO_PLANCACHE_REFS 300 +#define RELEASE_PRIO_TUPDESC_REFS 400 +#define RELEASE_PRIO_SNAPSHOT_REFS 500 +#define RELEASE_PRIO_FILES 600 + +/* 0 is considered invalid */ +#define RELEASE_PRIO_FIRST 1 +#define RELEASE_PRIO_LAST UINT32_MAX + +/* + * In order to track an object, resowner.c needs a few callbacks for it. + * The callbacks for resources of a specific kind are encapsulated in + * ResourceOwnerDesc. + * + * Note that the callbacks occur post-commit or post-abort, so the callback + * functions can only do noncritical cleanup and must not fail. + */ +typedef struct ResourceOwnerDesc +{ + const char *name; /* name for the object kind, for debugging */ + + /* when are these objects released? */ + ResourceReleasePhase release_phase; + ResourceReleasePriority release_priority; + + /* + * Release resource. + * + * This is called for each resource in the resource owner, in the order + * specified by 'release_phase' and 'release_priority' when the whole + * resource owner is been released or when ResourceOwnerReleaseAllOfKind() + * is called. The resource is implicitly removed from the owner, the + * callback function doesn't need to call ResourceOwnerForget. + */ + void (*ReleaseResource) (Datum res); + + /* + * Format a string describing the resource, for debugging purposes. If a + * resource has not been properly released before commit, this is used to + * print a WARNING. + * + * This can be left to NULL, in which case a generic "[resource name]: %p" + * format is used. + */ + char *(*DebugPrint) (Datum res); + +} ResourceOwnerDesc; + /* * Dynamically loaded modules can get control during ResourceOwnerRelease * by providing a callback of this form. @@ -71,16 +139,28 @@ extern void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel); -extern void ResourceOwnerReleaseAllPlanCacheRefs(ResourceOwner owner); extern void ResourceOwnerDelete(ResourceOwner owner); extern ResourceOwner ResourceOwnerGetParent(ResourceOwner owner); extern void ResourceOwnerNewParent(ResourceOwner owner, ResourceOwner newparent); + +extern void ResourceOwnerEnlarge(ResourceOwner owner); +extern void ResourceOwnerRemember(ResourceOwner owner, Datum res, const ResourceOwnerDesc *kind); +extern void ResourceOwnerForget(ResourceOwner owner, Datum res, const ResourceOwnerDesc *kind); + +extern void ResourceOwnerReleaseAllOfKind(ResourceOwner owner, const ResourceOwnerDesc *kind); + extern void RegisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg); extern void UnregisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg); + extern void CreateAuxProcessResourceOwner(void); extern void ReleaseAuxProcessResources(bool isCommit); +/* special support for local lock management */ +struct LOCALLOCK; +extern void ResourceOwnerRememberLock(ResourceOwner owner, struct LOCALLOCK *locallock); +extern void ResourceOwnerForgetLock(ResourceOwner owner, struct LOCALLOCK *locallock); + #endif /* RESOWNER_H */ |