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

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

Referenced by AuxiliaryProcessMain(), and InitPostgres().

872 {
874  Assert(CurrentResourceOwner == NULL);
875  AuxProcessResourceOwner = ResourceOwnerCreate(NULL, "AuxiliaryProcess");
877 
878  /*
879  * Register a shmem-exit callback for cleanup of aux-process resource
880  * owner. (This needs to run after, e.g., ShutdownXLOG.)
881  */
883 
884 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:147
static void ReleaseAuxProcessResourcesCallback(int code, Datum arg)
Definition: resowner.c:914
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define Assert(condition)
Definition: c.h:804
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:425

◆ RegisterResourceReleaseCallback()

void RegisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

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

834 {
836 
837  item = (ResourceReleaseCallbackItem *)
840  item->callback = callback;
841  item->arg = arg;
844 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:159
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:154
ResourceReleaseCallback callback
Definition: resowner.c:155
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
void * arg

◆ ReleaseAuxProcessResources()

void ReleaseAuxProcessResources ( bool  isCommit)

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

893 {
894  /*
895  * At this writing, the only thing that could actually get released is
896  * buffer pins; but we may as well do the full release protocol.
897  */
900  isCommit, true);
903  isCommit, true);
906  isCommit, true);
907 }
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:147
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:482

◆ ResourceOwnerCreate()

ResourceOwner ResourceOwnerCreate ( ResourceOwner  parent,
const char *  name 
)

Definition at line 425 of file resowner.c.

References ResourceOwnerData::bufferarr, BufferGetDatum, ResourceOwnerData::catlistrefarr, ResourceOwnerData::catrefarr, ResourceOwnerData::cryptohasharr, 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_call_handler(), plpgsql_create_econtext(), and plpgsql_inline_handler().

426 {
427  ResourceOwner owner;
428 
430  sizeof(ResourceOwnerData));
431  owner->name = name;
432 
433  if (parent)
434  {
435  owner->parent = parent;
436  owner->nextchild = parent->firstchild;
437  parent->firstchild = owner;
438  }
439 
441  ResourceArrayInit(&(owner->catrefarr), PointerGetDatum(NULL));
443  ResourceArrayInit(&(owner->relrefarr), PointerGetDatum(NULL));
444  ResourceArrayInit(&(owner->planrefarr), PointerGetDatum(NULL));
445  ResourceArrayInit(&(owner->tupdescarr), PointerGetDatum(NULL));
447  ResourceArrayInit(&(owner->filearr), FileGetDatum(-1));
448  ResourceArrayInit(&(owner->dsmarr), PointerGetDatum(NULL));
449  ResourceArrayInit(&(owner->jitarr), PointerGetDatum(NULL));
451 
452  return owner;
453 }
ResourceArray relrefarr
Definition: resowner.c:125
ResourceArray catrefarr
Definition: resowner.c:123
#define PointerGetDatum(X)
Definition: postgres.h:556
#define InvalidBuffer
Definition: buf.h:25
ResourceArray bufferarr
Definition: resowner.c:122
ResourceArray snapshotarr
Definition: resowner.c:128
struct ResourceOwnerData * ResourceOwner
Definition: resowner.h:27
const char * name
Definition: resowner.c:119
ResourceArray planrefarr
Definition: resowner.c:126
ResourceArray tupdescarr
Definition: resowner.c:127
static void ResourceArrayInit(ResourceArray *resarr, Datum invalidval)
Definition: resowner.c:192
MemoryContext TopMemoryContext
Definition: mcxt.c:44
ResourceOwner firstchild
Definition: resowner.c:117
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:840
#define BufferGetDatum(buffer)
Definition: resowner.c:45
#define FileGetDatum(file)
Definition: resowner.c:43
ResourceArray jitarr
Definition: resowner.c:131
ResourceOwner parent
Definition: resowner.c:116
const char * name
Definition: encode.c:515
ResourceArray catlistrefarr
Definition: resowner.c:124
ResourceArray dsmarr
Definition: resowner.c:130
ResourceOwner nextchild
Definition: resowner.c:118
ResourceArray filearr
Definition: resowner.c:129
ResourceArray cryptohasharr
Definition: resowner.c:132

◆ ResourceOwnerDelete()

void ResourceOwnerDelete ( ResourceOwner  owner)

Definition at line 723 of file resowner.c.

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

724 {
725  /* We had better not be deleting CurrentResourceOwner ... */
726  Assert(owner != CurrentResourceOwner);
727 
728  /* And it better not own any resources, either */
729  Assert(owner->bufferarr.nitems == 0);
730  Assert(owner->catrefarr.nitems == 0);
731  Assert(owner->catlistrefarr.nitems == 0);
732  Assert(owner->relrefarr.nitems == 0);
733  Assert(owner->planrefarr.nitems == 0);
734  Assert(owner->tupdescarr.nitems == 0);
735  Assert(owner->snapshotarr.nitems == 0);
736  Assert(owner->filearr.nitems == 0);
737  Assert(owner->dsmarr.nitems == 0);
738  Assert(owner->jitarr.nitems == 0);
739  Assert(owner->cryptohasharr.nitems == 0);
740  Assert(owner->nlocks == 0 || owner->nlocks == MAX_RESOWNER_LOCKS + 1);
741 
742  /*
743  * Delete children. The recursive call will delink the child from me, so
744  * just iterate as long as there is a child.
745  */
746  while (owner->firstchild != NULL)
748 
749  /*
750  * We delink the owner from its parent before deleting it, so that if
751  * there's an error we won't have deleted/busted owners still attached to
752  * the owner tree. Better a leak than a crash.
753  */
754  ResourceOwnerNewParent(owner, NULL);
755 
756  /* And free the object. */
757  ResourceArrayFree(&(owner->bufferarr));
758  ResourceArrayFree(&(owner->catrefarr));
759  ResourceArrayFree(&(owner->catlistrefarr));
760  ResourceArrayFree(&(owner->relrefarr));
761  ResourceArrayFree(&(owner->planrefarr));
762  ResourceArrayFree(&(owner->tupdescarr));
763  ResourceArrayFree(&(owner->snapshotarr));
764  ResourceArrayFree(&(owner->filearr));
765  ResourceArrayFree(&(owner->dsmarr));
766  ResourceArrayFree(&(owner->jitarr));
767  ResourceArrayFree(&(owner->cryptohasharr));
768 
769  pfree(owner);
770 }
ResourceArray relrefarr
Definition: resowner.c:125
ResourceArray catrefarr
Definition: resowner.c:123
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:723
ResourceArray bufferarr
Definition: resowner.c:122
ResourceArray snapshotarr
Definition: resowner.c:128
#define MAX_RESOWNER_LOCKS
Definition: resowner.c:109
void pfree(void *pointer)
Definition: mcxt.c:1057
ResourceArray planrefarr
Definition: resowner.c:126
static void ResourceArrayFree(ResourceArray *resarr)
Definition: resowner.c:405
ResourceArray tupdescarr
Definition: resowner.c:127
ResourceOwner firstchild
Definition: resowner.c:117
#define Assert(condition)
Definition: c.h:804
ResourceArray jitarr
Definition: resowner.c:131
void ResourceOwnerNewParent(ResourceOwner owner, ResourceOwner newparent)
Definition: resowner.c:785
ResourceArray catlistrefarr
Definition: resowner.c:124
ResourceArray dsmarr
Definition: resowner.c:130
uint32 nitems
Definition: resowner.c:69
ResourceArray filearr
Definition: resowner.c:129
ResourceArray cryptohasharr
Definition: resowner.c:132

◆ ResourceOwnerGetParent()

ResourceOwner ResourceOwnerGetParent ( ResourceOwner  owner)

Definition at line 776 of file resowner.c.

References ResourceOwnerData::parent.

Referenced by LockReassignCurrentOwner().

777 {
778  return owner->parent;
779 }
ResourceOwner parent
Definition: resowner.c:116

◆ ResourceOwnerNewParent()

void ResourceOwnerNewParent ( ResourceOwner  owner,
ResourceOwner  newparent 
)

Definition at line 785 of file resowner.c.

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

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

787 {
788  ResourceOwner oldparent = owner->parent;
789 
790  if (oldparent)
791  {
792  if (owner == oldparent->firstchild)
793  oldparent->firstchild = owner->nextchild;
794  else
795  {
796  ResourceOwner child;
797 
798  for (child = oldparent->firstchild; child; child = child->nextchild)
799  {
800  if (owner == child->nextchild)
801  {
802  child->nextchild = owner->nextchild;
803  break;
804  }
805  }
806  }
807  }
808 
809  if (newparent)
810  {
811  Assert(owner != newparent);
812  owner->parent = newparent;
813  owner->nextchild = newparent->firstchild;
814  newparent->firstchild = owner;
815  }
816  else
817  {
818  owner->parent = NULL;
819  owner->nextchild = NULL;
820  }
821 }
ResourceOwner firstchild
Definition: resowner.c:117
#define Assert(condition)
Definition: c.h:804
ResourceOwner parent
Definition: resowner.c:116
ResourceOwner nextchild
Definition: resowner.c:118

◆ ResourceOwnerRelease()

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

Definition at line 482 of file resowner.c.

References ResourceOwnerReleaseInternal().

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

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

◆ ResourceOwnerReleaseAllPlanCacheRefs()

void ResourceOwnerReleaseAllPlanCacheRefs ( ResourceOwner  owner)

Definition at line 704 of file resowner.c.

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

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

705 {
706  Datum foundres;
707 
708  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
709  {
710  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
711 
712  ReleaseCachedPlan(res, owner);
713  }
714 }
void ReleaseCachedPlan(CachedPlan *plan, ResourceOwner owner)
Definition: plancache.c:1264
static bool ResourceArrayGetAny(ResourceArray *resarr, Datum *value)
Definition: resowner.c:373
ResourceArray planrefarr
Definition: resowner.c:126
uintptr_t Datum
Definition: postgres.h:367
#define DatumGetPointer(X)
Definition: postgres.h:549

◆ UnregisterResourceReleaseCallback()

void UnregisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 847 of file resowner.c.

References ResourceReleaseCallbackItem::next, and pfree().

848 {
851 
852  prev = NULL;
853  for (item = ResourceRelease_callbacks; item; prev = item, item = item->next)
854  {
855  if (item->callback == callback && item->arg == arg)
856  {
857  if (prev)
858  prev->next = item->next;
859  else
861  pfree(item);
862  break;
863  }
864  }
865 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:159
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:154
void * arg

Variable Documentation

◆ AuxProcessResourceOwner

PGDLLIMPORT ResourceOwner AuxProcessResourceOwner

◆ CurrentResourceOwner

PGDLLIMPORT ResourceOwner CurrentResourceOwner

Definition at line 144 of file resowner.c.

Referenced by _SPI_execute_plan(), AssignTransactionId(), AtAbort_ResourceOwner(), AtStart_ResourceOwner(), AtSubAbort_ResourceOwner(), AtSubStart_ResourceOwner(), BgBufferSync(), BufferSync(), cipher_free_callback(), CleanupSubTransaction(), CleanupTransaction(), close_lo_relation(), CommitSubTransaction(), CommitTransaction(), DecrTupleDescRefCount(), digest_free_callback(), dsm_create_descriptor(), dsm_unpin_mapping(), exec_eval_simple_expr(), exec_init_tuple_store(), exec_simple_check_plan(), exec_stmt_block(), ExplainExecuteQuery(), extendBufFile(), FlushDatabaseBuffers(), FlushRelationBuffers(), FlushRelationsAllBuffers(), GetCurrentFDWTuplestore(), IncrBufferRefCount(), IncrTupleDescRefCount(), InitPostgres(), llvm_create_context(), LocalBufferAlloc(), lock_and_open_sequence(), LockAcquireExtended(), LockReassignCurrentOwner(), LockReassignOwner(), LockRelease(), makeBufFileCommon(), MakeTransitionCaptureState(), open_lo_relation(), OpenTemporaryFile(), PathNameCreateTemporaryFile(), PathNameOpenTemporaryFile(), perform_base_backup(), PersistHoldablePortal(), pg_cryptohash_create(), pg_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(), ReleaseCatCache(), ReleaseCatCacheList(), ReleaseLockIfHeld(), ResourceOwnerReleaseInternal(), SearchCatCacheInternal(), SearchCatCacheList(), SearchCatCacheMiss(), ShutdownXLOG(), SnapBuildClearExportedSnapshot(), SnapBuildExportSnapshot(), SPI_plan_get_cached_plan(), StartupXLOG(), tuplestore_begin_common(), tuplestore_puttuple_common(), UnpinBuffer(), UnregisterSnapshot(), and WalSndResourceCleanup().

◆ CurTransactionResourceOwner

◆ TopTransactionResourceOwner