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

References dsm_segment_handle(), elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1266 {
1267  elog(WARNING, "dynamic shared memory leak: segment %u still referenced",
1268  dsm_segment_handle(seg));
1269 }
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1016
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
static void PrintFileLeakWarning ( File  file)
static

Definition at line 1220 of file resowner.c.

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1221 {
1222  elog(WARNING, "temporary file leak: File %d still referenced",
1223  file);
1224 }
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
static void PrintPlanCacheLeakWarning ( CachedPlan plan)
static

Definition at line 1084 of file resowner.c.

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

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

Definition at line 1039 of file resowner.c.

References elog, RelationGetRelationName, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

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

Definition at line 1174 of file resowner.c.

References elog, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

1175 {
1176  elog(WARNING, "Snapshot reference leak: Snapshot %p still referenced",
1177  snapshot);
1178 }
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
static void PrintTupleDescLeakWarning ( TupleDesc  tupdesc)
static

Definition at line 1128 of file resowner.c.

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

Referenced by ResourceOwnerReleaseInternal().

1129 {
1130  elog(WARNING,
1131  "TupleDesc reference leak: TupleDesc %p (%u,%d) still referenced",
1132  tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
1133 }
Oid tdtypeid
Definition: tupdesc.h:77
int32 tdtypmod
Definition: tupdesc.h:78
#define WARNING
Definition: elog.h:40
#define elog
Definition: elog.h:219
void RegisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 793 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().

794 {
796 
797  item = (ResourceReleaseCallbackItem *)
800  item->callback = callback;
801  item->arg = arg;
804 }
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:707
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
static struct @76 value
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
unsigned int uint32
Definition: c.h:268
uint32 maxitems
Definition: resowner.c:66
#define Assert(condition)
Definition: c.h:675
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:307
#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, NULL, 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:950
Datum * itemsarr
Definition: resowner.c:62
Datum invalidval
Definition: resowner.c:63
unsigned int uint32
Definition: c.h:268
MemoryContext TopMemoryContext
Definition: mcxt.c:43
uint32 maxitems
Definition: resowner.c:66
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
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:950
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 }
static struct @76 value
uint32 lastidx
Definition: resowner.c:67
uint32 capacity
Definition: resowner.c:64
Datum * itemsarr
Definition: resowner.c:62
Datum invalidval
Definition: resowner.c:63
unsigned int uint32
Definition: c.h:268
#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, ResourceArray::nitems, and NULL.

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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
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
static struct @76 value
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
unsigned int uint32
Definition: c.h:268
#define Assert(condition)
Definition: c.h:675
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:307
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, NULL, 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 
438  ResourceArrayInit(&(owner->filearr), FileGetDatum(-1));
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:742
#define BufferGetDatum(buffer)
Definition: resowner.c:41
#define FileGetDatum(file)
Definition: resowner.c:39
#define NULL
Definition: c.h:229
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 687 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, NULL, pfree(), ResourceOwnerData::planrefarr, ResourceOwnerData::relrefarr, ResourceArrayFree(), ResourceOwnerDelete(), ResourceOwnerNewParent(), ResourceOwnerData::snapshotarr, and ResourceOwnerData::tupdescarr.

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

688 {
689  /* We had better not be deleting CurrentResourceOwner ... */
690  Assert(owner != CurrentResourceOwner);
691 
692  /* And it better not own any resources, either */
693  Assert(owner->bufferarr.nitems == 0);
694  Assert(owner->catrefarr.nitems == 0);
695  Assert(owner->catlistrefarr.nitems == 0);
696  Assert(owner->relrefarr.nitems == 0);
697  Assert(owner->planrefarr.nitems == 0);
698  Assert(owner->tupdescarr.nitems == 0);
699  Assert(owner->snapshotarr.nitems == 0);
700  Assert(owner->filearr.nitems == 0);
701  Assert(owner->dsmarr.nitems == 0);
702  Assert(owner->nlocks == 0 || owner->nlocks == MAX_RESOWNER_LOCKS + 1);
703 
704  /*
705  * Delete children. The recursive call will delink the child from me, so
706  * just iterate as long as there is a child.
707  */
708  while (owner->firstchild != NULL)
710 
711  /*
712  * We delink the owner from its parent before deleting it, so that if
713  * there's an error we won't have deleted/busted owners still attached to
714  * the owner tree. Better a leak than a crash.
715  */
717 
718  /* And free the object. */
719  ResourceArrayFree(&(owner->bufferarr));
720  ResourceArrayFree(&(owner->catrefarr));
721  ResourceArrayFree(&(owner->catlistrefarr));
722  ResourceArrayFree(&(owner->relrefarr));
723  ResourceArrayFree(&(owner->planrefarr));
724  ResourceArrayFree(&(owner->tupdescarr));
725  ResourceArrayFree(&(owner->snapshotarr));
726  ResourceArrayFree(&(owner->filearr));
727  ResourceArrayFree(&(owner->dsmarr));
728 
729  pfree(owner);
730 }
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:687
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:950
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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void ResourceOwnerNewParent(ResourceOwner owner, ResourceOwner newparent)
Definition: resowner.c:745
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 839 of file resowner.c.

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

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

840 {
841  if (owner == NULL)
842  return;
843  ResourceArrayEnlarge(&(owner->bufferarr));
844 }
static void ResourceArrayEnlarge(ResourceArray *resarr)
Definition: resowner.c:202
ResourceArray bufferarr
Definition: resowner.c:118
#define NULL
Definition: c.h:229
void ResourceOwnerEnlargeCatCacheListRefs ( ResourceOwner  owner)

Definition at line 973 of file resowner.c.

References ResourceOwnerData::catlistrefarr, and ResourceArrayEnlarge().

Referenced by SearchCatCacheList().

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

Definition at line 938 of file resowner.c.

References ResourceOwnerData::catrefarr, and ResourceArrayEnlarge().

Referenced by SearchCatCache().

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

Definition at line 1234 of file resowner.c.

References ResourceOwnerData::dsmarr, and ResourceArrayEnlarge().

Referenced by dsm_create_descriptor(), and dsm_unpin_mapping().

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

Definition at line 1189 of file resowner.c.

References ResourceOwnerData::filearr, and ResourceArrayEnlarge().

Referenced by OpenTemporaryFile().

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

Definition at line 1053 of file resowner.c.

References ResourceOwnerData::planrefarr, and ResourceArrayEnlarge().

Referenced by GetCachedPlan().

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

Definition at line 1008 of file resowner.c.

References ResourceOwnerData::relrefarr, and ResourceArrayEnlarge().

Referenced by RelationIncrementReferenceCount().

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

Definition at line 1143 of file resowner.c.

References ResourceArrayEnlarge(), and ResourceOwnerData::snapshotarr.

Referenced by RegisterSnapshotOnOwner().

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

Definition at line 1097 of file resowner.c.

References ResourceArrayEnlarge(), and ResourceOwnerData::tupdescarr.

Referenced by IncrTupleDescRefCount().

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

Definition at line 869 of file resowner.c.

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

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

870 {
871  if (owner == NULL)
872  return;
873  if (!ResourceArrayRemove(&(owner->bufferarr), BufferGetDatum(buffer)))
874  elog(ERROR, "buffer %d is not owned by resource owner %s",
875  buffer, owner->name);
876 }
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
#define NULL
Definition: c.h:229
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 993 of file resowner.c.

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

Referenced by ReleaseCatCacheList().

994 {
995  if (!ResourceArrayRemove(&(owner->catlistrefarr), PointerGetDatum(list)))
996  elog(ERROR, "catcache list reference %p is not owned by resource owner %s",
997  list, owner->name);
998 }
#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 958 of file resowner.c.

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

Referenced by ReleaseCatCache().

959 {
960  if (!ResourceArrayRemove(&(owner->catrefarr), PointerGetDatum(tuple)))
961  elog(ERROR, "catcache reference %p is not owned by resource owner %s",
962  tuple, owner->name);
963 }
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 1254 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().

1255 {
1256  if (!ResourceArrayRemove(&(owner->dsmarr), PointerGetDatum(seg)))
1257  elog(ERROR, "dynamic shared memory segment %u is not owned by resource owner %s",
1258  dsm_segment_handle(seg), owner->name);
1259 }
#define PointerGetDatum(X)
Definition: postgres.h:562
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1016
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 1209 of file resowner.c.

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

Referenced by FileClose().

1210 {
1211  if (!ResourceArrayRemove(&(owner->filearr), FileGetDatum(file)))
1212  elog(ERROR, "temporary file %d is not owned by resource owner %s",
1213  file, owner->name);
1214 }
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 909 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().

910 {
911  int i;
912 
913  if (owner->nlocks > MAX_RESOWNER_LOCKS)
914  return; /* we have overflowed */
915 
916  Assert(owner->nlocks > 0);
917  for (i = owner->nlocks - 1; i >= 0; i--)
918  {
919  if (locallock == owner->locks[i])
920  {
921  owner->locks[i] = owner->locks[owner->nlocks - 1];
922  owner->nlocks--;
923  return;
924  }
925  }
926  elog(ERROR, "lock reference %p is not owned by resource owner %s",
927  locallock, owner->name);
928 }
#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:675
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 1073 of file resowner.c.

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

Referenced by ReleaseCachedPlan().

1074 {
1075  if (!ResourceArrayRemove(&(owner->planrefarr), PointerGetDatum(plan)))
1076  elog(ERROR, "plancache reference %p is not owned by resource owner %s",
1077  plan, owner->name);
1078 }
#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 1028 of file resowner.c.

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

Referenced by RelationDecrementReferenceCount().

1029 {
1030  if (!ResourceArrayRemove(&(owner->relrefarr), PointerGetDatum(rel)))
1031  elog(ERROR, "relcache reference %s is not owned by resource owner %s",
1032  RelationGetRelationName(rel), owner->name);
1033 }
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:433
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 1163 of file resowner.c.

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

Referenced by UnregisterSnapshotFromOwner().

1164 {
1165  if (!ResourceArrayRemove(&(owner->snapshotarr), PointerGetDatum(snapshot)))
1166  elog(ERROR, "snapshot reference %p is not owned by resource owner %s",
1167  snapshot, owner->name);
1168 }
#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 1117 of file resowner.c.

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

Referenced by DecrTupleDescRefCount().

1118 {
1119  if (!ResourceArrayRemove(&(owner->tupdescarr), PointerGetDatum(tupdesc)))
1120  elog(ERROR, "tupdesc reference %p is not owned by resource owner %s",
1121  tupdesc, owner->name);
1122 }
#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 736 of file resowner.c.

References ResourceOwnerData::parent.

Referenced by LockReassignCurrentOwner().

737 {
738  return owner->parent;
739 }
ResourceOwner parent
Definition: resowner.c:112
void ResourceOwnerNewParent ( ResourceOwner  owner,
ResourceOwner  newparent 
)

Definition at line 745 of file resowner.c.

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

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

747 {
748  ResourceOwner oldparent = owner->parent;
749 
750  if (oldparent)
751  {
752  if (owner == oldparent->firstchild)
753  oldparent->firstchild = owner->nextchild;
754  else
755  {
756  ResourceOwner child;
757 
758  for (child = oldparent->firstchild; child; child = child->nextchild)
759  {
760  if (owner == child->nextchild)
761  {
762  child->nextchild = owner->nextchild;
763  break;
764  }
765  }
766  }
767  }
768 
769  if (newparent)
770  {
771  Assert(owner != newparent);
772  owner->parent = newparent;
773  owner->nextchild = newparent->firstchild;
774  newparent->firstchild = owner;
775  }
776  else
777  {
778  owner->parent = NULL;
779  owner->nextchild = NULL;
780  }
781 }
ResourceOwner firstchild
Definition: resowner.c:113
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
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 CurrentResourceOwner, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, and ResourceOwnerReleaseInternal().

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

475 {
476  /* Rather than PG_TRY at every level of recursion, set it up once */
477  ResourceOwner save;
478 
479  save = CurrentResourceOwner;
480  PG_TRY();
481  {
482  ResourceOwnerReleaseInternal(owner, phase, isCommit, isTopLevel);
483  }
484  PG_CATCH();
485  {
486  CurrentResourceOwner = save;
487  PG_RE_THROW();
488  }
489  PG_END_TRY();
490  CurrentResourceOwner = save;
491 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static void ResourceOwnerReleaseInternal(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:494
#define PG_CATCH()
Definition: elog.h:293
#define PG_RE_THROW()
Definition: elog.h:314
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
static void ResourceOwnerReleaseInternal ( ResourceOwner  owner,
ResourceReleasePhase  phase,
bool  isCommit,
bool  isTopLevel 
)
static

Definition at line 494 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, NULL, 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().

498 {
499  ResourceOwner child;
500  ResourceOwner save;
502  Datum foundres;
503 
504  /* Recurse to handle descendants */
505  for (child = owner->firstchild; child != NULL; child = child->nextchild)
506  ResourceOwnerReleaseInternal(child, phase, isCommit, isTopLevel);
507 
508  /*
509  * Make CurrentResourceOwner point to me, so that ReleaseBuffer etc don't
510  * get confused. We needn't PG_TRY here because the outermost level will
511  * fix it on error abort.
512  */
513  save = CurrentResourceOwner;
514  CurrentResourceOwner = owner;
515 
516  if (phase == RESOURCE_RELEASE_BEFORE_LOCKS)
517  {
518  /*
519  * Release buffer pins. Note that ReleaseBuffer will remove the
520  * buffer entry from our array, so we just have to iterate till there
521  * are none.
522  *
523  * During a commit, there shouldn't be any remaining pins --- that
524  * would indicate failure to clean up the executor correctly --- so
525  * issue warnings. In the abort case, just clean up quietly.
526  */
527  while (ResourceArrayGetAny(&(owner->bufferarr), &foundres))
528  {
529  Buffer res = DatumGetBuffer(foundres);
530 
531  if (isCommit)
533  ReleaseBuffer(res);
534  }
535 
536  /* Ditto for relcache references */
537  while (ResourceArrayGetAny(&(owner->relrefarr), &foundres))
538  {
539  Relation res = (Relation) DatumGetPointer(foundres);
540 
541  if (isCommit)
543  RelationClose(res);
544  }
545 
546  /* Ditto for dynamic shared memory segments */
547  while (ResourceArrayGetAny(&(owner->dsmarr), &foundres))
548  {
549  dsm_segment *res = (dsm_segment *) DatumGetPointer(foundres);
550 
551  if (isCommit)
552  PrintDSMLeakWarning(res);
553  dsm_detach(res);
554  }
555  }
556  else if (phase == RESOURCE_RELEASE_LOCKS)
557  {
558  if (isTopLevel)
559  {
560  /*
561  * For a top-level xact we are going to release all locks (or at
562  * least all non-session locks), so just do a single lmgr call at
563  * the top of the recursion.
564  */
565  if (owner == TopTransactionResourceOwner)
566  {
567  ProcReleaseLocks(isCommit);
568  ReleasePredicateLocks(isCommit);
569  }
570  }
571  else
572  {
573  /*
574  * Release locks retail. Note that if we are committing a
575  * subtransaction, we do NOT release its locks yet, but transfer
576  * them to the parent.
577  */
578  LOCALLOCK **locks;
579  int nlocks;
580 
581  Assert(owner->parent != NULL);
582 
583  /*
584  * Pass the list of locks owned by this resource owner to the lock
585  * manager, unless it has overflowed.
586  */
587  if (owner->nlocks > MAX_RESOWNER_LOCKS)
588  {
589  locks = NULL;
590  nlocks = 0;
591  }
592  else
593  {
594  locks = owner->locks;
595  nlocks = owner->nlocks;
596  }
597 
598  if (isCommit)
599  LockReassignCurrentOwner(locks, nlocks);
600  else
601  LockReleaseCurrentOwner(locks, nlocks);
602  }
603  }
604  else if (phase == RESOURCE_RELEASE_AFTER_LOCKS)
605  {
606  /*
607  * Release catcache references. Note that ReleaseCatCache will remove
608  * the catref entry from our array, so we just have to iterate till
609  * there are none.
610  *
611  * As with buffer pins, warn if any are left at commit time.
612  */
613  while (ResourceArrayGetAny(&(owner->catrefarr), &foundres))
614  {
615  HeapTuple res = (HeapTuple) DatumGetPointer(foundres);
616 
617  if (isCommit)
619  ReleaseCatCache(res);
620  }
621 
622  /* Ditto for catcache lists */
623  while (ResourceArrayGetAny(&(owner->catlistrefarr), &foundres))
624  {
625  CatCList *res = (CatCList *) DatumGetPointer(foundres);
626 
627  if (isCommit)
629  ReleaseCatCacheList(res);
630  }
631 
632  /* Ditto for plancache references */
633  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
634  {
635  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
636 
637  if (isCommit)
639  ReleaseCachedPlan(res, true);
640  }
641 
642  /* Ditto for tupdesc references */
643  while (ResourceArrayGetAny(&(owner->tupdescarr), &foundres))
644  {
645  TupleDesc res = (TupleDesc) DatumGetPointer(foundres);
646 
647  if (isCommit)
650  }
651 
652  /* Ditto for snapshot references */
653  while (ResourceArrayGetAny(&(owner->snapshotarr), &foundres))
654  {
655  Snapshot res = (Snapshot) DatumGetPointer(foundres);
656 
657  if (isCommit)
659  UnregisterSnapshot(res);
660  }
661 
662  /* Ditto for temporary files */
663  while (ResourceArrayGetAny(&(owner->filearr), &foundres))
664  {
665  File res = DatumGetFile(foundres);
666 
667  if (isCommit)
669  FileClose(res);
670  }
671  }
672 
673  /* Let add-on modules get a chance too */
674  for (item = ResourceRelease_callbacks; item; item = item->next)
675  (*item->callback) (phase, isCommit, isTopLevel, item->arg);
676 
677  CurrentResourceOwner = save;
678 }
ResourceArray relrefarr
Definition: resowner.c:121
void PrintCatCacheListLeakWarning(CatCList *list)
Definition: catcache.c:1926
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:494
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:152
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:1665
#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:1174
ResourceArray planrefarr
Definition: resowner.c:122
void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2413
static void PrintPlanCacheLeakWarning(CachedPlan *plan)
Definition: resowner.c:1084
void ReleaseCachedPlan(CachedPlan *plan, bool useResOwner)
Definition: plancache.c:1252
ResourceArray tupdescarr
Definition: resowner.c:123
void ProcReleaseLocks(bool isCommit)
Definition: proc.c:753
void RelationClose(Relation relation)
Definition: relcache.c:2155
struct tupleDesc * TupleDesc
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:900
static void PrintRelCacheLeakWarning(Relation rel)
Definition: resowner.c:1039
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:1910
static void PrintTupleDescLeakWarning(TupleDesc tupdesc)
Definition: resowner.c:1128
void ReleaseCatCache(HeapTuple tuple)
Definition: catcache.c:1317
void ReleasePredicateLocks(bool isCommit)
Definition: predicate.c:3185
void FileClose(File file)
Definition: fd.c:1456
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:334
static void PrintFileLeakWarning(File file)
Definition: resowner.c:1220
ResourceOwner parent
Definition: resowner.c:112
#define DatumGetPointer(X)
Definition: postgres.h:555
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:714
#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:1265
int Buffer
Definition: buf.h:23
int File
Definition: fd.h:51
ResourceArray filearr
Definition: resowner.c:125
void ResourceOwnerRememberBuffer ( ResourceOwner  owner,
Buffer  buffer 
)

Definition at line 855 of file resowner.c.

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

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

856 {
857  if (owner == NULL)
858  return;
859  ResourceArrayAdd(&(owner->bufferarr), BufferGetDatum(buffer));
860 }
ResourceArray bufferarr
Definition: resowner.c:118
#define BufferGetDatum(buffer)
Definition: resowner.c:41
#define NULL
Definition: c.h:229
static void ResourceArrayAdd(ResourceArray *resarr, Datum value)
Definition: resowner.c:257
void ResourceOwnerRememberCatCacheListRef ( ResourceOwner  owner,
CatCList list 
)

Definition at line 984 of file resowner.c.

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

Referenced by SearchCatCacheList().

985 {
987 }
#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 949 of file resowner.c.

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

Referenced by SearchCatCache().

950 {
951  ResourceArrayAdd(&(owner->catrefarr), PointerGetDatum(tuple));
952 }
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 1245 of file resowner.c.

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

Referenced by dsm_create_descriptor(), and dsm_unpin_mapping().

1246 {
1247  ResourceArrayAdd(&(owner->dsmarr), PointerGetDatum(seg));
1248 }
#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 1200 of file resowner.c.

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

Referenced by OpenTemporaryFile().

1201 {
1202  ResourceArrayAdd(&(owner->filearr), FileGetDatum(file));
1203 }
#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 889 of file resowner.c.

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

Referenced by GrantLockLocal(), and LockReassignOwner().

890 {
891  Assert(locallock != NULL);
892 
893  if (owner->nlocks > MAX_RESOWNER_LOCKS)
894  return; /* we have already overflowed */
895 
896  if (owner->nlocks < MAX_RESOWNER_LOCKS)
897  owner->locks[owner->nlocks] = locallock;
898  else
899  {
900  /* overflowed */
901  }
902  owner->nlocks++;
903 }
#define MAX_RESOWNER_LOCKS
Definition: resowner.c:105
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
LOCALLOCK * locks[MAX_RESOWNER_LOCKS]
Definition: resowner.c:130
void ResourceOwnerRememberPlanCacheRef ( ResourceOwner  owner,
CachedPlan plan 
)

Definition at line 1064 of file resowner.c.

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

Referenced by GetCachedPlan().

1065 {
1066  ResourceArrayAdd(&(owner->planrefarr), PointerGetDatum(plan));
1067 }
#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 1019 of file resowner.c.

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

Referenced by RelationIncrementReferenceCount().

1020 {
1021  ResourceArrayAdd(&(owner->relrefarr), PointerGetDatum(rel));
1022 }
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 1154 of file resowner.c.

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

Referenced by RegisterSnapshotOnOwner().

1155 {
1156  ResourceArrayAdd(&(owner->snapshotarr), PointerGetDatum(snapshot));
1157 }
#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 1108 of file resowner.c.

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

Referenced by IncrTupleDescRefCount().

1109 {
1110  ResourceArrayAdd(&(owner->tupdescarr), PointerGetDatum(tupdesc));
1111 }
#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 807 of file resowner.c.

References ResourceReleaseCallbackItem::next, NULL, and pfree().

808 {
811 
812  prev = NULL;
813  for (item = ResourceRelease_callbacks; item; prev = item, item = item->next)
814  {
815  if (item->callback == callback && item->arg == arg)
816  {
817  if (prev)
818  prev->next = item->next;
819  else
821  pfree(item);
822  break;
823  }
824  }
825 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:152
void pfree(void *pointer)
Definition: mcxt.c:950
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:147
#define NULL
Definition: c.h:229
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(), 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(), GetTriggerTransitionTuplestore(), IncrBufferRefCount(), IncrTupleDescRefCount(), LocalBufferAlloc(), LockAcquireExtended(), LockReassignCurrentOwner(), LockReassignOwner(), LockRelease(), makeBufFile(), open_lo_relation(), open_share_lock(), 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(), ResourceOwnerRelease(), ResourceOwnerReleaseInternal(), SearchCatCache(), SearchCatCacheList(), 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().