PostgreSQL Source Code  git master
resowner.c File Reference
#include "postgres.h"
#include "common/cryptohash.h"
#include "common/hashfn.h"
#include "common/hmac.h"
#include "jit/jit.h"
#include "storage/bufmgr.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/resowner_private.h"
#include "utils/snapmgr.h"
Include dependency graph for resowner.c:

Go to the source code of this file.

Data Structures

struct  ResourceArray
 
struct  ResourceOwnerData
 
struct  ResourceReleaseCallbackItem
 

Macros

#define FileGetDatum(file)   Int32GetDatum(file)
 
#define DatumGetFile(datum)   ((File) DatumGetInt32(datum))
 
#define BufferGetDatum(buffer)   Int32GetDatum(buffer)
 
#define DatumGetBuffer(datum)   ((Buffer) DatumGetInt32(datum))
 
#define RESARRAY_INIT_SIZE   16
 
#define RESARRAY_MAX_ARRAY   64
 
#define RESARRAY_IS_ARRAY(resarr)   ((resarr)->capacity <= RESARRAY_MAX_ARRAY)
 
#define RESARRAY_MAX_ITEMS(capacity)    ((capacity) <= RESARRAY_MAX_ARRAY ? (capacity) : (capacity)/4 * 3)
 
#define MAX_RESOWNER_LOCKS   15
 

Typedefs

typedef struct ResourceArray ResourceArray
 
typedef struct ResourceOwnerData ResourceOwnerData
 
typedef struct ResourceReleaseCallbackItem ResourceReleaseCallbackItem
 

Functions

static void ResourceArrayInit (ResourceArray *resarr, Datum invalidval)
 
static void ResourceArrayEnlarge (ResourceArray *resarr)
 
static void ResourceArrayAdd (ResourceArray *resarr, Datum value)
 
static bool ResourceArrayRemove (ResourceArray *resarr, Datum value)
 
static bool ResourceArrayGetAny (ResourceArray *resarr, Datum *value)
 
static void ResourceArrayFree (ResourceArray *resarr)
 
static void ResourceOwnerReleaseInternal (ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
 
static void ReleaseAuxProcessResourcesCallback (int code, Datum arg)
 
static void PrintRelCacheLeakWarning (Relation rel)
 
static void PrintPlanCacheLeakWarning (CachedPlan *plan)
 
static void PrintTupleDescLeakWarning (TupleDesc tupdesc)
 
static void PrintSnapshotLeakWarning (Snapshot snapshot)
 
static void PrintFileLeakWarning (File file)
 
static void PrintDSMLeakWarning (dsm_segment *seg)
 
static void PrintCryptoHashLeakWarning (Datum handle)
 
static void PrintHMACLeakWarning (Datum handle)
 
ResourceOwner ResourceOwnerCreate (ResourceOwner parent, const char *name)
 
void ResourceOwnerRelease (ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
 
void ResourceOwnerReleaseAllPlanCacheRefs (ResourceOwner owner)
 
void ResourceOwnerDelete (ResourceOwner owner)
 
ResourceOwner ResourceOwnerGetParent (ResourceOwner owner)
 
void ResourceOwnerNewParent (ResourceOwner owner, ResourceOwner newparent)
 
void RegisterResourceReleaseCallback (ResourceReleaseCallback callback, void *arg)
 
void UnregisterResourceReleaseCallback (ResourceReleaseCallback callback, void *arg)
 
void CreateAuxProcessResourceOwner (void)
 
void ReleaseAuxProcessResources (bool isCommit)
 
void ResourceOwnerEnlargeBuffers (ResourceOwner owner)
 
void ResourceOwnerRememberBuffer (ResourceOwner owner, Buffer buffer)
 
void ResourceOwnerForgetBuffer (ResourceOwner owner, Buffer buffer)
 
void ResourceOwnerRememberLock (ResourceOwner owner, LOCALLOCK *locallock)
 
void ResourceOwnerForgetLock (ResourceOwner owner, LOCALLOCK *locallock)
 
void ResourceOwnerEnlargeCatCacheRefs (ResourceOwner owner)
 
void ResourceOwnerRememberCatCacheRef (ResourceOwner owner, HeapTuple tuple)
 
void ResourceOwnerForgetCatCacheRef (ResourceOwner owner, HeapTuple tuple)
 
void ResourceOwnerEnlargeCatCacheListRefs (ResourceOwner owner)
 
void ResourceOwnerRememberCatCacheListRef (ResourceOwner owner, CatCList *list)
 
void ResourceOwnerForgetCatCacheListRef (ResourceOwner owner, CatCList *list)
 
void ResourceOwnerEnlargeRelationRefs (ResourceOwner owner)
 
void ResourceOwnerRememberRelationRef (ResourceOwner owner, Relation rel)
 
void ResourceOwnerForgetRelationRef (ResourceOwner owner, Relation rel)
 
void ResourceOwnerEnlargePlanCacheRefs (ResourceOwner owner)
 
void ResourceOwnerRememberPlanCacheRef (ResourceOwner owner, CachedPlan *plan)
 
void ResourceOwnerForgetPlanCacheRef (ResourceOwner owner, CachedPlan *plan)
 
void ResourceOwnerEnlargeTupleDescs (ResourceOwner owner)
 
void ResourceOwnerRememberTupleDesc (ResourceOwner owner, TupleDesc tupdesc)
 
void ResourceOwnerForgetTupleDesc (ResourceOwner owner, TupleDesc tupdesc)
 
void ResourceOwnerEnlargeSnapshots (ResourceOwner owner)
 
void ResourceOwnerRememberSnapshot (ResourceOwner owner, Snapshot snapshot)
 
void ResourceOwnerForgetSnapshot (ResourceOwner owner, Snapshot snapshot)
 
void ResourceOwnerEnlargeFiles (ResourceOwner owner)
 
void ResourceOwnerRememberFile (ResourceOwner owner, File file)
 
void ResourceOwnerForgetFile (ResourceOwner owner, File file)
 
void ResourceOwnerEnlargeDSMs (ResourceOwner owner)
 
void ResourceOwnerRememberDSM (ResourceOwner owner, dsm_segment *seg)
 
void ResourceOwnerForgetDSM (ResourceOwner owner, dsm_segment *seg)
 
void ResourceOwnerEnlargeJIT (ResourceOwner owner)
 
void ResourceOwnerRememberJIT (ResourceOwner owner, Datum handle)
 
void ResourceOwnerForgetJIT (ResourceOwner owner, Datum handle)
 
void ResourceOwnerEnlargeCryptoHash (ResourceOwner owner)
 
void ResourceOwnerRememberCryptoHash (ResourceOwner owner, Datum handle)
 
void ResourceOwnerForgetCryptoHash (ResourceOwner owner, Datum handle)
 
void ResourceOwnerEnlargeHMAC (ResourceOwner owner)
 
void ResourceOwnerRememberHMAC (ResourceOwner owner, Datum handle)
 
void ResourceOwnerForgetHMAC (ResourceOwner owner, Datum handle)
 

Variables

ResourceOwner CurrentResourceOwner = NULL
 
ResourceOwner CurTransactionResourceOwner = NULL
 
ResourceOwner TopTransactionResourceOwner = NULL
 
ResourceOwner AuxProcessResourceOwner = NULL
 
static ResourceReleaseCallbackItemResourceRelease_callbacks = NULL
 

Macro Definition Documentation

◆ BufferGetDatum

#define BufferGetDatum (   buffer)    Int32GetDatum(buffer)

Definition at line 46 of file resowner.c.

◆ DatumGetBuffer

#define DatumGetBuffer (   datum)    ((Buffer) DatumGetInt32(datum))

Definition at line 47 of file resowner.c.

◆ DatumGetFile

#define DatumGetFile (   datum)    ((File) DatumGetInt32(datum))

Definition at line 45 of file resowner.c.

◆ FileGetDatum

#define FileGetDatum (   file)    Int32GetDatum(file)

Definition at line 44 of file resowner.c.

◆ MAX_RESOWNER_LOCKS

#define MAX_RESOWNER_LOCKS   15

Definition at line 110 of file resowner.c.

◆ RESARRAY_INIT_SIZE

#define RESARRAY_INIT_SIZE   16

Definition at line 79 of file resowner.c.

◆ RESARRAY_IS_ARRAY

#define RESARRAY_IS_ARRAY (   resarr)    ((resarr)->capacity <= RESARRAY_MAX_ARRAY)

Definition at line 85 of file resowner.c.

◆ RESARRAY_MAX_ARRAY

#define RESARRAY_MAX_ARRAY   64

Definition at line 84 of file resowner.c.

◆ RESARRAY_MAX_ITEMS

#define RESARRAY_MAX_ITEMS (   capacity)     ((capacity) <= RESARRAY_MAX_ARRAY ? (capacity) : (capacity)/4 * 3)

Definition at line 91 of file resowner.c.

Typedef Documentation

◆ ResourceArray

typedef struct ResourceArray ResourceArray

◆ ResourceOwnerData

◆ ResourceReleaseCallbackItem

Function Documentation

◆ CreateAuxProcessResourceOwner()

void CreateAuxProcessResourceOwner ( void  )

Definition at line 892 of file resowner.c.

893 {
895  Assert(CurrentResourceOwner == NULL);
896  AuxProcessResourceOwner = ResourceOwnerCreate(NULL, "AuxiliaryProcess");
898 
899  /*
900  * Register a shmem-exit callback for cleanup of aux-process resource
901  * owner. (This needs to run after, e.g., ShutdownXLOG.)
902  */
904 }
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
Assert(fmt[strlen(fmt) - 1] !='\n')
static void ReleaseAuxProcessResourcesCallback(int code, Datum arg)
Definition: resowner.c:934
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:428
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:149

References Assert(), AuxProcessResourceOwner, CurrentResourceOwner, on_shmem_exit(), ReleaseAuxProcessResourcesCallback(), and ResourceOwnerCreate().

Referenced by AuxiliaryProcessMain(), and InitPostgres().

◆ PrintCryptoHashLeakWarning()

static void PrintCryptoHashLeakWarning ( Datum  handle)
static

Definition at line 1446 of file resowner.c.

1447 {
1448  elog(WARNING, "cryptohash context reference leak: context %p still referenced",
1449  DatumGetPointer(handle));
1450 }
#define WARNING
Definition: elog.h:32
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:660

References DatumGetPointer(), elog(), and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintDSMLeakWarning()

static void PrintDSMLeakWarning ( dsm_segment seg)
static

Definition at line 1366 of file resowner.c.

1367 {
1368  elog(WARNING, "dynamic shared memory leak: segment %u still referenced",
1369  dsm_segment_handle(seg));
1370 }
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1095

References dsm_segment_handle(), elog(), and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintFileLeakWarning()

static void PrintFileLeakWarning ( File  file)
static

Definition at line 1321 of file resowner.c.

1322 {
1323  elog(WARNING, "temporary file leak: File %d still referenced",
1324  file);
1325 }

References elog(), and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintHMACLeakWarning()

static void PrintHMACLeakWarning ( Datum  handle)
static

Definition at line 1491 of file resowner.c.

1492 {
1493  elog(WARNING, "HMAC context reference leak: context %p still referenced",
1494  DatumGetPointer(handle));
1495 }

References DatumGetPointer(), elog(), and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintPlanCacheLeakWarning()

static void PrintPlanCacheLeakWarning ( CachedPlan plan)
static

Definition at line 1185 of file resowner.c.

1186 {
1187  elog(WARNING, "plancache reference leak: plan %p not closed", plan);
1188 }

References elog(), and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintRelCacheLeakWarning()

static void PrintRelCacheLeakWarning ( Relation  rel)
static

Definition at line 1140 of file resowner.c.

1141 {
1142  elog(WARNING, "relcache reference leak: relation \"%s\" not closed",
1144 }
#define RelationGetRelationName(relation)
Definition: rel.h:535

References elog(), RelationGetRelationName, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintSnapshotLeakWarning()

static void PrintSnapshotLeakWarning ( Snapshot  snapshot)
static

Definition at line 1275 of file resowner.c.

1276 {
1277  elog(WARNING, "Snapshot reference leak: Snapshot %p still referenced",
1278  snapshot);
1279 }

References elog(), and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintTupleDescLeakWarning()

static void PrintTupleDescLeakWarning ( TupleDesc  tupdesc)
static

Definition at line 1229 of file resowner.c.

1230 {
1231  elog(WARNING,
1232  "TupleDesc reference leak: TupleDesc %p (%u,%d) still referenced",
1233  tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
1234 }
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82

References elog(), TupleDescData::tdtypeid, TupleDescData::tdtypmod, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ RegisterResourceReleaseCallback()

void RegisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 854 of file resowner.c.

855 {
857 
858  item = (ResourceReleaseCallbackItem *)
861  item->callback = callback;
862  item->arg = arg;
865 }
MemoryContext TopMemoryContext
Definition: mcxt.c:130
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994
void * arg
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:161
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:156
ResourceReleaseCallback callback
Definition: resowner.c:157
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References ResourceReleaseCallbackItem::arg, arg, ResourceReleaseCallbackItem::callback, callback(), MemoryContextAlloc(), ResourceReleaseCallbackItem::next, ResourceRelease_callbacks, and TopMemoryContext.

Referenced by px_find_cipher(), and px_find_digest().

◆ ReleaseAuxProcessResources()

void ReleaseAuxProcessResources ( bool  isCommit)

Definition at line 912 of file resowner.c.

913 {
914  /*
915  * At this writing, the only thing that could actually get released is
916  * buffer pins; but we may as well do the full release protocol.
917  */
920  isCommit, true);
923  isCommit, true);
926  isCommit, true);
927 }
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:486
@ RESOURCE_RELEASE_LOCKS
Definition: resowner.h:49
@ RESOURCE_RELEASE_BEFORE_LOCKS
Definition: resowner.h:48
@ RESOURCE_RELEASE_AFTER_LOCKS
Definition: resowner.h:50

References AuxProcessResourceOwner, RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, and ResourceOwnerRelease().

Referenced by AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), InitPostgres(), ReleaseAuxProcessResourcesCallback(), and WalWriterMain().

◆ ReleaseAuxProcessResourcesCallback()

static void ReleaseAuxProcessResourcesCallback ( int  code,
Datum  arg 
)
static

Definition at line 934 of file resowner.c.

935 {
936  bool isCommit = (code == 0);
937 
938  ReleaseAuxProcessResources(isCommit);
939 }
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:912

References ReleaseAuxProcessResources().

Referenced by CreateAuxProcessResourceOwner().

◆ ResourceArrayAdd()

static void ResourceArrayAdd ( ResourceArray resarr,
Datum  value 
)
static

Definition at line 269 of file resowner.c.

270 {
271  uint32 idx;
272 
273  Assert(value != resarr->invalidval);
274  Assert(resarr->nitems < resarr->maxitems);
275 
276  if (RESARRAY_IS_ARRAY(resarr))
277  {
278  /* Append to linear array. */
279  idx = resarr->nitems;
280  }
281  else
282  {
283  /* Insert into first free slot at or after hash location. */
284  uint32 mask = resarr->capacity - 1;
285 
286  idx = DatumGetUInt32(hash_any((void *) &value, sizeof(value))) & mask;
287  for (;;)
288  {
289  if (resarr->itemsarr[idx] == resarr->invalidval)
290  break;
291  idx = (idx + 1) & mask;
292  }
293  }
294  resarr->lastidx = idx;
295  resarr->itemsarr[idx] = value;
296  resarr->nitems++;
297 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
unsigned int uint32
Definition: c.h:442
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
static struct @143 value
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:570
#define RESARRAY_IS_ARRAY(resarr)
Definition: resowner.c:85
Datum invalidval
Definition: resowner.c:68
uint32 maxitems
Definition: resowner.c:71
uint32 nitems
Definition: resowner.c:70
Datum * itemsarr
Definition: resowner.c:67
uint32 capacity
Definition: resowner.c:69
uint32 lastidx
Definition: resowner.c:72

References Assert(), ResourceArray::capacity, DatumGetUInt32(), hash_any(), idx(), ResourceArray::invalidval, ResourceArray::itemsarr, ResourceArray::lastidx, ResourceArray::maxitems, ResourceArray::nitems, RESARRAY_IS_ARRAY, and value.

Referenced by ResourceArrayEnlarge(), ResourceOwnerRememberBuffer(), ResourceOwnerRememberCatCacheListRef(), ResourceOwnerRememberCatCacheRef(), ResourceOwnerRememberCryptoHash(), ResourceOwnerRememberDSM(), ResourceOwnerRememberFile(), ResourceOwnerRememberHMAC(), ResourceOwnerRememberJIT(), ResourceOwnerRememberPlanCacheRef(), ResourceOwnerRememberRelationRef(), ResourceOwnerRememberSnapshot(), and ResourceOwnerRememberTupleDesc().

◆ ResourceArrayEnlarge()

static void ResourceArrayEnlarge ( ResourceArray resarr)
static

Definition at line 214 of file resowner.c.

215 {
216  uint32 i,
217  oldcap,
218  newcap;
219  Datum *olditemsarr;
220  Datum *newitemsarr;
221 
222  if (resarr->nitems < resarr->maxitems)
223  return; /* no work needed */
224 
225  olditemsarr = resarr->itemsarr;
226  oldcap = resarr->capacity;
227 
228  /* Double the capacity of the array (capacity must stay a power of 2!) */
229  newcap = (oldcap > 0) ? oldcap * 2 : RESARRAY_INIT_SIZE;
230  newitemsarr = (Datum *) MemoryContextAlloc(TopMemoryContext,
231  newcap * sizeof(Datum));
232  for (i = 0; i < newcap; i++)
233  newitemsarr[i] = resarr->invalidval;
234 
235  /* We assume we can't fail below this point, so OK to scribble on resarr */
236  resarr->itemsarr = newitemsarr;
237  resarr->capacity = newcap;
238  resarr->maxitems = RESARRAY_MAX_ITEMS(newcap);
239  resarr->nitems = 0;
240 
241  if (olditemsarr != NULL)
242  {
243  /*
244  * Transfer any pre-existing entries into the new array; they don't
245  * necessarily go where they were before, so this simple logic is the
246  * best way. Note that if we were managing the set as a simple array,
247  * the entries after nitems are garbage, but that shouldn't matter
248  * because we won't get here unless nitems was equal to oldcap.
249  */
250  for (i = 0; i < oldcap; i++)
251  {
252  if (olditemsarr[i] != resarr->invalidval)
253  ResourceArrayAdd(resarr, olditemsarr[i]);
254  }
255 
256  /* And release old array. */
257  pfree(olditemsarr);
258  }
259 
260  Assert(resarr->nitems < resarr->maxitems);
261 }
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1306
uintptr_t Datum
Definition: postgres.h:412
#define RESARRAY_MAX_ITEMS(capacity)
Definition: resowner.c:91
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:269
#define RESARRAY_INIT_SIZE
Definition: resowner.c:79

References Assert(), ResourceArray::capacity, i, ResourceArray::invalidval, ResourceArray::itemsarr, ResourceArray::maxitems, MemoryContextAlloc(), ResourceArray::nitems, pfree(), RESARRAY_INIT_SIZE, RESARRAY_MAX_ITEMS, ResourceArrayAdd(), and TopMemoryContext.

Referenced by ResourceOwnerEnlargeBuffers(), ResourceOwnerEnlargeCatCacheListRefs(), ResourceOwnerEnlargeCatCacheRefs(), ResourceOwnerEnlargeCryptoHash(), ResourceOwnerEnlargeDSMs(), ResourceOwnerEnlargeFiles(), ResourceOwnerEnlargeHMAC(), ResourceOwnerEnlargeJIT(), ResourceOwnerEnlargePlanCacheRefs(), ResourceOwnerEnlargeRelationRefs(), ResourceOwnerEnlargeSnapshots(), and ResourceOwnerEnlargeTupleDescs().

◆ ResourceArrayFree()

static void ResourceArrayFree ( ResourceArray resarr)
static

Definition at line 408 of file resowner.c.

409 {
410  if (resarr->itemsarr)
411  pfree(resarr->itemsarr);
412 }

References ResourceArray::itemsarr, and pfree().

Referenced by ResourceOwnerDelete().

◆ ResourceArrayGetAny()

static bool ResourceArrayGetAny ( ResourceArray resarr,
Datum value 
)
static

Definition at line 376 of file resowner.c.

377 {
378  if (resarr->nitems == 0)
379  return false;
380 
381  if (RESARRAY_IS_ARRAY(resarr))
382  {
383  /* Linear array: just return the first element. */
384  resarr->lastidx = 0;
385  }
386  else
387  {
388  /* Hash: search forward from wherever we were last. */
389  uint32 mask = resarr->capacity - 1;
390 
391  for (;;)
392  {
393  resarr->lastidx &= mask;
394  if (resarr->itemsarr[resarr->lastidx] != resarr->invalidval)
395  break;
396  resarr->lastidx++;
397  }
398  }
399 
400  *value = resarr->itemsarr[resarr->lastidx];
401  return true;
402 }

References ResourceArray::capacity, ResourceArray::invalidval, ResourceArray::itemsarr, ResourceArray::lastidx, ResourceArray::nitems, RESARRAY_IS_ARRAY, and value.

Referenced by ResourceOwnerReleaseAllPlanCacheRefs(), and ResourceOwnerReleaseInternal().

◆ ResourceArrayInit()

static void ResourceArrayInit ( ResourceArray resarr,
Datum  invalidval 
)
static

Definition at line 195 of file resowner.c.

196 {
197  /* Assert it's empty */
198  Assert(resarr->itemsarr == NULL);
199  Assert(resarr->capacity == 0);
200  Assert(resarr->nitems == 0);
201  Assert(resarr->maxitems == 0);
202  /* Remember the appropriate "invalid" value */
203  resarr->invalidval = invalidval;
204  /* We don't allocate any storage until needed */
205 }

References Assert(), ResourceArray::capacity, ResourceArray::invalidval, ResourceArray::itemsarr, ResourceArray::maxitems, and ResourceArray::nitems.

Referenced by ResourceOwnerCreate().

◆ ResourceArrayRemove()

static bool ResourceArrayRemove ( ResourceArray resarr,
Datum  value 
)
static

Definition at line 307 of file resowner.c.

308 {
309  uint32 i,
310  idx,
311  lastidx = resarr->lastidx;
312 
313  Assert(value != resarr->invalidval);
314 
315  /* Search through all items, but try lastidx first. */
316  if (RESARRAY_IS_ARRAY(resarr))
317  {
318  if (lastidx < resarr->nitems &&
319  resarr->itemsarr[lastidx] == value)
320  {
321  resarr->itemsarr[lastidx] = resarr->itemsarr[resarr->nitems - 1];
322  resarr->nitems--;
323  /* Update lastidx to make reverse-order removals fast. */
324  resarr->lastidx = resarr->nitems - 1;
325  return true;
326  }
327  for (i = 0; i < resarr->nitems; i++)
328  {
329  if (resarr->itemsarr[i] == value)
330  {
331  resarr->itemsarr[i] = resarr->itemsarr[resarr->nitems - 1];
332  resarr->nitems--;
333  /* Update lastidx to make reverse-order removals fast. */
334  resarr->lastidx = resarr->nitems - 1;
335  return true;
336  }
337  }
338  }
339  else
340  {
341  uint32 mask = resarr->capacity - 1;
342 
343  if (lastidx < resarr->capacity &&
344  resarr->itemsarr[lastidx] == value)
345  {
346  resarr->itemsarr[lastidx] = resarr->invalidval;
347  resarr->nitems--;
348  return true;
349  }
350  idx = DatumGetUInt32(hash_any((void *) &value, sizeof(value))) & mask;
351  for (i = 0; i < resarr->capacity; i++)
352  {
353  if (resarr->itemsarr[idx] == value)
354  {
355  resarr->itemsarr[idx] = resarr->invalidval;
356  resarr->nitems--;
357  return true;
358  }
359  idx = (idx + 1) & mask;
360  }
361  }
362 
363  return false;
364 }

References Assert(), ResourceArray::capacity, DatumGetUInt32(), hash_any(), i, idx(), ResourceArray::invalidval, ResourceArray::itemsarr, ResourceArray::lastidx, ResourceArray::nitems, RESARRAY_IS_ARRAY, and value.

Referenced by ResourceOwnerForgetBuffer(), ResourceOwnerForgetCatCacheListRef(), ResourceOwnerForgetCatCacheRef(), ResourceOwnerForgetCryptoHash(), ResourceOwnerForgetDSM(), ResourceOwnerForgetFile(), ResourceOwnerForgetHMAC(), ResourceOwnerForgetJIT(), ResourceOwnerForgetPlanCacheRef(), ResourceOwnerForgetRelationRef(), ResourceOwnerForgetSnapshot(), and ResourceOwnerForgetTupleDesc().

◆ ResourceOwnerCreate()

ResourceOwner ResourceOwnerCreate ( ResourceOwner  parent,
const char *  name 
)

Definition at line 428 of file resowner.c.

429 {
430  ResourceOwner owner;
431 
433  sizeof(ResourceOwnerData));
434  owner->name = name;
435 
436  if (parent)
437  {
438  owner->parent = parent;
439  owner->nextchild = parent->firstchild;
440  parent->firstchild = owner;
441  }
442 
444  ResourceArrayInit(&(owner->catrefarr), PointerGetDatum(NULL));
446  ResourceArrayInit(&(owner->relrefarr), PointerGetDatum(NULL));
447  ResourceArrayInit(&(owner->planrefarr), PointerGetDatum(NULL));
448  ResourceArrayInit(&(owner->tupdescarr), PointerGetDatum(NULL));
450  ResourceArrayInit(&(owner->filearr), FileGetDatum(-1));
451  ResourceArrayInit(&(owner->dsmarr), PointerGetDatum(NULL));
452  ResourceArrayInit(&(owner->jitarr), PointerGetDatum(NULL));
454  ResourceArrayInit(&(owner->hmacarr), PointerGetDatum(NULL));
455 
456  return owner;
457 }
#define InvalidBuffer
Definition: buf.h:25
const char * name
Definition: encode.c:561
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1037
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
static void ResourceArrayInit(ResourceArray *resarr, Datum invalidval)
Definition: resowner.c:195
#define BufferGetDatum(buffer)
Definition: resowner.c:46
#define FileGetDatum(file)
Definition: resowner.c:44
struct ResourceOwnerData * ResourceOwner
Definition: resowner.h:27
ResourceOwner parent
Definition: resowner.c:117
ResourceArray dsmarr
Definition: resowner.c:131
const char * name
Definition: resowner.c:120
ResourceArray catlistrefarr
Definition: resowner.c:125
ResourceArray bufferarr
Definition: resowner.c:123
ResourceArray catrefarr
Definition: resowner.c:124
ResourceArray hmacarr
Definition: resowner.c:134
ResourceArray jitarr
Definition: resowner.c:132
ResourceOwner nextchild
Definition: resowner.c:119
ResourceArray relrefarr
Definition: resowner.c:126
ResourceArray cryptohasharr
Definition: resowner.c:133
ResourceOwner firstchild
Definition: resowner.c:118
ResourceArray planrefarr
Definition: resowner.c:127
ResourceArray tupdescarr
Definition: resowner.c:128
ResourceArray snapshotarr
Definition: resowner.c:129
ResourceArray filearr
Definition: resowner.c:130

References ResourceOwnerData::bufferarr, BufferGetDatum, ResourceOwnerData::catlistrefarr, ResourceOwnerData::catrefarr, ResourceOwnerData::cryptohasharr, ResourceOwnerData::dsmarr, ResourceOwnerData::filearr, FileGetDatum, ResourceOwnerData::firstchild, ResourceOwnerData::hmacarr, InvalidBuffer, ResourceOwnerData::jitarr, MemoryContextAllocZero(), name, ResourceOwnerData::name, ResourceOwnerData::nextchild, ResourceOwnerData::parent, ResourceOwnerData::planrefarr, PointerGetDatum(), ResourceOwnerData::relrefarr, ResourceArrayInit(), ResourceOwnerData::snapshotarr, TopMemoryContext, and ResourceOwnerData::tupdescarr.

Referenced by AtStart_ResourceOwner(), AtSubStart_ResourceOwner(), CreateAuxProcessResourceOwner(), CreatePortal(), perform_base_backup(), plpgsql_call_handler(), plpgsql_create_econtext(), and plpgsql_inline_handler().

◆ ResourceOwnerDelete()

void ResourceOwnerDelete ( ResourceOwner  owner)

Definition at line 742 of file resowner.c.

743 {
744  /* We had better not be deleting CurrentResourceOwner ... */
745  Assert(owner != CurrentResourceOwner);
746 
747  /* And it better not own any resources, either */
748  Assert(owner->bufferarr.nitems == 0);
749  Assert(owner->catrefarr.nitems == 0);
750  Assert(owner->catlistrefarr.nitems == 0);
751  Assert(owner->relrefarr.nitems == 0);
752  Assert(owner->planrefarr.nitems == 0);
753  Assert(owner->tupdescarr.nitems == 0);
754  Assert(owner->snapshotarr.nitems == 0);
755  Assert(owner->filearr.nitems == 0);
756  Assert(owner->dsmarr.nitems == 0);
757  Assert(owner->jitarr.nitems == 0);
758  Assert(owner->cryptohasharr.nitems == 0);
759  Assert(owner->hmacarr.nitems == 0);
760  Assert(owner->nlocks == 0 || owner->nlocks == MAX_RESOWNER_LOCKS + 1);
761 
762  /*
763  * Delete children. The recursive call will delink the child from me, so
764  * just iterate as long as there is a child.
765  */
766  while (owner->firstchild != NULL)
768 
769  /*
770  * We delink the owner from its parent before deleting it, so that if
771  * there's an error we won't have deleted/busted owners still attached to
772  * the owner tree. Better a leak than a crash.
773  */
774  ResourceOwnerNewParent(owner, NULL);
775 
776  /* And free the object. */
777  ResourceArrayFree(&(owner->bufferarr));
778  ResourceArrayFree(&(owner->catrefarr));
779  ResourceArrayFree(&(owner->catlistrefarr));
780  ResourceArrayFree(&(owner->relrefarr));
781  ResourceArrayFree(&(owner->planrefarr));
782  ResourceArrayFree(&(owner->tupdescarr));
783  ResourceArrayFree(&(owner->snapshotarr));
784  ResourceArrayFree(&(owner->filearr));
785  ResourceArrayFree(&(owner->dsmarr));
786  ResourceArrayFree(&(owner->jitarr));
787  ResourceArrayFree(&(owner->cryptohasharr));
788  ResourceArrayFree(&(owner->hmacarr));
789 
790  pfree(owner);
791 }
void ResourceOwnerNewParent(ResourceOwner owner, ResourceOwner newparent)
Definition: resowner.c:806
#define MAX_RESOWNER_LOCKS
Definition: resowner.c:110
static void ResourceArrayFree(ResourceArray *resarr)
Definition: resowner.c:408
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:742

References Assert(), ResourceOwnerData::bufferarr, ResourceOwnerData::catlistrefarr, ResourceOwnerData::catrefarr, ResourceOwnerData::cryptohasharr, CurrentResourceOwner, ResourceOwnerData::dsmarr, ResourceOwnerData::filearr, ResourceOwnerData::firstchild, ResourceOwnerData::hmacarr, ResourceOwnerData::jitarr, MAX_RESOWNER_LOCKS, ResourceArray::nitems, ResourceOwnerData::nlocks, pfree(), ResourceOwnerData::planrefarr, ResourceOwnerData::relrefarr, ResourceArrayFree(), ResourceOwnerNewParent(), ResourceOwnerData::snapshotarr, and ResourceOwnerData::tupdescarr.

Referenced by CleanupSubTransaction(), CleanupTransaction(), CommitSubTransaction(), CommitTransaction(), plpgsql_call_handler(), plpgsql_inline_handler(), PortalDrop(), PrepareTransaction(), and WalSndResourceCleanup().

◆ ResourceOwnerEnlargeBuffers()

void ResourceOwnerEnlargeBuffers ( ResourceOwner  owner)

Definition at line 950 of file resowner.c.

951 {
952  /* We used to allow pinning buffers without a resowner, but no more */
953  Assert(owner != NULL);
954  ResourceArrayEnlarge(&(owner->bufferarr));
955 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:214

References Assert(), ResourceOwnerData::bufferarr, and ResourceArrayEnlarge().

Referenced by BgBufferSync(), BufferSync(), FlushDatabaseBuffers(), FlushRelationBuffers(), FlushRelationsAllBuffers(), IncrBufferRefCount(), ReadBuffer_common(), and ReadRecentBuffer().

◆ ResourceOwnerEnlargeCatCacheListRefs()

void ResourceOwnerEnlargeCatCacheListRefs ( ResourceOwner  owner)

Definition at line 1074 of file resowner.c.

1075 {
1077 }

References ResourceOwnerData::catlistrefarr, and ResourceArrayEnlarge().

Referenced by SearchCatCacheList().

◆ ResourceOwnerEnlargeCatCacheRefs()

void ResourceOwnerEnlargeCatCacheRefs ( ResourceOwner  owner)

Definition at line 1039 of file resowner.c.

1040 {
1041  ResourceArrayEnlarge(&(owner->catrefarr));
1042 }

References ResourceOwnerData::catrefarr, and ResourceArrayEnlarge().

Referenced by SearchCatCacheInternal(), and SearchCatCacheMiss().

◆ ResourceOwnerEnlargeCryptoHash()

void ResourceOwnerEnlargeCryptoHash ( ResourceOwner  owner)

Definition at line 1415 of file resowner.c.

1416 {
1418 }

References ResourceOwnerData::cryptohasharr, and ResourceArrayEnlarge().

Referenced by pg_cryptohash_create().

◆ ResourceOwnerEnlargeDSMs()

void ResourceOwnerEnlargeDSMs ( ResourceOwner  owner)

Definition at line 1335 of file resowner.c.

1336 {
1337  ResourceArrayEnlarge(&(owner->dsmarr));
1338 }

References ResourceOwnerData::dsmarr, and ResourceArrayEnlarge().

Referenced by dsm_create_descriptor(), and dsm_unpin_mapping().

◆ ResourceOwnerEnlargeFiles()

void ResourceOwnerEnlargeFiles ( ResourceOwner  owner)

Definition at line 1290 of file resowner.c.

1291 {
1292  ResourceArrayEnlarge(&(owner->filearr));
1293 }

References ResourceOwnerData::filearr, and ResourceArrayEnlarge().

Referenced by OpenTemporaryFile(), PathNameCreateTemporaryFile(), and PathNameOpenTemporaryFile().

◆ ResourceOwnerEnlargeHMAC()

void ResourceOwnerEnlargeHMAC ( ResourceOwner  owner)

Definition at line 1460 of file resowner.c.

1461 {
1462  ResourceArrayEnlarge(&(owner->hmacarr));
1463 }

References ResourceOwnerData::hmacarr, and ResourceArrayEnlarge().

Referenced by pg_hmac_create().

◆ ResourceOwnerEnlargeJIT()

void ResourceOwnerEnlargeJIT ( ResourceOwner  owner)

Definition at line 1380 of file resowner.c.

1381 {
1382  ResourceArrayEnlarge(&(owner->jitarr));
1383 }

References ResourceOwnerData::jitarr, and ResourceArrayEnlarge().

Referenced by llvm_create_context().

◆ ResourceOwnerEnlargePlanCacheRefs()

void ResourceOwnerEnlargePlanCacheRefs ( ResourceOwner  owner)

◆ ResourceOwnerEnlargeRelationRefs()

void ResourceOwnerEnlargeRelationRefs ( ResourceOwner  owner)

Definition at line 1109 of file resowner.c.

1110 {
1111  ResourceArrayEnlarge(&(owner->relrefarr));
1112 }

References ResourceOwnerData::relrefarr, and ResourceArrayEnlarge().

Referenced by RelationIncrementReferenceCount().

◆ ResourceOwnerEnlargeSnapshots()

void ResourceOwnerEnlargeSnapshots ( ResourceOwner  owner)

Definition at line 1244 of file resowner.c.

1245 {
1246  ResourceArrayEnlarge(&(owner->snapshotarr));
1247 }

References ResourceArrayEnlarge(), and ResourceOwnerData::snapshotarr.

Referenced by RegisterSnapshotOnOwner().

◆ ResourceOwnerEnlargeTupleDescs()

void ResourceOwnerEnlargeTupleDescs ( ResourceOwner  owner)

Definition at line 1198 of file resowner.c.

1199 {
1200  ResourceArrayEnlarge(&(owner->tupdescarr));
1201 }

References ResourceArrayEnlarge(), and ResourceOwnerData::tupdescarr.

Referenced by IncrTupleDescRefCount().

◆ ResourceOwnerForgetBuffer()

void ResourceOwnerForgetBuffer ( ResourceOwner  owner,
Buffer  buffer 
)

Definition at line 972 of file resowner.c.

973 {
974  if (!ResourceArrayRemove(&(owner->bufferarr), BufferGetDatum(buffer)))
975  elog(ERROR, "buffer %d is not owned by resource owner %s",
976  buffer, owner->name);
977 }
#define ERROR
Definition: elog.h:35
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:307

References ResourceOwnerData::bufferarr, BufferGetDatum, elog(), ERROR, ResourceOwnerData::name, and ResourceArrayRemove().

Referenced by ReleaseAndReadBuffer(), ReleaseBuffer(), and UnpinBuffer().

◆ ResourceOwnerForgetCatCacheListRef()

void ResourceOwnerForgetCatCacheListRef ( ResourceOwner  owner,
CatCList list 
)

Definition at line 1094 of file resowner.c.

1095 {
1097  elog(ERROR, "catcache list reference %p is not owned by resource owner %s",
1098  list, owner->name);
1099 }

References ResourceOwnerData::catlistrefarr, elog(), ERROR, sort-test::list, ResourceOwnerData::name, PointerGetDatum(), and ResourceArrayRemove().

Referenced by ReleaseCatCacheList().

◆ ResourceOwnerForgetCatCacheRef()

void ResourceOwnerForgetCatCacheRef ( ResourceOwner  owner,
HeapTuple  tuple 
)

Definition at line 1059 of file resowner.c.

1060 {
1061  if (!ResourceArrayRemove(&(owner->catrefarr), PointerGetDatum(tuple)))
1062  elog(ERROR, "catcache reference %p is not owned by resource owner %s",
1063  tuple, owner->name);
1064 }

References ResourceOwnerData::catrefarr, elog(), ERROR, ResourceOwnerData::name, PointerGetDatum(), and ResourceArrayRemove().

Referenced by ReleaseCatCache().

◆ ResourceOwnerForgetCryptoHash()

void ResourceOwnerForgetCryptoHash ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1435 of file resowner.c.

1436 {
1437  if (!ResourceArrayRemove(&(owner->cryptohasharr), handle))
1438  elog(ERROR, "cryptohash context %p is not owned by resource owner %s",
1439  DatumGetPointer(handle), owner->name);
1440 }

References ResourceOwnerData::cryptohasharr, DatumGetPointer(), elog(), ERROR, ResourceOwnerData::name, and ResourceArrayRemove().

Referenced by pg_cryptohash_free().

◆ ResourceOwnerForgetDSM()

void ResourceOwnerForgetDSM ( ResourceOwner  owner,
dsm_segment seg 
)

Definition at line 1355 of file resowner.c.

1356 {
1357  if (!ResourceArrayRemove(&(owner->dsmarr), PointerGetDatum(seg)))
1358  elog(ERROR, "dynamic shared memory segment %u is not owned by resource owner %s",
1359  dsm_segment_handle(seg), owner->name);
1360 }

References dsm_segment_handle(), ResourceOwnerData::dsmarr, elog(), ERROR, ResourceOwnerData::name, PointerGetDatum(), and ResourceArrayRemove().

Referenced by dsm_create(), dsm_detach(), and dsm_pin_mapping().

◆ ResourceOwnerForgetFile()

void ResourceOwnerForgetFile ( ResourceOwner  owner,
File  file 
)

Definition at line 1310 of file resowner.c.

1311 {
1312  if (!ResourceArrayRemove(&(owner->filearr), FileGetDatum(file)))
1313  elog(ERROR, "temporary file %d is not owned by resource owner %s",
1314  file, owner->name);
1315 }

References elog(), ERROR, ResourceOwnerData::filearr, FileGetDatum, ResourceOwnerData::name, and ResourceArrayRemove().

Referenced by FileClose().

◆ ResourceOwnerForgetHMAC()

void ResourceOwnerForgetHMAC ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1480 of file resowner.c.

1481 {
1482  if (!ResourceArrayRemove(&(owner->hmacarr), handle))
1483  elog(ERROR, "HMAC context %p is not owned by resource owner %s",
1484  DatumGetPointer(handle), owner->name);
1485 }

References DatumGetPointer(), elog(), ERROR, ResourceOwnerData::hmacarr, ResourceOwnerData::name, and ResourceArrayRemove().

Referenced by pg_hmac_free().

◆ ResourceOwnerForgetJIT()

void ResourceOwnerForgetJIT ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1400 of file resowner.c.

1401 {
1402  if (!ResourceArrayRemove(&(owner->jitarr), handle))
1403  elog(ERROR, "JIT context %p is not owned by resource owner %s",
1404  DatumGetPointer(handle), owner->name);
1405 }

References DatumGetPointer(), elog(), ERROR, ResourceOwnerData::jitarr, ResourceOwnerData::name, and ResourceArrayRemove().

Referenced by jit_release_context().

◆ ResourceOwnerForgetLock()

void ResourceOwnerForgetLock ( ResourceOwner  owner,
LOCALLOCK locallock 
)

Definition at line 1010 of file resowner.c.

1011 {
1012  int i;
1013 
1014  if (owner->nlocks > MAX_RESOWNER_LOCKS)
1015  return; /* we have overflowed */
1016 
1017  Assert(owner->nlocks > 0);
1018  for (i = owner->nlocks - 1; i >= 0; i--)
1019  {
1020  if (locallock == owner->locks[i])
1021  {
1022  owner->locks[i] = owner->locks[owner->nlocks - 1];
1023  owner->nlocks--;
1024  return;
1025  }
1026  }
1027  elog(ERROR, "lock reference %p is not owned by resource owner %s",
1028  locallock, owner->name);
1029 }
LOCALLOCK * locks[MAX_RESOWNER_LOCKS]
Definition: resowner.c:138

References Assert(), elog(), ERROR, i, ResourceOwnerData::locks, MAX_RESOWNER_LOCKS, ResourceOwnerData::name, and ResourceOwnerData::nlocks.

Referenced by LockReassignOwner(), LockRelease(), LockReleaseAll(), ReleaseLockIfHeld(), and RemoveLocalLock().

◆ ResourceOwnerForgetPlanCacheRef()

void ResourceOwnerForgetPlanCacheRef ( ResourceOwner  owner,
CachedPlan plan 
)

Definition at line 1174 of file resowner.c.

1175 {
1176  if (!ResourceArrayRemove(&(owner->planrefarr), PointerGetDatum(plan)))
1177  elog(ERROR, "plancache reference %p is not owned by resource owner %s",
1178  plan, owner->name);
1179 }

References elog(), ERROR, ResourceOwnerData::name, ResourceOwnerData::planrefarr, PointerGetDatum(), and ResourceArrayRemove().

Referenced by ReleaseCachedPlan().

◆ ResourceOwnerForgetRelationRef()

void ResourceOwnerForgetRelationRef ( ResourceOwner  owner,
Relation  rel 
)

Definition at line 1129 of file resowner.c.

1130 {
1131  if (!ResourceArrayRemove(&(owner->relrefarr), PointerGetDatum(rel)))
1132  elog(ERROR, "relcache reference %s is not owned by resource owner %s",
1133  RelationGetRelationName(rel), owner->name);
1134 }

References elog(), ERROR, ResourceOwnerData::name, PointerGetDatum(), RelationGetRelationName, ResourceOwnerData::relrefarr, and ResourceArrayRemove().

Referenced by RelationDecrementReferenceCount().

◆ ResourceOwnerForgetSnapshot()

void ResourceOwnerForgetSnapshot ( ResourceOwner  owner,
Snapshot  snapshot 
)

Definition at line 1264 of file resowner.c.

1265 {
1266  if (!ResourceArrayRemove(&(owner->snapshotarr), PointerGetDatum(snapshot)))
1267  elog(ERROR, "snapshot reference %p is not owned by resource owner %s",
1268  snapshot, owner->name);
1269 }

References elog(), ERROR, ResourceOwnerData::name, PointerGetDatum(), ResourceArrayRemove(), and ResourceOwnerData::snapshotarr.

Referenced by UnregisterSnapshotFromOwner().

◆ ResourceOwnerForgetTupleDesc()

void ResourceOwnerForgetTupleDesc ( ResourceOwner  owner,
TupleDesc  tupdesc 
)

Definition at line 1218 of file resowner.c.

1219 {
1220  if (!ResourceArrayRemove(&(owner->tupdescarr), PointerGetDatum(tupdesc)))
1221  elog(ERROR, "tupdesc reference %p is not owned by resource owner %s",
1222  tupdesc, owner->name);
1223 }

References elog(), ERROR, ResourceOwnerData::name, PointerGetDatum(), ResourceArrayRemove(), and ResourceOwnerData::tupdescarr.

Referenced by DecrTupleDescRefCount().

◆ ResourceOwnerGetParent()

ResourceOwner ResourceOwnerGetParent ( ResourceOwner  owner)

Definition at line 797 of file resowner.c.

798 {
799  return owner->parent;
800 }

References ResourceOwnerData::parent.

Referenced by LockReassignCurrentOwner().

◆ ResourceOwnerNewParent()

void ResourceOwnerNewParent ( ResourceOwner  owner,
ResourceOwner  newparent 
)

Definition at line 806 of file resowner.c.

808 {
809  ResourceOwner oldparent = owner->parent;
810 
811  if (oldparent)
812  {
813  if (owner == oldparent->firstchild)
814  oldparent->firstchild = owner->nextchild;
815  else
816  {
817  ResourceOwner child;
818 
819  for (child = oldparent->firstchild; child; child = child->nextchild)
820  {
821  if (owner == child->nextchild)
822  {
823  child->nextchild = owner->nextchild;
824  break;
825  }
826  }
827  }
828  }
829 
830  if (newparent)
831  {
832  Assert(owner != newparent);
833  owner->parent = newparent;
834  owner->nextchild = newparent->firstchild;
835  newparent->firstchild = owner;
836  }
837  else
838  {
839  owner->parent = NULL;
840  owner->nextchild = NULL;
841  }
842 }

References Assert(), ResourceOwnerData::firstchild, ResourceOwnerData::nextchild, and ResourceOwnerData::parent.

Referenced by AtSubAbort_Portals(), AtSubCommit_Portals(), and ResourceOwnerDelete().

◆ ResourceOwnerRelease()

void ResourceOwnerRelease ( ResourceOwner  owner,
ResourceReleasePhase  phase,
bool  isCommit,
bool  isTopLevel 
)

Definition at line 486 of file resowner.c.

490 {
491  /* There's not currently any setup needed before recursing */
492  ResourceOwnerReleaseInternal(owner, phase, isCommit, isTopLevel);
493 }
static void ResourceOwnerReleaseInternal(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:496

References ResourceOwnerReleaseInternal().

Referenced by AbortSubTransaction(), AbortTransaction(), CommitSubTransaction(), CommitTransaction(), PortalDrop(), PrepareTransaction(), ReleaseAuxProcessResources(), and WalSndResourceCleanup().

◆ ResourceOwnerReleaseAllPlanCacheRefs()

void ResourceOwnerReleaseAllPlanCacheRefs ( ResourceOwner  owner)

Definition at line 723 of file resowner.c.

724 {
725  Datum foundres;
726 
727  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
728  {
729  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
730 
731  ReleaseCachedPlan(res, owner);
732  }
733 }
void ReleaseCachedPlan(CachedPlan *plan, ResourceOwner owner)
Definition: plancache.c:1264
static bool ResourceArrayGetAny(ResourceArray *resarr, Datum *value)
Definition: resowner.c:376

References DatumGetPointer(), ResourceOwnerData::planrefarr, ReleaseCachedPlan(), res, and ResourceArrayGetAny().

Referenced by plpgsql_call_handler(), plpgsql_inline_handler(), and plpgsql_xact_cb().

◆ ResourceOwnerReleaseInternal()

static void ResourceOwnerReleaseInternal ( ResourceOwner  owner,
ResourceReleasePhase  phase,
bool  isCommit,
bool  isTopLevel 
)
static

Definition at line 496 of file resowner.c.

500 {
501  ResourceOwner child;
502  ResourceOwner save;
505  Datum foundres;
506 
507  /* Recurse to handle descendants */
508  for (child = owner->firstchild; child != NULL; child = child->nextchild)
509  ResourceOwnerReleaseInternal(child, phase, isCommit, isTopLevel);
510 
511  /*
512  * Make CurrentResourceOwner point to me, so that ReleaseBuffer etc don't
513  * get confused.
514  */
515  save = CurrentResourceOwner;
516  CurrentResourceOwner = owner;
517 
518  if (phase == RESOURCE_RELEASE_BEFORE_LOCKS)
519  {
520  /*
521  * Release buffer pins. Note that ReleaseBuffer will remove the
522  * buffer entry from our array, so we just have to iterate till there
523  * are none.
524  *
525  * During a commit, there shouldn't be any remaining pins --- that
526  * would indicate failure to clean up the executor correctly --- so
527  * issue warnings. In the abort case, just clean up quietly.
528  */
529  while (ResourceArrayGetAny(&(owner->bufferarr), &foundres))
530  {
531  Buffer res = DatumGetBuffer(foundres);
532 
533  if (isCommit)
536  }
537 
538  /* Ditto for relcache references */
539  while (ResourceArrayGetAny(&(owner->relrefarr), &foundres))
540  {
541  Relation res = (Relation) DatumGetPointer(foundres);
542 
543  if (isCommit)
546  }
547 
548  /* Ditto for dynamic shared memory segments */
549  while (ResourceArrayGetAny(&(owner->dsmarr), &foundres))
550  {
551  dsm_segment *res = (dsm_segment *) DatumGetPointer(foundres);
552 
553  if (isCommit)
555  dsm_detach(res);
556  }
557 
558  /* Ditto for JIT contexts */
559  while (ResourceArrayGetAny(&(owner->jitarr), &foundres))
560  {
561  JitContext *context = (JitContext *) DatumGetPointer(foundres);
562 
563  jit_release_context(context);
564  }
565 
566  /* Ditto for cryptohash contexts */
567  while (ResourceArrayGetAny(&(owner->cryptohasharr), &foundres))
568  {
569  pg_cryptohash_ctx *context =
570  (pg_cryptohash_ctx *) DatumGetPointer(foundres);
571 
572  if (isCommit)
573  PrintCryptoHashLeakWarning(foundres);
574  pg_cryptohash_free(context);
575  }
576 
577  /* Ditto for HMAC contexts */
578  while (ResourceArrayGetAny(&(owner->hmacarr), &foundres))
579  {
580  pg_hmac_ctx *context = (pg_hmac_ctx *) DatumGetPointer(foundres);
581 
582  if (isCommit)
583  PrintHMACLeakWarning(foundres);
584  pg_hmac_free(context);
585  }
586  }
587  else if (phase == RESOURCE_RELEASE_LOCKS)
588  {
589  if (isTopLevel)
590  {
591  /*
592  * For a top-level xact we are going to release all locks (or at
593  * least all non-session locks), so just do a single lmgr call at
594  * the top of the recursion.
595  */
596  if (owner == TopTransactionResourceOwner)
597  {
598  ProcReleaseLocks(isCommit);
599  ReleasePredicateLocks(isCommit, false);
600  }
601  }
602  else
603  {
604  /*
605  * Release locks retail. Note that if we are committing a
606  * subtransaction, we do NOT release its locks yet, but transfer
607  * them to the parent.
608  */
609  LOCALLOCK **locks;
610  int nlocks;
611 
612  Assert(owner->parent != NULL);
613 
614  /*
615  * Pass the list of locks owned by this resource owner to the lock
616  * manager, unless it has overflowed.
617  */
618  if (owner->nlocks > MAX_RESOWNER_LOCKS)
619  {
620  locks = NULL;
621  nlocks = 0;
622  }
623  else
624  {
625  locks = owner->locks;
626  nlocks = owner->nlocks;
627  }
628 
629  if (isCommit)
630  LockReassignCurrentOwner(locks, nlocks);
631  else
632  LockReleaseCurrentOwner(locks, nlocks);
633  }
634  }
635  else if (phase == RESOURCE_RELEASE_AFTER_LOCKS)
636  {
637  /*
638  * Release catcache references. Note that ReleaseCatCache will remove
639  * the catref entry from our array, so we just have to iterate till
640  * there are none.
641  *
642  * As with buffer pins, warn if any are left at commit time.
643  */
644  while (ResourceArrayGetAny(&(owner->catrefarr), &foundres))
645  {
646  HeapTuple res = (HeapTuple) DatumGetPointer(foundres);
647 
648  if (isCommit)
651  }
652 
653  /* Ditto for catcache lists */
654  while (ResourceArrayGetAny(&(owner->catlistrefarr), &foundres))
655  {
656  CatCList *res = (CatCList *) DatumGetPointer(foundres);
657 
658  if (isCommit)
661  }
662 
663  /* Ditto for plancache references */
664  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
665  {
666  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
667 
668  if (isCommit)
670  ReleaseCachedPlan(res, owner);
671  }
672 
673  /* Ditto for tupdesc references */
674  while (ResourceArrayGetAny(&(owner->tupdescarr), &foundres))
675  {
676  TupleDesc res = (TupleDesc) DatumGetPointer(foundres);
677 
678  if (isCommit)
681  }
682 
683  /* Ditto for snapshot references */
684  while (ResourceArrayGetAny(&(owner->snapshotarr), &foundres))
685  {
686  Snapshot res = (Snapshot) DatumGetPointer(foundres);
687 
688  if (isCommit)
691  }
692 
693  /* Ditto for temporary files */
694  while (ResourceArrayGetAny(&(owner->filearr), &foundres))
695  {
696  File res = DatumGetFile(foundres);
697 
698  if (isCommit)
700  FileClose(res);
701  }
702  }
703 
704  /* Let add-on modules get a chance too */
705  for (item = ResourceRelease_callbacks; item; item = next)
706  {
707  /* allow callbacks to unregister themselves when called */
708  next = item->next;
709  item->callback(phase, isCommit, isTopLevel, item->arg);
710  }
711 
712  CurrentResourceOwner = save;
713 }
static int32 next
Definition: blutils.c:219
int Buffer
Definition: buf.h:23
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3931
void PrintBufferLeakWarning(Buffer buffer)
Definition: bufmgr.c:2695
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:1777
void PrintCatCacheLeakWarning(HeapTuple tuple)
Definition: catcache.c:2065
void PrintCatCacheListLeakWarning(CatCList *list)
Definition: catcache.c:2081
void ReleaseCatCache(HeapTuple tuple)
Definition: catcache.c:1436
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:243
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:777
void FileClose(File file)
Definition: fd.c:1883
int File
Definition: fd.h:54
void pg_hmac_free(pg_hmac_ctx *ctx)
Definition: hmac.c:289
HeapTupleData * HeapTuple
Definition: htup.h:71
void jit_release_context(JitContext *context)
Definition: jit.c:138
void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2579
void LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2484
void ReleasePredicateLocks(bool isCommit, bool isReadOnlySafe)
Definition: predicate.c:3335
void RelationClose(Relation relation)
Definition: relcache.c:2160
struct RelationData * Relation
Definition: relcache.h:27
#define DatumGetBuffer(datum)
Definition: resowner.c:47
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
static void PrintPlanCacheLeakWarning(CachedPlan *plan)
Definition: resowner.c:1185
static void PrintSnapshotLeakWarning(Snapshot snapshot)
Definition: resowner.c:1275
static void PrintCryptoHashLeakWarning(Datum handle)
Definition: resowner.c:1446
static void PrintTupleDescLeakWarning(TupleDesc tupdesc)
Definition: resowner.c:1229
#define DatumGetFile(datum)
Definition: resowner.c:45
static void PrintRelCacheLeakWarning(Relation rel)
Definition: resowner.c:1140
static void PrintHMACLeakWarning(Datum handle)
Definition: resowner.c:1491
static void PrintDSMLeakWarning(dsm_segment *seg)
Definition: resowner.c:1366
static void PrintFileLeakWarning(File file)
Definition: resowner.c:1321
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:871
struct SnapshotData * Snapshot
Definition: snapshot.h:121
void ProcReleaseLocks(bool isCommit)
Definition: proc.c:775
Definition: jit.h:55
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:384
struct TupleDescData * TupleDesc
Definition: tupdesc.h:89

References ResourceReleaseCallbackItem::arg, Assert(), ResourceOwnerData::bufferarr, ResourceReleaseCallbackItem::callback, ResourceOwnerData::catlistrefarr, ResourceOwnerData::catrefarr, ResourceOwnerData::cryptohasharr, CurrentResourceOwner, DatumGetBuffer, DatumGetFile, DatumGetPointer(), DecrTupleDescRefCount(), dsm_detach(), ResourceOwnerData::dsmarr, ResourceOwnerData::filearr, FileClose(), ResourceOwnerData::firstchild, ResourceOwnerData::hmacarr, jit_release_context(), ResourceOwnerData::jitarr, LockReassignCurrentOwner(), LockReleaseCurrentOwner(), ResourceOwnerData::locks, MAX_RESOWNER_LOCKS, next, ResourceReleaseCallbackItem::next, ResourceOwnerData::nextchild, ResourceOwnerData::nlocks, ResourceOwnerData::parent, pg_cryptohash_free(), pg_hmac_free(), ResourceOwnerData::planrefarr, PrintBufferLeakWarning(), PrintCatCacheLeakWarning(), PrintCatCacheListLeakWarning(), PrintCryptoHashLeakWarning(), PrintDSMLeakWarning(), PrintFileLeakWarning(), PrintHMACLeakWarning(), PrintPlanCacheLeakWarning(), PrintRelCacheLeakWarning(), PrintSnapshotLeakWarning(), PrintTupleDescLeakWarning(), ProcReleaseLocks(), RelationClose(), ReleaseBuffer(), ReleaseCachedPlan(), ReleaseCatCache(), ReleaseCatCacheList(), ReleasePredicateLocks(), ResourceOwnerData::relrefarr, res, RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceArrayGetAny(), ResourceRelease_callbacks, ResourceOwnerData::snapshotarr, TopTransactionResourceOwner, ResourceOwnerData::tupdescarr, and UnregisterSnapshot().

Referenced by ResourceOwnerRelease().

◆ ResourceOwnerRememberBuffer()

void ResourceOwnerRememberBuffer ( ResourceOwner  owner,
Buffer  buffer 
)

◆ ResourceOwnerRememberCatCacheListRef()

void ResourceOwnerRememberCatCacheListRef ( ResourceOwner  owner,
CatCList list 
)

Definition at line 1085 of file resowner.c.

1086 {
1088 }

References ResourceOwnerData::catlistrefarr, sort-test::list, PointerGetDatum(), and ResourceArrayAdd().

Referenced by SearchCatCacheList().

◆ ResourceOwnerRememberCatCacheRef()

void ResourceOwnerRememberCatCacheRef ( ResourceOwner  owner,
HeapTuple  tuple 
)

Definition at line 1050 of file resowner.c.

1051 {
1052  ResourceArrayAdd(&(owner->catrefarr), PointerGetDatum(tuple));
1053 }

References ResourceOwnerData::catrefarr, PointerGetDatum(), and ResourceArrayAdd().

Referenced by SearchCatCacheInternal(), and SearchCatCacheMiss().

◆ ResourceOwnerRememberCryptoHash()

void ResourceOwnerRememberCryptoHash ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1426 of file resowner.c.

1427 {
1428  ResourceArrayAdd(&(owner->cryptohasharr), handle);
1429 }

References ResourceOwnerData::cryptohasharr, and ResourceArrayAdd().

Referenced by pg_cryptohash_create().

◆ ResourceOwnerRememberDSM()

void ResourceOwnerRememberDSM ( ResourceOwner  owner,
dsm_segment seg 
)

Definition at line 1346 of file resowner.c.

1347 {
1348  ResourceArrayAdd(&(owner->dsmarr), PointerGetDatum(seg));
1349 }

References ResourceOwnerData::dsmarr, PointerGetDatum(), and ResourceArrayAdd().

Referenced by dsm_create_descriptor(), and dsm_unpin_mapping().

◆ ResourceOwnerRememberFile()

void ResourceOwnerRememberFile ( ResourceOwner  owner,
File  file 
)

Definition at line 1301 of file resowner.c.

1302 {
1303  ResourceArrayAdd(&(owner->filearr), FileGetDatum(file));
1304 }

References ResourceOwnerData::filearr, FileGetDatum, and ResourceArrayAdd().

Referenced by RegisterTemporaryFile().

◆ ResourceOwnerRememberHMAC()

void ResourceOwnerRememberHMAC ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1471 of file resowner.c.

1472 {
1473  ResourceArrayAdd(&(owner->hmacarr), handle);
1474 }

References ResourceOwnerData::hmacarr, and ResourceArrayAdd().

Referenced by pg_hmac_create().

◆ ResourceOwnerRememberJIT()

void ResourceOwnerRememberJIT ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1391 of file resowner.c.

1392 {
1393  ResourceArrayAdd(&(owner->jitarr), handle);
1394 }

References ResourceOwnerData::jitarr, and ResourceArrayAdd().

Referenced by llvm_create_context().

◆ ResourceOwnerRememberLock()

void ResourceOwnerRememberLock ( ResourceOwner  owner,
LOCALLOCK locallock 
)

Definition at line 990 of file resowner.c.

991 {
992  Assert(locallock != NULL);
993 
994  if (owner->nlocks > MAX_RESOWNER_LOCKS)
995  return; /* we have already overflowed */
996 
997  if (owner->nlocks < MAX_RESOWNER_LOCKS)
998  owner->locks[owner->nlocks] = locallock;
999  else
1000  {
1001  /* overflowed */
1002  }
1003  owner->nlocks++;
1004 }

References Assert(), ResourceOwnerData::locks, MAX_RESOWNER_LOCKS, and ResourceOwnerData::nlocks.

Referenced by GrantLockLocal(), and LockReassignOwner().

◆ ResourceOwnerRememberPlanCacheRef()

void ResourceOwnerRememberPlanCacheRef ( ResourceOwner  owner,
CachedPlan plan 
)

◆ ResourceOwnerRememberRelationRef()

void ResourceOwnerRememberRelationRef ( ResourceOwner  owner,
Relation  rel 
)

Definition at line 1120 of file resowner.c.

1121 {
1122  ResourceArrayAdd(&(owner->relrefarr), PointerGetDatum(rel));
1123 }

References PointerGetDatum(), ResourceOwnerData::relrefarr, and ResourceArrayAdd().

Referenced by RelationIncrementReferenceCount().

◆ ResourceOwnerRememberSnapshot()

void ResourceOwnerRememberSnapshot ( ResourceOwner  owner,
Snapshot  snapshot 
)

Definition at line 1255 of file resowner.c.

1256 {
1257  ResourceArrayAdd(&(owner->snapshotarr), PointerGetDatum(snapshot));
1258 }

References PointerGetDatum(), ResourceArrayAdd(), and ResourceOwnerData::snapshotarr.

Referenced by RegisterSnapshotOnOwner().

◆ ResourceOwnerRememberTupleDesc()

void ResourceOwnerRememberTupleDesc ( ResourceOwner  owner,
TupleDesc  tupdesc 
)

Definition at line 1209 of file resowner.c.

1210 {
1211  ResourceArrayAdd(&(owner->tupdescarr), PointerGetDatum(tupdesc));
1212 }

References PointerGetDatum(), ResourceArrayAdd(), and ResourceOwnerData::tupdescarr.

Referenced by IncrTupleDescRefCount().

◆ UnregisterResourceReleaseCallback()

void UnregisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 868 of file resowner.c.

869 {
872 
873  prev = NULL;
874  for (item = ResourceRelease_callbacks; item; prev = item, item = item->next)
875  {
876  if (item->callback == callback && item->arg == arg)
877  {
878  if (prev)
879  prev->next = item->next;
880  else
882  pfree(item);
883  break;
884  }
885  }
886 }

References ResourceReleaseCallbackItem::arg, arg, ResourceReleaseCallbackItem::callback, callback(), ResourceReleaseCallbackItem::next, pfree(), and ResourceRelease_callbacks.

Variable Documentation

◆ AuxProcessResourceOwner

ResourceOwner AuxProcessResourceOwner = NULL

◆ CurrentResourceOwner

ResourceOwner CurrentResourceOwner = NULL

Definition at line 146 of file resowner.c.

Referenced by _SPI_execute_plan(), AssignTransactionId(), AtAbort_ResourceOwner(), AtStart_ResourceOwner(), AtSubAbort_ResourceOwner(), AtSubStart_ResourceOwner(), BgBufferSync(), BufferSync(), cipher_free_callback(), CleanupSubTransaction(), CleanupTransaction(), close_lo_relation(), CommitSubTransaction(), CommitTransaction(), CreateAuxProcessResourceOwner(), DecrTupleDescRefCount(), digest_free_callback(), dsm_create_descriptor(), dsm_unpin_mapping(), exec_eval_simple_expr(), exec_init_tuple_store(), exec_simple_check_plan(), exec_stmt_block(), ExplainExecuteQuery(), extendBufFile(), FlushDatabaseBuffers(), FlushRelationBuffers(), FlushRelationsAllBuffers(), GetCurrentFDWTuplestore(), IncrBufferRefCount(), IncrTupleDescRefCount(), InitPostgres(), llvm_create_context(), LocalBufferAlloc(), lock_and_open_sequence(), LockAcquireExtended(), LockReassignCurrentOwner(), LockReassignOwner(), LockRelease(), makeBufFileCommon(), MakeTransitionCaptureState(), open_lo_relation(), OpenTemporaryFile(), PathNameCreateTemporaryFile(), PathNameOpenTemporaryFile(), perform_base_backup(), PersistHoldablePortal(), pg_cryptohash_create(), pg_hmac_create(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), PinBuffer(), PinBuffer_Locked(), plperl_spi_exec(), plperl_spi_exec_prepared(), plperl_spi_fetchrow(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), plpgsql_estate_setup(), pltcl_func_handler(), pltcl_init_tuple_store(), pltcl_returnnext(), pltcl_SPI_execute(), pltcl_SPI_execute_plan(), pltcl_SPI_prepare(), pltcl_subtrans_abort(), pltcl_subtrans_commit(), pltcl_subtransaction(), PLy_abort_open_subtransactions(), PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_cursor_plan(), PLy_cursor_query(), PLy_spi_execute_plan(), PLy_spi_execute_query(), PLy_spi_prepare(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_commit(), PLy_subtransaction_enter(), PLy_subtransaction_exit(), PopTransaction(), PortalCleanup(), PortalRun(), PortalRunFetch(), PortalStart(), PrepareTransaction(), px_find_cipher(), px_find_digest(), ReadBuffer_common(), ReadRecentBuffer(), RegisterSnapshot(), RegisterTemporaryFile(), RelationDecrementReferenceCount(), RelationIncrementReferenceCount(), ReleaseAndReadBuffer(), ReleaseBuffer(), ReleaseCatCache(), ReleaseCatCacheList(), ReleaseLockIfHeld(), ResourceOwnerDelete(), ResourceOwnerReleaseInternal(), SearchCatCacheInternal(), SearchCatCacheList(), SearchCatCacheMiss(), ShutdownXLOG(), SnapBuildClearExportedSnapshot(), SnapBuildExportSnapshot(), SPI_plan_get_cached_plan(), StartupXLOG(), tuplestore_begin_common(), tuplestore_puttuple_common(), UnpinBuffer(), UnregisterSnapshot(), and WalSndResourceCleanup().

◆ CurTransactionResourceOwner

◆ ResourceRelease_callbacks

ResourceReleaseCallbackItem* ResourceRelease_callbacks = NULL
static

◆ TopTransactionResourceOwner