PostgreSQL Source Code  git master
resowner.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * resowner.h
4  * POSTGRES resource owner definitions.
5  *
6  * Query-lifespan resources are tracked by associating them with
7  * ResourceOwner objects. This provides a simple mechanism for ensuring
8  * that such resources are freed at the right time.
9  * See utils/resowner/README for more info.
10  *
11  *
12  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * src/include/utils/resowner.h
16  *
17  *-------------------------------------------------------------------------
18  */
19 #ifndef RESOWNER_H
20 #define RESOWNER_H
21 
22 
23 /*
24  * ResourceOwner objects are an opaque data structure known only within
25  * resowner.c.
26  */
28 
29 
30 /*
31  * Globally known ResourceOwners
32  */
37 
38 /*
39  * Resource releasing is done in three phases: pre-locks, locks, and
40  * post-locks. The pre-lock phase must release any resources that are visible
41  * to other backends (such as pinned buffers); this ensures that when we
42  * release a lock that another backend may be waiting on, it will see us as
43  * being fully out of our transaction. The post-lock phase should be used for
44  * backend-internal cleanup.
45  *
46  * Within each phase, resources are released in priority order. Priority is
47  * just an integer specified in ResourceOwnerDesc. The priorities of built-in
48  * resource types are given below, extensions may use any priority relative to
49  * those or RELEASE_PRIO_FIRST/LAST. RELEASE_PRIO_FIRST is a fine choice if
50  * your resource doesn't depend on any other resources.
51  */
52 typedef enum
53 {
58 
60 
61 /* priorities of built-in BEFORE_LOCKS resources */
62 #define RELEASE_PRIO_BUFFER_IOS 100
63 #define RELEASE_PRIO_BUFFER_PINS 200
64 #define RELEASE_PRIO_RELCACHE_REFS 300
65 #define RELEASE_PRIO_DSMS 400
66 #define RELEASE_PRIO_JIT_CONTEXTS 500
67 #define RELEASE_PRIO_CRYPTOHASH_CONTEXTS 600
68 #define RELEASE_PRIO_HMAC_CONTEXTS 700
69 
70 /* priorities of built-in AFTER_LOCKS resources */
71 #define RELEASE_PRIO_CATCACHE_REFS 100
72 #define RELEASE_PRIO_CATCACHE_LIST_REFS 200
73 #define RELEASE_PRIO_PLANCACHE_REFS 300
74 #define RELEASE_PRIO_TUPDESC_REFS 400
75 #define RELEASE_PRIO_SNAPSHOT_REFS 500
76 #define RELEASE_PRIO_FILES 600
77 #define RELEASE_PRIO_WAITEVENTSETS 700
78 
79 /* 0 is considered invalid */
80 #define RELEASE_PRIO_FIRST 1
81 #define RELEASE_PRIO_LAST UINT32_MAX
82 
83 /*
84  * In order to track an object, resowner.c needs a few callbacks for it.
85  * The callbacks for resources of a specific kind are encapsulated in
86  * ResourceOwnerDesc.
87  *
88  * Note that the callbacks occur post-commit or post-abort, so the callback
89  * functions can only do noncritical cleanup and must not fail.
90  */
91 typedef struct ResourceOwnerDesc
92 {
93  const char *name; /* name for the object kind, for debugging */
94 
95  /* when are these objects released? */
98 
99  /*
100  * Release resource.
101  *
102  * This is called for each resource in the resource owner, in the order
103  * specified by 'release_phase' and 'release_priority' when the whole
104  * resource owner is been released or when ResourceOwnerReleaseAllOfKind()
105  * is called. The resource is implicitly removed from the owner, the
106  * callback function doesn't need to call ResourceOwnerForget.
107  */
109 
110  /*
111  * Format a string describing the resource, for debugging purposes. If a
112  * resource has not been properly released before commit, this is used to
113  * print a WARNING.
114  *
115  * This can be left to NULL, in which case a generic "[resource name]: %p"
116  * format is used.
117  */
118  char *(*DebugPrint) (Datum res);
119 
121 
122 /*
123  * Dynamically loaded modules can get control during ResourceOwnerRelease
124  * by providing a callback of this form.
125  */
127  bool isCommit,
128  bool isTopLevel,
129  void *arg);
130 
131 
132 /*
133  * Functions in resowner.c
134  */
135 
136 /* generic routines */
138  const char *name);
139 extern void ResourceOwnerRelease(ResourceOwner owner,
140  ResourceReleasePhase phase,
141  bool isCommit,
142  bool isTopLevel);
143 extern void ResourceOwnerDelete(ResourceOwner owner);
145 extern void ResourceOwnerNewParent(ResourceOwner owner,
146  ResourceOwner newparent);
147 
148 extern void ResourceOwnerEnlarge(ResourceOwner owner);
149 extern void ResourceOwnerRemember(ResourceOwner owner, Datum res, const ResourceOwnerDesc *kind);
150 extern void ResourceOwnerForget(ResourceOwner owner, Datum res, const ResourceOwnerDesc *kind);
151 
152 extern void ResourceOwnerReleaseAllOfKind(ResourceOwner owner, const ResourceOwnerDesc *kind);
153 
155  void *arg);
157  void *arg);
158 
159 extern void CreateAuxProcessResourceOwner(void);
160 extern void ReleaseAuxProcessResources(bool isCommit);
161 
162 /* special support for local lock management */
163 struct LOCALLOCK;
164 extern void ResourceOwnerRememberLock(ResourceOwner owner, struct LOCALLOCK *locallock);
165 extern void ResourceOwnerForgetLock(ResourceOwner owner, struct LOCALLOCK *locallock);
166 
167 #endif /* RESOWNER_H */
unsigned int uint32
Definition: c.h:495
#define PGDLLIMPORT
Definition: c.h:1305
void * arg
uintptr_t Datum
Definition: postgres.h:64
void ResourceOwnerNewParent(ResourceOwner owner, ResourceOwner newparent)
Definition: resowner.c:897
void ResourceOwnerRemember(ResourceOwner owner, Datum res, const ResourceOwnerDesc *kind)
Definition: resowner.c:514
void ResourceOwnerRememberLock(ResourceOwner owner, struct LOCALLOCK *locallock)
Definition: resowner.c:1045
struct ResourceOwnerDesc ResourceOwnerDesc
PGDLLIMPORT ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:167
void UnregisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg)
Definition: resowner.c:958
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:413
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:1002
ResourceOwner ResourceOwnerGetParent(ResourceOwner owner)
Definition: resowner.c:888
ResourceReleasePhase
Definition: resowner.h:53
@ RESOURCE_RELEASE_LOCKS
Definition: resowner.h:55
@ RESOURCE_RELEASE_BEFORE_LOCKS
Definition: resowner.h:54
@ RESOURCE_RELEASE_AFTER_LOCKS
Definition: resowner.h:56
void ResourceOwnerForget(ResourceOwner owner, Datum res, const ResourceOwnerDesc *kind)
Definition: resowner.c:554
PGDLLIMPORT ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:168
uint32 ResourceReleasePriority
Definition: resowner.h:59
void CreateAuxProcessResourceOwner(void)
Definition: resowner.c:982
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:648
PGDLLIMPORT ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:166
void ResourceOwnerForgetLock(ResourceOwner owner, struct LOCALLOCK *locallock)
Definition: resowner.c:1065
void RegisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg)
Definition: resowner.c:944
void ResourceOwnerReleaseAllOfKind(ResourceOwner owner, const ResourceOwnerDesc *kind)
Definition: resowner.c:801
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:854
PGDLLIMPORT ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
struct ResourceOwnerData * ResourceOwner
Definition: resowner.h:27
void(* ResourceReleaseCallback)(ResourceReleasePhase phase, bool isCommit, bool isTopLevel, void *arg)
Definition: resowner.h:126
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:442
ResourceReleasePhase release_phase
Definition: resowner.h:96
void(* ReleaseResource)(Datum res)
Definition: resowner.h:108
ResourceReleasePriority release_priority
Definition: resowner.h:97
const char * name
Definition: resowner.h:93
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46
const char * name