]> granicus.if.org Git - postgresql/blob - src/backend/storage/lmgr/proc.c
4ee651e3069172ae8abd2f36b823b4092b96c0b1
[postgresql] / src / backend / storage / lmgr / proc.c
1 /*-------------------------------------------------------------------------
2  *
3  * proc.c
4  *        routines to manage per-process shared memory data structure
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.204 2008/12/09 15:59:39 heikki Exp $
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/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"
47
48
49 /* GUC variables */
50 int                     DeadlockTimeout = 1000;
51 int                     StatementTimeout = 0;
52 bool            log_lock_waits = false;
53
54 /* Pointer to this process's PGPROC struct, if any */
55 PGPROC     *MyProc = NULL;
56
57 /*
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.
63  */
64 NON_EXEC_STATIC slock_t *ProcStructLock = NULL;
65
66 /* Pointers to shared-memory structures */
67 NON_EXEC_STATIC PROC_HDR *ProcGlobal = NULL;
68 NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL;
69
70 /* If we are waiting for a lock, this points to the associated LOCALLOCK */
71 static LOCALLOCK *lockAwaited = NULL;
72
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;
78
79 /* timeout_start_time is set when log_lock_waits is true */
80 static TimestampTz timeout_start_time;
81
82 /* statement_fin_time is valid only if statement_timeout_active is true */
83 static TimestampTz statement_fin_time;
84
85
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);
90
91
92 /*
93  * Report shared-memory space needed by InitProcGlobal.
94  */
95 Size
96 ProcGlobalShmemSize(void)
97 {
98         Size            size = 0;
99
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));
108
109         return size;
110 }
111
112 /*
113  * Report number of semaphores needed by InitProcGlobal.
114  */
115 int
116 ProcGlobalSemas(void)
117 {
118         /*
119          * We need a sema per backend (including autovacuum), plus one for each
120          * auxiliary process.
121          */
122         return MaxBackends + NUM_AUXILIARY_PROCS;
123 }
124
125 /*
126  * InitProcGlobal -
127  *        Initialize the global process table during postmaster or standalone
128  *        backend startup.
129  *
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.
140  *
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.
144  *
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.
148  */
149 void
150 InitProcGlobal(void)
151 {
152         PGPROC     *procs;
153         int                     i;
154         bool            found;
155
156         /* Create the ProcGlobal shared structure */
157         ProcGlobal = (PROC_HDR *)
158                 ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
159         Assert(!found);
160
161         /*
162          * Create the PGPROC structures for auxiliary (bgwriter) processes, too.
163          * These do not get linked into the freeProcs list.
164          */
165         AuxiliaryProcs = (PGPROC *)
166                 ShmemInitStruct("AuxiliaryProcs", NUM_AUXILIARY_PROCS * sizeof(PGPROC),
167                                                 &found);
168         Assert(!found);
169
170         /*
171          * Initialize the data structures.
172          */
173         ProcGlobal->freeProcs = NULL;
174         ProcGlobal->autovacFreeProcs = NULL;
175
176         ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;
177
178         /*
179          * Pre-create the PGPROC structures and create a semaphore for each.
180          */
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++)
188         {
189                 PGSemaphoreCreate(&(procs[i].sem));
190                 procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs;
191                 ProcGlobal->freeProcs = &procs[i];
192         }
193
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++)
201         {
202                 PGSemaphoreCreate(&(procs[i].sem));
203                 procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs;
204                 ProcGlobal->autovacFreeProcs = &procs[i];
205         }
206
207         MemSet(AuxiliaryProcs, 0, NUM_AUXILIARY_PROCS * sizeof(PGPROC));
208         for (i = 0; i < NUM_AUXILIARY_PROCS; i++)
209         {
210                 AuxiliaryProcs[i].pid = 0;              /* marks auxiliary proc as not in use */
211                 PGSemaphoreCreate(&(AuxiliaryProcs[i].sem));
212         }
213
214         /* Create ProcStructLock spinlock, too */
215         ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));
216         SpinLockInit(ProcStructLock);
217 }
218
219 /*
220  * InitProcess -- initialize a per-process data structure for this backend
221  */
222 void
223 InitProcess(void)
224 {
225         /* use volatile pointer to prevent code rearrangement */
226         volatile PROC_HDR *procglobal = ProcGlobal;
227         int                     i;
228
229         /*
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).
232          */
233         if (procglobal == NULL)
234                 elog(PANIC, "proc header uninitialized");
235
236         if (MyProc != NULL)
237                 elog(ERROR, "you already exist");
238
239         /*
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).
242          *
243          * While we are holding the ProcStructLock, also copy the current shared
244          * estimate of spins_per_delay to local storage.
245          */
246         SpinLockAcquire(ProcStructLock);
247
248         set_spins_per_delay(procglobal->spins_per_delay);
249
250         if (IsAutoVacuumWorkerProcess())
251                 MyProc = procglobal->autovacFreeProcs;
252         else
253                 MyProc = procglobal->freeProcs;
254
255         if (MyProc != NULL)
256         {
257                 if (IsAutoVacuumWorkerProcess())
258                         procglobal->autovacFreeProcs = (PGPROC *) MyProc->links.next;
259                 else
260                         procglobal->freeProcs = (PGPROC *) MyProc->links.next;
261                 SpinLockRelease(ProcStructLock);
262         }
263         else
264         {
265                 /*
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?
270                  */
271                 SpinLockRelease(ProcStructLock);
272                 ereport(FATAL,
273                                 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
274                                  errmsg("sorry, too many clients already")));
275         }
276
277         /*
278          * Initialize all fields of MyProc, except for the semaphore which was
279          * prepared for us by InitProcGlobal.
280          */
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]));
302
303         /*
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.)
307          */
308         PGSemaphoreReset(&MyProc->sem);
309
310         /*
311          * Arrange to clean up at backend exit.
312          */
313         on_shmem_exit(ProcKill, 0);
314
315         /*
316          * Now that we have a PGPROC, we could try to acquire locks, so initialize
317          * the deadlock checker.
318          */
319         InitDeadLockChecking();
320 }
321
322 /*
323  * InitProcessPhase2 -- make MyProc visible in the shared ProcArray.
324  *
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.
328  */
329 void
330 InitProcessPhase2(void)
331 {
332         Assert(MyProc != NULL);
333
334         /*
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.)
338          */
339         Assert(OidIsValid(MyDatabaseId));
340         MyProc->databaseId = MyDatabaseId;
341
342         /*
343          * Add our PGPROC to the PGPROC array in shared memory.
344          */
345         ProcArrayAdd(MyProc);
346
347         /*
348          * Arrange to clean that up at backend exit.
349          */
350         on_shmem_exit(RemoveProcFromArray, 0);
351 }
352
353 /*
354  * InitAuxiliaryProcess -- create a per-auxiliary-process data structure
355  *
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.
360  *
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.
366  */
367 void
368 InitAuxiliaryProcess(void)
369 {
370         PGPROC     *auxproc;
371         int                     proctype;
372         int                     i;
373
374         /*
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).
377          */
378         if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
379                 elog(PANIC, "proc header uninitialized");
380
381         if (MyProc != NULL)
382                 elog(ERROR, "you already exist");
383
384         /*
385          * We use the ProcStructLock to protect assignment and releasing of
386          * AuxiliaryProcs entries.
387          *
388          * While we are holding the ProcStructLock, also copy the current shared
389          * estimate of spins_per_delay to local storage.
390          */
391         SpinLockAcquire(ProcStructLock);
392
393         set_spins_per_delay(ProcGlobal->spins_per_delay);
394
395         /*
396          * Find a free auxproc ... *big* trouble if there isn't one ...
397          */
398         for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)
399         {
400                 auxproc = &AuxiliaryProcs[proctype];
401                 if (auxproc->pid == 0)
402                         break;
403         }
404         if (proctype >= NUM_AUXILIARY_PROCS)
405         {
406                 SpinLockRelease(ProcStructLock);
407                 elog(FATAL, "all AuxiliaryProcs are in use");
408         }
409
410         /* Mark auxiliary proc as in use by me */
411         /* use volatile pointer to prevent code rearrangement */
412         ((volatile PGPROC *) auxproc)->pid = MyProcPid;
413
414         MyProc = auxproc;
415
416         SpinLockRelease(ProcStructLock);
417
418         /*
419          * Initialize all fields of MyProc, except for the semaphore which was
420          * prepared for us by InitProcGlobal.
421          */
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]));
440
441         /*
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.)
445          */
446         PGSemaphoreReset(&MyProc->sem);
447
448         /*
449          * Arrange to clean up at process exit.
450          */
451         on_shmem_exit(AuxiliaryProcKill, Int32GetDatum(proctype));
452 }
453
454 /*
455  * Check whether there are at least N free PGPROC objects.
456  *
457  * Note: this is designed on the assumption that N will generally be small.
458  */
459 bool
460 HaveNFreeProcs(int n)
461 {
462         PGPROC     *proc;
463
464         /* use volatile pointer to prevent code rearrangement */
465         volatile PROC_HDR *procglobal = ProcGlobal;
466
467         SpinLockAcquire(ProcStructLock);
468
469         proc = procglobal->freeProcs;
470
471         while (n > 0 && proc != NULL)
472         {
473                 proc = (PGPROC *) proc->links.next;
474                 n--;
475         }
476
477         SpinLockRelease(ProcStructLock);
478
479         return (n <= 0);
480 }
481
482 /*
483  * Cancel any pending wait for lock, when aborting a transaction.
484  *
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.)
488  */
489 void
490 LockWaitCancel(void)
491 {
492         LWLockId        partitionLock;
493
494         /* Nothing to do if we weren't waiting for a lock */
495         if (lockAwaited == NULL)
496                 return;
497
498         /* Turn off the deadlock timer, if it's still running (see ProcSleep) */
499         disable_sig_alarm(false);
500
501         /* Unlink myself from the wait queue, if on it (might not be anymore!) */
502         partitionLock = LockHashPartitionLock(lockAwaited->hashcode);
503         LWLockAcquire(partitionLock, LW_EXCLUSIVE);
504
505         if (MyProc->links.next != NULL)
506         {
507                 /* We could not have been granted the lock yet */
508                 RemoveFromWaitQueue(MyProc, lockAwaited->hashcode);
509         }
510         else
511         {
512                 /*
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.
517                  */
518                 if (MyProc->waitStatus == STATUS_OK)
519                         GrantAwaitedLock();
520         }
521
522         lockAwaited = NULL;
523
524         LWLockRelease(partitionLock);
525
526         /*
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.
534          */
535 }
536
537
538 /*
539  * ProcReleaseLocks() -- release locks associated with current transaction
540  *                      at main transaction commit or abort
541  *
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.
545  *
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.
551  *
552  * Note that user locks are not released in any case.
553  */
554 void
555 ProcReleaseLocks(bool isCommit)
556 {
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);
563 }
564
565
566 /*
567  * RemoveProcFromArray() -- Remove this process from the shared ProcArray.
568  */
569 static void
570 RemoveProcFromArray(int code, Datum arg)
571 {
572         Assert(MyProc != NULL);
573         ProcArrayRemove(MyProc, InvalidTransactionId);
574 }
575
576 /*
577  * ProcKill() -- Destroy the per-proc data structure for
578  *              this process. Release any of its held LW locks.
579  */
580 static void
581 ProcKill(int code, Datum arg)
582 {
583         /* use volatile pointer to prevent code rearrangement */
584         volatile PROC_HDR *procglobal = ProcGlobal;
585
586         Assert(MyProc != NULL);
587
588         /*
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 ...
592          */
593         LWLockReleaseAll();
594
595         SpinLockAcquire(ProcStructLock);
596
597         /* Return PGPROC structure (and semaphore) to freelist */
598         if (IsAutoVacuumWorkerProcess())
599         {
600                 MyProc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
601                 procglobal->autovacFreeProcs = MyProc;
602         }
603         else
604         {
605                 MyProc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
606                 procglobal->freeProcs = MyProc;
607         }
608
609         /* PGPROC struct isn't mine anymore */
610         MyProc = NULL;
611
612         /* Update shared estimate of spins_per_delay */
613         procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay);
614
615         SpinLockRelease(ProcStructLock);
616
617         /* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
618         if (AutovacuumLauncherPid != 0)
619                 kill(AutovacuumLauncherPid, SIGUSR1);
620 }
621
622 /*
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.
626  */
627 static void
628 AuxiliaryProcKill(int code, Datum arg)
629 {
630         int                     proctype = DatumGetInt32(arg);
631         PGPROC     *auxproc;
632
633         Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
634
635         auxproc = &AuxiliaryProcs[proctype];
636
637         Assert(MyProc == auxproc);
638
639         /* Release any LW locks I am holding (see notes above) */
640         LWLockReleaseAll();
641
642         SpinLockAcquire(ProcStructLock);
643
644         /* Mark auxiliary proc no longer in use */
645         MyProc->pid = 0;
646
647         /* PGPROC struct isn't mine anymore */
648         MyProc = NULL;
649
650         /* Update shared estimate of spins_per_delay */
651         ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
652
653         SpinLockRelease(ProcStructLock);
654 }
655
656
657 /*
658  * ProcQueue package: routines for putting processes to sleep
659  *              and  waking them up
660  */
661
662 /*
663  * ProcQueueAlloc -- alloc/attach to a shared memory process queue
664  *
665  * Returns: a pointer to the queue or NULL
666  * Side Effects: Initializes the queue if we allocated one
667  */
668 #ifdef NOT_USED
669 PROC_QUEUE *
670 ProcQueueAlloc(char *name)
671 {
672         bool            found;
673         PROC_QUEUE *queue = (PROC_QUEUE *)
674         ShmemInitStruct(name, sizeof(PROC_QUEUE), &found);
675
676         if (!queue)
677                 return NULL;
678         if (!found)
679                 ProcQueueInit(queue);
680         return queue;
681 }
682 #endif
683
684 /*
685  * ProcQueueInit -- initialize a shared memory process queue
686  */
687 void
688 ProcQueueInit(PROC_QUEUE *queue)
689 {
690         SHMQueueInit(&(queue->links));
691         queue->size = 0;
692 }
693
694
695 /*
696  * ProcSleep -- put a process to sleep on the specified lock
697  *
698  * Caller must have set MyProc->heldLocks to reflect locks already held
699  * on the lockable object by this process (under all XIDs).
700  *
701  * The lock table's partition lock must be held at entry, and will be held
702  * at exit.
703  *
704  * Result: STATUS_OK if we acquired the lock, STATUS_ERROR if not (deadlock).
705  *
706  * ASSUME: that no one will fiddle with the queue until after
707  *              we release the partition lock.
708  *
709  * NOTES: The process queue is now a priority queue for locking.
710  *
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.
713  */
714 int
715 ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
716 {
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;
729
730         /*
731          * Determine where to add myself in the wait queue.
732          *
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.
739          *
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.
746          */
747         if (myHeldLocks != 0)
748         {
749                 LOCKMASK        aheadRequests = 0;
750
751                 proc = (PGPROC *) waitQueue->links.next;
752                 for (i = 0; i < waitQueue->size; i++)
753                 {
754                         /* Must he wait for me? */
755                         if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
756                         {
757                                 /* Must I wait for him ? */
758                                 if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
759                                 {
760                                         /*
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.
766                                          */
767                                         RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
768                                         early_deadlock = true;
769                                         break;
770                                 }
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)
778                                 {
779                                         /* Skip the wait and just grant myself the lock. */
780                                         GrantLock(lock, proclock, lockmode);
781                                         GrantAwaitedLock();
782                                         return STATUS_OK;
783                                 }
784                                 /* Break out of loop to put myself before him */
785                                 break;
786                         }
787                         /* Nope, so advance to next waiter */
788                         aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
789                         proc = (PGPROC *) proc->links.next;
790                 }
791
792                 /*
793                  * If we fall out of loop normally, proc points to waitQueue head, so
794                  * we will insert at tail of queue as desired.
795                  */
796         }
797         else
798         {
799                 /* I hold no locks, so I can't push in front of anyone. */
800                 proc = (PGPROC *) &(waitQueue->links);
801         }
802
803         /*
804          * Insert self into queue, ahead of the given proc (or at tail of queue).
805          */
806         SHMQueueInsertBefore(&(proc->links), &(MyProc->links));
807         waitQueue->size++;
808
809         lock->waitMask |= LOCKBIT_ON(lockmode);
810
811         /* Set up wait information in PGPROC object, too */
812         MyProc->waitLock = lock;
813         MyProc->waitProcLock = proclock;
814         MyProc->waitLockMode = lockmode;
815
816         MyProc->waitStatus = STATUS_WAITING;
817
818         /*
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.
822          */
823         if (early_deadlock)
824         {
825                 RemoveFromWaitQueue(MyProc, hashcode);
826                 return STATUS_ERROR;
827         }
828
829         /* mark that we are waiting for a lock */
830         lockAwaited = locallock;
831
832         /*
833          * Release the lock table's partition lock.
834          *
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.
839          */
840         LWLockRelease(partitionLock);
841
842         /* Reset deadlock_state before enabling the signal handler */
843         deadlock_state = DS_NOT_YET_CHECKED;
844
845         /*
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.
850          *
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.
853          */
854         if (!enable_sig_alarm(DeadlockTimeout, false))
855                 elog(FATAL, "could not set timer for process wakeup");
856
857         /*
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.
865          *
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.
873          */
874         do
875         {
876                 PGSemaphoreLock(&MyProc->sem, true);
877
878                 /*
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).
882                  */
883                 myWaitStatus = MyProc->waitStatus;
884
885                 /*
886                  * If we are not deadlocked, but are waiting on an autovacuum-induced
887                  * task, send a signal to interrupt it.
888                  */
889                 if (deadlock_state == DS_BLOCKED_BY_AUTOVACUUM && allow_autovacuum_cancel)
890                 {
891                         PGPROC     *autovac = GetBlockingAutoVacuumPgproc();
892
893                         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
894
895                         /*
896                          * Only do it if the worker is not working to protect against Xid
897                          * wraparound.
898                          */
899                         if ((autovac != NULL) &&
900                                 (autovac->vacuumFlags & PROC_IS_AUTOVACUUM) &&
901                                 !(autovac->vacuumFlags & PROC_VACUUM_FOR_WRAPAROUND))
902                         {
903                                 int                     pid = autovac->pid;
904
905                                 elog(DEBUG2, "sending cancel to blocking autovacuum pid = %d",
906                                          pid);
907
908                                 /* don't hold the lock across the kill() syscall */
909                                 LWLockRelease(ProcArrayLock);
910
911                                 /* send the autovacuum worker Back to Old Kent Road */
912                                 if (kill(pid, SIGINT) < 0)
913                                 {
914                                         /* Just a warning to allow multiple callers */
915                                         ereport(WARNING,
916                                                         (errmsg("could not send signal to process %d: %m",
917                                                                         pid)));
918                                 }
919                         }
920                         else
921                                 LWLockRelease(ProcArrayLock);
922
923                         /* prevent signal from being resent more than once */
924                         allow_autovacuum_cancel = false;
925                 }
926
927                 /*
928                  * If awoken after the deadlock check interrupt has run, and
929                  * log_lock_waits is on, then report about the wait.
930                  */
931                 if (log_lock_waits && deadlock_state != DS_NOT_YET_CHECKED)
932                 {
933                         StringInfoData buf;
934                         const char *modename;
935                         long            secs;
936                         int                     usecs;
937                         long            msecs;
938
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;
947
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)
953                         {
954                                 /*
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.
960                                  */
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)));
964                         }
965
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
975                         {
976                                 Assert(myWaitStatus == STATUS_ERROR);
977
978                                 /*
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.
985                                  */
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)));
990                         }
991
992                         /*
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.
995                          */
996                         deadlock_state = DS_NO_DEADLOCK;
997
998                         pfree(buf.data);
999                 }
1000         } while (myWaitStatus == STATUS_WAITING);
1001
1002         /*
1003          * Disable the timer, if it's still running
1004          */
1005         if (!disable_sig_alarm(false))
1006                 elog(FATAL, "could not disable timer for process wakeup");
1007
1008         /*
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).
1012          */
1013         LWLockAcquire(partitionLock, LW_EXCLUSIVE);
1014
1015         /*
1016          * We no longer want LockWaitCancel to do anything.
1017          */
1018         lockAwaited = NULL;
1019
1020         /*
1021          * If we got the lock, be sure to remember it in the locallock table.
1022          */
1023         if (MyProc->waitStatus == STATUS_OK)
1024                 GrantAwaitedLock();
1025
1026         /*
1027          * We don't have to do anything else, because the awaker did all the
1028          * necessary update of the lock table and MyProc.
1029          */
1030         return MyProc->waitStatus;
1031 }
1032
1033
1034 /*
1035  * ProcWakeup -- wake up a process by releasing its private semaphore.
1036  *
1037  *       Also remove the process from the wait queue and set its links invalid.
1038  *       RETURN: the next process in the wait queue.
1039  *
1040  * The appropriate lock partition lock must be held by caller.
1041  *
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.
1046  */
1047 PGPROC *
1048 ProcWakeup(PGPROC *proc, int waitStatus)
1049 {
1050         PGPROC     *retProc;
1051
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);
1057
1058         /* Save next process before we zap the list link */
1059         retProc = (PGPROC *) proc->links.next;
1060
1061         /* Remove process from wait queue */
1062         SHMQueueDelete(&(proc->links));
1063         (proc->waitLock->waitProcs.size)--;
1064
1065         /* Clean up process' state and pass it the ok/fail signal */
1066         proc->waitLock = NULL;
1067         proc->waitProcLock = NULL;
1068         proc->waitStatus = waitStatus;
1069
1070         /* And awaken it */
1071         PGSemaphoreUnlock(&proc->sem);
1072
1073         return retProc;
1074 }
1075
1076 /*
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.
1080  *
1081  * The appropriate lock partition lock must be held by caller.
1082  */
1083 void
1084 ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
1085 {
1086         PROC_QUEUE *waitQueue = &(lock->waitProcs);
1087         int                     queue_size = waitQueue->size;
1088         PGPROC     *proc;
1089         LOCKMASK        aheadRequests = 0;
1090
1091         Assert(queue_size >= 0);
1092
1093         if (queue_size == 0)
1094                 return;
1095
1096         proc = (PGPROC *) waitQueue->links.next;
1097
1098         while (queue_size-- > 0)
1099         {
1100                 LOCKMODE        lockmode = proc->waitLockMode;
1101
1102                 /*
1103                  * Waken if (a) doesn't conflict with requests of earlier waiters, and
1104                  * (b) doesn't conflict with already-held locks.
1105                  */
1106                 if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1107                         LockCheckConflicts(lockMethodTable,
1108                                                            lockmode,
1109                                                            lock,
1110                                                            proc->waitProcLock,
1111                                                            proc) == STATUS_OK)
1112                 {
1113                         /* OK to waken */
1114                         GrantLock(lock, proc->waitProcLock, lockmode);
1115                         proc = ProcWakeup(proc, STATUS_OK);
1116
1117                         /*
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.
1121                          */
1122                 }
1123                 else
1124                 {
1125                         /*
1126                          * Cannot wake this guy. Remember his request for later checks.
1127                          */
1128                         aheadRequests |= LOCKBIT_ON(lockmode);
1129                         proc = (PGPROC *) proc->links.next;
1130                 }
1131         }
1132
1133         Assert(waitQueue->size >= 0);
1134 }
1135
1136 /*
1137  * CheckDeadLock
1138  *
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.
1145  *
1146  * NB: this is run inside a signal handler, so be very wary about what is done
1147  * here or in called routines.
1148  */
1149 static void
1150 CheckDeadLock(void)
1151 {
1152         int                     i;
1153
1154         /*
1155          * Acquire exclusive lock on the entire shared lock data structures. Must
1156          * grab LWLocks in partition-number order to avoid LWLock deadlock.
1157          *
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.
1163          */
1164         for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1165                 LWLockAcquire(FirstLockMgrLock + i, LW_EXCLUSIVE);
1166
1167         /*
1168          * Check to see if we've been awoken by anyone in the interim.
1169          *
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.
1173          *
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.
1177          */
1178         if (MyProc->links.prev == NULL ||
1179                 MyProc->links.next == NULL)
1180                 goto check_done;
1181
1182 #ifdef LOCK_DEBUG
1183         if (Debug_deadlocks)
1184                 DumpAllLocks();
1185 #endif
1186
1187         /* Run the deadlock check, and set deadlock_state for use by ProcSleep */
1188         deadlock_state = DeadLockCheck(MyProc);
1189
1190         if (deadlock_state == DS_HARD_DEADLOCK)
1191         {
1192                 /*
1193                  * Oops.  We have a deadlock.
1194                  *
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.)
1199                  *
1200                  * RemoveFromWaitQueue sets MyProc->waitStatus to STATUS_ERROR, so
1201                  * ProcSleep will report an error after we return from the signal
1202                  * handler.
1203                  */
1204                 Assert(MyProc->waitLock != NULL);
1205                 RemoveFromWaitQueue(MyProc, LockTagHashCode(&(MyProc->waitLock->tag)));
1206
1207                 /*
1208                  * Unlock my semaphore so that the interrupted ProcSleep() call can
1209                  * finish.
1210                  */
1211                 PGSemaphoreUnlock(&MyProc->sem);
1212
1213                 /*
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.
1222                  */
1223         }
1224         else if (log_lock_waits || deadlock_state == DS_BLOCKED_BY_AUTOVACUUM)
1225         {
1226                 /*
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.
1231                  *
1232                  * If blocked by autovacuum, this wakeup will enable ProcSleep to send
1233                  * the cancelling signal to the autovacuum worker.
1234                  */
1235                 PGSemaphoreUnlock(&MyProc->sem);
1236         }
1237
1238         /*
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.
1244          */
1245 check_done:
1246         for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
1247                 LWLockRelease(FirstLockMgrLock + i);
1248 }
1249
1250
1251 /*
1252  * ProcWaitForSignal - wait for a signal from another backend.
1253  *
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.
1261  */
1262 void
1263 ProcWaitForSignal(void)
1264 {
1265         PGSemaphoreLock(&MyProc->sem, true);
1266 }
1267
1268 /*
1269  * ProcSendSignal - send a signal to a backend identified by PID
1270  */
1271 void
1272 ProcSendSignal(int pid)
1273 {
1274         PGPROC     *proc = BackendPidGetProc(pid);
1275
1276         if (proc != NULL)
1277                 PGSemaphoreUnlock(&proc->sem);
1278 }
1279
1280
1281 /*****************************************************************************
1282  * SIGALRM interrupt support
1283  *
1284  * Maybe these should be in pqsignal.c?
1285  *****************************************************************************/
1286
1287 /*
1288  * Enable the SIGALRM interrupt to fire after the specified delay
1289  *
1290  * Delay is given in milliseconds.      Caller should be sure a SIGALRM
1291  * signal handler is installed before this is called.
1292  *
1293  * This code properly handles nesting of deadlock timeout alarms within
1294  * statement timeout alarms.
1295  *
1296  * Returns TRUE if okay, FALSE on failure.
1297  */
1298 bool
1299 enable_sig_alarm(int delayms, bool is_statement_timeout)
1300 {
1301         TimestampTz fin_time;
1302         struct itimerval timeval;
1303
1304         if (is_statement_timeout)
1305         {
1306                 /*
1307                  * Begin statement-level timeout
1308                  *
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.
1320                  */
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;
1327         }
1328         else if (statement_timeout_active)
1329         {
1330                 /*
1331                  * Begin deadlock timeout with statement-level timeout active
1332                  *
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.
1337                  *
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.
1343                  */
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;
1349         }
1350         else
1351         {
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();
1357         }
1358
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;
1366 }
1367
1368 /*
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.
1372  *
1373  * Returns TRUE if okay, FALSE on failure.
1374  */
1375 bool
1376 disable_sig_alarm(bool is_statement_timeout)
1377 {
1378         /*
1379          * Always disable the interrupt if it is active; this avoids being
1380          * interrupted by the signal handler and thereby possibly getting
1381          * confused.
1382          *
1383          * We will re-enable the interrupt if necessary in CheckStatementTimeout.
1384          */
1385         if (statement_timeout_active || deadlock_timeout_active)
1386         {
1387                 struct itimerval timeval;
1388
1389                 MemSet(&timeval, 0, sizeof(struct itimerval));
1390                 if (setitimer(ITIMER_REAL, &timeval, NULL))
1391                 {
1392                         statement_timeout_active = false;
1393                         cancel_from_timeout = false;
1394                         deadlock_timeout_active = false;
1395                         return false;
1396                 }
1397         }
1398
1399         /* Always cancel deadlock timeout, in case this is error cleanup */
1400         deadlock_timeout_active = false;
1401
1402         /* Cancel or reschedule statement timeout */
1403         if (is_statement_timeout)
1404         {
1405                 statement_timeout_active = false;
1406                 cancel_from_timeout = false;
1407         }
1408         else if (statement_timeout_active)
1409         {
1410                 if (!CheckStatementTimeout())
1411                         return false;
1412         }
1413         return true;
1414 }
1415
1416
1417 /*
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.
1421  *
1422  * Returns true if okay, false if failed to set the interrupt.
1423  */
1424 static bool
1425 CheckStatementTimeout(void)
1426 {
1427         TimestampTz now;
1428
1429         if (!statement_timeout_active)
1430                 return true;                    /* do nothing if not active */
1431
1432         now = GetCurrentTimestamp();
1433
1434         if (now >= statement_fin_time)
1435         {
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);
1444         }
1445         else
1446         {
1447                 /* Not time yet, so (re)schedule the interrupt */
1448                 long            secs;
1449                 int                     usecs;
1450                 struct itimerval timeval;
1451
1452                 TimestampDifference(now, statement_fin_time,
1453                                                         &secs, &usecs);
1454
1455                 /*
1456                  * It's possible that the difference is less than a microsecond;
1457                  * ensure we don't cancel, rather than set, the interrupt.
1458                  */
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;
1466         }
1467
1468         return true;
1469 }
1470
1471
1472 /*
1473  * Signal handler for SIGALRM
1474  *
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.
1479  */
1480 void
1481 handle_sig_alarm(SIGNAL_ARGS)
1482 {
1483         int                     save_errno = errno;
1484
1485         if (deadlock_timeout_active)
1486         {
1487                 deadlock_timeout_active = false;
1488                 CheckDeadLock();
1489         }
1490
1491         if (statement_timeout_active)
1492                 (void) CheckStatementTimeout();
1493
1494         errno = save_errno;
1495 }