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