Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb8bff07

Browse files
committed
Make ResourceOwners more easily extensible.
Instead of having a separate array/hash for each resource kind, use asingle array and hash to hold all kinds of resources. This makes itpossible to introduce new resource "kinds" without having to modifythe ResourceOwnerData struct. In particular, this makes it possiblefor 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 usesan array and a hash, but now the array and the hash are used at thesame time. The array is used to hold the recently added resources, andwhen it fills up, they are moved to the hash. This keeps the access torecent entries fast, even when there are a lot of long-held resources.All the resource-specific ResourceOwnerEnlarge*(),ResourceOwnerRemember*(), and ResourceOwnerForget*() functions havebeen replaced with three generic functions that take resource kind asargument. For convenience, we still define resource-specific wrappermacros around the generic functions with the old names, but they arenow defined in the source files that use those resource kinds.The release callback no longer needs to call ResourceOwnerForget onthe resource being released. ResourceOwnerRelease unregisters theresource from the owner before calling the callback. That needed somechanges in bufmgr.c and some other files, where releasing theresources previously always called ResourceOwnerForget.Each resource kind specifies a release priority, andResourceOwnerReleaseAll releases the resources in priority order. Tomake that possible, we have to restrict what you can do betweenphases. After calling ResourceOwnerRelease(), you are no longerallowed to remember any more resources in it or to forget anypreviously remembered resources by calling ResourceOwnerForget. Therewas one case where that was done previously. At subtransaction commit,AtEOSubXact_Inval() would handle the invalidation messages and callRelationFlushRelation(), which temporarily increased the referencecount on the relation being flushed. We now switch to the parentsubtransaction's resource owner before calling AtEOSubXact_Inval(), sothat there is a valid ResourceOwner to temporarily hold that relcachereference.Other end-of-xact routines make similar calls to AtEOXact_Inval()between release phases, but I didn't see any regression test failuresfrom those, so I'm not sure if they could reach a codepath that needsremembering extra resources.There were two exceptions to how the resource leak WARNINGs on commitwere printed previously: llvmjit silently released the context withoutprinting the warning, and a leaked buffer io triggered a PANIC. Noweverything prints a WARNING, including those cases.Add tests in src/test/modules/test_resowner.Reviewed-by: Aleksander Alekseev, Michael Paquier, Julien RouhaudReviewed-by: Kyotaro Horiguchi, Hayato Kuroda, Álvaro Herrera, Zhihong YuReviewed-by: Peter Eisentraut, Andres FreundDiscussion:https://www.postgresql.org/message-id/cbfabeb0-cd3c-e951-a572-19b365ed314d%40iki.fi
1 parentb70c214 commitb8bff07

36 files changed

+2278
-1144
lines changed

‎src/backend/access/common/tupdesc.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,34 @@
2727
#include"common/hashfn.h"
2828
#include"utils/builtins.h"
2929
#include"utils/datum.h"
30-
#include"utils/resowner_private.h"
30+
#include"utils/resowner.h"
3131
#include"utils/syscache.h"
3232

33+
/* ResourceOwner callbacks to hold tupledesc references */
34+
staticvoidResOwnerReleaseTupleDesc(Datumres);
35+
staticchar*ResOwnerPrintTupleDesc(Datumres);
36+
37+
staticconstResourceOwnerDesctupdesc_resowner_desc=
38+
{
39+
.name="tupdesc reference",
40+
.release_phase=RESOURCE_RELEASE_AFTER_LOCKS,
41+
.release_priority=RELEASE_PRIO_TUPDESC_REFS,
42+
.ReleaseResource=ResOwnerReleaseTupleDesc,
43+
.DebugPrint=ResOwnerPrintTupleDesc
44+
};
45+
46+
/* Convenience wrappers over ResourceOwnerRemember/Forget */
47+
staticinlinevoid
48+
ResourceOwnerRememberTupleDesc(ResourceOwnerowner,TupleDesctupdesc)
49+
{
50+
ResourceOwnerRemember(owner,PointerGetDatum(tupdesc),&tupdesc_resowner_desc);
51+
}
52+
53+
staticinlinevoid
54+
ResourceOwnerForgetTupleDesc(ResourceOwnerowner,TupleDesctupdesc)
55+
{
56+
ResourceOwnerForget(owner,PointerGetDatum(tupdesc),&tupdesc_resowner_desc);
57+
}
3358

3459
/*
3560
* CreateTemplateTupleDesc
@@ -364,7 +389,7 @@ IncrTupleDescRefCount(TupleDesc tupdesc)
364389
{
365390
Assert(tupdesc->tdrefcount >=0);
366391

367-
ResourceOwnerEnlargeTupleDescs(CurrentResourceOwner);
392+
ResourceOwnerEnlarge(CurrentResourceOwner);
368393
tupdesc->tdrefcount++;
369394
ResourceOwnerRememberTupleDesc(CurrentResourceOwner,tupdesc);
370395
}
@@ -847,3 +872,25 @@ TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
847872

848873
returnresult;
849874
}
875+
876+
/* ResourceOwner callbacks */
877+
878+
staticvoid
879+
ResOwnerReleaseTupleDesc(Datumres)
880+
{
881+
TupleDesctupdesc= (TupleDesc)DatumGetPointer(res);
882+
883+
/* Like DecrTupleDescRefCount, but don't call ResourceOwnerForget() */
884+
Assert(tupdesc->tdrefcount>0);
885+
if (--tupdesc->tdrefcount==0)
886+
FreeTupleDesc(tupdesc);
887+
}
888+
889+
staticchar*
890+
ResOwnerPrintTupleDesc(Datumres)
891+
{
892+
TupleDesctupdesc= (TupleDesc)DatumGetPointer(res);
893+
894+
returnpsprintf("TupleDesc %p (%u,%d)",
895+
tupdesc,tupdesc->tdtypeid,tupdesc->tdtypmod);
896+
}

‎src/backend/access/transam/xact.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5172,9 +5172,23 @@ AbortSubTransaction(void)
51725172
ResourceOwnerRelease(s->curTransactionOwner,
51735173
RESOURCE_RELEASE_BEFORE_LOCKS,
51745174
false, false);
5175+
51755176
AtEOSubXact_RelationCache(false,s->subTransactionId,
51765177
s->parent->subTransactionId);
5178+
5179+
5180+
/*
5181+
* AtEOSubXact_Inval sometimes needs to temporarily bump the refcount
5182+
* on the relcache entries that it processes. We cannot use the
5183+
* subtransaction's resource owner anymore, because we've already
5184+
* started releasing it. But we can use the parent resource owner.
5185+
*/
5186+
CurrentResourceOwner=s->parent->curTransactionOwner;
5187+
51775188
AtEOSubXact_Inval(false);
5189+
5190+
CurrentResourceOwner=s->curTransactionOwner;
5191+
51785192
ResourceOwnerRelease(s->curTransactionOwner,
51795193
RESOURCE_RELEASE_LOCKS,
51805194
false, false);

‎src/backend/jit/jit.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include"jit/jit.h"
2727
#include"miscadmin.h"
2828
#include"utils/fmgrprotos.h"
29-
#include"utils/resowner_private.h"
3029

3130
/* GUCs */
3231
booljit_enabled= true;
@@ -140,7 +139,6 @@ jit_release_context(JitContext *context)
140139
if (provider_successfully_loaded)
141140
provider.release_context(context);
142141

143-
ResourceOwnerForgetJIT(context->resowner,PointerGetDatum(context));
144142
pfree(context);
145143
}
146144

‎src/backend/jit/llvm/llvmjit.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include"portability/instr_time.h"
4646
#include"storage/ipc.h"
4747
#include"utils/memutils.h"
48-
#include"utils/resowner_private.h"
48+
#include"utils/resowner.h"
4949

5050
#defineLLVMJIT_LLVM_CONTEXT_REUSE_MAX 100
5151

@@ -131,6 +131,30 @@ static LLVMOrcLLJITRef llvm_create_jit_instance(LLVMTargetMachineRef tm);
131131
staticchar*llvm_error_message(LLVMErrorReferror);
132132
#endif/* LLVM_VERSION_MAJOR > 11 */
133133

134+
/* ResourceOwner callbacks to hold JitContexts */
135+
staticvoidResOwnerReleaseJitContext(Datumres);
136+
137+
staticconstResourceOwnerDescjit_resowner_desc=
138+
{
139+
.name="LLVM JIT context",
140+
.release_phase=RESOURCE_RELEASE_BEFORE_LOCKS,
141+
.release_priority=RELEASE_PRIO_JIT_CONTEXTS,
142+
.ReleaseResource=ResOwnerReleaseJitContext,
143+
.DebugPrint=NULL/* the default message is fine */
144+
};
145+
146+
/* Convenience wrappers over ResourceOwnerRemember/Forget */
147+
staticinlinevoid
148+
ResourceOwnerRememberJIT(ResourceOwnerowner,LLVMJitContext*handle)
149+
{
150+
ResourceOwnerRemember(owner,PointerGetDatum(handle),&jit_resowner_desc);
151+
}
152+
staticinlinevoid
153+
ResourceOwnerForgetJIT(ResourceOwnerowner,LLVMJitContext*handle)
154+
{
155+
ResourceOwnerForget(owner,PointerGetDatum(handle),&jit_resowner_desc);
156+
}
157+
134158
PG_MODULE_MAGIC;
135159

136160

@@ -220,15 +244,15 @@ llvm_create_context(int jitFlags)
220244

221245
llvm_recreate_llvm_context();
222246

223-
ResourceOwnerEnlargeJIT(CurrentResourceOwner);
247+
ResourceOwnerEnlarge(CurrentResourceOwner);
224248

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

229253
/* ensure cleanup */
230254
context->base.resowner=CurrentResourceOwner;
231-
ResourceOwnerRememberJIT(CurrentResourceOwner,PointerGetDatum(context));
255+
ResourceOwnerRememberJIT(CurrentResourceOwner,context);
232256

233257
llvm_jit_context_in_use_count++;
234258

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

302326
llvm_leave_fatal_on_oom();
327+
328+
if (context->resowner)
329+
ResourceOwnerForgetJIT(context->resowner,llvm_jit_context);
303330
}
304331

305332
/*
@@ -1394,3 +1421,15 @@ llvm_error_message(LLVMErrorRef error)
13941421
}
13951422

13961423
#endif/* LLVM_VERSION_MAJOR > 11 */
1424+
1425+
/*
1426+
* ResourceOwner callbacks
1427+
*/
1428+
staticvoid
1429+
ResOwnerReleaseJitContext(Datumres)
1430+
{
1431+
JitContext*context= (JitContext*)DatumGetPointer(res);
1432+
1433+
context->resowner=NULL;
1434+
jit_release_context(context);
1435+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp