Skip to content

Commit

Permalink
Make ResourceOwners more easily extensible.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hlinnaka committed Nov 8, 2023
1 parent b70c214 commit b8bff07
Show file tree
Hide file tree
Showing 36 changed files with 2,278 additions and 1,144 deletions.
51 changes: 49 additions & 2 deletions src/backend/access/common/tupdesc.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,34 @@
#include "common/hashfn.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/resowner_private.h"
#include "utils/resowner.h"
#include "utils/syscache.h"

/* ResourceOwner callbacks to hold tupledesc references */
static void ResOwnerReleaseTupleDesc(Datum res);
static char *ResOwnerPrintTupleDesc(Datum res);

static const ResourceOwnerDesc tupdesc_resowner_desc =
{
.name = "tupdesc reference",
.release_phase = RESOURCE_RELEASE_AFTER_LOCKS,
.release_priority = RELEASE_PRIO_TUPDESC_REFS,
.ReleaseResource = ResOwnerReleaseTupleDesc,
.DebugPrint = ResOwnerPrintTupleDesc
};

/* Convenience wrappers over ResourceOwnerRemember/Forget */
static inline void
ResourceOwnerRememberTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
{
ResourceOwnerRemember(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
}

static inline void
ResourceOwnerForgetTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
{
ResourceOwnerForget(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
}

/*
* CreateTemplateTupleDesc
Expand Down Expand Up @@ -364,7 +389,7 @@ IncrTupleDescRefCount(TupleDesc tupdesc)
{
Assert(tupdesc->tdrefcount >= 0);

ResourceOwnerEnlargeTupleDescs(CurrentResourceOwner);
ResourceOwnerEnlarge(CurrentResourceOwner);
tupdesc->tdrefcount++;
ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
}
Expand Down Expand Up @@ -847,3 +872,25 @@ TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)

return result;
}

/* ResourceOwner callbacks */

static void
ResOwnerReleaseTupleDesc(Datum res)
{
TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);

/* Like DecrTupleDescRefCount, but don't call ResourceOwnerForget() */
Assert(tupdesc->tdrefcount > 0);
if (--tupdesc->tdrefcount == 0)
FreeTupleDesc(tupdesc);
}

static char *
ResOwnerPrintTupleDesc(Datum res)
{
TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);

return psprintf("TupleDesc %p (%u,%d)",
tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
}
14 changes: 14 additions & 0 deletions src/backend/access/transam/xact.c
Original file line number Diff line number Diff line change
Expand Up @@ -5172,9 +5172,23 @@ AbortSubTransaction(void)
ResourceOwnerRelease(s->curTransactionOwner,
RESOURCE_RELEASE_BEFORE_LOCKS,
false, false);

AtEOSubXact_RelationCache(false, s->subTransactionId,
s->parent->subTransactionId);


/*
* AtEOSubXact_Inval sometimes needs to temporarily bump the refcount
* on the relcache entries that it processes. We cannot use the
* subtransaction's resource owner anymore, because we've already
* started releasing it. But we can use the parent resource owner.
*/
CurrentResourceOwner = s->parent->curTransactionOwner;

AtEOSubXact_Inval(false);

CurrentResourceOwner = s->curTransactionOwner;

ResourceOwnerRelease(s->curTransactionOwner,
RESOURCE_RELEASE_LOCKS,
false, false);
Expand Down
2 changes: 0 additions & 2 deletions src/backend/jit/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "jit/jit.h"
#include "miscadmin.h"
#include "utils/fmgrprotos.h"
#include "utils/resowner_private.h"

/* GUCs */
bool jit_enabled = true;
Expand Down Expand Up @@ -140,7 +139,6 @@ jit_release_context(JitContext *context)
if (provider_successfully_loaded)
provider.release_context(context);

ResourceOwnerForgetJIT(context->resowner, PointerGetDatum(context));
pfree(context);
}

Expand Down
45 changes: 42 additions & 3 deletions src/backend/jit/llvm/llvmjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#include "portability/instr_time.h"
#include "storage/ipc.h"
#include "utils/memutils.h"
#include "utils/resowner_private.h"
#include "utils/resowner.h"

#define LLVMJIT_LLVM_CONTEXT_REUSE_MAX 100

Expand Down Expand Up @@ -131,6 +131,30 @@ static LLVMOrcLLJITRef llvm_create_jit_instance(LLVMTargetMachineRef tm);
static char *llvm_error_message(LLVMErrorRef error);
#endif /* LLVM_VERSION_MAJOR > 11 */

/* ResourceOwner callbacks to hold JitContexts */
static void ResOwnerReleaseJitContext(Datum res);

static const ResourceOwnerDesc jit_resowner_desc =
{
.name = "LLVM JIT context",
.release_phase = RESOURCE_RELEASE_BEFORE_LOCKS,
.release_priority = RELEASE_PRIO_JIT_CONTEXTS,
.ReleaseResource = ResOwnerReleaseJitContext,
.DebugPrint = NULL /* the default message is fine */
};

/* Convenience wrappers over ResourceOwnerRemember/Forget */
static inline void
ResourceOwnerRememberJIT(ResourceOwner owner, LLVMJitContext *handle)
{
ResourceOwnerRemember(owner, PointerGetDatum(handle), &jit_resowner_desc);
}
static inline void
ResourceOwnerForgetJIT(ResourceOwner owner, LLVMJitContext *handle)
{
ResourceOwnerForget(owner, PointerGetDatum(handle), &jit_resowner_desc);
}

PG_MODULE_MAGIC;


Expand Down Expand Up @@ -220,15 +244,15 @@ llvm_create_context(int jitFlags)

llvm_recreate_llvm_context();

ResourceOwnerEnlargeJIT(CurrentResourceOwner);
ResourceOwnerEnlarge(CurrentResourceOwner);

context = MemoryContextAllocZero(TopMemoryContext,
sizeof(LLVMJitContext));
context->base.flags = jitFlags;

/* ensure cleanup */
context->base.resowner = CurrentResourceOwner;
ResourceOwnerRememberJIT(CurrentResourceOwner, PointerGetDatum(context));
ResourceOwnerRememberJIT(CurrentResourceOwner, context);

llvm_jit_context_in_use_count++;

Expand Down Expand Up @@ -300,6 +324,9 @@ llvm_release_context(JitContext *context)
llvm_jit_context->handles = NIL;

llvm_leave_fatal_on_oom();

if (context->resowner)
ResourceOwnerForgetJIT(context->resowner, llvm_jit_context);
}

/*
Expand Down Expand Up @@ -1394,3 +1421,15 @@ llvm_error_message(LLVMErrorRef error)
}

#endif /* LLVM_VERSION_MAJOR > 11 */

/*
* ResourceOwner callbacks
*/
static void
ResOwnerReleaseJitContext(Datum res)
{
JitContext *context = (JitContext *) DatumGetPointer(res);

context->resowner = NULL;
jit_release_context(context);
}

0 comments on commit b8bff07

Please sign in to comment.