PostgreSQL Source Code git master
Data Structures | Typedefs | Functions | Variables
pmchild.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "postmaster/autovacuum.h"
#include "postmaster/postmaster.h"
#include "replication/walsender.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
Include dependency graph for pmchild.c:

Go to the source code of this file.

Data Structures

struct   PMChildPool
 

Typedefs

typedef struct PMChildPool  PMChildPool
 

Functions

 
 
 
 
 
 

Variables

 
 
 

Typedef Documentation

PMChildPool

typedef struct PMChildPool PMChildPool

Function Documentation

AllocDeadEndChild()

PMChild * AllocDeadEndChild ( void  )

Definition at line 224 of file pmchild.c.

225{
226 PMChild *pmchild;
227
228 elog(DEBUG2, "allocating dead-end child");
229
230 pmchild = (PMChild *) palloc_extended(sizeof(PMChild), MCXT_ALLOC_NO_OOM);
231 if (pmchild)
232 {
233 pmchild->pid = 0;
234 pmchild->child_slot = 0;
236 pmchild->rw = NULL;
237 pmchild->bgworker_notify = false;
238
240 }
241
242 return pmchild;
243}
#define DEBUG2
Definition: elog.h:29
#define elog(elevel,...)
Definition: elog.h:226
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:29
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:347
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1417
@ B_DEAD_END_BACKEND
Definition: miscadmin.h:342
dlist_head ActiveChildList
Definition: pmchild.c:60
struct RegisteredBgWorker * rw
Definition: postmaster.h:45
bool bgworker_notify
Definition: postmaster.h:46
BackendType bkend_type
Definition: postmaster.h:44
dlist_node elem
Definition: postmaster.h:47
pid_t pid
Definition: postmaster.h:42
int child_slot
Definition: postmaster.h:43

References ActiveChildList, B_DEAD_END_BACKEND, PMChild::bgworker_notify, PMChild::bkend_type, PMChild::child_slot, DEBUG2, dlist_push_head(), PMChild::elem, elog, MCXT_ALLOC_NO_OOM, palloc_extended(), PMChild::pid, and PMChild::rw.

Referenced by BackendStartup().

AssignPostmasterChildSlot()

PMChild * AssignPostmasterChildSlot ( BackendType  btype )

Definition at line 178 of file pmchild.c.

179{
180 dlist_head *freelist;
181 PMChild *pmchild;
182
183 if (pmchild_pools[btype].size == 0)
184 elog(ERROR, "cannot allocate a PMChild slot for backend type %d", btype);
185
186 freelist = &pmchild_pools[btype].freelist;
187 if (dlist_is_empty(freelist))
188 return NULL;
189
190 pmchild = dlist_container(PMChild, elem, dlist_pop_head_node(freelist));
191 pmchild->pid = 0;
192 pmchild->bkend_type = btype;
193 pmchild->rw = NULL;
194 pmchild->bgworker_notify = true;
195
196 /*
197 * pmchild->child_slot for each entry was initialized when the array of
198 * slots was allocated. Sanity check it.
199 */
200 if (!(pmchild->child_slot >= pmchild_pools[btype].first_slotno &&
201 pmchild->child_slot < pmchild_pools[btype].first_slotno + pmchild_pools[btype].size))
202 {
203 elog(ERROR, "pmchild freelist for backend type %d is corrupt",
204 pmchild->bkend_type);
205 }
206
208
209 /* Update the status in the shared memory array */
211
212 elog(DEBUG2, "assigned pm child slot %d for %s",
213 pmchild->child_slot, PostmasterChildName(btype));
214
215 return pmchild;
216}
#define ERROR
Definition: elog.h:39
static dlist_node * dlist_pop_head_node(dlist_head *head)
Definition: ilist.h:450
static bool dlist_is_empty(const dlist_head *head)
Definition: ilist.h:336
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
const char * PostmasterChildName(BackendType child_type)
static PMChildPool pmchild_pools[BACKEND_NUM_TYPES]
Definition: pmchild.c:54
void MarkPostmasterChildSlotAssigned(int slot)
Definition: pmsignal.c:230
int first_slotno
Definition: pmchild.c:50
int size
Definition: pmchild.c:48
dlist_head freelist
Definition: pmchild.c:51
Definition: ilist.h:152

References ActiveChildList, PMChild::bgworker_notify, PMChild::bkend_type, PMChild::child_slot, DEBUG2, dlist_container, dlist_is_empty(), dlist_pop_head_node(), dlist_push_head(), PMChild::elem, elog, ERROR, PMChildPool::first_slotno, PMChildPool::freelist, MarkPostmasterChildSlotAssigned(), PMChild::pid, pmchild_pools, PostmasterChildName(), PMChild::rw, and PMChildPool::size.

Referenced by BackendStartup(), StartBackgroundWorker(), StartChildProcess(), and StartSysLogger().

FindPostmasterChildByPid()

PMChild * FindPostmasterChildByPid ( int  pid )

Definition at line 290 of file pmchild.c.

291{
292 dlist_iter iter;
293
295 {
296 PMChild *bp = dlist_container(PMChild, elem, iter.cur);
297
298 if (bp->pid == pid)
299 return bp;
300 }
301 return NULL;
302}
#define dlist_foreach(iter, lhead)
Definition: ilist.h:623
Definition: ilist.h:178
dlist_node * cur
Definition: ilist.h:179

References ActiveChildList, dlist_iter::cur, dlist_container, dlist_foreach, and PMChild::pid.

Referenced by process_pm_child_exit().

InitPostmasterChildSlots()

void InitPostmasterChildSlots ( void  )

Definition at line 97 of file pmchild.c.

98{
99 int slotno;
100 PMChild *slots;
101
102 /*
103 * We allow more connections here than we can have backends because some
104 * might still be authenticating; they might fail auth, or some existing
105 * backend might exit before the auth cycle is completed. The exact
106 * MaxConnections limit is enforced when a new backend tries to join the
107 * PGPROC array.
108 *
109 * WAL senders start out as regular backends, so they share the same pool.
110 */
112
116
117 /*
118 * There can be only one of each of these running at a time. They each
119 * get their own pool of just one entry.
120 */
131
132 /* The rest of the pmchild_pools are left at zero size */
133
134 /* Count the total number of slots */
136 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
138
139 /* Allocate enough slots, and make sure Valgrind doesn't complain */
140 slots = palloc(num_pmchild_slots * sizeof(PMChild));
141#ifdef USE_VALGRIND
142 pmchild_array = slots;
143#endif
144
145 /* Initialize them */
146 slotno = 0;
147 for (int btype = 0; btype < BACKEND_NUM_TYPES; btype++)
148 {
149 pmchild_pools[btype].first_slotno = slotno + 1;
150 dlist_init(&pmchild_pools[btype].freelist);
151
152 for (int j = 0; j < pmchild_pools[btype].size; j++)
153 {
154 slots[slotno].pid = 0;
155 slots[slotno].child_slot = slotno + 1;
156 slots[slotno].bkend_type = B_INVALID;
157 slots[slotno].rw = NULL;
158 slots[slotno].bgworker_notify = false;
159 dlist_push_tail(&pmchild_pools[btype].freelist, &slots[slotno].elem);
160 slotno++;
161 }
162 }
163 Assert(slotno == num_pmchild_slots);
164
165 /* Initialize other structures */
167}
int autovacuum_worker_slots
Definition: autovacuum.c:118
int MaxConnections
Definition: globals.c:143
int max_worker_processes
Definition: globals.c:144
Assert(PointerIsAligned(start, uint64))
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
j
int j
Definition: isn.c:78
i
int i
Definition: isn.c:77
void * palloc(Size size)
Definition: mcxt.c:1365
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:376
@ B_WAL_SUMMARIZER
Definition: miscadmin.h:366
@ B_WAL_WRITER
Definition: miscadmin.h:367
@ B_WAL_RECEIVER
Definition: miscadmin.h:365
@ B_CHECKPOINTER
Definition: miscadmin.h:362
@ B_IO_WORKER
Definition: miscadmin.h:363
@ B_LOGGER
Definition: miscadmin.h:373
@ B_STARTUP
Definition: miscadmin.h:364
@ B_BG_WORKER
Definition: miscadmin.h:345
@ B_INVALID
Definition: miscadmin.h:338
@ B_BG_WRITER
Definition: miscadmin.h:361
@ B_BACKEND
Definition: miscadmin.h:341
@ B_ARCHIVER
Definition: miscadmin.h:360
@ B_AUTOVAC_LAUNCHER
Definition: miscadmin.h:343
@ B_SLOTSYNC_WORKER
Definition: miscadmin.h:347
@ B_AUTOVAC_WORKER
Definition: miscadmin.h:344
NON_EXEC_STATIC int num_pmchild_slots
Definition: pmchild.c:55
#define MAX_IO_WORKERS
Definition: proc.h:462
int max_wal_senders
Definition: walsender.c:129

References ActiveChildList, Assert(), autovacuum_worker_slots, B_ARCHIVER, B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BACKEND, B_BG_WORKER, B_BG_WRITER, B_CHECKPOINTER, B_INVALID, B_IO_WORKER, B_LOGGER, B_SLOTSYNC_WORKER, B_STARTUP, B_WAL_RECEIVER, B_WAL_SUMMARIZER, B_WAL_WRITER, BACKEND_NUM_TYPES, PMChild::bgworker_notify, PMChild::bkend_type, PMChild::child_slot, dlist_init(), dlist_push_tail(), PMChildPool::first_slotno, i, j, MAX_IO_WORKERS, max_wal_senders, max_worker_processes, MaxConnections, num_pmchild_slots, palloc(), PMChild::pid, pmchild_pools, PMChild::rw, and PMChildPool::size.

Referenced by BootstrapModeMain(), PostgresSingleUserMain(), and PostmasterMain().

MaxLivePostmasterChildren()

int MaxLivePostmasterChildren ( void  )

Definition at line 81 of file pmchild.c.

82{
83 if (num_pmchild_slots == 0)
84 elog(ERROR, "PM child array not initialized yet");
85 return num_pmchild_slots;
86}

References elog, ERROR, and num_pmchild_slots.

Referenced by PMSignalShmemInit(), and PMSignalShmemSize().

ReleasePostmasterChildSlot()

bool ReleasePostmasterChildSlot ( PMChildpmchild )

Definition at line 252 of file pmchild.c.

253{
254 dlist_delete(&pmchild->elem);
255 if (pmchild->bkend_type == B_DEAD_END_BACKEND)
256 {
257 elog(DEBUG2, "releasing dead-end backend");
258 pfree(pmchild);
259 return true;
260 }
261 else
262 {
263 PMChildPool *pool;
264
265 elog(DEBUG2, "releasing pm child slot %d", pmchild->child_slot);
266
267 /* WAL senders start out as regular backends, and share the pool */
268 if (pmchild->bkend_type == B_WAL_SENDER)
269 pool = &pmchild_pools[B_BACKEND];
270 else
271 pool = &pmchild_pools[pmchild->bkend_type];
272
273 /* sanity check that we return the entry to the right pool */
274 if (!(pmchild->child_slot >= pool->first_slotno &&
275 pmchild->child_slot < pool->first_slotno + pool->size))
276 {
277 elog(ERROR, "pmchild freelist for backend type %d is corrupt",
278 pmchild->bkend_type);
279 }
280
281 dlist_push_head(&pool->freelist, &pmchild->elem);
283 }
284}
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
void pfree(void *pointer)
Definition: mcxt.c:1594
@ B_WAL_SENDER
Definition: miscadmin.h:346
bool MarkPostmasterChildSlotUnassigned(int slot)
Definition: pmsignal.c:249

References B_BACKEND, B_DEAD_END_BACKEND, B_WAL_SENDER, PMChild::bkend_type, PMChild::child_slot, DEBUG2, dlist_delete(), dlist_push_head(), PMChild::elem, elog, ERROR, PMChildPool::first_slotno, PMChildPool::freelist, MarkPostmasterChildSlotUnassigned(), pfree(), pmchild_pools, and PMChildPool::size.

Referenced by BackendStartup(), CleanupBackend(), maybe_reap_io_worker(), process_pm_child_exit(), StartBackgroundWorker(), StartChildProcess(), and StartSysLogger().

Variable Documentation

ActiveChildList

dlist_head ActiveChildList

Definition at line 60 of file pmchild.c.

Referenced by AllocDeadEndChild(), AssignPostmasterChildSlot(), CountChildren(), FindPostmasterChildByPid(), InitPostmasterChildSlots(), PostmasterMarkPIDForWorkerNotify(), and SignalChildren().

num_pmchild_slots

NON_EXEC_STATIC int num_pmchild_slots = 0

Definition at line 55 of file pmchild.c.

Referenced by InitPostmasterChildSlots(), and MaxLivePostmasterChildren().

pmchild_pools

static

Definition at line 54 of file pmchild.c.

Referenced by AssignPostmasterChildSlot(), InitPostmasterChildSlots(), and ReleasePostmasterChildSlot().

AltStyle によって変換されたページ (->オリジナル) /