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

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

Referenced by AuxiliaryProcessMain(), and InitPostgres().

888 {
890  Assert(CurrentResourceOwner == NULL);
891  AuxProcessResourceOwner = ResourceOwnerCreate(NULL, "AuxiliaryProcess");
893 
894  /*
895  * Register a shmem-exit callback for cleanup of aux-process resource
896  * owner. (This needs to run after, e.g., ShutdownXLOG.)
897  */
899 
900 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:149
static void ReleaseAuxProcessResourcesCallback(int code, Datum arg)
Definition: resowner.c:930
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:428

◆ RegisterResourceReleaseCallback()

void RegisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

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

850 {
852 
853  item = (ResourceReleaseCallbackItem *)
856  item->callback = callback;
857  item->arg = arg;
860 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:161
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
MemoryContext TopMemoryContext
Definition: mcxt.c:48
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:156
ResourceReleaseCallback callback
Definition: resowner.c:157
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
void * arg

◆ ReleaseAuxProcessResources()

void ReleaseAuxProcessResources ( bool  isCommit)

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

909 {
910  /*
911  * At this writing, the only thing that could actually get released is
912  * buffer pins; but we may as well do the full release protocol.
913  */
916  isCommit, true);
919  isCommit, true);
922  isCommit, true);
923 }
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:149
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:486

◆ ResourceOwnerCreate()

ResourceOwner ResourceOwnerCreate ( ResourceOwner  parent,
const char *  name 
)

Definition at line 428 of file resowner.c.

References ResourceOwnerData::bufferarr, BufferGetDatum, ResourceOwnerData::catlistrefarr, ResourceOwnerData::catrefarr, ResourceOwnerData::cryptohasharr, ResourceOwnerData::dsmarr, ResourceOwnerData::filearr, FileGetDatum, ResourceOwnerData::firstchild, ResourceOwnerData::hmacarr, 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().

429 {
430  ResourceOwner owner;
431 
433  sizeof(ResourceOwnerData));
434  owner->name = name;
435 
436  if (parent)
437  {
438  owner->parent = parent;
439  owner->nextchild = parent->firstchild;
440  parent->firstchild = owner;
441  }
442 
444  ResourceArrayInit(&(owner->catrefarr), PointerGetDatum(NULL));
446  ResourceArrayInit(&(owner->relrefarr), PointerGetDatum(NULL));
447  ResourceArrayInit(&(owner->planrefarr), PointerGetDatum(NULL));
448  ResourceArrayInit(&(owner->tupdescarr), PointerGetDatum(NULL));
450  ResourceArrayInit(&(owner->filearr), FileGetDatum(-1));
451  ResourceArrayInit(&(owner->dsmarr), PointerGetDatum(NULL));
452  ResourceArrayInit(&(owner->jitarr), PointerGetDatum(NULL));
454  ResourceArrayInit(&(owner->hmacarr), PointerGetDatum(NULL));
455 
456  return owner;
457 }
ResourceArray relrefarr
Definition: resowner.c:126
ResourceArray catrefarr
Definition: resowner.c:124
#define PointerGetDatum(X)
Definition: postgres.h:600
#define InvalidBuffer
Definition: buf.h:25
ResourceArray bufferarr
Definition: resowner.c:123
ResourceArray snapshotarr
Definition: resowner.c:129
struct ResourceOwnerData * ResourceOwner
Definition: resowner.h:27
const char * name
Definition: resowner.c:120
ResourceArray planrefarr
Definition: resowner.c:127
ResourceArray tupdescarr
Definition: resowner.c:128
static void ResourceArrayInit(ResourceArray *resarr, Datum invalidval)
Definition: resowner.c:195
MemoryContext TopMemoryContext
Definition: mcxt.c:48
ResourceOwner firstchild
Definition: resowner.c:118
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
#define BufferGetDatum(buffer)
Definition: resowner.c:46
#define FileGetDatum(file)
Definition: resowner.c:44
ResourceArray jitarr
Definition: resowner.c:132
ResourceOwner parent
Definition: resowner.c:117
const char * name
Definition: encode.c:561
ResourceArray catlistrefarr
Definition: resowner.c:125
ResourceArray dsmarr
Definition: resowner.c:131
ResourceOwner nextchild
Definition: resowner.c:119
ResourceArray hmacarr
Definition: resowner.c:134
ResourceArray filearr
Definition: resowner.c:130
ResourceArray cryptohasharr
Definition: resowner.c:133

◆ ResourceOwnerDelete()

void ResourceOwnerDelete ( ResourceOwner  owner)

Definition at line 737 of file resowner.c.

References Assert, ResourceOwnerData::bufferarr, ResourceOwnerData::catlistrefarr, ResourceOwnerData::catrefarr, ResourceOwnerData::cryptohasharr, ResourceOwnerData::dsmarr, ResourceOwnerData::filearr, ResourceOwnerData::firstchild, ResourceOwnerData::hmacarr, 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().

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

◆ ResourceOwnerGetParent()

ResourceOwner ResourceOwnerGetParent ( ResourceOwner  owner)

Definition at line 792 of file resowner.c.

References ResourceOwnerData::parent.

Referenced by LockReassignCurrentOwner().

793 {
794  return owner->parent;
795 }
ResourceOwner parent
Definition: resowner.c:117

◆ ResourceOwnerNewParent()

void ResourceOwnerNewParent ( ResourceOwner  owner,
ResourceOwner  newparent 
)

Definition at line 801 of file resowner.c.

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

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

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

◆ ResourceOwnerRelease()

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

Definition at line 486 of file resowner.c.

References ResourceOwnerReleaseInternal().

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

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

◆ ResourceOwnerReleaseAllPlanCacheRefs()

void ResourceOwnerReleaseAllPlanCacheRefs ( ResourceOwner  owner)

Definition at line 718 of file resowner.c.

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

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

719 {
720  Datum foundres;
721 
722  while (ResourceArrayGetAny(&(owner->planrefarr), &foundres))
723  {
724  CachedPlan *res = (CachedPlan *) DatumGetPointer(foundres);
725 
726  ReleaseCachedPlan(res, owner);
727  }
728 }
void ReleaseCachedPlan(CachedPlan *plan, ResourceOwner owner)
Definition: plancache.c:1264
static bool ResourceArrayGetAny(ResourceArray *resarr, Datum *value)
Definition: resowner.c:376
ResourceArray planrefarr
Definition: resowner.c:127
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593

◆ UnregisterResourceReleaseCallback()

void UnregisterResourceReleaseCallback ( ResourceReleaseCallback  callback,
void *  arg 
)

Definition at line 863 of file resowner.c.

References ResourceReleaseCallbackItem::next, and pfree().

864 {
867 
868  prev = NULL;
869  for (item = ResourceRelease_callbacks; item; prev = item, item = item->next)
870  {
871  if (item->callback == callback && item->arg == arg)
872  {
873  if (prev)
874  prev->next = item->next;
875  else
877  pfree(item);
878  break;
879  }
880  }
881 }
static ResourceReleaseCallbackItem * ResourceRelease_callbacks
Definition: resowner.c:161
void pfree(void *pointer)
Definition: mcxt.c:1169
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct ResourceReleaseCallbackItem * next
Definition: resowner.c:156
void * arg

Variable Documentation

◆ AuxProcessResourceOwner

PGDLLIMPORT ResourceOwner AuxProcessResourceOwner

◆ CurrentResourceOwner

PGDLLIMPORT ResourceOwner CurrentResourceOwner

Definition at line 146 of file resowner.c.

Referenced by _SPI_execute_plan(), AssignTransactionId(), AtAbort_ResourceOwner(), AtStart_ResourceOwner(), AtSubAbort_ResourceOwner(), AtSubStart_ResourceOwner(), BgBufferSync(), BufferSync(), cipher_free_callback(), CleanupSubTransaction(), CleanupTransaction(), close_lo_relation(), CommitSubTransaction(), CommitTransaction(), 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_hmac_create(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), PinBuffer(), PinBuffer_Locked(), plperl_spi_exec(), plperl_spi_exec_prepared(), plperl_spi_fetchrow(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), plpgsql_estate_setup(), pltcl_func_handler(), pltcl_init_tuple_store(), pltcl_returnnext(), pltcl_SPI_execute(), pltcl_SPI_execute_plan(), pltcl_SPI_prepare(), pltcl_subtrans_abort(), pltcl_subtrans_commit(), pltcl_subtransaction(), PLy_abort_open_subtransactions(), PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_cursor_plan(), PLy_cursor_query(), PLy_spi_execute_plan(), PLy_spi_execute_query(), PLy_spi_prepare(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_commit(), PLy_subtransaction_enter(), PLy_subtransaction_exit(), PopTransaction(), PortalCleanup(), PortalRun(), PortalRunFetch(), PortalStart(), PrepareTransaction(), px_find_cipher(), px_find_digest(), ReadBuffer_common(), ReadRecentBuffer(), RegisterSnapshot(), RegisterTemporaryFile(), RelationDecrementReferenceCount(), RelationIncrementReferenceCount(), ReleaseAndReadBuffer(), ReleaseBuffer(), ReleaseCatCache(), ReleaseCatCacheList(), ReleaseLockIfHeld(), 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