PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
deadlock.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * deadlock.c
4  * POSTGRES deadlock detection code
5  *
6  * See src/backend/storage/lmgr/README for a description of the deadlock
7  * detection and resolution algorithms.
8  *
9  *
10  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  * src/backend/storage/lmgr/deadlock.c
16  *
17  * Interface:
18  *
19  * DeadLockCheck()
20  * DeadLockReport()
21  * RememberSimpleDeadLock()
22  * InitDeadLockChecking()
23  *
24  *-------------------------------------------------------------------------
25  */
26 #include "postgres.h"
27 
28 #include "miscadmin.h"
29 #include "pg_trace.h"
30 #include "pgstat.h"
31 #include "storage/lmgr.h"
32 #include "storage/proc.h"
33 #include "utils/memutils.h"
34 
35 
36 /*
37  * One edge in the waits-for graph.
38  *
39  * waiter and blocker may or may not be members of a lock group, but if either
40  * is, it will be the leader rather than any other member of the lock group.
41  * The group leaders act as representatives of the whole group even though
42  * those particular processes need not be waiting at all. There will be at
43  * least one member of the waiter's lock group on the wait queue for the given
44  * lock, maybe more.
45  */
46 typedef struct
47 {
48  PGPROC *waiter; /* the leader of the waiting lock group */
49  PGPROC *blocker; /* the leader of the group it is waiting for */
50  LOCK *lock; /* the lock being waited for */
51  int pred; /* workspace for TopoSort */
52  int link; /* workspace for TopoSort */
53 } EDGE;
54 
55 /* One potential reordering of a lock's wait queue */
56 typedef struct
57 {
58  LOCK *lock; /* the lock whose wait queue is described */
59  PGPROC **procs; /* array of PGPROC *'s in new wait order */
60  int nProcs;
61 } WAIT_ORDER;
62 
63 /*
64  * Information saved about each edge in a detected deadlock cycle. This
65  * is used to print a diagnostic message upon failure.
66  *
67  * Note: because we want to examine this info after releasing the lock
68  * manager's partition locks, we can't just store LOCK and PGPROC pointers;
69  * we must extract out all the info we want to be able to print.
70  */
71 typedef struct
72 {
73  LOCKTAG locktag; /* ID of awaited lock object */
74  LOCKMODE lockmode; /* type of lock we're waiting for */
75  int pid; /* PID of blocked backend */
77 
78 
79 static bool DeadLockCheckRecurse(PGPROC *proc);
80 static int TestConfiguration(PGPROC *startProc);
81 static bool FindLockCycle(PGPROC *checkProc,
82  EDGE *softEdges, int *nSoftEdges);
83 static bool FindLockCycleRecurse(PGPROC *checkProc, int depth,
84  EDGE *softEdges, int *nSoftEdges);
85 static bool FindLockCycleRecurseMember(PGPROC *checkProc,
86  PGPROC *checkProcLeader,
87  int depth, EDGE *softEdges, int *nSoftEdges);
88 static bool ExpandConstraints(EDGE *constraints, int nConstraints);
89 static bool TopoSort(LOCK *lock, EDGE *constraints, int nConstraints,
90  PGPROC **ordering);
91 
92 #ifdef DEBUG_DEADLOCK
93 static void PrintLockQueue(LOCK *lock, const char *info);
94 #endif
95 
96 
97 /*
98  * Working space for the deadlock detector
99  */
100 
101 /* Workspace for FindLockCycle */
102 static PGPROC **visitedProcs; /* Array of visited procs */
103 static int nVisitedProcs;
104 
105 /* Workspace for TopoSort */
106 static PGPROC **topoProcs; /* Array of not-yet-output procs */
107 static int *beforeConstraints; /* Counts of remaining before-constraints */
108 static int *afterConstraints; /* List head for after-constraints */
109 
110 /* Output area for ExpandConstraints */
111 static WAIT_ORDER *waitOrders; /* Array of proposed queue rearrangements */
112 static int nWaitOrders;
113 static PGPROC **waitOrderProcs; /* Space for waitOrders queue contents */
114 
115 /* Current list of constraints being considered */
117 static int nCurConstraints;
118 static int maxCurConstraints;
119 
120 /* Storage space for results from FindLockCycle */
125 static int nDeadlockDetails;
126 
127 /* PGPROC pointer of any blocking autovacuum worker found */
129 
130 
131 /*
132  * InitDeadLockChecking -- initialize deadlock checker during backend startup
133  *
134  * This does per-backend initialization of the deadlock checker; primarily,
135  * allocation of working memory for DeadLockCheck. We do this per-backend
136  * since there's no percentage in making the kernel do copy-on-write
137  * inheritance of workspace from the postmaster. We want to allocate the
138  * space at startup because (a) the deadlock checker might be invoked when
139  * there's no free memory left, and (b) the checker is normally run inside a
140  * signal handler, which is a very dangerous place to invoke palloc from.
141  */
142 void
144 {
145  MemoryContext oldcxt;
146 
147  /* Make sure allocations are permanent */
149 
150  /*
151  * FindLockCycle needs at most MaxBackends entries in visitedProcs[] and
152  * deadlockDetails[].
153  */
154  visitedProcs = (PGPROC **) palloc(MaxBackends * sizeof(PGPROC *));
155  deadlockDetails = (DEADLOCK_INFO *) palloc(MaxBackends * sizeof(DEADLOCK_INFO));
156 
157  /*
158  * TopoSort needs to consider at most MaxBackends wait-queue entries, and
159  * it needn't run concurrently with FindLockCycle.
160  */
161  topoProcs = visitedProcs; /* re-use this space */
162  beforeConstraints = (int *) palloc(MaxBackends * sizeof(int));
163  afterConstraints = (int *) palloc(MaxBackends * sizeof(int));
164 
165  /*
166  * We need to consider rearranging at most MaxBackends/2 wait queues
167  * (since it takes at least two waiters in a queue to create a soft edge),
168  * and the expanded form of the wait queues can't involve more than
169  * MaxBackends total waiters.
170  */
171  waitOrders = (WAIT_ORDER *)
172  palloc((MaxBackends / 2) * sizeof(WAIT_ORDER));
173  waitOrderProcs = (PGPROC **) palloc(MaxBackends * sizeof(PGPROC *));
174 
175  /*
176  * Allow at most MaxBackends distinct constraints in a configuration. (Is
177  * this enough? In practice it seems it should be, but I don't quite see
178  * how to prove it. If we run out, we might fail to find a workable wait
179  * queue rearrangement even though one exists.) NOTE that this number
180  * limits the maximum recursion depth of DeadLockCheckRecurse. Making it
181  * really big might potentially allow a stack-overflow problem.
182  */
184  curConstraints = (EDGE *) palloc(maxCurConstraints * sizeof(EDGE));
185 
186  /*
187  * Allow up to 3*MaxBackends constraints to be saved without having to
188  * re-run TestConfiguration. (This is probably more than enough, but we
189  * can survive if we run low on space by doing excess runs of
190  * TestConfiguration to re-compute constraint lists each time needed.) The
191  * last MaxBackends entries in possibleConstraints[] are reserved as
192  * output workspace for FindLockCycle.
193  */
195  possibleConstraints =
196  (EDGE *) palloc(maxPossibleConstraints * sizeof(EDGE));
197 
198  MemoryContextSwitchTo(oldcxt);
199 }
200 
201 /*
202  * DeadLockCheck -- Checks for deadlocks for a given process
203  *
204  * This code looks for deadlocks involving the given process. If any
205  * are found, it tries to rearrange lock wait queues to resolve the
206  * deadlock. If resolution is impossible, return DS_HARD_DEADLOCK ---
207  * the caller is then expected to abort the given proc's transaction.
208  *
209  * Caller must already have locked all partitions of the lock tables.
210  *
211  * On failure, deadlock details are recorded in deadlockDetails[] for
212  * subsequent printing by DeadLockReport(). That activity is separate
213  * because (a) we don't want to do it while holding all those LWLocks,
214  * and (b) we are typically invoked inside a signal handler.
215  */
218 {
219  int i,
220  j;
221 
222  /* Initialize to "no constraints" */
223  nCurConstraints = 0;
225  nWaitOrders = 0;
226 
227  /* Initialize to not blocked by an autovacuum worker */
228  blocking_autovacuum_proc = NULL;
229 
230  /* Search for deadlocks and possible fixes */
231  if (DeadLockCheckRecurse(proc))
232  {
233  /*
234  * Call FindLockCycle one more time, to record the correct
235  * deadlockDetails[] for the basic state with no rearrangements.
236  */
237  int nSoftEdges;
238 
239  TRACE_POSTGRESQL_DEADLOCK_FOUND();
240 
241  nWaitOrders = 0;
242  if (!FindLockCycle(proc, possibleConstraints, &nSoftEdges))
243  elog(FATAL, "deadlock seems to have disappeared");
244 
245  return DS_HARD_DEADLOCK; /* cannot find a non-deadlocked state */
246  }
247 
248  /* Apply any needed rearrangements of wait queues */
249  for (i = 0; i < nWaitOrders; i++)
250  {
251  LOCK *lock = waitOrders[i].lock;
252  PGPROC **procs = waitOrders[i].procs;
253  int nProcs = waitOrders[i].nProcs;
254  PROC_QUEUE *waitQueue = &(lock->waitProcs);
255 
256  Assert(nProcs == waitQueue->size);
257 
258 #ifdef DEBUG_DEADLOCK
259  PrintLockQueue(lock, "DeadLockCheck:");
260 #endif
261 
262  /* Reset the queue and re-add procs in the desired order */
263  ProcQueueInit(waitQueue);
264  for (j = 0; j < nProcs; j++)
265  {
266  SHMQueueInsertBefore(&(waitQueue->links), &(procs[j]->links));
267  waitQueue->size++;
268  }
269 
270 #ifdef DEBUG_DEADLOCK
271  PrintLockQueue(lock, "rearranged to:");
272 #endif
273 
274  /* See if any waiters for the lock can be woken up now */
275  ProcLockWakeup(GetLocksMethodTable(lock), lock);
276  }
277 
278  /* Return code tells caller if we had to escape a deadlock or not */
279  if (nWaitOrders > 0)
280  return DS_SOFT_DEADLOCK;
281  else if (blocking_autovacuum_proc != NULL)
283  else
284  return DS_NO_DEADLOCK;
285 }
286 
287 /*
288  * Return the PGPROC of the autovacuum that's blocking a process.
289  *
290  * We reset the saved pointer as soon as we pass it back.
291  */
292 PGPROC *
294 {
295  PGPROC *ptr;
296 
298  blocking_autovacuum_proc = NULL;
299 
300  return ptr;
301 }
302 
303 /*
304  * DeadLockCheckRecurse -- recursively search for valid orderings
305  *
306  * curConstraints[] holds the current set of constraints being considered
307  * by an outer level of recursion. Add to this each possible solution
308  * constraint for any cycle detected at this level.
309  *
310  * Returns TRUE if no solution exists. Returns FALSE if a deadlock-free
311  * state is attainable, in which case waitOrders[] shows the required
312  * rearrangements of lock wait queues (if any).
313  */
314 static bool
316 {
317  int nEdges;
318  int oldPossibleConstraints;
319  bool savedList;
320  int i;
321 
322  nEdges = TestConfiguration(proc);
323  if (nEdges < 0)
324  return true; /* hard deadlock --- no solution */
325  if (nEdges == 0)
326  return false; /* good configuration found */
328  return true; /* out of room for active constraints? */
329  oldPossibleConstraints = nPossibleConstraints;
331  {
332  /* We can save the edge list in possibleConstraints[] */
333  nPossibleConstraints += nEdges;
334  savedList = true;
335  }
336  else
337  {
338  /* Not room; will need to regenerate the edges on-the-fly */
339  savedList = false;
340  }
341 
342  /*
343  * Try each available soft edge as an addition to the configuration.
344  */
345  for (i = 0; i < nEdges; i++)
346  {
347  if (!savedList && i > 0)
348  {
349  /* Regenerate the list of possible added constraints */
350  if (nEdges != TestConfiguration(proc))
351  elog(FATAL, "inconsistent results during deadlock check");
352  }
353  curConstraints[nCurConstraints] =
354  possibleConstraints[oldPossibleConstraints + i];
355  nCurConstraints++;
356  if (!DeadLockCheckRecurse(proc))
357  return false; /* found a valid solution! */
358  /* give up on that added constraint, try again */
359  nCurConstraints--;
360  }
361  nPossibleConstraints = oldPossibleConstraints;
362  return true; /* no solution found */
363 }
364 
365 
366 /*--------------------
367  * Test a configuration (current set of constraints) for validity.
368  *
369  * Returns:
370  * 0: the configuration is good (no deadlocks)
371  * -1: the configuration has a hard deadlock or is not self-consistent
372  * >0: the configuration has one or more soft deadlocks
373  *
374  * In the soft-deadlock case, one of the soft cycles is chosen arbitrarily
375  * and a list of its soft edges is returned beginning at
376  * possibleConstraints+nPossibleConstraints. The return value is the
377  * number of soft edges.
378  *--------------------
379  */
380 static int
382 {
383  int softFound = 0;
384  EDGE *softEdges = possibleConstraints + nPossibleConstraints;
385  int nSoftEdges;
386  int i;
387 
388  /*
389  * Make sure we have room for FindLockCycle's output.
390  */
391  if (nPossibleConstraints + MaxBackends > maxPossibleConstraints)
392  return -1;
393 
394  /*
395  * Expand current constraint set into wait orderings. Fail if the
396  * constraint set is not self-consistent.
397  */
398  if (!ExpandConstraints(curConstraints, nCurConstraints))
399  return -1;
400 
401  /*
402  * Check for cycles involving startProc or any of the procs mentioned in
403  * constraints. We check startProc last because if it has a soft cycle
404  * still to be dealt with, we want to deal with that first.
405  */
406  for (i = 0; i < nCurConstraints; i++)
407  {
408  if (FindLockCycle(curConstraints[i].waiter, softEdges, &nSoftEdges))
409  {
410  if (nSoftEdges == 0)
411  return -1; /* hard deadlock detected */
412  softFound = nSoftEdges;
413  }
414  if (FindLockCycle(curConstraints[i].blocker, softEdges, &nSoftEdges))
415  {
416  if (nSoftEdges == 0)
417  return -1; /* hard deadlock detected */
418  softFound = nSoftEdges;
419  }
420  }
421  if (FindLockCycle(startProc, softEdges, &nSoftEdges))
422  {
423  if (nSoftEdges == 0)
424  return -1; /* hard deadlock detected */
425  softFound = nSoftEdges;
426  }
427  return softFound;
428 }
429 
430 
431 /*
432  * FindLockCycle -- basic check for deadlock cycles
433  *
434  * Scan outward from the given proc to see if there is a cycle in the
435  * waits-for graph that includes this proc. Return TRUE if a cycle
436  * is found, else FALSE. If a cycle is found, we return a list of
437  * the "soft edges", if any, included in the cycle. These edges could
438  * potentially be eliminated by rearranging wait queues. We also fill
439  * deadlockDetails[] with information about the detected cycle; this info
440  * is not used by the deadlock algorithm itself, only to print a useful
441  * message after failing.
442  *
443  * Since we need to be able to check hypothetical configurations that would
444  * exist after wait queue rearrangement, the routine pays attention to the
445  * table of hypothetical queue orders in waitOrders[]. These orders will
446  * be believed in preference to the actual ordering seen in the locktable.
447  */
448 static bool
450  EDGE *softEdges, /* output argument */
451  int *nSoftEdges) /* output argument */
452 {
453  nVisitedProcs = 0;
454  nDeadlockDetails = 0;
455  *nSoftEdges = 0;
456  return FindLockCycleRecurse(checkProc, 0, softEdges, nSoftEdges);
457 }
458 
459 static bool
461  int depth,
462  EDGE *softEdges, /* output argument */
463  int *nSoftEdges) /* output argument */
464 {
465  int i;
466  dlist_iter iter;
467 
468  /*
469  * If this process is a lock group member, check the leader instead. (Note
470  * that we might be the leader, in which case this is a no-op.)
471  */
472  if (checkProc->lockGroupLeader != NULL)
473  checkProc = checkProc->lockGroupLeader;
474 
475  /*
476  * Have we already seen this proc?
477  */
478  for (i = 0; i < nVisitedProcs; i++)
479  {
480  if (visitedProcs[i] == checkProc)
481  {
482  /* If we return to starting point, we have a deadlock cycle */
483  if (i == 0)
484  {
485  /*
486  * record total length of cycle --- outer levels will now fill
487  * deadlockDetails[]
488  */
489  Assert(depth <= MaxBackends);
490  nDeadlockDetails = depth;
491 
492  return true;
493  }
494 
495  /*
496  * Otherwise, we have a cycle but it does not include the start
497  * point, so say "no deadlock".
498  */
499  return false;
500  }
501  }
502  /* Mark proc as seen */
503  Assert(nVisitedProcs < MaxBackends);
504  visitedProcs[nVisitedProcs++] = checkProc;
505 
506  /*
507  * If the process is waiting, there is an outgoing waits-for edge to each
508  * process that blocks it.
509  */
510  if (checkProc->links.next != NULL && checkProc->waitLock != NULL &&
511  FindLockCycleRecurseMember(checkProc, checkProc, depth, softEdges,
512  nSoftEdges))
513  return true;
514 
515  /*
516  * If the process is not waiting, there could still be outgoing waits-for
517  * edges if it is part of a lock group, because other members of the lock
518  * group might be waiting even though this process is not. (Given lock
519  * groups {A1, A2} and {B1, B2}, if A1 waits for B1 and B2 waits for A2,
520  * that is a deadlock even neither of B1 and A2 are waiting for anything.)
521  */
522  dlist_foreach(iter, &checkProc->lockGroupMembers)
523  {
524  PGPROC *memberProc;
525 
526  memberProc = dlist_container(PGPROC, lockGroupLink, iter.cur);
527 
528  if (memberProc->links.next != NULL && memberProc->waitLock != NULL &&
529  memberProc != checkProc &&
530  FindLockCycleRecurseMember(memberProc, checkProc, depth, softEdges,
531  nSoftEdges))
532  return true;
533  }
534 
535  return false;
536 }
537 
538 static bool
540  PGPROC *checkProcLeader,
541  int depth,
542  EDGE *softEdges, /* output argument */
543  int *nSoftEdges) /* output argument */
544 {
545  PGPROC *proc;
546  LOCK *lock = checkProc->waitLock;
547  PGXACT *pgxact;
548  PROCLOCK *proclock;
549  SHM_QUEUE *procLocks;
550  LockMethod lockMethodTable;
551  PROC_QUEUE *waitQueue;
552  int queue_size;
553  int conflictMask;
554  int i;
555  int numLockModes,
556  lm;
557 
558  lockMethodTable = GetLocksMethodTable(lock);
559  numLockModes = lockMethodTable->numLockModes;
560  conflictMask = lockMethodTable->conflictTab[checkProc->waitLockMode];
561 
562  /*
563  * Scan for procs that already hold conflicting locks. These are "hard"
564  * edges in the waits-for graph.
565  */
566  procLocks = &(lock->procLocks);
567 
568  proclock = (PROCLOCK *) SHMQueueNext(procLocks, procLocks,
569  offsetof(PROCLOCK, lockLink));
570 
571  while (proclock)
572  {
573  PGPROC *leader;
574 
575  proc = proclock->tag.myProc;
576  pgxact = &ProcGlobal->allPgXact[proc->pgprocno];
577  leader = proc->lockGroupLeader == NULL ? proc : proc->lockGroupLeader;
578 
579  /* A proc never blocks itself or any other lock group member */
580  if (leader != checkProcLeader)
581  {
582  for (lm = 1; lm <= numLockModes; lm++)
583  {
584  if ((proclock->holdMask & LOCKBIT_ON(lm)) &&
585  (conflictMask & LOCKBIT_ON(lm)))
586  {
587  /* This proc hard-blocks checkProc */
588  if (FindLockCycleRecurse(proc, depth + 1,
589  softEdges, nSoftEdges))
590  {
591  /* fill deadlockDetails[] */
592  DEADLOCK_INFO *info = &deadlockDetails[depth];
593 
594  info->locktag = lock->tag;
595  info->lockmode = checkProc->waitLockMode;
596  info->pid = checkProc->pid;
597 
598  return true;
599  }
600 
601  /*
602  * No deadlock here, but see if this proc is an autovacuum
603  * that is directly hard-blocking our own proc. If so,
604  * report it so that the caller can send a cancel signal
605  * to it, if appropriate. If there's more than one such
606  * proc, it's indeterminate which one will be reported.
607  *
608  * We don't touch autovacuums that are indirectly blocking
609  * us; it's up to the direct blockee to take action. This
610  * rule simplifies understanding the behavior and ensures
611  * that an autovacuum won't be canceled with less than
612  * deadlock_timeout grace period.
613  *
614  * Note we read vacuumFlags without any locking. This is
615  * OK only for checking the PROC_IS_AUTOVACUUM flag,
616  * because that flag is set at process start and never
617  * reset. There is logic elsewhere to avoid canceling an
618  * autovacuum that is working to prevent XID wraparound
619  * problems (which needs to read a different vacuumFlag
620  * bit), but we don't do that here to avoid grabbing
621  * ProcArrayLock.
622  */
623  if (checkProc == MyProc &&
625  blocking_autovacuum_proc = proc;
626 
627  /* We're done looking at this proclock */
628  break;
629  }
630  }
631  }
632 
633  proclock = (PROCLOCK *) SHMQueueNext(procLocks, &proclock->lockLink,
634  offsetof(PROCLOCK, lockLink));
635  }
636 
637  /*
638  * Scan for procs that are ahead of this one in the lock's wait queue.
639  * Those that have conflicting requests soft-block this one. This must be
640  * done after the hard-block search, since if another proc both hard- and
641  * soft-blocks this one, we want to call it a hard edge.
642  *
643  * If there is a proposed re-ordering of the lock's wait order, use that
644  * rather than the current wait order.
645  */
646  for (i = 0; i < nWaitOrders; i++)
647  {
648  if (waitOrders[i].lock == lock)
649  break;
650  }
651 
652  if (i < nWaitOrders)
653  {
654  /* Use the given hypothetical wait queue order */
655  PGPROC **procs = waitOrders[i].procs;
656 
657  queue_size = waitOrders[i].nProcs;
658 
659  for (i = 0; i < queue_size; i++)
660  {
661  PGPROC *leader;
662 
663  proc = procs[i];
664  leader = proc->lockGroupLeader == NULL ? proc :
665  proc->lockGroupLeader;
666 
667  /*
668  * TopoSort will always return an ordering with group members
669  * adjacent to each other in the wait queue (see comments
670  * therein). So, as soon as we reach a process in the same lock
671  * group as checkProc, we know we've found all the conflicts that
672  * precede any member of the lock group lead by checkProcLeader.
673  */
674  if (leader == checkProcLeader)
675  break;
676 
677  /* Is there a conflict with this guy's request? */
678  if ((LOCKBIT_ON(proc->waitLockMode) & conflictMask) != 0)
679  {
680  /* This proc soft-blocks checkProc */
681  if (FindLockCycleRecurse(proc, depth + 1,
682  softEdges, nSoftEdges))
683  {
684  /* fill deadlockDetails[] */
685  DEADLOCK_INFO *info = &deadlockDetails[depth];
686 
687  info->locktag = lock->tag;
688  info->lockmode = checkProc->waitLockMode;
689  info->pid = checkProc->pid;
690 
691  /*
692  * Add this edge to the list of soft edges in the cycle
693  */
694  Assert(*nSoftEdges < MaxBackends);
695  softEdges[*nSoftEdges].waiter = checkProcLeader;
696  softEdges[*nSoftEdges].blocker = leader;
697  softEdges[*nSoftEdges].lock = lock;
698  (*nSoftEdges)++;
699  return true;
700  }
701  }
702  }
703  }
704  else
705  {
706  PGPROC *lastGroupMember = NULL;
707 
708  /* Use the true lock wait queue order */
709  waitQueue = &(lock->waitProcs);
710 
711  /*
712  * Find the last member of the lock group that is present in the wait
713  * queue. Anything after this is not a soft lock conflict. If group
714  * locking is not in use, then we know immediately which process we're
715  * looking for, but otherwise we've got to search the wait queue to
716  * find the last process actually present.
717  */
718  if (checkProc->lockGroupLeader == NULL)
719  lastGroupMember = checkProc;
720  else
721  {
722  proc = (PGPROC *) waitQueue->links.next;
723  queue_size = waitQueue->size;
724  while (queue_size-- > 0)
725  {
726  if (proc->lockGroupLeader == checkProcLeader)
727  lastGroupMember = proc;
728  proc = (PGPROC *) proc->links.next;
729  }
730  Assert(lastGroupMember != NULL);
731  }
732 
733  /*
734  * OK, now rescan (or scan) the queue to identify the soft conflicts.
735  */
736  queue_size = waitQueue->size;
737  proc = (PGPROC *) waitQueue->links.next;
738  while (queue_size-- > 0)
739  {
740  PGPROC *leader;
741 
742  leader = proc->lockGroupLeader == NULL ? proc :
743  proc->lockGroupLeader;
744 
745  /* Done when we reach the target proc */
746  if (proc == lastGroupMember)
747  break;
748 
749  /* Is there a conflict with this guy's request? */
750  if ((LOCKBIT_ON(proc->waitLockMode) & conflictMask) != 0 &&
751  leader != checkProcLeader)
752  {
753  /* This proc soft-blocks checkProc */
754  if (FindLockCycleRecurse(proc, depth + 1,
755  softEdges, nSoftEdges))
756  {
757  /* fill deadlockDetails[] */
758  DEADLOCK_INFO *info = &deadlockDetails[depth];
759 
760  info->locktag = lock->tag;
761  info->lockmode = checkProc->waitLockMode;
762  info->pid = checkProc->pid;
763 
764  /*
765  * Add this edge to the list of soft edges in the cycle
766  */
767  Assert(*nSoftEdges < MaxBackends);
768  softEdges[*nSoftEdges].waiter = checkProcLeader;
769  softEdges[*nSoftEdges].blocker = leader;
770  softEdges[*nSoftEdges].lock = lock;
771  (*nSoftEdges)++;
772  return true;
773  }
774  }
775 
776  proc = (PGPROC *) proc->links.next;
777  }
778  }
779 
780  /*
781  * No conflict detected here.
782  */
783  return false;
784 }
785 
786 
787 /*
788  * ExpandConstraints -- expand a list of constraints into a set of
789  * specific new orderings for affected wait queues
790  *
791  * Input is a list of soft edges to be reversed. The output is a list
792  * of nWaitOrders WAIT_ORDER structs in waitOrders[], with PGPROC array
793  * workspace in waitOrderProcs[].
794  *
795  * Returns TRUE if able to build an ordering that satisfies all the
796  * constraints, FALSE if not (there are contradictory constraints).
797  */
798 static bool
799 ExpandConstraints(EDGE *constraints,
800  int nConstraints)
801 {
802  int nWaitOrderProcs = 0;
803  int i,
804  j;
805 
806  nWaitOrders = 0;
807 
808  /*
809  * Scan constraint list backwards. This is because the last-added
810  * constraint is the only one that could fail, and so we want to test it
811  * for inconsistency first.
812  */
813  for (i = nConstraints; --i >= 0;)
814  {
815  LOCK *lock = constraints[i].lock;
816 
817  /* Did we already make a list for this lock? */
818  for (j = nWaitOrders; --j >= 0;)
819  {
820  if (waitOrders[j].lock == lock)
821  break;
822  }
823  if (j >= 0)
824  continue;
825  /* No, so allocate a new list */
826  waitOrders[nWaitOrders].lock = lock;
827  waitOrders[nWaitOrders].procs = waitOrderProcs + nWaitOrderProcs;
828  waitOrders[nWaitOrders].nProcs = lock->waitProcs.size;
829  nWaitOrderProcs += lock->waitProcs.size;
830  Assert(nWaitOrderProcs <= MaxBackends);
831 
832  /*
833  * Do the topo sort. TopoSort need not examine constraints after this
834  * one, since they must be for different locks.
835  */
836  if (!TopoSort(lock, constraints, i + 1,
837  waitOrders[nWaitOrders].procs))
838  return false;
839  nWaitOrders++;
840  }
841  return true;
842 }
843 
844 
845 /*
846  * TopoSort -- topological sort of a wait queue
847  *
848  * Generate a re-ordering of a lock's wait queue that satisfies given
849  * constraints about certain procs preceding others. (Each such constraint
850  * is a fact of a partial ordering.) Minimize rearrangement of the queue
851  * not needed to achieve the partial ordering.
852  *
853  * This is a lot simpler and slower than, for example, the topological sort
854  * algorithm shown in Knuth's Volume 1. However, Knuth's method doesn't
855  * try to minimize the damage to the existing order. In practice we are
856  * not likely to be working with more than a few constraints, so the apparent
857  * slowness of the algorithm won't really matter.
858  *
859  * The initial queue ordering is taken directly from the lock's wait queue.
860  * The output is an array of PGPROC pointers, of length equal to the lock's
861  * wait queue length (the caller is responsible for providing this space).
862  * The partial order is specified by an array of EDGE structs. Each EDGE
863  * is one that we need to reverse, therefore the "waiter" must appear before
864  * the "blocker" in the output array. The EDGE array may well contain
865  * edges associated with other locks; these should be ignored.
866  *
867  * Returns TRUE if able to build an ordering that satisfies all the
868  * constraints, FALSE if not (there are contradictory constraints).
869  */
870 static bool
872  EDGE *constraints,
873  int nConstraints,
874  PGPROC **ordering) /* output argument */
875 {
876  PROC_QUEUE *waitQueue = &(lock->waitProcs);
877  int queue_size = waitQueue->size;
878  PGPROC *proc;
879  int i,
880  j,
881  jj,
882  k,
883  kk,
884  last;
885 
886  /* First, fill topoProcs[] array with the procs in their current order */
887  proc = (PGPROC *) waitQueue->links.next;
888  for (i = 0; i < queue_size; i++)
889  {
890  topoProcs[i] = proc;
891  proc = (PGPROC *) proc->links.next;
892  }
893 
894  /*
895  * Scan the constraints, and for each proc in the array, generate a count
896  * of the number of constraints that say it must be before something else,
897  * plus a list of the constraints that say it must be after something
898  * else. The count for the j'th proc is stored in beforeConstraints[j],
899  * and the head of its list in afterConstraints[j]. Each constraint
900  * stores its list link in constraints[i].link (note any constraint will
901  * be in just one list). The array index for the before-proc of the i'th
902  * constraint is remembered in constraints[i].pred.
903  *
904  * Note that it's not necessarily the case that every constraint affects
905  * this particular wait queue. Prior to group locking, a process could be
906  * waiting for at most one lock. But a lock group can be waiting for
907  * zero, one, or multiple locks. Since topoProcs[] is an array of the
908  * processes actually waiting, while constraints[] is an array of group
909  * leaders, we've got to scan through topoProcs[] for each constraint,
910  * checking whether both a waiter and a blocker for that group are
911  * present. If so, the constraint is relevant to this wait queue; if not,
912  * it isn't.
913  */
914  MemSet(beforeConstraints, 0, queue_size * sizeof(int));
915  MemSet(afterConstraints, 0, queue_size * sizeof(int));
916  for (i = 0; i < nConstraints; i++)
917  {
918  /*
919  * Find a representative process that is on the lock queue and part of
920  * the waiting lock group. This may or may not be the leader, which
921  * may or may not be waiting at all. If there are any other processes
922  * in the same lock group on the queue, set their number of
923  * beforeConstraints to -1 to indicate that they should be emitted
924  * with their groupmates rather than considered separately.
925  */
926  proc = constraints[i].waiter;
927  Assert(proc != NULL);
928  jj = -1;
929  for (j = queue_size; --j >= 0;)
930  {
931  PGPROC *waiter = topoProcs[j];
932 
933  if (waiter == proc || waiter->lockGroupLeader == proc)
934  {
935  Assert(waiter->waitLock == lock);
936  if (jj == -1)
937  jj = j;
938  else
939  {
940  Assert(beforeConstraints[j] <= 0);
941  beforeConstraints[j] = -1;
942  }
943  break;
944  }
945  }
946 
947  /* If no matching waiter, constraint is not relevant to this lock. */
948  if (jj < 0)
949  continue;
950 
951  /*
952  * Similarly, find a representative process that is on the lock queue
953  * and waiting for the blocking lock group. Again, this could be the
954  * leader but does not need to be.
955  */
956  proc = constraints[i].blocker;
957  Assert(proc != NULL);
958  kk = -1;
959  for (k = queue_size; --k >= 0;)
960  {
961  PGPROC *blocker = topoProcs[k];
962 
963  if (blocker == proc || blocker->lockGroupLeader == proc)
964  {
965  Assert(blocker->waitLock == lock);
966  if (kk == -1)
967  kk = k;
968  else
969  {
970  Assert(beforeConstraints[k] <= 0);
971  beforeConstraints[k] = -1;
972  }
973  }
974  }
975 
976  /* If no matching blocker, constraint is not relevant to this lock. */
977  if (kk < 0)
978  continue;
979 
980  beforeConstraints[jj]++; /* waiter must come before */
981  /* add this constraint to list of after-constraints for blocker */
982  constraints[i].pred = jj;
983  constraints[i].link = afterConstraints[kk];
984  afterConstraints[kk] = i + 1;
985  }
986 
987  /*--------------------
988  * Now scan the topoProcs array backwards. At each step, output the
989  * last proc that has no remaining before-constraints plus any other
990  * members of the same lock group; then decrease the beforeConstraints
991  * count of each of the procs it was constrained against.
992  * i = index of ordering[] entry we want to output this time
993  * j = search index for topoProcs[]
994  * k = temp for scanning constraint list for proc j
995  * last = last non-null index in topoProcs (avoid redundant searches)
996  *--------------------
997  */
998  last = queue_size - 1;
999  for (i = queue_size - 1; i >= 0;)
1000  {
1001  int c;
1002  int nmatches = 0;
1003 
1004  /* Find next candidate to output */
1005  while (topoProcs[last] == NULL)
1006  last--;
1007  for (j = last; j >= 0; j--)
1008  {
1009  if (topoProcs[j] != NULL && beforeConstraints[j] == 0)
1010  break;
1011  }
1012 
1013  /* If no available candidate, topological sort fails */
1014  if (j < 0)
1015  return false;
1016 
1017  /*
1018  * Output everything in the lock group. There's no point in
1019  * outputting an ordering where members of the same lock group are not
1020  * consecutive on the wait queue: if some other waiter is between two
1021  * requests that belong to the same group, then either it conflicts
1022  * with both of them and is certainly not a solution; or it conflicts
1023  * with at most one of them and is thus isomorphic to an ordering
1024  * where the group members are consecutive.
1025  */
1026  proc = topoProcs[j];
1027  if (proc->lockGroupLeader != NULL)
1028  proc = proc->lockGroupLeader;
1029  Assert(proc != NULL);
1030  for (c = 0; c <= last; ++c)
1031  {
1032  if (topoProcs[c] == proc || (topoProcs[c] != NULL &&
1033  topoProcs[c]->lockGroupLeader == proc))
1034  {
1035  ordering[i - nmatches] = topoProcs[c];
1036  topoProcs[c] = NULL;
1037  ++nmatches;
1038  }
1039  }
1040  Assert(nmatches > 0);
1041  i -= nmatches;
1042 
1043  /* Update beforeConstraints counts of its predecessors */
1044  for (k = afterConstraints[j]; k > 0; k = constraints[k - 1].link)
1045  beforeConstraints[constraints[k - 1].pred]--;
1046  }
1047 
1048  /* Done */
1049  return true;
1050 }
1051 
1052 #ifdef DEBUG_DEADLOCK
1053 static void
1054 PrintLockQueue(LOCK *lock, const char *info)
1055 {
1056  PROC_QUEUE *waitQueue = &(lock->waitProcs);
1057  int queue_size = waitQueue->size;
1058  PGPROC *proc;
1059  int i;
1060 
1061  printf("%s lock %p queue ", info, lock);
1062  proc = (PGPROC *) waitQueue->links.next;
1063  for (i = 0; i < queue_size; i++)
1064  {
1065  printf(" %d", proc->pid);
1066  proc = (PGPROC *) proc->links.next;
1067  }
1068  printf("\n");
1069  fflush(stdout);
1070 }
1071 #endif
1072 
1073 /*
1074  * Report a detected deadlock, with available details.
1075  */
1076 void
1078 {
1079  StringInfoData clientbuf; /* errdetail for client */
1080  StringInfoData logbuf; /* errdetail for server log */
1081  StringInfoData locktagbuf;
1082  int i;
1083 
1084  initStringInfo(&clientbuf);
1085  initStringInfo(&logbuf);
1086  initStringInfo(&locktagbuf);
1087 
1088  /* Generate the "waits for" lines sent to the client */
1089  for (i = 0; i < nDeadlockDetails; i++)
1090  {
1091  DEADLOCK_INFO *info = &deadlockDetails[i];
1092  int nextpid;
1093 
1094  /* The last proc waits for the first one... */
1095  if (i < nDeadlockDetails - 1)
1096  nextpid = info[1].pid;
1097  else
1098  nextpid = deadlockDetails[0].pid;
1099 
1100  /* reset locktagbuf to hold next object description */
1101  resetStringInfo(&locktagbuf);
1102 
1103  DescribeLockTag(&locktagbuf, &info->locktag);
1104 
1105  if (i > 0)
1106  appendStringInfoChar(&clientbuf, '\n');
1107 
1108  appendStringInfo(&clientbuf,
1109  _("Process %d waits for %s on %s; blocked by process %d."),
1110  info->pid,
1112  info->lockmode),
1113  locktagbuf.data,
1114  nextpid);
1115  }
1116 
1117  /* Duplicate all the above for the server ... */
1118  appendStringInfoString(&logbuf, clientbuf.data);
1119 
1120  /* ... and add info about query strings */
1121  for (i = 0; i < nDeadlockDetails; i++)
1122  {
1123  DEADLOCK_INFO *info = &deadlockDetails[i];
1124 
1125  appendStringInfoChar(&logbuf, '\n');
1126 
1127  appendStringInfo(&logbuf,
1128  _("Process %d: %s"),
1129  info->pid,
1131  }
1132 
1134 
1135  ereport(ERROR,
1136  (errcode(ERRCODE_T_R_DEADLOCK_DETECTED),
1137  errmsg("deadlock detected"),
1138  errdetail_internal("%s", clientbuf.data),
1139  errdetail_log("%s", logbuf.data),
1140  errhint("See server log for query details.")));
1141 }
1142 
1143 /*
1144  * RememberSimpleDeadLock: set up info for DeadLockReport when ProcSleep
1145  * detects a trivial (two-way) deadlock. proc1 wants to block for lockmode
1146  * on lock, but proc2 is already waiting and would be blocked by proc1.
1147  */
1148 void
1150  LOCKMODE lockmode,
1151  LOCK *lock,
1152  PGPROC *proc2)
1153 {
1154  DEADLOCK_INFO *info = &deadlockDetails[0];
1155 
1156  info->locktag = lock->tag;
1157  info->lockmode = lockmode;
1158  info->pid = proc1->pid;
1159  info++;
1160  info->locktag = proc2->waitLock->tag;
1161  info->lockmode = proc2->waitLockMode;
1162  info->pid = proc2->pid;
1163  nDeadlockDetails = 2;
1164 }
int link
Definition: deadlock.c:52
void pgstat_report_deadlock(void)
Definition: pgstat.c:1471
PROCLOCKTAG tag
Definition: lock.h:348
LOCK * lock
Definition: deadlock.c:50
int errhint(const char *fmt,...)
Definition: elog.c:987
static int maxCurConstraints
Definition: deadlock.c:118
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition: lock.c:3845
LOCKMODE lockmode
Definition: deadlock.c:74
Definition: proc.h:207
static WAIT_ORDER * waitOrders
Definition: deadlock.c:111
int LOCKMODE
Definition: lockdefs.h:26
dlist_head lockGroupMembers
Definition: proc.h:189
SHM_QUEUE links
Definition: lock.h:32
PGXACT * allPgXact
Definition: proc.h:234
PGPROC * MyProc
Definition: proc.c:67
LOCKMASK holdMask
Definition: lock.h:352
SHM_QUEUE links
Definition: proc.h:97
Definition: deadlock.c:46
static PGPROC * blocking_autovacuum_proc
Definition: deadlock.c:128
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
struct SHM_QUEUE * next
Definition: shmem.h:31
PGPROC * waiter
Definition: deadlock.c:48
LOCKMODE waitLockMode
Definition: proc.h:137
LOCKTAG tag
Definition: lock.h:288
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: lock.h:179
const LOCKMASK * conflictTab
Definition: lock.h:115
SHM_QUEUE lockLink
Definition: lock.h:354
int errcode(int sqlerrcode)
Definition: elog.c:575
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define MemSet(start, val, len)
Definition: c.h:857
void SHMQueueInsertBefore(SHM_QUEUE *queue, SHM_QUEUE *elem)
Definition: shmqueue.c:89
static int nCurConstraints
Definition: deadlock.c:117
static int nPossibleConstraints
Definition: deadlock.c:122
int nProcs
Definition: deadlock.c:60
static bool ExpandConstraints(EDGE *constraints, int nConstraints)
Definition: deadlock.c:799
int errdetail_internal(const char *fmt,...)
Definition: elog.c:900
static PGPROC ** waitOrderProcs
Definition: deadlock.c:113
PROC_QUEUE waitProcs
Definition: lock.h:294
uint8 vacuumFlags
Definition: proc.h:218
void RememberSimpleDeadLock(PGPROC *proc1, LOCKMODE lockmode, LOCK *lock, PGPROC *proc2)
Definition: deadlock.c:1149
LOCK * lock
Definition: deadlock.c:58
static bool FindLockCycle(PGPROC *checkProc, EDGE *softEdges, int *nSoftEdges)
Definition: deadlock.c:449
int pred
Definition: deadlock.c:51
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
PGPROC * blocker
Definition: deadlock.c:49
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ERROR
Definition: elog.h:43
PGPROC ** procs
Definition: deadlock.c:59
#define FATAL
Definition: elog.h:52
static PGPROC ** topoProcs
Definition: deadlock.c:106
void DescribeLockTag(StringInfo buf, const LOCKTAG *tag)
Definition: lmgr.c:954
int MaxBackends
Definition: globals.c:126
static int nDeadlockDetails
Definition: deadlock.c:125
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
void InitDeadLockChecking(void)
Definition: deadlock.c:143
char * c
void ProcQueueInit(PROC_QUEUE *queue)
Definition: proc.c:1004
static int TestConfiguration(PGPROC *startProc)
Definition: deadlock.c:381
DeadLockState DeadLockCheck(PGPROC *proc)
Definition: deadlock.c:217
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:94
int errdetail_log(const char *fmt,...)
Definition: elog.c:921
static EDGE * curConstraints
Definition: deadlock.c:116
Definition: lock.h:285
LOCK * waitLock
Definition: proc.h:135
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
SHM_QUEUE procLocks
Definition: lock.h:293
const char * pgstat_get_backend_current_activity(int pid, bool checkUser)
Definition: pgstat.c:3855
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
Definition: proc.c:1595
static bool TopoSort(LOCK *lock, EDGE *constraints, int nConstraints, PGPROC **ordering)
Definition: deadlock.c:871
static int * afterConstraints
Definition: deadlock.c:108
Pointer SHMQueueNext(const SHM_QUEUE *queue, const SHM_QUEUE *curElem, Size linkOffset)
Definition: shmqueue.c:145
static bool FindLockCycleRecurseMember(PGPROC *checkProc, PGPROC *checkProcLeader, int depth, EDGE *softEdges, int *nSoftEdges)
Definition: deadlock.c:539
dlist_node * cur
Definition: ilist.h:161
void DeadLockReport(void)
Definition: deadlock.c:1077
DeadLockState
Definition: lock.h:480
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static DEADLOCK_INFO * deadlockDetails
Definition: deadlock.c:124
static PGPROC ** visitedProcs
Definition: deadlock.c:102
static bool FindLockCycleRecurse(PGPROC *checkProc, int depth, EDGE *softEdges, int *nSoftEdges)
Definition: deadlock.c:460
uint8 locktag_lockmethodid
Definition: lock.h:186
static EDGE * possibleConstraints
Definition: deadlock.c:121
PGPROC * myProc
Definition: lock.h:342
#define LOCKBIT_ON(lockmode)
Definition: lock.h:88
Definition: lock.h:345
int pgprocno
Definition: proc.h:109
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
static bool DeadLockCheckRecurse(PGPROC *proc)
Definition: deadlock.c:315
int i
int size
Definition: lock.h:33
static int nWaitOrders
Definition: deadlock.c:112
LOCKTAG locktag
Definition: deadlock.c:73
#define elog
Definition: elog.h:219
PGPROC * GetBlockingAutoVacuumPgproc(void)
Definition: deadlock.c:293
static int nVisitedProcs
Definition: deadlock.c:103
static int maxPossibleConstraints
Definition: deadlock.c:123
static int * beforeConstraints
Definition: deadlock.c:107
Definition: proc.h:94
int pid
Definition: proc.h:108
LockMethod GetLocksMethodTable(const LOCK *lock)
Definition: lock.c:460
#define _(x)
Definition: elog.c:84
PGPROC * lockGroupLeader
Definition: proc.h:188
#define PROC_IS_AUTOVACUUM
Definition: proc.h:52
#define offsetof(type, field)
Definition: c.h:555
int numLockModes
Definition: lock.h:114