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