procarray.c 39.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*-------------------------------------------------------------------------
 *
 * procarray.c
 *	  POSTGRES process array code.
 *
 *
 * This module maintains an unsorted array of the PGPROC structures for all
 * active backends.  Although there are several uses for this, the principal
 * one is as a means of determining the set of currently running transactions.
 *
 * Because of various subtle race conditions it is critical that a backend
 * hold the correct locks while setting or clearing its MyProc->xid field.
13
 * See notes in src/backend/access/transam/README.
14 15 16
 *
 * The process array now also includes PGPROC structures representing
 * prepared transactions.  The xid and subxids fields of these are valid,
17 18
 * as are the myProcLocks lists.  They can be distinguished from regular
 * backend PGPROCs at need by checking for pid == 0.
B
Bruce Momjian 已提交
19
 *
20
 *
B
Bruce Momjian 已提交
21
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
22 23 24 25
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
26
 *	  $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.49 2009/04/04 17:40:36 tgl Exp $
27 28 29 30 31
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

32 33
#include <signal.h>

34
#include "access/subtrans.h"
35 36
#include "access/transam.h"
#include "access/xact.h"
37
#include "access/twophase.h"
38 39
#include "miscadmin.h"
#include "storage/procarray.h"
40
#include "utils/snapmgr.h"
41 42 43 44 45 46 47 48 49


/* Our shared memory area */
typedef struct ProcArrayStruct
{
	int			numProcs;		/* number of valid procs entries */
	int			maxProcs;		/* allocated size of procs array */

	/*
B
Bruce Momjian 已提交
50 51
	 * We declare procs[] as 1 entry because C wants a fixed-size array, but
	 * actually it is maxProcs entries long.
52 53 54 55 56 57 58 59 60 61 62
	 */
	PGPROC	   *procs[1];		/* VARIABLE LENGTH ARRAY */
} ProcArrayStruct;

static ProcArrayStruct *procArray;


#ifdef XIDCACHE_DEBUG

/* counters for XidCache measurement */
static long xc_by_recent_xmin = 0;
63
static long xc_by_known_xact = 0;
64
static long xc_by_my_xact = 0;
65
static long xc_by_latest_xid = 0;
66 67
static long xc_by_main_xid = 0;
static long xc_by_child_xid = 0;
68
static long xc_no_overflow = 0;
69 70 71
static long xc_slow_answer = 0;

#define xc_by_recent_xmin_inc()		(xc_by_recent_xmin++)
72
#define xc_by_known_xact_inc()		(xc_by_known_xact++)
73
#define xc_by_my_xact_inc()			(xc_by_my_xact++)
74
#define xc_by_latest_xid_inc()		(xc_by_latest_xid++)
75 76
#define xc_by_main_xid_inc()		(xc_by_main_xid++)
#define xc_by_child_xid_inc()		(xc_by_child_xid++)
77
#define xc_no_overflow_inc()		(xc_no_overflow++)
78 79 80 81 82 83
#define xc_slow_answer_inc()		(xc_slow_answer++)

static void DisplayXidCache(void);
#else							/* !XIDCACHE_DEBUG */

#define xc_by_recent_xmin_inc()		((void) 0)
84
#define xc_by_known_xact_inc()		((void) 0)
85
#define xc_by_my_xact_inc()			((void) 0)
86
#define xc_by_latest_xid_inc()		((void) 0)
87 88
#define xc_by_main_xid_inc()		((void) 0)
#define xc_by_child_xid_inc()		((void) 0)
89
#define xc_no_overflow_inc()		((void) 0)
90 91 92 93 94 95 96
#define xc_slow_answer_inc()		((void) 0)
#endif   /* XIDCACHE_DEBUG */


/*
 * Report shared-memory space needed by CreateSharedProcArray.
 */
97
Size
98
ProcArrayShmemSize(void)
99
{
100 101 102 103
	Size		size;

	size = offsetof(ProcArrayStruct, procs);
	size = add_size(size, mul_size(sizeof(PGPROC *),
B
Bruce Momjian 已提交
104
								 add_size(MaxBackends, max_prepared_xacts)));
105 106

	return size;
107 108 109 110 111 112
}

/*
 * Initialize the shared PGPROC array during postmaster startup.
 */
void
113
CreateSharedProcArray(void)
114 115 116 117 118
{
	bool		found;

	/* Create or attach to the ProcArray shared structure */
	procArray = (ProcArrayStruct *)
119
		ShmemInitStruct("Proc Array", ProcArrayShmemSize(), &found);
120 121 122 123 124 125 126

	if (!found)
	{
		/*
		 * We're the first - initialize.
		 */
		procArray->numProcs = 0;
127
		procArray->maxProcs = MaxBackends + max_prepared_xacts;
128 129 130 131
	}
}

/*
132
 * Add the specified PGPROC to the shared array.
133 134
 */
void
135
ProcArrayAdd(PGPROC *proc)
136 137 138 139 140 141 142 143
{
	ProcArrayStruct *arrayP = procArray;

	LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);

	if (arrayP->numProcs >= arrayP->maxProcs)
	{
		/*
B
Bruce Momjian 已提交
144 145 146
		 * Ooops, no room.	(This really shouldn't happen, since there is a
		 * fixed supply of PGPROC structs too, and so we should have failed
		 * earlier.)
147 148 149 150 151 152 153
		 */
		LWLockRelease(ProcArrayLock);
		ereport(FATAL,
				(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
				 errmsg("sorry, too many clients already")));
	}

154
	arrayP->procs[arrayP->numProcs] = proc;
155 156 157 158 159 160
	arrayP->numProcs++;

	LWLockRelease(ProcArrayLock);
}

/*
161
 * Remove the specified PGPROC from the shared array.
162 163 164 165 166 167 168
 *
 * When latestXid is a valid XID, we are removing a live 2PC gxact from the
 * array, and thus causing it to appear as "not running" anymore.  In this
 * case we must advance latestCompletedXid.  (This is essentially the same
 * as ProcArrayEndTransaction followed by removal of the PGPROC, but we take
 * the ProcArrayLock only once, and don't damage the content of the PGPROC;
 * twophase.c depends on the latter.)
169 170
 */
void
171
ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
172 173 174 175 176
{
	ProcArrayStruct *arrayP = procArray;
	int			index;

#ifdef XIDCACHE_DEBUG
177 178 179
	/* dump stats at backend shutdown, but not prepared-xact end */
	if (proc->pid != 0)
		DisplayXidCache();
180 181 182 183
#endif

	LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	if (TransactionIdIsValid(latestXid))
	{
		Assert(TransactionIdIsValid(proc->xid));

		/* Advance global latestCompletedXid while holding the lock */
		if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
								  latestXid))
			ShmemVariableCache->latestCompletedXid = latestXid;
	}
	else
	{
		/* Shouldn't be trying to remove a live transaction here */
		Assert(!TransactionIdIsValid(proc->xid));
	}

199 200
	for (index = 0; index < arrayP->numProcs; index++)
	{
201
		if (arrayP->procs[index] == proc)
202 203
		{
			arrayP->procs[index] = arrayP->procs[arrayP->numProcs - 1];
204
			arrayP->procs[arrayP->numProcs - 1] = NULL; /* for debugging */
205 206 207 208 209 210 211 212 213
			arrayP->numProcs--;
			LWLockRelease(ProcArrayLock);
			return;
		}
	}

	/* Ooops */
	LWLockRelease(ProcArrayLock);

214
	elog(LOG, "failed to find proc %p in ProcArray", proc);
215 216 217
}


218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
/*
 * ProcArrayEndTransaction -- mark a transaction as no longer running
 *
 * This is used interchangeably for commit and abort cases.  The transaction
 * commit/abort must already be reported to WAL and pg_clog.
 *
 * proc is currently always MyProc, but we pass it explicitly for flexibility.
 * latestXid is the latest Xid among the transaction's main XID and
 * subtransactions, or InvalidTransactionId if it has no XID.  (We must ask
 * the caller to pass latestXid, instead of computing it from the PGPROC's
 * contents, because the subxid information in the PGPROC might be
 * incomplete.)
 */
void
ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
{
	if (TransactionIdIsValid(latestXid))
	{
		/*
B
Bruce Momjian 已提交
237 238 239
		 * We must lock ProcArrayLock while clearing proc->xid, so that we do
		 * not exit the set of "running" transactions while someone else is
		 * taking a snapshot.  See discussion in
240 241 242 243 244 245 246 247 248
		 * src/backend/access/transam/README.
		 */
		Assert(TransactionIdIsValid(proc->xid));

		LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);

		proc->xid = InvalidTransactionId;
		proc->lxid = InvalidLocalTransactionId;
		proc->xmin = InvalidTransactionId;
249 250
		/* must be cleared with xid/xmin: */
		proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
B
Bruce Momjian 已提交
251
		proc->inCommit = false; /* be sure this is cleared in abort */
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

		/* Clear the subtransaction-XID cache too while holding the lock */
		proc->subxids.nxids = 0;
		proc->subxids.overflowed = false;

		/* Also advance global latestCompletedXid while holding the lock */
		if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
								  latestXid))
			ShmemVariableCache->latestCompletedXid = latestXid;

		LWLockRelease(ProcArrayLock);
	}
	else
	{
		/*
B
Bruce Momjian 已提交
267 268 269
		 * If we have no XID, we don't need to lock, since we won't affect
		 * anyone else's calculation of a snapshot.  We might change their
		 * estimate of global xmin, but that's OK.
270 271 272 273 274
		 */
		Assert(!TransactionIdIsValid(proc->xid));

		proc->lxid = InvalidLocalTransactionId;
		proc->xmin = InvalidTransactionId;
275 276
		/* must be cleared with xid/xmin: */
		proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
B
Bruce Momjian 已提交
277
		proc->inCommit = false; /* be sure this is cleared in abort */
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

		Assert(proc->subxids.nxids == 0);
		Assert(proc->subxids.overflowed == false);
	}
}


/*
 * ProcArrayClearTransaction -- clear the transaction fields
 *
 * This is used after successfully preparing a 2-phase transaction.  We are
 * not actually reporting the transaction's XID as no longer running --- it
 * will still appear as running because the 2PC's gxact is in the ProcArray
 * too.  We just have to clear out our own PGPROC.
 */
void
ProcArrayClearTransaction(PGPROC *proc)
{
	/*
	 * We can skip locking ProcArrayLock here, because this action does not
B
Bruce Momjian 已提交
298 299
	 * actually change anyone's view of the set of running XIDs: our entry is
	 * duplicate with the gxact that has already been inserted into the
300 301 302 303 304
	 * ProcArray.
	 */
	proc->xid = InvalidTransactionId;
	proc->lxid = InvalidLocalTransactionId;
	proc->xmin = InvalidTransactionId;
305 306 307 308

	/* redundant, but just in case */
	proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
	proc->inCommit = false;
309 310 311 312 313 314 315

	/* Clear the subtransaction-XID cache too */
	proc->subxids.nxids = 0;
	proc->subxids.overflowed = false;
}


316 317 318
/*
 * TransactionIdIsInProgress -- is given transaction running in some backend
 *
319 320
 * Aside from some shortcuts such as checking RecentXmin and our own Xid,
 * there are three possibilities for finding a running transaction:
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
 *
 * 1. the given Xid is a main transaction Id.  We will find this out cheaply
 * by looking at the PGPROC struct for each backend.
 *
 * 2. the given Xid is one of the cached subxact Xids in the PGPROC array.
 * We can find this out cheaply too.
 *
 * 3. Search the SubTrans tree to find the Xid's topmost parent, and then
 * see if that is running according to PGPROC.	This is the slowest, but
 * sadly it has to be done always if the other two failed, unless we see
 * that the cached subxact sets are complete (none have overflowed).
 *
 * ProcArrayLock has to be held while we do 1 and 2.  If we save the top Xids
 * while doing 1, we can release the ProcArrayLock while we do 3.  This buys
 * back some concurrency (we can't retrieve the main Xids from PGPROC again
 * anyway; see GetNewTransactionId).
 */
bool
TransactionIdIsInProgress(TransactionId xid)
{
341 342
	static TransactionId *xids = NULL;
	int			nxids = 0;
343
	ProcArrayStruct *arrayP = procArray;
344
	TransactionId topxid;
345 346 347 348
	int			i,
				j;

	/*
B
Bruce Momjian 已提交
349
	 * Don't bother checking a transaction older than RecentXmin; it could not
B
Bruce Momjian 已提交
350 351 352
	 * possibly still be running.  (Note: in particular, this guarantees that
	 * we reject InvalidTransactionId, FrozenTransactionId, etc as not
	 * running.)
353 354 355 356 357 358 359
	 */
	if (TransactionIdPrecedes(xid, RecentXmin))
	{
		xc_by_recent_xmin_inc();
		return false;
	}

360 361 362 363 364 365 366 367 368 369 370
	/*
	 * We may have just checked the status of this transaction, so if it is
	 * already known to be completed, we can fall out without any access to
	 * shared memory.
	 */
	if (TransactionIdIsKnownCompleted(xid))
	{
		xc_by_known_xact_inc();
		return false;
	}

371 372 373 374 375 376 377 378 379 380 381
	/*
	 * Also, we can handle our own transaction (and subtransactions) without
	 * any access to shared memory.
	 */
	if (TransactionIdIsCurrentTransactionId(xid))
	{
		xc_by_my_xact_inc();
		return true;
	}

	/*
B
Bruce Momjian 已提交
382 383
	 * If not first time through, get workspace to remember main XIDs in. We
	 * malloc it permanently to avoid repeated palloc/pfree overhead.
384 385 386 387 388 389 390 391 392 393
	 */
	if (xids == NULL)
	{
		xids = (TransactionId *)
			malloc(arrayP->maxProcs * sizeof(TransactionId));
		if (xids == NULL)
			ereport(ERROR,
					(errcode(ERRCODE_OUT_OF_MEMORY),
					 errmsg("out of memory")));
	}
394 395 396

	LWLockAcquire(ProcArrayLock, LW_SHARED);

397 398 399 400 401 402 403 404 405 406 407 408
	/*
	 * Now that we have the lock, we can check latestCompletedXid; if the
	 * target Xid is after that, it's surely still running.
	 */
	if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid, xid))
	{
		LWLockRelease(ProcArrayLock);
		xc_by_latest_xid_inc();
		return true;
	}

	/* No shortcuts, gotta grovel through the array */
409 410
	for (i = 0; i < arrayP->numProcs; i++)
	{
B
Bruce Momjian 已提交
411
		volatile PGPROC *proc = arrayP->procs[i];
412 413 414 415 416
		TransactionId pxid;

		/* Ignore my own proc --- dealt with it above */
		if (proc == MyProc)
			continue;
417 418

		/* Fetch xid just once - see GetNewTransactionId */
419
		pxid = proc->xid;
420 421 422 423 424 425 426 427 428

		if (!TransactionIdIsValid(pxid))
			continue;

		/*
		 * Step 1: check the main Xid
		 */
		if (TransactionIdEquals(pxid, xid))
		{
429
			LWLockRelease(ProcArrayLock);
430
			xc_by_main_xid_inc();
431
			return true;
432 433 434
		}

		/*
B
Bruce Momjian 已提交
435 436
		 * We can ignore main Xids that are younger than the target Xid, since
		 * the target could not possibly be their child.
437 438 439 440 441 442 443 444 445 446 447 448 449 450
		 */
		if (TransactionIdPrecedes(xid, pxid))
			continue;

		/*
		 * Step 2: check the cached child-Xids arrays
		 */
		for (j = proc->subxids.nxids - 1; j >= 0; j--)
		{
			/* Fetch xid just once - see GetNewTransactionId */
			TransactionId cxid = proc->subxids.xids[j];

			if (TransactionIdEquals(cxid, xid))
			{
451
				LWLockRelease(ProcArrayLock);
452
				xc_by_child_xid_inc();
453
				return true;
454 455 456 457
			}
		}

		/*
B
Bruce Momjian 已提交
458 459 460 461 462
		 * Save the main Xid for step 3.  We only need to remember main Xids
		 * that have uncached children.  (Note: there is no race condition
		 * here because the overflowed flag cannot be cleared, only set, while
		 * we hold ProcArrayLock.  So we can't miss an Xid that we need to
		 * worry about.)
463 464 465 466 467 468 469 470 471 472 473 474
		 */
		if (proc->subxids.overflowed)
			xids[nxids++] = pxid;
	}

	LWLockRelease(ProcArrayLock);

	/*
	 * If none of the relevant caches overflowed, we know the Xid is not
	 * running without looking at pg_subtrans.
	 */
	if (nxids == 0)
475 476
	{
		xc_no_overflow_inc();
477
		return false;
478
	}
479 480 481 482

	/*
	 * Step 3: have to check pg_subtrans.
	 *
483 484 485 486
	 * At this point, we know it's either a subtransaction of one of the Xids
	 * in xids[], or it's not running.  If it's an already-failed
	 * subtransaction, we want to say "not running" even though its parent may
	 * still be running.  So first, check pg_clog to see if it's been aborted.
487 488 489 490
	 */
	xc_slow_answer_inc();

	if (TransactionIdDidAbort(xid))
491
		return false;
492 493

	/*
B
Bruce Momjian 已提交
494
	 * It isn't aborted, so check whether the transaction tree it belongs to
B
Bruce Momjian 已提交
495 496
	 * is still running (or, more precisely, whether it was running when we
	 * held ProcArrayLock).
497 498 499 500 501 502 503 504
	 */
	topxid = SubTransGetTopmostTransaction(xid);
	Assert(TransactionIdIsValid(topxid));
	if (!TransactionIdEquals(topxid, xid))
	{
		for (i = 0; i < nxids; i++)
		{
			if (TransactionIdEquals(xids[i], topxid))
505
				return true;
506 507 508
		}
	}

509
	return false;
510 511
}

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
/*
 * TransactionIdIsActive -- is xid the top-level XID of an active backend?
 *
 * This differs from TransactionIdIsInProgress in that it ignores prepared
 * transactions.  Also, we ignore subtransactions since that's not needed
 * for current uses.
 */
bool
TransactionIdIsActive(TransactionId xid)
{
	bool		result = false;
	ProcArrayStruct *arrayP = procArray;
	int			i;

	/*
B
Bruce Momjian 已提交
527 528
	 * Don't bother checking a transaction older than RecentXmin; it could not
	 * possibly still be running.
529 530 531 532 533 534 535 536
	 */
	if (TransactionIdPrecedes(xid, RecentXmin))
		return false;

	LWLockAcquire(ProcArrayLock, LW_SHARED);

	for (i = 0; i < arrayP->numProcs; i++)
	{
B
Bruce Momjian 已提交
537
		volatile PGPROC *proc = arrayP->procs[i];
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

		/* Fetch xid just once - see GetNewTransactionId */
		TransactionId pxid = proc->xid;

		if (!TransactionIdIsValid(pxid))
			continue;

		if (proc->pid == 0)
			continue;			/* ignore prepared transactions */

		if (TransactionIdEquals(pxid, xid))
		{
			result = true;
			break;
		}
	}

	LWLockRelease(ProcArrayLock);

	return result;
}


561 562 563 564 565 566 567
/*
 * GetOldestXmin -- returns oldest transaction that was running
 *					when any current transaction was started.
 *
 * If allDbs is TRUE then all backends are considered; if allDbs is FALSE
 * then only backends running in my own database are considered.
 *
568 569
 * If ignoreVacuum is TRUE then backends with the PROC_IN_VACUUM flag set are
 * ignored.
570
 *
571 572 573
 * This is used by VACUUM to decide which deleted tuples must be preserved
 * in a table.	allDbs = TRUE is needed for shared relations, but allDbs =
 * FALSE is sufficient for non-shared relations, since only backends in my
B
Bruce Momjian 已提交
574
 * own database could ever see the tuples in them.	Also, we can ignore
575 576
 * concurrently running lazy VACUUMs because (a) they must be working on other
 * tables, and (b) they don't need to do snapshot-based lookups.
577 578
 *
 * This is also used to determine where to truncate pg_subtrans.  allDbs
579
 * must be TRUE for that case, and ignoreVacuum FALSE.
580
 *
581
 * Note: we include all currently running xids in the set of considered xids.
582 583
 * This ensures that if a just-started xact has not yet set its snapshot,
 * when it does set the snapshot it cannot set xmin less than what we compute.
584
 * See notes in src/backend/access/transam/README.
585 586
 */
TransactionId
587
GetOldestXmin(bool allDbs, bool ignoreVacuum)
588 589 590 591 592
{
	ProcArrayStruct *arrayP = procArray;
	TransactionId result;
	int			index;

593 594
	LWLockAcquire(ProcArrayLock, LW_SHARED);

595
	/*
B
Bruce Momjian 已提交
596 597 598 599
	 * We initialize the MIN() calculation with latestCompletedXid + 1. This
	 * is a lower bound for the XIDs that might appear in the ProcArray later,
	 * and so protects us against overestimating the result due to future
	 * additions.
600
	 */
601 602 603
	result = ShmemVariableCache->latestCompletedXid;
	Assert(TransactionIdIsNormal(result));
	TransactionIdAdvance(result);
604 605 606

	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
607
		volatile PGPROC *proc = arrayP->procs[index];
608

609
		if (ignoreVacuum && (proc->vacuumFlags & PROC_IN_VACUUM))
610 611
			continue;

612 613 614 615 616
		if (allDbs || proc->databaseId == MyDatabaseId)
		{
			/* Fetch xid just once - see GetNewTransactionId */
			TransactionId xid = proc->xid;

617 618 619 620 621 622 623 624 625
			/* First consider the transaction's own Xid, if any */
			if (TransactionIdIsNormal(xid) &&
				TransactionIdPrecedes(xid, result))
				result = xid;

			/*
			 * Also consider the transaction's Xmin, if set.
			 *
			 * We must check both Xid and Xmin because a transaction might
B
Bruce Momjian 已提交
626 627
			 * have an Xmin but not (yet) an Xid; conversely, if it has an
			 * Xid, that could determine some not-yet-set Xmin.
628 629 630 631 632
			 */
			xid = proc->xmin;	/* Fetch just once */
			if (TransactionIdIsNormal(xid) &&
				TransactionIdPrecedes(xid, result))
				result = xid;
633 634 635 636 637 638 639 640
		}
	}

	LWLockRelease(ProcArrayLock);

	return result;
}

641
/*
642 643 644
 * GetSnapshotData -- returns information about running transactions.
 *
 * The returned snapshot includes xmin (lowest still-running xact ID),
645
 * xmax (highest completed xact ID + 1), and a list of running xact IDs
646 647 648 649 650 651 652 653
 * in the range xmin <= xid < xmax.  It is used as follows:
 *		All xact IDs < xmin are considered finished.
 *		All xact IDs >= xmax are considered still running.
 *		For an xact ID xmin <= xid < xmax, consult list to see whether
 *		it is considered running or not.
 * This ensures that the set of transactions seen as "running" by the
 * current xact will not change after it takes the snapshot.
 *
654 655 656 657 658 659 660
 * All running top-level XIDs are included in the snapshot, except for lazy
 * VACUUM processes.  We also try to include running subtransaction XIDs,
 * but since PGPROC has only a limited cache area for subxact XIDs, full
 * information may not be available.  If we find any overflowed subxid arrays,
 * we have to mark the snapshot's subxid data as overflowed, and extra work
 * will need to be done to determine what's running (see XidInMVCCSnapshot()
 * in tqual.c).
661 662 663
 *
 * We also update the following backend-global variables:
 *		TransactionXmin: the oldest xmin of any snapshot in use in the
664
 *			current transaction (this is the same as MyProc->xmin).
665 666 667
 *		RecentXmin: the xmin computed for the most recent snapshot.  XIDs
 *			older than this are known not running any more.
 *		RecentGlobalXmin: the global xmin (oldest TransactionXmin across all
668
 *			running transactions, except those running LAZY VACUUM).  This is
T
Tom Lane 已提交
669
 *			the same computation done by GetOldestXmin(true, true).
670 671 672
 *
 * Note: this function should probably not be called with an argument that's
 * not statically allocated (see xip allocation below).
673 674
 */
Snapshot
675
GetSnapshotData(Snapshot snapshot)
676 677 678 679 680 681 682
{
	ProcArrayStruct *arrayP = procArray;
	TransactionId xmin;
	TransactionId xmax;
	TransactionId globalxmin;
	int			index;
	int			count = 0;
683
	int			subcount = 0;
684 685 686 687

	Assert(snapshot != NULL);

	/*
B
Bruce Momjian 已提交
688 689
	 * Allocating space for maxProcs xids is usually overkill; numProcs would
	 * be sufficient.  But it seems better to do the malloc while not holding
690 691
	 * the lock, so we can't look at numProcs.  Likewise, we allocate much
	 * more subxip storage than is probably needed.
692 693
	 *
	 * This does open a possibility for avoiding repeated malloc/free: since
B
Bruce Momjian 已提交
694
	 * maxProcs does not change at runtime, we can simply reuse the previous
695
	 * xip arrays if any.  (This relies on the fact that all callers pass
B
Bruce Momjian 已提交
696
	 * static SnapshotData structs.)
697 698 699 700 701 702 703
	 */
	if (snapshot->xip == NULL)
	{
		/*
		 * First call for this snapshot
		 */
		snapshot->xip = (TransactionId *)
704
			malloc(arrayP->maxProcs * sizeof(TransactionId));
705 706 707 708
		if (snapshot->xip == NULL)
			ereport(ERROR,
					(errcode(ERRCODE_OUT_OF_MEMORY),
					 errmsg("out of memory")));
709 710 711 712 713 714 715
		Assert(snapshot->subxip == NULL);
		snapshot->subxip = (TransactionId *)
			malloc(arrayP->maxProcs * PGPROC_MAX_CACHED_SUBXIDS * sizeof(TransactionId));
		if (snapshot->subxip == NULL)
			ereport(ERROR,
					(errcode(ERRCODE_OUT_OF_MEMORY),
					 errmsg("out of memory")));
716 717 718
	}

	/*
B
Bruce Momjian 已提交
719
	 * It is sufficient to get shared lock on ProcArrayLock, even if we are
720
	 * going to set MyProc->xmin.
721
	 */
722
	LWLockAcquire(ProcArrayLock, LW_SHARED);
723

724 725 726 727
	/* xmax is always latestCompletedXid + 1 */
	xmax = ShmemVariableCache->latestCompletedXid;
	Assert(TransactionIdIsNormal(xmax));
	TransactionIdAdvance(xmax);
728

729 730 731
	/* initialize xmin calculation with xmax */
	globalxmin = xmin = xmax;

B
Bruce Momjian 已提交
732
	/*
B
Bruce Momjian 已提交
733 734
	 * Spin over procArray checking xid, xmin, and subxids.  The goal is to
	 * gather all active xids, find the lowest xmin, and try to record
B
Bruce Momjian 已提交
735 736
	 * subxids.
	 */
737 738
	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
739
		volatile PGPROC *proc = arrayP->procs[index];
740 741 742
		TransactionId xid;

		/* Ignore procs running LAZY VACUUM */
743
		if (proc->vacuumFlags & PROC_IN_VACUUM)
744 745 746 747 748 749 750
			continue;

		/* Update globalxmin to be the smallest valid xmin */
		xid = proc->xmin;		/* fetch just once */
		if (TransactionIdIsNormal(xid) &&
			TransactionIdPrecedes(xid, globalxmin))
			globalxmin = xid;
751 752

		/* Fetch xid just once - see GetNewTransactionId */
753
		xid = proc->xid;
754 755

		/*
756
		 * If the transaction has been assigned an xid < xmax we add it to the
B
Bruce Momjian 已提交
757
		 * snapshot, and update xmin if necessary.	There's no need to store
758 759
		 * XIDs >= xmax, since we'll treat them as running anyway.  We don't
		 * bother to examine their subxids either.
760 761 762
		 *
		 * We don't include our own XID (if any) in the snapshot, but we must
		 * include it into xmin.
763 764
		 */
		if (TransactionIdIsNormal(xid))
765 766 767 768 769 770 771 772
		{
			if (TransactionIdFollowsOrEquals(xid, xmax))
				continue;
			if (proc != MyProc)
				snapshot->xip[count++] = xid;
			if (TransactionIdPrecedes(xid, xmin))
				xmin = xid;
		}
773 774 775 776

		/*
		 * Save subtransaction XIDs if possible (if we've already overflowed,
		 * there's no point).  Note that the subxact XIDs must be later than
777 778 779
		 * their parent, so no need to check them against xmin.  We could
		 * filter against xmax, but it seems better not to do that much work
		 * while holding the ProcArrayLock.
780 781
		 *
		 * The other backend can add more subxids concurrently, but cannot
B
Bruce Momjian 已提交
782 783 784
		 * remove any.	Hence it's important to fetch nxids just once. Should
		 * be safe to use memcpy, though.  (We needn't worry about missing any
		 * xids added concurrently, because they must postdate xmax.)
785 786
		 *
		 * Again, our own XIDs are not included in the snapshot.
787
		 */
788
		if (subcount >= 0 && proc != MyProc)
789 790
		{
			if (proc->subxids.overflowed)
B
Bruce Momjian 已提交
791
				subcount = -1;	/* overflowed */
792 793
			else
			{
B
Bruce Momjian 已提交
794
				int			nxids = proc->subxids.nxids;
795 796 797 798

				if (nxids > 0)
				{
					memcpy(snapshot->subxip + subcount,
799
						   (void *) proc->subxids.xids,
800 801 802 803 804
						   nxids * sizeof(TransactionId));
					subcount += nxids;
				}
			}
		}
805 806
	}

807
	if (!TransactionIdIsValid(MyProc->xmin))
808 809 810 811 812
		MyProc->xmin = TransactionXmin = xmin;

	LWLockRelease(ProcArrayLock);

	/*
B
Bruce Momjian 已提交
813 814 815
	 * Update globalxmin to include actual process xids.  This is a slightly
	 * different way of computing it than GetOldestXmin uses, but should give
	 * the same result.
816 817 818 819 820 821 822 823 824 825 826
	 */
	if (TransactionIdPrecedes(xmin, globalxmin))
		globalxmin = xmin;

	/* Update global variables too */
	RecentGlobalXmin = globalxmin;
	RecentXmin = xmin;

	snapshot->xmin = xmin;
	snapshot->xmax = xmax;
	snapshot->xcnt = count;
827
	snapshot->subxcnt = subcount;
828

829
	snapshot->curcid = GetCurrentCommandId(false);
830

831 832 833 834 835 836 837 838
	/*
	 * This is a new snapshot, so set both refcounts are zero, and mark it
	 * as not copied in persistent memory.
	 */
	snapshot->active_count = 0;
	snapshot->regd_count = 0;
	snapshot->copied = false;

839 840 841
	return snapshot;
}

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
/*
 * GetTransactionsInCommit -- Get the XIDs of transactions that are committing
 *
 * Constructs an array of XIDs of transactions that are currently in commit
 * critical sections, as shown by having inCommit set in their PGPROC entries.
 *
 * *xids_p is set to a palloc'd array that should be freed by the caller.
 * The return value is the number of valid entries.
 *
 * Note that because backends set or clear inCommit without holding any lock,
 * the result is somewhat indeterminate, but we don't really care.  Even in
 * a multiprocessor with delayed writes to shared memory, it should be certain
 * that setting of inCommit will propagate to shared memory when the backend
 * takes the WALInsertLock, so we cannot fail to see an xact as inCommit if
 * it's already inserted its commit record.  Whether it takes a little while
 * for clearing of inCommit to propagate is unimportant for correctness.
 */
int
GetTransactionsInCommit(TransactionId **xids_p)
{
	ProcArrayStruct *arrayP = procArray;
	TransactionId *xids;
B
Bruce Momjian 已提交
864 865
	int			nxids;
	int			index;
866 867 868 869 870 871 872 873

	xids = (TransactionId *) palloc(arrayP->maxProcs * sizeof(TransactionId));
	nxids = 0;

	LWLockAcquire(ProcArrayLock, LW_SHARED);

	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
874 875
		volatile PGPROC *proc = arrayP->procs[index];

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
		/* Fetch xid just once - see GetNewTransactionId */
		TransactionId pxid = proc->xid;

		if (proc->inCommit && TransactionIdIsValid(pxid))
			xids[nxids++] = pxid;
	}

	LWLockRelease(ProcArrayLock);

	*xids_p = xids;
	return nxids;
}

/*
 * HaveTransactionsInCommit -- Are any of the specified XIDs in commit?
 *
 * This is used with the results of GetTransactionsInCommit to see if any
 * of the specified XIDs are still in their commit critical sections.
 *
 * Note: this is O(N^2) in the number of xacts that are/were in commit, but
 * those numbers should be small enough for it not to be a problem.
 */
bool
HaveTransactionsInCommit(TransactionId *xids, int nxids)
{
B
Bruce Momjian 已提交
901
	bool		result = false;
902
	ProcArrayStruct *arrayP = procArray;
B
Bruce Momjian 已提交
903
	int			index;
904 905 906 907 908

	LWLockAcquire(ProcArrayLock, LW_SHARED);

	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
909 910
		volatile PGPROC *proc = arrayP->procs[index];

911 912 913 914 915
		/* Fetch xid just once - see GetNewTransactionId */
		TransactionId pxid = proc->xid;

		if (proc->inCommit && TransactionIdIsValid(pxid))
		{
B
Bruce Momjian 已提交
916
			int			i;
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

			for (i = 0; i < nxids; i++)
			{
				if (xids[i] == pxid)
				{
					result = true;
					break;
				}
			}
			if (result)
				break;
		}
	}

	LWLockRelease(ProcArrayLock);

	return result;
}

936 937
/*
 * BackendPidGetProc -- get a backend's PGPROC given its PID
938 939 940 941
 *
 * Returns NULL if not found.  Note that it is up to the caller to be
 * sure that the question remains meaningful for long enough for the
 * answer to be used ...
942
 */
943
PGPROC *
944 945 946 947 948 949
BackendPidGetProc(int pid)
{
	PGPROC	   *result = NULL;
	ProcArrayStruct *arrayP = procArray;
	int			index;

950 951 952
	if (pid == 0)				/* never match dummy PGPROCs */
		return NULL;

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	LWLockAcquire(ProcArrayLock, LW_SHARED);

	for (index = 0; index < arrayP->numProcs; index++)
	{
		PGPROC	   *proc = arrayP->procs[index];

		if (proc->pid == pid)
		{
			result = proc;
			break;
		}
	}

	LWLockRelease(ProcArrayLock);

	return result;
}

T
Tatsuo Ishii 已提交
971 972 973 974 975 976
/*
 * BackendXidGetPid -- get a backend's pid given its XID
 *
 * Returns 0 if not found or it's a prepared transaction.  Note that
 * it is up to the caller to be sure that the question remains
 * meaningful for long enough for the answer to be used ...
B
Bruce Momjian 已提交
977
 *
T
Tatsuo Ishii 已提交
978 979
 * Only main transaction Ids are considered.  This function is mainly
 * useful for determining what backend owns a lock.
980
 *
B
Bruce Momjian 已提交
981
 * Beware that not every xact has an XID assigned.	However, as long as you
982
 * only call this using an XID found on disk, you're safe.
T
Tatsuo Ishii 已提交
983 984 985 986
 */
int
BackendXidGetPid(TransactionId xid)
{
B
Bruce Momjian 已提交
987
	int			result = 0;
T
Tatsuo Ishii 已提交
988 989 990 991 992 993 994 995 996 997
	ProcArrayStruct *arrayP = procArray;
	int			index;

	if (xid == InvalidTransactionId)	/* never match invalid xid */
		return 0;

	LWLockAcquire(ProcArrayLock, LW_SHARED);

	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
998
		volatile PGPROC *proc = arrayP->procs[index];
T
Tatsuo Ishii 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

		if (proc->xid == xid)
		{
			result = proc->pid;
			break;
		}
	}

	LWLockRelease(ProcArrayLock);

	return result;
}

1012 1013 1014 1015 1016 1017 1018 1019 1020
/*
 * IsBackendPid -- is a given pid a running backend
 */
bool
IsBackendPid(int pid)
{
	return (BackendPidGetProc(pid) != NULL);
}

1021 1022 1023 1024

/*
 * GetCurrentVirtualXIDs -- returns an array of currently active VXIDs.
 *
1025
 * The array is palloc'd. The number of valid entries is returned into *nvxids.
1026
 *
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
 * The arguments allow filtering the set of VXIDs returned.  Our own process
 * is always skipped.  In addition:
 *	If limitXmin is not InvalidTransactionId, skip processes with
 *		xmin > limitXmin.
 *	If excludeXmin0 is true, skip processes with xmin = 0.
 *	If allDbs is false, skip processes attached to other databases.
 *	If excludeVacuum isn't zero, skip processes for which
 *		(vacuumFlags & excludeVacuum) is not zero.
 *
 * Note: the purpose of the limitXmin and excludeXmin0 parameters is to
 * allow skipping backends whose oldest live snapshot is no older than
 * some snapshot we have.  Since we examine the procarray with only shared
 * lock, there are race conditions: a backend could set its xmin just after
 * we look.  Indeed, on multiprocessors with weak memory ordering, the
 * other backend could have set its xmin *before* we look.  We know however
 * that such a backend must have held shared ProcArrayLock overlapping our
 * own hold of ProcArrayLock, else we would see its xmin update.  Therefore,
 * any snapshot the other backend is taking concurrently with our scan cannot
 * consider any transactions as still running that we think are committed
 * (since backends must hold ProcArrayLock exclusive to commit).
1047 1048
 */
VirtualTransactionId *
1049 1050 1051
GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0,
					  bool allDbs, int excludeVacuum,
					  int *nvxids)
1052 1053 1054 1055 1056 1057
{
	VirtualTransactionId *vxids;
	ProcArrayStruct *arrayP = procArray;
	int			count = 0;
	int			index;

1058
	/* allocate what's certainly enough result space */
1059
	vxids = (VirtualTransactionId *)
1060
		palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
1061 1062 1063 1064 1065

	LWLockAcquire(ProcArrayLock, LW_SHARED);

	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
1066
		volatile PGPROC *proc = arrayP->procs[index];
1067 1068 1069 1070

		if (proc == MyProc)
			continue;

1071 1072 1073
		if (excludeVacuum & proc->vacuumFlags)
			continue;

1074
		if (allDbs || proc->databaseId == MyDatabaseId)
1075
		{
1076
			/* Fetch xmin just once - might change on us */
1077 1078
			TransactionId pxmin = proc->xmin;

1079 1080 1081
			if (excludeXmin0 && !TransactionIdIsValid(pxmin))
				continue;

1082
			/*
1083 1084
			 * InvalidTransactionId precedes all other XIDs, so a proc that
			 * hasn't set xmin yet will not be rejected by this test.
1085 1086
			 */
			if (!TransactionIdIsValid(limitXmin) ||
1087
				TransactionIdPrecedesOrEquals(pxmin, limitXmin))
1088 1089
			{
				VirtualTransactionId vxid;
1090

1091 1092 1093 1094
				GET_VXID_FROM_PGPROC(vxid, *proc);
				if (VirtualTransactionIdIsValid(vxid))
					vxids[count++] = vxid;
			}
1095 1096 1097 1098 1099
		}
	}

	LWLockRelease(ProcArrayLock);

1100
	*nvxids = count;
1101 1102 1103 1104
	return vxids;
}


1105 1106 1107 1108 1109
/*
 * CountActiveBackends --- count backends (other than myself) that are in
 *		active transactions.  This is used as a heuristic to decide if
 *		a pre-XLOG-flush delay is worthwhile during commit.
 *
1110 1111
 * Do not count backends that are blocked waiting for locks, since they are
 * not going to get to run until someone else commits.
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
 */
int
CountActiveBackends(void)
{
	ProcArrayStruct *arrayP = procArray;
	int			count = 0;
	int			index;

	/*
	 * Note: for speed, we don't acquire ProcArrayLock.  This is a little bit
B
Bruce Momjian 已提交
1122 1123
	 * bogus, but since we are only testing fields for zero or nonzero, it
	 * should be OK.  The result is only used for heuristic purposes anyway...
1124 1125 1126
	 */
	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
1127
		volatile PGPROC *proc = arrayP->procs[index];
1128

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
		/*
		 * Since we're not holding a lock, need to check that the pointer is
		 * valid. Someone holding the lock could have incremented numProcs
		 * already, but not yet inserted a valid pointer to the array.
		 *
		 * If someone just decremented numProcs, 'proc' could also point to a
		 * PGPROC entry that's no longer in the array. It still points to a
		 * PGPROC struct, though, because freed PGPPROC entries just go to
		 * the free list and are recycled. Its contents are nonsense in that
		 * case, but that's acceptable for this function.
		 */
		if (proc != NULL)
			continue;

1143 1144
		if (proc == MyProc)
			continue;			/* do not count myself */
1145 1146 1147
		if (proc->pid == 0)
			continue;			/* do not count prepared xacts */
		if (proc->xid == InvalidTransactionId)
1148
			continue;			/* do not count if no XID assigned */
1149 1150 1151 1152 1153 1154 1155 1156
		if (proc->waitLock != NULL)
			continue;			/* do not count if blocked on a lock */
		count++;
	}

	return count;
}

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
/*
 * CountDBBackends --- count backends that are using specified database
 */
int
CountDBBackends(Oid databaseid)
{
	ProcArrayStruct *arrayP = procArray;
	int			count = 0;
	int			index;

	LWLockAcquire(ProcArrayLock, LW_SHARED);

	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
1171
		volatile PGPROC *proc = arrayP->procs[index];
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

		if (proc->pid == 0)
			continue;			/* do not count prepared xacts */
		if (proc->databaseId == databaseid)
			count++;
	}

	LWLockRelease(ProcArrayLock);

	return count;
}

/*
 * CountUserBackends --- count backends that are used by specified user
 */
int
CountUserBackends(Oid roleid)
{
	ProcArrayStruct *arrayP = procArray;
	int			count = 0;
	int			index;

	LWLockAcquire(ProcArrayLock, LW_SHARED);

	for (index = 0; index < arrayP->numProcs; index++)
	{
B
Bruce Momjian 已提交
1198
		volatile PGPROC *proc = arrayP->procs[index];
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

		if (proc->pid == 0)
			continue;			/* do not count prepared xacts */
		if (proc->roleId == roleid)
			count++;
	}

	LWLockRelease(ProcArrayLock);

	return count;
}

1211
/*
1212
 * CountOtherDBBackends -- check for other backends running in the given DB
1213 1214 1215
 *
 * If there are other backends in the DB, we will wait a maximum of 5 seconds
 * for them to exit.  Autovacuum backends are encouraged to exit early by
1216
 * sending them SIGTERM, but normal user backends are just waited for.
1217 1218 1219 1220 1221
 *
 * The current backend is always ignored; it is caller's responsibility to
 * check whether the current backend uses the given DB, if it's important.
 *
 * Returns TRUE if there are (still) other backends in the DB, FALSE if not.
1222 1223
 * Also, *nbackends and *nprepared are set to the number of other backends
 * and prepared transactions in the DB, respectively.
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
 *
 * This function is used to interlock DROP DATABASE and related commands
 * against there being any active backends in the target DB --- dropping the
 * DB while active backends remain would be a Bad Thing.  Note that we cannot
 * detect here the possibility of a newly-started backend that is trying to
 * connect to the doomed database, so additional interlocking is needed during
 * backend startup.  The caller should normally hold an exclusive lock on the
 * target DB before calling this, which is one reason we mustn't wait
 * indefinitely.
 */
bool
1235
CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
1236 1237
{
	ProcArrayStruct *arrayP = procArray;
1238 1239
#define MAXAUTOVACPIDS  10		/* max autovacs to SIGTERM per iteration */
	int			autovac_pids[MAXAUTOVACPIDS];
1240 1241 1242 1243 1244
	int			tries;

	/* 50 tries with 100ms sleep between tries makes 5 sec total wait */
	for (tries = 0; tries < 50; tries++)
	{
1245
		int			nautovacs = 0;
1246 1247 1248 1249 1250
		bool		found = false;
		int			index;

		CHECK_FOR_INTERRUPTS();

1251 1252
		*nbackends = *nprepared = 0;

1253 1254 1255 1256
		LWLockAcquire(ProcArrayLock, LW_SHARED);

		for (index = 0; index < arrayP->numProcs; index++)
		{
B
Bruce Momjian 已提交
1257
			volatile PGPROC *proc = arrayP->procs[index];
1258 1259 1260 1261 1262 1263 1264 1265

			if (proc->databaseId != databaseId)
				continue;
			if (proc == MyProc)
				continue;

			found = true;

1266 1267
			if (proc->pid == 0)
				(*nprepared)++;
1268 1269
			else
			{
1270 1271 1272 1273
				(*nbackends)++;
				if ((proc->vacuumFlags & PROC_IS_AUTOVACUUM) &&
					nautovacs < MAXAUTOVACPIDS)
					autovac_pids[nautovacs++] = proc->pid;
1274 1275 1276
			}
		}

1277 1278
		LWLockRelease(ProcArrayLock);

1279
		if (!found)
B
Bruce Momjian 已提交
1280
			return false;		/* no conflicting backends, so done */
1281

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
		/*
		 * Send SIGTERM to any conflicting autovacuums before sleeping.
		 * We postpone this step until after the loop because we don't
		 * want to hold ProcArrayLock while issuing kill().
		 * We have no idea what might block kill() inside the kernel...
		 */
		for (index = 0; index < nautovacs; index++)
			(void) kill(autovac_pids[index], SIGTERM);	/* ignore any error */

		/* sleep, then try again */
B
Bruce Momjian 已提交
1292
		pg_usleep(100 * 1000L); /* 100ms */
1293 1294
	}

B
Bruce Momjian 已提交
1295
	return true;				/* timed out, still conflicts */
1296 1297
}

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

#define XidCacheRemove(i) \
	do { \
		MyProc->subxids.xids[i] = MyProc->subxids.xids[MyProc->subxids.nxids - 1]; \
		MyProc->subxids.nxids--; \
	} while (0)

/*
 * XidCacheRemoveRunningXids
 *
 * Remove a bunch of TransactionIds from the list of known-running
 * subtransactions for my backend.	Both the specified xid and those in
 * the xids[] array (of length nxids) are removed from the subxids cache.
1311
 * latestXid must be the latest XID among the group.
1312 1313
 */
void
1314 1315 1316
XidCacheRemoveRunningXids(TransactionId xid,
						  int nxids, const TransactionId *xids,
						  TransactionId latestXid)
1317 1318 1319 1320
{
	int			i,
				j;

1321
	Assert(TransactionIdIsValid(xid));
1322 1323 1324

	/*
	 * We must hold ProcArrayLock exclusively in order to remove transactions
1325 1326 1327 1328
	 * from the PGPROC array.  (See src/backend/access/transam/README.)  It's
	 * possible this could be relaxed since we know this routine is only used
	 * to abort subtransactions, but pending closer analysis we'd best be
	 * conservative.
1329 1330 1331 1332
	 */
	LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);

	/*
B
Bruce Momjian 已提交
1333 1334 1335
	 * Under normal circumstances xid and xids[] will be in increasing order,
	 * as will be the entries in subxids.  Scan backwards to avoid O(N^2)
	 * behavior when removing a lot of xids.
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
	 */
	for (i = nxids - 1; i >= 0; i--)
	{
		TransactionId anxid = xids[i];

		for (j = MyProc->subxids.nxids - 1; j >= 0; j--)
		{
			if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
			{
				XidCacheRemove(j);
				break;
			}
		}
B
Bruce Momjian 已提交
1349

1350
		/*
B
Bruce Momjian 已提交
1351 1352 1353 1354 1355
		 * Ordinarily we should have found it, unless the cache has
		 * overflowed. However it's also possible for this routine to be
		 * invoked multiple times for the same subtransaction, in case of an
		 * error during AbortSubTransaction.  So instead of Assert, emit a
		 * debug warning.
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
		 */
		if (j < 0 && !MyProc->subxids.overflowed)
			elog(WARNING, "did not find subXID %u in MyProc", anxid);
	}

	for (j = MyProc->subxids.nxids - 1; j >= 0; j--)
	{
		if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
		{
			XidCacheRemove(j);
			break;
		}
	}
	/* Ordinarily we should have found it, unless the cache has overflowed */
	if (j < 0 && !MyProc->subxids.overflowed)
		elog(WARNING, "did not find subXID %u in MyProc", xid);

1373 1374 1375 1376 1377
	/* Also advance global latestCompletedXid while holding the lock */
	if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
							  latestXid))
		ShmemVariableCache->latestCompletedXid = latestXid;

1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
	LWLockRelease(ProcArrayLock);
}

#ifdef XIDCACHE_DEBUG

/*
 * Print stats about effectiveness of XID cache
 */
static void
DisplayXidCache(void)
{
	fprintf(stderr,
1390
			"XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, nooflo: %ld, slow: %ld\n",
1391
			xc_by_recent_xmin,
1392
			xc_by_known_xact,
1393
			xc_by_my_xact,
1394
			xc_by_latest_xid,
1395 1396
			xc_by_main_xid,
			xc_by_child_xid,
1397
			xc_no_overflow,
1398 1399 1400 1401
			xc_slow_answer);
}

#endif   /* XIDCACHE_DEBUG */