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 887 of file resowner.c.

888 {
890  Assert(CurrentResourceOwner == NULL);
891  AuxProcessResourceOwner = ResourceOwnerCreate(NULL, "AuxiliaryProcess");
893 
894  /*
895  * Register a shmem-exit callback for cleanup of aux-process resource
896  * owner. (This needs to run after, e.g., ShutdownXLOG.)
897  */
899 }
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:929
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 1441 of file resowner.c.

1442 {
1443  elog(WARNING, "cryptohash context reference leak: context %p still referenced",
1444  DatumGetPointer(handle));
1445 }
#define WARNING
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:218
#define DatumGetPointer(X)
Definition: postgres.h:593

References DatumGetPointer, elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintDSMLeakWarning()

static void PrintDSMLeakWarning ( dsm_segment seg)
static

Definition at line 1361 of file resowner.c.

1362 {
1363  elog(WARNING, "dynamic shared memory leak: segment %u still referenced",
1364  dsm_segment_handle(seg));
1365 }
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 1316 of file resowner.c.

1317 {
1318  elog(WARNING, "temporary file leak: File %d still referenced",
1319  file);
1320 }

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintHMACLeakWarning()

static void PrintHMACLeakWarning ( Datum  handle)
static

Definition at line 1486 of file resowner.c.

1487 {
1488  elog(WARNING, "HMAC context reference leak: context %p still referenced",
1489  DatumGetPointer(handle));
1490 }

References DatumGetPointer, elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintPlanCacheLeakWarning()

static void PrintPlanCacheLeakWarning ( CachedPlan plan)
static

Definition at line 1180 of file resowner.c.

1181 {
1182  elog(WARNING, "plancache reference leak: plan %p not closed", plan);
1183 }

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintRelCacheLeakWarning()

static void PrintRelCacheLeakWarning ( Relation  rel)
static

Definition at line 1135 of file resowner.c.

1136 {
1137  elog(WARNING, "relcache reference leak: relation \"%s\" not closed",
1139 }
#define RelationGetRelationName(relation)
Definition: rel.h:523

References elog, RelationGetRelationName, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintSnapshotLeakWarning()

static void PrintSnapshotLeakWarning ( Snapshot  snapshot)
static

Definition at line 1270 of file resowner.c.

1271 {
1272  elog(WARNING, "Snapshot reference leak: Snapshot %p still referenced",
1273  snapshot);
1274 }

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ PrintTupleDescLeakWarning()

static void PrintTupleDescLeakWarning ( TupleDesc  tupdesc)
static

Definition at line 1224 of file resowner.c.

1225 {
1226  elog(WARNING,
1227  "TupleDesc reference leak: TupleDesc %p (%u,%d) still referenced",
1228  tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
1229 }
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 849 of file resowner.c.

850 {
852 
853  item = (ResourceReleaseCallbackItem *)
856  item->callback = callback;
857  item->arg = arg;
860 }
MemoryContext TopMemoryContext
Definition: mcxt.c:48
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
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:48

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 907 of file resowner.c.

908 {
909  /*
910  * At this writing, the only thing that could actually get released is
911  * buffer pins; but we may as well do the full release protocol.
912  */
915  isCommit, true);
918  isCommit, true);
921  isCommit, true);
922 }
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 929 of file resowner.c.

930 {
931  bool isCommit = (code == 0);
932 
933  ReleaseAuxProcessResources(isCommit);
934 }
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:907

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:441
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
static struct @151 value
#define DatumGetUInt32(X)
Definition: postgres.h:530
#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:1175
uintptr_t Datum
Definition: postgres.h:411
#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:906
#define PointerGetDatum(X)
Definition: postgres.h:600
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 737 of file resowner.c.

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

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 945 of file resowner.c.

946 {
947  /* We used to allow pinning buffers without a resowner, but no more */
948  Assert(owner != NULL);
949  ResourceArrayEnlarge(&(owner->bufferarr));
950 }
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 1069 of file resowner.c.

1070 {
1072 }

References ResourceOwnerData::catlistrefarr, and ResourceArrayEnlarge().

Referenced by SearchCatCacheList().

◆ ResourceOwnerEnlargeCatCacheRefs()

void ResourceOwnerEnlargeCatCacheRefs ( ResourceOwner  owner)

Definition at line 1034 of file resowner.c.

1035 {
1036  ResourceArrayEnlarge(&(owner->catrefarr));
1037 }

References ResourceOwnerData::catrefarr, and ResourceArrayEnlarge().

Referenced by SearchCatCacheInternal(), and SearchCatCacheMiss().

◆ ResourceOwnerEnlargeCryptoHash()

void ResourceOwnerEnlargeCryptoHash ( ResourceOwner  owner)

Definition at line 1410 of file resowner.c.

1411 {
1413 }

References ResourceOwnerData::cryptohasharr, and ResourceArrayEnlarge().

Referenced by pg_cryptohash_create().

◆ ResourceOwnerEnlargeDSMs()

void ResourceOwnerEnlargeDSMs ( ResourceOwner  owner)

Definition at line 1330 of file resowner.c.

1331 {
1332  ResourceArrayEnlarge(&(owner->dsmarr));
1333 }

References ResourceOwnerData::dsmarr, and ResourceArrayEnlarge().

Referenced by dsm_create_descriptor(), and dsm_unpin_mapping().

◆ ResourceOwnerEnlargeFiles()

void ResourceOwnerEnlargeFiles ( ResourceOwner  owner)

Definition at line 1285 of file resowner.c.

1286 {
1287  ResourceArrayEnlarge(&(owner->filearr));
1288 }

References ResourceOwnerData::filearr, and ResourceArrayEnlarge().

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

◆ ResourceOwnerEnlargeHMAC()

void ResourceOwnerEnlargeHMAC ( ResourceOwner  owner)

Definition at line 1455 of file resowner.c.

1456 {
1457  ResourceArrayEnlarge(&(owner->hmacarr));
1458 }

References ResourceOwnerData::hmacarr, and ResourceArrayEnlarge().

Referenced by pg_hmac_create().

◆ ResourceOwnerEnlargeJIT()

void ResourceOwnerEnlargeJIT ( ResourceOwner  owner)

Definition at line 1375 of file resowner.c.

1376 {
1377  ResourceArrayEnlarge(&(owner->jitarr));
1378 }

References ResourceOwnerData::jitarr, and ResourceArrayEnlarge().

Referenced by llvm_create_context().

◆ ResourceOwnerEnlargePlanCacheRefs()

void ResourceOwnerEnlargePlanCacheRefs ( ResourceOwner  owner)

◆ ResourceOwnerEnlargeRelationRefs()

void ResourceOwnerEnlargeRelationRefs ( ResourceOwner  owner)

Definition at line 1104 of file resowner.c.

1105 {
1106  ResourceArrayEnlarge(&(owner->relrefarr));
1107 }

References ResourceOwnerData::relrefarr, and ResourceArrayEnlarge().

Referenced by RelationIncrementReferenceCount().

◆ ResourceOwnerEnlargeSnapshots()

void ResourceOwnerEnlargeSnapshots ( ResourceOwner  owner)

Definition at line 1239 of file resowner.c.

1240 {
1241  ResourceArrayEnlarge(&(owner->snapshotarr));
1242 }

References ResourceArrayEnlarge(), and ResourceOwnerData::snapshotarr.

Referenced by RegisterSnapshotOnOwner().

◆ ResourceOwnerEnlargeTupleDescs()

void ResourceOwnerEnlargeTupleDescs ( ResourceOwner  owner)

Definition at line 1193 of file resowner.c.

1194 {
1195  ResourceArrayEnlarge(&(owner->tupdescarr));
1196 }

References ResourceArrayEnlarge(), and ResourceOwnerData::tupdescarr.

Referenced by IncrTupleDescRefCount().

◆ ResourceOwnerForgetBuffer()

void ResourceOwnerForgetBuffer ( ResourceOwner  owner,
Buffer  buffer 
)

Definition at line 967 of file resowner.c.

968 {
969  if (!ResourceArrayRemove(&(owner->bufferarr), BufferGetDatum(buffer)))
970  elog(ERROR, "buffer %d is not owned by resource owner %s",
971  buffer, owner->name);
972 }
#define ERROR
Definition: elog.h:33
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 1089 of file resowner.c.

1090 {
1092  elog(ERROR, "catcache list reference %p is not owned by resource owner %s",
1093  list, owner->name);
1094 }

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 1054 of file resowner.c.

1055 {
1056  if (!ResourceArrayRemove(&(owner->catrefarr), PointerGetDatum(tuple)))
1057  elog(ERROR, "catcache reference %p is not owned by resource owner %s",
1058  tuple, owner->name);
1059 }

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

Referenced by ReleaseCatCache().

◆ ResourceOwnerForgetCryptoHash()

void ResourceOwnerForgetCryptoHash ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1430 of file resowner.c.

1431 {
1432  if (!ResourceArrayRemove(&(owner->cryptohasharr), handle))
1433  elog(ERROR, "cryptohash context %p is not owned by resource owner %s",
1434  DatumGetPointer(handle), owner->name);
1435 }

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 1350 of file resowner.c.

1351 {
1352  if (!ResourceArrayRemove(&(owner->dsmarr), PointerGetDatum(seg)))
1353  elog(ERROR, "dynamic shared memory segment %u is not owned by resource owner %s",
1354  dsm_segment_handle(seg), owner->name);
1355 }

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 1305 of file resowner.c.

1306 {
1307  if (!ResourceArrayRemove(&(owner->filearr), FileGetDatum(file)))
1308  elog(ERROR, "temporary file %d is not owned by resource owner %s",
1309  file, owner->name);
1310 }

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

Referenced by FileClose().

◆ ResourceOwnerForgetHMAC()

void ResourceOwnerForgetHMAC ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1475 of file resowner.c.

1476 {
1477  if (!ResourceArrayRemove(&(owner->hmacarr), handle))
1478  elog(ERROR, "HMAC context %p is not owned by resource owner %s",
1479  DatumGetPointer(handle), owner->name);
1480 }

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 1395 of file resowner.c.

1396 {
1397  if (!ResourceArrayRemove(&(owner->jitarr), handle))
1398  elog(ERROR, "JIT context %p is not owned by resource owner %s",
1399  DatumGetPointer(handle), owner->name);
1400 }

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 1005 of file resowner.c.

1006 {
1007  int i;
1008 
1009  if (owner->nlocks > MAX_RESOWNER_LOCKS)
1010  return; /* we have overflowed */
1011 
1012  Assert(owner->nlocks > 0);
1013  for (i = owner->nlocks - 1; i >= 0; i--)
1014  {
1015  if (locallock == owner->locks[i])
1016  {
1017  owner->locks[i] = owner->locks[owner->nlocks - 1];
1018  owner->nlocks--;
1019  return;
1020  }
1021  }
1022  elog(ERROR, "lock reference %p is not owned by resource owner %s",
1023  locallock, owner->name);
1024 }
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 1169 of file resowner.c.

1170 {
1171  if (!ResourceArrayRemove(&(owner->planrefarr), PointerGetDatum(plan)))
1172  elog(ERROR, "plancache reference %p is not owned by resource owner %s",
1173  plan, owner->name);
1174 }

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

Referenced by ReleaseCachedPlan().

◆ ResourceOwnerForgetRelationRef()

void ResourceOwnerForgetRelationRef ( ResourceOwner  owner,
Relation  rel 
)

Definition at line 1124 of file resowner.c.

1125 {
1126  if (!ResourceArrayRemove(&(owner->relrefarr), PointerGetDatum(rel)))
1127  elog(ERROR, "relcache reference %s is not owned by resource owner %s",
1128  RelationGetRelationName(rel), owner->name);
1129 }

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

Referenced by RelationDecrementReferenceCount().

◆ ResourceOwnerForgetSnapshot()

void ResourceOwnerForgetSnapshot ( ResourceOwner  owner,
Snapshot  snapshot 
)

Definition at line 1259 of file resowner.c.

1260 {
1261  if (!ResourceArrayRemove(&(owner->snapshotarr), PointerGetDatum(snapshot)))
1262  elog(ERROR, "snapshot reference %p is not owned by resource owner %s",
1263  snapshot, owner->name);
1264 }

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

Referenced by UnregisterSnapshotFromOwner().

◆ ResourceOwnerForgetTupleDesc()

void ResourceOwnerForgetTupleDesc ( ResourceOwner  owner,
TupleDesc  tupdesc 
)

Definition at line 1213 of file resowner.c.

1214 {
1215  if (!ResourceArrayRemove(&(owner->tupdescarr), PointerGetDatum(tupdesc)))
1216  elog(ERROR, "tupdesc reference %p is not owned by resource owner %s",
1217  tupdesc, owner->name);
1218 }

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

Referenced by DecrTupleDescRefCount().

◆ ResourceOwnerGetParent()

ResourceOwner ResourceOwnerGetParent ( ResourceOwner  owner)

Definition at line 792 of file resowner.c.

793 {
794  return owner->parent;
795 }

References ResourceOwnerData::parent.

Referenced by LockReassignCurrentOwner().

◆ ResourceOwnerNewParent()

void ResourceOwnerNewParent ( ResourceOwner  owner,
ResourceOwner  newparent 
)

Definition at line 801 of file resowner.c.

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

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 718 of file resowner.c.

719 {
720  Datum foundres;
721 
722  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
723  {
724  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
725 
726  ReleaseCachedPlan(res, owner);
727  }
728 }
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;
504  Datum foundres;
505 
506  /* Recurse to handle descendants */
507  for (child = owner->firstchild; child != NULL; child = child->nextchild)
508  ResourceOwnerReleaseInternal(child, phase, isCommit, isTopLevel);
509 
510  /*
511  * Make CurrentResourceOwner point to me, so that ReleaseBuffer etc don't
512  * get confused.
513  */
514  save = CurrentResourceOwner;
515  CurrentResourceOwner = owner;
516 
517  if (phase == RESOURCE_RELEASE_BEFORE_LOCKS)
518  {
519  /*
520  * Release buffer pins. Note that ReleaseBuffer will remove the
521  * buffer entry from our array, so we just have to iterate till there
522  * are none.
523  *
524  * During a commit, there shouldn't be any remaining pins --- that
525  * would indicate failure to clean up the executor correctly --- so
526  * issue warnings. In the abort case, just clean up quietly.
527  */
528  while (ResourceArrayGetAny(&(owner->bufferarr), &foundres))
529  {
530  Buffer res = DatumGetBuffer(foundres);
531 
532  if (isCommit)
535  }
536 
537  /* Ditto for relcache references */
538  while (ResourceArrayGetAny(&(owner->relrefarr), &foundres))
539  {
540  Relation res = (Relation) DatumGetPointer(foundres);
541 
542  if (isCommit)
545  }
546 
547  /* Ditto for dynamic shared memory segments */
548  while (ResourceArrayGetAny(&(owner->dsmarr), &foundres))
549  {
550  dsm_segment *res = (dsm_segment *) DatumGetPointer(foundres);
551 
552  if (isCommit)
554  dsm_detach(res);
555  }
556 
557  /* Ditto for JIT contexts */
558  while (ResourceArrayGetAny(&(owner->jitarr), &foundres))
559  {
560  JitContext *context = (JitContext *) PointerGetDatum(foundres);
561 
562  jit_release_context(context);
563  }
564 
565  /* Ditto for cryptohash contexts */
566  while (ResourceArrayGetAny(&(owner->cryptohasharr), &foundres))
567  {
568  pg_cryptohash_ctx *context =
569  (pg_cryptohash_ctx *) PointerGetDatum(foundres);
570 
571  if (isCommit)
572  PrintCryptoHashLeakWarning(foundres);
573  pg_cryptohash_free(context);
574  }
575 
576  /* Ditto for HMAC contexts */
577  while (ResourceArrayGetAny(&(owner->hmacarr), &foundres))
578  {
579  pg_hmac_ctx *context = (pg_hmac_ctx *) PointerGetDatum(foundres);
580 
581  if (isCommit)
582  PrintHMACLeakWarning(foundres);
583  pg_hmac_free(context);
584  }
585  }
586  else if (phase == RESOURCE_RELEASE_LOCKS)
587  {
588  if (isTopLevel)
589  {
590  /*
591  * For a top-level xact we are going to release all locks (or at
592  * least all non-session locks), so just do a single lmgr call at
593  * the top of the recursion.
594  */
595  if (owner == TopTransactionResourceOwner)
596  {
597  ProcReleaseLocks(isCommit);
598  ReleasePredicateLocks(isCommit, false);
599  }
600  }
601  else
602  {
603  /*
604  * Release locks retail. Note that if we are committing a
605  * subtransaction, we do NOT release its locks yet, but transfer
606  * them to the parent.
607  */
608  LOCALLOCK **locks;
609  int nlocks;
610 
611  Assert(owner->parent != NULL);
612 
613  /*
614  * Pass the list of locks owned by this resource owner to the lock
615  * manager, unless it has overflowed.
616  */
617  if (owner->nlocks > MAX_RESOWNER_LOCKS)
618  {
619  locks = NULL;
620  nlocks = 0;
621  }
622  else
623  {
624  locks = owner->locks;
625  nlocks = owner->nlocks;
626  }
627 
628  if (isCommit)
629  LockReassignCurrentOwner(locks, nlocks);
630  else
631  LockReleaseCurrentOwner(locks, nlocks);
632  }
633  }
634  else if (phase == RESOURCE_RELEASE_AFTER_LOCKS)
635  {
636  /*
637  * Release catcache references. Note that ReleaseCatCache will remove
638  * the catref entry from our array, so we just have to iterate till
639  * there are none.
640  *
641  * As with buffer pins, warn if any are left at commit time.
642  */
643  while (ResourceArrayGetAny(&(owner->catrefarr), &foundres))
644  {
645  HeapTuple res = (HeapTuple) DatumGetPointer(foundres);
646 
647  if (isCommit)
650  }
651 
652  /* Ditto for catcache lists */
653  while (ResourceArrayGetAny(&(owner->catlistrefarr), &foundres))
654  {
655  CatCList *res = (CatCList *) DatumGetPointer(foundres);
656 
657  if (isCommit)
660  }
661 
662  /* Ditto for plancache references */
663  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
664  {
665  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
666 
667  if (isCommit)
669  ReleaseCachedPlan(res, owner);
670  }
671 
672  /* Ditto for tupdesc references */
673  while (ResourceArrayGetAny(&(owner->tupdescarr), &foundres))
674  {
675  TupleDesc res = (TupleDesc) DatumGetPointer(foundres);
676 
677  if (isCommit)
680  }
681 
682  /* Ditto for snapshot references */
683  while (ResourceArrayGetAny(&(owner->snapshotarr), &foundres))
684  {
685  Snapshot res = (Snapshot) DatumGetPointer(foundres);
686 
687  if (isCommit)
690  }
691 
692  /* Ditto for temporary files */
693  while (ResourceArrayGetAny(&(owner->filearr), &foundres))
694  {
695  File res = DatumGetFile(foundres);
696 
697  if (isCommit)
699  FileClose(res);
700  }
701  }
702 
703  /* Let add-on modules get a chance too */
704  for (item = ResourceRelease_callbacks; item; item = item->next)
705  item->callback(phase, isCommit, isTopLevel, item->arg);
706 
707  CurrentResourceOwner = save;
708 }
int Buffer
Definition: buf.h:23
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3915
void PrintBufferLeakWarning(Buffer buffer)
Definition: bufmgr.c:2688
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:1961
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:3334
void RelationClose(Relation relation)
Definition: relcache.c:2159
struct RelationData * Relation
Definition: relcache.h:26
#define DatumGetBuffer(datum)
Definition: resowner.c:47
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
static void PrintPlanCacheLeakWarning(CachedPlan *plan)
Definition: resowner.c:1180
static void PrintSnapshotLeakWarning(Snapshot snapshot)
Definition: resowner.c:1270
static void PrintCryptoHashLeakWarning(Datum handle)
Definition: resowner.c:1441
static void PrintTupleDescLeakWarning(TupleDesc tupdesc)
Definition: resowner.c:1224
#define DatumGetFile(datum)
Definition: resowner.c:45
static void PrintRelCacheLeakWarning(Relation rel)
Definition: resowner.c:1135
static void PrintHMACLeakWarning(Datum handle)
Definition: resowner.c:1486
static void PrintDSMLeakWarning(dsm_segment *seg)
Definition: resowner.c:1361
static void PrintFileLeakWarning(File file)
Definition: resowner.c:1316
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:869
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, ResourceReleaseCallbackItem::next, ResourceOwnerData::nextchild, ResourceOwnerData::nlocks, ResourceOwnerData::parent, pg_cryptohash_free(), pg_hmac_free(), ResourceOwnerData::planrefarr, PointerGetDatum, 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 1080 of file resowner.c.

1081 {
1083 }

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

Referenced by SearchCatCacheList().

◆ ResourceOwnerRememberCatCacheRef()

void ResourceOwnerRememberCatCacheRef ( ResourceOwner  owner,
HeapTuple  tuple 
)

Definition at line 1045 of file resowner.c.

1046 {
1047  ResourceArrayAdd(&(owner->catrefarr), PointerGetDatum(tuple));
1048 }

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

Referenced by SearchCatCacheInternal(), and SearchCatCacheMiss().

◆ ResourceOwnerRememberCryptoHash()

void ResourceOwnerRememberCryptoHash ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1421 of file resowner.c.

1422 {
1423  ResourceArrayAdd(&(owner->cryptohasharr), handle);
1424 }

References ResourceOwnerData::cryptohasharr, and ResourceArrayAdd().

Referenced by pg_cryptohash_create().

◆ ResourceOwnerRememberDSM()

void ResourceOwnerRememberDSM ( ResourceOwner  owner,
dsm_segment seg 
)

Definition at line 1341 of file resowner.c.

1342 {
1343  ResourceArrayAdd(&(owner->dsmarr), PointerGetDatum(seg));
1344 }

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 1296 of file resowner.c.

1297 {
1298  ResourceArrayAdd(&(owner->filearr), FileGetDatum(file));
1299 }

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

Referenced by RegisterTemporaryFile().

◆ ResourceOwnerRememberHMAC()

void ResourceOwnerRememberHMAC ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1466 of file resowner.c.

1467 {
1468  ResourceArrayAdd(&(owner->hmacarr), handle);
1469 }

References ResourceOwnerData::hmacarr, and ResourceArrayAdd().

Referenced by pg_hmac_create().

◆ ResourceOwnerRememberJIT()

void ResourceOwnerRememberJIT ( ResourceOwner  owner,
Datum  handle 
)

Definition at line 1386 of file resowner.c.

1387 {
1388  ResourceArrayAdd(&(owner->jitarr), handle);
1389 }

References ResourceOwnerData::jitarr, and ResourceArrayAdd().

Referenced by llvm_create_context().

◆ ResourceOwnerRememberLock()

void ResourceOwnerRememberLock ( ResourceOwner  owner,
LOCALLOCK locallock 
)

Definition at line 985 of file resowner.c.

986 {
987  Assert(locallock != NULL);
988 
989  if (owner->nlocks > MAX_RESOWNER_LOCKS)
990  return; /* we have already overflowed */
991 
992  if (owner->nlocks < MAX_RESOWNER_LOCKS)
993  owner->locks[owner->nlocks] = locallock;
994  else
995  {
996  /* overflowed */
997  }
998  owner->nlocks++;
999 }

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 1115 of file resowner.c.

1116 {
1117  ResourceArrayAdd(&(owner->relrefarr), PointerGetDatum(rel));
1118 }

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

Referenced by RelationIncrementReferenceCount().

◆ ResourceOwnerRememberSnapshot()

void ResourceOwnerRememberSnapshot ( ResourceOwner  owner,
Snapshot  snapshot 
)

Definition at line 1250 of file resowner.c.

1251 {
1252  ResourceArrayAdd(&(owner->snapshotarr), PointerGetDatum(snapshot));
1253 }

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

Referenced by RegisterSnapshotOnOwner().

◆ ResourceOwnerRememberTupleDesc()

void ResourceOwnerRememberTupleDesc ( ResourceOwner  owner,
TupleDesc  tupdesc 
)

Definition at line 1204 of file resowner.c.

1205 {
1206  ResourceArrayAdd(&(owner->tupdescarr), PointerGetDatum(tupdesc));
1207 }

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

Referenced by IncrTupleDescRefCount().

◆ UnregisterResourceReleaseCallback()

void UnregisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 863 of file resowner.c.

864 {
867 
868  prev = NULL;
869  for (item = ResourceRelease_callbacks; item; prev = item, item = item->next)
870  {
871  if (item->callback == callback && item->arg == arg)
872  {
873  if (prev)
874  prev->next = item->next;
875  else
877  pfree(item);
878  break;
879  }
880  }
881 }

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(), ExecEvalJsonExprSubtrans(), 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