Revert SIGUSR1 multiplexing patch, per Tom's objection.
[PostgreSQL.git] / src / backend / storage / lmgr / proc.c
blobf1f4d79baba5d07a4999f8574aac6db57c420e4b
1 /*-------------------------------------------------------------------------
3 * proc.c
4 * routines to manage per-process shared memory data structure
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
16 * Interface (a):
17 * ProcSleep(), ProcWakeup(),
18 * ProcQueueAlloc() -- create a shm queue for sleeping processes
19 * ProcQueueInit() -- create a queue without allocing memory
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).
25 * Interface (b):
27 * ProcReleaseLocks -- frees the locks associated with current transaction
29 * ProcKill -- destroys the shared memory state (and locks)
30 * associated with the process.
32 #include "postgres.h"
34 #include <signal.h>
35 #include <unistd.h>
36 #include <sys/time.h>
38 #include "access/transam.h"
39 #include "access/xact.h"
40 #include "miscadmin.h"
41 #include "postmaster/autovacuum.h"
42 #include "storage/ipc.h"
43 #include "storage/lmgr.h"
44 #include "storage/proc.h"
45 #include "storage/procarray.h"
46 #include "storage/spin.h"
49 /* GUC variables */
50 int DeadlockTimeout = 1000;
51 int StatementTimeout = 0;
52 bool log_lock_waits = false;
54 /* Pointer to this process's PGPROC struct, if any */
55 PGPROC *MyProc = NULL;
58 * This spinlock protects the freelist of recycled PGPROC structures.
59 * We cannot use an LWLock because the LWLock manager depends on already
60 * having a PGPROC and a wait semaphore! But these structures are touched
61 * relatively infrequently (only at backend startup or shutdown) and not for
62 * very long, so a spinlock is okay.
64 NON_EXEC_STATIC slock_t *ProcStructLock = NULL;
66 /* Pointers to shared-memory structures */
67 NON_EXEC_STATIC PROC_HDR *ProcGlobal = NULL;
68 NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL;
70 /* If we are waiting for a lock, this points to the associated LOCALLOCK */
71 static LOCALLOCK *lockAwaited = NULL;
73 /* Mark these volatile because they can be changed by signal handler */
74 static volatile bool statement_timeout_active = false;
75 static volatile bool deadlock_timeout_active = false;
76 static volatile DeadLockState deadlock_state = DS_NOT_YET_CHECKED;
77 volatile bool cancel_from_timeout = false;
79 /* timeout_start_time is set when log_lock_waits is true */
80 static TimestampTz timeout_start_time;
82 /* statement_fin_time is valid only if statement_timeout_active is true */
83 static TimestampTz statement_fin_time;
86 static void RemoveProcFromArray(int code, Datum arg);
87 static void ProcKill(int code, Datum arg);
88 static void AuxiliaryProcKill(int code, Datum arg);
89 static bool CheckStatementTimeout(void);
93 * Report shared-memory space needed by InitProcGlobal.
95 Size
96 ProcGlobalShmemSize(void)
98 Size size = 0;
100 /* ProcGlobal */
101 size = add_size(size, sizeof(PROC_HDR));
102 /* AuxiliaryProcs */
103 size = add_size(size, mul_size(NUM_AUXILIARY_PROCS, sizeof(PGPROC)));
104 /* MyProcs, including autovacuum */
105 size = add_size(size, mul_size(MaxBackends, sizeof(PGPROC)));
106 /* ProcStructLock */
107 size = add_size(size, sizeof(slock_t));
109 return size;
113 * Report number of semaphores needed by InitProcGlobal.
116 ProcGlobalSemas(void)
119 * We need a sema per backend (including autovacuum), plus one for each
120 * auxiliary process.
122 return MaxBackends + NUM_AUXILIARY_PROCS;
126 * InitProcGlobal -
127 * Initialize the global process table during postmaster or standalone
128 * backend startup.
130 * We also create all the per-process semaphores we will need to support
131 * the requested number of backends. We used to allocate semaphores
132 * only when backends were actually started up, but that is bad because
133 * it lets Postgres fail under load --- a lot of Unix systems are
134 * (mis)configured with small limits on the number of semaphores, and
135 * running out when trying to start another backend is a common failure.
136 * So, now we grab enough semaphores to support the desired max number
137 * of backends immediately at initialization --- if the sysadmin has set
138 * MaxConnections or autovacuum_max_workers higher than his kernel will
139 * support, he'll find out sooner rather than later.
141 * Another reason for creating semaphores here is that the semaphore
142 * implementation typically requires us to create semaphores in the
143 * postmaster, not in backends.
145 * Note: this is NOT called by individual backends under a postmaster,
146 * not even in the EXEC_BACKEND case. The ProcGlobal and AuxiliaryProcs
147 * pointers must be propagated specially for EXEC_BACKEND operation.
149 void
150 InitProcGlobal(void)
152 PGPROC *procs;
153 int i;
154 bool found;
156 /* Create the ProcGlobal shared structure */
157 ProcGlobal = (PROC_HDR *)
158 ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
159 Assert(!found);
162 * Create the PGPROC structures for auxiliary (bgwriter) processes, too.
163 * These do not get linked into the freeProcs list.
165 AuxiliaryProcs = (PGPROC *)
166 ShmemInitStruct("AuxiliaryProcs", NUM_AUXILIARY_PROCS * sizeof(PGPROC),
167 &found);
168 Assert(!found);
171 * Initialize the data structures.
173 ProcGlobal->freeProcs = NULL;
174 ProcGlobal->autovacFreeProcs = NULL;
176 ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;
179 * Pre-create the PGPROC structures and create a semaphore for each.
181 procs = (PGPROC *) ShmemAlloc((MaxConnections) * sizeof(PGPROC));
182 if (!procs)
183 ereport(FATAL,
184 (errcode(ERRCODE_OUT_OF_MEMORY),
185 errmsg("out of shared memory")));
186 MemSet(procs, 0, MaxConnections * sizeof(PGPROC));
187 for (i = 0; i < MaxConnections; i++)
189 PGSemaphoreCreate(&(procs[i].sem));
190 procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs;
191 ProcGlobal->freeProcs = &procs[i];
194 procs = (PGPROC *) ShmemAlloc((autovacuum_max_workers) * sizeof(PGPROC));
195 if (!procs)
196 ereport(FATAL,
197 (errcode(ERRCODE_OUT_OF_MEMORY),
198 errmsg("out of shared memory")));
199 MemSet(procs, 0, autovacuum_max_workers * sizeof(PGPROC));
200 for (i = 0; i < autovacuum_max_workers; i++)
202 PGSemaphoreCreate(&(procs[i].sem));
203 procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs;
204 ProcGlobal->autovacFreeProcs = &procs[i];
207 MemSet(AuxiliaryProcs, 0, NUM_AUXILIARY_PROCS * sizeof(PGPROC));
208 for (i = 0; i < NUM_AUXILIARY_PROCS; i++)
210 AuxiliaryProcs[i].pid = 0; /* marks auxiliary proc as not in use */
211 PGSemaphoreCreate(&(AuxiliaryProcs[i].sem));
214 /* Create ProcStructLock spinlock, too */
215 ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));
216 SpinLockInit(ProcStructLock);
220 * InitProcess -- initialize a per-process data structure for this backend
222 void
223 InitProcess(void)
225 /* use volatile pointer to prevent code rearrangement */
226 volatile PROC_HDR *procglobal = ProcGlobal;
227 int i;
230 * ProcGlobal should be set up already (if we are a backend, we inherit
231 * this by fork() or EXEC_BACKEND mechanism from the postmaster).
233 if (procglobal == NULL)
234 elog(PANIC, "proc header uninitialized");
236 if (MyProc != NULL)
237 elog(ERROR, "you already exist");
240 * Try to get a proc struct from the free list. If this fails, we must be
241 * out of PGPROC structures (not to mention semaphores).
243 * While we are holding the ProcStructLock, also copy the current shared
244 * estimate of spins_per_delay to local storage.
246 SpinLockAcquire(ProcStructLock);
248 set_spins_per_delay(procglobal->spins_per_delay);
250 if (IsAutoVacuumWorkerProcess())
251 MyProc = procglobal->autovacFreeProcs;
252 else
253 MyProc = procglobal->freeProcs;
255 if (MyProc != NULL)
257 if (IsAutoVacuumWorkerProcess())
258 procglobal->autovacFreeProcs = (PGPROC *) MyProc->links.next;
259 else
260 procglobal->freeProcs = (PGPROC *) MyProc->links.next;
261 SpinLockRelease(ProcStructLock);
263 else
266 * If we reach here, all the PGPROCs are in use. This is one of the
267 * possible places to detect "too many backends", so give the standard
268 * error message. XXX do we need to give a different failure message
269 * in the autovacuum case?
271 SpinLockRelease(ProcStructLock);
272 ereport(FATAL,
273 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
274 errmsg("sorry, too many clients already")));
278 * Initialize all fields of MyProc, except for the semaphore which was
279 * prepared for us by InitProcGlobal.
281 SHMQueueElemInit(&(MyProc->links));
282 MyProc->waitStatus = STATUS_OK;
283 MyProc->lxid = InvalidLocalTransactionId;
284 MyProc->xid = InvalidTransactionId;
285 MyProc->xmin = InvalidTransactionId;
286 MyProc->pid = MyProcPid;
287 /* backendId, databaseId and roleId will be filled in later */
288 MyProc->backendId = InvalidBackendId;
289 MyProc->databaseId = InvalidOid;
290 MyProc->roleId = InvalidOid;
291 MyProc->inCommit = false;
292 MyProc->vacuumFlags = 0;
293 if (IsAutoVacuumWorkerProcess())
294 MyProc->vacuumFlags |= PROC_IS_AUTOVACUUM;
295 MyProc->lwWaiting = false;
296 MyProc->lwExclusive = false;
297 MyProc->lwWaitLink = NULL;
298 MyProc->waitLock = NULL;
299 MyProc->waitProcLock = NULL;
300 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
301 SHMQueueInit(&(MyProc->myProcLocks[i]));
304 * We might be reusing a semaphore that belonged to a failed process. So
305 * be careful and reinitialize its value here. (This is not strictly
306 * necessary anymore, but seems like a good idea for cleanliness.)
308 PGSemaphoreReset(&MyProc->sem);
311 * Arrange to clean up at backend exit.
313 on_shmem_exit(ProcKill, 0);
316 * Now that we have a PGPROC, we could try to acquire locks, so initialize
317 * the deadlock checker.
319 InitDeadLockChecking();
323 * InitProcessPhase2 -- make MyProc visible in the shared ProcArray.
325 * This is separate from InitProcess because we can't acquire LWLocks until
326 * we've created a PGPROC, but in the EXEC_BACKEND case there is a good deal
327 * of stuff to be done before this step that will require LWLock access.
329 void
330 InitProcessPhase2(void)
332 Assert(MyProc != NULL);
335 * We should now know what database we're in, so advertise that. (We need
336 * not do any locking here, since no other backend can yet see our
337 * PGPROC.)
339 Assert(OidIsValid(MyDatabaseId));
340 MyProc->databaseId = MyDatabaseId;
343 * Add our PGPROC to the PGPROC array in shared memory.
345 ProcArrayAdd(MyProc);
348 * Arrange to clean that up at backend exit.
350 on_shmem_exit(RemoveProcFromArray, 0);
354 * InitAuxiliaryProcess -- create a per-auxiliary-process data structure
356 * This is called by bgwriter and similar processes so that they will have a
357 * MyProc value that's real enough to let them wait for LWLocks. The PGPROC
358 * and sema that are assigned are one of the extra ones created during
359 * InitProcGlobal.
361 * Auxiliary processes are presently not expected to wait for real (lockmgr)
362 * locks, so we need not set up the deadlock checker. They are never added
363 * to the ProcArray or the sinval messaging mechanism, either. They also
364 * don't get a VXID assigned, since this is only useful when we actually
365 * hold lockmgr locks.
367 void
368 InitAuxiliaryProcess(void)
370 PGPROC *auxproc;
371 int proctype;
372 int i;
375 * ProcGlobal should be set up already (if we are a backend, we inherit
376 * this by fork() or EXEC_BACKEND mechanism from the postmaster).
378 if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
379 elog(PANIC, "proc header uninitialized");
381 if (MyProc != NULL)
382 elog(ERROR, "you already exist");
385 * We use the ProcStructLock to protect assignment and releasing of
386 * AuxiliaryProcs entries.
388 * While we are holding the ProcStructLock, also copy the current shared
389 * estimate of spins_per_delay to local storage.
391 SpinLockAcquire(ProcStructLock);
393 set_spins_per_delay(ProcGlobal->spins_per_delay);
396 * Find a free auxproc ... *big* trouble if there isn't one ...
398 for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)
400 auxproc = &AuxiliaryProcs[proctype];
401 if (auxproc->pid == 0)
402 break;
404 if (proctype >= NUM_AUXILIARY_PROCS)
406 SpinLockRelease(ProcStructLock);
407 elog(FATAL, "all AuxiliaryProcs are in use");
410 /* Mark auxiliary proc as in use by me */
411 /* use volatile pointer to prevent code rearrangement */
412 ((volatile PGPROC *) auxproc)->pid = MyProcPid;
414 MyProc = auxproc;
416 SpinLockRelease(ProcStructLock);
419 * Initialize all fields of MyProc, except for the semaphore which was
420 * prepared for us by InitProcGlobal.
422 SHMQueueElemInit(&(MyProc->links));
423 MyProc->waitStatus = STATUS_OK;
424 MyProc->lxid = InvalidLocalTransactionId;
425 MyProc->xid = InvalidTransactionId;
426 MyProc->xmin = InvalidTransactionId;
427 MyProc->backendId = InvalidBackendId;
428 MyProc->databaseId = InvalidOid;
429 MyProc->roleId = InvalidOid;
430 MyProc->inCommit = false;
431 /* we don't set the "is autovacuum" flag in the launcher */
432 MyProc->vacuumFlags = 0;
433 MyProc->lwWaiting = false;
434 MyProc->lwExclusive = false;
435 MyProc->lwWaitLink = NULL;
436 MyProc->waitLock = NULL;
437 MyProc->waitProcLock = NULL;
438 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
439 SHMQueueInit(&(MyProc->myProcLocks[i]));
442 * We might be reusing a semaphore that belonged to a failed process. So
443 * be careful and reinitialize its value here. (This is not strictly
444 * necessary anymore, but seems like a good idea for cleanliness.)
446 PGSemaphoreReset(&MyProc->sem);
449 * Arrange to clean up at process exit.
451 on_shmem_exit(AuxiliaryProcKill, Int32GetDatum(proctype));
455 * Check whether there are at least N free PGPROC objects.
457 * Note: this is designed on the assumption that N will generally be small.
459 bool
460 HaveNFreeProcs(int n)
462 PGPROC *proc;
464 /* use volatile pointer to prevent code rearrangement */
465 volatile PROC_HDR *procglobal = ProcGlobal;
467 SpinLockAcquire(ProcStructLock);
469 proc = procglobal->freeProcs;
471 while (n > 0 && proc != NULL)
473 proc = (PGPROC *) proc->links.next;
474 n--;
477 SpinLockRelease(ProcStructLock);
479 return (n <= 0);
483 * Cancel any pending wait for lock, when aborting a transaction.
485 * (Normally, this would only happen if we accept a cancel/die
486 * interrupt while waiting; but an ereport(ERROR) while waiting is
487 * within the realm of possibility, too.)
489 void
490 LockWaitCancel(void)
492 LWLockId partitionLock;
494 /* Nothing to do if we weren't waiting for a lock */
495 if (lockAwaited == NULL)
496 return;
498 /* Turn off the deadlock timer, if it's still running (see ProcSleep) */
499 disable_sig_alarm(false);
501 /* Unlink myself from the wait queue, if on it (might not be anymore!) */
502 partitionLock = LockHashPartitionLock(lockAwaited->hashcode);
503 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
505 if (MyProc->links.next != NULL)
507 /* We could not have been granted the lock yet */
508 RemoveFromWaitQueue(MyProc, lockAwaited->hashcode);
510 else
513 * Somebody kicked us off the lock queue already. Perhaps they
514 * granted us the lock, or perhaps they detected a deadlock. If they
515 * did grant us the lock, we'd better remember it in our local lock
516 * table.
518 if (MyProc->waitStatus == STATUS_OK)
519 GrantAwaitedLock();
522 lockAwaited = NULL;
524 LWLockRelease(partitionLock);
527 * We used to do PGSemaphoreReset() here to ensure that our proc's wait
528 * semaphore is reset to zero. This prevented a leftover wakeup signal
529 * from remaining in the semaphore if someone else had granted us the lock
530 * we wanted before we were able to remove ourselves from the wait-list.
531 * However, now that ProcSleep loops until waitStatus changes, a leftover
532 * wakeup signal isn't harmful, and it seems not worth expending cycles to
533 * get rid of a signal that most likely isn't there.
539 * ProcReleaseLocks() -- release locks associated with current transaction
540 * at main transaction commit or abort
542 * At main transaction commit, we release all locks except session locks.
543 * At main transaction abort, we release all locks including session locks;
544 * this lets us clean up after a VACUUM FULL failure.
546 * At subtransaction commit, we don't release any locks (so this func is not
547 * needed at all); we will defer the releasing to the parent transaction.
548 * At subtransaction abort, we release all locks held by the subtransaction;
549 * this is implemented by retail releasing of the locks under control of
550 * the ResourceOwner mechanism.
552 * Note that user locks are not released in any case.
554 void
555 ProcReleaseLocks(bool isCommit)
557 if (!MyProc)
558 return;
559 /* If waiting, get off wait queue (should only be needed after error) */
560 LockWaitCancel();
561 /* Release locks */
562 LockReleaseAll(DEFAULT_LOCKMETHOD, !isCommit);
567 * RemoveProcFromArray() -- Remove this process from the shared ProcArray.
569 static void
570 RemoveProcFromArray(int code, Datum arg)
572 Assert(MyProc != NULL);
573 ProcArrayRemove(MyProc, InvalidTransactionId);
577 * ProcKill() -- Destroy the per-proc data structure for
578 * this process. Release any of its held LW locks.
580 static void
581 ProcKill(int code, Datum arg)
583 /* use volatile pointer to prevent code rearrangement */
584 volatile PROC_HDR *procglobal = ProcGlobal;
586 Assert(MyProc != NULL);
589 * Release any LW locks I am holding. There really shouldn't be any, but
590 * it's cheap to check again before we cut the knees off the LWLock
591 * facility by releasing our PGPROC ...
593 LWLockReleaseAll();
595 SpinLockAcquire(ProcStructLock);
597 /* Return PGPROC structure (and semaphore) to freelist */
598 if (IsAutoVacuumWorkerProcess())
600 MyProc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
601 procglobal->autovacFreeProcs = MyProc;
603 else
605 MyProc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
606 procglobal->freeProcs = MyProc;
609 /* PGPROC struct isn't mine anymore */
610 MyProc = NULL;
612 /* Update shared estimate of spins_per_delay */
613 procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay);
615 SpinLockRelease(ProcStructLock);
617 /* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
618 if (AutovacuumLauncherPid != 0)
619 kill(AutovacuumLauncherPid, SIGUSR1);
623 * AuxiliaryProcKill() -- Cut-down version of ProcKill for auxiliary
624 * processes (bgwriter, etc). The PGPROC and sema are not released, only
625 * marked as not-in-use.
627 static void
628 AuxiliaryProcKill(int code, Datum arg)
630 int proctype = DatumGetInt32(arg);
631 PGPROC *auxproc;
633 Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
635 auxproc = &AuxiliaryProcs[proctype];
637 Assert(MyProc == auxproc);
639 /* Release any LW locks I am holding (see notes above) */
640 LWLockReleaseAll();
642 SpinLockAcquire(ProcStructLock);
644 /* Mark auxiliary proc no longer in use */
645 MyProc->pid = 0;
647 /* PGPROC struct isn't mine anymore */
648 MyProc = NULL;
650 /* Update shared estimate of spins_per_delay */
651 ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
653 SpinLockRelease(ProcStructLock);
658 * ProcQueue package: routines for putting processes to sleep
659 * and waking them up
663 * ProcQueueAlloc -- alloc/attach to a shared memory process queue
665 * Returns: a pointer to the queue or NULL
666 * Side Effects: Initializes the queue if we allocated one
668 #ifdef NOT_USED
669 PROC_QUEUE *
670 ProcQueueAlloc(char *name)
672 bool found;
673 PROC_QUEUE *queue = (PROC_QUEUE *)
674 ShmemInitStruct(name, sizeof(PROC_QUEUE), &found);
676 if (!queue)
677 return NULL;
678 if (!found)
679 ProcQueueInit(queue);
680 return queue;
682 #endif
685 * ProcQueueInit -- initialize a shared memory process queue
687 void
688 ProcQueueInit(PROC_QUEUE *queue)
690 SHMQueueInit(&(queue->links));
691 queue->size = 0;
696 * ProcSleep -- put a process to sleep on the specified lock
698 * Caller must have set MyProc->heldLocks to reflect locks already held
699 * on the lockable object by this process (under all XIDs).
701 * The lock table's partition lock must be held at entry, and will be held
702 * at exit.
704 * Result: STATUS_OK if we acquired the lock, STATUS_ERROR if not (deadlock).
706 * ASSUME: that no one will fiddle with the queue until after
707 * we release the partition lock.
709 * NOTES: The process queue is now a priority queue for locking.
711 * P() on the semaphore should put us to sleep. The process
712 * semaphore is normally zero, so when we try to acquire it, we sleep.
715 ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
717 LOCKMODE lockmode = locallock->tag.mode;
718 LOCK *lock = locallock->lock;
719 PROCLOCK *proclock = locallock->proclock;
720 uint32 hashcode = locallock->hashcode;
721 LWLockId partitionLock = LockHashPartitionLock(hashcode);
722 PROC_QUEUE *waitQueue = &(lock->waitProcs);
723 LOCKMASK myHeldLocks = MyProc->heldLocks;
724 bool early_deadlock = false;
725 bool allow_autovacuum_cancel = true;
726 int myWaitStatus;
727 PGPROC *proc;
728 int i;
731 * Determine where to add myself in the wait queue.
733 * Normally I should go at the end of the queue. However, if I already
734 * hold locks that conflict with the request of any previous waiter, put
735 * myself in the queue just in front of the first such waiter. This is not
736 * a necessary step, since deadlock detection would move me to before that
737 * waiter anyway; but it's relatively cheap to detect such a conflict
738 * immediately, and avoid delaying till deadlock timeout.
740 * Special case: if I find I should go in front of some waiter, check to
741 * see if I conflict with already-held locks or the requests before that
742 * waiter. If not, then just grant myself the requested lock immediately.
743 * This is the same as the test for immediate grant in LockAcquire, except
744 * we are only considering the part of the wait queue before my insertion
745 * point.
747 if (myHeldLocks != 0)
749 LOCKMASK aheadRequests = 0;
751 proc = (PGPROC *) waitQueue->links.next;
752 for (i = 0; i < waitQueue->size; i++)
754 /* Must he wait for me? */
755 if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
757 /* Must I wait for him ? */
758 if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
761 * Yes, so we have a deadlock. Easiest way to clean up
762 * correctly is to call RemoveFromWaitQueue(), but we
763 * can't do that until we are *on* the wait queue. So, set
764 * a flag to check below, and break out of loop. Also,
765 * record deadlock info for later message.
767 RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
768 early_deadlock = true;
769 break;
771 /* I must go before this waiter. Check special case. */
772 if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
773 LockCheckConflicts(lockMethodTable,
774 lockmode,
775 lock,
776 proclock,
777 MyProc) == STATUS_OK)
779 /* Skip the wait and just grant myself the lock. */
780 GrantLock(lock, proclock, lockmode);
781 GrantAwaitedLock();
782 return STATUS_OK;
784 /* Break out of loop to put myself before him */
785 break;
787 /* Nope, so advance to next waiter */
788 aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
789 proc = (PGPROC *) proc->links.next;
793 * If we fall out of loop normally, proc points to waitQueue head, so
794 * we will insert at tail of queue as desired.
797 else
799 /* I hold no locks, so I can't push in front of anyone. */
800 proc = (PGPROC *) &(waitQueue->links);
804 * Insert self into queue, ahead of the given proc (or at tail of queue).
806 SHMQueueInsertBefore(&(proc->links), &(MyProc->links));
807 waitQueue->size++;
809 lock->waitMask |= LOCKBIT_ON(lockmode);
811 /* Set up wait information in PGPROC object, too */
812 MyProc->waitLock = lock;
813 MyProc->waitProcLock = proclock;
814 MyProc->waitLockMode = lockmode;
816 MyProc->waitStatus = STATUS_WAITING;
819 * If we detected deadlock, give up without waiting. This must agree with
820 * CheckDeadLock's recovery code, except that we shouldn't release the
821 * semaphore since we haven't tried to lock it yet.
823 if (early_deadlock)
825 RemoveFromWaitQueue(MyProc, hashcode);
826 return STATUS_ERROR;
829 /* mark that we are waiting for a lock */
830 lockAwaited = locallock;
833 * Release the lock table's partition lock.
835 * NOTE: this may also cause us to exit critical-section state, possibly
836 * allowing a cancel/die interrupt to be accepted. This is OK because we
837 * have recorded the fact that we are waiting for a lock, and so
838 * LockWaitCancel will clean up if cancel/die happens.
840 LWLockRelease(partitionLock);
842 /* Reset deadlock_state before enabling the signal handler */
843 deadlock_state = DS_NOT_YET_CHECKED;
846 * Set timer so we can wake up after awhile and check for a deadlock. If a
847 * deadlock is detected, the handler releases the process's semaphore and
848 * sets MyProc->waitStatus = STATUS_ERROR, allowing us to know that we
849 * must report failure rather than success.
851 * By delaying the check until we've waited for a bit, we can avoid
852 * running the rather expensive deadlock-check code in most cases.
854 if (!enable_sig_alarm(DeadlockTimeout, false))
855 elog(FATAL, "could not set timer for process wakeup");
858 * If someone wakes us between LWLockRelease and PGSemaphoreLock,
859 * PGSemaphoreLock will not block. The wakeup is "saved" by the semaphore
860 * implementation. While this is normally good, there are cases where a
861 * saved wakeup might be leftover from a previous operation (for example,
862 * we aborted ProcWaitForSignal just before someone did ProcSendSignal).
863 * So, loop to wait again if the waitStatus shows we haven't been granted
864 * nor denied the lock yet.
866 * We pass interruptOK = true, which eliminates a window in which
867 * cancel/die interrupts would be held off undesirably. This is a promise
868 * that we don't mind losing control to a cancel/die interrupt here. We
869 * don't, because we have no shared-state-change work to do after being
870 * granted the lock (the grantor did it all). We do have to worry about
871 * updating the locallock table, but if we lose control to an error,
872 * LockWaitCancel will fix that up.
876 PGSemaphoreLock(&MyProc->sem, true);
879 * waitStatus could change from STATUS_WAITING to something else
880 * asynchronously. Read it just once per loop to prevent surprising
881 * behavior (such as missing log messages).
883 myWaitStatus = MyProc->waitStatus;
886 * If we are not deadlocked, but are waiting on an autovacuum-induced
887 * task, send a signal to interrupt it.
889 if (deadlock_state == DS_BLOCKED_BY_AUTOVACUUM && allow_autovacuum_cancel)
891 PGPROC *autovac = GetBlockingAutoVacuumPgproc();
893 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
896 * Only do it if the worker is not working to protect against Xid
897 * wraparound.
899 if ((autovac != NULL) &&
900 (autovac->vacuumFlags & PROC_IS_AUTOVACUUM) &&
901 !(autovac->vacuumFlags & PROC_VACUUM_FOR_WRAPAROUND))
903 int pid = autovac->pid;
905 elog(DEBUG2, "sending cancel to blocking autovacuum pid = %d",
906 pid);
908 /* don't hold the lock across the kill() syscall */
909 LWLockRelease(ProcArrayLock);
911 /* send the autovacuum worker Back to Old Kent Road */
912 if (kill(pid, SIGINT) < 0)
914 /* Just a warning to allow multiple callers */
915 ereport(WARNING,
916 (errmsg("could not send signal to process %d: %m",
917 pid)));
920 else
921 LWLockRelease(ProcArrayLock);
923 /* prevent signal from being resent more than once */
924 allow_autovacuum_cancel = false;
928 * If awoken after the deadlock check interrupt has run, and
929 * log_lock_waits is on, then report about the wait.
931 if (log_lock_waits && deadlock_state != DS_NOT_YET_CHECKED)
933 StringInfoData buf;
934 const char *modename;
935 long secs;
936 int usecs;
937 long msecs;
939 initStringInfo(&buf);
940 DescribeLockTag(&buf, &locallock->tag.lock);
941 modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
942 lockmode);
943 TimestampDifference(timeout_start_time, GetCurrentTimestamp(),
944 &secs, &usecs);
945 msecs = secs * 1000 + usecs / 1000;
946 usecs = usecs % 1000;
948 if (deadlock_state == DS_SOFT_DEADLOCK)
949 ereport(LOG,
950 (errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
951 MyProcPid, modename, buf.data, msecs, usecs)));
952 else if (deadlock_state == DS_HARD_DEADLOCK)
955 * This message is a bit redundant with the error that will be
956 * reported subsequently, but in some cases the error report
957 * might not make it to the log (eg, if it's caught by an
958 * exception handler), and we want to ensure all long-wait
959 * events get logged.
961 ereport(LOG,
962 (errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
963 MyProcPid, modename, buf.data, msecs, usecs)));
966 if (myWaitStatus == STATUS_WAITING)
967 ereport(LOG,
968 (errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
969 MyProcPid, modename, buf.data, msecs, usecs)));
970 else if (myWaitStatus == STATUS_OK)
971 ereport(LOG,
972 (errmsg("process %d acquired %s on %s after %ld.%03d ms",
973 MyProcPid, modename, buf.data, msecs, usecs)));
974 else
976 Assert(myWaitStatus == STATUS_ERROR);
979 * Currently, the deadlock checker always kicks its own
980 * process, which means that we'll only see STATUS_ERROR when
981 * deadlock_state == DS_HARD_DEADLOCK, and there's no need to
982 * print redundant messages. But for completeness and
983 * future-proofing, print a message if it looks like someone
984 * else kicked us off the lock.
986 if (deadlock_state != DS_HARD_DEADLOCK)
987 ereport(LOG,
988 (errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
989 MyProcPid, modename, buf.data, msecs, usecs)));
993 * At this point we might still need to wait for the lock. Reset
994 * state so we don't print the above messages again.
996 deadlock_state = DS_NO_DEADLOCK;
998 pfree(buf.data);
1000 } while (myWaitStatus == STATUS_WAITING);
1003 * Disable the timer, if it's still running
1005 if (!disable_sig_alarm(false))
1006 elog(FATAL, "could not disable timer for process wakeup");
1009 * Re-acquire the lock table's partition lock. We have to do this to hold
1010 * off cancel/die interrupts before we can mess with lockAwaited (else we
1011 * might have a missed or duplicated locallock update).
1013 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
1016 * We no longer want LockWaitCancel to do anything.
1018 lockAwaited = NULL;
1021 * If we got the lock, be sure to remember it in the locallock table.
1023 if (MyProc->waitStatus == STATUS_OK)
1024 GrantAwaitedLock();
1027 * We don't have to do anything else, because the awaker did all the
1028 * necessary update of the lock table and MyProc.
1030 return MyProc->waitStatus;
1035 * ProcWakeup -- wake up a process by releasing its private semaphore.
1037 * Also remove the process from the wait queue and set its links invalid.
1038 * RETURN: the next process in the wait queue.
1040 * The appropriate lock partition lock must be held by caller.
1042 * XXX: presently, this code is only used for the "success" case, and only
1043 * works correctly for that case. To clean up in failure case, would need
1044 * to twiddle the lock's request counts too --- see RemoveFromWaitQueue.
1045 * Hence, in practice the waitStatus parameter must be STATUS_OK.
1047 PGPROC *
1048 ProcWakeup(PGPROC *proc, int waitStatus)
1050 PGPROC *retProc;
1052 /* Proc should be sleeping ... */
1053 if (proc->links.prev == NULL ||
1054 proc->links.next == NULL)
1055 return NULL;
1056 Assert(proc->waitStatus == STATUS_WAITING);
1058 /* Save next process before we zap the list link */
1059 retProc = (PGPROC *) proc->links.next;
1061 /* Remove process from wait queue */
1062 SHMQueueDelete(&(proc->links));
1063 (proc->waitLock->waitProcs.size)--;
1065 /* Clean up process' state and pass it the ok/fail signal */
1066 proc->waitLock = NULL;
1067 proc->waitProcLock = NULL;
1068 proc->waitStatus = waitStatus;
1070 /* And awaken it */
1071 PGSemaphoreUnlock(&proc->sem);
1073 return retProc;
1077 * ProcLockWakeup -- routine for waking up processes when a lock is
1078 * released (or a prior waiter is aborted). Scan all waiters
1079 * for lock, waken any that are no longer blocked.
1081 * The appropriate lock partition lock must be held by caller.
1083 void
1084 ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
1086 PROC_QUEUE *waitQueue = &(lock->waitProcs);
1087 int queue_size = waitQueue->size;
1088 PGPROC *proc;
1089 LOCKMASK aheadRequests = 0;
1091 Assert(queue_size >= 0);
1093 if (queue_size == 0)
1094 return;
1096 proc = (PGPROC *) waitQueue->links.next;
1098 while (queue_size-- > 0)
1100 LOCKMODE lockmode = proc->waitLockMode;
1103 * Waken if (a) doesn't conflict with requests of earlier waiters, and
1104 * (b) doesn't conflict with already-held locks.
1106 if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1107 LockCheckConflicts(lockMethodTable,
1108 lockmode,
1109 lock,
1110 proc->waitProcLock,
1111 proc) == STATUS_OK)
1113 /* OK to waken */
1114 GrantLock(lock, proc->waitProcLock, lockmode);
1115 proc = ProcWakeup(proc, STATUS_OK);
1118 * ProcWakeup removes proc from the lock's waiting process queue
1119 * and returns the next proc in chain; don't use proc's next-link,
1120 * because it's been cleared.
1123 else
1126 * Cannot wake this guy. Remember his request for later checks.
1128 aheadRequests |= LOCKBIT_ON(lockmode);
1129 proc = (PGPROC *) proc->links.next;
1133 Assert(waitQueue->size >= 0);
1137 * CheckDeadLock
1139 * We only get to this routine if we got SIGALRM after DeadlockTimeout
1140 * while waiting for a lock to be released by some other process. Look
1141 * to see if there's a deadlock; if not, just return and continue waiting.
1142 * (But signal ProcSleep to log a message, if log_lock_waits is true.)
1143 * If we have a real deadlock, remove ourselves from the lock's wait queue
1144 * and signal an error to ProcSleep.
1146 * NB: this is run inside a signal handler, so be very wary about what is done
1147 * here or in called routines.
1149 static void
1150 CheckDeadLock(void)
1152 int i;
1155 * Acquire exclusive lock on the entire shared lock data structures. Must
1156 * grab LWLocks in partition-number order to avoid LWLock deadlock.
1158 * Note that the deadlock check interrupt had better not be enabled
1159 * anywhere that this process itself holds lock partition locks, else this
1160 * will wait forever. Also note that LWLockAcquire creates a critical
1161 * section, so that this routine cannot be interrupted by cancel/die
1162 * interrupts.
1164 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1165 LWLockAcquire(FirstLockMgrLock + i, LW_EXCLUSIVE);
1168 * Check to see if we've been awoken by anyone in the interim.
1170 * If we have, we can return and resume our transaction -- happy day.
1171 * Before we are awoken the process releasing the lock grants it to us
1172 * so we know that we don't have to wait anymore.
1174 * We check by looking to see if we've been unlinked from the wait queue.
1175 * This is quicker than checking our semaphore's state, since no kernel
1176 * call is needed, and it is safe because we hold the lock partition lock.
1178 if (MyProc->links.prev == NULL ||
1179 MyProc->links.next == NULL)
1180 goto check_done;
1182 #ifdef LOCK_DEBUG
1183 if (Debug_deadlocks)
1184 DumpAllLocks();
1185 #endif
1187 /* Run the deadlock check, and set deadlock_state for use by ProcSleep */
1188 deadlock_state = DeadLockCheck(MyProc);
1190 if (deadlock_state == DS_HARD_DEADLOCK)
1193 * Oops. We have a deadlock.
1195 * Get this process out of wait state. (Note: we could do this more
1196 * efficiently by relying on lockAwaited, but use this coding to
1197 * preserve the flexibility to kill some other transaction than the
1198 * one detecting the deadlock.)
1200 * RemoveFromWaitQueue sets MyProc->waitStatus to STATUS_ERROR, so
1201 * ProcSleep will report an error after we return from the signal
1202 * handler.
1204 Assert(MyProc->waitLock != NULL);
1205 RemoveFromWaitQueue(MyProc, LockTagHashCode(&(MyProc->waitLock->tag)));
1208 * Unlock my semaphore so that the interrupted ProcSleep() call can
1209 * finish.
1211 PGSemaphoreUnlock(&MyProc->sem);
1214 * We're done here. Transaction abort caused by the error that
1215 * ProcSleep will raise will cause any other locks we hold to be
1216 * released, thus allowing other processes to wake up; we don't need
1217 * to do that here. NOTE: an exception is that releasing locks we
1218 * hold doesn't consider the possibility of waiters that were blocked
1219 * behind us on the lock we just failed to get, and might now be
1220 * wakable because we're not in front of them anymore. However,
1221 * RemoveFromWaitQueue took care of waking up any such processes.
1224 else if (log_lock_waits || deadlock_state == DS_BLOCKED_BY_AUTOVACUUM)
1227 * Unlock my semaphore so that the interrupted ProcSleep() call can
1228 * print the log message (we daren't do it here because we are inside
1229 * a signal handler). It will then sleep again until someone releases
1230 * the lock.
1232 * If blocked by autovacuum, this wakeup will enable ProcSleep to send
1233 * the cancelling signal to the autovacuum worker.
1235 PGSemaphoreUnlock(&MyProc->sem);
1239 * And release locks. We do this in reverse order for two reasons: (1)
1240 * Anyone else who needs more than one of the locks will be trying to lock
1241 * them in increasing order; we don't want to release the other process
1242 * until it can get all the locks it needs. (2) This avoids O(N^2)
1243 * behavior inside LWLockRelease.
1245 check_done:
1246 for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
1247 LWLockRelease(FirstLockMgrLock + i);
1252 * ProcWaitForSignal - wait for a signal from another backend.
1254 * This can share the semaphore normally used for waiting for locks,
1255 * since a backend could never be waiting for a lock and a signal at
1256 * the same time. As with locks, it's OK if the signal arrives just
1257 * before we actually reach the waiting state. Also as with locks,
1258 * it's necessary that the caller be robust against bogus wakeups:
1259 * always check that the desired state has occurred, and wait again
1260 * if not. This copes with possible "leftover" wakeups.
1262 void
1263 ProcWaitForSignal(void)
1265 PGSemaphoreLock(&MyProc->sem, true);
1269 * ProcSendSignal - send a signal to a backend identified by PID
1271 void
1272 ProcSendSignal(int pid)
1274 PGPROC *proc = BackendPidGetProc(pid);
1276 if (proc != NULL)
1277 PGSemaphoreUnlock(&proc->sem);
1281 /*****************************************************************************
1282 * SIGALRM interrupt support
1284 * Maybe these should be in pqsignal.c?
1285 *****************************************************************************/
1288 * Enable the SIGALRM interrupt to fire after the specified delay
1290 * Delay is given in milliseconds. Caller should be sure a SIGALRM
1291 * signal handler is installed before this is called.
1293 * This code properly handles nesting of deadlock timeout alarms within
1294 * statement timeout alarms.
1296 * Returns TRUE if okay, FALSE on failure.
1298 bool
1299 enable_sig_alarm(int delayms, bool is_statement_timeout)
1301 TimestampTz fin_time;
1302 struct itimerval timeval;
1304 if (is_statement_timeout)
1307 * Begin statement-level timeout
1309 * Note that we compute statement_fin_time with reference to the
1310 * statement_timestamp, but apply the specified delay without any
1311 * correction; that is, we ignore whatever time has elapsed since
1312 * statement_timestamp was set. In the normal case only a small
1313 * interval will have elapsed and so this doesn't matter, but there
1314 * are corner cases (involving multi-statement query strings with
1315 * embedded COMMIT or ROLLBACK) where we might re-initialize the
1316 * statement timeout long after initial receipt of the message. In
1317 * such cases the enforcement of the statement timeout will be a bit
1318 * inconsistent. This annoyance is judged not worth the cost of
1319 * performing an additional gettimeofday() here.
1321 Assert(!deadlock_timeout_active);
1322 fin_time = GetCurrentStatementStartTimestamp();
1323 fin_time = TimestampTzPlusMilliseconds(fin_time, delayms);
1324 statement_fin_time = fin_time;
1325 cancel_from_timeout = false;
1326 statement_timeout_active = true;
1328 else if (statement_timeout_active)
1331 * Begin deadlock timeout with statement-level timeout active
1333 * Here, we want to interrupt at the closer of the two timeout times.
1334 * If fin_time >= statement_fin_time then we need not touch the
1335 * existing timer setting; else set up to interrupt at the deadlock
1336 * timeout time.
1338 * NOTE: in this case it is possible that this routine will be
1339 * interrupted by the previously-set timer alarm. This is okay
1340 * because the signal handler will do only what it should do according
1341 * to the state variables. The deadlock checker may get run earlier
1342 * than normal, but that does no harm.
1344 timeout_start_time = GetCurrentTimestamp();
1345 fin_time = TimestampTzPlusMilliseconds(timeout_start_time, delayms);
1346 deadlock_timeout_active = true;
1347 if (fin_time >= statement_fin_time)
1348 return true;
1350 else
1352 /* Begin deadlock timeout with no statement-level timeout */
1353 deadlock_timeout_active = true;
1354 /* GetCurrentTimestamp can be expensive, so only do it if we must */
1355 if (log_lock_waits)
1356 timeout_start_time = GetCurrentTimestamp();
1359 /* If we reach here, okay to set the timer interrupt */
1360 MemSet(&timeval, 0, sizeof(struct itimerval));
1361 timeval.it_value.tv_sec = delayms / 1000;
1362 timeval.it_value.tv_usec = (delayms % 1000) * 1000;
1363 if (setitimer(ITIMER_REAL, &timeval, NULL))
1364 return false;
1365 return true;
1369 * Cancel the SIGALRM timer, either for a deadlock timeout or a statement
1370 * timeout. If a deadlock timeout is canceled, any active statement timeout
1371 * remains in force.
1373 * Returns TRUE if okay, FALSE on failure.
1375 bool
1376 disable_sig_alarm(bool is_statement_timeout)
1379 * Always disable the interrupt if it is active; this avoids being
1380 * interrupted by the signal handler and thereby possibly getting
1381 * confused.
1383 * We will re-enable the interrupt if necessary in CheckStatementTimeout.
1385 if (statement_timeout_active || deadlock_timeout_active)
1387 struct itimerval timeval;
1389 MemSet(&timeval, 0, sizeof(struct itimerval));
1390 if (setitimer(ITIMER_REAL, &timeval, NULL))
1392 statement_timeout_active = false;
1393 cancel_from_timeout = false;
1394 deadlock_timeout_active = false;
1395 return false;
1399 /* Always cancel deadlock timeout, in case this is error cleanup */
1400 deadlock_timeout_active = false;
1402 /* Cancel or reschedule statement timeout */
1403 if (is_statement_timeout)
1405 statement_timeout_active = false;
1406 cancel_from_timeout = false;
1408 else if (statement_timeout_active)
1410 if (!CheckStatementTimeout())
1411 return false;
1413 return true;
1418 * Check for statement timeout. If the timeout time has come,
1419 * trigger a query-cancel interrupt; if not, reschedule the SIGALRM
1420 * interrupt to occur at the right time.
1422 * Returns true if okay, false if failed to set the interrupt.
1424 static bool
1425 CheckStatementTimeout(void)
1427 TimestampTz now;
1429 if (!statement_timeout_active)
1430 return true; /* do nothing if not active */
1432 now = GetCurrentTimestamp();
1434 if (now >= statement_fin_time)
1436 /* Time to die */
1437 statement_timeout_active = false;
1438 cancel_from_timeout = true;
1439 #ifdef HAVE_SETSID
1440 /* try to signal whole process group */
1441 kill(-MyProcPid, SIGINT);
1442 #endif
1443 kill(MyProcPid, SIGINT);
1445 else
1447 /* Not time yet, so (re)schedule the interrupt */
1448 long secs;
1449 int usecs;
1450 struct itimerval timeval;
1452 TimestampDifference(now, statement_fin_time,
1453 &secs, &usecs);
1456 * It's possible that the difference is less than a microsecond;
1457 * ensure we don't cancel, rather than set, the interrupt.
1459 if (secs == 0 && usecs == 0)
1460 usecs = 1;
1461 MemSet(&timeval, 0, sizeof(struct itimerval));
1462 timeval.it_value.tv_sec = secs;
1463 timeval.it_value.tv_usec = usecs;
1464 if (setitimer(ITIMER_REAL, &timeval, NULL))
1465 return false;
1468 return true;
1473 * Signal handler for SIGALRM
1475 * Process deadlock check and/or statement timeout check, as needed.
1476 * To avoid various edge cases, we must be careful to do nothing
1477 * when there is nothing to be done. We also need to be able to
1478 * reschedule the timer interrupt if called before end of statement.
1480 void
1481 handle_sig_alarm(SIGNAL_ARGS)
1483 int save_errno = errno;
1485 if (deadlock_timeout_active)
1487 deadlock_timeout_active = false;
1488 CheckDeadLock();
1491 if (statement_timeout_active)
1492 (void) CheckStatementTimeout();
1494 errno = save_errno;