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

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

38
#include "access/transam.h"
39
#include "access/twophase.h"
40
#include "access/xact.h"
41
#include "miscadmin.h"
42
#include "postmaster/autovacuum.h"
R
Robert Haas 已提交
43
#include "replication/slot.h"
44
#include "replication/syncrep.h"
45
#include "storage/ipc.h"
46
#include "storage/lmgr.h"
47
#include "storage/pmsignal.h"
48
#include "storage/proc.h"
49
#include "storage/procarray.h"
50
#include "storage/procsignal.h"
51
#include "storage/spin.h"
52
#include "utils/timeout.h"
53
#include "utils/timestamp.h"
54

55

56
/* GUC variables */
B
Bruce Momjian 已提交
57
int			DeadlockTimeout = 1000;
58
int			StatementTimeout = 0;
59
int			LockTimeout = 0;
60
bool		log_lock_waits = false;
M
 
Marc G. Fournier 已提交
61

62
/* Pointer to this process's PGPROC and PGXACT structs, if any */
J
Jan Wieck 已提交
63
PGPROC	   *MyProc = NULL;
64
PGXACT	   *MyPgXact = NULL;
65 66

/*
J
Jan Wieck 已提交
67
 * This spinlock protects the freelist of recycled PGPROC structures.
68
 * We cannot use an LWLock because the LWLock manager depends on already
J
Jan Wieck 已提交
69
 * having a PGPROC and a wait semaphore!  But these structures are touched
70 71
 * relatively infrequently (only at backend startup or shutdown) and not for
 * very long, so a spinlock is okay.
72
 */
73
NON_EXEC_STATIC slock_t *ProcStructLock = NULL;
74

75
/* Pointers to shared-memory structures */
76
PROC_HDR   *ProcGlobal = NULL;
77
NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL;
78
PGPROC	   *PreparedXactProcs = NULL;
79

80 81
/* If we are waiting for a lock, this points to the associated LOCALLOCK */
static LOCALLOCK *lockAwaited = NULL;
82

83
/* Mark this volatile because it can be changed by signal handler */
84
static volatile DeadLockState deadlock_state = DS_NOT_YET_CHECKED;
85 86


87
static void RemoveProcFromArray(int code, Datum arg);
88
static void ProcKill(int code, Datum arg);
89
static void AuxiliaryProcKill(int code, Datum arg);
90

V
Vadim B. Mikheev 已提交
91

92 93 94
/*
 * Report shared-memory space needed by InitProcGlobal.
 */
95
Size
96
ProcGlobalShmemSize(void)
97
{
98 99 100 101
	Size		size = 0;

	/* ProcGlobal */
	size = add_size(size, sizeof(PROC_HDR));
102
	/* MyProcs, including autovacuum workers and launcher */
103
	size = add_size(size, mul_size(MaxBackends, sizeof(PGPROC)));
104 105 106 107
	/* AuxiliaryProcs */
	size = add_size(size, mul_size(NUM_AUXILIARY_PROCS, sizeof(PGPROC)));
	/* Prepared xacts */
	size = add_size(size, mul_size(max_prepared_xacts, sizeof(PGPROC)));
108 109
	/* ProcStructLock */
	size = add_size(size, sizeof(slock_t));
110

111 112 113 114
	size = add_size(size, mul_size(MaxBackends, sizeof(PGXACT)));
	size = add_size(size, mul_size(NUM_AUXILIARY_PROCS, sizeof(PGXACT)));
	size = add_size(size, mul_size(max_prepared_xacts, sizeof(PGXACT)));

115 116 117
	return size;
}

118 119 120 121
/*
 * Report number of semaphores needed by InitProcGlobal.
 */
int
122
ProcGlobalSemas(void)
123
{
124 125 126 127
	/*
	 * We need a sema per backend (including autovacuum), plus one for each
	 * auxiliary process.
	 */
128
	return MaxBackends + NUM_AUXILIARY_PROCS;
129 130
}

131 132
/*
 * InitProcGlobal -
133 134
 *	  Initialize the global process table during postmaster or standalone
 *	  backend startup.
135
 *
136
 *	  We also create all the per-process semaphores we will need to support
137 138 139 140 141 142 143
 *	  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
144 145
 *	  MaxConnections, max_worker_processes, or autovacuum_max_workers higher
 *	  than his kernel will support, he'll find out sooner rather than later.
146 147 148 149
 *
 *	  Another reason for creating semaphores here is that the semaphore
 *	  implementation typically requires us to create semaphores in the
 *	  postmaster, not in backends.
150 151
 *
 * Note: this is NOT called by individual backends under a postmaster,
152
 * not even in the EXEC_BACKEND case.  The ProcGlobal and AuxiliaryProcs
153
 * pointers must be propagated specially for EXEC_BACKEND operation.
154 155
 */
void
156
InitProcGlobal(void)
157
{
158
	PGPROC	   *procs;
159
	PGXACT	   *pgxacts;
160 161
	int			i,
				j;
162
	bool		found;
163
	uint32		TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS + max_prepared_xacts;
164

165
	/* Create the ProcGlobal shared structure */
166
	ProcGlobal = (PROC_HDR *)
167 168
		ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
	Assert(!found);
169

170 171 172
	/*
	 * Initialize the data structures.
	 */
R
Robert Haas 已提交
173
	ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;
174 175
	ProcGlobal->freeProcs = NULL;
	ProcGlobal->autovacFreeProcs = NULL;
A
Alvaro Herrera 已提交
176
	ProcGlobal->bgworkerFreeProcs = NULL;
177 178 179
	ProcGlobal->startupProc = NULL;
	ProcGlobal->startupProcPid = 0;
	ProcGlobal->startupBufferPinWaitBufId = -1;
180 181
	ProcGlobal->walwriterLatch = NULL;
	ProcGlobal->checkpointerLatch = NULL;
182

183
	/*
184
	 * Create and initialize all the PGPROC structures we'll need.  There are
A
Alvaro Herrera 已提交
185 186 187
	 * five separate consumers: (1) normal backends, (2) autovacuum workers
	 * and the autovacuum launcher, (3) background workers, (4) auxiliary
	 * processes, and (5) prepared transactions.  Each PGPROC structure is
B
Bruce Momjian 已提交
188 189
	 * dedicated to exactly one of these purposes, and they do not move
	 * between groups.
190
	 */
R
Robert Haas 已提交
191
	procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC));
192
	ProcGlobal->allProcs = procs;
193 194
	/* XXX allProcCount isn't really all of them; it excludes prepared xacts */
	ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS;
195 196 197 198
	if (!procs)
		ereport(FATAL,
				(errcode(ERRCODE_OUT_OF_MEMORY),
				 errmsg("out of shared memory")));
R
Robert Haas 已提交
199
	MemSet(procs, 0, TotalProcs * sizeof(PGPROC));
200 201 202 203 204 205

	/*
	 * Also allocate a separate array of PGXACT structures.  This is separate
	 * from the main PGPROC array so that the most heavily accessed data is
	 * stored contiguously in memory in as few cache lines as possible. This
	 * provides significant performance benefits, especially on a
206
	 * multiprocessor system.  There is one PGXACT structure for every PGPROC
207 208 209 210 211 212
	 * structure.
	 */
	pgxacts = (PGXACT *) ShmemAlloc(TotalProcs * sizeof(PGXACT));
	MemSet(pgxacts, 0, TotalProcs * sizeof(PGXACT));
	ProcGlobal->allPgXact = pgxacts;

R
Robert Haas 已提交
213
	for (i = 0; i < TotalProcs; i++)
214
	{
R
Robert Haas 已提交
215
		/* Common initialization for all PGPROCs, regardless of type. */
216

217
		/*
218 219 220
		 * Set up per-PGPROC semaphore, latch, and backendLock. Prepared xact
		 * dummy PGPROCs don't need these though - they're never associated
		 * with a real process
221 222 223 224 225 226 227 228
		 */
		if (i < MaxBackends + NUM_AUXILIARY_PROCS)
		{
			PGSemaphoreCreate(&(procs[i].sem));
			InitSharedLatch(&(procs[i].procLatch));
			procs[i].backendLock = LWLockAssign();
		}
		procs[i].pgprocno = i;
R
Robert Haas 已提交
229 230

		/*
A
Alvaro Herrera 已提交
231 232 233 234 235 236
		 * Newly created PGPROCs for normal backends, autovacuum and bgworkers
		 * must be queued up on the appropriate free list.	Because there can
		 * only ever be a small, fixed number of auxiliary processes, no free
		 * list is used in that case; InitAuxiliaryProcess() instead uses a
		 * linear search.	PGPROCs for prepared transactions are added to a
		 * free list by TwoPhaseShmemInit().
R
Robert Haas 已提交
237 238 239 240 241 242 243
		 */
		if (i < MaxConnections)
		{
			/* PGPROC for normal backend, add to freeProcs list */
			procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs;
			ProcGlobal->freeProcs = &procs[i];
		}
A
Alvaro Herrera 已提交
244
		else if (i < MaxConnections + autovacuum_max_workers + 1)
R
Robert Haas 已提交
245 246 247 248 249
		{
			/* PGPROC for AV launcher/worker, add to autovacFreeProcs list */
			procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs;
			ProcGlobal->autovacFreeProcs = &procs[i];
		}
A
Alvaro Herrera 已提交
250 251 252 253 254 255
		else if (i < MaxBackends)
		{
			/* PGPROC for bgworker, add to bgworkerFreeProcs list */
			procs[i].links.next = (SHM_QUEUE *) ProcGlobal->bgworkerFreeProcs;
			ProcGlobal->bgworkerFreeProcs = &procs[i];
		}
256 257 258 259

		/* Initialize myProcLocks[] shared memory queues. */
		for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
			SHMQueueInit(&(procs[i].myProcLocks[j]));
260 261
	}

262
	/*
263 264
	 * Save pointers to the blocks of PGPROC structures reserved for auxiliary
	 * processes and prepared transactions.
265
	 */
R
Robert Haas 已提交
266
	AuxiliaryProcs = &procs[MaxBackends];
267
	PreparedXactProcs = &procs[MaxBackends + NUM_AUXILIARY_PROCS];
268 269 270 271

	/* Create ProcStructLock spinlock, too */
	ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));
	SpinLockInit(ProcStructLock);
272 273
}

274
/*
275
 * InitProcess -- initialize a per-process data structure for this backend
276 277
 */
void
278
InitProcess(void)
279
{
280 281
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;
282 283

	/*
284 285
	 * ProcGlobal should be set up already (if we are a backend, we inherit
	 * this by fork() or EXEC_BACKEND mechanism from the postmaster).
286
	 */
287
	if (procglobal == NULL)
288
		elog(PANIC, "proc header uninitialized");
289 290

	if (MyProc != NULL)
291
		elog(ERROR, "you already exist");
292

293
	/*
B
Bruce Momjian 已提交
294
	 * Initialize process-local latch support.	This could fail if the kernel
295 296 297 298 299
	 * is low on resources, and if so we want to exit cleanly before acquiring
	 * any shared-memory resources.
	 */
	InitializeLatchSupport();

300
	/*
B
Bruce Momjian 已提交
301 302
	 * Try to get a proc struct from the free list.  If this fails, we must be
	 * out of PGPROC structures (not to mention semaphores).
303
	 *
B
Bruce Momjian 已提交
304 305
	 * While we are holding the ProcStructLock, also copy the current shared
	 * estimate of spins_per_delay to local storage.
306
	 */
307
	SpinLockAcquire(ProcStructLock);
308

309 310
	set_spins_per_delay(procglobal->spins_per_delay);

311
	if (IsAnyAutoVacuumProcess())
312
		MyProc = procglobal->autovacFreeProcs;
A
Alvaro Herrera 已提交
313 314
	else if (IsBackgroundWorker)
		MyProc = procglobal->bgworkerFreeProcs;
315
	else
316
		MyProc = procglobal->freeProcs;
317

318
	if (MyProc != NULL)
319
	{
320
		if (IsAnyAutoVacuumProcess())
321
			procglobal->autovacFreeProcs = (PGPROC *) MyProc->links.next;
A
Alvaro Herrera 已提交
322 323
		else if (IsBackgroundWorker)
			procglobal->bgworkerFreeProcs = (PGPROC *) MyProc->links.next;
324
		else
325
			procglobal->freeProcs = (PGPROC *) MyProc->links.next;
326
		SpinLockRelease(ProcStructLock);
327 328 329 330
	}
	else
	{
		/*
B
Bruce Momjian 已提交
331 332
		 * 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
333 334
		 * error message.  XXX do we need to give a different failure message
		 * in the autovacuum case?
335
		 */
336
		SpinLockRelease(ProcStructLock);
337 338 339
		ereport(FATAL,
				(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
				 errmsg("sorry, too many clients already")));
340
	}
341
	MyPgXact = &ProcGlobal->allPgXact[MyProc->pgprocno];
342

343 344
	/*
	 * Now that we have a PGPROC, mark ourselves as an active postmaster
345
	 * child; this is so that the postmaster can detect it if we exit without
346 347
	 * cleaning up.  (XXX autovac launcher currently doesn't participate in
	 * this; it probably should.)
348
	 */
349
	if (IsUnderPostmaster && !IsAutoVacuumLauncherProcess())
350
		MarkPostmasterChildActive();
351

352
	/*
353 354
	 * Initialize all fields of MyProc, except for those previously
	 * initialized by InitProcGlobal.
355
	 */
356
	SHMQueueElemInit(&(MyProc->links));
357
	MyProc->waitStatus = STATUS_OK;
358
	MyProc->lxid = InvalidLocalTransactionId;
359 360
	MyProc->fpVXIDLock = false;
	MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
361 362
	MyPgXact->xid = InvalidTransactionId;
	MyPgXact->xmin = InvalidTransactionId;
363
	MyProc->pid = MyProcPid;
364 365
	/* backendId, databaseId and roleId will be filled in later */
	MyProc->backendId = InvalidBackendId;
366
	MyProc->databaseId = InvalidOid;
367
	MyProc->roleId = InvalidOid;
368
	MyPgXact->delayChkpt = false;
369
	MyPgXact->vacuumFlags = 0;
370
	/* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
371
	if (IsAutoVacuumWorkerProcess())
372
		MyPgXact->vacuumFlags |= PROC_IS_AUTOVACUUM;
373
	MyProc->lwWaiting = false;
374
	MyProc->lwWaitMode = 0;
375
	MyProc->lwWaitLink = NULL;
376
	MyProc->waitLock = NULL;
377
	MyProc->waitProcLock = NULL;
378 379 380
#ifdef USE_ASSERT_CHECKING
	if (assert_enabled)
	{
381
		int			i;
382 383 384 385 386 387

		/* Last process should have released all locks. */
		for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
			Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
	}
#endif
388
	MyProc->recoveryConflictPending = false;
389

390
	/* Initialize fields for sync rep */
391
	MyProc->waitLSN = 0;
392 393
	MyProc->syncRepState = SYNC_REP_NOT_WAITING;
	SHMQueueElemInit(&(MyProc->syncRepLinks));
394 395 396 397 398 399

	/*
	 * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
	 * Note that there's no particular need to do ResetLatch here.
	 */
	OwnLatch(&MyProc->procLatch);
400

401
	/*
402
	 * We might be reusing a semaphore that belonged to a failed process. So
B
Bruce Momjian 已提交
403
	 * be careful and reinitialize its value here.	(This is not strictly
404
	 * necessary anymore, but seems like a good idea for cleanliness.)
405
	 */
406
	PGSemaphoreReset(&MyProc->sem);
407

408
	/*
409
	 * Arrange to clean up at backend exit.
410
	 */
411
	on_shmem_exit(ProcKill, 0);
412 413

	/*
B
Bruce Momjian 已提交
414 415
	 * Now that we have a PGPROC, we could try to acquire locks, so initialize
	 * the deadlock checker.
416 417
	 */
	InitDeadLockChecking();
418 419
}

420 421 422 423
/*
 * InitProcessPhase2 -- make MyProc visible in the shared ProcArray.
 *
 * This is separate from InitProcess because we can't acquire LWLocks until
424 425
 * we've created a PGPROC, but in the EXEC_BACKEND case ProcArrayAdd won't
 * work until after we've done CreateSharedMemoryAndSemaphores.
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
 */
void
InitProcessPhase2(void)
{
	Assert(MyProc != NULL);

	/*
	 * Add our PGPROC to the PGPROC array in shared memory.
	 */
	ProcArrayAdd(MyProc);

	/*
	 * Arrange to clean that up at backend exit.
	 */
	on_shmem_exit(RemoveProcFromArray, 0);
}

443
/*
444
 * InitAuxiliaryProcess -- create a per-auxiliary-process data structure
445
 *
446 447
 * 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
448
 * and sema that are assigned are one of the extra ones created during
449
 * InitProcGlobal.
450
 *
451
 * Auxiliary processes are presently not expected to wait for real (lockmgr)
452
 * locks, so we need not set up the deadlock checker.  They are never added
B
Bruce Momjian 已提交
453
 * to the ProcArray or the sinval messaging mechanism, either.	They also
454 455
 * don't get a VXID assigned, since this is only useful when we actually
 * hold lockmgr locks.
456 457 458 459 460
 *
 * Startup process however uses locks but never waits for them in the
 * normal backend sense. Startup process also takes part in sinval messaging
 * as a sendOnly process, so never reads messages from sinval queue. So
 * Startup process does have a VXID and does show up in pg_locks.
461 462
 */
void
463
InitAuxiliaryProcess(void)
464
{
465
	PGPROC	   *auxproc;
466
	int			proctype;
J
Jan Wieck 已提交
467

468
	/*
469 470
	 * ProcGlobal should be set up already (if we are a backend, we inherit
	 * this by fork() or EXEC_BACKEND mechanism from the postmaster).
471
	 */
472
	if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
473
		elog(PANIC, "proc header uninitialized");
474 475

	if (MyProc != NULL)
476
		elog(ERROR, "you already exist");
477

478
	/*
B
Bruce Momjian 已提交
479
	 * Initialize process-local latch support.	This could fail if the kernel
480 481 482 483 484
	 * is low on resources, and if so we want to exit cleanly before acquiring
	 * any shared-memory resources.
	 */
	InitializeLatchSupport();

485
	/*
486
	 * We use the ProcStructLock to protect assignment and releasing of
487
	 * AuxiliaryProcs entries.
488
	 *
B
Bruce Momjian 已提交
489 490
	 * While we are holding the ProcStructLock, also copy the current shared
	 * estimate of spins_per_delay to local storage.
491 492 493 494 495
	 */
	SpinLockAcquire(ProcStructLock);

	set_spins_per_delay(ProcGlobal->spins_per_delay);

496
	/*
497
	 * Find a free auxproc ... *big* trouble if there isn't one ...
498
	 */
499
	for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)
500
	{
501 502
		auxproc = &AuxiliaryProcs[proctype];
		if (auxproc->pid == 0)
503 504
			break;
	}
505
	if (proctype >= NUM_AUXILIARY_PROCS)
506 507
	{
		SpinLockRelease(ProcStructLock);
508
		elog(FATAL, "all AuxiliaryProcs are in use");
509
	}
510

511
	/* Mark auxiliary proc as in use by me */
512
	/* use volatile pointer to prevent code rearrangement */
513
	((volatile PGPROC *) auxproc)->pid = MyProcPid;
514

515
	MyProc = auxproc;
516
	MyPgXact = &ProcGlobal->allPgXact[auxproc->pgprocno];
517 518 519

	SpinLockRelease(ProcStructLock);

520
	/*
521 522
	 * Initialize all fields of MyProc, except for those previously
	 * initialized by InitProcGlobal.
523 524
	 */
	SHMQueueElemInit(&(MyProc->links));
525
	MyProc->waitStatus = STATUS_OK;
526
	MyProc->lxid = InvalidLocalTransactionId;
527 528
	MyProc->fpVXIDLock = false;
	MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
529 530
	MyPgXact->xid = InvalidTransactionId;
	MyPgXact->xmin = InvalidTransactionId;
531
	MyProc->backendId = InvalidBackendId;
532
	MyProc->databaseId = InvalidOid;
533
	MyProc->roleId = InvalidOid;
534
	MyPgXact->delayChkpt = false;
535
	MyPgXact->vacuumFlags = 0;
536
	MyProc->lwWaiting = false;
537
	MyProc->lwWaitMode = 0;
538 539
	MyProc->lwWaitLink = NULL;
	MyProc->waitLock = NULL;
540
	MyProc->waitProcLock = NULL;
541 542 543
#ifdef USE_ASSERT_CHECKING
	if (assert_enabled)
	{
544
		int			i;
545 546 547 548 549 550

		/* Last process should have released all locks. */
		for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
			Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
	}
#endif
551

552 553 554 555 556 557
	/*
	 * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
	 * Note that there's no particular need to do ResetLatch here.
	 */
	OwnLatch(&MyProc->procLatch);

558
	/*
B
Bruce Momjian 已提交
559
	 * We might be reusing a semaphore that belonged to a failed process. So
B
Bruce Momjian 已提交
560
	 * be careful and reinitialize its value here.	(This is not strictly
561
	 * necessary anymore, but seems like a good idea for cleanliness.)
562
	 */
563
	PGSemaphoreReset(&MyProc->sem);
564 565 566 567

	/*
	 * Arrange to clean up at process exit.
	 */
568
	on_shmem_exit(AuxiliaryProcKill, Int32GetDatum(proctype));
569 570
}

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
/*
 * Record the PID and PGPROC structures for the Startup process, for use in
 * ProcSendSignal().  See comments there for further explanation.
 */
void
PublishStartupProcessInformation(void)
{
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;

	SpinLockAcquire(ProcStructLock);

	procglobal->startupProc = MyProc;
	procglobal->startupProcPid = MyProcPid;

	SpinLockRelease(ProcStructLock);
}

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
/*
 * Used from bufgr to share the value of the buffer that Startup waits on,
 * or to reset the value to "not waiting" (-1). This allows processing
 * of recovery conflicts for buffer pins. Set is made before backends look
 * at this value, so locking not required, especially since the set is
 * an atomic integer set operation.
 */
void
SetStartupBufferPinWaitBufId(int bufid)
{
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;

	procglobal->startupBufferPinWaitBufId = bufid;
}

/*
 * Used by backends when they receive a request to check for buffer pin waits.
 */
int
GetStartupBufferPinWaitBufId(void)
{
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;

614
	return procglobal->startupBufferPinWaitBufId;
615 616
}

617 618 619 620 621 622 623 624 625
/*
 * 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)
{
	PGPROC	   *proc;
B
Bruce Momjian 已提交
626

627 628 629 630 631
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;

	SpinLockAcquire(ProcStructLock);

632
	proc = procglobal->freeProcs;
633

634
	while (n > 0 && proc != NULL)
635
	{
636
		proc = (PGPROC *) proc->links.next;
637 638 639 640 641 642 643 644
		n--;
	}

	SpinLockRelease(ProcStructLock);

	return (n <= 0);
}

645 646 647
/*
 * Check if the current process is awaiting a lock.
 */
648 649 650 651 652 653 654 655 656
bool
IsWaitingForLock(void)
{
	if (lockAwaited == NULL)
		return false;

	return true;
}

657
/*
658 659
 * Cancel any pending wait for lock, when aborting a transaction, and revert
 * any strong lock count acquisition for a lock being acquired.
660 661
 *
 * (Normally, this would only happen if we accept a cancel/die
662 663
 * interrupt while waiting; but an ereport(ERROR) before or during the lock
 * wait is within the realm of possibility, too.)
664
 */
665
void
666
LockErrorCleanup(void)
667
{
668
	LWLock	   *partitionLock;
669
	DisableTimeoutParams timeouts[2];
670

671 672
	AbortStrongLockAcquire();

673
	/* Nothing to do if we weren't waiting for a lock */
674
	if (lockAwaited == NULL)
675
		return;
676

677 678 679 680 681 682 683 684 685 686 687 688 689
	/*
	 * Turn off the deadlock and lock timeout timers, if they are still
	 * running (see ProcSleep).  Note we must preserve the LOCK_TIMEOUT
	 * indicator flag, since this function is executed before
	 * ProcessInterrupts when responding to SIGINT; else we'd lose the
	 * knowledge that the SIGINT came from a lock timeout and not an external
	 * source.
	 */
	timeouts[0].id = DEADLOCK_TIMEOUT;
	timeouts[0].keep_indicator = false;
	timeouts[1].id = LOCK_TIMEOUT;
	timeouts[1].keep_indicator = true;
	disable_timeouts(timeouts, 2);
690 691

	/* Unlink myself from the wait queue, if on it (might not be anymore!) */
692
	partitionLock = LockHashPartitionLock(lockAwaited->hashcode);
693
	LWLockAcquire(partitionLock, LW_EXCLUSIVE);
694

695
	if (MyProc->links.next != NULL)
696 697
	{
		/* We could not have been granted the lock yet */
698
		RemoveFromWaitQueue(MyProc, lockAwaited->hashcode);
699 700 701 702 703
	}
	else
	{
		/*
		 * Somebody kicked us off the lock queue already.  Perhaps they
B
Bruce Momjian 已提交
704 705 706
		 * 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.
707
		 */
708 709
		if (MyProc->waitStatus == STATUS_OK)
			GrantAwaitedLock();
710 711
	}

712
	lockAwaited = NULL;
713

714
	LWLockRelease(partitionLock);
H
Hiroshi Inoue 已提交
715

716
	/*
717
	 * We used to do PGSemaphoreReset() here to ensure that our proc's wait
B
Bruce Momjian 已提交
718 719 720 721 722 723
	 * semaphore is reset to zero.	This prevented a leftover wakeup signal
	 * from remaining in the semaphore if someone else had granted us the lock
	 * we wanted before we were able to remove ourselves from the wait-list.
	 * However, now that ProcSleep loops until waitStatus changes, a leftover
	 * wakeup signal isn't harmful, and it seems not worth expending cycles to
	 * get rid of a signal that most likely isn't there.
724
	 */
H
Hiroshi Inoue 已提交
725
}
726

727

728
/*
729
 * ProcReleaseLocks() -- release locks associated with current transaction
730
 *			at main transaction commit or abort
731
 *
732
 * At main transaction commit, we release standard locks except session locks.
733
 * At main transaction abort, we release all locks including session locks.
734
 *
735 736 737
 * Advisory locks are released only if they are transaction-level;
 * session-level holds remain, whether this is a commit or not.
 *
738
 * At subtransaction commit, we don't release any locks (so this func is not
739
 * needed at all); we will defer the releasing to the parent transaction.
740
 * At subtransaction abort, we release all locks held by the subtransaction;
741 742
 * this is implemented by retail releasing of the locks under control of
 * the ResourceOwner mechanism.
743 744
 */
void
745
ProcReleaseLocks(bool isCommit)
746
{
747 748
	if (!MyProc)
		return;
749
	/* If waiting, get off wait queue (should only be needed after error) */
750
	LockErrorCleanup();
751
	/* Release standard locks, including session-level if aborting */
752
	LockReleaseAll(DEFAULT_LOCKMETHOD, !isCommit);
753
	/* Release transaction-level advisory locks */
754
	LockReleaseAll(USER_LOCKMETHOD, false);
755 756 757
}


758 759 760 761 762 763 764
/*
 * RemoveProcFromArray() -- Remove this process from the shared ProcArray.
 */
static void
RemoveProcFromArray(int code, Datum arg)
{
	Assert(MyProc != NULL);
765
	ProcArrayRemove(MyProc, InvalidTransactionId);
766 767
}

768 769
/*
 * ProcKill() -- Destroy the per-proc data structure for
770
 *		this process. Release any of its held LW locks.
771 772
 */
static void
773
ProcKill(int code, Datum arg)
774
{
775 776
	/* use volatile pointer to prevent code rearrangement */
	volatile PROC_HDR *procglobal = ProcGlobal;
777
	PGPROC	   *proc;
778

779
	Assert(MyProc != NULL);
780

781 782 783
	/* Make sure we're out of the sync rep lists */
	SyncRepCleanupAtProcExit();

R
Robert Haas 已提交
784 785 786 787
	/* Make sure active replication slots are released */
	if (MyReplicationSlot != NULL)
		ReplicationSlotRelease();

788 789 790
#ifdef USE_ASSERT_CHECKING
	if (assert_enabled)
	{
791
		int			i;
792 793 794 795 796 797 798

		/* Last process should have released all locks. */
		for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
			Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
	}
#endif

799
	/*
B
Bruce Momjian 已提交
800 801
	 * 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
802
	 * facility by releasing our PGPROC ...
803
	 */
804
	LWLockReleaseAll();
805

806 807 808 809 810 811 812 813
	/*
	 * Clear MyProc first; then disown the process latch.  This is so that
	 * signal handlers won't try to clear the process latch after it's no
	 * longer ours.
	 */
	proc = MyProc;
	MyProc = NULL;
	DisownLatch(&proc->procLatch);
814

815
	SpinLockAcquire(ProcStructLock);
816

817 818
	/* Return PGPROC structure (and semaphore) to appropriate freelist */
	if (IsAnyAutoVacuumProcess())
819
	{
820 821
		proc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
		procglobal->autovacFreeProcs = proc;
822
	}
A
Alvaro Herrera 已提交
823 824
	else if (IsBackgroundWorker)
	{
825 826
		proc->links.next = (SHM_QUEUE *) procglobal->bgworkerFreeProcs;
		procglobal->bgworkerFreeProcs = proc;
A
Alvaro Herrera 已提交
827
	}
828 829
	else
	{
830 831
		proc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
		procglobal->freeProcs = proc;
832
	}
833

834 835 836
	/* Update shared estimate of spins_per_delay */
	procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay);

837
	SpinLockRelease(ProcStructLock);
838

839 840
	/*
	 * This process is no longer present in shared memory in any meaningful
B
Bruce Momjian 已提交
841 842
	 * way, so tell the postmaster we've cleaned up acceptably well. (XXX
	 * autovac launcher should be included here someday)
843
	 */
844
	if (IsUnderPostmaster && !IsAutoVacuumLauncherProcess())
845 846
		MarkPostmasterChildInactive();

847 848
	/* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
	if (AutovacuumLauncherPid != 0)
849
		kill(AutovacuumLauncherPid, SIGUSR2);
850 851 852
}

/*
853 854 855
 * AuxiliaryProcKill() -- Cut-down version of ProcKill for auxiliary
 *		processes (bgwriter, etc).	The PGPROC and sema are not released, only
 *		marked as not-in-use.
856 857
 */
static void
858
AuxiliaryProcKill(int code, Datum arg)
859
{
B
Bruce Momjian 已提交
860
	int			proctype = DatumGetInt32(arg);
861
	PGPROC	   *auxproc PG_USED_FOR_ASSERTS_ONLY;
862
	PGPROC	   *proc;
J
Jan Wieck 已提交
863

864
	Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
J
Jan Wieck 已提交
865

866
	auxproc = &AuxiliaryProcs[proctype];
J
Jan Wieck 已提交
867

868
	Assert(MyProc == auxproc);
869

870
	/* Release any LW locks I am holding (see notes above) */
871 872
	LWLockReleaseAll();

873 874 875 876 877 878 879 880
	/*
	 * Clear MyProc first; then disown the process latch.  This is so that
	 * signal handlers won't try to clear the process latch after it's no
	 * longer ours.
	 */
	proc = MyProc;
	MyProc = NULL;
	DisownLatch(&proc->procLatch);
881

882 883
	SpinLockAcquire(ProcStructLock);

884
	/* Mark auxiliary proc no longer in use */
885
	proc->pid = 0;
886 887 888 889 890

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

	SpinLockRelease(ProcStructLock);
891 892
}

893

894 895
/*
 * ProcQueue package: routines for putting processes to sleep
896
 *		and  waking them up
897 898 899 900 901
 */

/*
 * ProcQueueAlloc -- alloc/attach to a shared memory process queue
 *
902 903
 * Returns: a pointer to the queue
 * Side Effects: Initializes the queue if it wasn't there before
904
 */
905
#ifdef NOT_USED
906
PROC_QUEUE *
907
ProcQueueAlloc(const char *name)
908
{
909
	PROC_QUEUE *queue;
910
	bool		found;
911

912 913 914
	queue = (PROC_QUEUE *)
		ShmemInitStruct(name, sizeof(PROC_QUEUE), &found);

915 916
	if (!found)
		ProcQueueInit(queue);
917

918
	return queue;
919
}
920
#endif
921 922 923 924 925

/*
 * ProcQueueInit -- initialize a shared memory process queue
 */
void
926
ProcQueueInit(PROC_QUEUE *queue)
927
{
928 929
	SHMQueueInit(&(queue->links));
	queue->size = 0;
930 931 932 933
}


/*
934
 * ProcSleep -- put a process to sleep on the specified lock
935
 *
936 937
 * Caller must have set MyProc->heldLocks to reflect locks already held
 * on the lockable object by this process (under all XIDs).
938
 *
939
 * The lock table's partition lock must be held at entry, and will be held
940
 * at exit.
941
 *
942
 * Result: STATUS_OK if we acquired the lock, STATUS_ERROR if not (deadlock).
943
 *
944
 * ASSUME: that no one will fiddle with the queue until after
945
 *		we release the partition lock.
946 947
 *
 * NOTES: The process queue is now a priority queue for locking.
948 949 950
 *
 * P() on the semaphore should put us to sleep.  The process
 * semaphore is normally zero, so when we try to acquire it, we sleep.
951 952
 */
int
953
ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
954
{
955 956 957
	LOCKMODE	lockmode = locallock->tag.mode;
	LOCK	   *lock = locallock->lock;
	PROCLOCK   *proclock = locallock->proclock;
958
	uint32		hashcode = locallock->hashcode;
959
	LWLock	   *partitionLock = LockHashPartitionLock(hashcode);
960
	PROC_QUEUE *waitQueue = &(lock->waitProcs);
961
	LOCKMASK	myHeldLocks = MyProc->heldLocks;
962
	bool		early_deadlock = false;
B
Bruce Momjian 已提交
963
	bool		allow_autovacuum_cancel = true;
964
	int			myWaitStatus;
J
Jan Wieck 已提交
965
	PGPROC	   *proc;
966
	int			i;
967

968
	/*
969 970
	 * Determine where to add myself in the wait queue.
	 *
971 972 973 974
	 * 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
B
Bruce Momjian 已提交
975 976
	 * waiter anyway; but it's relatively cheap to detect such a conflict
	 * immediately, and avoid delaying till deadlock timeout.
977
	 *
978 979
	 * 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
B
Bruce Momjian 已提交
980 981 982 983
	 * 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.
984 985
	 */
	if (myHeldLocks != 0)
V
Vadim B. Mikheev 已提交
986
	{
987
		LOCKMASK	aheadRequests = 0;
988

989
		proc = (PGPROC *) waitQueue->links.next;
990
		for (i = 0; i < waitQueue->size; i++)
V
Vadim B. Mikheev 已提交
991
		{
992
			/* Must he wait for me? */
B
Bruce Momjian 已提交
993
			if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
V
Vadim B. Mikheev 已提交
994
			{
995
				/* Must I wait for him ? */
B
Bruce Momjian 已提交
996
				if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
997
				{
998
					/*
B
Bruce Momjian 已提交
999 1000 1001 1002 1003
					 * 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.
1004
					 */
1005
					RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
1006 1007
					early_deadlock = true;
					break;
1008
				}
1009
				/* I must go before this waiter.  Check special case. */
B
Bruce Momjian 已提交
1010
				if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1011 1012 1013
					LockCheckConflicts(lockMethodTable,
									   lockmode,
									   lock,
1014
									   proclock) == STATUS_OK)
1015
				{
1016
					/* Skip the wait and just grant myself the lock. */
1017
					GrantLock(lock, proclock, lockmode);
1018
					GrantAwaitedLock();
1019
					return STATUS_OK;
1020 1021
				}
				/* Break out of loop to put myself before him */
V
Vadim B. Mikheev 已提交
1022
				break;
1023
			}
1024
			/* Nope, so advance to next waiter */
1025
			aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
1026
			proc = (PGPROC *) proc->links.next;
V
Vadim B. Mikheev 已提交
1027
		}
B
Bruce Momjian 已提交
1028

1029
		/*
B
Bruce Momjian 已提交
1030 1031
		 * If we fall out of loop normally, proc points to waitQueue head, so
		 * we will insert at tail of queue as desired.
1032
		 */
1033 1034 1035 1036
	}
	else
	{
		/* I hold no locks, so I can't push in front of anyone. */
J
Jan Wieck 已提交
1037
		proc = (PGPROC *) &(waitQueue->links);
V
Vadim B. Mikheev 已提交
1038
	}
1039

1040
	/*
B
Bruce Momjian 已提交
1041
	 * Insert self into queue, ahead of the given proc (or at tail of queue).
1042
	 */
1043
	SHMQueueInsertBefore(&(proc->links), &(MyProc->links));
B
Bruce Momjian 已提交
1044
	waitQueue->size++;
1045

1046
	lock->waitMask |= LOCKBIT_ON(lockmode);
1047

J
Jan Wieck 已提交
1048
	/* Set up wait information in PGPROC object, too */
1049
	MyProc->waitLock = lock;
1050
	MyProc->waitProcLock = proclock;
1051 1052
	MyProc->waitLockMode = lockmode;

1053
	MyProc->waitStatus = STATUS_WAITING;
1054 1055

	/*
B
Bruce Momjian 已提交
1056 1057 1058
	 * 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.
1059 1060 1061
	 */
	if (early_deadlock)
	{
1062
		RemoveFromWaitQueue(MyProc, hashcode);
1063 1064
		return STATUS_ERROR;
	}
1065

1066
	/* mark that we are waiting for a lock */
1067
	lockAwaited = locallock;
1068

1069
	/*
1070
	 * Release the lock table's partition lock.
1071
	 *
1072
	 * NOTE: this may also cause us to exit critical-section state, possibly
B
Bruce Momjian 已提交
1073 1074
	 * 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
1075
	 * LockErrorCleanup will clean up if cancel/die happens.
1076
	 */
1077
	LWLockRelease(partitionLock);
1078

1079 1080 1081
	/*
	 * Also, now that we will successfully clean up after an ereport, it's
	 * safe to check to see if there's a buffer pin deadlock against the
1082 1083
	 * Startup process.  Of course, that's only necessary if we're doing Hot
	 * Standby and are not the Startup process ourselves.
1084 1085 1086 1087
	 */
	if (RecoveryInProgress() && !InRecovery)
		CheckRecoveryConflictDeadlock();

1088
	/* Reset deadlock_state before enabling the timeout handler */
1089 1090
	deadlock_state = DS_NOT_YET_CHECKED;

1091
	/*
B
Bruce Momjian 已提交
1092 1093 1094 1095
	 * 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.
1096
	 *
1097 1098
	 * By delaying the check until we've waited for a bit, we can avoid
	 * running the rather expensive deadlock-check code in most cases.
1099 1100 1101
	 *
	 * If LockTimeout is set, also enable the timeout for that.  We can save a
	 * few cycles by enabling both timeout sources in one call.
1102
	 */
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	if (LockTimeout > 0)
	{
		EnableTimeoutParams timeouts[2];

		timeouts[0].id = DEADLOCK_TIMEOUT;
		timeouts[0].type = TMPARAM_AFTER;
		timeouts[0].delay_ms = DeadlockTimeout;
		timeouts[1].id = LOCK_TIMEOUT;
		timeouts[1].type = TMPARAM_AFTER;
		timeouts[1].delay_ms = LockTimeout;
		enable_timeouts(timeouts, 2);
	}
	else
		enable_timeout_after(DEADLOCK_TIMEOUT, DeadlockTimeout);
1117

1118
	/*
1119
	 * If someone wakes us between LWLockRelease and PGSemaphoreLock,
B
Bruce Momjian 已提交
1120
	 * PGSemaphoreLock will not block.	The wakeup is "saved" by the semaphore
B
Bruce Momjian 已提交
1121 1122 1123 1124 1125
	 * implementation.	While this is normally good, there are cases where a
	 * saved wakeup might be leftover from a previous operation (for example,
	 * we aborted ProcWaitForSignal just before someone did ProcSendSignal).
	 * So, loop to wait again if the waitStatus shows we haven't been granted
	 * nor denied the lock yet.
1126
	 *
1127 1128 1129 1130 1131
	 * 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
1132 1133
	 * canceling the deadlock timeout and updating the locallock table, but if
	 * we lose control to an error, LockErrorCleanup will fix that up.
1134
	 */
B
Bruce Momjian 已提交
1135 1136
	do
	{
1137
		PGSemaphoreLock(&MyProc->sem, true);
1138

1139 1140
		/*
		 * waitStatus could change from STATUS_WAITING to something else
B
Bruce Momjian 已提交
1141
		 * asynchronously.	Read it just once per loop to prevent surprising
1142 1143 1144 1145
		 * behavior (such as missing log messages).
		 */
		myWaitStatus = MyProc->waitStatus;

1146 1147
		/*
		 * If we are not deadlocked, but are waiting on an autovacuum-induced
B
Bruce Momjian 已提交
1148
		 * task, send a signal to interrupt it.
1149 1150 1151
		 */
		if (deadlock_state == DS_BLOCKED_BY_AUTOVACUUM && allow_autovacuum_cancel)
		{
B
Bruce Momjian 已提交
1152
			PGPROC	   *autovac = GetBlockingAutoVacuumPgproc();
1153
			PGXACT	   *autovac_pgxact = &ProcGlobal->allPgXact[autovac->pgprocno];
1154 1155 1156 1157 1158 1159 1160

			LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);

			/*
			 * Only do it if the worker is not working to protect against Xid
			 * wraparound.
			 */
S
Stephen Frost 已提交
1161
			if ((autovac_pgxact->vacuumFlags & PROC_IS_AUTOVACUUM) &&
1162
				!(autovac_pgxact->vacuumFlags & PROC_VACUUM_FOR_WRAPAROUND))
1163
			{
B
Bruce Momjian 已提交
1164
				int			pid = autovac->pid;
1165
				StringInfoData locktagbuf;
B
Bruce Momjian 已提交
1166
				StringInfoData logbuf;	/* errdetail for server log */
1167 1168 1169 1170 1171

				initStringInfo(&locktagbuf);
				initStringInfo(&logbuf);
				DescribeLockTag(&locktagbuf, &lock->tag);
				appendStringInfo(&logbuf,
B
Bruce Momjian 已提交
1172 1173 1174 1175 1176
								 _("Process %d waits for %s on %s."),
								 MyProcPid,
							  GetLockmodeName(lock->tag.locktag_lockmethodid,
											  lockmode),
								 locktagbuf.data);
1177 1178 1179

				/* release lock as quickly as possible */
				LWLockRelease(ProcArrayLock);
1180

1181
				ereport(LOG,
B
Bruce Momjian 已提交
1182 1183 1184
					  (errmsg("sending cancel to blocking autovacuum PID %d",
							  pid),
					   errdetail_log("%s", logbuf.data)));
1185

1186 1187
				pfree(logbuf.data);
				pfree(locktagbuf.data);
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

				/* send the autovacuum worker Back to Old Kent Road */
				if (kill(pid, SIGINT) < 0)
				{
					/* Just a warning to allow multiple callers */
					ereport(WARNING,
							(errmsg("could not send signal to process %d: %m",
									pid)));
				}
			}
			else
				LWLockRelease(ProcArrayLock);

			/* prevent signal from being resent more than once */
			allow_autovacuum_cancel = false;
		}

1205 1206 1207 1208
		/*
		 * If awoken after the deadlock check interrupt has run, and
		 * log_lock_waits is on, then report about the wait.
		 */
1209
		if (log_lock_waits && deadlock_state != DS_NOT_YET_CHECKED)
1210
		{
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
			StringInfoData buf;
			const char *modename;
			long		secs;
			int			usecs;
			long		msecs;

			initStringInfo(&buf);
			DescribeLockTag(&buf, &locallock->tag.lock);
			modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
									   lockmode);
1221 1222
			TimestampDifference(get_timeout_start_time(DEADLOCK_TIMEOUT),
								GetCurrentTimestamp(),
1223 1224 1225 1226 1227 1228 1229
								&secs, &usecs);
			msecs = secs * 1000 + usecs / 1000;
			usecs = usecs % 1000;

			if (deadlock_state == DS_SOFT_DEADLOCK)
				ereport(LOG,
						(errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
B
Bruce Momjian 已提交
1230
							  MyProcPid, modename, buf.data, msecs, usecs)));
1231
			else if (deadlock_state == DS_HARD_DEADLOCK)
1232
			{
1233
				/*
B
Bruce Momjian 已提交
1234 1235 1236 1237
				 * This message is a bit redundant with the error that will be
				 * reported subsequently, but in some cases the error report
				 * might not make it to the log (eg, if it's caught by an
				 * exception handler), and we want to ensure all long-wait
1238 1239 1240 1241
				 * events get logged.
				 */
				ereport(LOG,
						(errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
B
Bruce Momjian 已提交
1242
							  MyProcPid, modename, buf.data, msecs, usecs)));
1243
			}
1244 1245 1246 1247

			if (myWaitStatus == STATUS_WAITING)
				ereport(LOG,
						(errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
B
Bruce Momjian 已提交
1248
							  MyProcPid, modename, buf.data, msecs, usecs)));
1249 1250
			else if (myWaitStatus == STATUS_OK)
				ereport(LOG,
B
Bruce Momjian 已提交
1251 1252
					(errmsg("process %d acquired %s on %s after %ld.%03d ms",
							MyProcPid, modename, buf.data, msecs, usecs)));
1253 1254 1255
			else
			{
				Assert(myWaitStatus == STATUS_ERROR);
B
Bruce Momjian 已提交
1256

1257 1258
				/*
				 * Currently, the deadlock checker always kicks its own
B
Bruce Momjian 已提交
1259 1260 1261 1262 1263
				 * process, which means that we'll only see STATUS_ERROR when
				 * deadlock_state == DS_HARD_DEADLOCK, and there's no need to
				 * print redundant messages.  But for completeness and
				 * future-proofing, print a message if it looks like someone
				 * else kicked us off the lock.
1264 1265 1266 1267
				 */
				if (deadlock_state != DS_HARD_DEADLOCK)
					ereport(LOG,
							(errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
B
Bruce Momjian 已提交
1268
							  MyProcPid, modename, buf.data, msecs, usecs)));
1269 1270 1271
			}

			/*
B
Bruce Momjian 已提交
1272 1273
			 * At this point we might still need to wait for the lock. Reset
			 * state so we don't print the above messages again.
1274 1275 1276 1277
			 */
			deadlock_state = DS_NO_DEADLOCK;

			pfree(buf.data);
1278
		}
1279
	} while (myWaitStatus == STATUS_WAITING);
1280

1281
	/*
1282 1283 1284 1285
	 * Disable the timers, if they are still running.  As in LockErrorCleanup,
	 * we must preserve the LOCK_TIMEOUT indicator flag: if a lock timeout has
	 * already caused QueryCancelPending to become set, we want the cancel to
	 * be reported as a lock timeout, not a user cancel.
B
Bruce Momjian 已提交
1286
	 */
1287 1288 1289 1290 1291 1292 1293
	if (LockTimeout > 0)
	{
		DisableTimeoutParams timeouts[2];

		timeouts[0].id = DEADLOCK_TIMEOUT;
		timeouts[0].keep_indicator = false;
		timeouts[1].id = LOCK_TIMEOUT;
1294
		timeouts[1].keep_indicator = true;
1295 1296 1297 1298
		disable_timeouts(timeouts, 2);
	}
	else
		disable_timeout(DEADLOCK_TIMEOUT, false);
B
Bruce Momjian 已提交
1299

1300
	/*
B
Bruce Momjian 已提交
1301 1302 1303
	 * Re-acquire the lock table's partition lock.  We have to do this to hold
	 * off cancel/die interrupts before we can mess with lockAwaited (else we
	 * might have a missed or duplicated locallock update).
1304
	 */
1305
	LWLockAcquire(partitionLock, LW_EXCLUSIVE);
1306 1307

	/*
1308
	 * We no longer want LockErrorCleanup to do anything.
1309
	 */
1310
	lockAwaited = NULL;
1311

1312
	/*
1313
	 * If we got the lock, be sure to remember it in the locallock table.
1314
	 */
1315
	if (MyProc->waitStatus == STATUS_OK)
1316
		GrantAwaitedLock();
1317

1318 1319 1320 1321
	/*
	 * We don't have to do anything else, because the awaker did all the
	 * necessary update of the lock table and MyProc.
	 */
1322
	return MyProc->waitStatus;
1323 1324 1325 1326 1327 1328
}


/*
 * ProcWakeup -- wake up a process by releasing its private semaphore.
 *
1329
 *	 Also remove the process from the wait queue and set its links invalid.
1330
 *	 RETURN: the next process in the wait queue.
1331
 *
1332 1333
 * The appropriate lock partition lock must be held by caller.
 *
1334 1335 1336
 * 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.
1337
 * Hence, in practice the waitStatus parameter must be STATUS_OK.
1338
 */
J
Jan Wieck 已提交
1339
PGPROC *
1340
ProcWakeup(PGPROC *proc, int waitStatus)
1341
{
J
Jan Wieck 已提交
1342
	PGPROC	   *retProc;
1343

1344
	/* Proc should be sleeping ... */
1345 1346
	if (proc->links.prev == NULL ||
		proc->links.next == NULL)
1347
		return NULL;
1348
	Assert(proc->waitStatus == STATUS_WAITING);
1349

1350
	/* Save next process before we zap the list link */
1351
	retProc = (PGPROC *) proc->links.next;
1352

1353
	/* Remove process from wait queue */
1354
	SHMQueueDelete(&(proc->links));
1355
	(proc->waitLock->waitProcs.size)--;
1356

1357 1358
	/* Clean up process' state and pass it the ok/fail signal */
	proc->waitLock = NULL;
1359
	proc->waitProcLock = NULL;
1360
	proc->waitStatus = waitStatus;
1361

1362
	/* And awaken it */
1363
	PGSemaphoreUnlock(&proc->sem);
1364 1365

	return retProc;
1366 1367 1368 1369
}

/*
 * ProcLockWakeup -- routine for waking up processes when a lock is
1370 1371
 *		released (or a prior waiter is aborted).  Scan all waiters
 *		for lock, waken any that are no longer blocked.
1372 1373
 *
 * The appropriate lock partition lock must be held by caller.
1374
 */
1375
void
1376
ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
1377
{
1378 1379
	PROC_QUEUE *waitQueue = &(lock->waitProcs);
	int			queue_size = waitQueue->size;
J
Jan Wieck 已提交
1380
	PGPROC	   *proc;
1381
	LOCKMASK	aheadRequests = 0;
M
 
Marc G. Fournier 已提交
1382

1383
	Assert(queue_size >= 0);
1384

1385 1386
	if (queue_size == 0)
		return;
1387

1388
	proc = (PGPROC *) waitQueue->links.next;
1389

1390 1391
	while (queue_size-- > 0)
	{
B
Bruce Momjian 已提交
1392
		LOCKMODE	lockmode = proc->waitLockMode;
M
 
Marc G. Fournier 已提交
1393 1394

		/*
B
Bruce Momjian 已提交
1395 1396
		 * Waken if (a) doesn't conflict with requests of earlier waiters, and
		 * (b) doesn't conflict with already-held locks.
M
 
Marc G. Fournier 已提交
1397
		 */
B
Bruce Momjian 已提交
1398
		if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1399 1400 1401
			LockCheckConflicts(lockMethodTable,
							   lockmode,
							   lock,
1402
							   proc->waitProcLock) == STATUS_OK)
M
 
Marc G. Fournier 已提交
1403
		{
1404
			/* OK to waken */
1405
			GrantLock(lock, proc->waitProcLock, lockmode);
1406
			proc = ProcWakeup(proc, STATUS_OK);
B
Bruce Momjian 已提交
1407

1408
			/*
B
Bruce Momjian 已提交
1409 1410 1411
			 * 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.
1412
			 */
M
 
Marc G. Fournier 已提交
1413
		}
1414
		else
1415
		{
B
Bruce Momjian 已提交
1416
			/*
B
Bruce Momjian 已提交
1417
			 * Cannot wake this guy. Remember his request for later checks.
B
Bruce Momjian 已提交
1418
			 */
1419
			aheadRequests |= LOCKBIT_ON(lockmode);
1420
			proc = (PGPROC *) proc->links.next;
1421
		}
M
 
Marc G. Fournier 已提交
1422
	}
1423 1424

	Assert(waitQueue->size >= 0);
1425 1426
}

1427 1428 1429
/*
 * CheckDeadLock
 *
1430
 * We only get to this routine if the DEADLOCK_TIMEOUT fired
1431 1432
 * 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.
1433
 * (But signal ProcSleep to log a message, if log_lock_waits is true.)
1434 1435
 * If we have a real deadlock, remove ourselves from the lock's wait queue
 * and signal an error to ProcSleep.
1436 1437 1438
 *
 * NB: this is run inside a signal handler, so be very wary about what is done
 * here or in called routines.
1439
 */
1440
void
1441
CheckDeadLock(void)
1442
{
1443 1444
	int			i;

1445
	/*
B
Bruce Momjian 已提交
1446 1447
	 * Acquire exclusive lock on the entire shared lock data structures. Must
	 * grab LWLocks in partition-number order to avoid LWLock deadlock.
1448 1449 1450 1451 1452 1453
	 *
	 * Note that the deadlock check interrupt had better not be enabled
	 * anywhere that this process itself holds lock partition locks, else this
	 * will wait forever.  Also note that LWLockAcquire creates a critical
	 * section, so that this routine cannot be interrupted by cancel/die
	 * interrupts.
1454
	 */
1455
	for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1456
		LWLockAcquire(LockHashPartitionLockByIndex(i), LW_EXCLUSIVE);
1457

1458
	/*
1459 1460
	 * Check to see if we've been awoken by anyone in the interim.
	 *
1461
	 * If we have, we can return and resume our transaction -- happy day.
1462 1463
	 * Before we are awoken the process releasing the lock grants it to us so
	 * we know that we don't have to wait anymore.
1464
	 *
1465
	 * We check by looking to see if we've been unlinked from the wait queue.
B
Bruce Momjian 已提交
1466
	 * This is quicker than checking our semaphore's state, since no kernel
1467
	 * call is needed, and it is safe because we hold the lock partition lock.
1468
	 */
1469 1470
	if (MyProc->links.prev == NULL ||
		MyProc->links.next == NULL)
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
		goto check_done;

#ifdef LOCK_DEBUG
	if (Debug_deadlocks)
		DumpAllLocks();
#endif

	/* Run the deadlock check, and set deadlock_state for use by ProcSleep */
	deadlock_state = DeadLockCheck(MyProc);

1481
	if (deadlock_state == DS_HARD_DEADLOCK)
B
Bruce Momjian 已提交
1482
	{
1483 1484 1485
		/*
		 * Oops.  We have a deadlock.
		 *
1486 1487 1488 1489
		 * Get this process out of wait state. (Note: we could do this more
		 * efficiently by relying on lockAwaited, but use this coding to
		 * preserve the flexibility to kill some other transaction than the
		 * one detecting the deadlock.)
1490 1491
		 *
		 * RemoveFromWaitQueue sets MyProc->waitStatus to STATUS_ERROR, so
1492 1493
		 * ProcSleep will report an error after we return from the signal
		 * handler.
1494 1495 1496
		 */
		Assert(MyProc->waitLock != NULL);
		RemoveFromWaitQueue(MyProc, LockTagHashCode(&(MyProc->waitLock->tag)));
1497

1498 1499 1500 1501 1502
		/*
		 * Unlock my semaphore so that the interrupted ProcSleep() call can
		 * finish.
		 */
		PGSemaphoreUnlock(&MyProc->sem);
1503

1504
		/*
1505 1506 1507 1508 1509 1510 1511 1512
		 * 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.
1513 1514
		 */
	}
1515
	else if (log_lock_waits || deadlock_state == DS_BLOCKED_BY_AUTOVACUUM)
1516 1517 1518 1519
	{
		/*
		 * Unlock my semaphore so that the interrupted ProcSleep() call can
		 * print the log message (we daren't do it here because we are inside
B
Bruce Momjian 已提交
1520 1521
		 * a signal handler).  It will then sleep again until someone releases
		 * the lock.
1522 1523
		 *
		 * If blocked by autovacuum, this wakeup will enable ProcSleep to send
1524
		 * the canceling signal to the autovacuum worker.
1525 1526 1527
		 */
		PGSemaphoreUnlock(&MyProc->sem);
	}
1528 1529

	/*
B
Bruce Momjian 已提交
1530 1531 1532 1533 1534
	 * And release locks.  We do this in reverse order for two reasons: (1)
	 * Anyone else who needs more than one of the locks will be trying to lock
	 * them in increasing order; we don't want to release the other process
	 * until it can get all the locks it needs. (2) This avoids O(N^2)
	 * behavior inside LWLockRelease.
1535
	 */
1536
check_done:
B
Bruce Momjian 已提交
1537
	for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
1538
		LWLockRelease(LockHashPartitionLockByIndex(i));
1539 1540 1541
}


1542 1543 1544 1545 1546 1547
/*
 * 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
B
Bruce Momjian 已提交
1548
 * before we actually reach the waiting state.	Also as with locks,
1549 1550
 * it's necessary that the caller be robust against bogus wakeups:
 * always check that the desired state has occurred, and wait again
B
Bruce Momjian 已提交
1551
 * if not.	This copes with possible "leftover" wakeups.
1552 1553 1554 1555
 */
void
ProcWaitForSignal(void)
{
1556
	PGSemaphoreLock(&MyProc->sem, true);
1557 1558 1559
}

/*
1560
 * ProcSendSignal - send a signal to a backend identified by PID
1561 1562
 */
void
1563
ProcSendSignal(int pid)
1564
{
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
	PGPROC	   *proc = NULL;

	if (RecoveryInProgress())
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile PROC_HDR *procglobal = ProcGlobal;

		SpinLockAcquire(ProcStructLock);

		/*
		 * Check to see whether it is the Startup process we wish to signal.
B
Bruce Momjian 已提交
1576 1577
		 * This call is made by the buffer manager when it wishes to wake up a
		 * process that has been waiting for a pin in so it can obtain a
1578
		 * cleanup lock using LockBufferForCleanup(). Startup is not a normal
B
Bruce Momjian 已提交
1579 1580
		 * backend, so BackendPidGetProc() will not return any pid at all. So
		 * we remember the information for this special case.
1581 1582 1583 1584 1585 1586 1587 1588 1589
		 */
		if (pid == procglobal->startupProcPid)
			proc = procglobal->startupProc;

		SpinLockRelease(ProcStructLock);
	}

	if (proc == NULL)
		proc = BackendPidGetProc(pid);
1590 1591

	if (proc != NULL)
1592
		PGSemaphoreUnlock(&proc->sem);
1593
}