PostgreSQL Source Code  git master
resowner.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct ResourceOwnerDataResourceOwner
 
typedef void(* ResourceReleaseCallback) (ResourceReleasePhase phase, bool isCommit, bool isTopLevel, void *arg)
 

Enumerations

enum  ResourceReleasePhase { RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, RESOURCE_RELEASE_AFTER_LOCKS }
 

Functions

ResourceOwner ResourceOwnerCreate (ResourceOwner parent, const char *name)
 
void ResourceOwnerRelease (ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
 
void ResourceOwnerReleaseAllPlanCacheRefs (ResourceOwner owner)
 
void ResourceOwnerDelete (ResourceOwner owner)
 
ResourceOwner ResourceOwnerGetParent (ResourceOwner owner)
 
void ResourceOwnerNewParent (ResourceOwner owner, ResourceOwner newparent)
 
void RegisterResourceReleaseCallback (ResourceReleaseCallback callback, void *arg)
 
void UnregisterResourceReleaseCallback (ResourceReleaseCallback callback, void *arg)
 
void CreateAuxProcessResourceOwner (void)
 
void ReleaseAuxProcessResources (bool isCommit)
 

Variables

PGDLLIMPORT ResourceOwner CurrentResourceOwner
 
PGDLLIMPORT ResourceOwner CurTransactionResourceOwner
 
PGDLLIMPORT ResourceOwner TopTransactionResourceOwner
 
PGDLLIMPORT ResourceOwner AuxProcessResourceOwner
 

Typedef Documentation

◆ ResourceOwner

Definition at line 27 of file resowner.h.

◆ ResourceReleaseCallback

typedef void(* ResourceReleaseCallback) (ResourceReleasePhase phase, bool isCommit, bool isTopLevel, void *arg)

Definition at line 57 of file resowner.h.

Enumeration Type Documentation

◆ ResourceReleasePhase

Enumerator
RESOURCE_RELEASE_BEFORE_LOCKS 
RESOURCE_RELEASE_LOCKS 
RESOURCE_RELEASE_AFTER_LOCKS 

Definition at line 46 of file resowner.h.

Function Documentation

◆ CreateAuxProcessResourceOwner()

void CreateAuxProcessResourceOwner ( void  )

Definition at line 858 of file resowner.c.

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

Referenced by AuxiliaryProcessMain(), and InitPostgres().

859 {
861  Assert(CurrentResourceOwner == NULL);
862  AuxProcessResourceOwner = ResourceOwnerCreate(NULL, "AuxiliaryProcess");
864 
865  /*
866  * Register a shmem-exit callback for cleanup of aux-process resource
867  * owner. (This needs to run after, e.g., ShutdownXLOG.)
868  */
870 
871 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:145
static void ReleaseAuxProcessResourcesCallback(int code, Datum arg)
Definition: resowner.c:901
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define Assert(condition)
Definition: c.h:745
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:422

◆ RegisterResourceReleaseCallback()

void RegisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

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

821 {
823 
824  item = (ResourceReleaseCallbackItem *)
827  item->callback = callback;
828  item->arg = arg;
831 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:157
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
MemoryContext TopMemoryContext
Definition: mcxt.c:44
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:152
ResourceReleaseCallback callback
Definition: resowner.c:153
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
void * arg

◆ ReleaseAuxProcessResources()

void ReleaseAuxProcessResources ( bool  isCommit)

Definition at line 879 of file resowner.c.

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

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

880 {
881  /*
882  * At this writing, the only thing that could actually get released is
883  * buffer pins; but we may as well do the full release protocol.
884  */
887  isCommit, true);
890  isCommit, true);
893  isCommit, true);
894 }
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:145
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478

◆ ResourceOwnerCreate()

ResourceOwner ResourceOwnerCreate ( ResourceOwner  parent,
const char *  name 
)

Definition at line 422 of file resowner.c.

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

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

423 {
424  ResourceOwner owner;
425 
427  sizeof(ResourceOwnerData));
428  owner->name = name;
429 
430  if (parent)
431  {
432  owner->parent = parent;
433  owner->nextchild = parent->firstchild;
434  parent->firstchild = owner;
435  }
436 
438  ResourceArrayInit(&(owner->catrefarr), PointerGetDatum(NULL));
440  ResourceArrayInit(&(owner->relrefarr), PointerGetDatum(NULL));
441  ResourceArrayInit(&(owner->planrefarr), PointerGetDatum(NULL));
442  ResourceArrayInit(&(owner->tupdescarr), PointerGetDatum(NULL));
444  ResourceArrayInit(&(owner->filearr), FileGetDatum(-1));
445  ResourceArrayInit(&(owner->dsmarr), PointerGetDatum(NULL));
446  ResourceArrayInit(&(owner->jitarr), PointerGetDatum(NULL));
447 
448  return owner;
449 }
ResourceArray relrefarr
Definition: resowner.c:124
ResourceArray catrefarr
Definition: resowner.c:122
#define PointerGetDatum(X)
Definition: postgres.h:556
#define InvalidBuffer
Definition: buf.h:25
ResourceArray bufferarr
Definition: resowner.c:121
ResourceArray snapshotarr
Definition: resowner.c:127
struct ResourceOwnerData * ResourceOwner
Definition: resowner.h:27
const char * name
Definition: resowner.c:118
ResourceArray planrefarr
Definition: resowner.c:125
ResourceArray tupdescarr
Definition: resowner.c:126
static void ResourceArrayInit(ResourceArray *resarr, Datum invalidval)
Definition: resowner.c:189
MemoryContext TopMemoryContext
Definition: mcxt.c:44
ResourceOwner firstchild
Definition: resowner.c:116
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:840
#define BufferGetDatum(buffer)
Definition: resowner.c:44
#define FileGetDatum(file)
Definition: resowner.c:42
ResourceArray jitarr
Definition: resowner.c:130
ResourceOwner parent
Definition: resowner.c:115
const char * name
Definition: encode.c:561
ResourceArray catlistrefarr
Definition: resowner.c:123
ResourceArray dsmarr
Definition: resowner.c:129
ResourceOwner nextchild
Definition: resowner.c:117
ResourceArray filearr
Definition: resowner.c:128

◆ ResourceOwnerDelete()

void ResourceOwnerDelete ( ResourceOwner  owner)

Definition at line 712 of file resowner.c.

References Assert, ResourceOwnerData::bufferarr, ResourceOwnerData::catlistrefarr, ResourceOwnerData::catrefarr, ResourceOwnerData::dsmarr, ResourceOwnerData::filearr, ResourceOwnerData::firstchild, ResourceOwnerData::jitarr, 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(), plpgsql_inline_handler(), PortalDrop(), PrepareTransaction(), ResourceOwnerDelete(), and WalSndResourceCleanup().

713 {
714  /* We had better not be deleting CurrentResourceOwner ... */
715  Assert(owner != CurrentResourceOwner);
716 
717  /* And it better not own any resources, either */
718  Assert(owner->bufferarr.nitems == 0);
719  Assert(owner->catrefarr.nitems == 0);
720  Assert(owner->catlistrefarr.nitems == 0);
721  Assert(owner->relrefarr.nitems == 0);
722  Assert(owner->planrefarr.nitems == 0);
723  Assert(owner->tupdescarr.nitems == 0);
724  Assert(owner->snapshotarr.nitems == 0);
725  Assert(owner->filearr.nitems == 0);
726  Assert(owner->dsmarr.nitems == 0);
727  Assert(owner->jitarr.nitems == 0);
728  Assert(owner->nlocks == 0 || owner->nlocks == MAX_RESOWNER_LOCKS + 1);
729 
730  /*
731  * Delete children. The recursive call will delink the child from me, so
732  * just iterate as long as there is a child.
733  */
734  while (owner->firstchild != NULL)
736 
737  /*
738  * We delink the owner from its parent before deleting it, so that if
739  * there's an error we won't have deleted/busted owners still attached to
740  * the owner tree. Better a leak than a crash.
741  */
742  ResourceOwnerNewParent(owner, NULL);
743 
744  /* And free the object. */
745  ResourceArrayFree(&(owner->bufferarr));
746  ResourceArrayFree(&(owner->catrefarr));
747  ResourceArrayFree(&(owner->catlistrefarr));
748  ResourceArrayFree(&(owner->relrefarr));
749  ResourceArrayFree(&(owner->planrefarr));
750  ResourceArrayFree(&(owner->tupdescarr));
751  ResourceArrayFree(&(owner->snapshotarr));
752  ResourceArrayFree(&(owner->filearr));
753  ResourceArrayFree(&(owner->dsmarr));
754  ResourceArrayFree(&(owner->jitarr));
755 
756  pfree(owner);
757 }
ResourceArray relrefarr
Definition: resowner.c:124
ResourceArray catrefarr
Definition: resowner.c:122
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
ResourceArray bufferarr
Definition: resowner.c:121
ResourceArray snapshotarr
Definition: resowner.c:127
#define MAX_RESOWNER_LOCKS
Definition: resowner.c:108
void pfree(void *pointer)
Definition: mcxt.c:1057
ResourceArray planrefarr
Definition: resowner.c:125
static void ResourceArrayFree(ResourceArray *resarr)
Definition: resowner.c:402
ResourceArray tupdescarr
Definition: resowner.c:126
ResourceOwner firstchild
Definition: resowner.c:116
#define Assert(condition)
Definition: c.h:745
ResourceArray jitarr
Definition: resowner.c:130
void ResourceOwnerNewParent(ResourceOwner owner, ResourceOwner newparent)
Definition: resowner.c:772
ResourceArray catlistrefarr
Definition: resowner.c:123
ResourceArray dsmarr
Definition: resowner.c:129
uint32 nitems
Definition: resowner.c:68
ResourceArray filearr
Definition: resowner.c:128

◆ ResourceOwnerGetParent()

ResourceOwner ResourceOwnerGetParent ( ResourceOwner  owner)

Definition at line 763 of file resowner.c.

References ResourceOwnerData::parent.

Referenced by LockReassignCurrentOwner().

764 {
765  return owner->parent;
766 }
ResourceOwner parent
Definition: resowner.c:115

◆ ResourceOwnerNewParent()

void ResourceOwnerNewParent ( ResourceOwner  owner,
ResourceOwner  newparent 
)

Definition at line 772 of file resowner.c.

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

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

774 {
775  ResourceOwner oldparent = owner->parent;
776 
777  if (oldparent)
778  {
779  if (owner == oldparent->firstchild)
780  oldparent->firstchild = owner->nextchild;
781  else
782  {
783  ResourceOwner child;
784 
785  for (child = oldparent->firstchild; child; child = child->nextchild)
786  {
787  if (owner == child->nextchild)
788  {
789  child->nextchild = owner->nextchild;
790  break;
791  }
792  }
793  }
794  }
795 
796  if (newparent)
797  {
798  Assert(owner != newparent);
799  owner->parent = newparent;
800  owner->nextchild = newparent->firstchild;
801  newparent->firstchild = owner;
802  }
803  else
804  {
805  owner->parent = NULL;
806  owner->nextchild = NULL;
807  }
808 }
ResourceOwner firstchild
Definition: resowner.c:116
#define Assert(condition)
Definition: c.h:745
ResourceOwner parent
Definition: resowner.c:115
ResourceOwner nextchild
Definition: resowner.c:117

◆ ResourceOwnerRelease()

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

Definition at line 478 of file resowner.c.

References ResourceOwnerReleaseInternal().

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

482 {
483  /* There's not currently any setup needed before recursing */
484  ResourceOwnerReleaseInternal(owner, phase, isCommit, isTopLevel);
485 }
static void ResourceOwnerReleaseInternal(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:488

◆ ResourceOwnerReleaseAllPlanCacheRefs()

void ResourceOwnerReleaseAllPlanCacheRefs ( ResourceOwner  owner)

Definition at line 689 of file resowner.c.

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

Referenced by plpgsql_inline_handler(), and plpgsql_xact_cb().

690 {
691  ResourceOwner save;
692  Datum foundres;
693 
694  save = CurrentResourceOwner;
695  CurrentResourceOwner = owner;
696  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
697  {
698  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
699 
700  ReleaseCachedPlan(res, true);
701  }
702  CurrentResourceOwner = save;
703 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
static bool ResourceArrayGetAny(ResourceArray *resarr, Datum *value)
Definition: resowner.c:370
ResourceArray planrefarr
Definition: resowner.c:125
void ReleaseCachedPlan(CachedPlan *plan, bool useResOwner)
Definition: plancache.c:1264
uintptr_t Datum
Definition: postgres.h:367
#define DatumGetPointer(X)
Definition: postgres.h:549

◆ UnregisterResourceReleaseCallback()

void UnregisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 834 of file resowner.c.

References ResourceReleaseCallbackItem::next, and pfree().

835 {
838 
839  prev = NULL;
840  for (item = ResourceRelease_callbacks; item; prev = item, item = item->next)
841  {
842  if (item->callback == callback && item->arg == arg)
843  {
844  if (prev)
845  prev->next = item->next;
846  else
848  pfree(item);
849  break;
850  }
851  }
852 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:157
void pfree(void *pointer)
Definition: mcxt.c:1057
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:152
void * arg

Variable Documentation

◆ AuxProcessResourceOwner

PGDLLIMPORT ResourceOwner AuxProcessResourceOwner

◆ CurrentResourceOwner

PGDLLIMPORT ResourceOwner CurrentResourceOwner

Definition at line 142 of file resowner.c.

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

◆ CurTransactionResourceOwner

◆ TopTransactionResourceOwner