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