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