PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
resowner.c File Reference
#include "postgres.h"
#include "access/hash.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 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)
 
ResourceOwner ResourceOwnerCreate (ResourceOwner parent, const char *name)
 
void ResourceOwnerRelease (ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
 
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 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)
 

Variables

ResourceOwner CurrentResourceOwner = NULL
 
ResourceOwner CurTransactionResourceOwner = NULL
 
ResourceOwner TopTransactionResourceOwner = NULL
 
static
ResourceReleaseCallbackItem
ResourceRelease_callbacks = NULL
 

Macro Definition Documentation

#define BufferGetDatum (   buffer)    Int32GetDatum(buffer)
#define DatumGetBuffer (   datum)    ((Buffer) DatumGetInt32(datum))

Definition at line 42 of file resowner.c.

Referenced by ResourceOwnerReleaseInternal().

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

Definition at line 40 of file resowner.c.

Referenced by ResourceOwnerReleaseInternal().

#define FileGetDatum (   file)    Int32GetDatum(file)
#define MAX_RESOWNER_LOCKS   15
#define RESARRAY_INIT_SIZE   16

Definition at line 74 of file resowner.c.

Referenced by ResourceArrayEnlarge().

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

Definition at line 80 of file resowner.c.

Referenced by ResourceArrayAdd(), ResourceArrayGetAny(), and ResourceArrayRemove().

#define RESARRAY_MAX_ARRAY   64

Definition at line 79 of file resowner.c.

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

Definition at line 86 of file resowner.c.

Referenced by ResourceArrayEnlarge().

Typedef Documentation

Function Documentation

static void PrintDSMLeakWarning ( dsm_segment seg)
static

Definition at line 1251 of file resowner.c.

References dsm_segment_handle(), elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1252 {
1253  elog(WARNING, "dynamic shared memory leak: segment %u still referenced",
1254  dsm_segment_handle(seg));
1255 }
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1028
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
static void PrintFileLeakWarning ( File  file)
static

Definition at line 1206 of file resowner.c.

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1207 {
1208  elog(WARNING, "temporary file leak: File %d still referenced",
1209  file);
1210 }
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
static void PrintPlanCacheLeakWarning ( CachedPlan plan)
static

Definition at line 1070 of file resowner.c.

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1071 {
1072  elog(WARNING, "plancache reference leak: plan %p not closed", plan);
1073 }
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
static void PrintRelCacheLeakWarning ( Relation  rel)
static

Definition at line 1025 of file resowner.c.

References elog, RelationGetRelationName, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1026 {
1027  elog(WARNING, "relcache reference leak: relation \"%s\" not closed",
1029 }
#define RelationGetRelationName(relation)
Definition: rel.h:436
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
static void PrintSnapshotLeakWarning ( Snapshot  snapshot)
static

Definition at line 1160 of file resowner.c.

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1161 {
1162  elog(WARNING, "Snapshot reference leak: Snapshot %p still referenced",
1163  snapshot);
1164 }
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
static void PrintTupleDescLeakWarning ( TupleDesc  tupdesc)
static

Definition at line 1114 of file resowner.c.

References elog, tupleDesc::tdtypeid, tupleDesc::tdtypmod, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1115 {
1116  elog(WARNING,
1117  "TupleDesc reference leak: TupleDesc %p (%u,%d) still referenced",
1118  tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
1119 }
Oid tdtypeid
Definition: tupdesc.h:74
int32 tdtypmod
Definition: tupdesc.h:75
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
void RegisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 779 of file resowner.c.

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

Referenced by px_find_cipher(), and px_find_digest().

780 {
782 
783  item = (ResourceReleaseCallbackItem *)
786  item->callback = callback;
787  item->arg = arg;
790 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:152
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
MemoryContext TopMemoryContext
Definition: mcxt.c:43
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:147
ResourceReleaseCallback callback
Definition: resowner.c:148
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
void * arg
static void ResourceArrayAdd ( ResourceArray resarr,
Datum  value 
)
static

Definition at line 257 of file resowner.c.

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(), ResourceOwnerRememberDSM(), ResourceOwnerRememberFile(), ResourceOwnerRememberPlanCacheRef(), ResourceOwnerRememberRelationRef(), ResourceOwnerRememberSnapshot(), and ResourceOwnerRememberTupleDesc().

258 {
259  uint32 idx;
260 
261  Assert(value != resarr->invalidval);
262  Assert(resarr->nitems < resarr->maxitems);
263 
264  if (RESARRAY_IS_ARRAY(resarr))
265  {
266  /* Append to linear array. */
267  idx = resarr->nitems;
268  }
269  else
270  {
271  /* Insert into first free slot at or after hash location. */
272  uint32 mask = resarr->capacity - 1;
273 
274  idx = DatumGetUInt32(hash_any((void *) &value, sizeof(value))) & mask;
275  for (;;)
276  {
277  if (resarr->itemsarr[idx] == resarr->invalidval)
278  break;
279  idx = (idx + 1) & mask;
280  }
281  }
282  resarr->lastidx = idx;
283  resarr->itemsarr[idx] = value;
284  resarr->nitems++;
285 }
#define DatumGetUInt32(X)
Definition: postgres.h:492
uint32 lastidx
Definition: resowner.c:67
uint32 capacity
Definition: resowner.c:64
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
Datum * itemsarr
Definition: resowner.c:62
Datum invalidval
Definition: resowner.c:63
static struct @121 value
unsigned int uint32
Definition: c.h:258
uint32 maxitems
Definition: resowner.c:66
#define Assert(condition)
Definition: c.h:681
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
#define RESARRAY_IS_ARRAY(resarr)
Definition: resowner.c:80
uint32 nitems
Definition: resowner.c:65
static void ResourceArrayEnlarge ( ResourceArray resarr)
static

Definition at line 202 of file resowner.c.

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(), ResourceOwnerEnlargeDSMs(), ResourceOwnerEnlargeFiles(), ResourceOwnerEnlargePlanCacheRefs(), ResourceOwnerEnlargeRelationRefs(), ResourceOwnerEnlargeSnapshots(), and ResourceOwnerEnlargeTupleDescs().

203 {
204  uint32 i,
205  oldcap,
206  newcap;
207  Datum *olditemsarr;
208  Datum *newitemsarr;
209 
210  if (resarr->nitems < resarr->maxitems)
211  return; /* no work needed */
212 
213  olditemsarr = resarr->itemsarr;
214  oldcap = resarr->capacity;
215 
216  /* Double the capacity of the array (capacity must stay a power of 2!) */
217  newcap = (oldcap > 0) ? oldcap * 2 : RESARRAY_INIT_SIZE;
218  newitemsarr = (Datum *) MemoryContextAlloc(TopMemoryContext,
219  newcap * sizeof(Datum));
220  for (i = 0; i < newcap; i++)
221  newitemsarr[i] = resarr->invalidval;
222 
223  /* We assume we can't fail below this point, so OK to scribble on resarr */
224  resarr->itemsarr = newitemsarr;
225  resarr->capacity = newcap;
226  resarr->maxitems = RESARRAY_MAX_ITEMS(newcap);
227  resarr->nitems = 0;
228 
229  if (olditemsarr != NULL)
230  {
231  /*
232  * Transfer any pre-existing entries into the new array; they don't
233  * necessarily go where they were before, so this simple logic is the
234  * best way. Note that if we were managing the set as a simple array,
235  * the entries after nitems are garbage, but that shouldn't matter
236  * because we won't get here unless nitems was equal to oldcap.
237  */
238  for (i = 0; i < oldcap; i++)
239  {
240  if (olditemsarr[i] != resarr->invalidval)
241  ResourceArrayAdd(resarr, olditemsarr[i]);
242  }
243 
244  /* And release old array. */
245  pfree(olditemsarr);
246  }
247 
248  Assert(resarr->nitems < resarr->maxitems);
249 }
#define RESARRAY_INIT_SIZE
Definition: resowner.c:74
uint32 capacity
Definition: resowner.c:64
#define RESARRAY_MAX_ITEMS(capacity)
Definition: resowner.c:86
void pfree(void *pointer)
Definition: mcxt.c:949
Datum * itemsarr
Definition: resowner.c:62
Datum invalidval
Definition: resowner.c:63
unsigned int uint32
Definition: c.h:258
MemoryContext TopMemoryContext
Definition: mcxt.c:43
uint32 maxitems
Definition: resowner.c:66
uintptr_t Datum
Definition: postgres.h:372
#define Assert(condition)
Definition: c.h:681
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
int i
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
uint32 nitems
Definition: resowner.c:65
static void ResourceArrayFree ( ResourceArray resarr)
static

Definition at line 396 of file resowner.c.

References ResourceArray::itemsarr, and pfree().

Referenced by ResourceOwnerDelete().

397 {
398  if (resarr->itemsarr)
399  pfree(resarr->itemsarr);
400 }
void pfree(void *pointer)
Definition: mcxt.c:949
Datum * itemsarr
Definition: resowner.c:62
static bool ResourceArrayGetAny ( ResourceArray resarr,
Datum value 
)
static

Definition at line 364 of file resowner.c.

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

Referenced by ResourceOwnerReleaseInternal().

365 {
366  if (resarr->nitems == 0)
367  return false;
368 
369  if (RESARRAY_IS_ARRAY(resarr))
370  {
371  /* Linear array: just return the first element. */
372  resarr->lastidx = 0;
373  }
374  else
375  {
376  /* Hash: search forward from wherever we were last. */
377  uint32 mask = resarr->capacity - 1;
378 
379  for (;;)
380  {
381  resarr->lastidx &= mask;
382  if (resarr->itemsarr[resarr->lastidx] != resarr->invalidval)
383  break;
384  resarr->lastidx++;
385  }
386  }
387 
388  *value = resarr->itemsarr[resarr->lastidx];
389  return true;
390 }
uint32 lastidx
Definition: resowner.c:67
uint32 capacity
Definition: resowner.c:64
Datum * itemsarr
Definition: resowner.c:62
Datum invalidval
Definition: resowner.c:63
static struct @121 value
unsigned int uint32
Definition: c.h:258
#define RESARRAY_IS_ARRAY(resarr)
Definition: resowner.c:80
uint32 nitems
Definition: resowner.c:65
static void ResourceArrayInit ( ResourceArray resarr,
Datum  invalidval 
)
static

Definition at line 183 of file resowner.c.

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

Referenced by ResourceOwnerCreate().

184 {
185  /* Assert it's empty */
186  Assert(resarr->itemsarr == NULL);
187  Assert(resarr->capacity == 0);
188  Assert(resarr->nitems == 0);
189  Assert(resarr->maxitems == 0);
190  /* Remember the appropriate "invalid" value */
191  resarr->invalidval = invalidval;
192  /* We don't allocate any storage until needed */
193 }
uint32 capacity
Definition: resowner.c:64
Datum * itemsarr
Definition: resowner.c:62
Datum invalidval
Definition: resowner.c:63
uint32 maxitems
Definition: resowner.c:66
#define Assert(condition)
Definition: c.h:681
uint32 nitems
Definition: resowner.c:65
static bool ResourceArrayRemove ( ResourceArray resarr,
Datum  value 
)
static

Definition at line 295 of file resowner.c.

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

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

296 {
297  uint32 i,
298  idx,
299  lastidx = resarr->lastidx;
300 
301  Assert(value != resarr->invalidval);
302 
303  /* Search through all items, but try lastidx first. */
304  if (RESARRAY_IS_ARRAY(resarr))
305  {
306  if (lastidx < resarr->nitems &&
307  resarr->itemsarr[lastidx] == value)
308  {
309  resarr->itemsarr[lastidx] = resarr->itemsarr[resarr->nitems - 1];
310  resarr->nitems--;
311  /* Update lastidx to make reverse-order removals fast. */
312  resarr->lastidx = resarr->nitems - 1;
313  return true;
314  }
315  for (i = 0; i < resarr->nitems; i++)
316  {
317  if (resarr->itemsarr[i] == value)
318  {
319  resarr->itemsarr[i] = resarr->itemsarr[resarr->nitems - 1];
320  resarr->nitems--;
321  /* Update lastidx to make reverse-order removals fast. */
322  resarr->lastidx = resarr->nitems - 1;
323  return true;
324  }
325  }
326  }
327  else
328  {
329  uint32 mask = resarr->capacity - 1;
330 
331  if (lastidx < resarr->capacity &&
332  resarr->itemsarr[lastidx] == value)
333  {
334  resarr->itemsarr[lastidx] = resarr->invalidval;
335  resarr->nitems--;
336  return true;
337  }
338  idx = DatumGetUInt32(hash_any((void *) &value, sizeof(value))) & mask;
339  for (i = 0; i < resarr->capacity; i++)
340  {
341  if (resarr->itemsarr[idx] == value)
342  {
343  resarr->itemsarr[idx] = resarr->invalidval;
344  resarr->nitems--;
345  return true;
346  }
347  idx = (idx + 1) & mask;
348  }
349  }
350 
351  return false;
352 }
#define DatumGetUInt32(X)
Definition: postgres.h:492
uint32 lastidx
Definition: resowner.c:67
uint32 capacity
Definition: resowner.c:64
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
Datum * itemsarr
Definition: resowner.c:62
Datum invalidval
Definition: resowner.c:63
static struct @121 value
unsigned int uint32
Definition: c.h:258
#define Assert(condition)
Definition: c.h:681
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
int i
#define RESARRAY_IS_ARRAY(resarr)
Definition: resowner.c:80
uint32 nitems
Definition: resowner.c:65
ResourceOwner ResourceOwnerCreate ( ResourceOwner  parent,
const char *  name 
)

Definition at line 416 of file resowner.c.

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

Referenced by ApplyWorkerMain(), AtStart_ResourceOwner(), AtSubStart_ResourceOwner(), BackgroundWriterMain(), CheckpointerMain(), CreatePortal(), ParallelWorkerMain(), pg_logical_slot_get_changes_guts(), test_shm_mq_main(), WalReceiverMain(), WalSndShutdown(), and WalWriterMain().

417 {
418  ResourceOwner owner;
419 
421  sizeof(ResourceOwnerData));
422  owner->name = name;
423 
424  if (parent)
425  {
426  owner->parent = parent;
427  owner->nextchild = parent->firstchild;
428  parent->firstchild = owner;
429  }
430 
432  ResourceArrayInit(&(owner->catrefarr), PointerGetDatum(NULL));
434  ResourceArrayInit(&(owner->relrefarr), PointerGetDatum(NULL));
435  ResourceArrayInit(&(owner->planrefarr), PointerGetDatum(NULL));
436  ResourceArrayInit(&(owner->tupdescarr), PointerGetDatum(NULL));
438  ResourceArrayInit(&(owner->filearr), FileGetDatum(-1));
439  ResourceArrayInit(&(owner->dsmarr), PointerGetDatum(NULL));
440 
441  return owner;
442 }
ResourceArray relrefarr
Definition: resowner.c:121
ResourceArray catrefarr
Definition: resowner.c:119
#define PointerGetDatum(X)
Definition: postgres.h:562
#define InvalidBuffer
Definition: buf.h:25
ResourceArray bufferarr
Definition: resowner.c:118
ResourceArray snapshotarr
Definition: resowner.c:124
struct ResourceOwnerData * ResourceOwner
Definition: resowner.h:27
const char * name
Definition: resowner.c:115
ResourceArray planrefarr
Definition: resowner.c:122
ResourceArray tupdescarr
Definition: resowner.c:123
static void ResourceArrayInit(ResourceArray *resarr, Datum invalidval)
Definition: resowner.c:183
MemoryContext TopMemoryContext
Definition: mcxt.c:43
ResourceOwner firstchild
Definition: resowner.c:113
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:741
#define BufferGetDatum(buffer)
Definition: resowner.c:41
#define FileGetDatum(file)
Definition: resowner.c:39
ResourceOwner parent
Definition: resowner.c:112
const char * name
Definition: encode.c:521
ResourceArray catlistrefarr
Definition: resowner.c:120
ResourceArray dsmarr
Definition: resowner.c:126
ResourceOwner nextchild
Definition: resowner.c:114
ResourceArray filearr
Definition: resowner.c:125
void ResourceOwnerDelete ( ResourceOwner  owner)

Definition at line 673 of file resowner.c.

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

Referenced by CleanupSubTransaction(), CleanupTransaction(), CommitSubTransaction(), CommitTransaction(), PortalDrop(), PrepareTransaction(), and ResourceOwnerDelete().

674 {
675  /* We had better not be deleting CurrentResourceOwner ... */
676  Assert(owner != CurrentResourceOwner);
677 
678  /* And it better not own any resources, either */
679  Assert(owner->bufferarr.nitems == 0);
680  Assert(owner->catrefarr.nitems == 0);
681  Assert(owner->catlistrefarr.nitems == 0);
682  Assert(owner->relrefarr.nitems == 0);
683  Assert(owner->planrefarr.nitems == 0);
684  Assert(owner->tupdescarr.nitems == 0);
685  Assert(owner->snapshotarr.nitems == 0);
686  Assert(owner->filearr.nitems == 0);
687  Assert(owner->dsmarr.nitems == 0);
688  Assert(owner->nlocks == 0 || owner->nlocks == MAX_RESOWNER_LOCKS + 1);
689 
690  /*
691  * Delete children. The recursive call will delink the child from me, so
692  * just iterate as long as there is a child.
693  */
694  while (owner->firstchild != NULL)
696 
697  /*
698  * We delink the owner from its parent before deleting it, so that if
699  * there's an error we won't have deleted/busted owners still attached to
700  * the owner tree. Better a leak than a crash.
701  */
702  ResourceOwnerNewParent(owner, NULL);
703 
704  /* And free the object. */
705  ResourceArrayFree(&(owner->bufferarr));
706  ResourceArrayFree(&(owner->catrefarr));
707  ResourceArrayFree(&(owner->catlistrefarr));
708  ResourceArrayFree(&(owner->relrefarr));
709  ResourceArrayFree(&(owner->planrefarr));
710  ResourceArrayFree(&(owner->tupdescarr));
711  ResourceArrayFree(&(owner->snapshotarr));
712  ResourceArrayFree(&(owner->filearr));
713  ResourceArrayFree(&(owner->dsmarr));
714 
715  pfree(owner);
716 }
ResourceArray relrefarr
Definition: resowner.c:121
ResourceArray catrefarr
Definition: resowner.c:119
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:673
ResourceArray bufferarr
Definition: resowner.c:118
ResourceArray snapshotarr
Definition: resowner.c:124
#define MAX_RESOWNER_LOCKS
Definition: resowner.c:105
void pfree(void *pointer)
Definition: mcxt.c:949
ResourceArray planrefarr
Definition: resowner.c:122
static void ResourceArrayFree(ResourceArray *resarr)
Definition: resowner.c:396
ResourceArray tupdescarr
Definition: resowner.c:123
ResourceOwner firstchild
Definition: resowner.c:113
#define Assert(condition)
Definition: c.h:681
void ResourceOwnerNewParent(ResourceOwner owner, ResourceOwner newparent)
Definition: resowner.c:731
ResourceArray catlistrefarr
Definition: resowner.c:120
ResourceArray dsmarr
Definition: resowner.c:126
uint32 nitems
Definition: resowner.c:65
ResourceArray filearr
Definition: resowner.c:125
void ResourceOwnerEnlargeBuffers ( ResourceOwner  owner)

Definition at line 825 of file resowner.c.

References ResourceOwnerData::bufferarr, and ResourceArrayEnlarge().

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

826 {
827  if (owner == NULL)
828  return;
829  ResourceArrayEnlarge(&(owner->bufferarr));
830 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray bufferarr
Definition: resowner.c:118
void ResourceOwnerEnlargeCatCacheListRefs ( ResourceOwner  owner)

Definition at line 959 of file resowner.c.

References ResourceOwnerData::catlistrefarr, and ResourceArrayEnlarge().

Referenced by SearchCatCacheList().

960 {
962 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray catlistrefarr
Definition: resowner.c:120
void ResourceOwnerEnlargeCatCacheRefs ( ResourceOwner  owner)

Definition at line 924 of file resowner.c.

References ResourceOwnerData::catrefarr, and ResourceArrayEnlarge().

Referenced by SearchCatCacheInternal(), and SearchCatCacheMiss().

925 {
926  ResourceArrayEnlarge(&(owner->catrefarr));
927 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray catrefarr
Definition: resowner.c:119
void ResourceOwnerEnlargeDSMs ( ResourceOwner  owner)

Definition at line 1220 of file resowner.c.

References ResourceOwnerData::dsmarr, and ResourceArrayEnlarge().

Referenced by dsm_create_descriptor(), and dsm_unpin_mapping().

1221 {
1222  ResourceArrayEnlarge(&(owner->dsmarr));
1223 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray dsmarr
Definition: resowner.c:126
void ResourceOwnerEnlargeFiles ( ResourceOwner  owner)

Definition at line 1175 of file resowner.c.

References ResourceOwnerData::filearr, and ResourceArrayEnlarge().

Referenced by OpenTemporaryFile().

1176 {
1177  ResourceArrayEnlarge(&(owner->filearr));
1178 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray filearr
Definition: resowner.c:125
void ResourceOwnerEnlargePlanCacheRefs ( ResourceOwner  owner)

Definition at line 1039 of file resowner.c.

References ResourceOwnerData::planrefarr, and ResourceArrayEnlarge().

Referenced by GetCachedPlan().

1040 {
1041  ResourceArrayEnlarge(&(owner->planrefarr));
1042 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray planrefarr
Definition: resowner.c:122
void ResourceOwnerEnlargeRelationRefs ( ResourceOwner  owner)

Definition at line 994 of file resowner.c.

References ResourceOwnerData::relrefarr, and ResourceArrayEnlarge().

Referenced by RelationIncrementReferenceCount().

995 {
996  ResourceArrayEnlarge(&(owner->relrefarr));
997 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray relrefarr
Definition: resowner.c:121
void ResourceOwnerEnlargeSnapshots ( ResourceOwner  owner)

Definition at line 1129 of file resowner.c.

References ResourceArrayEnlarge(), and ResourceOwnerData::snapshotarr.

Referenced by RegisterSnapshotOnOwner().

1130 {
1131  ResourceArrayEnlarge(&(owner->snapshotarr));
1132 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray snapshotarr
Definition: resowner.c:124
void ResourceOwnerEnlargeTupleDescs ( ResourceOwner  owner)

Definition at line 1083 of file resowner.c.

References ResourceArrayEnlarge(), and ResourceOwnerData::tupdescarr.

Referenced by IncrTupleDescRefCount().

1084 {
1085  ResourceArrayEnlarge(&(owner->tupdescarr));
1086 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray tupdescarr
Definition: resowner.c:123
void ResourceOwnerForgetBuffer ( ResourceOwner  owner,
Buffer  buffer 
)

Definition at line 855 of file resowner.c.

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

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

856 {
857  if (owner == NULL)
858  return;
860  elog(ERROR, "buffer %d is not owned by resource owner %s",
861  buffer, owner->name);
862 }
ResourceArray bufferarr
Definition: resowner.c:118
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
#define BufferGetDatum(buffer)
Definition: resowner.c:41
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
#define elog
Definition: elog.h:219
void ResourceOwnerForgetCatCacheListRef ( ResourceOwner  owner,
CatCList list 
)

Definition at line 979 of file resowner.c.

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

Referenced by ReleaseCatCacheList().

980 {
981  if (!ResourceArrayRemove(&(owner->catlistrefarr), PointerGetDatum(list)))
982  elog(ERROR, "catcache list reference %p is not owned by resource owner %s",
983  list, owner->name);
984 }
#define PointerGetDatum(X)
Definition: postgres.h:562
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
ResourceArray catlistrefarr
Definition: resowner.c:120
#define elog
Definition: elog.h:219
void ResourceOwnerForgetCatCacheRef ( ResourceOwner  owner,
HeapTuple  tuple 
)

Definition at line 944 of file resowner.c.

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

Referenced by ReleaseCatCache().

945 {
946  if (!ResourceArrayRemove(&(owner->catrefarr), PointerGetDatum(tuple)))
947  elog(ERROR, "catcache reference %p is not owned by resource owner %s",
948  tuple, owner->name);
949 }
ResourceArray catrefarr
Definition: resowner.c:119
#define PointerGetDatum(X)
Definition: postgres.h:562
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
#define elog
Definition: elog.h:219
void ResourceOwnerForgetDSM ( ResourceOwner  owner,
dsm_segment seg 
)

Definition at line 1240 of file resowner.c.

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

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

1241 {
1242  if (!ResourceArrayRemove(&(owner->dsmarr), PointerGetDatum(seg)))
1243  elog(ERROR, "dynamic shared memory segment %u is not owned by resource owner %s",
1244  dsm_segment_handle(seg), owner->name);
1245 }
#define PointerGetDatum(X)
Definition: postgres.h:562
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1028
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
ResourceArray dsmarr
Definition: resowner.c:126
#define elog
Definition: elog.h:219
void ResourceOwnerForgetFile ( ResourceOwner  owner,
File  file 
)

Definition at line 1195 of file resowner.c.

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

Referenced by FileClose().

1196 {
1197  if (!ResourceArrayRemove(&(owner->filearr), FileGetDatum(file)))
1198  elog(ERROR, "temporary file %d is not owned by resource owner %s",
1199  file, owner->name);
1200 }
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
#define FileGetDatum(file)
Definition: resowner.c:39
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
#define elog
Definition: elog.h:219
ResourceArray filearr
Definition: resowner.c:125
void ResourceOwnerForgetLock ( ResourceOwner  owner,
LOCALLOCK locallock 
)

Definition at line 895 of file resowner.c.

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

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

896 {
897  int i;
898 
899  if (owner->nlocks > MAX_RESOWNER_LOCKS)
900  return; /* we have overflowed */
901 
902  Assert(owner->nlocks > 0);
903  for (i = owner->nlocks - 1; i >= 0; i--)
904  {
905  if (locallock == owner->locks[i])
906  {
907  owner->locks[i] = owner->locks[owner->nlocks - 1];
908  owner->nlocks--;
909  return;
910  }
911  }
912  elog(ERROR, "lock reference %p is not owned by resource owner %s",
913  locallock, owner->name);
914 }
#define MAX_RESOWNER_LOCKS
Definition: resowner.c:105
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:681
LOCALLOCK * locks[MAX_RESOWNER_LOCKS]
Definition: resowner.c:130
int i
#define elog
Definition: elog.h:219
void ResourceOwnerForgetPlanCacheRef ( ResourceOwner  owner,
CachedPlan plan 
)

Definition at line 1059 of file resowner.c.

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

Referenced by ReleaseCachedPlan().

1060 {
1061  if (!ResourceArrayRemove(&(owner->planrefarr), PointerGetDatum(plan)))
1062  elog(ERROR, "plancache reference %p is not owned by resource owner %s",
1063  plan, owner->name);
1064 }
#define PointerGetDatum(X)
Definition: postgres.h:562
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
ResourceArray planrefarr
Definition: resowner.c:122
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
#define elog
Definition: elog.h:219
void ResourceOwnerForgetRelationRef ( ResourceOwner  owner,
Relation  rel 
)

Definition at line 1014 of file resowner.c.

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

Referenced by RelationDecrementReferenceCount().

1015 {
1016  if (!ResourceArrayRemove(&(owner->relrefarr), PointerGetDatum(rel)))
1017  elog(ERROR, "relcache reference %s is not owned by resource owner %s",
1018  RelationGetRelationName(rel), owner->name);
1019 }
ResourceArray relrefarr
Definition: resowner.c:121
#define PointerGetDatum(X)
Definition: postgres.h:562
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:436
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
#define elog
Definition: elog.h:219
void ResourceOwnerForgetSnapshot ( ResourceOwner  owner,
Snapshot  snapshot 
)

Definition at line 1149 of file resowner.c.

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

Referenced by UnregisterSnapshotFromOwner().

1150 {
1151  if (!ResourceArrayRemove(&(owner->snapshotarr), PointerGetDatum(snapshot)))
1152  elog(ERROR, "snapshot reference %p is not owned by resource owner %s",
1153  snapshot, owner->name);
1154 }
#define PointerGetDatum(X)
Definition: postgres.h:562
ResourceArray snapshotarr
Definition: resowner.c:124
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
#define elog
Definition: elog.h:219
void ResourceOwnerForgetTupleDesc ( ResourceOwner  owner,
TupleDesc  tupdesc 
)

Definition at line 1103 of file resowner.c.

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

Referenced by DecrTupleDescRefCount().

1104 {
1105  if (!ResourceArrayRemove(&(owner->tupdescarr), PointerGetDatum(tupdesc)))
1106  elog(ERROR, "tupdesc reference %p is not owned by resource owner %s",
1107  tupdesc, owner->name);
1108 }
#define PointerGetDatum(X)
Definition: postgres.h:562
const char * name
Definition: resowner.c:115
#define ERROR
Definition: elog.h:43
ResourceArray tupdescarr
Definition: resowner.c:123
static bool ResourceArrayRemove(ResourceArray *resarr, Datum value)
Definition: resowner.c:295
#define elog
Definition: elog.h:219
ResourceOwner ResourceOwnerGetParent ( ResourceOwner  owner)

Definition at line 722 of file resowner.c.

References ResourceOwnerData::parent.

Referenced by LockReassignCurrentOwner().

723 {
724  return owner->parent;
725 }
ResourceOwner parent
Definition: resowner.c:112
void ResourceOwnerNewParent ( ResourceOwner  owner,
ResourceOwner  newparent 
)

Definition at line 731 of file resowner.c.

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

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

733 {
734  ResourceOwner oldparent = owner->parent;
735 
736  if (oldparent)
737  {
738  if (owner == oldparent->firstchild)
739  oldparent->firstchild = owner->nextchild;
740  else
741  {
742  ResourceOwner child;
743 
744  for (child = oldparent->firstchild; child; child = child->nextchild)
745  {
746  if (owner == child->nextchild)
747  {
748  child->nextchild = owner->nextchild;
749  break;
750  }
751  }
752  }
753  }
754 
755  if (newparent)
756  {
757  Assert(owner != newparent);
758  owner->parent = newparent;
759  owner->nextchild = newparent->firstchild;
760  newparent->firstchild = owner;
761  }
762  else
763  {
764  owner->parent = NULL;
765  owner->nextchild = NULL;
766  }
767 }
ResourceOwner firstchild
Definition: resowner.c:113
#define Assert(condition)
Definition: c.h:681
ResourceOwner parent
Definition: resowner.c:112
ResourceOwner nextchild
Definition: resowner.c:114
void ResourceOwnerRelease ( ResourceOwner  owner,
ResourceReleasePhase  phase,
bool  isCommit,
bool  isTopLevel 
)

Definition at line 471 of file resowner.c.

References ResourceOwnerReleaseInternal().

Referenced by AbortSubTransaction(), AbortTransaction(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), CommitSubTransaction(), CommitTransaction(), PortalDrop(), PrepareTransaction(), and WalWriterMain().

475 {
476  /* There's not currently any setup needed before recursing */
477  ResourceOwnerReleaseInternal(owner, phase, isCommit, isTopLevel);
478 }
static void ResourceOwnerReleaseInternal(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:481
static void ResourceOwnerReleaseInternal ( ResourceOwner  owner,
ResourceReleasePhase  phase,
bool  isCommit,
bool  isTopLevel 
)
static

Definition at line 481 of file resowner.c.

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

Referenced by ResourceOwnerRelease().

485 {
486  ResourceOwner child;
487  ResourceOwner save;
489  Datum foundres;
490 
491  /* Recurse to handle descendants */
492  for (child = owner->firstchild; child != NULL; child = child->nextchild)
493  ResourceOwnerReleaseInternal(child, phase, isCommit, isTopLevel);
494 
495  /*
496  * Make CurrentResourceOwner point to me, so that ReleaseBuffer etc don't
497  * get confused.
498  */
499  save = CurrentResourceOwner;
500  CurrentResourceOwner = owner;
501 
502  if (phase == RESOURCE_RELEASE_BEFORE_LOCKS)
503  {
504  /*
505  * Release buffer pins. Note that ReleaseBuffer will remove the
506  * buffer entry from our array, so we just have to iterate till there
507  * are none.
508  *
509  * During a commit, there shouldn't be any remaining pins --- that
510  * would indicate failure to clean up the executor correctly --- so
511  * issue warnings. In the abort case, just clean up quietly.
512  */
513  while (ResourceArrayGetAny(&(owner->bufferarr), &foundres))
514  {
515  Buffer res = DatumGetBuffer(foundres);
516 
517  if (isCommit)
519  ReleaseBuffer(res);
520  }
521 
522  /* Ditto for relcache references */
523  while (ResourceArrayGetAny(&(owner->relrefarr), &foundres))
524  {
525  Relation res = (Relation) DatumGetPointer(foundres);
526 
527  if (isCommit)
529  RelationClose(res);
530  }
531 
532  /* Ditto for dynamic shared memory segments */
533  while (ResourceArrayGetAny(&(owner->dsmarr), &foundres))
534  {
535  dsm_segment *res = (dsm_segment *) DatumGetPointer(foundres);
536 
537  if (isCommit)
538  PrintDSMLeakWarning(res);
539  dsm_detach(res);
540  }
541  }
542  else if (phase == RESOURCE_RELEASE_LOCKS)
543  {
544  if (isTopLevel)
545  {
546  /*
547  * For a top-level xact we are going to release all locks (or at
548  * least all non-session locks), so just do a single lmgr call at
549  * the top of the recursion.
550  */
551  if (owner == TopTransactionResourceOwner)
552  {
553  ProcReleaseLocks(isCommit);
554  ReleasePredicateLocks(isCommit);
555  }
556  }
557  else
558  {
559  /*
560  * Release locks retail. Note that if we are committing a
561  * subtransaction, we do NOT release its locks yet, but transfer
562  * them to the parent.
563  */
564  LOCALLOCK **locks;
565  int nlocks;
566 
567  Assert(owner->parent != NULL);
568 
569  /*
570  * Pass the list of locks owned by this resource owner to the lock
571  * manager, unless it has overflowed.
572  */
573  if (owner->nlocks > MAX_RESOWNER_LOCKS)
574  {
575  locks = NULL;
576  nlocks = 0;
577  }
578  else
579  {
580  locks = owner->locks;
581  nlocks = owner->nlocks;
582  }
583 
584  if (isCommit)
585  LockReassignCurrentOwner(locks, nlocks);
586  else
587  LockReleaseCurrentOwner(locks, nlocks);
588  }
589  }
590  else if (phase == RESOURCE_RELEASE_AFTER_LOCKS)
591  {
592  /*
593  * Release catcache references. Note that ReleaseCatCache will remove
594  * the catref entry from our array, so we just have to iterate till
595  * there are none.
596  *
597  * As with buffer pins, warn if any are left at commit time.
598  */
599  while (ResourceArrayGetAny(&(owner->catrefarr), &foundres))
600  {
601  HeapTuple res = (HeapTuple) DatumGetPointer(foundres);
602 
603  if (isCommit)
605  ReleaseCatCache(res);
606  }
607 
608  /* Ditto for catcache lists */
609  while (ResourceArrayGetAny(&(owner->catlistrefarr), &foundres))
610  {
611  CatCList *res = (CatCList *) DatumGetPointer(foundres);
612 
613  if (isCommit)
615  ReleaseCatCacheList(res);
616  }
617 
618  /* Ditto for plancache references */
619  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
620  {
621  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
622 
623  if (isCommit)
625  ReleaseCachedPlan(res, true);
626  }
627 
628  /* Ditto for tupdesc references */
629  while (ResourceArrayGetAny(&(owner->tupdescarr), &foundres))
630  {
631  TupleDesc res = (TupleDesc) DatumGetPointer(foundres);
632 
633  if (isCommit)
636  }
637 
638  /* Ditto for snapshot references */
639  while (ResourceArrayGetAny(&(owner->snapshotarr), &foundres))
640  {
641  Snapshot res = (Snapshot) DatumGetPointer(foundres);
642 
643  if (isCommit)
645  UnregisterSnapshot(res);
646  }
647 
648  /* Ditto for temporary files */
649  while (ResourceArrayGetAny(&(owner->filearr), &foundres))
650  {
651  File res = DatumGetFile(foundres);
652 
653  if (isCommit)
655  FileClose(res);
656  }
657  }
658 
659  /* Let add-on modules get a chance too */
660  for (item = ResourceRelease_callbacks; item; item = item->next)
661  item->callback(phase, isCommit, isTopLevel, item->arg);
662 
663  CurrentResourceOwner = save;
664 }
ResourceArray relrefarr
Definition: resowner.c:121
void PrintCatCacheListLeakWarning(CatCList *list)
Definition: catcache.c:2110
HeapTupleData * HeapTuple
Definition: htup.h:70
void PrintBufferLeakWarning(Buffer buffer)
Definition: bufmgr.c:2531
ResourceArray catrefarr
Definition: resowner.c:119
#define DatumGetFile(datum)
Definition: resowner.c:40
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
ResourceArray bufferarr
Definition: resowner.c:118
static bool ResourceArrayGetAny(ResourceArray *resarr, Datum *value)
Definition: resowner.c:364
ResourceArray snapshotarr
Definition: resowner.c:124
struct SnapshotData * Snapshot
Definition: snapshot.h:23
static void ResourceOwnerReleaseInternal(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:481
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:152
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:1795
#define MAX_RESOWNER_LOCKS
Definition: resowner.c:105
void LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2318
struct RelationData * Relation
Definition: relcache.h:21
static void PrintSnapshotLeakWarning(Snapshot snapshot)
Definition: resowner.c:1160
ResourceArray planrefarr
Definition: resowner.c:122
void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2413
static void PrintPlanCacheLeakWarning(CachedPlan *plan)
Definition: resowner.c:1070
void ReleaseCachedPlan(CachedPlan *plan, bool useResOwner)
Definition: plancache.c:1256
ResourceArray tupdescarr
Definition: resowner.c:123
void ProcReleaseLocks(bool isCommit)
Definition: proc.c:762
void RelationClose(Relation relation)
Definition: relcache.c:2164
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:905
static void PrintRelCacheLeakWarning(Relation rel)
Definition: resowner.c:1025
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:147
uintptr_t Datum
Definition: postgres.h:372
ResourceReleaseCallback callback
Definition: resowner.c:148
ResourceOwner firstchild
Definition: resowner.c:113
void PrintCatCacheLeakWarning(HeapTuple tuple)
Definition: catcache.c:2094
static void PrintTupleDescLeakWarning(TupleDesc tupdesc)
Definition: resowner.c:1114
void ReleaseCatCache(HeapTuple tuple)
Definition: catcache.c:1458
void ReleasePredicateLocks(bool isCommit)
Definition: predicate.c:3268
struct tupleDesc * TupleDesc
void FileClose(File file)
Definition: fd.c:1516
#define Assert(condition)
Definition: c.h:681
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:319
static void PrintFileLeakWarning(File file)
Definition: resowner.c:1206
ResourceOwner parent
Definition: resowner.c:112
#define DatumGetPointer(X)
Definition: postgres.h:555
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:726
#define DatumGetBuffer(datum)
Definition: resowner.c:42
LOCALLOCK * locks[MAX_RESOWNER_LOCKS]
Definition: resowner.c:130
ResourceArray catlistrefarr
Definition: resowner.c:120
ResourceArray dsmarr
Definition: resowner.c:126
ResourceOwner nextchild
Definition: resowner.c:114
static void PrintDSMLeakWarning(dsm_segment *seg)
Definition: resowner.c:1251
int Buffer
Definition: buf.h:23
int File
Definition: fd.h:49
ResourceArray filearr
Definition: resowner.c:125
void ResourceOwnerRememberBuffer ( ResourceOwner  owner,
Buffer  buffer 
)

Definition at line 841 of file resowner.c.

References ResourceOwnerData::bufferarr, BufferGetDatum, and ResourceArrayAdd().

Referenced by IncrBufferRefCount(), LocalBufferAlloc(), PinBuffer(), and PinBuffer_Locked().

842 {
843  if (owner == NULL)
844  return;
846 }
ResourceArray bufferarr
Definition: resowner.c:118
#define BufferGetDatum(buffer)
Definition: resowner.c:41
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void ResourceOwnerRememberCatCacheListRef ( ResourceOwner  owner,
CatCList list 
)

Definition at line 970 of file resowner.c.

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

Referenced by SearchCatCacheList().

971 {
973 }
#define PointerGetDatum(X)
Definition: postgres.h:562
ResourceArray catlistrefarr
Definition: resowner.c:120
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void ResourceOwnerRememberCatCacheRef ( ResourceOwner  owner,
HeapTuple  tuple 
)

Definition at line 935 of file resowner.c.

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

Referenced by SearchCatCacheInternal(), and SearchCatCacheMiss().

936 {
937  ResourceArrayAdd(&(owner->catrefarr), PointerGetDatum(tuple));
938 }
ResourceArray catrefarr
Definition: resowner.c:119
#define PointerGetDatum(X)
Definition: postgres.h:562
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void ResourceOwnerRememberDSM ( ResourceOwner  owner,
dsm_segment seg 
)

Definition at line 1231 of file resowner.c.

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

Referenced by dsm_create_descriptor(), and dsm_unpin_mapping().

1232 {
1233  ResourceArrayAdd(&(owner->dsmarr), PointerGetDatum(seg));
1234 }
#define PointerGetDatum(X)
Definition: postgres.h:562
ResourceArray dsmarr
Definition: resowner.c:126
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void ResourceOwnerRememberFile ( ResourceOwner  owner,
File  file 
)

Definition at line 1186 of file resowner.c.

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

Referenced by OpenTemporaryFile().

1187 {
1188  ResourceArrayAdd(&(owner->filearr), FileGetDatum(file));
1189 }
#define FileGetDatum(file)
Definition: resowner.c:39
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
ResourceArray filearr
Definition: resowner.c:125
void ResourceOwnerRememberLock ( ResourceOwner  owner,
LOCALLOCK locallock 
)

Definition at line 875 of file resowner.c.

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

Referenced by GrantLockLocal(), and LockReassignOwner().

876 {
877  Assert(locallock != NULL);
878 
879  if (owner->nlocks > MAX_RESOWNER_LOCKS)
880  return; /* we have already overflowed */
881 
882  if (owner->nlocks < MAX_RESOWNER_LOCKS)
883  owner->locks[owner->nlocks] = locallock;
884  else
885  {
886  /* overflowed */
887  }
888  owner->nlocks++;
889 }
#define MAX_RESOWNER_LOCKS
Definition: resowner.c:105
#define Assert(condition)
Definition: c.h:681
LOCALLOCK * locks[MAX_RESOWNER_LOCKS]
Definition: resowner.c:130
void ResourceOwnerRememberPlanCacheRef ( ResourceOwner  owner,
CachedPlan plan 
)

Definition at line 1050 of file resowner.c.

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

Referenced by GetCachedPlan().

1051 {
1052  ResourceArrayAdd(&(owner->planrefarr), PointerGetDatum(plan));
1053 }
#define PointerGetDatum(X)
Definition: postgres.h:562
ResourceArray planrefarr
Definition: resowner.c:122
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void ResourceOwnerRememberRelationRef ( ResourceOwner  owner,
Relation  rel 
)

Definition at line 1005 of file resowner.c.

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

Referenced by RelationIncrementReferenceCount().

1006 {
1007  ResourceArrayAdd(&(owner->relrefarr), PointerGetDatum(rel));
1008 }
ResourceArray relrefarr
Definition: resowner.c:121
#define PointerGetDatum(X)
Definition: postgres.h:562
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void ResourceOwnerRememberSnapshot ( ResourceOwner  owner,
Snapshot  snapshot 
)

Definition at line 1140 of file resowner.c.

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

Referenced by RegisterSnapshotOnOwner().

1141 {
1142  ResourceArrayAdd(&(owner->snapshotarr), PointerGetDatum(snapshot));
1143 }
#define PointerGetDatum(X)
Definition: postgres.h:562
ResourceArray snapshotarr
Definition: resowner.c:124
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void ResourceOwnerRememberTupleDesc ( ResourceOwner  owner,
TupleDesc  tupdesc 
)

Definition at line 1094 of file resowner.c.

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

Referenced by IncrTupleDescRefCount().

1095 {
1096  ResourceArrayAdd(&(owner->tupdescarr), PointerGetDatum(tupdesc));
1097 }
#define PointerGetDatum(X)
Definition: postgres.h:562
ResourceArray tupdescarr
Definition: resowner.c:123
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void UnregisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 793 of file resowner.c.

References ResourceReleaseCallbackItem::next, and pfree().

794 {
797 
798  prev = NULL;
799  for (item = ResourceRelease_callbacks; item; prev = item, item = item->next)
800  {
801  if (item->callback == callback && item->arg == arg)
802  {
803  if (prev)
804  prev->next = item->next;
805  else
807  pfree(item);
808  break;
809  }
810  }
811 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:152
void pfree(void *pointer)
Definition: mcxt.c:949
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:147
void * arg

Variable Documentation

ResourceOwner CurrentResourceOwner = NULL

Definition at line 138 of file resowner.c.

Referenced by ApplyWorkerMain(), AssignTransactionId(), AtAbort_ResourceOwner(), AtStart_ResourceOwner(), AtSubAbort_ResourceOwner(), AtSubStart_ResourceOwner(), AutoVacLauncherMain(), BackgroundWriterMain(), BgBufferSync(), BufferSync(), CheckpointerMain(), cipher_free_callback(), CleanupSubTransaction(), CleanupTransaction(), close_lo_relation(), CommitSubTransaction(), CommitTransaction(), DecrTupleDescRefCount(), digest_free_callback(), dsm_create_descriptor(), dsm_unpin_mapping(), exec_init_tuple_store(), exec_stmt_block(), extendBufFile(), FlushDatabaseBuffers(), FlushRelationBuffers(), GetCachedPlan(), GetCurrentFDWTuplestore(), IncrBufferRefCount(), IncrTupleDescRefCount(), LocalBufferAlloc(), lock_and_open_sequence(), LockAcquireExtended(), LockReassignCurrentOwner(), LockReassignOwner(), LockRelease(), makeBufFile(), MakeTransitionCaptureState(), open_lo_relation(), OpenTemporaryFile(), ParallelWorkerMain(), PersistHoldablePortal(), 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(), RegisterSnapshot(), RelationDecrementReferenceCount(), RelationIncrementReferenceCount(), ReleaseAndReadBuffer(), ReleaseBuffer(), ReleaseCachedPlan(), ReleaseCatCache(), ReleaseCatCacheList(), ReleaseLockIfHeld(), ResourceOwnerReleaseInternal(), SearchCatCacheInternal(), SearchCatCacheList(), SearchCatCacheMiss(), SnapBuildClearExportedSnapshot(), SnapBuildExportSnapshot(), test_shm_mq_main(), tuplestore_begin_common(), tuplestore_puttuple_common(), UnpinBuffer(), UnregisterSnapshot(), WalReceiverMain(), WalSndShutdown(), and WalWriterMain().

ResourceReleaseCallbackItem* ResourceRelease_callbacks = NULL
static

Definition at line 152 of file resowner.c.

Referenced by RegisterResourceReleaseCallback().