Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * proc.c
4 : * routines to manage per-process shared memory data structure
5 : *
6 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/storage/lmgr/proc.c
12 : *
13 : *-------------------------------------------------------------------------
14 : */
15 : /*
16 : * Interface (a):
17 : * JoinWaitQueue(), ProcSleep(), ProcWakeup()
18 : *
19 : * Waiting for a lock causes the backend to be put to sleep. Whoever releases
20 : * the lock wakes the process up again (and gives it an error code so it knows
21 : * whether it was awoken on an error condition).
22 : *
23 : * Interface (b):
24 : *
25 : * ProcReleaseLocks -- frees the locks associated with current transaction
26 : *
27 : * ProcKill -- destroys the shared memory state (and locks)
28 : * associated with the process.
29 : */
30 : #include "postgres.h"
31 :
32 : #include <signal.h>
33 : #include <unistd.h>
34 : #include <sys/time.h>
35 :
36 : #include "access/transam.h"
37 : #include "access/twophase.h"
38 : #include "access/xlogutils.h"
39 : #include "miscadmin.h"
40 : #include "pgstat.h"
41 : #include "postmaster/autovacuum.h"
42 : #include "replication/slotsync.h"
43 : #include "replication/syncrep.h"
44 : #include "storage/condition_variable.h"
45 : #include "storage/ipc.h"
46 : #include "storage/lmgr.h"
47 : #include "storage/pmsignal.h"
48 : #include "storage/proc.h"
49 : #include "storage/procarray.h"
50 : #include "storage/procsignal.h"
51 : #include "storage/spin.h"
52 : #include "storage/standby.h"
53 : #include "utils/timeout.h"
54 : #include "utils/timestamp.h"
55 :
56 : /* GUC variables */
57 : int DeadlockTimeout = 1000;
58 : int StatementTimeout = 0;
59 : int LockTimeout = 0;
60 : int IdleInTransactionSessionTimeout = 0;
61 : int TransactionTimeout = 0;
62 : int IdleSessionTimeout = 0;
63 : bool log_lock_waits = false;
64 :
65 : /* Pointer to this process's PGPROC struct, if any */
66 : PGPROC *MyProc = NULL;
67 :
68 : /*
69 : * This spinlock protects the freelist of recycled PGPROC structures.
70 : * We cannot use an LWLock because the LWLock manager depends on already
71 : * having a PGPROC and a wait semaphore! But these structures are touched
72 : * relatively infrequently (only at backend startup or shutdown) and not for
73 : * very long, so a spinlock is okay.
74 : */
75 : NON_EXEC_STATIC slock_t *ProcStructLock = NULL;
76 :
77 : /* Pointers to shared-memory structures */
78 : PROC_HDR *ProcGlobal = NULL;
79 : NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL;
80 : PGPROC *PreparedXactProcs = NULL;
81 :
82 : static DeadLockState deadlock_state = DS_NOT_YET_CHECKED;
83 :
84 : /* Is a deadlock check pending? */
85 : static volatile sig_atomic_t got_deadlock_timeout;
86 :
87 : static void RemoveProcFromArray(int code, Datum arg);
88 : static void ProcKill(int code, Datum arg);
89 : static void AuxiliaryProcKill(int code, Datum arg);
90 : static void CheckDeadLock(void);
91 :
92 :
93 : /*
94 : * Report shared-memory space needed by InitProcGlobal.
95 : */
96 : Size
97 3794 : ProcGlobalShmemSize(void)
98 : {
99 3794 : Size size = 0;
100 : Size TotalProcs =
101 3794 : add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts));
102 : Size fpLockBitsSize,
103 : fpRelIdSize;
104 :
105 : /* ProcGlobal */
106 3794 : size = add_size(size, sizeof(PROC_HDR));
107 3794 : size = add_size(size, mul_size(TotalProcs, sizeof(PGPROC)));
108 3794 : size = add_size(size, sizeof(slock_t));
109 :
110 3794 : size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->xids)));
111 3794 : size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->subxidStates)));
112 3794 : size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->statusFlags)));
113 :
114 : /*
115 : * Memory needed for PGPROC fast-path lock arrays. Make sure the sizes are
116 : * nicely aligned in each backend.
117 : */
118 3794 : fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64));
119 3794 : fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid));
120 :
121 3794 : size = add_size(size, mul_size(TotalProcs, (fpLockBitsSize + fpRelIdSize)));
122 :
123 3794 : return size;
124 : }
125 :
126 : /*
127 : * Report number of semaphores needed by InitProcGlobal.
128 : */
129 : int
130 3794 : ProcGlobalSemas(void)
131 : {
132 : /*
133 : * We need a sema per backend (including autovacuum), plus one for each
134 : * auxiliary process.
135 : */
136 3794 : return MaxBackends + NUM_AUXILIARY_PROCS;
137 : }
138 :
139 : /*
140 : * InitProcGlobal -
141 : * Initialize the global process table during postmaster or standalone
142 : * backend startup.
143 : *
144 : * We also create all the per-process semaphores we will need to support
145 : * the requested number of backends. We used to allocate semaphores
146 : * only when backends were actually started up, but that is bad because
147 : * it lets Postgres fail under load --- a lot of Unix systems are
148 : * (mis)configured with small limits on the number of semaphores, and
149 : * running out when trying to start another backend is a common failure.
150 : * So, now we grab enough semaphores to support the desired max number
151 : * of backends immediately at initialization --- if the sysadmin has set
152 : * MaxConnections, max_worker_processes, max_wal_senders, or
153 : * autovacuum_worker_slots higher than his kernel will support, he'll
154 : * find out sooner rather than later.
155 : *
156 : * Another reason for creating semaphores here is that the semaphore
157 : * implementation typically requires us to create semaphores in the
158 : * postmaster, not in backends.
159 : *
160 : * Note: this is NOT called by individual backends under a postmaster,
161 : * not even in the EXEC_BACKEND case. The ProcGlobal and AuxiliaryProcs
162 : * pointers must be propagated specially for EXEC_BACKEND operation.
163 : */
164 : void
165 2032 : InitProcGlobal(void)
166 : {
167 : PGPROC *procs;
168 : int i,
169 : j;
170 : bool found;
171 2032 : uint32 TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS + max_prepared_xacts;
172 :
173 : /* Used for setup of per-backend fast-path slots. */
174 : char *fpPtr,
175 : *fpEndPtr PG_USED_FOR_ASSERTS_ONLY;
176 : Size fpLockBitsSize,
177 : fpRelIdSize;
178 :
179 : /* Create the ProcGlobal shared structure */
180 2032 : ProcGlobal = (PROC_HDR *)
181 2032 : ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
182 : Assert(!found);
183 :
184 : /*
185 : * Initialize the data structures.
186 : */
187 2032 : ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;
188 2032 : dlist_init(&ProcGlobal->freeProcs);
189 2032 : dlist_init(&ProcGlobal->autovacFreeProcs);
190 2032 : dlist_init(&ProcGlobal->bgworkerFreeProcs);
191 2032 : dlist_init(&ProcGlobal->walsenderFreeProcs);
192 2032 : ProcGlobal->startupBufferPinWaitBufId = -1;
193 2032 : ProcGlobal->walwriterProc = INVALID_PROC_NUMBER;
194 2032 : ProcGlobal->checkpointerProc = INVALID_PROC_NUMBER;
195 2032 : pg_atomic_init_u32(&ProcGlobal->procArrayGroupFirst, INVALID_PROC_NUMBER);
196 2032 : pg_atomic_init_u32(&ProcGlobal->clogGroupFirst, INVALID_PROC_NUMBER);
197 :
198 : /*
199 : * Create and initialize all the PGPROC structures we'll need. There are
200 : * six separate consumers: (1) normal backends, (2) autovacuum workers and
201 : * special workers, (3) background workers, (4) walsenders, (5) auxiliary
202 : * processes, and (6) prepared transactions. (For largely-historical
203 : * reasons, we combine autovacuum and special workers into one category
204 : * with a single freelist.) Each PGPROC structure is dedicated to exactly
205 : * one of these purposes, and they do not move between groups.
206 : */
207 2032 : procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC));
208 2032 : MemSet(procs, 0, TotalProcs * sizeof(PGPROC));
209 2032 : ProcGlobal->allProcs = procs;
210 : /* XXX allProcCount isn't really all of them; it excludes prepared xacts */
211 2032 : ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS;
212 :
213 : /*
214 : * Allocate arrays mirroring PGPROC fields in a dense manner. See
215 : * PROC_HDR.
216 : *
217 : * XXX: It might make sense to increase padding for these arrays, given
218 : * how hotly they are accessed.
219 : */
220 4064 : ProcGlobal->xids =
221 2032 : (TransactionId *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->xids));
222 103300 : MemSet(ProcGlobal->xids, 0, TotalProcs * sizeof(*ProcGlobal->xids));
223 2032 : ProcGlobal->subxidStates = (XidCacheStatus *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->subxidStates));
224 34524 : MemSet(ProcGlobal->subxidStates, 0, TotalProcs * sizeof(*ProcGlobal->subxidStates));
225 2032 : ProcGlobal->statusFlags = (uint8 *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->statusFlags));
226 6202 : MemSet(ProcGlobal->statusFlags, 0, TotalProcs * sizeof(*ProcGlobal->statusFlags));
227 :
228 : /*
229 : * Allocate arrays for fast-path locks. Those are variable-length, so
230 : * can't be included in PGPROC directly. We allocate a separate piece of
231 : * shared memory and then divide that between backends.
232 : */
233 2032 : fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64));
234 2032 : fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid));
235 :
236 2032 : fpPtr = ShmemAlloc(TotalProcs * (fpLockBitsSize + fpRelIdSize));
237 2032 : MemSet(fpPtr, 0, TotalProcs * (fpLockBitsSize + fpRelIdSize));
238 :
239 : /* For asserts checking we did not overflow. */
240 2032 : fpEndPtr = fpPtr + (TotalProcs * (fpLockBitsSize + fpRelIdSize));
241 :
242 270142 : for (i = 0; i < TotalProcs; i++)
243 : {
244 268110 : PGPROC *proc = &procs[i];
245 :
246 : /* Common initialization for all PGPROCs, regardless of type. */
247 :
248 : /*
249 : * Set the fast-path lock arrays, and move the pointer. We interleave
250 : * the two arrays, to (hopefully) get some locality for each backend.
251 : */
252 268110 : proc->fpLockBits = (uint64 *) fpPtr;
253 268110 : fpPtr += fpLockBitsSize;
254 :
255 268110 : proc->fpRelId = (Oid *) fpPtr;
256 268110 : fpPtr += fpRelIdSize;
257 :
258 : Assert(fpPtr <= fpEndPtr);
259 :
260 : /*
261 : * Set up per-PGPROC semaphore, latch, and fpInfoLock. Prepared xact
262 : * dummy PGPROCs don't need these though - they're never associated
263 : * with a real process
264 : */
265 268110 : if (i < MaxBackends + NUM_AUXILIARY_PROCS)
266 : {
267 266452 : proc->sem = PGSemaphoreCreate();
268 266452 : InitSharedLatch(&(proc->procLatch));
269 266452 : LWLockInitialize(&(proc->fpInfoLock), LWTRANCHE_LOCK_FASTPATH);
270 : }
271 :
272 : /*
273 : * Newly created PGPROCs for normal backends, autovacuum workers,
274 : * special workers, bgworkers, and walsenders must be queued up on the
275 : * appropriate free list. Because there can only ever be a small,
276 : * fixed number of auxiliary processes, no free list is used in that
277 : * case; InitAuxiliaryProcess() instead uses a linear search. PGPROCs
278 : * for prepared transactions are added to a free list by
279 : * TwoPhaseShmemInit().
280 : */
281 268110 : if (i < MaxConnections)
282 : {
283 : /* PGPROC for normal backend, add to freeProcs list */
284 133422 : dlist_push_tail(&ProcGlobal->freeProcs, &proc->links);
285 133422 : proc->procgloballist = &ProcGlobal->freeProcs;
286 : }
287 134688 : else if (i < MaxConnections + autovacuum_worker_slots + NUM_SPECIAL_WORKER_PROCS)
288 : {
289 : /* PGPROC for AV or special worker, add to autovacFreeProcs list */
290 26566 : dlist_push_tail(&ProcGlobal->autovacFreeProcs, &proc->links);
291 26566 : proc->procgloballist = &ProcGlobal->autovacFreeProcs;
292 : }
293 108122 : else if (i < MaxConnections + autovacuum_worker_slots + NUM_SPECIAL_WORKER_PROCS + max_worker_processes)
294 : {
295 : /* PGPROC for bgworker, add to bgworkerFreeProcs list */
296 16252 : dlist_push_tail(&ProcGlobal->bgworkerFreeProcs, &proc->links);
297 16252 : proc->procgloballist = &ProcGlobal->bgworkerFreeProcs;
298 : }
299 91870 : else if (i < MaxBackends)
300 : {
301 : /* PGPROC for walsender, add to walsenderFreeProcs list */
302 12996 : dlist_push_tail(&ProcGlobal->walsenderFreeProcs, &proc->links);
303 12996 : proc->procgloballist = &ProcGlobal->walsenderFreeProcs;
304 : }
305 :
306 : /* Initialize myProcLocks[] shared memory queues. */
307 4557870 : for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
308 4289760 : dlist_init(&(proc->myProcLocks[j]));
309 :
310 : /* Initialize lockGroupMembers list. */
311 268110 : dlist_init(&proc->lockGroupMembers);
312 :
313 : /*
314 : * Initialize the atomic variables, otherwise, it won't be safe to
315 : * access them for backends that aren't currently in use.
316 : */
317 268110 : pg_atomic_init_u32(&(proc->procArrayGroupNext), INVALID_PROC_NUMBER);
318 268110 : pg_atomic_init_u32(&(proc->clogGroupNext), INVALID_PROC_NUMBER);
319 268110 : pg_atomic_init_u64(&(proc->waitStart), 0);
320 : }
321 :
322 : /* Should have consumed exactly the expected amount of fast-path memory. */
323 : Assert(fpPtr == fpEndPtr);
324 :
325 : /*
326 : * Save pointers to the blocks of PGPROC structures reserved for auxiliary
327 : * processes and prepared transactions.
328 : */
329 2032 : AuxiliaryProcs = &procs[MaxBackends];
330 2032 : PreparedXactProcs = &procs[MaxBackends + NUM_AUXILIARY_PROCS];
331 :
332 : /* Create ProcStructLock spinlock, too */
333 2032 : ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));
334 2032 : SpinLockInit(ProcStructLock);
335 2032 : }
336 :
337 : /*
338 : * InitProcess -- initialize a per-process PGPROC entry for this backend
339 : */
340 : void
341 34434 : InitProcess(void)
342 : {
343 : dlist_head *procgloballist;
344 :
345 : /*
346 : * ProcGlobal should be set up already (if we are a backend, we inherit
347 : * this by fork() or EXEC_BACKEND mechanism from the postmaster).
348 : */
349 34434 : if (ProcGlobal == NULL)
350 0 : elog(PANIC, "proc header uninitialized");
351 :
352 34434 : if (MyProc != NULL)
353 0 : elog(ERROR, "you already exist");
354 :
355 : /*
356 : * Before we start accessing the shared memory in a serious way, mark
357 : * ourselves as an active postmaster child; this is so that the postmaster
358 : * can detect it if we exit without cleaning up.
359 : */
360 34434 : if (IsUnderPostmaster)
361 34236 : RegisterPostmasterChildActive();
362 :
363 : /*
364 : * Decide which list should supply our PGPROC. This logic must match the
365 : * way the freelists were constructed in InitProcGlobal().
366 : */
367 34434 : if (AmAutoVacuumWorkerProcess() || AmSpecialWorkerProcess())
368 3124 : procgloballist = &ProcGlobal->autovacFreeProcs;
369 31310 : else if (AmBackgroundWorkerProcess())
370 4370 : procgloballist = &ProcGlobal->bgworkerFreeProcs;
371 26940 : else if (AmWalSenderProcess())
372 2200 : procgloballist = &ProcGlobal->walsenderFreeProcs;
373 : else
374 24740 : procgloballist = &ProcGlobal->freeProcs;
375 :
376 : /*
377 : * Try to get a proc struct from the appropriate free list. If this
378 : * fails, we must be out of PGPROC structures (not to mention semaphores).
379 : *
380 : * While we are holding the ProcStructLock, also copy the current shared
381 : * estimate of spins_per_delay to local storage.
382 : */
383 34434 : SpinLockAcquire(ProcStructLock);
384 :
385 34434 : set_spins_per_delay(ProcGlobal->spins_per_delay);
386 :
387 34434 : if (!dlist_is_empty(procgloballist))
388 : {
389 34428 : MyProc = dlist_container(PGPROC, links, dlist_pop_head_node(procgloballist));
390 34428 : SpinLockRelease(ProcStructLock);
391 : }
392 : else
393 : {
394 : /*
395 : * If we reach here, all the PGPROCs are in use. This is one of the
396 : * possible places to detect "too many backends", so give the standard
397 : * error message. XXX do we need to give a different failure message
398 : * in the autovacuum case?
399 : */
400 6 : SpinLockRelease(ProcStructLock);
401 6 : if (AmWalSenderProcess())
402 4 : ereport(FATAL,
403 : (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
404 : errmsg("number of requested standby connections exceeds \"max_wal_senders\" (currently %d)",
405 : max_wal_senders)));
406 2 : ereport(FATAL,
407 : (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
408 : errmsg("sorry, too many clients already")));
409 : }
410 34428 : MyProcNumber = GetNumberFromPGProc(MyProc);
411 :
412 : /*
413 : * Cross-check that the PGPROC is of the type we expect; if this were not
414 : * the case, it would get returned to the wrong list.
415 : */
416 : Assert(MyProc->procgloballist == procgloballist);
417 :
418 : /*
419 : * Initialize all fields of MyProc, except for those previously
420 : * initialized by InitProcGlobal.
421 : */
422 34428 : dlist_node_init(&MyProc->links);
423 34428 : MyProc->waitStatus = PROC_WAIT_STATUS_OK;
424 34428 : MyProc->fpVXIDLock = false;
425 34428 : MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
426 34428 : MyProc->xid = InvalidTransactionId;
427 34428 : MyProc->xmin = InvalidTransactionId;
428 34428 : MyProc->pid = MyProcPid;
429 34428 : MyProc->vxid.procNumber = MyProcNumber;
430 34428 : MyProc->vxid.lxid = InvalidLocalTransactionId;
431 : /* databaseId and roleId will be filled in later */
432 34428 : MyProc->databaseId = InvalidOid;
433 34428 : MyProc->roleId = InvalidOid;
434 34428 : MyProc->tempNamespaceId = InvalidOid;
435 34428 : MyProc->isRegularBackend = AmRegularBackendProcess();
436 34428 : MyProc->delayChkptFlags = 0;
437 34428 : MyProc->statusFlags = 0;
438 : /* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
439 34428 : if (AmAutoVacuumWorkerProcess())
440 2386 : MyProc->statusFlags |= PROC_IS_AUTOVACUUM;
441 34428 : MyProc->lwWaiting = LW_WS_NOT_WAITING;
442 34428 : MyProc->lwWaitMode = 0;
443 34428 : MyProc->waitLock = NULL;
444 34428 : MyProc->waitProcLock = NULL;
445 34428 : pg_atomic_write_u64(&MyProc->waitStart, 0);
446 : #ifdef USE_ASSERT_CHECKING
447 : {
448 : int i;
449 :
450 : /* Last process should have released all locks. */
451 : for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
452 : Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
453 : }
454 : #endif
455 34428 : MyProc->recoveryConflictPending = false;
456 :
457 : /* Initialize fields for sync rep */
458 34428 : MyProc->waitLSN = 0;
459 34428 : MyProc->syncRepState = SYNC_REP_NOT_WAITING;
460 34428 : dlist_node_init(&MyProc->syncRepLinks);
461 :
462 : /* Initialize fields for group XID clearing. */
463 34428 : MyProc->procArrayGroupMember = false;
464 34428 : MyProc->procArrayGroupMemberXid = InvalidTransactionId;
465 : Assert(pg_atomic_read_u32(&MyProc->procArrayGroupNext) == INVALID_PROC_NUMBER);
466 :
467 : /* Check that group locking fields are in a proper initial state. */
468 : Assert(MyProc->lockGroupLeader == NULL);
469 : Assert(dlist_is_empty(&MyProc->lockGroupMembers));
470 :
471 : /* Initialize wait event information. */
472 34428 : MyProc->wait_event_info = 0;
473 :
474 : /* Initialize fields for group transaction status update. */
475 34428 : MyProc->clogGroupMember = false;
476 34428 : MyProc->clogGroupMemberXid = InvalidTransactionId;
477 34428 : MyProc->clogGroupMemberXidStatus = TRANSACTION_STATUS_IN_PROGRESS;
478 34428 : MyProc->clogGroupMemberPage = -1;
479 34428 : MyProc->clogGroupMemberLsn = InvalidXLogRecPtr;
480 : Assert(pg_atomic_read_u32(&MyProc->clogGroupNext) == INVALID_PROC_NUMBER);
481 :
482 : /*
483 : * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
484 : * on it. That allows us to repoint the process latch, which so far
485 : * points to process local one, to the shared one.
486 : */
487 34428 : OwnLatch(&MyProc->procLatch);
488 34428 : SwitchToSharedLatch();
489 :
490 : /* now that we have a proc, report wait events to shared memory */
491 34428 : pgstat_set_wait_event_storage(&MyProc->wait_event_info);
492 :
493 : /*
494 : * We might be reusing a semaphore that belonged to a failed process. So
495 : * be careful and reinitialize its value here. (This is not strictly
496 : * necessary anymore, but seems like a good idea for cleanliness.)
497 : */
498 34428 : PGSemaphoreReset(MyProc->sem);
499 :
500 : /*
501 : * Arrange to clean up at backend exit.
502 : */
503 34428 : on_shmem_exit(ProcKill, 0);
504 :
505 : /*
506 : * Now that we have a PGPROC, we could try to acquire locks, so initialize
507 : * local state needed for LWLocks, and the deadlock checker.
508 : */
509 34428 : InitLWLockAccess();
510 34428 : InitDeadLockChecking();
511 :
512 : #ifdef EXEC_BACKEND
513 :
514 : /*
515 : * Initialize backend-local pointers to all the shared data structures.
516 : * (We couldn't do this until now because it needs LWLocks.)
517 : */
518 : if (IsUnderPostmaster)
519 : AttachSharedMemoryStructs();
520 : #endif
521 34428 : }
522 :
523 : /*
524 : * InitProcessPhase2 -- make MyProc visible in the shared ProcArray.
525 : *
526 : * This is separate from InitProcess because we can't acquire LWLocks until
527 : * we've created a PGPROC, but in the EXEC_BACKEND case ProcArrayAdd won't
528 : * work until after we've done AttachSharedMemoryStructs.
529 : */
530 : void
531 34410 : InitProcessPhase2(void)
532 : {
533 : Assert(MyProc != NULL);
534 :
535 : /*
536 : * Add our PGPROC to the PGPROC array in shared memory.
537 : */
538 34410 : ProcArrayAdd(MyProc);
539 :
540 : /*
541 : * Arrange to clean that up at backend exit.
542 : */
543 34410 : on_shmem_exit(RemoveProcFromArray, 0);
544 34410 : }
545 :
546 : /*
547 : * InitAuxiliaryProcess -- create a PGPROC entry for an auxiliary process
548 : *
549 : * This is called by bgwriter and similar processes so that they will have a
550 : * MyProc value that's real enough to let them wait for LWLocks. The PGPROC
551 : * and sema that are assigned are one of the extra ones created during
552 : * InitProcGlobal.
553 : *
554 : * Auxiliary processes are presently not expected to wait for real (lockmgr)
555 : * locks, so we need not set up the deadlock checker. They are never added
556 : * to the ProcArray or the sinval messaging mechanism, either. They also
557 : * don't get a VXID assigned, since this is only useful when we actually
558 : * hold lockmgr locks.
559 : *
560 : * Startup process however uses locks but never waits for them in the
561 : * normal backend sense. Startup process also takes part in sinval messaging
562 : * as a sendOnly process, so never reads messages from sinval queue. So
563 : * Startup process does have a VXID and does show up in pg_locks.
564 : */
565 : void
566 7856 : InitAuxiliaryProcess(void)
567 : {
568 : PGPROC *auxproc;
569 : int proctype;
570 :
571 : /*
572 : * ProcGlobal should be set up already (if we are a backend, we inherit
573 : * this by fork() or EXEC_BACKEND mechanism from the postmaster).
574 : */
575 7856 : if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
576 0 : elog(PANIC, "proc header uninitialized");
577 :
578 7856 : if (MyProc != NULL)
579 0 : elog(ERROR, "you already exist");
580 :
581 7856 : if (IsUnderPostmaster)
582 7856 : RegisterPostmasterChildActive();
583 :
584 : /*
585 : * We use the ProcStructLock to protect assignment and releasing of
586 : * AuxiliaryProcs entries.
587 : *
588 : * While we are holding the ProcStructLock, also copy the current shared
589 : * estimate of spins_per_delay to local storage.
590 : */
591 7856 : SpinLockAcquire(ProcStructLock);
592 :
593 7856 : set_spins_per_delay(ProcGlobal->spins_per_delay);
594 :
595 : /*
596 : * Find a free auxproc ... *big* trouble if there isn't one ...
597 : */
598 32762 : for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)
599 : {
600 32762 : auxproc = &AuxiliaryProcs[proctype];
601 32762 : if (auxproc->pid == 0)
602 7856 : break;
603 : }
604 7856 : if (proctype >= NUM_AUXILIARY_PROCS)
605 : {
606 0 : SpinLockRelease(ProcStructLock);
607 0 : elog(FATAL, "all AuxiliaryProcs are in use");
608 : }
609 :
610 : /* Mark auxiliary proc as in use by me */
611 : /* use volatile pointer to prevent code rearrangement */
612 7856 : ((volatile PGPROC *) auxproc)->pid = MyProcPid;
613 :
614 7856 : SpinLockRelease(ProcStructLock);
615 :
616 7856 : MyProc = auxproc;
617 7856 : MyProcNumber = GetNumberFromPGProc(MyProc);
618 :
619 : /*
620 : * Initialize all fields of MyProc, except for those previously
621 : * initialized by InitProcGlobal.
622 : */
623 7856 : dlist_node_init(&MyProc->links);
624 7856 : MyProc->waitStatus = PROC_WAIT_STATUS_OK;
625 7856 : MyProc->fpVXIDLock = false;
626 7856 : MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
627 7856 : MyProc->xid = InvalidTransactionId;
628 7856 : MyProc->xmin = InvalidTransactionId;
629 7856 : MyProc->vxid.procNumber = INVALID_PROC_NUMBER;
630 7856 : MyProc->vxid.lxid = InvalidLocalTransactionId;
631 7856 : MyProc->databaseId = InvalidOid;
632 7856 : MyProc->roleId = InvalidOid;
633 7856 : MyProc->tempNamespaceId = InvalidOid;
634 7856 : MyProc->isRegularBackend = false;
635 7856 : MyProc->delayChkptFlags = 0;
636 7856 : MyProc->statusFlags = 0;
637 7856 : MyProc->lwWaiting = LW_WS_NOT_WAITING;
638 7856 : MyProc->lwWaitMode = 0;
639 7856 : MyProc->waitLock = NULL;
640 7856 : MyProc->waitProcLock = NULL;
641 7856 : pg_atomic_write_u64(&MyProc->waitStart, 0);
642 : #ifdef USE_ASSERT_CHECKING
643 : {
644 : int i;
645 :
646 : /* Last process should have released all locks. */
647 : for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
648 : Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
649 : }
650 : #endif
651 :
652 : /*
653 : * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
654 : * on it. That allows us to repoint the process latch, which so far
655 : * points to process local one, to the shared one.
656 : */
657 7856 : OwnLatch(&MyProc->procLatch);
658 7856 : SwitchToSharedLatch();
659 :
660 : /* now that we have a proc, report wait events to shared memory */
661 7856 : pgstat_set_wait_event_storage(&MyProc->wait_event_info);
662 :
663 : /* Check that group locking fields are in a proper initial state. */
664 : Assert(MyProc->lockGroupLeader == NULL);
665 : Assert(dlist_is_empty(&MyProc->lockGroupMembers));
666 :
667 : /*
668 : * We might be reusing a semaphore that belonged to a failed process. So
669 : * be careful and reinitialize its value here. (This is not strictly
670 : * necessary anymore, but seems like a good idea for cleanliness.)
671 : */
672 7856 : PGSemaphoreReset(MyProc->sem);
673 :
674 : /*
675 : * Arrange to clean up at process exit.
676 : */
677 7856 : on_shmem_exit(AuxiliaryProcKill, Int32GetDatum(proctype));
678 :
679 : /*
680 : * Now that we have a PGPROC, we could try to acquire lightweight locks.
681 : * Initialize local state needed for them. (Heavyweight locks cannot be
682 : * acquired in aux processes.)
683 : */
684 7856 : InitLWLockAccess();
685 :
686 : #ifdef EXEC_BACKEND
687 :
688 : /*
689 : * Initialize backend-local pointers to all the shared data structures.
690 : * (We couldn't do this until now because it needs LWLocks.)
691 : */
692 : if (IsUnderPostmaster)
693 : AttachSharedMemoryStructs();
694 : #endif
695 7856 : }
696 :
697 : /*
698 : * Used from bufmgr to share the value of the buffer that Startup waits on,
699 : * or to reset the value to "not waiting" (-1). This allows processing
700 : * of recovery conflicts for buffer pins. Set is made before backends look
701 : * at this value, so locking not required, especially since the set is
702 : * an atomic integer set operation.
703 : */
704 : void
705 40 : SetStartupBufferPinWaitBufId(int bufid)
706 : {
707 : /* use volatile pointer to prevent code rearrangement */
708 40 : volatile PROC_HDR *procglobal = ProcGlobal;
709 :
710 40 : procglobal->startupBufferPinWaitBufId = bufid;
711 40 : }
712 :
713 : /*
714 : * Used by backends when they receive a request to check for buffer pin waits.
715 : */
716 : int
717 10 : GetStartupBufferPinWaitBufId(void)
718 : {
719 : /* use volatile pointer to prevent code rearrangement */
720 10 : volatile PROC_HDR *procglobal = ProcGlobal;
721 :
722 10 : return procglobal->startupBufferPinWaitBufId;
723 : }
724 :
725 : /*
726 : * Check whether there are at least N free PGPROC objects. If false is
727 : * returned, *nfree will be set to the number of free PGPROC objects.
728 : * Otherwise, *nfree will be set to n.
729 : *
730 : * Note: this is designed on the assumption that N will generally be small.
731 : */
732 : bool
733 476 : HaveNFreeProcs(int n, int *nfree)
734 : {
735 : dlist_iter iter;
736 :
737 : Assert(n > 0);
738 : Assert(nfree);
739 :
740 476 : SpinLockAcquire(ProcStructLock);
741 :
742 476 : *nfree = 0;
743 1424 : dlist_foreach(iter, &ProcGlobal->freeProcs)
744 : {
745 1420 : (*nfree)++;
746 1420 : if (*nfree == n)
747 472 : break;
748 : }
749 :
750 476 : SpinLockRelease(ProcStructLock);
751 :
752 476 : return (*nfree == n);
753 : }
754 :
755 : /*
756 : * Cancel any pending wait for lock, when aborting a transaction, and revert
757 : * any strong lock count acquisition for a lock being acquired.
758 : *
759 : * (Normally, this would only happen if we accept a cancel/die
760 : * interrupt while waiting; but an ereport(ERROR) before or during the lock
761 : * wait is within the realm of possibility, too.)
762 : */
763 : void
764 876898 : LockErrorCleanup(void)
765 : {
766 : LOCALLOCK *lockAwaited;
767 : LWLock *partitionLock;
768 : DisableTimeoutParams timeouts[2];
769 :
770 876898 : HOLD_INTERRUPTS();
771 :
772 876898 : AbortStrongLockAcquire();
773 :
774 : /* Nothing to do if we weren't waiting for a lock */
775 876898 : lockAwaited = GetAwaitedLock();
776 876898 : if (lockAwaited == NULL)
777 : {
778 876818 : RESUME_INTERRUPTS();
779 876818 : return;
780 : }
781 :
782 : /*
783 : * Turn off the deadlock and lock timeout timers, if they are still
784 : * running (see ProcSleep). Note we must preserve the LOCK_TIMEOUT
785 : * indicator flag, since this function is executed before
786 : * ProcessInterrupts when responding to SIGINT; else we'd lose the
787 : * knowledge that the SIGINT came from a lock timeout and not an external
788 : * source.
789 : */
790 80 : timeouts[0].id = DEADLOCK_TIMEOUT;
791 80 : timeouts[0].keep_indicator = false;
792 80 : timeouts[1].id = LOCK_TIMEOUT;
793 80 : timeouts[1].keep_indicator = true;
794 80 : disable_timeouts(timeouts, 2);
795 :
796 : /* Unlink myself from the wait queue, if on it (might not be anymore!) */
797 80 : partitionLock = LockHashPartitionLock(lockAwaited->hashcode);
798 80 : LWLockAcquire(partitionLock, LW_EXCLUSIVE);
799 :
800 80 : if (!dlist_node_is_detached(&MyProc->links))
801 : {
802 : /* We could not have been granted the lock yet */
803 80 : RemoveFromWaitQueue(MyProc, lockAwaited->hashcode);
804 : }
805 : else
806 : {
807 : /*
808 : * Somebody kicked us off the lock queue already. Perhaps they
809 : * granted us the lock, or perhaps they detected a deadlock. If they
810 : * did grant us the lock, we'd better remember it in our local lock
811 : * table.
812 : */
813 0 : if (MyProc->waitStatus == PROC_WAIT_STATUS_OK)
814 0 : GrantAwaitedLock();
815 : }
816 :
817 80 : ResetAwaitedLock();
818 :
819 80 : LWLockRelease(partitionLock);
820 :
821 80 : RESUME_INTERRUPTS();
822 : }
823 :
824 :
825 : /*
826 : * ProcReleaseLocks() -- release locks associated with current transaction
827 : * at main transaction commit or abort
828 : *
829 : * At main transaction commit, we release standard locks except session locks.
830 : * At main transaction abort, we release all locks including session locks.
831 : *
832 : * Advisory locks are released only if they are transaction-level;
833 : * session-level holds remain, whether this is a commit or not.
834 : *
835 : * At subtransaction commit, we don't release any locks (so this func is not
836 : * needed at all); we will defer the releasing to the parent transaction.
837 : * At subtransaction abort, we release all locks held by the subtransaction;
838 : * this is implemented by retail releasing of the locks under control of
839 : * the ResourceOwner mechanism.
840 : */
841 : void
842 818070 : ProcReleaseLocks(bool isCommit)
843 : {
844 818070 : if (!MyProc)
845 0 : return;
846 : /* If waiting, get off wait queue (should only be needed after error) */
847 818070 : LockErrorCleanup();
848 : /* Release standard locks, including session-level if aborting */
849 818070 : LockReleaseAll(DEFAULT_LOCKMETHOD, !isCommit);
850 : /* Release transaction-level advisory locks */
851 818070 : LockReleaseAll(USER_LOCKMETHOD, false);
852 : }
853 :
854 :
855 : /*
856 : * RemoveProcFromArray() -- Remove this process from the shared ProcArray.
857 : */
858 : static void
859 34410 : RemoveProcFromArray(int code, Datum arg)
860 : {
861 : Assert(MyProc != NULL);
862 34410 : ProcArrayRemove(MyProc, InvalidTransactionId);
863 34410 : }
864 :
865 : /*
866 : * ProcKill() -- Destroy the per-proc data structure for
867 : * this process. Release any of its held LW locks.
868 : */
869 : static void
870 34428 : ProcKill(int code, Datum arg)
871 : {
872 : PGPROC *proc;
873 : dlist_head *procgloballist;
874 :
875 : Assert(MyProc != NULL);
876 :
877 : /* not safe if forked by system(), etc. */
878 34428 : if (MyProc->pid != (int) getpid())
879 0 : elog(PANIC, "ProcKill() called in child process");
880 :
881 : /* Make sure we're out of the sync rep lists */
882 34428 : SyncRepCleanupAtProcExit();
883 :
884 : #ifdef USE_ASSERT_CHECKING
885 : {
886 : int i;
887 :
888 : /* Last process should have released all locks. */
889 : for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
890 : Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
891 : }
892 : #endif
893 :
894 : /*
895 : * Release any LW locks I am holding. There really shouldn't be any, but
896 : * it's cheap to check again before we cut the knees off the LWLock
897 : * facility by releasing our PGPROC ...
898 : */
899 34428 : LWLockReleaseAll();
900 :
901 : /* Cancel any pending condition variable sleep, too */
902 34428 : ConditionVariableCancelSleep();
903 :
904 : /*
905 : * Detach from any lock group of which we are a member. If the leader
906 : * exits before all other group members, its PGPROC will remain allocated
907 : * until the last group process exits; that process must return the
908 : * leader's PGPROC to the appropriate list.
909 : */
910 34428 : if (MyProc->lockGroupLeader != NULL)
911 : {
912 2874 : PGPROC *leader = MyProc->lockGroupLeader;
913 2874 : LWLock *leader_lwlock = LockHashPartitionLockByProc(leader);
914 :
915 2874 : LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
916 : Assert(!dlist_is_empty(&leader->lockGroupMembers));
917 2874 : dlist_delete(&MyProc->lockGroupLink);
918 2874 : if (dlist_is_empty(&leader->lockGroupMembers))
919 : {
920 138 : leader->lockGroupLeader = NULL;
921 138 : if (leader != MyProc)
922 : {
923 0 : procgloballist = leader->procgloballist;
924 :
925 : /* Leader exited first; return its PGPROC. */
926 0 : SpinLockAcquire(ProcStructLock);
927 0 : dlist_push_head(procgloballist, &leader->links);
928 0 : SpinLockRelease(ProcStructLock);
929 : }
930 : }
931 2736 : else if (leader != MyProc)
932 2736 : MyProc->lockGroupLeader = NULL;
933 2874 : LWLockRelease(leader_lwlock);
934 : }
935 :
936 : /*
937 : * Reset MyLatch to the process local one. This is so that signal
938 : * handlers et al can continue using the latch after the shared latch
939 : * isn't ours anymore.
940 : *
941 : * Similarly, stop reporting wait events to MyProc->wait_event_info.
942 : *
943 : * After that clear MyProc and disown the shared latch.
944 : */
945 34428 : SwitchBackToLocalLatch();
946 34428 : pgstat_reset_wait_event_storage();
947 :
948 34428 : proc = MyProc;
949 34428 : MyProc = NULL;
950 34428 : MyProcNumber = INVALID_PROC_NUMBER;
951 34428 : DisownLatch(&proc->procLatch);
952 :
953 : /* Mark the proc no longer in use */
954 34428 : proc->pid = 0;
955 34428 : proc->vxid.procNumber = INVALID_PROC_NUMBER;
956 34428 : proc->vxid.lxid = InvalidTransactionId;
957 :
958 34428 : procgloballist = proc->procgloballist;
959 34428 : SpinLockAcquire(ProcStructLock);
960 :
961 : /*
962 : * If we're still a member of a locking group, that means we're a leader
963 : * which has somehow exited before its children. The last remaining child
964 : * will release our PGPROC. Otherwise, release it now.
965 : */
966 34428 : if (proc->lockGroupLeader == NULL)
967 : {
968 : /* Since lockGroupLeader is NULL, lockGroupMembers should be empty. */
969 : Assert(dlist_is_empty(&proc->lockGroupMembers));
970 :
971 : /* Return PGPROC structure (and semaphore) to appropriate freelist */
972 34428 : dlist_push_tail(procgloballist, &proc->links);
973 : }
974 :
975 : /* Update shared estimate of spins_per_delay */
976 34428 : ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
977 :
978 34428 : SpinLockRelease(ProcStructLock);
979 :
980 : /* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
981 34428 : if (AutovacuumLauncherPid != 0)
982 2386 : kill(AutovacuumLauncherPid, SIGUSR2);
983 34428 : }
984 :
985 : /*
986 : * AuxiliaryProcKill() -- Cut-down version of ProcKill for auxiliary
987 : * processes (bgwriter, etc). The PGPROC and sema are not released, only
988 : * marked as not-in-use.
989 : */
990 : static void
991 7856 : AuxiliaryProcKill(int code, Datum arg)
992 : {
993 7856 : int proctype = DatumGetInt32(arg);
994 : PGPROC *auxproc PG_USED_FOR_ASSERTS_ONLY;
995 : PGPROC *proc;
996 :
997 : Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
998 :
999 : /* not safe if forked by system(), etc. */
1000 7856 : if (MyProc->pid != (int) getpid())
1001 0 : elog(PANIC, "AuxiliaryProcKill() called in child process");
1002 :
1003 7856 : auxproc = &AuxiliaryProcs[proctype];
1004 :
1005 : Assert(MyProc == auxproc);
1006 :
1007 : /* Release any LW locks I am holding (see notes above) */
1008 7856 : LWLockReleaseAll();
1009 :
1010 : /* Cancel any pending condition variable sleep, too */
1011 7856 : ConditionVariableCancelSleep();
1012 :
1013 : /* look at the equivalent ProcKill() code for comments */
1014 7856 : SwitchBackToLocalLatch();
1015 7856 : pgstat_reset_wait_event_storage();
1016 :
1017 7856 : proc = MyProc;
1018 7856 : MyProc = NULL;
1019 7856 : MyProcNumber = INVALID_PROC_NUMBER;
1020 7856 : DisownLatch(&proc->procLatch);
1021 :
1022 7856 : SpinLockAcquire(ProcStructLock);
1023 :
1024 : /* Mark auxiliary proc no longer in use */
1025 7856 : proc->pid = 0;
1026 7856 : proc->vxid.procNumber = INVALID_PROC_NUMBER;
1027 7856 : proc->vxid.lxid = InvalidTransactionId;
1028 :
1029 : /* Update shared estimate of spins_per_delay */
1030 7856 : ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
1031 :
1032 7856 : SpinLockRelease(ProcStructLock);
1033 7856 : }
1034 :
1035 : /*
1036 : * AuxiliaryPidGetProc -- get PGPROC for an auxiliary process
1037 : * given its PID
1038 : *
1039 : * Returns NULL if not found.
1040 : */
1041 : PGPROC *
1042 8606 : AuxiliaryPidGetProc(int pid)
1043 : {
1044 8606 : PGPROC *result = NULL;
1045 : int index;
1046 :
1047 8606 : if (pid == 0) /* never match dummy PGPROCs */
1048 6 : return NULL;
1049 :
1050 30428 : for (index = 0; index < NUM_AUXILIARY_PROCS; index++)
1051 : {
1052 30428 : PGPROC *proc = &AuxiliaryProcs[index];
1053 :
1054 30428 : if (proc->pid == pid)
1055 : {
1056 8600 : result = proc;
1057 8600 : break;
1058 : }
1059 : }
1060 8600 : return result;
1061 : }
1062 :
1063 :
1064 : /*
1065 : * JoinWaitQueue -- join the wait queue on the specified lock
1066 : *
1067 : * It's not actually guaranteed that we need to wait when this function is
1068 : * called, because it could be that when we try to find a position at which
1069 : * to insert ourself into the wait queue, we discover that we must be inserted
1070 : * ahead of everyone who wants a lock that conflict with ours. In that case,
1071 : * we get the lock immediately. Because of this, it's sensible for this function
1072 : * to have a dontWait argument, despite the name.
1073 : *
1074 : * On entry, the caller has already set up LOCK and PROCLOCK entries to
1075 : * reflect that we have "requested" the lock. The caller is responsible for
1076 : * cleaning that up, if we end up not joining the queue after all.
1077 : *
1078 : * The lock table's partition lock must be held at entry, and is still held
1079 : * at exit. The caller must release it before calling ProcSleep().
1080 : *
1081 : * Result is one of the following:
1082 : *
1083 : * PROC_WAIT_STATUS_OK - lock was immediately granted
1084 : * PROC_WAIT_STATUS_WAITING - joined the wait queue; call ProcSleep()
1085 : * PROC_WAIT_STATUS_ERROR - immediate deadlock was detected, or would
1086 : * need to wait and dontWait == true
1087 : *
1088 : * NOTES: The process queue is now a priority queue for locking.
1089 : */
1090 : ProcWaitStatus
1091 4118 : JoinWaitQueue(LOCALLOCK *locallock, LockMethod lockMethodTable, bool dontWait)
1092 : {
1093 4118 : LOCKMODE lockmode = locallock->tag.mode;
1094 4118 : LOCK *lock = locallock->lock;
1095 4118 : PROCLOCK *proclock = locallock->proclock;
1096 4118 : uint32 hashcode = locallock->hashcode;
1097 4118 : LWLock *partitionLock PG_USED_FOR_ASSERTS_ONLY = LockHashPartitionLock(hashcode);
1098 4118 : dclist_head *waitQueue = &lock->waitProcs;
1099 4118 : PGPROC *insert_before = NULL;
1100 : LOCKMASK myProcHeldLocks;
1101 : LOCKMASK myHeldLocks;
1102 4118 : bool early_deadlock = false;
1103 4118 : PGPROC *leader = MyProc->lockGroupLeader;
1104 :
1105 : Assert(LWLockHeldByMeInMode(partitionLock, LW_EXCLUSIVE));
1106 :
1107 : /*
1108 : * Set bitmask of locks this process already holds on this object.
1109 : */
1110 4118 : myHeldLocks = MyProc->heldLocks = proclock->holdMask;
1111 :
1112 : /*
1113 : * Determine which locks we're already holding.
1114 : *
1115 : * If group locking is in use, locks held by members of my locking group
1116 : * need to be included in myHeldLocks. This is not required for relation
1117 : * extension lock which conflict among group members. However, including
1118 : * them in myHeldLocks will give group members the priority to get those
1119 : * locks as compared to other backends which are also trying to acquire
1120 : * those locks. OTOH, we can avoid giving priority to group members for
1121 : * that kind of locks, but there doesn't appear to be a clear advantage of
1122 : * the same.
1123 : */
1124 4118 : myProcHeldLocks = proclock->holdMask;
1125 4118 : myHeldLocks = myProcHeldLocks;
1126 4118 : if (leader != NULL)
1127 : {
1128 : dlist_iter iter;
1129 :
1130 78 : dlist_foreach(iter, &lock->procLocks)
1131 : {
1132 : PROCLOCK *otherproclock;
1133 :
1134 58 : otherproclock = dlist_container(PROCLOCK, lockLink, iter.cur);
1135 :
1136 58 : if (otherproclock->groupLeader == leader)
1137 28 : myHeldLocks |= otherproclock->holdMask;
1138 : }
1139 : }
1140 :
1141 : /*
1142 : * Determine where to add myself in the wait queue.
1143 : *
1144 : * Normally I should go at the end of the queue. However, if I already
1145 : * hold locks that conflict with the request of any previous waiter, put
1146 : * myself in the queue just in front of the first such waiter. This is not
1147 : * a necessary step, since deadlock detection would move me to before that
1148 : * waiter anyway; but it's relatively cheap to detect such a conflict
1149 : * immediately, and avoid delaying till deadlock timeout.
1150 : *
1151 : * Special case: if I find I should go in front of some waiter, check to
1152 : * see if I conflict with already-held locks or the requests before that
1153 : * waiter. If not, then just grant myself the requested lock immediately.
1154 : * This is the same as the test for immediate grant in LockAcquire, except
1155 : * we are only considering the part of the wait queue before my insertion
1156 : * point.
1157 : */
1158 4118 : if (myHeldLocks != 0 && !dclist_is_empty(waitQueue))
1159 : {
1160 12 : LOCKMASK aheadRequests = 0;
1161 : dlist_iter iter;
1162 :
1163 12 : dclist_foreach(iter, waitQueue)
1164 : {
1165 12 : PGPROC *proc = dlist_container(PGPROC, links, iter.cur);
1166 :
1167 : /*
1168 : * If we're part of the same locking group as this waiter, its
1169 : * locks neither conflict with ours nor contribute to
1170 : * aheadRequests.
1171 : */
1172 12 : if (leader != NULL && leader == proc->lockGroupLeader)
1173 0 : continue;
1174 :
1175 : /* Must he wait for me? */
1176 12 : if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
1177 : {
1178 : /* Must I wait for him ? */
1179 12 : if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
1180 : {
1181 : /*
1182 : * Yes, so we have a deadlock. Easiest way to clean up
1183 : * correctly is to call RemoveFromWaitQueue(), but we
1184 : * can't do that until we are *on* the wait queue. So, set
1185 : * a flag to check below, and break out of loop. Also,
1186 : * record deadlock info for later message.
1187 : */
1188 2 : RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
1189 2 : early_deadlock = true;
1190 2 : break;
1191 : }
1192 : /* I must go before this waiter. Check special case. */
1193 10 : if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1194 10 : !LockCheckConflicts(lockMethodTable, lockmode, lock,
1195 : proclock))
1196 : {
1197 : /* Skip the wait and just grant myself the lock. */
1198 10 : GrantLock(lock, proclock, lockmode);
1199 10 : return PROC_WAIT_STATUS_OK;
1200 : }
1201 :
1202 : /* Put myself into wait queue before conflicting process */
1203 0 : insert_before = proc;
1204 0 : break;
1205 : }
1206 : /* Nope, so advance to next waiter */
1207 0 : aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
1208 : }
1209 : }
1210 :
1211 : /*
1212 : * If we detected deadlock, give up without waiting. This must agree with
1213 : * CheckDeadLock's recovery code.
1214 : */
1215 4108 : if (early_deadlock)
1216 2 : return PROC_WAIT_STATUS_ERROR;
1217 :
1218 : /*
1219 : * At this point we know that we'd really need to sleep. If we've been
1220 : * commanded not to do that, bail out.
1221 : */
1222 4106 : if (dontWait)
1223 1512 : return PROC_WAIT_STATUS_ERROR;
1224 :
1225 : /*
1226 : * Insert self into queue, at the position determined above.
1227 : */
1228 2594 : if (insert_before)
1229 0 : dclist_insert_before(waitQueue, &insert_before->links, &MyProc->links);
1230 : else
1231 2594 : dclist_push_tail(waitQueue, &MyProc->links);
1232 :
1233 2594 : lock->waitMask |= LOCKBIT_ON(lockmode);
1234 :
1235 : /* Set up wait information in PGPROC object, too */
1236 2594 : MyProc->heldLocks = myProcHeldLocks;
1237 2594 : MyProc->waitLock = lock;
1238 2594 : MyProc->waitProcLock = proclock;
1239 2594 : MyProc->waitLockMode = lockmode;
1240 :
1241 2594 : MyProc->waitStatus = PROC_WAIT_STATUS_WAITING;
1242 :
1243 2594 : return PROC_WAIT_STATUS_WAITING;
1244 : }
1245 :
1246 : /*
1247 : * ProcSleep -- put process to sleep waiting on lock
1248 : *
1249 : * This must be called when JoinWaitQueue() returns PROC_WAIT_STATUS_WAITING.
1250 : * Returns after the lock has been granted, or if a deadlock is detected. Can
1251 : * also bail out with ereport(ERROR), if some other error condition, or a
1252 : * timeout or cancellation is triggered.
1253 : *
1254 : * Result is one of the following:
1255 : *
1256 : * PROC_WAIT_STATUS_OK - lock was granted
1257 : * PROC_WAIT_STATUS_ERROR - a deadlock was detected
1258 : */
1259 : ProcWaitStatus
1260 2594 : ProcSleep(LOCALLOCK *locallock)
1261 : {
1262 2594 : LOCKMODE lockmode = locallock->tag.mode;
1263 2594 : LOCK *lock = locallock->lock;
1264 2594 : uint32 hashcode = locallock->hashcode;
1265 2594 : LWLock *partitionLock = LockHashPartitionLock(hashcode);
1266 2594 : TimestampTz standbyWaitStart = 0;
1267 2594 : bool allow_autovacuum_cancel = true;
1268 2594 : bool logged_recovery_conflict = false;
1269 : ProcWaitStatus myWaitStatus;
1270 :
1271 : /* The caller must've armed the on-error cleanup mechanism */
1272 : Assert(GetAwaitedLock() == locallock);
1273 : Assert(!LWLockHeldByMe(partitionLock));
1274 :
1275 : /*
1276 : * Now that we will successfully clean up after an ereport, it's safe to
1277 : * check to see if there's a buffer pin deadlock against the Startup
1278 : * process. Of course, that's only necessary if we're doing Hot Standby
1279 : * and are not the Startup process ourselves.
1280 : */
1281 2594 : if (RecoveryInProgress() && !InRecovery)
1282 2 : CheckRecoveryConflictDeadlock();
1283 :
1284 : /* Reset deadlock_state before enabling the timeout handler */
1285 2594 : deadlock_state = DS_NOT_YET_CHECKED;
1286 2594 : got_deadlock_timeout = false;
1287 :
1288 : /*
1289 : * Set timer so we can wake up after awhile and check for a deadlock. If a
1290 : * deadlock is detected, the handler sets MyProc->waitStatus =
1291 : * PROC_WAIT_STATUS_ERROR, allowing us to know that we must report failure
1292 : * rather than success.
1293 : *
1294 : * By delaying the check until we've waited for a bit, we can avoid
1295 : * running the rather expensive deadlock-check code in most cases.
1296 : *
1297 : * If LockTimeout is set, also enable the timeout for that. We can save a
1298 : * few cycles by enabling both timeout sources in one call.
1299 : *
1300 : * If InHotStandby we set lock waits slightly later for clarity with other
1301 : * code.
1302 : */
1303 2594 : if (!InHotStandby)
1304 : {
1305 2592 : if (LockTimeout > 0)
1306 : {
1307 : EnableTimeoutParams timeouts[2];
1308 :
1309 206 : timeouts[0].id = DEADLOCK_TIMEOUT;
1310 206 : timeouts[0].type = TMPARAM_AFTER;
1311 206 : timeouts[0].delay_ms = DeadlockTimeout;
1312 206 : timeouts[1].id = LOCK_TIMEOUT;
1313 206 : timeouts[1].type = TMPARAM_AFTER;
1314 206 : timeouts[1].delay_ms = LockTimeout;
1315 206 : enable_timeouts(timeouts, 2);
1316 : }
1317 : else
1318 2386 : enable_timeout_after(DEADLOCK_TIMEOUT, DeadlockTimeout);
1319 :
1320 : /*
1321 : * Use the current time obtained for the deadlock timeout timer as
1322 : * waitStart (i.e., the time when this process started waiting for the
1323 : * lock). Since getting the current time newly can cause overhead, we
1324 : * reuse the already-obtained time to avoid that overhead.
1325 : *
1326 : * Note that waitStart is updated without holding the lock table's
1327 : * partition lock, to avoid the overhead by additional lock
1328 : * acquisition. This can cause "waitstart" in pg_locks to become NULL
1329 : * for a very short period of time after the wait started even though
1330 : * "granted" is false. This is OK in practice because we can assume
1331 : * that users are likely to look at "waitstart" when waiting for the
1332 : * lock for a long time.
1333 : */
1334 2592 : pg_atomic_write_u64(&MyProc->waitStart,
1335 2592 : get_timeout_start_time(DEADLOCK_TIMEOUT));
1336 : }
1337 2 : else if (log_recovery_conflict_waits)
1338 : {
1339 : /*
1340 : * Set the wait start timestamp if logging is enabled and in hot
1341 : * standby.
1342 : */
1343 2 : standbyWaitStart = GetCurrentTimestamp();
1344 : }
1345 :
1346 : /*
1347 : * If somebody wakes us between LWLockRelease and WaitLatch, the latch
1348 : * will not wait. But a set latch does not necessarily mean that the lock
1349 : * is free now, as there are many other sources for latch sets than
1350 : * somebody releasing the lock.
1351 : *
1352 : * We process interrupts whenever the latch has been set, so cancel/die
1353 : * interrupts are processed quickly. This means we must not mind losing
1354 : * control to a cancel/die interrupt here. We don't, because we have no
1355 : * shared-state-change work to do after being granted the lock (the
1356 : * grantor did it all). We do have to worry about canceling the deadlock
1357 : * timeout and updating the locallock table, but if we lose control to an
1358 : * error, LockErrorCleanup will fix that up.
1359 : */
1360 : do
1361 : {
1362 5600 : if (InHotStandby)
1363 : {
1364 8 : bool maybe_log_conflict =
1365 8 : (standbyWaitStart != 0 && !logged_recovery_conflict);
1366 :
1367 : /* Set a timer and wait for that or for the lock to be granted */
1368 8 : ResolveRecoveryConflictWithLock(locallock->tag.lock,
1369 : maybe_log_conflict);
1370 :
1371 : /*
1372 : * Emit the log message if the startup process is waiting longer
1373 : * than deadlock_timeout for recovery conflict on lock.
1374 : */
1375 8 : if (maybe_log_conflict)
1376 : {
1377 4 : TimestampTz now = GetCurrentTimestamp();
1378 :
1379 4 : if (TimestampDifferenceExceeds(standbyWaitStart, now,
1380 : DeadlockTimeout))
1381 : {
1382 : VirtualTransactionId *vxids;
1383 : int cnt;
1384 :
1385 2 : vxids = GetLockConflicts(&locallock->tag.lock,
1386 : AccessExclusiveLock, &cnt);
1387 :
1388 : /*
1389 : * Log the recovery conflict and the list of PIDs of
1390 : * backends holding the conflicting lock. Note that we do
1391 : * logging even if there are no such backends right now
1392 : * because the startup process here has already waited
1393 : * longer than deadlock_timeout.
1394 : */
1395 2 : LogRecoveryConflict(PROCSIG_RECOVERY_CONFLICT_LOCK,
1396 : standbyWaitStart, now,
1397 2 : cnt > 0 ? vxids : NULL, true);
1398 2 : logged_recovery_conflict = true;
1399 : }
1400 : }
1401 : }
1402 : else
1403 : {
1404 5592 : (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
1405 5592 : PG_WAIT_LOCK | locallock->tag.lock.locktag_type);
1406 5592 : ResetLatch(MyLatch);
1407 : /* check for deadlocks first, as that's probably log-worthy */
1408 5592 : if (got_deadlock_timeout)
1409 : {
1410 56 : CheckDeadLock();
1411 56 : got_deadlock_timeout = false;
1412 : }
1413 5592 : CHECK_FOR_INTERRUPTS();
1414 : }
1415 :
1416 : /*
1417 : * waitStatus could change from PROC_WAIT_STATUS_WAITING to something
1418 : * else asynchronously. Read it just once per loop to prevent
1419 : * surprising behavior (such as missing log messages).
1420 : */
1421 5520 : myWaitStatus = *((volatile ProcWaitStatus *) &MyProc->waitStatus);
1422 :
1423 : /*
1424 : * If we are not deadlocked, but are waiting on an autovacuum-induced
1425 : * task, send a signal to interrupt it.
1426 : */
1427 5520 : if (deadlock_state == DS_BLOCKED_BY_AUTOVACUUM && allow_autovacuum_cancel)
1428 : {
1429 0 : PGPROC *autovac = GetBlockingAutoVacuumPgproc();
1430 : uint8 statusFlags;
1431 : uint8 lockmethod_copy;
1432 : LOCKTAG locktag_copy;
1433 :
1434 : /*
1435 : * Grab info we need, then release lock immediately. Note this
1436 : * coding means that there is a tiny chance that the process
1437 : * terminates its current transaction and starts a different one
1438 : * before we have a change to send the signal; the worst possible
1439 : * consequence is that a for-wraparound vacuum is canceled. But
1440 : * that could happen in any case unless we were to do kill() with
1441 : * the lock held, which is much more undesirable.
1442 : */
1443 0 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1444 0 : statusFlags = ProcGlobal->statusFlags[autovac->pgxactoff];
1445 0 : lockmethod_copy = lock->tag.locktag_lockmethodid;
1446 0 : locktag_copy = lock->tag;
1447 0 : LWLockRelease(ProcArrayLock);
1448 :
1449 : /*
1450 : * Only do it if the worker is not working to protect against Xid
1451 : * wraparound.
1452 : */
1453 0 : if ((statusFlags & PROC_IS_AUTOVACUUM) &&
1454 0 : !(statusFlags & PROC_VACUUM_FOR_WRAPAROUND))
1455 : {
1456 0 : int pid = autovac->pid;
1457 :
1458 : /* report the case, if configured to do so */
1459 0 : if (message_level_is_interesting(DEBUG1))
1460 : {
1461 : StringInfoData locktagbuf;
1462 : StringInfoData logbuf; /* errdetail for server log */
1463 :
1464 0 : initStringInfo(&locktagbuf);
1465 0 : initStringInfo(&logbuf);
1466 0 : DescribeLockTag(&locktagbuf, &locktag_copy);
1467 0 : appendStringInfo(&logbuf,
1468 : "Process %d waits for %s on %s.",
1469 : MyProcPid,
1470 : GetLockmodeName(lockmethod_copy, lockmode),
1471 : locktagbuf.data);
1472 :
1473 0 : ereport(DEBUG1,
1474 : (errmsg_internal("sending cancel to blocking autovacuum PID %d",
1475 : pid),
1476 : errdetail_log("%s", logbuf.data)));
1477 :
1478 0 : pfree(locktagbuf.data);
1479 0 : pfree(logbuf.data);
1480 : }
1481 :
1482 : /* send the autovacuum worker Back to Old Kent Road */
1483 0 : if (kill(pid, SIGINT) < 0)
1484 : {
1485 : /*
1486 : * There's a race condition here: once we release the
1487 : * ProcArrayLock, it's possible for the autovac worker to
1488 : * close up shop and exit before we can do the kill().
1489 : * Therefore, we do not whinge about no-such-process.
1490 : * Other errors such as EPERM could conceivably happen if
1491 : * the kernel recycles the PID fast enough, but such cases
1492 : * seem improbable enough that it's probably best to issue
1493 : * a warning if we see some other errno.
1494 : */
1495 0 : if (errno != ESRCH)
1496 0 : ereport(WARNING,
1497 : (errmsg("could not send signal to process %d: %m",
1498 : pid)));
1499 : }
1500 : }
1501 :
1502 : /* prevent signal from being sent again more than once */
1503 0 : allow_autovacuum_cancel = false;
1504 : }
1505 :
1506 : /*
1507 : * If awoken after the deadlock check interrupt has run, and
1508 : * log_lock_waits is on, then report about the wait.
1509 : */
1510 5520 : if (log_lock_waits && deadlock_state != DS_NOT_YET_CHECKED)
1511 : {
1512 : StringInfoData buf,
1513 : lock_waiters_sbuf,
1514 : lock_holders_sbuf;
1515 : const char *modename;
1516 : long secs;
1517 : int usecs;
1518 : long msecs;
1519 38 : int lockHoldersNum = 0;
1520 :
1521 38 : initStringInfo(&buf);
1522 38 : initStringInfo(&lock_waiters_sbuf);
1523 38 : initStringInfo(&lock_holders_sbuf);
1524 :
1525 38 : DescribeLockTag(&buf, &locallock->tag.lock);
1526 38 : modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
1527 : lockmode);
1528 38 : TimestampDifference(get_timeout_start_time(DEADLOCK_TIMEOUT),
1529 : GetCurrentTimestamp(),
1530 : &secs, &usecs);
1531 38 : msecs = secs * 1000 + usecs / 1000;
1532 38 : usecs = usecs % 1000;
1533 :
1534 : /* Gather a list of all lock holders and waiters */
1535 38 : LWLockAcquire(partitionLock, LW_SHARED);
1536 38 : GetLockHoldersAndWaiters(locallock, &lock_holders_sbuf,
1537 : &lock_waiters_sbuf, &lockHoldersNum);
1538 38 : LWLockRelease(partitionLock);
1539 :
1540 38 : if (deadlock_state == DS_SOFT_DEADLOCK)
1541 6 : ereport(LOG,
1542 : (errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
1543 : MyProcPid, modename, buf.data, msecs, usecs),
1544 : (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1545 : "Processes holding the lock: %s. Wait queue: %s.",
1546 : lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1547 32 : else if (deadlock_state == DS_HARD_DEADLOCK)
1548 : {
1549 : /*
1550 : * This message is a bit redundant with the error that will be
1551 : * reported subsequently, but in some cases the error report
1552 : * might not make it to the log (eg, if it's caught by an
1553 : * exception handler), and we want to ensure all long-wait
1554 : * events get logged.
1555 : */
1556 4 : ereport(LOG,
1557 : (errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
1558 : MyProcPid, modename, buf.data, msecs, usecs),
1559 : (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1560 : "Processes holding the lock: %s. Wait queue: %s.",
1561 : lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1562 : }
1563 :
1564 38 : if (myWaitStatus == PROC_WAIT_STATUS_WAITING)
1565 18 : ereport(LOG,
1566 : (errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
1567 : MyProcPid, modename, buf.data, msecs, usecs),
1568 : (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1569 : "Processes holding the lock: %s. Wait queue: %s.",
1570 : lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1571 20 : else if (myWaitStatus == PROC_WAIT_STATUS_OK)
1572 16 : ereport(LOG,
1573 : (errmsg("process %d acquired %s on %s after %ld.%03d ms",
1574 : MyProcPid, modename, buf.data, msecs, usecs)));
1575 : else
1576 : {
1577 : Assert(myWaitStatus == PROC_WAIT_STATUS_ERROR);
1578 :
1579 : /*
1580 : * Currently, the deadlock checker always kicks its own
1581 : * process, which means that we'll only see
1582 : * PROC_WAIT_STATUS_ERROR when deadlock_state ==
1583 : * DS_HARD_DEADLOCK, and there's no need to print redundant
1584 : * messages. But for completeness and future-proofing, print
1585 : * a message if it looks like someone else kicked us off the
1586 : * lock.
1587 : */
1588 4 : if (deadlock_state != DS_HARD_DEADLOCK)
1589 0 : ereport(LOG,
1590 : (errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
1591 : MyProcPid, modename, buf.data, msecs, usecs),
1592 : (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1593 : "Processes holding the lock: %s. Wait queue: %s.",
1594 : lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1595 : }
1596 :
1597 : /*
1598 : * At this point we might still need to wait for the lock. Reset
1599 : * state so we don't print the above messages again.
1600 : */
1601 38 : deadlock_state = DS_NO_DEADLOCK;
1602 :
1603 38 : pfree(buf.data);
1604 38 : pfree(lock_holders_sbuf.data);
1605 38 : pfree(lock_waiters_sbuf.data);
1606 : }
1607 5520 : } while (myWaitStatus == PROC_WAIT_STATUS_WAITING);
1608 :
1609 : /*
1610 : * Disable the timers, if they are still running. As in LockErrorCleanup,
1611 : * we must preserve the LOCK_TIMEOUT indicator flag: if a lock timeout has
1612 : * already caused QueryCancelPending to become set, we want the cancel to
1613 : * be reported as a lock timeout, not a user cancel.
1614 : */
1615 2514 : if (!InHotStandby)
1616 : {
1617 2512 : if (LockTimeout > 0)
1618 : {
1619 : DisableTimeoutParams timeouts[2];
1620 :
1621 194 : timeouts[0].id = DEADLOCK_TIMEOUT;
1622 194 : timeouts[0].keep_indicator = false;
1623 194 : timeouts[1].id = LOCK_TIMEOUT;
1624 194 : timeouts[1].keep_indicator = true;
1625 194 : disable_timeouts(timeouts, 2);
1626 : }
1627 : else
1628 2318 : disable_timeout(DEADLOCK_TIMEOUT, false);
1629 : }
1630 :
1631 : /*
1632 : * Emit the log message if recovery conflict on lock was resolved but the
1633 : * startup process waited longer than deadlock_timeout for it.
1634 : */
1635 2514 : if (InHotStandby && logged_recovery_conflict)
1636 2 : LogRecoveryConflict(PROCSIG_RECOVERY_CONFLICT_LOCK,
1637 : standbyWaitStart, GetCurrentTimestamp(),
1638 : NULL, false);
1639 :
1640 : /*
1641 : * We don't have to do anything else, because the awaker did all the
1642 : * necessary updates of the lock table and MyProc. (The caller is
1643 : * responsible for updating the local lock table.)
1644 : */
1645 2514 : return myWaitStatus;
1646 : }
1647 :
1648 :
1649 : /*
1650 : * ProcWakeup -- wake up a process by setting its latch.
1651 : *
1652 : * Also remove the process from the wait queue and set its links invalid.
1653 : *
1654 : * The appropriate lock partition lock must be held by caller.
1655 : *
1656 : * XXX: presently, this code is only used for the "success" case, and only
1657 : * works correctly for that case. To clean up in failure case, would need
1658 : * to twiddle the lock's request counts too --- see RemoveFromWaitQueue.
1659 : * Hence, in practice the waitStatus parameter must be PROC_WAIT_STATUS_OK.
1660 : */
1661 : void
1662 2530 : ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus)
1663 : {
1664 2530 : if (dlist_node_is_detached(&proc->links))
1665 0 : return;
1666 :
1667 : Assert(proc->waitStatus == PROC_WAIT_STATUS_WAITING);
1668 :
1669 : /* Remove process from wait queue */
1670 2530 : dclist_delete_from_thoroughly(&proc->waitLock->waitProcs, &proc->links);
1671 :
1672 : /* Clean up process' state and pass it the ok/fail signal */
1673 2530 : proc->waitLock = NULL;
1674 2530 : proc->waitProcLock = NULL;
1675 2530 : proc->waitStatus = waitStatus;
1676 2530 : pg_atomic_write_u64(&MyProc->waitStart, 0);
1677 :
1678 : /* And awaken it */
1679 2530 : SetLatch(&proc->procLatch);
1680 : }
1681 :
1682 : /*
1683 : * ProcLockWakeup -- routine for waking up processes when a lock is
1684 : * released (or a prior waiter is aborted). Scan all waiters
1685 : * for lock, waken any that are no longer blocked.
1686 : *
1687 : * The appropriate lock partition lock must be held by caller.
1688 : */
1689 : void
1690 2580 : ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
1691 : {
1692 2580 : dclist_head *waitQueue = &lock->waitProcs;
1693 2580 : LOCKMASK aheadRequests = 0;
1694 : dlist_mutable_iter miter;
1695 :
1696 2580 : if (dclist_is_empty(waitQueue))
1697 90 : return;
1698 :
1699 6266 : dclist_foreach_modify(miter, waitQueue)
1700 : {
1701 3776 : PGPROC *proc = dlist_container(PGPROC, links, miter.cur);
1702 3776 : LOCKMODE lockmode = proc->waitLockMode;
1703 :
1704 : /*
1705 : * Waken if (a) doesn't conflict with requests of earlier waiters, and
1706 : * (b) doesn't conflict with already-held locks.
1707 : */
1708 3776 : if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1709 3076 : !LockCheckConflicts(lockMethodTable, lockmode, lock,
1710 : proc->waitProcLock))
1711 : {
1712 : /* OK to waken */
1713 2530 : GrantLock(lock, proc->waitProcLock, lockmode);
1714 : /* removes proc from the lock's waiting process queue */
1715 2530 : ProcWakeup(proc, PROC_WAIT_STATUS_OK);
1716 : }
1717 : else
1718 : {
1719 : /*
1720 : * Lock conflicts: Don't wake, but remember requested mode for
1721 : * later checks.
1722 : */
1723 1246 : aheadRequests |= LOCKBIT_ON(lockmode);
1724 : }
1725 : }
1726 : }
1727 :
1728 : /*
1729 : * CheckDeadLock
1730 : *
1731 : * We only get to this routine, if DEADLOCK_TIMEOUT fired while waiting for a
1732 : * lock to be released by some other process. Check if there's a deadlock; if
1733 : * not, just return. (But signal ProcSleep to log a message, if
1734 : * log_lock_waits is true.) If we have a real deadlock, remove ourselves from
1735 : * the lock's wait queue and signal an error to ProcSleep.
1736 : */
1737 : static void
1738 56 : CheckDeadLock(void)
1739 : {
1740 : int i;
1741 :
1742 : /*
1743 : * Acquire exclusive lock on the entire shared lock data structures. Must
1744 : * grab LWLocks in partition-number order to avoid LWLock deadlock.
1745 : *
1746 : * Note that the deadlock check interrupt had better not be enabled
1747 : * anywhere that this process itself holds lock partition locks, else this
1748 : * will wait forever. Also note that LWLockAcquire creates a critical
1749 : * section, so that this routine cannot be interrupted by cancel/die
1750 : * interrupts.
1751 : */
1752 952 : for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1753 896 : LWLockAcquire(LockHashPartitionLockByIndex(i), LW_EXCLUSIVE);
1754 :
1755 : /*
1756 : * Check to see if we've been awoken by anyone in the interim.
1757 : *
1758 : * If we have, we can return and resume our transaction -- happy day.
1759 : * Before we are awoken the process releasing the lock grants it to us so
1760 : * we know that we don't have to wait anymore.
1761 : *
1762 : * We check by looking to see if we've been unlinked from the wait queue.
1763 : * This is safe because we hold the lock partition lock.
1764 : */
1765 56 : if (MyProc->links.prev == NULL ||
1766 56 : MyProc->links.next == NULL)
1767 0 : goto check_done;
1768 :
1769 : #ifdef LOCK_DEBUG
1770 : if (Debug_deadlocks)
1771 : DumpAllLocks();
1772 : #endif
1773 :
1774 : /* Run the deadlock check, and set deadlock_state for use by ProcSleep */
1775 56 : deadlock_state = DeadLockCheck(MyProc);
1776 :
1777 56 : if (deadlock_state == DS_HARD_DEADLOCK)
1778 : {
1779 : /*
1780 : * Oops. We have a deadlock.
1781 : *
1782 : * Get this process out of wait state. (Note: we could do this more
1783 : * efficiently by relying on lockAwaited, but use this coding to
1784 : * preserve the flexibility to kill some other transaction than the
1785 : * one detecting the deadlock.)
1786 : *
1787 : * RemoveFromWaitQueue sets MyProc->waitStatus to
1788 : * PROC_WAIT_STATUS_ERROR, so ProcSleep will report an error after we
1789 : * return from the signal handler.
1790 : */
1791 : Assert(MyProc->waitLock != NULL);
1792 10 : RemoveFromWaitQueue(MyProc, LockTagHashCode(&(MyProc->waitLock->tag)));
1793 :
1794 : /*
1795 : * We're done here. Transaction abort caused by the error that
1796 : * ProcSleep will raise will cause any other locks we hold to be
1797 : * released, thus allowing other processes to wake up; we don't need
1798 : * to do that here. NOTE: an exception is that releasing locks we
1799 : * hold doesn't consider the possibility of waiters that were blocked
1800 : * behind us on the lock we just failed to get, and might now be
1801 : * wakable because we're not in front of them anymore. However,
1802 : * RemoveFromWaitQueue took care of waking up any such processes.
1803 : */
1804 : }
1805 :
1806 : /*
1807 : * And release locks. We do this in reverse order for two reasons: (1)
1808 : * Anyone else who needs more than one of the locks will be trying to lock
1809 : * them in increasing order; we don't want to release the other process
1810 : * until it can get all the locks it needs. (2) This avoids O(N^2)
1811 : * behavior inside LWLockRelease.
1812 : */
1813 46 : check_done:
1814 952 : for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
1815 896 : LWLockRelease(LockHashPartitionLockByIndex(i));
1816 56 : }
1817 :
1818 : /*
1819 : * CheckDeadLockAlert - Handle the expiry of deadlock_timeout.
1820 : *
1821 : * NB: Runs inside a signal handler, be careful.
1822 : */
1823 : void
1824 56 : CheckDeadLockAlert(void)
1825 : {
1826 56 : int save_errno = errno;
1827 :
1828 56 : got_deadlock_timeout = true;
1829 :
1830 : /*
1831 : * Have to set the latch again, even if handle_sig_alarm already did. Back
1832 : * then got_deadlock_timeout wasn't yet set... It's unlikely that this
1833 : * ever would be a problem, but setting a set latch again is cheap.
1834 : *
1835 : * Note that, when this function runs inside procsignal_sigusr1_handler(),
1836 : * the handler function sets the latch again after the latch is set here.
1837 : */
1838 56 : SetLatch(MyLatch);
1839 56 : errno = save_errno;
1840 56 : }
1841 :
1842 : /*
1843 : * GetLockHoldersAndWaiters - get lock holders and waiters for a lock
1844 : *
1845 : * Fill lock_holders_sbuf and lock_waiters_sbuf with the PIDs of processes holding
1846 : * and waiting for the lock, and set lockHoldersNum to the number of lock holders.
1847 : *
1848 : * The lock table's partition lock must be held on entry and remains held on exit.
1849 : */
1850 : void
1851 38 : GetLockHoldersAndWaiters(LOCALLOCK *locallock, StringInfo lock_holders_sbuf,
1852 : StringInfo lock_waiters_sbuf, int *lockHoldersNum)
1853 : {
1854 : dlist_iter proc_iter;
1855 : PROCLOCK *curproclock;
1856 38 : LOCK *lock = locallock->lock;
1857 38 : bool first_holder = true,
1858 38 : first_waiter = true;
1859 :
1860 : #ifdef USE_ASSERT_CHECKING
1861 : {
1862 : uint32 hashcode = locallock->hashcode;
1863 : LWLock *partitionLock = LockHashPartitionLock(hashcode);
1864 :
1865 : Assert(LWLockHeldByMe(partitionLock));
1866 : }
1867 : #endif
1868 :
1869 38 : *lockHoldersNum = 0;
1870 :
1871 : /*
1872 : * Loop over the lock's procLocks to gather a list of all holders and
1873 : * waiters. Thus we will be able to provide more detailed information for
1874 : * lock debugging purposes.
1875 : *
1876 : * lock->procLocks contains all processes which hold or wait for this
1877 : * lock.
1878 : */
1879 122 : dlist_foreach(proc_iter, &lock->procLocks)
1880 : {
1881 84 : curproclock =
1882 84 : dlist_container(PROCLOCK, lockLink, proc_iter.cur);
1883 :
1884 : /*
1885 : * We are a waiter if myProc->waitProcLock == curproclock; we are a
1886 : * holder if it is NULL or something different.
1887 : */
1888 84 : if (curproclock->tag.myProc->waitProcLock == curproclock)
1889 : {
1890 38 : if (first_waiter)
1891 : {
1892 20 : appendStringInfo(lock_waiters_sbuf, "%d",
1893 20 : curproclock->tag.myProc->pid);
1894 20 : first_waiter = false;
1895 : }
1896 : else
1897 18 : appendStringInfo(lock_waiters_sbuf, ", %d",
1898 18 : curproclock->tag.myProc->pid);
1899 : }
1900 : else
1901 : {
1902 46 : if (first_holder)
1903 : {
1904 38 : appendStringInfo(lock_holders_sbuf, "%d",
1905 38 : curproclock->tag.myProc->pid);
1906 38 : first_holder = false;
1907 : }
1908 : else
1909 8 : appendStringInfo(lock_holders_sbuf, ", %d",
1910 8 : curproclock->tag.myProc->pid);
1911 :
1912 46 : (*lockHoldersNum)++;
1913 : }
1914 : }
1915 38 : }
1916 :
1917 : /*
1918 : * ProcWaitForSignal - wait for a signal from another backend.
1919 : *
1920 : * As this uses the generic process latch the caller has to be robust against
1921 : * unrelated wakeups: Always check that the desired state has occurred, and
1922 : * wait again if not.
1923 : */
1924 : void
1925 36 : ProcWaitForSignal(uint32 wait_event_info)
1926 : {
1927 36 : (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
1928 : wait_event_info);
1929 36 : ResetLatch(MyLatch);
1930 36 : CHECK_FOR_INTERRUPTS();
1931 36 : }
1932 :
1933 : /*
1934 : * ProcSendSignal - set the latch of a backend identified by ProcNumber
1935 : */
1936 : void
1937 8 : ProcSendSignal(ProcNumber procNumber)
1938 : {
1939 8 : if (procNumber < 0 || procNumber >= ProcGlobal->allProcCount)
1940 0 : elog(ERROR, "procNumber out of range");
1941 :
1942 8 : SetLatch(&ProcGlobal->allProcs[procNumber].procLatch);
1943 8 : }
1944 :
1945 : /*
1946 : * BecomeLockGroupLeader - designate process as lock group leader
1947 : *
1948 : * Once this function has returned, other processes can join the lock group
1949 : * by calling BecomeLockGroupMember.
1950 : */
1951 : void
1952 1168 : BecomeLockGroupLeader(void)
1953 : {
1954 : LWLock *leader_lwlock;
1955 :
1956 : /* If we already did it, we don't need to do it again. */
1957 1168 : if (MyProc->lockGroupLeader == MyProc)
1958 1030 : return;
1959 :
1960 : /* We had better not be a follower. */
1961 : Assert(MyProc->lockGroupLeader == NULL);
1962 :
1963 : /* Create single-member group, containing only ourselves. */
1964 138 : leader_lwlock = LockHashPartitionLockByProc(MyProc);
1965 138 : LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
1966 138 : MyProc->lockGroupLeader = MyProc;
1967 138 : dlist_push_head(&MyProc->lockGroupMembers, &MyProc->lockGroupLink);
1968 138 : LWLockRelease(leader_lwlock);
1969 : }
1970 :
1971 : /*
1972 : * BecomeLockGroupMember - designate process as lock group member
1973 : *
1974 : * This is pretty straightforward except for the possibility that the leader
1975 : * whose group we're trying to join might exit before we manage to do so;
1976 : * and the PGPROC might get recycled for an unrelated process. To avoid
1977 : * that, we require the caller to pass the PID of the intended PGPROC as
1978 : * an interlock. Returns true if we successfully join the intended lock
1979 : * group, and false if not.
1980 : */
1981 : bool
1982 2736 : BecomeLockGroupMember(PGPROC *leader, int pid)
1983 : {
1984 : LWLock *leader_lwlock;
1985 2736 : bool ok = false;
1986 :
1987 : /* Group leader can't become member of group */
1988 : Assert(MyProc != leader);
1989 :
1990 : /* Can't already be a member of a group */
1991 : Assert(MyProc->lockGroupLeader == NULL);
1992 :
1993 : /* PID must be valid. */
1994 : Assert(pid != 0);
1995 :
1996 : /*
1997 : * Get lock protecting the group fields. Note LockHashPartitionLockByProc
1998 : * calculates the proc number based on the PGPROC slot without looking at
1999 : * its contents, so we will acquire the correct lock even if the leader
2000 : * PGPROC is in process of being recycled.
2001 : */
2002 2736 : leader_lwlock = LockHashPartitionLockByProc(leader);
2003 2736 : LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
2004 :
2005 : /* Is this the leader we're looking for? */
2006 2736 : if (leader->pid == pid && leader->lockGroupLeader == leader)
2007 : {
2008 : /* OK, join the group */
2009 2736 : ok = true;
2010 2736 : MyProc->lockGroupLeader = leader;
2011 2736 : dlist_push_tail(&leader->lockGroupMembers, &MyProc->lockGroupLink);
2012 : }
2013 2736 : LWLockRelease(leader_lwlock);
2014 :
2015 2736 : return ok;
2016 : }
|