proc.c 34.8 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * proc.c
4
 *	  routines to manage per-process shared memory data structure
5
 *
P
 
PostgreSQL Daemon 已提交
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
11
 *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.167 2005/10/15 02:49:26 momjian Exp $
12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
/*
 * Interface (a):
17
 *		ProcSleep(), ProcWakeup(),
18 19
 *		ProcQueueAlloc() -- create a shm queue for sleeping processes
 *		ProcQueueInit() -- create a queue without allocing memory
20 21 22 23 24 25 26 27
 *
 * Locking and waiting for buffers can cause the backend to be
 * put to sleep.  Whoever releases the lock, etc. wakes the
 * process up again (and gives it an error code so it knows
 * whether it was awoken on an error condition).
 *
 * Interface (b):
 *
28 29
 * ProcReleaseLocks -- frees the locks associated with current transaction
 *
30
 * ProcKill -- destroys the shared memory state (and locks)
31
 *		associated with the process.
32
 */
33 34
#include "postgres.h"

35
#include <signal.h>
36 37
#include <unistd.h>
#include <sys/time.h>
M
Marc G. Fournier 已提交
38

39
#include "miscadmin.h"
40
#include "access/xact.h"
41
#include "storage/bufmgr.h"
42
#include "storage/ipc.h"
43
#include "storage/proc.h"
44
#include "storage/procarray.h"
45
#include "storage/spin.h"
46

47

48
/* GUC variables */
B
Bruce Momjian 已提交
49
int			DeadlockTimeout = 1000;
50
int			StatementTimeout = 0;
M
 
Marc G. Fournier 已提交
51

52
/* Pointer to this process's PGPROC struct, if any */
J
Jan Wieck 已提交
53
PGPROC	   *MyProc = NULL;
54 55

/*
J
Jan Wieck 已提交
56
 * This spinlock protects the freelist of recycled PGPROC structures.
57
 * We cannot use an LWLock because the LWLock manager depends on already
J
Jan Wieck 已提交
58
 * having a PGPROC and a wait semaphore!  But these structures are touched
59 60
 * relatively infrequently (only at backend startup or shutdown) and not for
 * very long, so a spinlock is okay.
61
 */
62
NON_EXEC_STATIC slock_t *ProcStructLock = NULL;
63

64
/* Pointers to shared-memory structures */
65
static PROC_HDR *ProcGlobal = NULL;
66
static PGPROC *DummyProcs = NULL;
67

68 69
static bool waitingForLock = false;

70 71 72
/* Mark these volatile because they can be changed by signal handler */
static volatile bool statement_timeout_active = false;
static volatile bool deadlock_timeout_active = false;
73
volatile bool cancel_from_timeout = false;
B
Bruce Momjian 已提交
74

75 76 77 78
/* statement_fin_time is valid only if statement_timeout_active is true */
static struct timeval statement_fin_time;


79 80
static void ProcKill(int code, Datum arg);
static void DummyProcKill(int code, Datum arg);
81
static bool CheckStatementTimeout(void);
82

V
Vadim B. Mikheev 已提交
83

84 85 86
/*
 * Report shared-memory space needed by InitProcGlobal.
 */
87
Size
88
ProcGlobalShmemSize(void)
89
{
90 91 92 93 94 95 96 97 98 99
	Size		size = 0;

	/* ProcGlobal */
	size = add_size(size, sizeof(PROC_HDR));
	/* DummyProcs */
	size = add_size(size, mul_size(NUM_DUMMY_PROCS, sizeof(PGPROC)));
	/* MyProcs */
	size = add_size(size, mul_size(MaxBackends, sizeof(PGPROC)));
	/* ProcStructLock */
	size = add_size(size, sizeof(slock_t));
100 101 102 103

	return size;
}

104 105 106 107
/*
 * Report number of semaphores needed by InitProcGlobal.
 */
int
108
ProcGlobalSemas(void)
109
{
110
	/* We need a sema per backend, plus one for each dummy process. */
111
	return MaxBackends + NUM_DUMMY_PROCS;
112 113
}

114 115
/*
 * InitProcGlobal -
116
 *	  Initialize the global process table during postmaster startup.
117
 *
118
 *	  We also create all the per-process semaphores we will need to support
119 120 121 122 123 124 125 126 127
 *	  the requested number of backends.  We used to allocate semaphores
 *	  only when backends were actually started up, but that is bad because
 *	  it lets Postgres fail under load --- a lot of Unix systems are
 *	  (mis)configured with small limits on the number of semaphores, and
 *	  running out when trying to start another backend is a common failure.
 *	  So, now we grab enough semaphores to support the desired max number
 *	  of backends immediately at initialization --- if the sysadmin has set
 *	  MaxBackends higher than his kernel will support, he'll find out sooner
 *	  rather than later.
128 129 130 131
 *
 *	  Another reason for creating semaphores here is that the semaphore
 *	  implementation typically requires us to create semaphores in the
 *	  postmaster, not in backends.
132 133
 */
void
134
InitProcGlobal(void)
135
{
B
Bruce Momjian 已提交
136 137
	bool		foundProcGlobal,
				foundDummy;
138

139
	/* Create or attach to the ProcGlobal shared structure */
140
	ProcGlobal = (PROC_HDR *)
141
		ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &foundProcGlobal);
142

143
	/*
144
	 * Create or attach to the PGPROC structures for dummy (bgwriter)
145
	 * processes, too.	These do not get linked into the freeProcs list.
146
	 */
147
	DummyProcs = (PGPROC *)
148
		ShmemInitStruct("DummyProcs", NUM_DUMMY_PROCS * sizeof(PGPROC),
149
						&foundDummy);
150 151 152 153 154 155 156

	if (foundProcGlobal || foundDummy)
	{
		/* both should be present or neither */
		Assert(foundProcGlobal && foundDummy);
	}
	else
157
	{
158 159 160
		/*
		 * We're the first - initialize.
		 */
161
		PGPROC	   *procs;
162
		int			i;
163

164
		ProcGlobal->freeProcs = INVALID_OFFSET;
165

166 167
		ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;

B
Bruce Momjian 已提交
168
		/*
B
Bruce Momjian 已提交
169
		 * Pre-create the PGPROC structures and create a semaphore for each.
170
		 */
171
		procs = (PGPROC *) ShmemAlloc(MaxBackends * sizeof(PGPROC));
172 173 174 175
		if (!procs)
			ereport(FATAL,
					(errcode(ERRCODE_OUT_OF_MEMORY),
					 errmsg("out of shared memory")));
176 177
		MemSet(procs, 0, MaxBackends * sizeof(PGPROC));
		for (i = 0; i < MaxBackends; i++)
178
		{
179 180 181
			PGSemaphoreCreate(&(procs[i].sem));
			procs[i].links.next = ProcGlobal->freeProcs;
			ProcGlobal->freeProcs = MAKE_OFFSET(&procs[i]);
182
		}
183

184
		MemSet(DummyProcs, 0, NUM_DUMMY_PROCS * sizeof(PGPROC));
J
Jan Wieck 已提交
185 186
		for (i = 0; i < NUM_DUMMY_PROCS; i++)
		{
187 188
			DummyProcs[i].pid = 0;		/* marks dummy proc as not in use */
			PGSemaphoreCreate(&(DummyProcs[i].sem));
J
Jan Wieck 已提交
189
		}
190 191 192 193

		/* Create ProcStructLock spinlock, too */
		ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));
		SpinLockInit(ProcStructLock);
194 195 196
	}
}

197
/*
198
 * InitProcess -- initialize a per-process data structure for this backend
199 200
 */
void
201
InitProcess(void)
202
{
203
	SHMEM_OFFSET myOffset;
B
Bruce Momjian 已提交
204

205 206
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;
207 208

	/*
B
Bruce Momjian 已提交
209 210
	 * ProcGlobal should be set by a previous call to InitProcGlobal (if we
	 * are a backend, we inherit this by fork() from the postmaster).
211
	 */
212
	if (procglobal == NULL)
213
		elog(PANIC, "proc header uninitialized");
214 215

	if (MyProc != NULL)
216
		elog(ERROR, "you already exist");
217

218
	/*
B
Bruce Momjian 已提交
219 220
	 * Try to get a proc struct from the free list.  If this fails, we must be
	 * out of PGPROC structures (not to mention semaphores).
221
	 *
B
Bruce Momjian 已提交
222 223
	 * While we are holding the ProcStructLock, also copy the current shared
	 * estimate of spins_per_delay to local storage.
224
	 */
225
	SpinLockAcquire(ProcStructLock);
226

227 228
	set_spins_per_delay(procglobal->spins_per_delay);

229
	myOffset = procglobal->freeProcs;
230 231

	if (myOffset != INVALID_OFFSET)
232
	{
J
Jan Wieck 已提交
233
		MyProc = (PGPROC *) MAKE_PTR(myOffset);
234
		procglobal->freeProcs = MyProc->links.next;
235
		SpinLockRelease(ProcStructLock);
236 237 238 239
	}
	else
	{
		/*
B
Bruce Momjian 已提交
240 241 242
		 * If we reach here, all the PGPROCs are in use.  This is one of the
		 * possible places to detect "too many backends", so give the standard
		 * error message.
243
		 */
244
		SpinLockRelease(ProcStructLock);
245 246 247
		ereport(FATAL,
				(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
				 errmsg("sorry, too many clients already")));
248
	}
249

250
	/*
B
Bruce Momjian 已提交
251 252
	 * Initialize all fields of MyProc, except for the semaphore which was
	 * prepared for us by InitProcGlobal.
253
	 */
254
	SHMQueueElemInit(&(MyProc->links));
255
	MyProc->waitStatus = STATUS_OK;
256
	MyProc->xid = InvalidTransactionId;
257
	MyProc->xmin = InvalidTransactionId;
258 259
	MyProc->pid = MyProcPid;
	MyProc->databaseId = MyDatabaseId;
260 261
	/* Will be set properly after the session role id is determined */
	MyProc->roleId = InvalidOid;
262 263 264
	MyProc->lwWaiting = false;
	MyProc->lwExclusive = false;
	MyProc->lwWaitLink = NULL;
265
	MyProc->waitLock = NULL;
266 267
	MyProc->waitProcLock = NULL;
	SHMQueueInit(&(MyProc->procLocks));
268

269 270 271
	/*
	 * Add our PGPROC to the PGPROC array in shared memory.
	 */
272
	ProcArrayAdd(MyProc);
273

274
	/*
275
	 * Arrange to clean up at backend exit.
276
	 */
277
	on_shmem_exit(ProcKill, 0);
278

279
	/*
B
Bruce Momjian 已提交
280 281
	 * We might be reusing a semaphore that belonged to a failed process. So
	 * be careful and reinitialize its value here.
282
	 */
283
	PGSemaphoreReset(&MyProc->sem);
284

285
	/*
B
Bruce Momjian 已提交
286 287
	 * Now that we have a PGPROC, we could try to acquire locks, so initialize
	 * the deadlock checker.
288 289
	 */
	InitDeadLockChecking();
290 291
}

292 293 294
/*
 * InitDummyProcess -- create a dummy per-process data structure
 *
295 296 297 298
 * This is called by bgwriter and similar processes so that they will have a
 * MyProc value that's real enough to let them wait for LWLocks.  The PGPROC
 * and sema that are assigned are the extra ones created during
 * InitProcGlobal.
299 300 301
 *
 * Dummy processes are presently not expected to wait for real (lockmgr)
 * locks, nor to participate in sinval messaging.
302 303
 */
void
J
Jan Wieck 已提交
304
InitDummyProcess(int proctype)
305
{
B
Bruce Momjian 已提交
306
	PGPROC	   *dummyproc;
J
Jan Wieck 已提交
307

308
	/*
309 310
	 * ProcGlobal should be set by a previous call to InitProcGlobal (we
	 * inherit this by fork() from the postmaster).
311
	 */
312
	if (ProcGlobal == NULL || DummyProcs == NULL)
313
		elog(PANIC, "proc header uninitialized");
314 315

	if (MyProc != NULL)
316
		elog(ERROR, "you already exist");
317

318 319
	Assert(proctype >= 0 && proctype < NUM_DUMMY_PROCS);

320 321 322 323
	/*
	 * Just for paranoia's sake, we use the ProcStructLock to protect
	 * assignment and releasing of DummyProcs entries.
	 *
B
Bruce Momjian 已提交
324 325
	 * While we are holding the ProcStructLock, also copy the current shared
	 * estimate of spins_per_delay to local storage.
326 327 328 329 330
	 */
	SpinLockAcquire(ProcStructLock);

	set_spins_per_delay(ProcGlobal->spins_per_delay);

331
	dummyproc = &DummyProcs[proctype];
J
Jan Wieck 已提交
332

333
	/*
J
Jan Wieck 已提交
334
	 * dummyproc should not presently be in use by anyone else
335
	 */
J
Jan Wieck 已提交
336
	if (dummyproc->pid != 0)
337 338
	{
		SpinLockRelease(ProcStructLock);
J
Jan Wieck 已提交
339
		elog(FATAL, "DummyProc[%d] is in use by PID %d",
340
			 proctype, dummyproc->pid);
341
	}
J
Jan Wieck 已提交
342
	MyProc = dummyproc;
343

344 345 346 347
	MyProc->pid = MyProcPid;	/* marks dummy proc as in use by me */

	SpinLockRelease(ProcStructLock);

348
	/*
B
Bruce Momjian 已提交
349 350
	 * Initialize all fields of MyProc, except MyProc->sem which was set up by
	 * InitProcGlobal.
351 352
	 */
	SHMQueueElemInit(&(MyProc->links));
353
	MyProc->waitStatus = STATUS_OK;
354 355 356
	MyProc->xid = InvalidTransactionId;
	MyProc->xmin = InvalidTransactionId;
	MyProc->databaseId = MyDatabaseId;
357
	MyProc->roleId = InvalidOid;
358 359 360 361
	MyProc->lwWaiting = false;
	MyProc->lwExclusive = false;
	MyProc->lwWaitLink = NULL;
	MyProc->waitLock = NULL;
362 363
	MyProc->waitProcLock = NULL;
	SHMQueueInit(&(MyProc->procLocks));
364 365 366 367

	/*
	 * Arrange to clean up at process exit.
	 */
368
	on_shmem_exit(DummyProcKill, Int32GetDatum(proctype));
369 370

	/*
B
Bruce Momjian 已提交
371 372
	 * We might be reusing a semaphore that belonged to a failed process. So
	 * be careful and reinitialize its value here.
373
	 */
374
	PGSemaphoreReset(&MyProc->sem);
375 376
}

377 378 379 380 381 382 383 384 385 386
/*
 * Check whether there are at least N free PGPROC objects.
 *
 * Note: this is designed on the assumption that N will generally be small.
 */
bool
HaveNFreeProcs(int n)
{
	SHMEM_OFFSET offset;
	PGPROC	   *proc;
B
Bruce Momjian 已提交
387

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;

	SpinLockAcquire(ProcStructLock);

	offset = procglobal->freeProcs;

	while (n > 0 && offset != INVALID_OFFSET)
	{
		proc = (PGPROC *) MAKE_PTR(offset);
		offset = proc->links.next;
		n--;
	}

	SpinLockRelease(ProcStructLock);

	return (n <= 0);
}

407 408 409
/*
 * Cancel any pending wait for lock, when aborting a transaction.
 *
410 411
 * Returns true if we had been waiting for a lock, else false.
 *
412
 * (Normally, this would only happen if we accept a cancel/die
413
 * interrupt while waiting; but an ereport(ERROR) while waiting is
414 415
 * within the realm of possibility, too.)
 */
416
bool
417 418 419 420
LockWaitCancel(void)
{
	/* Nothing to do if we weren't waiting for a lock */
	if (!waitingForLock)
421 422
		return false;

423
	/* Turn off the deadlock timer, if it's still running (see ProcSleep) */
424
	disable_sig_alarm(false);
425 426

	/* Unlink myself from the wait queue, if on it (might not be anymore!) */
427
	LWLockAcquire(LockMgrLock, LW_EXCLUSIVE);
428

429
	if (MyProc->links.next != INVALID_OFFSET)
430 431 432
	{
		/* We could not have been granted the lock yet */
		Assert(MyProc->waitStatus == STATUS_ERROR);
433
		RemoveFromWaitQueue(MyProc);
434 435 436 437 438
	}
	else
	{
		/*
		 * Somebody kicked us off the lock queue already.  Perhaps they
B
Bruce Momjian 已提交
439 440 441
		 * granted us the lock, or perhaps they detected a deadlock. If they
		 * did grant us the lock, we'd better remember it in our local lock
		 * table.
442
		 */
443 444
		if (MyProc->waitStatus == STATUS_OK)
			GrantAwaitedLock();
445 446 447 448
	}

	waitingForLock = false;

449
	LWLockRelease(LockMgrLock);
H
Hiroshi Inoue 已提交
450

451 452 453
	/*
	 * Reset the proc wait semaphore to zero.  This is necessary in the
	 * scenario where someone else granted us the lock we wanted before we
B
Bruce Momjian 已提交
454 455 456 457 458
	 * were able to remove ourselves from the wait-list.  The semaphore will
	 * have been bumped to 1 by the would-be grantor, and since we are no
	 * longer going to wait on the sema, we have to force it back to zero.
	 * Otherwise, our next attempt to wait for a lock will fall through
	 * prematurely.
459
	 */
460
	PGSemaphoreReset(&MyProc->sem);
461 462

	/*
B
Bruce Momjian 已提交
463 464
	 * Return true even if we were kicked off the lock before we were able to
	 * remove ourselves.
465 466
	 */
	return true;
H
Hiroshi Inoue 已提交
467
}
468

469

470
/*
471
 * ProcReleaseLocks() -- release locks associated with current transaction
472
 *			at main transaction commit or abort
473 474 475 476 477 478
 *
 * At main transaction commit, we release all locks except session locks.
 * At main transaction abort, we release all locks including session locks;
 * this lets us clean up after a VACUUM FULL failure.
 *
 * At subtransaction commit, we don't release any locks (so this func is not
479
 * needed at all); we will defer the releasing to the parent transaction.
480
 * At subtransaction abort, we release all locks held by the subtransaction;
481 482
 * this is implemented by retail releasing of the locks under control of
 * the ResourceOwner mechanism.
483 484
 *
 * Note that user locks are not released in any case.
485 486
 */
void
487
ProcReleaseLocks(bool isCommit)
488
{
489 490
	if (!MyProc)
		return;
491 492 493
	/* If waiting, get off wait queue (should only be needed after error) */
	LockWaitCancel();
	/* Release locks */
494
	LockReleaseAll(DEFAULT_LOCKMETHOD, !isCommit);
495 496 497 498 499
}


/*
 * ProcKill() -- Destroy the per-proc data structure for
500
 *		this process. Release any of its held LW locks.
501 502
 */
static void
503
ProcKill(int code, Datum arg)
504
{
505 506 507
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;

508
	Assert(MyProc != NULL);
509

510
	/*
B
Bruce Momjian 已提交
511 512
	 * Release any LW locks I am holding.  There really shouldn't be any, but
	 * it's cheap to check again before we cut the knees off the LWLock
513
	 * facility by releasing our PGPROC ...
514
	 */
515
	LWLockReleaseAll();
516

517
	/* Remove our PGPROC from the PGPROC array in shared memory */
518
	ProcArrayRemove(MyProc);
519

520
	SpinLockAcquire(ProcStructLock);
521

J
Jan Wieck 已提交
522
	/* Return PGPROC structure (and semaphore) to freelist */
523 524
	MyProc->links.next = procglobal->freeProcs;
	procglobal->freeProcs = MAKE_OFFSET(MyProc);
525

J
Jan Wieck 已提交
526
	/* PGPROC struct isn't mine anymore */
527
	MyProc = NULL;
528

529 530 531
	/* Update shared estimate of spins_per_delay */
	procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay);

532 533 534 535
	SpinLockRelease(ProcStructLock);
}

/*
536
 * DummyProcKill() -- Cut-down version of ProcKill for dummy (bgwriter)
J
Jan Wieck 已提交
537
 *		processes.	The PGPROC and sema are not released, only marked
538 539 540
 *		as not-in-use.
 */
static void
541
DummyProcKill(int code, Datum arg)
542
{
B
Bruce Momjian 已提交
543 544
	int			proctype = DatumGetInt32(arg);
	PGPROC	   *dummyproc;
J
Jan Wieck 已提交
545

546
	Assert(proctype >= 0 && proctype < NUM_DUMMY_PROCS);
J
Jan Wieck 已提交
547

548
	dummyproc = &DummyProcs[proctype];
J
Jan Wieck 已提交
549

550
	Assert(MyProc == dummyproc);
551

552
	/* Release any LW locks I am holding (see notes above) */
553 554
	LWLockReleaseAll();

555 556
	SpinLockAcquire(ProcStructLock);

557
	/* Mark dummy proc no longer in use */
558 559
	MyProc->pid = 0;

J
Jan Wieck 已提交
560
	/* PGPROC struct isn't mine anymore */
561
	MyProc = NULL;
562 563 564 565 566

	/* Update shared estimate of spins_per_delay */
	ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);

	SpinLockRelease(ProcStructLock);
567 568
}

569

570 571
/*
 * ProcQueue package: routines for putting processes to sleep
572
 *		and  waking them up
573 574 575 576 577 578 579 580
 */

/*
 * ProcQueueAlloc -- alloc/attach to a shared memory process queue
 *
 * Returns: a pointer to the queue or NULL
 * Side Effects: Initializes the queue if we allocated one
 */
581
#ifdef NOT_USED
582
PROC_QUEUE *
583 584
ProcQueueAlloc(char *name)
{
585 586
	bool		found;
	PROC_QUEUE *queue = (PROC_QUEUE *)
B
Bruce Momjian 已提交
587
	ShmemInitStruct(name, sizeof(PROC_QUEUE), &found);
588 589

	if (!queue)
590
		return NULL;
591 592
	if (!found)
		ProcQueueInit(queue);
593
	return queue;
594
}
595
#endif
596 597 598 599 600

/*
 * ProcQueueInit -- initialize a shared memory process queue
 */
void
601
ProcQueueInit(PROC_QUEUE *queue)
602
{
603 604
	SHMQueueInit(&(queue->links));
	queue->size = 0;
605 606 607 608 609 610
}


/*
 * ProcSleep -- put a process to sleep
 *
611 612
 * Caller must have set MyProc->heldLocks to reflect locks already held
 * on the lockable object by this process (under all XIDs).
613
 *
614
 * Locktable's masterLock must be held at entry, and will be held
615
 * at exit.
616
 *
617
 * Result: STATUS_OK if we acquired the lock, STATUS_ERROR if not (deadlock).
618
 *
619
 * ASSUME: that no one will fiddle with the queue until after
620
 *		we release the masterLock.
621 622
 *
 * NOTES: The process queue is now a priority queue for locking.
623 624 625
 *
 * P() on the semaphore should put us to sleep.  The process
 * semaphore is normally zero, so when we try to acquire it, we sleep.
626 627
 */
int
628
ProcSleep(LockMethod lockMethodTable,
629 630
		  LOCKMODE lockmode,
		  LOCK *lock,
631
		  PROCLOCK *proclock)
632
{
B
Bruce Momjian 已提交
633
	LWLockId	masterLock = lockMethodTable->masterLock;
634
	PROC_QUEUE *waitQueue = &(lock->waitProcs);
635
	LOCKMASK	myHeldLocks = MyProc->heldLocks;
636
	bool		early_deadlock = false;
J
Jan Wieck 已提交
637
	PGPROC	   *proc;
638
	int			i;
639

640
	/*
641 642
	 * Determine where to add myself in the wait queue.
	 *
B
Bruce Momjian 已提交
643 644 645 646 647 648
	 * Normally I should go at the end of the queue.  However, if I already hold
	 * locks that conflict with the request of any previous waiter, put myself
	 * in the queue just in front of the first such waiter. This is not a
	 * necessary step, since deadlock detection would move me to before that
	 * waiter anyway; but it's relatively cheap to detect such a conflict
	 * immediately, and avoid delaying till deadlock timeout.
649
	 *
B
Bruce Momjian 已提交
650 651 652 653 654 655
	 * Special case: if I find I should go in front of some waiter, check to see
	 * if I conflict with already-held locks or the requests before that
	 * waiter.	If not, then just grant myself the requested lock immediately.
	 * This is the same as the test for immediate grant in LockAcquire, except
	 * we are only considering the part of the wait queue before my insertion
	 * point.
656 657
	 */
	if (myHeldLocks != 0)
V
Vadim B. Mikheev 已提交
658
	{
659
		LOCKMASK	aheadRequests = 0;
660

J
Jan Wieck 已提交
661
		proc = (PGPROC *) MAKE_PTR(waitQueue->links.next);
662
		for (i = 0; i < waitQueue->size; i++)
V
Vadim B. Mikheev 已提交
663
		{
664
			/* Must he wait for me? */
B
Bruce Momjian 已提交
665
			if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
V
Vadim B. Mikheev 已提交
666
			{
667
				/* Must I wait for him ? */
B
Bruce Momjian 已提交
668
				if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
669
				{
670
					/*
B
Bruce Momjian 已提交
671 672 673 674 675
					 * Yes, so we have a deadlock.	Easiest way to clean up
					 * correctly is to call RemoveFromWaitQueue(), but we
					 * can't do that until we are *on* the wait queue. So, set
					 * a flag to check below, and break out of loop.  Also,
					 * record deadlock info for later message.
676
					 */
677
					RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
678 679
					early_deadlock = true;
					break;
680
				}
681
				/* I must go before this waiter.  Check special case. */
B
Bruce Momjian 已提交
682
				if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
683 684 685
					LockCheckConflicts(lockMethodTable,
									   lockmode,
									   lock,
686
									   proclock,
687
									   MyProc) == STATUS_OK)
688
				{
689
					/* Skip the wait and just grant myself the lock. */
690
					GrantLock(lock, proclock, lockmode);
691
					GrantAwaitedLock();
692
					return STATUS_OK;
693 694
				}
				/* Break out of loop to put myself before him */
V
Vadim B. Mikheev 已提交
695
				break;
696
			}
697
			/* Nope, so advance to next waiter */
698
			aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
J
Jan Wieck 已提交
699
			proc = (PGPROC *) MAKE_PTR(proc->links.next);
V
Vadim B. Mikheev 已提交
700
		}
B
Bruce Momjian 已提交
701

702
		/*
B
Bruce Momjian 已提交
703 704
		 * If we fall out of loop normally, proc points to waitQueue head, so
		 * we will insert at tail of queue as desired.
705
		 */
706 707 708 709
	}
	else
	{
		/* I hold no locks, so I can't push in front of anyone. */
J
Jan Wieck 已提交
710
		proc = (PGPROC *) &(waitQueue->links);
V
Vadim B. Mikheev 已提交
711
	}
712

713
	/*
B
Bruce Momjian 已提交
714
	 * Insert self into queue, ahead of the given proc (or at tail of queue).
715
	 */
716
	SHMQueueInsertBefore(&(proc->links), &(MyProc->links));
B
Bruce Momjian 已提交
717
	waitQueue->size++;
718

719
	lock->waitMask |= LOCKBIT_ON(lockmode);
720

J
Jan Wieck 已提交
721
	/* Set up wait information in PGPROC object, too */
722
	MyProc->waitLock = lock;
723
	MyProc->waitProcLock = proclock;
724 725
	MyProc->waitLockMode = lockmode;

726
	MyProc->waitStatus = STATUS_ERROR;	/* initialize result for error */
727 728

	/*
B
Bruce Momjian 已提交
729 730 731
	 * If we detected deadlock, give up without waiting.  This must agree with
	 * CheckDeadLock's recovery code, except that we shouldn't release the
	 * semaphore since we haven't tried to lock it yet.
732 733 734 735 736 737
	 */
	if (early_deadlock)
	{
		RemoveFromWaitQueue(MyProc);
		return STATUS_ERROR;
	}
738

739 740 741
	/* mark that we are waiting for a lock */
	waitingForLock = true;

742
	/*
743
	 * Release the locktable's masterLock.
744
	 *
745
	 * NOTE: this may also cause us to exit critical-section state, possibly
B
Bruce Momjian 已提交
746 747
	 * allowing a cancel/die interrupt to be accepted. This is OK because we
	 * have recorded the fact that we are waiting for a lock, and so
748
	 * LockWaitCancel will clean up if cancel/die happens.
749
	 */
750
	LWLockRelease(masterLock);
751

752
	/*
B
Bruce Momjian 已提交
753 754 755 756
	 * Set timer so we can wake up after awhile and check for a deadlock. If a
	 * deadlock is detected, the handler releases the process's semaphore and
	 * sets MyProc->waitStatus = STATUS_ERROR, allowing us to know that we
	 * must report failure rather than success.
757
	 *
B
Bruce Momjian 已提交
758 759
	 * By delaying the check until we've waited for a bit, we can avoid running
	 * the rather expensive deadlock-check code in most cases.
760
	 */
761
	if (!enable_sig_alarm(DeadlockTimeout, false))
762
		elog(FATAL, "could not set timer for process wakeup");
763

764
	/*
765
	 * If someone wakes us between LWLockRelease and PGSemaphoreLock,
B
Bruce Momjian 已提交
766 767 768 769
	 * PGSemaphoreLock will not block.	The wakeup is "saved" by the semaphore
	 * implementation.	Note also that if CheckDeadLock is invoked but does
	 * not detect a deadlock, PGSemaphoreLock() will continue to wait.	There
	 * used to be a loop here, but it was useless code...
770
	 *
B
Bruce Momjian 已提交
771 772 773 774 775 776 777
	 * We pass interruptOK = true, which eliminates a window in which cancel/die
	 * interrupts would be held off undesirably.  This is a promise that we
	 * don't mind losing control to a cancel/die interrupt here.  We don't,
	 * because we have no shared-state-change work to do after being granted
	 * the lock (the grantor did it all).  We do have to worry about updating
	 * the locallock table, but if we lose control to an error, LockWaitCancel
	 * will fix that up.
778
	 */
779
	PGSemaphoreLock(&MyProc->sem, true);
780

781
	/*
782
	 * Disable the timer, if it's still running
B
Bruce Momjian 已提交
783
	 */
784
	if (!disable_sig_alarm(false))
785
		elog(FATAL, "could not disable timer for process wakeup");
B
Bruce Momjian 已提交
786

787
	/*
B
Bruce Momjian 已提交
788 789 790
	 * Re-acquire the locktable's masterLock.  We have to do this to hold off
	 * cancel/die interrupts before we can mess with waitingForLock (else we
	 * might have a missed or duplicated locallock update).
791 792 793 794 795
	 */
	LWLockAcquire(masterLock, LW_EXCLUSIVE);

	/*
	 * We no longer want LockWaitCancel to do anything.
796 797 798
	 */
	waitingForLock = false;

799
	/*
800
	 * If we got the lock, be sure to remember it in the locallock table.
801
	 */
802
	if (MyProc->waitStatus == STATUS_OK)
803
		GrantAwaitedLock();
804

805 806 807 808
	/*
	 * We don't have to do anything else, because the awaker did all the
	 * necessary update of the lock table and MyProc.
	 */
809
	return MyProc->waitStatus;
810 811 812 813 814 815
}


/*
 * ProcWakeup -- wake up a process by releasing its private semaphore.
 *
816
 *	 Also remove the process from the wait queue and set its links invalid.
817
 *	 RETURN: the next process in the wait queue.
818 819 820 821
 *
 * XXX: presently, this code is only used for the "success" case, and only
 * works correctly for that case.  To clean up in failure case, would need
 * to twiddle the lock's request counts too --- see RemoveFromWaitQueue.
822
 */
J
Jan Wieck 已提交
823
PGPROC *
824
ProcWakeup(PGPROC *proc, int waitStatus)
825
{
J
Jan Wieck 已提交
826
	PGPROC	   *retProc;
827

828
	/* assume that masterLock has been acquired */
829

830
	/* Proc should be sleeping ... */
831 832
	if (proc->links.prev == INVALID_OFFSET ||
		proc->links.next == INVALID_OFFSET)
833
		return NULL;
834

835
	/* Save next process before we zap the list link */
J
Jan Wieck 已提交
836
	retProc = (PGPROC *) MAKE_PTR(proc->links.next);
837

838
	/* Remove process from wait queue */
839
	SHMQueueDelete(&(proc->links));
840
	(proc->waitLock->waitProcs.size)--;
841

842 843
	/* Clean up process' state and pass it the ok/fail signal */
	proc->waitLock = NULL;
844
	proc->waitProcLock = NULL;
845
	proc->waitStatus = waitStatus;
846

847
	/* And awaken it */
848
	PGSemaphoreUnlock(&proc->sem);
849 850

	return retProc;
851 852 853 854
}

/*
 * ProcLockWakeup -- routine for waking up processes when a lock is
855 856
 *		released (or a prior waiter is aborted).  Scan all waiters
 *		for lock, waken any that are no longer blocked.
857
 */
858
void
859
ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
860
{
861 862
	PROC_QUEUE *waitQueue = &(lock->waitProcs);
	int			queue_size = waitQueue->size;
J
Jan Wieck 已提交
863
	PGPROC	   *proc;
864
	LOCKMASK	aheadRequests = 0;
M
 
Marc G. Fournier 已提交
865

866
	Assert(queue_size >= 0);
867

868 869
	if (queue_size == 0)
		return;
870

J
Jan Wieck 已提交
871
	proc = (PGPROC *) MAKE_PTR(waitQueue->links.next);
872

873 874
	while (queue_size-- > 0)
	{
B
Bruce Momjian 已提交
875
		LOCKMODE	lockmode = proc->waitLockMode;
M
 
Marc G. Fournier 已提交
876 877

		/*
B
Bruce Momjian 已提交
878 879
		 * Waken if (a) doesn't conflict with requests of earlier waiters, and
		 * (b) doesn't conflict with already-held locks.
M
 
Marc G. Fournier 已提交
880
		 */
B
Bruce Momjian 已提交
881
		if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
882 883 884
			LockCheckConflicts(lockMethodTable,
							   lockmode,
							   lock,
885
							   proc->waitProcLock,
886
							   proc) == STATUS_OK)
M
 
Marc G. Fournier 已提交
887
		{
888
			/* OK to waken */
889
			GrantLock(lock, proc->waitProcLock, lockmode);
890
			proc = ProcWakeup(proc, STATUS_OK);
B
Bruce Momjian 已提交
891

892
			/*
B
Bruce Momjian 已提交
893 894 895
			 * ProcWakeup removes proc from the lock's waiting process queue
			 * and returns the next proc in chain; don't use proc's next-link,
			 * because it's been cleared.
896
			 */
M
 
Marc G. Fournier 已提交
897
		}
898
		else
899
		{
B
Bruce Momjian 已提交
900
			/*
B
Bruce Momjian 已提交
901
			 * Cannot wake this guy. Remember his request for later checks.
B
Bruce Momjian 已提交
902
			 */
903
			aheadRequests |= LOCKBIT_ON(lockmode);
J
Jan Wieck 已提交
904
			proc = (PGPROC *) MAKE_PTR(proc->links.next);
905
		}
M
 
Marc G. Fournier 已提交
906
	}
907 908

	Assert(waitQueue->size >= 0);
909 910 911
}

/* --------------------
912
 * We only get to this routine if we got SIGALRM after DeadlockTimeout
913 914 915 916
 * while waiting for a lock to be released by some other process.  Look
 * to see if there's a deadlock; if not, just return and continue waiting.
 * If we have a real deadlock, remove ourselves from the lock's wait queue
 * and signal an error to ProcSleep.
917 918
 * --------------------
 */
919
static void
920
CheckDeadLock(void)
921
{
922
	/*
923 924
	 * Acquire locktable lock.	Note that the deadlock check interrupt had
	 * better not be enabled anywhere that this process itself holds the
925
	 * locktable lock, else this will wait forever.  Also note that
B
Bruce Momjian 已提交
926 927
	 * LWLockAcquire creates a critical section, so that this routine cannot
	 * be interrupted by cancel/die interrupts.
928
	 */
929
	LWLockAcquire(LockMgrLock, LW_EXCLUSIVE);
930

931
	/*
932 933
	 * Check to see if we've been awoken by anyone in the interim.
	 *
B
Bruce Momjian 已提交
934 935 936
	 * If we have we can return and resume our transaction -- happy day. Before
	 * we are awoken the process releasing the lock grants it to us so we know
	 * that we don't have to wait anymore.
937
	 *
938
	 * We check by looking to see if we've been unlinked from the wait queue.
B
Bruce Momjian 已提交
939 940
	 * This is quicker than checking our semaphore's state, since no kernel
	 * call is needed, and it is safe because we hold the locktable lock.
941 942 943 944
	 */
	if (MyProc->links.prev == INVALID_OFFSET ||
		MyProc->links.next == INVALID_OFFSET)
	{
945
		LWLockRelease(LockMgrLock);
946 947 948
		return;
	}

949
#ifdef LOCK_DEBUG
B
Bruce Momjian 已提交
950 951
	if (Debug_deadlocks)
		DumpAllLocks();
952 953
#endif

954
	if (!DeadLockCheck(MyProc))
B
Bruce Momjian 已提交
955
	{
956
		/* No deadlock, so keep waiting */
957
		LWLockRelease(LockMgrLock);
B
Bruce Momjian 已提交
958 959 960
		return;
	}

961
	/*
962 963 964
	 * Oops.  We have a deadlock.
	 *
	 * Get this process out of wait state.
965
	 */
966 967
	RemoveFromWaitQueue(MyProc);

968
	/*
B
Bruce Momjian 已提交
969 970
	 * Set MyProc->waitStatus to STATUS_ERROR so that ProcSleep will report an
	 * error after we return from the signal handler.
971
	 */
972
	MyProc->waitStatus = STATUS_ERROR;
973

974 975 976
	/*
	 * Unlock my semaphore so that the interrupted ProcSleep() call can
	 * finish.
977
	 */
978
	PGSemaphoreUnlock(&MyProc->sem);
979

980
	/*
B
Bruce Momjian 已提交
981 982 983 984 985 986 987 988
	 * We're done here.  Transaction abort caused by the error that ProcSleep
	 * will raise will cause any other locks we hold to be released, thus
	 * allowing other processes to wake up; we don't need to do that here.
	 * NOTE: an exception is that releasing locks we hold doesn't consider the
	 * possibility of waiters that were blocked behind us on the lock we just
	 * failed to get, and might now be wakable because we're not in front of
	 * them anymore.  However, RemoveFromWaitQueue took care of waking up any
	 * such processes.
989
	 */
990
	LWLockRelease(LockMgrLock);
991 992 993
}


994 995 996 997 998 999 1000 1001 1002 1003 1004
/*
 * ProcWaitForSignal - wait for a signal from another backend.
 *
 * This can share the semaphore normally used for waiting for locks,
 * since a backend could never be waiting for a lock and a signal at
 * the same time.  As with locks, it's OK if the signal arrives just
 * before we actually reach the waiting state.
 */
void
ProcWaitForSignal(void)
{
1005
	PGSemaphoreLock(&MyProc->sem, true);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
}

/*
 * ProcCancelWaitForSignal - clean up an aborted wait for signal
 *
 * We need this in case the signal arrived after we aborted waiting,
 * or if it arrived but we never reached ProcWaitForSignal() at all.
 * Caller should call this after resetting the signal request status.
 */
void
ProcCancelWaitForSignal(void)
{
1018
	PGSemaphoreReset(&MyProc->sem);
1019 1020 1021
}

/*
1022
 * ProcSendSignal - send a signal to a backend identified by PID
1023 1024
 */
void
1025
ProcSendSignal(int pid)
1026
{
1027
	PGPROC	   *proc = BackendPidGetProc(pid);
1028 1029

	if (proc != NULL)
1030
		PGSemaphoreUnlock(&proc->sem);
1031 1032 1033
}


1034 1035 1036 1037 1038 1039 1040 1041 1042
/*****************************************************************************
 * SIGALRM interrupt support
 *
 * Maybe these should be in pqsignal.c?
 *****************************************************************************/

/*
 * Enable the SIGALRM interrupt to fire after the specified delay
 *
1043
 * Delay is given in milliseconds.	Caller should be sure a SIGALRM
1044 1045
 * signal handler is installed before this is called.
 *
1046 1047
 * This code properly handles nesting of deadlock timeout alarms within
 * statement timeout alarms.
1048
 *
1049 1050 1051
 * Returns TRUE if okay, FALSE on failure.
 */
bool
1052
enable_sig_alarm(int delayms, bool is_statement_timeout)
1053
{
1054
	struct timeval fin_time;
B
Bruce Momjian 已提交
1055

1056
#ifndef __BEOS__
1057
	struct itimerval timeval;
1058
#else
1059
	bigtime_t	time_interval;
1060
#endif
1061

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	/* Compute target timeout time if we will need it */
	if (is_statement_timeout || statement_timeout_active)
	{
		gettimeofday(&fin_time, NULL);
		fin_time.tv_sec += delayms / 1000;
		fin_time.tv_usec += (delayms % 1000) * 1000;
		if (fin_time.tv_usec >= 1000000)
		{
			fin_time.tv_sec++;
			fin_time.tv_usec -= 1000000;
		}
	}

	if (is_statement_timeout)
	{
		/* Begin statement-level timeout */
		Assert(!deadlock_timeout_active);
		statement_fin_time = fin_time;
		statement_timeout_active = true;
1081
		cancel_from_timeout = false;
1082 1083 1084 1085 1086 1087
	}
	else if (statement_timeout_active)
	{
		/*
		 * Begin deadlock timeout with statement-level timeout active
		 *
B
Bruce Momjian 已提交
1088 1089 1090 1091
		 * Here, we want to interrupt at the closer of the two timeout times. If
		 * fin_time >= statement_fin_time then we need not touch the existing
		 * timer setting; else set up to interrupt at the deadlock timeout
		 * time.
1092 1093 1094
		 *
		 * NOTE: in this case it is possible that this routine will be
		 * interrupted by the previously-set timer alarm.  This is okay
B
Bruce Momjian 已提交
1095 1096 1097
		 * because the signal handler will do only what it should do according
		 * to the state variables.	The deadlock checker may get run earlier
		 * than normal, but that does no harm.
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
		 */
		deadlock_timeout_active = true;
		if (fin_time.tv_sec > statement_fin_time.tv_sec ||
			(fin_time.tv_sec == statement_fin_time.tv_sec &&
			 fin_time.tv_usec >= statement_fin_time.tv_usec))
			return true;
	}
	else
	{
		/* Begin deadlock timeout with no statement-level timeout */
		deadlock_timeout_active = true;
	}
1110

1111
	/* If we reach here, okay to set the timer interrupt */
1112
#ifndef __BEOS__
1113
	MemSet(&timeval, 0, sizeof(struct itimerval));
1114 1115
	timeval.it_value.tv_sec = delayms / 1000;
	timeval.it_value.tv_usec = (delayms % 1000) * 1000;
1116
	if (setitimer(ITIMER_REAL, &timeval, NULL))
1117 1118 1119
		return false;
#else
	/* BeOS doesn't have setitimer, but has set_alarm */
1120
	time_interval = delayms * 1000;		/* usecs */
1121
	if (set_alarm(time_interval, B_ONE_SHOT_RELATIVE_ALARM) < 0)
1122
		return false;
1123
#endif
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	return true;
}

/*
 * Cancel the SIGALRM timer, either for a deadlock timeout or a statement
 * timeout.  If a deadlock timeout is canceled, any active statement timeout
 * remains in force.
 *
 * Returns TRUE if okay, FALSE on failure.
 */
bool
disable_sig_alarm(bool is_statement_timeout)
{
	/*
	 * Always disable the interrupt if it is active; this avoids being
	 * interrupted by the signal handler and thereby possibly getting
	 * confused.
	 *
	 * We will re-enable the interrupt if necessary in CheckStatementTimeout.
	 */
	if (statement_timeout_active || deadlock_timeout_active)
1145 1146
	{
#ifndef __BEOS__
1147
		struct itimerval timeval;
1148

1149
		MemSet(&timeval, 0, sizeof(struct itimerval));
1150
		if (setitimer(ITIMER_REAL, &timeval, NULL))
1151
		{
1152 1153 1154
			statement_timeout_active = false;
			cancel_from_timeout = false;
			deadlock_timeout_active = false;
1155 1156
			return false;
		}
1157
#else
1158 1159 1160
		/* BeOS doesn't have setitimer, but has set_alarm */
		if (set_alarm(B_INFINITE_TIMEOUT, B_PERIODIC_ALARM) < 0)
		{
1161 1162 1163
			statement_timeout_active = false;
			cancel_from_timeout = false;
			deadlock_timeout_active = false;
1164
			return false;
1165
		}
1166
#endif
1167 1168
	}

1169 1170 1171 1172
	/* Always cancel deadlock timeout, in case this is error cleanup */
	deadlock_timeout_active = false;

	/* Cancel or reschedule statement timeout */
1173
	if (is_statement_timeout)
1174
	{
1175
		statement_timeout_active = false;
1176 1177
		cancel_from_timeout = false;
	}
1178 1179 1180 1181 1182
	else if (statement_timeout_active)
	{
		if (!CheckStatementTimeout())
			return false;
	}
1183 1184 1185
	return true;
}

1186

1187
/*
1188 1189 1190
 * Check for statement timeout.  If the timeout time has come,
 * trigger a query-cancel interrupt; if not, reschedule the SIGALRM
 * interrupt to occur at the right time.
1191
 *
1192
 * Returns true if okay, false if failed to set the interrupt.
1193
 */
1194 1195
static bool
CheckStatementTimeout(void)
1196
{
1197
	struct timeval now;
B
Bruce Momjian 已提交
1198

1199 1200 1201 1202
	if (!statement_timeout_active)
		return true;			/* do nothing if not active */

	gettimeofday(&now, NULL);
1203

1204 1205 1206
	if (now.tv_sec > statement_fin_time.tv_sec ||
		(now.tv_sec == statement_fin_time.tv_sec &&
		 now.tv_usec >= statement_fin_time.tv_usec))
1207
	{
1208 1209
		/* Time to die */
		statement_timeout_active = false;
1210
		cancel_from_timeout = true;
1211
		kill(MyProcPid, SIGINT);
1212 1213 1214 1215
	}
	else
	{
		/* Not time yet, so (re)schedule the interrupt */
1216
#ifndef __BEOS__
1217 1218
		struct itimerval timeval;

1219
		MemSet(&timeval, 0, sizeof(struct itimerval));
1220 1221 1222 1223 1224 1225 1226 1227
		timeval.it_value.tv_sec = statement_fin_time.tv_sec - now.tv_sec;
		timeval.it_value.tv_usec = statement_fin_time.tv_usec - now.tv_usec;
		if (timeval.it_value.tv_usec < 0)
		{
			timeval.it_value.tv_sec--;
			timeval.it_value.tv_usec += 1000000;
		}
		if (setitimer(ITIMER_REAL, &timeval, NULL))
1228 1229 1230
			return false;
#else
		/* BeOS doesn't have setitimer, but has set_alarm */
1231
		bigtime_t	time_interval;
B
Bruce Momjian 已提交
1232

1233 1234 1235 1236
		time_interval =
			(statement_fin_time.tv_sec - now.tv_sec) * 1000000 +
			(statement_fin_time.tv_usec - now.tv_usec);
		if (set_alarm(time_interval, B_ONE_SHOT_RELATIVE_ALARM) < 0)
1237 1238 1239 1240
			return false;
#endif
	}

1241 1242
	return true;
}
1243 1244 1245


/*
1246 1247 1248 1249 1250 1251
 * Signal handler for SIGALRM
 *
 * Process deadlock check and/or statement timeout check, as needed.
 * To avoid various edge cases, we must be careful to do nothing
 * when there is nothing to be done.  We also need to be able to
 * reschedule the timer interrupt if called before end of statement.
1252 1253 1254 1255
 */
void
handle_sig_alarm(SIGNAL_ARGS)
{
1256 1257 1258
	int			save_errno = errno;

	if (deadlock_timeout_active)
1259
	{
1260
		deadlock_timeout_active = false;
1261 1262
		CheckDeadLock();
	}
1263 1264 1265 1266 1267

	if (statement_timeout_active)
		(void) CheckStatementTimeout();

	errno = save_errno;
1268
}