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