xact.c 113.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * xact.c
4
 *	  top level transaction system support routines
5
 *
6 7
 * See src/backend/access/transam/README for more information.
 *
8
 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
9
 * Portions Copyright (c) 1994, Regents of the University of California
10 11 12
 *
 *
 * IDENTIFICATION
13
 *	  $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.241 2007/04/30 03:23:48 tgl Exp $
14 15 16
 *
 *-------------------------------------------------------------------------
 */
M
-Wall'd  
Marc G. Fournier 已提交
17

18
#include "postgres.h"
M
-Wall'd  
Marc G. Fournier 已提交
19

20
#include <time.h>
T
Tom Lane 已提交
21
#include <unistd.h>
22

23
#include "access/multixact.h"
24
#include "access/subtrans.h"
25
#include "access/transam.h"
26
#include "access/twophase.h"
27
#include "access/xact.h"
28
#include "access/xlogutils.h"
29
#include "catalog/namespace.h"
30
#include "commands/async.h"
31
#include "commands/tablecmds.h"
32
#include "commands/trigger.h"
33
#include "executor/spi.h"
34
#include "libpq/be-fsstubs.h"
35
#include "miscadmin.h"
36
#include "pgstat.h"
37
#include "storage/fd.h"
38
#include "storage/lmgr.h"
39
#include "storage/procarray.h"
40
#include "storage/smgr.h"
41
#include "utils/combocid.h"
42
#include "utils/flatfiles.h"
43
#include "utils/guc.h"
B
Bruce Momjian 已提交
44
#include "utils/inval.h"
45
#include "utils/memutils.h"
46
#include "utils/relcache.h"
47

48

49 50 51 52 53 54 55 56 57 58 59 60 61
/*
 *	User-tweakable parameters
 */
int			DefaultXactIsoLevel = XACT_READ_COMMITTED;
int			XactIsoLevel;

bool		DefaultXactReadOnly = false;
bool		XactReadOnly;

int			CommitDelay = 0;	/* precommit delay in microseconds */
int			CommitSiblings = 5; /* # concurrent xacts needed to sleep */


62 63 64 65 66 67 68 69 70
/*
 *	transaction states - transaction state from server perspective
 */
typedef enum TransState
{
	TRANS_DEFAULT,
	TRANS_START,
	TRANS_INPROGRESS,
	TRANS_COMMIT,
71 72
	TRANS_ABORT,
	TRANS_PREPARE
73 74 75 76
} TransState;

/*
 *	transaction block states - transaction state of client queries
77 78 79
 *
 * Note: the subtransaction states are used only for non-topmost
 * transactions; the others appear only in the topmost transaction.
80 81 82
 */
typedef enum TBlockState
{
83
	/* not-in-transaction-block states */
84 85
	TBLOCK_DEFAULT,				/* idle */
	TBLOCK_STARTED,				/* running single-query transaction */
86 87

	/* transaction block states */
88 89 90 91 92 93
	TBLOCK_BEGIN,				/* starting transaction block */
	TBLOCK_INPROGRESS,			/* live transaction */
	TBLOCK_END,					/* COMMIT received */
	TBLOCK_ABORT,				/* failed xact, awaiting ROLLBACK */
	TBLOCK_ABORT_END,			/* failed xact, ROLLBACK received */
	TBLOCK_ABORT_PENDING,		/* live xact, ROLLBACK received */
94
	TBLOCK_PREPARE,				/* live xact, PREPARE received */
95

96
	/* subtransaction states */
97 98 99 100 101 102 103 104
	TBLOCK_SUBBEGIN,			/* starting a subtransaction */
	TBLOCK_SUBINPROGRESS,		/* live subtransaction */
	TBLOCK_SUBEND,				/* RELEASE received */
	TBLOCK_SUBABORT,			/* failed subxact, awaiting ROLLBACK */
	TBLOCK_SUBABORT_END,		/* failed subxact, ROLLBACK received */
	TBLOCK_SUBABORT_PENDING,	/* live subxact, ROLLBACK received */
	TBLOCK_SUBRESTART,			/* live subxact, ROLLBACK TO received */
	TBLOCK_SUBABORT_RESTART		/* failed subxact, ROLLBACK TO received */
105 106 107 108 109 110 111
} TBlockState;

/*
 *	transaction state structure
 */
typedef struct TransactionStateData
{
B
Bruce Momjian 已提交
112
	TransactionId transactionId;	/* my XID, or Invalid if none */
113
	SubTransactionId subTransactionId;	/* my subxact ID */
B
Bruce Momjian 已提交
114 115 116 117 118
	char	   *name;			/* savepoint name, if any */
	int			savepointLevel; /* savepoint level */
	TransState	state;			/* low-level state */
	TBlockState blockState;		/* high-level state */
	int			nestingLevel;	/* nest depth */
B
Bruce Momjian 已提交
119
	MemoryContext curTransactionContext;		/* my xact-lifetime context */
B
Bruce Momjian 已提交
120 121
	ResourceOwner curTransactionOwner;	/* my query resources */
	List	   *childXids;		/* subcommitted child XIDs */
122
	Oid			currentUser;	/* subxact start current_user */
B
Bruce Momjian 已提交
123 124
	bool		prevXactReadOnly;		/* entry-time xact r/o state */
	struct TransactionStateData *parent;		/* back link to parent */
125 126 127 128
} TransactionStateData;

typedef TransactionStateData *TransactionState;

129
/*
130 131 132
 * CurrentTransactionState always points to the current transaction state
 * block.  It will point to TopTransactionStateData when not in a
 * transaction at all, or when in a top-level transaction.
133
 */
134
static TransactionStateData TopTransactionStateData = {
135
	0,							/* transaction id */
136
	0,							/* subtransaction id */
137 138
	NULL,						/* savepoint name */
	0,							/* savepoint level */
139
	TRANS_DEFAULT,				/* transaction state */
140
	TBLOCK_DEFAULT,				/* transaction block state from the client
B
Bruce Momjian 已提交
141
								 * perspective */
142 143
	0,							/* nesting level */
	NULL,						/* cur transaction context */
144
	NULL,						/* cur transaction resource owner */
145 146
	NIL,						/* subcommitted child Xids */
	0,							/* entry-time current userid */
147
	false,						/* entry-time xact r/o state */
148
	NULL						/* link to parent state block */
149
};
150

151 152
static TransactionState CurrentTransactionState = &TopTransactionStateData;

153 154 155 156 157 158 159
/*
 * The subtransaction ID and command ID assignment counters are global
 * to a whole transaction, so we do not keep them in the state stack.
 */
static SubTransactionId currentSubTransactionId;
static CommandId currentCommandId;

160
/*
161 162
 * xactStartTimestamp is the value of transaction_timestamp().
 * stmtStartTimestamp is the value of statement_timestamp().
163
 * xactStopTimestamp is the time at which we log a commit or abort WAL record.
164 165
 * These do not change as we enter and exit subtransactions, so we don't
 * keep them inside the TransactionState stack.
166
 */
167
static TimestampTz xactStartTimestamp;
168
static TimestampTz stmtStartTimestamp;
169
static TimestampTz xactStopTimestamp;
170

171 172 173 174 175 176
/*
 * GID to be used for preparing the current transaction.  This is also
 * global to a whole transaction, so we don't keep it in the state stack.
 */
static char *prepareGID;

177 178 179 180 181 182
/*
 * Private context for transaction-abort work --- we reserve space for this
 * at startup to ensure that AbortTransaction and AbortSubTransaction can work
 * when we've run out of memory.
 */
static MemoryContext TransactionAbortContext = NULL;
183

184
/*
185
 * List of add-on start- and end-of-xact callbacks
186
 */
187
typedef struct XactCallbackItem
188
{
189 190
	struct XactCallbackItem *next;
	XactCallback callback;
191
	void	   *arg;
192
} XactCallbackItem;
193

194
static XactCallbackItem *Xact_callbacks = NULL;
195

196 197 198 199 200 201 202 203 204 205 206 207
/*
 * List of add-on start- and end-of-subxact callbacks
 */
typedef struct SubXactCallbackItem
{
	struct SubXactCallbackItem *next;
	SubXactCallback callback;
	void	   *arg;
} SubXactCallbackItem;

static SubXactCallbackItem *SubXact_callbacks = NULL;

208

209 210 211 212 213
/* local function prototypes */
static void AssignSubTransactionId(TransactionState s);
static void AbortTransaction(void);
static void AtAbort_Memory(void);
static void AtCleanup_Memory(void);
214
static void AtAbort_ResourceOwner(void);
215 216 217 218 219 220 221
static void AtCommit_LocalCache(void);
static void AtCommit_Memory(void);
static void AtStart_Cache(void);
static void AtStart_Memory(void);
static void AtStart_ResourceOwner(void);
static void CallXactCallbacks(XactEvent event);
static void CallSubXactCallbacks(SubXactEvent event,
B
Bruce Momjian 已提交
222 223
					 SubTransactionId mySubid,
					 SubTransactionId parentSubid);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
static void CleanupTransaction(void);
static void CommitTransaction(void);
static void RecordTransactionAbort(void);
static void StartTransaction(void);

static void RecordSubTransactionCommit(void);
static void StartSubTransaction(void);
static void CommitSubTransaction(void);
static void AbortSubTransaction(void);
static void CleanupSubTransaction(void);
static void PushTransaction(void);
static void PopTransaction(void);

static void AtSubAbort_Memory(void);
static void AtSubCleanup_Memory(void);
239
static void AtSubAbort_ResourceOwner(void);
240 241 242 243 244 245 246 247 248 249
static void AtSubCommit_Memory(void);
static void AtSubStart_Memory(void);
static void AtSubStart_ResourceOwner(void);

static void ShowTransactionState(const char *str);
static void ShowTransactionStateRec(TransactionState state);
static const char *BlockStateAsString(TBlockState blockState);
static const char *TransStateAsString(TransState state);


250
/* ----------------------------------------------------------------
251
 *	transaction state accessors
252 253
 * ----------------------------------------------------------------
 */
254

255
/*
256
 *	IsTransactionState
257
 *
258 259
 *	This returns true if we are currently running a query
 *	within an executing transaction.
260 261
 */
bool
262
IsTransactionState(void)
263
{
264 265 266 267
	TransactionState s = CurrentTransactionState;

	switch (s->state)
	{
268 269 270 271 272 273 274 275 276 277
		case TRANS_DEFAULT:
			return false;
		case TRANS_START:
			return true;
		case TRANS_INPROGRESS:
			return true;
		case TRANS_COMMIT:
			return true;
		case TRANS_ABORT:
			return true;
278 279
		case TRANS_PREPARE:
			return true;
280 281 282
	}

	/*
283
	 * Shouldn't get here, but lint is not happy without this...
284
	 */
285
	return false;
286 287
}

288
/*
289
 *	IsAbortedTransactionBlockState
290
 *
291 292
 *	This returns true if we are currently running a query
 *	within an aborted transaction block.
293 294
 */
bool
295
IsAbortedTransactionBlockState(void)
296
{
297 298
	TransactionState s = CurrentTransactionState;

B
Bruce Momjian 已提交
299
	if (s->blockState == TBLOCK_ABORT ||
300
		s->blockState == TBLOCK_SUBABORT)
301 302 303
		return true;

	return false;
304 305 306
}


307 308 309 310 311 312 313 314 315
/*
 *	GetTopTransactionId
 *
 * Get the ID of the main transaction, even if we are currently inside
 * a subtransaction.
 */
TransactionId
GetTopTransactionId(void)
{
316
	return TopTransactionStateData.transactionId;
317 318 319
}


320
/*
321
 *	GetCurrentTransactionId
322 323 324 325 326
 *
 * We do not assign XIDs to subtransactions until/unless this is called.
 * When we do assign an XID to a subtransaction, recursively make sure
 * its parent has one as well (this maintains the invariant that a child
 * transaction has an XID following its parent's).
327 328
 */
TransactionId
329
GetCurrentTransactionId(void)
330
{
331 332
	TransactionState s = CurrentTransactionState;

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	if (!TransactionIdIsValid(s->transactionId))
		AssignSubTransactionId(s);

	return s->transactionId;
}

static void
AssignSubTransactionId(TransactionState s)
{
	ResourceOwner currentOwner;

	Assert(s->parent != NULL);
	Assert(s->state == TRANS_INPROGRESS);
	if (!TransactionIdIsValid(s->parent->transactionId))
		AssignSubTransactionId(s->parent);

	/*
	 * Generate a new Xid and record it in PG_PROC and pg_subtrans.
	 *
B
Bruce Momjian 已提交
352 353 354 355
	 * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
	 * shared storage other than PG_PROC; because if there's no room for it in
	 * PG_PROC, the subtrans entry is needed to ensure that other backends see
	 * the Xid as "running".  See GetNewTransactionId.
356 357 358 359 360 361
	 */
	s->transactionId = GetNewTransactionId(true);

	SubTransSetParent(s->transactionId, s->parent->transactionId);

	/*
B
Bruce Momjian 已提交
362 363
	 * Acquire lock on the transaction XID.  (We assume this cannot block.) We
	 * have to be sure that the lock is assigned to the transaction's
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	 * ResourceOwner.
	 */
	currentOwner = CurrentResourceOwner;
	PG_TRY();
	{
		CurrentResourceOwner = s->curTransactionOwner;

		XactLockTableInsert(s->transactionId);
	}
	PG_CATCH();
	{
		/* Ensure CurrentResourceOwner is restored on error */
		CurrentResourceOwner = currentOwner;
		PG_RE_THROW();
	}
	PG_END_TRY();
	CurrentResourceOwner = currentOwner;
}


/*
 *	GetCurrentTransactionIdIfAny
 *
 * Unlike GetCurrentTransactionId, this will return InvalidTransactionId
 * if we are currently not in a transaction, or in a transaction or
 * subtransaction that has not yet assigned itself an XID.
 */
TransactionId
GetCurrentTransactionIdIfAny(void)
{
	TransactionState s = CurrentTransactionState;

	return s->transactionId;
}


/*
 *	GetCurrentSubTransactionId
 */
SubTransactionId
GetCurrentSubTransactionId(void)
{
	TransactionState s = CurrentTransactionState;

	return s->subTransactionId;
409 410 411
}


412
/*
413
 *	GetCurrentCommandId
414 415
 */
CommandId
416
GetCurrentCommandId(void)
417
{
418 419
	/* this is global to a transaction, not subtransaction-local */
	return currentCommandId;
420 421
}

422
/*
423
 *	GetCurrentTransactionStartTimestamp
424
 */
425 426
TimestampTz
GetCurrentTransactionStartTimestamp(void)
427
{
428
	return xactStartTimestamp;
429 430
}

431 432 433 434 435 436 437 438 439
/*
 *	GetCurrentStatementStartTimestamp
 */
TimestampTz
GetCurrentStatementStartTimestamp(void)
{
	return stmtStartTimestamp;
}

440 441 442 443 444 445 446 447 448 449 450 451 452 453
/*
 *	GetCurrentTransactionStopTimestamp
 *
 * We return current time if the transaction stop time hasn't been set
 * (which can happen if we decide we don't need to log an XLOG record).
 */
TimestampTz
GetCurrentTransactionStopTimestamp(void)
{
	if (xactStopTimestamp != 0)
		return xactStopTimestamp;
	return GetCurrentTimestamp();
}

454 455 456 457 458 459 460 461 462
/*
 *	SetCurrentStatementStartTimestamp
 */
void
SetCurrentStatementStartTimestamp(void)
{
	stmtStartTimestamp = GetCurrentTimestamp();
}

463 464 465 466 467 468 469 470 471
/*
 *	SetCurrentTransactionStopTimestamp
 */
static inline void
SetCurrentTransactionStopTimestamp(void)
{
	xactStopTimestamp = GetCurrentTimestamp();
}

472 473 474 475 476 477 478 479 480 481
/*
 *	GetCurrentTransactionNestLevel
 *
 * Note: this will return zero when not inside any transaction, one when
 * inside a top-level transaction, etc.
 */
int
GetCurrentTransactionNestLevel(void)
{
	TransactionState s = CurrentTransactionState;
482

483
	return s->nestingLevel;
484 485 486
}


487
/*
488
 *	TransactionIdIsCurrentTransactionId
489 490 491 492
 */
bool
TransactionIdIsCurrentTransactionId(TransactionId xid)
{
493
	TransactionState s;
494

495 496
	/*
	 * We always say that BootstrapTransactionId is "not my transaction ID"
B
Bruce Momjian 已提交
497
	 * even when it is (ie, during bootstrap).	Along with the fact that
498
	 * transam.c always treats BootstrapTransactionId as already committed,
B
Bruce Momjian 已提交
499 500 501 502
	 * this causes the tqual.c routines to see all tuples as committed, which
	 * is what we need during bootstrap.  (Bootstrap mode only inserts tuples,
	 * it never updates or deletes them, so all tuples can be presumed good
	 * immediately.)
503 504 505 506
	 *
	 * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
	 * not my transaction ID, so we can just return "false" immediately for
	 * any non-normal XID.
507
	 */
508
	if (!TransactionIdIsNormal(xid))
509 510
		return false;

511
	/*
B
Bruce Momjian 已提交
512 513 514 515 516
	 * We will return true for the Xid of the current subtransaction, any of
	 * its subcommitted children, any of its parents, or any of their
	 * previously subcommitted children.  However, a transaction being aborted
	 * is no longer "current", even though it may still have an entry on the
	 * state stack.
517
	 */
518
	for (s = CurrentTransactionState; s != NULL; s = s->parent)
519
	{
B
Bruce Momjian 已提交
520
		ListCell   *cell;
521

522 523
		if (s->state == TRANS_ABORT)
			continue;
524 525 526
		if (!TransactionIdIsValid(s->transactionId))
			continue;			/* it can't have any child XIDs either */
		if (TransactionIdEquals(xid, s->transactionId))
527 528 529
			return true;
		foreach(cell, s->childXids)
		{
530
			if (TransactionIdEquals(xid, lfirst_xid(cell)))
531 532 533
				return true;
		}
	}
534

535
	return false;
536 537 538
}


539
/*
540
 *	CommandCounterIncrement
541 542
 */
void
543
CommandCounterIncrement(void)
544
{
545
	currentCommandId += 1;
B
Bruce Momjian 已提交
546
	if (currentCommandId == FirstCommandId)		/* check for overflow */
547 548
	{
		currentCommandId -= 1;
549 550
		ereport(ERROR,
				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
B
Bruce Momjian 已提交
551
		  errmsg("cannot have more than 2^32-1 commands in a transaction")));
552
	}
553

554
	/* Propagate new command ID into static snapshots, if set */
555
	if (SerializableSnapshot)
556
		SerializableSnapshot->curcid = currentCommandId;
557
	if (LatestSnapshot)
558
		LatestSnapshot->curcid = currentCommandId;
559

H
 
Hiroshi Inoue 已提交
560
	/*
561
	 * make cache changes visible to me.
H
 
Hiroshi Inoue 已提交
562 563
	 */
	AtCommit_LocalCache();
564
	AtStart_Cache();
565 566
}

567

568
/* ----------------------------------------------------------------
569
 *						StartTransaction stuff
570 571 572
 * ----------------------------------------------------------------
 */

573
/*
574
 *	AtStart_Cache
575
 */
576
static void
577
AtStart_Cache(void)
578
{
579
	AcceptInvalidationMessages();
580 581
}

582
/*
583
 *	AtStart_Memory
584
 */
585
static void
586
AtStart_Memory(void)
587
{
588 589
	TransactionState s = CurrentTransactionState;

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	/*
	 * If this is the first time through, create a private context for
	 * AbortTransaction to work in.  By reserving some space now, we can
	 * insulate AbortTransaction from out-of-memory scenarios.  Like
	 * ErrorContext, we set it up with slow growth rate and a nonzero
	 * minimum size, so that space will be reserved immediately.
	 */
	if (TransactionAbortContext == NULL)
		TransactionAbortContext =
			AllocSetContextCreate(TopMemoryContext,
								  "TransactionAbortContext",
								  32 * 1024,
								  32 * 1024,
								  32 * 1024);

605
	/*
606
	 * We shouldn't have a transaction context already.
607 608
	 */
	Assert(TopTransactionContext == NULL);
609

610
	/*
611
	 * Create a toplevel context for the transaction.
612
	 */
613 614 615 616 617 618
	TopTransactionContext =
		AllocSetContextCreate(TopMemoryContext,
							  "TopTransactionContext",
							  ALLOCSET_DEFAULT_MINSIZE,
							  ALLOCSET_DEFAULT_INITSIZE,
							  ALLOCSET_DEFAULT_MAXSIZE);
619

620 621 622 623 624 625 626 627 628
	/*
	 * In a top-level transaction, CurTransactionContext is the same as
	 * TopTransactionContext.
	 */
	CurTransactionContext = TopTransactionContext;
	s->curTransactionContext = CurTransactionContext;

	/* Make the CurTransactionContext active. */
	MemoryContextSwitchTo(CurTransactionContext);
629 630
}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
/*
 *	AtStart_ResourceOwner
 */
static void
AtStart_ResourceOwner(void)
{
	TransactionState s = CurrentTransactionState;

	/*
	 * We shouldn't have a transaction resource owner already.
	 */
	Assert(TopTransactionResourceOwner == NULL);

	/*
	 * Create a toplevel resource owner for the transaction.
	 */
	s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");

	TopTransactionResourceOwner = s->curTransactionOwner;
	CurTransactionResourceOwner = s->curTransactionOwner;
	CurrentResourceOwner = s->curTransactionOwner;
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
/* ----------------------------------------------------------------
 *						StartSubTransaction stuff
 * ----------------------------------------------------------------
 */

/*
 * AtSubStart_Memory
 */
static void
AtSubStart_Memory(void)
{
	TransactionState s = CurrentTransactionState;

	Assert(CurTransactionContext != NULL);

	/*
B
Bruce Momjian 已提交
670 671 672
	 * Create a CurTransactionContext, which will be used to hold data that
	 * survives subtransaction commit but disappears on subtransaction abort.
	 * We make it a child of the immediate parent's CurTransactionContext.
673 674 675
	 */
	CurTransactionContext = AllocSetContextCreate(CurTransactionContext,
												  "CurTransactionContext",
B
Bruce Momjian 已提交
676 677 678
												  ALLOCSET_DEFAULT_MINSIZE,
												  ALLOCSET_DEFAULT_INITSIZE,
												  ALLOCSET_DEFAULT_MAXSIZE);
679 680 681 682 683
	s->curTransactionContext = CurTransactionContext;

	/* Make the CurTransactionContext active. */
	MemoryContextSwitchTo(CurTransactionContext);
}
684

685 686 687 688 689 690 691 692 693 694 695
/*
 * AtSubStart_ResourceOwner
 */
static void
AtSubStart_ResourceOwner(void)
{
	TransactionState s = CurrentTransactionState;

	Assert(s->parent != NULL);

	/*
B
Bruce Momjian 已提交
696 697
	 * Create a resource owner for the subtransaction.	We make it a child of
	 * the immediate parent's resource owner.
698 699 700 701 702 703 704 705 706
	 */
	s->curTransactionOwner =
		ResourceOwnerCreate(s->parent->curTransactionOwner,
							"SubTransaction");

	CurTransactionResourceOwner = s->curTransactionOwner;
	CurrentResourceOwner = s->curTransactionOwner;
}

707
/* ----------------------------------------------------------------
708
 *						CommitTransaction stuff
709 710 711
 * ----------------------------------------------------------------
 */

712
/*
713
 *	RecordTransactionCommit
714
 */
V
WAL  
Vadim B. Mikheev 已提交
715
void
716
RecordTransactionCommit(void)
717
{
718
	int			nrels;
719
	RelFileNode *rels;
720 721 722 723
	int			nchildren;
	TransactionId *children;

	/* Get data needed for commit record */
724
	nrels = smgrGetPendingDeletes(true, &rels);
725
	nchildren = xactGetCommittedChildren(&children);
726

727
	/*
B
Bruce Momjian 已提交
728 729 730 731
	 * If we made neither any XLOG entries nor any temp-rel updates, and have
	 * no files to be deleted, we can omit recording the transaction commit at
	 * all.  (This test includes the effects of subtransactions, so the
	 * presence of committed subxacts need not alone force a write.)
732
	 */
733
	if (MyXactMadeXLogEntry || MyXactMadeTempRelUpdate || nrels > 0)
V
Vadim B. Mikheev 已提交
734
	{
735
		TransactionId xid = GetCurrentTransactionId();
736
		bool		madeTCentries;
B
Bruce Momjian 已提交
737
		XLogRecPtr	recptr;
V
Vadim B. Mikheev 已提交
738

739
		/* Tell bufmgr and smgr to prepare for commit */
V
WAL  
Vadim B. Mikheev 已提交
740 741
		BufmgrCommit();

742
		START_CRIT_SECTION();
B
Bruce Momjian 已提交
743

744
		/*
B
Bruce Momjian 已提交
745 746
		 * We only need to log the commit in XLOG if the transaction made any
		 * transaction-controlled XLOG entries or will delete files.
747
		 */
748
		madeTCentries = (MyLastRecPtr.xrecoff != 0);
749
		if (madeTCentries || nrels > 0)
750
		{
751 752
			XLogRecData rdata[3];
			int			lastrdata = 0;
753 754
			xl_xact_commit xlrec;

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
			/*
			 * Mark ourselves as within our "commit critical section".  This
			 * forces any concurrent checkpoint to wait until we've updated
			 * pg_clog.  Without this, it is possible for the checkpoint to
			 * set REDO after the XLOG record but fail to flush the pg_clog
			 * update to disk, leading to loss of the transaction commit if
			 * the system crashes a little later.
			 *
			 * Note: we could, but don't bother to, set this flag in
			 * RecordTransactionAbort.  That's because loss of a transaction
			 * abort is noncritical; the presumption would be that it aborted,
			 * anyway.
			 *
			 * It's safe to change the inCommit flag of our own backend
			 * without holding the ProcArrayLock, since we're the only one
			 * modifying it.  This makes checkpoint's determination of which
			 * xacts are inCommit a bit fuzzy, but it doesn't matter.
			 */
			MyProc->inCommit = true;

775 776
			SetCurrentTransactionStopTimestamp();
			xlrec.xtime = timestamptz_to_time_t(xactStopTimestamp);
777 778
			xlrec.nrels = nrels;
			xlrec.nsubxacts = nchildren;
779 780
			rdata[0].data = (char *) (&xlrec);
			rdata[0].len = MinSizeOfXactCommit;
781
			rdata[0].buffer = InvalidBuffer;
782
			/* dump rels to delete */
783 784 785
			if (nrels > 0)
			{
				rdata[0].next = &(rdata[1]);
786
				rdata[1].data = (char *) rels;
787
				rdata[1].len = nrels * sizeof(RelFileNode);
788
				rdata[1].buffer = InvalidBuffer;
789
				lastrdata = 1;
790
			}
791 792 793 794 795 796
			/* dump committed child Xids */
			if (nchildren > 0)
			{
				rdata[lastrdata].next = &(rdata[2]);
				rdata[2].data = (char *) children;
				rdata[2].len = nchildren * sizeof(TransactionId);
797
				rdata[2].buffer = InvalidBuffer;
798 799 800
				lastrdata = 2;
			}
			rdata[lastrdata].next = NULL;
801

802
			recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT, rdata);
803 804 805 806 807 808
		}
		else
		{
			/* Just flush through last record written by me */
			recptr = ProcLastRecEnd;
		}
V
Vadim B. Mikheev 已提交
809

B
Bruce Momjian 已提交
810
		/*
B
Bruce Momjian 已提交
811 812 813 814 815
		 * We must flush our XLOG entries to disk if we made any XLOG entries,
		 * whether in or out of transaction control.  For example, if we
		 * reported a nextval() result to the client, this ensures that any
		 * XLOG record generated by nextval will hit the disk before we report
		 * the transaction committed.
816
		 *
817 818
		 * Note: if we generated a commit record above, MyXactMadeXLogEntry
		 * will certainly be set now.
V
Vadim B. Mikheev 已提交
819
		 */
820
		if (MyXactMadeXLogEntry)
821
		{
822 823
			/*
			 * Sleep before flush! So we can flush more than one commit
B
Bruce Momjian 已提交
824 825 826 827
			 * records per single fsync.  (The idea is some other backend may
			 * do the XLogFlush while we're sleeping.  This needs work still,
			 * because on most Unixen, the minimum select() delay is 10msec or
			 * more, which is way too long.)
828
			 *
829 830
			 * We do not sleep if enableFsync is not turned on, nor if there
			 * are fewer than CommitSiblings other backends with active
831 832 833 834
			 * transactions.
			 */
			if (CommitDelay > 0 && enableFsync &&
				CountActiveBackends() >= CommitSiblings)
835
				pg_usleep(CommitDelay);
836

837 838
			XLogFlush(recptr);
		}
T
Tom Lane 已提交
839

840
		/*
B
Bruce Momjian 已提交
841 842 843 844 845 846 847
		 * We must mark the transaction committed in clog if its XID appears
		 * either in permanent rels or in local temporary rels. We test this
		 * by seeing if we made transaction-controlled entries *OR* local-rel
		 * tuple updates.  Note that if we made only the latter, we have not
		 * emitted an XLOG record for our commit, and so in the event of a
		 * crash the clog update might be lost.  This is okay because no one
		 * else will ever care whether we committed.
848
		 */
849 850
		if (madeTCentries || MyXactMadeTempRelUpdate)
		{
851
			TransactionIdCommit(xid);
852 853 854
			/* to avoid race conditions, the parent must commit first */
			TransactionIdCommitTree(nchildren, children);
		}
V
Vadim B. Mikheev 已提交
855

856 857
		/* Checkpoint can proceed now */
		MyProc->inCommit = false;
858

859
		END_CRIT_SECTION();
V
Vadim B. Mikheev 已提交
860
	}
861

862 863 864
	/* Break the chain of back-links in the XLOG records I output */
	MyLastRecPtr.xrecoff = 0;
	MyXactMadeXLogEntry = false;
865
	MyXactMadeTempRelUpdate = false;
866

867
	/* And clean up local data */
868 869
	if (rels)
		pfree(rels);
870 871
	if (children)
		pfree(children);
872 873 874
}


875
/*
876
 *	AtCommit_LocalCache
H
 
Hiroshi Inoue 已提交
877 878
 */
static void
879
AtCommit_LocalCache(void)
H
 
Hiroshi Inoue 已提交
880
{
881
	/*
H
 
Hiroshi Inoue 已提交
882 883
	 * Make catalog changes visible to me for the next command.
	 */
884
	CommandEndInvalidationMessages();
H
 
Hiroshi Inoue 已提交
885 886
}

887
/*
888
 *	AtCommit_Memory
889
 */
890
static void
891
AtCommit_Memory(void)
892
{
893
	/*
B
Bruce Momjian 已提交
894 895
	 * Now that we're "out" of a transaction, have the system allocate things
	 * in the top memory context instead of per-transaction contexts.
896
	 */
897
	MemoryContextSwitchTo(TopMemoryContext);
898

899 900
	/*
	 * Release all transaction-local memory.
901
	 */
902
	Assert(TopTransactionContext != NULL);
903 904
	MemoryContextDelete(TopTransactionContext);
	TopTransactionContext = NULL;
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
	CurTransactionContext = NULL;
	CurrentTransactionState->curTransactionContext = NULL;
}

/* ----------------------------------------------------------------
 *						CommitSubTransaction stuff
 * ----------------------------------------------------------------
 */

/*
 * AtSubCommit_Memory
 */
static void
AtSubCommit_Memory(void)
{
	TransactionState s = CurrentTransactionState;

	Assert(s->parent != NULL);

	/* Return to parent transaction level's memory context. */
	CurTransactionContext = s->parent->curTransactionContext;
	MemoryContextSwitchTo(CurTransactionContext);
927 928 929

	/*
	 * Ordinarily we cannot throw away the child's CurTransactionContext,
B
Bruce Momjian 已提交
930 931 932
	 * since the data it contains will be needed at upper commit.  However, if
	 * there isn't actually anything in it, we can throw it away.  This avoids
	 * a small memory leak in the common case of "trivial" subxacts.
933 934 935 936 937 938
	 */
	if (MemoryContextIsEmpty(s->curTransactionContext))
	{
		MemoryContextDelete(s->curTransactionContext);
		s->curTransactionContext = NULL;
	}
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
}

/*
 * AtSubCommit_childXids
 *
 * Pass my own XID and my child XIDs up to my parent as committed children.
 */
static void
AtSubCommit_childXids(void)
{
	TransactionState s = CurrentTransactionState;
	MemoryContext old_cxt;

	Assert(s->parent != NULL);

954 955 956 957 958 959
	/*
	 * We keep the child-XID lists in TopTransactionContext; this avoids
	 * setting up child-transaction contexts for what might be just a few
	 * bytes of grandchild XIDs.
	 */
	old_cxt = MemoryContextSwitchTo(TopTransactionContext);
960

961
	s->parent->childXids = lappend_xid(s->parent->childXids,
962
									   s->transactionId);
963

964 965 966 967
	if (s->childXids != NIL)
	{
		s->parent->childXids = list_concat(s->parent->childXids,
										   s->childXids);
B
Bruce Momjian 已提交
968

969
		/*
B
Bruce Momjian 已提交
970 971
		 * list_concat doesn't free the list header for the second list; do so
		 * here to avoid memory leakage (kluge)
972 973 974 975
		 */
		pfree(s->childXids);
		s->childXids = NIL;
	}
976 977 978 979 980 981 982 983 984 985 986

	MemoryContextSwitchTo(old_cxt);
}

/*
 * RecordSubTransactionCommit
 */
static void
RecordSubTransactionCommit(void)
{
	/*
B
Bruce Momjian 已提交
987 988
	 * We do not log the subcommit in XLOG; it doesn't matter until the
	 * top-level transaction commits.
989
	 *
B
Bruce Momjian 已提交
990 991 992 993 994 995 996 997
	 * We must mark the subtransaction subcommitted in clog if its XID appears
	 * either in permanent rels or in local temporary rels. We test this by
	 * seeing if we made transaction-controlled entries *OR* local-rel tuple
	 * updates.  (The test here actually covers the entire transaction tree so
	 * far, so it may mark subtransactions that don't really need it, but it's
	 * probably not worth being tenser. Note that if a prior subtransaction
	 * dirtied these variables, then RecordTransactionCommit will have to do
	 * the full pushup anyway...)
998 999 1000
	 */
	if (MyLastRecPtr.xrecoff != 0 || MyXactMadeTempRelUpdate)
	{
B
Bruce Momjian 已提交
1001
		TransactionId xid = GetCurrentTransactionId();
1002 1003 1004 1005 1006 1007 1008 1009 1010

		/* XXX does this really need to be a critical section? */
		START_CRIT_SECTION();

		/* Record subtransaction subcommit */
		TransactionIdSubCommit(xid);

		END_CRIT_SECTION();
	}
1011 1012 1013
}

/* ----------------------------------------------------------------
1014
 *						AbortTransaction stuff
1015 1016 1017
 * ----------------------------------------------------------------
 */

1018
/*
1019
 *	RecordTransactionAbort
1020
 */
1021
static void
1022
RecordTransactionAbort(void)
1023
{
1024
	int			nrels;
1025
	RelFileNode *rels;
B
Bruce Momjian 已提交
1026 1027
	int			nchildren;
	TransactionId *children;
1028 1029

	/* Get data needed for abort record */
1030
	nrels = smgrGetPendingDeletes(false, &rels);
1031
	nchildren = xactGetCommittedChildren(&children);
1032

T
Tom Lane 已提交
1033
	/*
1034
	 * If we made neither any transaction-controlled XLOG entries nor any
B
Bruce Momjian 已提交
1035 1036 1037
	 * temp-rel updates, and are not going to delete any files, we can omit
	 * recording the transaction abort at all.	No one will ever care that it
	 * aborted.  (These tests cover our whole transaction tree.)
T
Tom Lane 已提交
1038
	 */
1039
	if (MyLastRecPtr.xrecoff != 0 || MyXactMadeTempRelUpdate || nrels > 0)
1040
	{
B
Bruce Momjian 已提交
1041
		TransactionId xid = GetCurrentTransactionId();
1042

1043 1044 1045 1046 1047
		/*
		 * Catch the scenario where we aborted partway through
		 * RecordTransactionCommit ...
		 */
		if (TransactionIdDidCommit(xid))
1048
			elog(PANIC, "cannot abort transaction %u, it was already committed", xid);
1049

1050
		START_CRIT_SECTION();
T
Tom Lane 已提交
1051

1052
		/*
B
Bruce Momjian 已提交
1053 1054 1055 1056 1057
		 * We only need to log the abort in XLOG if the transaction made any
		 * transaction-controlled XLOG entries or will delete files. (If it
		 * made no transaction-controlled XLOG entries, its XID appears
		 * nowhere in permanent storage, so no one else will ever care if it
		 * committed.)
1058
		 *
1059 1060 1061 1062
		 * We do not flush XLOG to disk unless deleting files, since the
		 * default assumption after a crash would be that we aborted, anyway.
		 * For the same reason, we don't need to worry about interlocking
		 * against checkpoint start.
1063
		 */
1064
		if (MyLastRecPtr.xrecoff != 0 || nrels > 0)
1065
		{
1066 1067
			XLogRecData rdata[3];
			int			lastrdata = 0;
1068 1069 1070
			xl_xact_abort xlrec;
			XLogRecPtr	recptr;

1071 1072
			SetCurrentTransactionStopTimestamp();
			xlrec.xtime = timestamptz_to_time_t(xactStopTimestamp);
1073 1074
			xlrec.nrels = nrels;
			xlrec.nsubxacts = nchildren;
1075 1076
			rdata[0].data = (char *) (&xlrec);
			rdata[0].len = MinSizeOfXactAbort;
1077
			rdata[0].buffer = InvalidBuffer;
1078
			/* dump rels to delete */
1079 1080 1081
			if (nrels > 0)
			{
				rdata[0].next = &(rdata[1]);
1082
				rdata[1].data = (char *) rels;
1083
				rdata[1].len = nrels * sizeof(RelFileNode);
1084
				rdata[1].buffer = InvalidBuffer;
1085
				lastrdata = 1;
1086
			}
1087 1088 1089 1090 1091 1092
			/* dump committed child Xids */
			if (nchildren > 0)
			{
				rdata[lastrdata].next = &(rdata[2]);
				rdata[2].data = (char *) children;
				rdata[2].len = nchildren * sizeof(TransactionId);
1093
				rdata[2].buffer = InvalidBuffer;
1094 1095 1096
				lastrdata = 2;
			}
			rdata[lastrdata].next = NULL;
1097

1098 1099
			recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT, rdata);

1100
			/* Must flush if we are deleting files... */
1101 1102
			if (nrels > 0)
				XLogFlush(recptr);
1103
		}
V
Vadim B. Mikheev 已提交
1104

1105 1106 1107
		/*
		 * Mark the transaction aborted in clog.  This is not absolutely
		 * necessary but we may as well do it while we are here.
1108
		 *
1109 1110
		 * The ordering here isn't critical but it seems best to mark the
		 * parent first.  This assures an atomic transition of all the
1111 1112
		 * subtransactions to aborted state from the point of view of
		 * concurrent TransactionIdDidAbort calls.
1113
		 */
V
Vadim B. Mikheev 已提交
1114
		TransactionIdAbort(xid);
1115
		TransactionIdAbortTree(nchildren, children);
T
Tom Lane 已提交
1116

1117
		END_CRIT_SECTION();
1118 1119
	}

T
Tom Lane 已提交
1120 1121
	/* Break the chain of back-links in the XLOG records I output */
	MyLastRecPtr.xrecoff = 0;
1122
	MyXactMadeXLogEntry = false;
1123
	MyXactMadeTempRelUpdate = false;
1124

1125
	/* And clean up local data */
1126 1127
	if (rels)
		pfree(rels);
1128 1129
	if (children)
		pfree(children);
1130 1131
}

1132
/*
1133
 *	AtAbort_Memory
1134
 */
1135
static void
1136
AtAbort_Memory(void)
1137
{
1138
	/*
1139 1140 1141 1142 1143 1144
	 * Switch into TransactionAbortContext, which should have some free
	 * space even if nothing else does.  We'll work in this context until
	 * we've finished cleaning up.
	 *
	 * It is barely possible to get here when we've not been able to create
	 * TransactionAbortContext yet; if so use TopMemoryContext.
1145
	 */
1146 1147
	if (TransactionAbortContext != NULL)
		MemoryContextSwitchTo(TransactionAbortContext);
1148 1149
	else
		MemoryContextSwitchTo(TopMemoryContext);
1150 1151
}

1152 1153 1154 1155 1156 1157
/*
 * AtSubAbort_Memory
 */
static void
AtSubAbort_Memory(void)
{
1158
	Assert(TransactionAbortContext != NULL);
1159

1160
	MemoryContextSwitchTo(TransactionAbortContext);
1161 1162
}

1163 1164 1165 1166 1167 1168 1169 1170

/*
 *	AtAbort_ResourceOwner
 */
static void
AtAbort_ResourceOwner(void)
{
	/*
B
Bruce Momjian 已提交
1171 1172
	 * Make sure we have a valid ResourceOwner, if possible (else it will be
	 * NULL, which is OK)
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
	 */
	CurrentResourceOwner = TopTransactionResourceOwner;
}

/*
 * AtSubAbort_ResourceOwner
 */
static void
AtSubAbort_ResourceOwner(void)
{
	TransactionState s = CurrentTransactionState;

	/* Make sure we have a valid ResourceOwner */
	CurrentResourceOwner = s->curTransactionOwner;
}


1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
/*
 * AtSubAbort_childXids
 */
static void
AtSubAbort_childXids(void)
{
	TransactionState s = CurrentTransactionState;

	/*
	 * We keep the child-XID lists in TopTransactionContext (see
B
Bruce Momjian 已提交
1200
	 * AtSubCommit_childXids).	This means we'd better free the list
1201 1202 1203 1204 1205 1206
	 * explicitly at abort to avoid leakage.
	 */
	list_free(s->childXids);
	s->childXids = NIL;
}

1207 1208 1209 1210 1211 1212 1213
/*
 * RecordSubTransactionAbort
 */
static void
RecordSubTransactionAbort(void)
{
	int			nrels;
1214
	RelFileNode *rels;
B
Bruce Momjian 已提交
1215 1216 1217
	TransactionId xid = GetCurrentTransactionId();
	int			nchildren;
	TransactionId *children;
1218 1219

	/* Get data needed for abort record */
1220
	nrels = smgrGetPendingDeletes(false, &rels);
1221
	nchildren = xactGetCommittedChildren(&children);
1222 1223 1224

	/*
	 * If we made neither any transaction-controlled XLOG entries nor any
B
Bruce Momjian 已提交
1225 1226 1227 1228 1229
	 * temp-rel updates, and are not going to delete any files, we can omit
	 * recording the transaction abort at all.	No one will ever care that it
	 * aborted.  (These tests cover our whole transaction tree, and therefore
	 * may mark subxacts that don't really need it, but it's probably not
	 * worth being tenser.)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
	 *
	 * In this case we needn't worry about marking subcommitted children as
	 * aborted, because they didn't mark themselves as subcommitted in the
	 * first place; see the optimization in RecordSubTransactionCommit.
	 */
	if (MyLastRecPtr.xrecoff != 0 || MyXactMadeTempRelUpdate || nrels > 0)
	{
		START_CRIT_SECTION();

		/*
B
Bruce Momjian 已提交
1240 1241
		 * We only need to log the abort in XLOG if the transaction made any
		 * transaction-controlled XLOG entries or will delete files.
1242 1243 1244 1245
		 */
		if (MyLastRecPtr.xrecoff != 0 || nrels > 0)
		{
			XLogRecData rdata[3];
B
Bruce Momjian 已提交
1246
			int			lastrdata = 0;
1247
			xl_xact_abort xlrec;
B
Bruce Momjian 已提交
1248
			XLogRecPtr	recptr;
1249 1250 1251 1252 1253 1254

			xlrec.xtime = time(NULL);
			xlrec.nrels = nrels;
			xlrec.nsubxacts = nchildren;
			rdata[0].data = (char *) (&xlrec);
			rdata[0].len = MinSizeOfXactAbort;
1255
			rdata[0].buffer = InvalidBuffer;
1256 1257 1258 1259
			/* dump rels to delete */
			if (nrels > 0)
			{
				rdata[0].next = &(rdata[1]);
1260
				rdata[1].data = (char *) rels;
1261
				rdata[1].len = nrels * sizeof(RelFileNode);
1262
				rdata[1].buffer = InvalidBuffer;
1263 1264 1265 1266 1267 1268 1269 1270
				lastrdata = 1;
			}
			/* dump committed child Xids */
			if (nchildren > 0)
			{
				rdata[lastrdata].next = &(rdata[2]);
				rdata[2].data = (char *) children;
				rdata[2].len = nchildren * sizeof(TransactionId);
1271
				rdata[2].buffer = InvalidBuffer;
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
				lastrdata = 2;
			}
			rdata[lastrdata].next = NULL;

			recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT, rdata);

			/* Must flush if we are deleting files... */
			if (nrels > 0)
				XLogFlush(recptr);
		}

		/*
		 * Mark the transaction aborted in clog.  This is not absolutely
1285 1286
		 * necessary but XactLockTableWait makes use of it to avoid waiting
		 * for already-aborted subtransactions.
1287 1288
		 */
		TransactionIdAbort(xid);
1289
		TransactionIdAbortTree(nchildren, children);
1290 1291 1292 1293

		END_CRIT_SECTION();
	}

1294
	/*
B
Bruce Momjian 已提交
1295 1296 1297 1298
	 * We can immediately remove failed XIDs from PGPROC's cache of running
	 * child XIDs. It's easiest to do it here while we have the child XID
	 * array at hand, even though in the main-transaction case the equivalent
	 * work happens just after return from RecordTransactionAbort.
1299 1300 1301
	 */
	XidCacheRemoveRunningXids(xid, nchildren, children);

1302
	/* And clean up local data */
1303 1304
	if (rels)
		pfree(rels);
1305 1306 1307
	if (children)
		pfree(children);
}
1308 1309 1310 1311 1312

/* ----------------------------------------------------------------
 *						CleanupTransaction stuff
 * ----------------------------------------------------------------
 */
1313

1314
/*
1315
 *	AtCleanup_Memory
1316 1317
 */
static void
1318
AtCleanup_Memory(void)
1319
{
1320 1321
	Assert(CurrentTransactionState->parent == NULL);

1322
	/*
B
Bruce Momjian 已提交
1323 1324
	 * Now that we're "out" of a transaction, have the system allocate things
	 * in the top memory context instead of per-transaction contexts.
1325 1326
	 */
	MemoryContextSwitchTo(TopMemoryContext);
1327

1328 1329 1330 1331 1332
	/*
	 * Clear the special abort context for next time.
	 */
	if (TransactionAbortContext != NULL)
		MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1333

1334 1335
	/*
	 * Release all transaction-local memory.
1336
	 */
1337 1338
	if (TopTransactionContext != NULL)
		MemoryContextDelete(TopTransactionContext);
1339
	TopTransactionContext = NULL;
1340 1341
	CurTransactionContext = NULL;
	CurrentTransactionState->curTransactionContext = NULL;
1342 1343
}

1344

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
/* ----------------------------------------------------------------
 *						CleanupSubTransaction stuff
 * ----------------------------------------------------------------
 */

/*
 * AtSubCleanup_Memory
 */
static void
AtSubCleanup_Memory(void)
{
	TransactionState s = CurrentTransactionState;

	Assert(s->parent != NULL);

	/* Make sure we're not in an about-to-be-deleted context */
	MemoryContextSwitchTo(s->parent->curTransactionContext);
	CurTransactionContext = s->parent->curTransactionContext;

1364 1365 1366 1367 1368 1369
	/*
	 * Clear the special abort context for next time.
	 */
	if (TransactionAbortContext != NULL)
		MemoryContextResetAndDeleteChildren(TransactionAbortContext);

1370
	/*
B
Bruce Momjian 已提交
1371 1372 1373
	 * Delete the subxact local memory contexts. Its CurTransactionContext can
	 * go too (note this also kills CurTransactionContexts from any children
	 * of the subxact).
1374
	 */
1375 1376 1377
	if (s->curTransactionContext)
		MemoryContextDelete(s->curTransactionContext);
	s->curTransactionContext = NULL;
1378 1379
}

1380
/* ----------------------------------------------------------------
1381
 *						interface routines
1382 1383 1384
 * ----------------------------------------------------------------
 */

1385
/*
1386
 *	StartTransaction
1387
 */
1388
static void
1389
StartTransaction(void)
1390
{
1391 1392 1393 1394 1395 1396 1397
	TransactionState s;

	/*
	 * Let's just make sure the state stack is empty
	 */
	s = &TopTransactionStateData;
	CurrentTransactionState = s;
1398

1399
	/*
1400
	 * check the current transaction state
1401
	 */
1402
	if (s->state != TRANS_DEFAULT)
1403 1404
		elog(WARNING, "StartTransaction while in %s state",
			 TransStateAsString(s->state));
1405

1406 1407 1408
	/*
	 * set the current transaction state information appropriately during
	 * start processing
1409 1410
	 */
	s->state = TRANS_START;
B
Bruce Momjian 已提交
1411
	s->transactionId = InvalidTransactionId;	/* until assigned */
1412

1413
	/*
B
Bruce Momjian 已提交
1414
	 * Make sure we've freed any old snapshot, and reset xact state variables
1415 1416 1417 1418 1419
	 */
	FreeXactSnapshot();
	XactIsoLevel = DefaultXactIsoLevel;
	XactReadOnly = DefaultXactReadOnly;

1420 1421 1422 1423 1424 1425 1426
	/*
	 * reinitialize within-transaction counters
	 */
	s->subTransactionId = TopSubTransactionId;
	currentSubTransactionId = TopSubTransactionId;
	currentCommandId = FirstCommandId;

1427 1428 1429 1430 1431 1432
	/*
	 * must initialize resource-management stuff first
	 */
	AtStart_Memory();
	AtStart_ResourceOwner();

1433 1434
	/*
	 * generate a new transaction id
1435
	 */
1436
	s->transactionId = GetNewTransactionId(false);
1437

1438
	XactLockTableInsert(s->transactionId);
V
Vadim B. Mikheev 已提交
1439

B
Bruce Momjian 已提交
1440
	PG_TRACE1(transaction__start, s->transactionId);
1441

1442
	/*
B
Bruce Momjian 已提交
1443 1444
	 * set transaction_timestamp() (a/k/a now()).  We want this to be the same
	 * as the first command's statement_timestamp(), so don't do a fresh
1445 1446
	 * GetCurrentTimestamp() call (which'd be expensive anyway).  Also,
	 * mark xactStopTimestamp as unset.
1447
	 */
1448
	xactStartTimestamp = stmtStartTimestamp;
1449
	xactStopTimestamp = 0;
1450
	pgstat_report_txn_timestamp(xactStartTimestamp);
1451

1452 1453
	/*
	 * initialize current transaction state fields
1454
	 */
1455 1456 1457 1458
	s->nestingLevel = 1;
	s->childXids = NIL;

	/*
B
Bruce Momjian 已提交
1459 1460 1461 1462
	 * You might expect to see "s->currentUser = GetUserId();" here, but you
	 * won't because it doesn't work during startup; the userid isn't set yet
	 * during a backend's first transaction start.  We only use the
	 * currentUser field in sub-transaction state structs.
1463 1464
	 *
	 * prevXactReadOnly is also valid only in sub-transactions.
1465
	 */
1466

1467
	/*
1468
	 * initialize other subsystems for new transaction
1469
	 */
1470
	AtStart_Inval();
1471
	AtStart_Cache();
1472
	AfterTriggerBeginXact();
1473

1474 1475 1476
	/*
	 * done with start processing, set current transaction state to "in
	 * progress"
1477 1478 1479
	 */
	s->state = TRANS_INPROGRESS;

1480
	ShowTransactionState("StartTransaction");
1481 1482
}

1483

1484
/*
1485
 *	CommitTransaction
1486 1487
 *
 * NB: if you change this routine, better look at PrepareTransaction too!
1488
 */
1489
static void
1490
CommitTransaction(void)
1491
{
1492
	TransactionState s = CurrentTransactionState;
1493

1494 1495
	ShowTransactionState("CommitTransaction");

1496 1497
	/*
	 * check the current transaction state
1498
	 */
1499
	if (s->state != TRANS_INPROGRESS)
1500 1501
		elog(WARNING, "CommitTransaction while in %s state",
			 TransStateAsString(s->state));
1502
	Assert(s->parent == NULL);
1503

1504
	/*
B
Bruce Momjian 已提交
1505 1506
	 * Do pre-commit processing (most of this stuff requires database access,
	 * and in fact could still cause an error...)
1507
	 *
B
Bruce Momjian 已提交
1508 1509 1510
	 * It is possible for CommitHoldablePortals to invoke functions that queue
	 * deferred triggers, and it's also possible that triggers create holdable
	 * cursors.  So we have to loop until there's nothing left to do.
1511
	 */
1512 1513 1514 1515 1516 1517
	for (;;)
	{
		/*
		 * Fire all currently pending deferred triggers.
		 */
		AfterTriggerFireDeferred();
1518

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
		/*
		 * Convert any open holdable cursors into static portals.  If there
		 * weren't any, we are done ... otherwise loop back to check if they
		 * queued deferred triggers.  Lather, rinse, repeat.
		 */
		if (!CommitHoldablePortals())
			break;
	}

	/* Now we can shut down the deferred-trigger manager */
	AfterTriggerEndXact(true);
1530

1531
	/* Close any open regular cursors */
1532
	AtCommit_Portals();
1533

1534
	/*
1535 1536
	 * Let ON COMMIT management do its thing (must happen after closing
	 * cursors, to avoid dangling-reference problems)
1537
	 */
1538
	PreCommit_on_commit_actions();
1539

1540 1541
	/* close large objects before lower-level cleanup */
	AtEOXact_LargeObject(true);
1542

1543
	/* NOTIFY commit must come before lower-level cleanup */
1544 1545
	AtCommit_Notify();

1546 1547 1548 1549
	/*
	 * Update flat files if we changed pg_database, pg_authid or
	 * pg_auth_members.  This should be the last step before commit.
	 */
1550
	AtEOXact_UpdateFlatFiles(true);
1551

1552 1553 1554 1555 1556
	/* Prevent cancel/die interrupt while cleaning up */
	HOLD_INTERRUPTS();

	/*
	 * set the current transaction state information appropriately during
1557
	 * commit processing
1558 1559 1560
	 */
	s->state = TRANS_COMMIT;

1561 1562 1563
	/*
	 * Here is where we really truly commit.
	 */
1564
	RecordTransactionCommit();
1565

1566
	/*----------
1567
	 * Let others know about no transaction in progress by me. Note that
1568 1569 1570
	 * this must be done _before_ releasing locks we hold and _after_
	 * RecordTransactionCommit.
	 *
1571 1572 1573 1574 1575 1576 1577
	 * LWLockAcquire(ProcArrayLock) is required; consider this example:
	 *		UPDATE with xid 0 is blocked by xid 1's UPDATE.
	 *		xid 1 is doing commit while xid 2 gets snapshot.
	 * If xid 2's GetSnapshotData sees xid 1 as running then it must see
	 * xid 0 as running as well, or it will be able to see two tuple versions
	 * - one deleted by xid 1 and one inserted by xid 0.  See notes in
	 * GetSnapshotData.
1578 1579
	 *
	 * Note: MyProc may be null during bootstrap.
1580
	 *----------
1581
	 */
1582
	if (MyProc != NULL)
1583
	{
1584 1585
		/* Lock ProcArrayLock because that's what GetSnapshotData uses. */
		LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1586 1587
		MyProc->xid = InvalidTransactionId;
		MyProc->xmin = InvalidTransactionId;
B
Bruce Momjian 已提交
1588
		MyProc->inVacuum = false;		/* must be cleared with xid/xmin */
1589 1590 1591 1592 1593

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

1594
		LWLockRelease(ProcArrayLock);
1595 1596
	}

B
Bruce Momjian 已提交
1597
	PG_TRACE1(transaction__commit, s->transactionId);
1598

1599
	/*
B
Bruce Momjian 已提交
1600 1601
	 * This is all post-commit cleanup.  Note that if an error is raised here,
	 * it's too late to abort the transaction.  This should be just
1602
	 * noncritical resource releasing.
1603
	 *
1604 1605 1606 1607 1608
	 * The ordering of operations is not entirely random.  The idea is:
	 * release resources visible to other backends (eg, files, buffer pins);
	 * then release locks; then release backend-local resources. We want to
	 * release locks at the point where any backend waiting for us will see
	 * our transaction as being fully cleaned up.
1609
	 *
B
Bruce Momjian 已提交
1610 1611 1612
	 * Resources that can be associated with individual queries are handled by
	 * the ResourceOwner mechanism.  The other calls here are for backend-wide
	 * state.
1613 1614
	 */

1615
	CallXactCallbacks(XACT_EVENT_COMMIT);
1616

1617 1618 1619 1620
	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_BEFORE_LOCKS,
						 true, true);

1621 1622 1623
	/* Check we've released all buffer pins */
	AtEOXact_Buffers(true);

1624 1625 1626
	/* Clean up the relation cache */
	AtEOXact_RelationCache(true);

1627
	/*
B
Bruce Momjian 已提交
1628 1629 1630 1631 1632
	 * Make catalog changes visible to all backends.  This has to happen after
	 * relcache references are dropped (see comments for
	 * AtEOXact_RelationCache), but before locks are released (if anyone is
	 * waiting for lock on a relation we've modified, we want them to know
	 * about the catalog change before they start using the relation).
1633 1634 1635
	 */
	AtEOXact_Inval(true);

1636 1637 1638 1639 1640 1641 1642 1643
	/*
	 * Likewise, dropping of files deleted during the transaction is best done
	 * after releasing relcache and buffer pins.  (This is not strictly
	 * necessary during commit, since such pins should have been released
	 * already, but this ordering is definitely critical during abort.)
	 */
	smgrDoPendingDeletes(true);

1644 1645
	AtEOXact_MultiXact();

1646 1647 1648 1649 1650 1651
	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_LOCKS,
						 true, true);
	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_AFTER_LOCKS,
						 true, true);
1652

1653 1654 1655
	/* Check we've released all catcache entries */
	AtEOXact_CatCache(true);

1656
	AtEOXact_GUC(true, false);
1657
	AtEOXact_SPI(true);
1658
	AtEOXact_on_commit_actions(true);
1659
	AtEOXact_Namespace(true);
1660
	/* smgrcommit already done */
1661
	AtEOXact_Files();
1662
	AtEOXact_ComboCid();
1663
	AtEOXact_HashTables(true);
1664
	pgstat_clear_snapshot();
1665
	pgstat_count_xact_commit();
1666
	pgstat_report_txn_timestamp(0);
1667 1668 1669 1670 1671 1672 1673

	CurrentResourceOwner = NULL;
	ResourceOwnerDelete(TopTransactionResourceOwner);
	s->curTransactionOwner = NULL;
	CurTransactionResourceOwner = NULL;
	TopTransactionResourceOwner = NULL;

1674
	AtCommit_Memory();
1675

1676 1677
	s->transactionId = InvalidTransactionId;
	s->subTransactionId = InvalidSubTransactionId;
1678 1679 1680
	s->nestingLevel = 0;
	s->childXids = NIL;

1681 1682 1683
	/*
	 * done with commit processing, set current transaction state back to
	 * default
1684
	 */
1685
	s->state = TRANS_DEFAULT;
1686

1687
	RESUME_INTERRUPTS();
1688
}
1689

1690 1691 1692 1693 1694 1695 1696 1697 1698

/*
 *	PrepareTransaction
 *
 * NB: if you change this routine, better look at CommitTransaction too!
 */
static void
PrepareTransaction(void)
{
B
Bruce Momjian 已提交
1699 1700 1701 1702
	TransactionState s = CurrentTransactionState;
	TransactionId xid = GetCurrentTransactionId();
	GlobalTransaction gxact;
	TimestampTz prepared_at;
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714

	ShowTransactionState("PrepareTransaction");

	/*
	 * check the current transaction state
	 */
	if (s->state != TRANS_INPROGRESS)
		elog(WARNING, "PrepareTransaction while in %s state",
			 TransStateAsString(s->state));
	Assert(s->parent == NULL);

	/*
B
Bruce Momjian 已提交
1715 1716
	 * Do pre-commit processing (most of this stuff requires database access,
	 * and in fact could still cause an error...)
1717
	 *
1718 1719 1720
	 * It is possible for PrepareHoldablePortals to invoke functions that
	 * queue deferred triggers, and it's also possible that triggers create
	 * holdable cursors.  So we have to loop until there's nothing left to do.
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
	 */
	for (;;)
	{
		/*
		 * Fire all currently pending deferred triggers.
		 */
		AfterTriggerFireDeferred();

		/*
		 * Convert any open holdable cursors into static portals.  If there
		 * weren't any, we are done ... otherwise loop back to check if they
		 * queued deferred triggers.  Lather, rinse, repeat.
		 */
		if (!PrepareHoldablePortals())
			break;
	}

	/* Now we can shut down the deferred-trigger manager */
	AfterTriggerEndXact(true);

	/* Close any open regular cursors */
	AtCommit_Portals();

	/*
	 * Let ON COMMIT management do its thing (must happen after closing
	 * cursors, to avoid dangling-reference problems)
	 */
	PreCommit_on_commit_actions();

	/* close large objects before lower-level cleanup */
	AtEOXact_LargeObject(true);

	/* NOTIFY and flatfiles will be handled below */

	/* Prevent cancel/die interrupt while cleaning up */
	HOLD_INTERRUPTS();

	/*
	 * set the current transaction state information appropriately during
1760
	 * prepare processing
1761 1762 1763
	 */
	s->state = TRANS_PREPARE;

1764
	prepared_at = GetCurrentTimestamp();
1765

1766 1767 1768 1769
	/* Tell bufmgr and smgr to prepare for commit */
	BufmgrCommit();

	/*
B
Bruce Momjian 已提交
1770 1771
	 * Reserve the GID for this transaction. This could fail if the requested
	 * GID is invalid or already in use.
1772
	 */
1773 1774
	gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
							GetUserId(), MyDatabaseId);
1775 1776 1777 1778 1779 1780 1781 1782 1783
	prepareGID = NULL;

	/*
	 * Collect data for the 2PC state file.  Note that in general, no actual
	 * state change should happen in the called modules during this step,
	 * since it's still possible to fail before commit, and in that case we
	 * want transaction abort to be able to clean up.  (In particular, the
	 * AtPrepare routines may error out if they find cases they cannot
	 * handle.)  State cleanup should happen in the PostPrepare routines
B
Bruce Momjian 已提交
1784 1785
	 * below.  However, some modules can go ahead and clear state here because
	 * they wouldn't do anything with it during abort anyway.
1786 1787
	 *
	 * Note: because the 2PC state file records will be replayed in the same
B
Bruce Momjian 已提交
1788 1789 1790 1791
	 * order they are made, the order of these calls has to match the order in
	 * which we want things to happen during COMMIT PREPARED or ROLLBACK
	 * PREPARED; in particular, pay attention to whether things should happen
	 * before or after releasing the transaction's locks.
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
	 */
	StartPrepare(gxact);

	AtPrepare_Notify();
	AtPrepare_UpdateFlatFiles();
	AtPrepare_Inval();
	AtPrepare_Locks();

	/*
	 * Here is where we really truly prepare.
	 *
1803 1804 1805
	 * We have to record transaction prepares even if we didn't make any
	 * updates, because the transaction manager might get confused if we lose
	 * a global transaction.
1806 1807 1808 1809
	 */
	EndPrepare(gxact);

	/*
B
Bruce Momjian 已提交
1810
	 * Now we clean up backend-internal state and release internal resources.
1811 1812 1813 1814 1815 1816 1817 1818
	 */

	/* Break the chain of back-links in the XLOG records I output */
	MyLastRecPtr.xrecoff = 0;
	MyXactMadeXLogEntry = false;
	MyXactMadeTempRelUpdate = false;

	/*
B
Bruce Momjian 已提交
1819 1820 1821
	 * Let others know about no transaction in progress by me.	This has to be
	 * done *after* the prepared transaction has been marked valid, else
	 * someone may think it is unlocked and recyclable.
1822 1823 1824 1825 1826 1827
	 */

	/* Lock ProcArrayLock because that's what GetSnapshotData uses. */
	LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
	MyProc->xid = InvalidTransactionId;
	MyProc->xmin = InvalidTransactionId;
1828
	MyProc->inVacuum = false;	/* must be cleared with xid/xmin */
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

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

	LWLockRelease(ProcArrayLock);

	/*
	 * This is all post-transaction cleanup.  Note that if an error is raised
	 * here, it's too late to abort the transaction.  This should be just
B
Bruce Momjian 已提交
1839
	 * noncritical resource releasing.	See notes in CommitTransaction.
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
	 */

	CallXactCallbacks(XACT_EVENT_PREPARE);

	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_BEFORE_LOCKS,
						 true, true);

	/* Check we've released all buffer pins */
	AtEOXact_Buffers(true);

1851 1852 1853
	/* Clean up the relation cache */
	AtEOXact_RelationCache(true);

1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
	/* notify and flatfiles don't need a postprepare call */

	PostPrepare_Inval();

	PostPrepare_smgr();

	AtEOXact_MultiXact();

	PostPrepare_Locks(xid);

	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_LOCKS,
						 true, true);
	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_AFTER_LOCKS,
						 true, true);

1871 1872 1873
	/* Check we've released all catcache entries */
	AtEOXact_CatCache(true);

1874 1875 1876 1877 1878 1879 1880
	/* PREPARE acts the same as COMMIT as far as GUC is concerned */
	AtEOXact_GUC(true, false);
	AtEOXact_SPI(true);
	AtEOXact_on_commit_actions(true);
	AtEOXact_Namespace(true);
	/* smgrcommit already done */
	AtEOXact_Files();
1881
	AtEOXact_ComboCid();
1882
	AtEOXact_HashTables(true);
1883
	pgstat_clear_snapshot();
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898

	CurrentResourceOwner = NULL;
	ResourceOwnerDelete(TopTransactionResourceOwner);
	s->curTransactionOwner = NULL;
	CurTransactionResourceOwner = NULL;
	TopTransactionResourceOwner = NULL;

	AtCommit_Memory();

	s->transactionId = InvalidTransactionId;
	s->subTransactionId = InvalidSubTransactionId;
	s->nestingLevel = 0;
	s->childXids = NIL;

	/*
B
Bruce Momjian 已提交
1899 1900
	 * done with 1st phase commit processing, set current transaction state
	 * back to default
1901 1902 1903 1904 1905 1906 1907
	 */
	s->state = TRANS_DEFAULT;

	RESUME_INTERRUPTS();
}


1908
/*
1909
 *	AbortTransaction
1910
 */
1911
static void
1912
AbortTransaction(void)
1913
{
1914 1915
	TransactionState s = CurrentTransactionState;

1916
	/* Prevent cancel/die interrupt while cleaning up */
1917
	HOLD_INTERRUPTS();
1918

1919 1920 1921 1922
	/* Make sure we have a valid memory context and resource owner */
	AtAbort_Memory();
	AtAbort_ResourceOwner();

1923
	/*
1924 1925
	 * Release any LW locks we might be holding as quickly as possible.
	 * (Regular locks, however, must be held till we finish aborting.)
B
Bruce Momjian 已提交
1926 1927
	 * Releasing LW locks is critical since we might try to grab them again
	 * while cleaning up!
1928
	 */
1929 1930 1931 1932
	LWLockReleaseAll();

	/* Clean up buffer I/O and buffer context locks, too */
	AbortBufferIO();
1933
	UnlockBuffers();
B
Bruce Momjian 已提交
1934

1935
	/*
B
Bruce Momjian 已提交
1936 1937
	 * Also clean up any open wait for lock, since the lock manager will choke
	 * if we try to wait for another lock before doing this.
1938 1939
	 */
	LockWaitCancel();
1940

1941 1942
	/*
	 * check the current transaction state
1943
	 */
1944
	if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
1945 1946
		elog(WARNING, "AbortTransaction while in %s state",
			 TransStateAsString(s->state));
1947
	Assert(s->parent == NULL);
1948

1949
	/*
B
Bruce Momjian 已提交
1950 1951
	 * set the current transaction state information appropriately during the
	 * abort processing
1952
	 */
1953 1954
	s->state = TRANS_ABORT;

1955
	/*
B
Bruce Momjian 已提交
1956 1957 1958
	 * Reset user id which might have been changed transiently.  We cannot use
	 * s->currentUser, since it may not be set yet; instead rely on internal
	 * state of miscinit.c.
1959
	 *
1960 1961 1962 1963 1964
	 * (Note: it is not necessary to restore session authorization here
	 * because that can only be changed via GUC, and GUC will take care of
	 * rolling it back if need be.	However, an error within a SECURITY
	 * DEFINER function could send control here with the wrong current
	 * userid.)
1965
	 */
1966
	AtAbort_UserId();
1967

1968 1969
	/*
	 * do abort processing
1970
	 */
1971
	AfterTriggerEndXact(false);
1972
	AtAbort_Portals();
B
Bruce Momjian 已提交
1973
	AtEOXact_LargeObject(false);	/* 'false' means it's abort */
1974
	AtAbort_Notify();
1975
	AtEOXact_UpdateFlatFiles(false);
1976

1977
	/*
B
Bruce Momjian 已提交
1978 1979
	 * Advertise the fact that we aborted in pg_clog (assuming that we got as
	 * far as assigning an XID to advertise).
1980 1981 1982
	 */
	if (TransactionIdIsValid(s->transactionId))
		RecordTransactionAbort();
1983

1984
	/*
B
Bruce Momjian 已提交
1985 1986
	 * Let others know about no transaction in progress by me. Note that this
	 * must be done _before_ releasing locks we hold and _after_
1987 1988
	 * RecordTransactionAbort.
	 */
1989
	if (MyProc != NULL)
1990
	{
1991 1992
		/* Lock ProcArrayLock because that's what GetSnapshotData uses. */
		LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1993 1994
		MyProc->xid = InvalidTransactionId;
		MyProc->xmin = InvalidTransactionId;
B
Bruce Momjian 已提交
1995
		MyProc->inVacuum = false;		/* must be cleared with xid/xmin */
1996
		MyProc->inCommit = false;		/* be sure this gets cleared */
1997 1998 1999 2000 2001

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

2002
		LWLockRelease(ProcArrayLock);
2003
	}
2004

B
Bruce Momjian 已提交
2005
	PG_TRACE1(transaction__abort, s->transactionId);
2006

2007
	/*
B
Bruce Momjian 已提交
2008
	 * Post-abort cleanup.	See notes in CommitTransaction() concerning
2009 2010 2011
	 * ordering.
	 */

2012
	CallXactCallbacks(XACT_EVENT_ABORT);
2013

2014 2015 2016
	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_BEFORE_LOCKS,
						 false, true);
2017
	AtEOXact_Buffers(false);
2018
	AtEOXact_RelationCache(false);
2019
	AtEOXact_Inval(false);
2020
	smgrDoPendingDeletes(false);
2021
	AtEOXact_MultiXact();
2022 2023 2024 2025 2026 2027
	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_LOCKS,
						 false, true);
	ResourceOwnerRelease(TopTransactionResourceOwner,
						 RESOURCE_RELEASE_AFTER_LOCKS,
						 false, true);
2028
	AtEOXact_CatCache(false);
2029

2030
	AtEOXact_GUC(false, false);
2031
	AtEOXact_SPI(false);
2032
	AtEOXact_on_commit_actions(false);
2033
	AtEOXact_Namespace(false);
2034
	smgrabort();
2035
	AtEOXact_Files();
2036
	AtEOXact_ComboCid();
2037
	AtEOXact_HashTables(false);
2038
	pgstat_clear_snapshot();
2039
	pgstat_count_xact_rollback();
2040
	pgstat_report_txn_timestamp(0);
2041

2042 2043
	/*
	 * State remains TRANS_ABORT until CleanupTransaction().
2044
	 */
2045
	RESUME_INTERRUPTS();
2046 2047
}

2048
/*
2049
 *	CleanupTransaction
2050 2051
 */
static void
2052
CleanupTransaction(void)
2053 2054 2055
{
	TransactionState s = CurrentTransactionState;

2056 2057
	/*
	 * State should still be TRANS_ABORT from AbortTransaction().
2058 2059
	 */
	if (s->state != TRANS_ABORT)
T
Tom Lane 已提交
2060 2061
		elog(FATAL, "CleanupTransaction: unexpected state %s",
			 TransStateAsString(s->state));
2062

2063 2064
	/*
	 * do abort cleanup processing
2065
	 */
2066
	AtCleanup_Portals();		/* now safe to release portal memory */
2067

B
Bruce Momjian 已提交
2068
	CurrentResourceOwner = NULL;	/* and resource owner */
2069 2070
	if (TopTransactionResourceOwner)
		ResourceOwnerDelete(TopTransactionResourceOwner);
2071 2072 2073 2074
	s->curTransactionOwner = NULL;
	CurTransactionResourceOwner = NULL;
	TopTransactionResourceOwner = NULL;

2075
	AtCleanup_Memory();			/* and transaction memory */
2076

2077 2078
	s->transactionId = InvalidTransactionId;
	s->subTransactionId = InvalidSubTransactionId;
2079 2080 2081
	s->nestingLevel = 0;
	s->childXids = NIL;

2082 2083 2084
	/*
	 * done with abort processing, set current transaction state back to
	 * default
2085
	 */
2086 2087 2088
	s->state = TRANS_DEFAULT;
}

2089
/*
2090
 *	StartTransactionCommand
2091 2092
 */
void
2093
StartTransactionCommand(void)
2094 2095 2096 2097 2098
{
	TransactionState s = CurrentTransactionState;

	switch (s->blockState)
	{
2099
			/*
B
Bruce Momjian 已提交
2100 2101
			 * if we aren't in a transaction block, we just do our usual start
			 * transaction.
2102 2103 2104
			 */
		case TBLOCK_DEFAULT:
			StartTransaction();
2105 2106 2107
			s->blockState = TBLOCK_STARTED;
			break;

2108
			/*
B
Bruce Momjian 已提交
2109 2110 2111 2112 2113
			 * We are somewhere in a transaction block or subtransaction and
			 * about to start a new command.  For now we do nothing, but
			 * someday we may do command-local resource initialization. (Note
			 * that any needed CommandCounterIncrement was done by the
			 * previous CommitTransactionCommand.)
2114 2115
			 */
		case TBLOCK_INPROGRESS:
2116
		case TBLOCK_SUBINPROGRESS:
2117 2118
			break;

2119
			/*
B
Bruce Momjian 已提交
2120 2121 2122 2123 2124 2125
			 * Here we are in a failed transaction block (one of the commands
			 * caused an abort) so we do nothing but remain in the abort
			 * state.  Eventually we will get a ROLLBACK command which will
			 * get us out of this state.  (It is up to other code to ensure
			 * that no commands other than ROLLBACK will be processed in these
			 * states.)
2126 2127
			 */
		case TBLOCK_ABORT:
2128
		case TBLOCK_SUBABORT:
2129 2130
			break;

2131 2132 2133 2134 2135 2136
			/* These cases are invalid. */
		case TBLOCK_STARTED:
		case TBLOCK_BEGIN:
		case TBLOCK_SUBBEGIN:
		case TBLOCK_END:
		case TBLOCK_SUBEND:
2137 2138 2139
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
2140
		case TBLOCK_SUBABORT_PENDING:
2141 2142
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
2143
		case TBLOCK_PREPARE:
2144
			elog(ERROR, "StartTransactionCommand: unexpected state %s",
2145
				 BlockStateAsString(s->blockState));
2146
			break;
2147
	}
2148 2149

	/*
2150
	 * We must switch to CurTransactionContext before returning. This is
B
Bruce Momjian 已提交
2151
	 * already done if we called StartTransaction, otherwise not.
2152
	 */
2153 2154
	Assert(CurTransactionContext != NULL);
	MemoryContextSwitchTo(CurTransactionContext);
2155 2156
}

2157
/*
2158
 *	CommitTransactionCommand
2159 2160
 */
void
2161
CommitTransactionCommand(void)
2162 2163 2164 2165 2166
{
	TransactionState s = CurrentTransactionState;

	switch (s->blockState)
	{
2167 2168 2169
			/*
			 * This shouldn't happen, because it means the previous
			 * StartTransactionCommand didn't set the STARTED state
2170
			 * appropriately.
2171 2172
			 */
		case TBLOCK_DEFAULT:
2173 2174
			elog(FATAL, "CommitTransactionCommand: unexpected state %s",
				 BlockStateAsString(s->blockState));
2175 2176
			break;

2177
			/*
2178
			 * If we aren't in a transaction block, just do our usual
2179
			 * transaction commit, and return to the idle state.
2180
			 */
2181
		case TBLOCK_STARTED:
2182
			CommitTransaction();
2183
			s->blockState = TBLOCK_DEFAULT;
2184 2185
			break;

2186
			/*
B
Bruce Momjian 已提交
2187 2188 2189 2190
			 * We are completing a "BEGIN TRANSACTION" command, so we change
			 * to the "transaction block in progress" state and return.  (We
			 * assume the BEGIN did nothing to the database, so we need no
			 * CommandCounterIncrement.)
2191 2192 2193 2194 2195
			 */
		case TBLOCK_BEGIN:
			s->blockState = TBLOCK_INPROGRESS;
			break;

2196 2197
			/*
			 * This is the case when we have finished executing a command
B
Bruce Momjian 已提交
2198 2199
			 * someplace within a transaction block.  We increment the command
			 * counter and return.
2200 2201
			 */
		case TBLOCK_INPROGRESS:
2202
		case TBLOCK_SUBINPROGRESS:
2203 2204 2205
			CommandCounterIncrement();
			break;

2206
			/*
B
Bruce Momjian 已提交
2207 2208
			 * We are completing a "COMMIT" command.  Do it and return to the
			 * idle state.
2209 2210 2211
			 */
		case TBLOCK_END:
			CommitTransaction();
2212
			s->blockState = TBLOCK_DEFAULT;
2213 2214
			break;

2215
			/*
B
Bruce Momjian 已提交
2216 2217 2218
			 * Here we are in the middle of a transaction block but one of the
			 * commands caused an abort so we do nothing but remain in the
			 * abort state.  Eventually we will get a ROLLBACK comand.
2219 2220
			 */
		case TBLOCK_ABORT:
2221
		case TBLOCK_SUBABORT:
2222 2223
			break;

2224
			/*
B
Bruce Momjian 已提交
2225 2226
			 * Here we were in an aborted transaction block and we just got
			 * the ROLLBACK command from the user, so clean up the
2227
			 * already-aborted transaction and return to the idle state.
2228
			 */
2229
		case TBLOCK_ABORT_END:
2230
			CleanupTransaction();
2231 2232
			s->blockState = TBLOCK_DEFAULT;
			break;
2233

2234
			/*
B
Bruce Momjian 已提交
2235 2236 2237
			 * Here we were in a perfectly good transaction block but the user
			 * told us to ROLLBACK anyway.	We have to abort the transaction
			 * and then clean up.
2238
			 */
2239 2240 2241 2242
		case TBLOCK_ABORT_PENDING:
			AbortTransaction();
			CleanupTransaction();
			s->blockState = TBLOCK_DEFAULT;
2243 2244
			break;

2245 2246 2247 2248 2249 2250 2251 2252 2253
			/*
			 * We are completing a "PREPARE TRANSACTION" command.  Do it and
			 * return to the idle state.
			 */
		case TBLOCK_PREPARE:
			PrepareTransaction();
			s->blockState = TBLOCK_DEFAULT;
			break;

2254 2255
			/*
			 * We were just issued a SAVEPOINT inside a transaction block.
B
Bruce Momjian 已提交
2256
			 * Start a subtransaction.	(DefineSavepoint already did
B
Bruce Momjian 已提交
2257 2258
			 * PushTransaction, so as to have someplace to put the SUBBEGIN
			 * state.)
2259 2260 2261 2262 2263
			 */
		case TBLOCK_SUBBEGIN:
			StartSubTransaction();
			s->blockState = TBLOCK_SUBINPROGRESS;
			break;
2264

2265
			/*
2266 2267
			 * We were issued a COMMIT or RELEASE command, so we end the
			 * current subtransaction and return to the parent transaction.
2268 2269
			 * The parent might be ended too, so repeat till we are all the
			 * way out or find an INPROGRESS transaction.
2270 2271
			 */
		case TBLOCK_SUBEND:
B
Bruce Momjian 已提交
2272 2273
			do
			{
2274
				CommitSubTransaction();
B
Bruce Momjian 已提交
2275
				s = CurrentTransactionState;	/* changed by pop */
2276
			} while (s->blockState == TBLOCK_SUBEND);
2277 2278 2279 2280 2281 2282 2283
			/* If we had a COMMIT command, finish off the main xact too */
			if (s->blockState == TBLOCK_END)
			{
				Assert(s->parent == NULL);
				CommitTransaction();
				s->blockState = TBLOCK_DEFAULT;
			}
2284 2285 2286 2287 2288 2289
			else if (s->blockState == TBLOCK_PREPARE)
			{
				Assert(s->parent == NULL);
				PrepareTransaction();
				s->blockState = TBLOCK_DEFAULT;
			}
2290 2291 2292 2293 2294
			else
			{
				Assert(s->blockState == TBLOCK_INPROGRESS ||
					   s->blockState == TBLOCK_SUBINPROGRESS);
			}
2295 2296 2297
			break;

			/*
2298 2299 2300
			 * The current already-failed subtransaction is ending due to a
			 * ROLLBACK or ROLLBACK TO command, so pop it and recursively
			 * examine the parent (which could be in any of several states).
2301
			 */
2302 2303 2304
		case TBLOCK_SUBABORT_END:
			CleanupSubTransaction();
			CommitTransactionCommand();
2305 2306 2307
			break;

			/*
2308
			 * As above, but it's not dead yet, so abort first.
2309
			 */
2310 2311 2312 2313
		case TBLOCK_SUBABORT_PENDING:
			AbortSubTransaction();
			CleanupSubTransaction();
			CommitTransactionCommand();
2314 2315 2316
			break;

			/*
2317 2318 2319
			 * The current subtransaction is the target of a ROLLBACK TO
			 * command.  Abort and pop it, then start a new subtransaction
			 * with the same name.
2320
			 */
2321
		case TBLOCK_SUBRESTART:
2322
			{
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
				char	   *name;
				int			savepointLevel;

				/* save name and keep Cleanup from freeing it */
				name = s->name;
				s->name = NULL;
				savepointLevel = s->savepointLevel;

				AbortSubTransaction();
				CleanupSubTransaction();
2333

2334 2335 2336 2337
				DefineSavepoint(NULL);
				s = CurrentTransactionState;	/* changed by push */
				s->name = name;
				s->savepointLevel = savepointLevel;
2338 2339 2340 2341 2342 2343

				/* This is the same as TBLOCK_SUBBEGIN case */
				AssertState(s->blockState == TBLOCK_SUBBEGIN);
				StartSubTransaction();
				s->blockState = TBLOCK_SUBINPROGRESS;
			}
2344 2345
			break;

2346
			/*
B
Bruce Momjian 已提交
2347 2348
			 * Same as above, but the subtransaction had already failed, so we
			 * don't need AbortSubTransaction.
2349 2350 2351 2352 2353
			 */
		case TBLOCK_SUBABORT_RESTART:
			{
				char	   *name;
				int			savepointLevel;
B
Bruce Momjian 已提交
2354

2355 2356 2357 2358
				/* save name and keep Cleanup from freeing it */
				name = s->name;
				s->name = NULL;
				savepointLevel = s->savepointLevel;
2359

2360
				CleanupSubTransaction();
2361

2362 2363 2364 2365
				DefineSavepoint(NULL);
				s = CurrentTransactionState;	/* changed by push */
				s->name = name;
				s->savepointLevel = savepointLevel;
2366

2367 2368 2369 2370 2371 2372
				/* This is the same as TBLOCK_SUBBEGIN case */
				AssertState(s->blockState == TBLOCK_SUBBEGIN);
				StartSubTransaction();
				s->blockState = TBLOCK_SUBINPROGRESS;
			}
			break;
2373 2374 2375
	}
}

2376 2377 2378 2379 2380 2381 2382 2383 2384
/*
 *	AbortCurrentTransaction
 */
void
AbortCurrentTransaction(void)
{
	TransactionState s = CurrentTransactionState;

	switch (s->blockState)
2385
	{
2386
		case TBLOCK_DEFAULT:
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
			if (s->state == TRANS_DEFAULT)
			{
				/* we are idle, so nothing to do */
			}
			else
			{
				/*
				 * We can get here after an error during transaction start
				 * (state will be TRANS_START).  Need to clean up the
				 * incompletely started transaction.  First, adjust the
				 * low-level state to suppress warning message from
				 * AbortTransaction.
				 */
				if (s->state == TRANS_START)
					s->state = TRANS_INPROGRESS;
				AbortTransaction();
				CleanupTransaction();
			}
2405 2406
			break;

2407
			/*
B
Bruce Momjian 已提交
2408 2409
			 * if we aren't in a transaction block, we just do the basic abort
			 * & cleanup transaction.
2410
			 */
2411
		case TBLOCK_STARTED:
2412
			AbortTransaction();
2413
			CleanupTransaction();
2414
			s->blockState = TBLOCK_DEFAULT;
2415 2416
			break;

2417
			/*
B
Bruce Momjian 已提交
2418 2419 2420 2421 2422
			 * If we are in TBLOCK_BEGIN it means something screwed up right
			 * after reading "BEGIN TRANSACTION".  We assume that the user
			 * will interpret the error as meaning the BEGIN failed to get him
			 * into a transaction block, so we should abort and return to idle
			 * state.
2423 2424 2425
			 */
		case TBLOCK_BEGIN:
			AbortTransaction();
2426 2427
			CleanupTransaction();
			s->blockState = TBLOCK_DEFAULT;
2428 2429
			break;

2430
			/*
2431 2432 2433
			 * We are somewhere in a transaction block and we've gotten a
			 * failure, so we abort the transaction and set up the persistent
			 * ABORT state.  We will stay in ABORT until we get a ROLLBACK.
2434 2435 2436
			 */
		case TBLOCK_INPROGRESS:
			AbortTransaction();
2437
			s->blockState = TBLOCK_ABORT;
2438
			/* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
2439 2440
			break;

2441
			/*
B
Bruce Momjian 已提交
2442 2443 2444
			 * Here, we failed while trying to COMMIT.	Clean up the
			 * transaction and return to idle state (we do not want to stay in
			 * the transaction).
2445 2446 2447
			 */
		case TBLOCK_END:
			AbortTransaction();
2448
			CleanupTransaction();
2449
			s->blockState = TBLOCK_DEFAULT;
2450 2451
			break;

2452
			/*
B
Bruce Momjian 已提交
2453
			 * Here, we are already in an aborted transaction state and are
2454 2455
			 * waiting for a ROLLBACK, but for some reason we failed again! So
			 * we just remain in the abort state.
2456 2457
			 */
		case TBLOCK_ABORT:
2458
		case TBLOCK_SUBABORT:
2459 2460
			break;

2461
			/*
2462
			 * We are in a failed transaction and we got the ROLLBACK command.
B
Bruce Momjian 已提交
2463 2464
			 * We have already aborted, we just need to cleanup and go to idle
			 * state.
2465
			 */
2466
		case TBLOCK_ABORT_END:
2467
			CleanupTransaction();
2468 2469
			s->blockState = TBLOCK_DEFAULT;
			break;
2470 2471

			/*
2472 2473
			 * We are in a live transaction and we got a ROLLBACK command.
			 * Abort, cleanup, go to idle state.
2474
			 */
2475 2476 2477 2478
		case TBLOCK_ABORT_PENDING:
			AbortTransaction();
			CleanupTransaction();
			s->blockState = TBLOCK_DEFAULT;
2479 2480
			break;

2481 2482
			/*
			 * Here, we failed while trying to PREPARE.  Clean up the
B
Bruce Momjian 已提交
2483 2484
			 * transaction and return to idle state (we do not want to stay in
			 * the transaction).
2485 2486 2487 2488 2489 2490 2491
			 */
		case TBLOCK_PREPARE:
			AbortTransaction();
			CleanupTransaction();
			s->blockState = TBLOCK_DEFAULT;
			break;

2492 2493
			/*
			 * We got an error inside a subtransaction.  Abort just the
B
Bruce Momjian 已提交
2494 2495
			 * subtransaction, and go to the persistent SUBABORT state until
			 * we get ROLLBACK.
2496
			 */
2497 2498 2499 2500 2501 2502
		case TBLOCK_SUBINPROGRESS:
			AbortSubTransaction();
			s->blockState = TBLOCK_SUBABORT;
			break;

			/*
2503
			 * If we failed while trying to create a subtransaction, clean up
B
Bruce Momjian 已提交
2504
			 * the broken subtransaction and abort the parent.	The same
2505
			 * applies if we get a failure while ending a subtransaction.
2506
			 */
2507
		case TBLOCK_SUBBEGIN:
2508
		case TBLOCK_SUBEND:
2509
		case TBLOCK_SUBABORT_PENDING:
2510
		case TBLOCK_SUBRESTART:
2511 2512 2513 2514 2515 2516 2517 2518
			AbortSubTransaction();
			CleanupSubTransaction();
			AbortCurrentTransaction();
			break;

			/*
			 * Same as above, except the Abort() was already done.
			 */
2519 2520
		case TBLOCK_SUBABORT_END:
		case TBLOCK_SUBABORT_RESTART:
2521 2522 2523
			CleanupSubTransaction();
			AbortCurrentTransaction();
			break;
2524 2525 2526
	}
}

2527
/*
2528
 *	PreventTransactionChain
2529
 *
2530 2531 2532
 *	This routine is to be called by statements that must not run inside
 *	a transaction block, typically because they have non-rollback-able
 *	side effects or do internal commits.
2533
 *
2534 2535 2536
 *	If we have already started a transaction block, issue an error; also issue
 *	an error if we appear to be running inside a user-defined function (which
 *	could issue more commands and possibly cause a failure after the statement
2537
 *	completes).  Subtransactions are verboten too.
2538
 *
2539
 *	isTopLevel: passed down from ProcessUtility to determine whether we are
2540 2541 2542 2543
 *	inside a function or multi-query querystring.  (We will always fail if
 *	this is false, but it's convenient to centralize the check here instead of
 *	making callers do it.)
 *  stmtType: statement type name, for error messages.
2544 2545
 */
void
2546
PreventTransactionChain(bool isTopLevel, const char *stmtType)
2547 2548 2549 2550 2551
{
	/*
	 * xact block already started?
	 */
	if (IsTransactionBlock())
2552 2553
		ereport(ERROR,
				(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
B
Bruce Momjian 已提交
2554
		/* translator: %s represents an SQL statement name */
2555 2556
				 errmsg("%s cannot run inside a transaction block",
						stmtType)));
B
Bruce Momjian 已提交
2557

2558 2559 2560 2561 2562 2563 2564 2565 2566 2567
	/*
	 * subtransaction?
	 */
	if (IsSubTransaction())
		ereport(ERROR,
				(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
		/* translator: %s represents an SQL statement name */
				 errmsg("%s cannot run inside a subtransaction",
						stmtType)));

2568
	/*
2569
	 * inside a function call?
2570
	 */
2571
	if (!isTopLevel)
2572 2573
		ereport(ERROR,
				(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
B
Bruce Momjian 已提交
2574
		/* translator: %s represents an SQL statement name */
2575 2576
				 errmsg("%s cannot be executed from a function or multi-command string",
						stmtType)));
2577

2578
	/* If we got past IsTransactionBlock test, should be in default state */
2579
	if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2580
		CurrentTransactionState->blockState != TBLOCK_STARTED)
2581
		elog(FATAL, "cannot prevent transaction chain");
2582
	/* all okay */
2583 2584
}

2585
/*
2586 2587 2588 2589 2590 2591 2592 2593 2594
 *	RequireTransactionChain
 *
 *	This routine is to be called by statements that must run inside
 *	a transaction block, because they have no effects that persist past
 *	transaction end (and so calling them outside a transaction block
 *	is presumably an error).  DECLARE CURSOR is an example.
 *
 *	If we appear to be running inside a user-defined function, we do not
 *	issue an error, since the function could issue more commands that make
2595 2596
 *	use of the current statement's results.  Likewise subtransactions.
 *	Thus this is an inverse for PreventTransactionChain.
2597
 *
2598 2599 2600
 *	isTopLevel: passed down from ProcessUtility to determine whether we are
 *	inside a function.
 *	stmtType: statement type name, for error messages.
2601 2602
 */
void
2603
RequireTransactionChain(bool isTopLevel, const char *stmtType)
2604 2605 2606 2607 2608 2609
{
	/*
	 * xact block already started?
	 */
	if (IsTransactionBlock())
		return;
B
Bruce Momjian 已提交
2610

2611 2612 2613 2614 2615 2616
	/*
	 * subtransaction?
	 */
	if (IsSubTransaction())
		return;

2617
	/*
2618
	 * inside a function call?
2619
	 */
2620
	if (!isTopLevel)
2621
		return;
2622

2623 2624
	ereport(ERROR,
			(errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
B
Bruce Momjian 已提交
2625
	/* translator: %s represents an SQL statement name */
2626
			 errmsg("%s can only be used in transaction blocks",
2627
					stmtType)));
2628 2629
}

2630 2631 2632 2633 2634 2635 2636
/*
 *	IsInTransactionChain
 *
 *	This routine is for statements that need to behave differently inside
 *	a transaction block than when running as single commands.  ANALYZE is
 *	currently the only example.
 *
2637 2638
 *	isTopLevel: passed down from ProcessUtility to determine whether we are
 *	inside a function.
2639 2640
 */
bool
2641
IsInTransactionChain(bool isTopLevel)
2642 2643
{
	/*
B
Bruce Momjian 已提交
2644 2645
	 * Return true on same conditions that would make PreventTransactionChain
	 * error out
2646 2647 2648 2649
	 */
	if (IsTransactionBlock())
		return true;

2650 2651 2652
	if (IsSubTransaction())
		return true;

2653
	if (!isTopLevel)
2654 2655 2656 2657 2658 2659 2660 2661 2662
		return true;

	if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
		CurrentTransactionState->blockState != TBLOCK_STARTED)
		return true;

	return false;
}

2663

2664
/*
2665 2666
 * Register or deregister callback functions for start- and end-of-xact
 * operations.
2667 2668 2669 2670 2671
 *
 * These functions are intended for use by dynamically loaded modules.
 * For built-in modules we generally just hardwire the appropriate calls
 * (mainly because it's easier to control the order that way, where needed).
 *
2672
 * At transaction end, the callback occurs post-commit or post-abort, so the
2673
 * callback functions can only do noncritical cleanup.
2674 2675
 */
void
2676
RegisterXactCallback(XactCallback callback, void *arg)
2677
{
2678
	XactCallbackItem *item;
2679

2680 2681
	item = (XactCallbackItem *)
		MemoryContextAlloc(TopMemoryContext, sizeof(XactCallbackItem));
2682 2683
	item->callback = callback;
	item->arg = arg;
2684 2685
	item->next = Xact_callbacks;
	Xact_callbacks = item;
2686 2687 2688
}

void
2689
UnregisterXactCallback(XactCallback callback, void *arg)
2690
{
2691 2692
	XactCallbackItem *item;
	XactCallbackItem *prev;
2693 2694

	prev = NULL;
2695
	for (item = Xact_callbacks; item; prev = item, item = item->next)
2696 2697 2698 2699 2700 2701
	{
		if (item->callback == callback && item->arg == arg)
		{
			if (prev)
				prev->next = item->next;
			else
2702
				Xact_callbacks = item->next;
2703 2704 2705 2706 2707 2708 2709
			pfree(item);
			break;
		}
	}
}

static void
2710
CallXactCallbacks(XactEvent event)
2711
{
2712
	XactCallbackItem *item;
2713

2714
	for (item = Xact_callbacks; item; item = item->next)
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
		(*item->callback) (event, item->arg);
}


/*
 * Register or deregister callback functions for start- and end-of-subxact
 * operations.
 *
 * Pretty much same as above, but for subtransaction events.
 *
 * At subtransaction end, the callback occurs post-subcommit or post-subabort,
 * so the callback functions can only do noncritical cleanup.  At
 * subtransaction start, the callback is called when the subtransaction has
 * finished initializing.
 */
void
RegisterSubXactCallback(SubXactCallback callback, void *arg)
{
	SubXactCallbackItem *item;

	item = (SubXactCallbackItem *)
		MemoryContextAlloc(TopMemoryContext, sizeof(SubXactCallbackItem));
	item->callback = callback;
	item->arg = arg;
	item->next = SubXact_callbacks;
	SubXact_callbacks = item;
}

void
UnregisterSubXactCallback(SubXactCallback callback, void *arg)
{
	SubXactCallbackItem *item;
	SubXactCallbackItem *prev;

	prev = NULL;
	for (item = SubXact_callbacks; item; prev = item, item = item->next)
	{
		if (item->callback == callback && item->arg == arg)
		{
			if (prev)
				prev->next = item->next;
			else
				SubXact_callbacks = item->next;
			pfree(item);
			break;
		}
	}
}

static void
CallSubXactCallbacks(SubXactEvent event,
					 SubTransactionId mySubid,
					 SubTransactionId parentSubid)
{
	SubXactCallbackItem *item;

	for (item = SubXact_callbacks; item; item = item->next)
		(*item->callback) (event, mySubid, parentSubid, item->arg);
2773 2774 2775
}


2776 2777 2778 2779
/* ----------------------------------------------------------------
 *					   transaction block support
 * ----------------------------------------------------------------
 */
2780

2781
/*
2782
 *	BeginTransactionBlock
2783
 *		This executes a BEGIN command.
2784 2785 2786 2787 2788 2789
 */
void
BeginTransactionBlock(void)
{
	TransactionState s = CurrentTransactionState;

2790 2791
	switch (s->blockState)
	{
2792
			/*
B
Bruce Momjian 已提交
2793
			 * We are not inside a transaction block, so allow one to begin.
2794 2795 2796 2797
			 */
		case TBLOCK_STARTED:
			s->blockState = TBLOCK_BEGIN;
			break;
2798

2799 2800 2801
			/*
			 * Already a transaction block in progress.
			 */
2802
		case TBLOCK_INPROGRESS:
2803
		case TBLOCK_SUBINPROGRESS:
2804
		case TBLOCK_ABORT:
2805
		case TBLOCK_SUBABORT:
2806 2807
			ereport(WARNING,
					(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
B
Bruce Momjian 已提交
2808
					 errmsg("there is already a transaction in progress")));
2809
			break;
2810

2811
			/* These cases are invalid. */
2812 2813
		case TBLOCK_DEFAULT:
		case TBLOCK_BEGIN:
2814
		case TBLOCK_SUBBEGIN:
2815
		case TBLOCK_END:
2816
		case TBLOCK_SUBEND:
2817 2818 2819 2820 2821 2822
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
		case TBLOCK_SUBABORT_PENDING:
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
2823
		case TBLOCK_PREPARE:
2824 2825
			elog(FATAL, "BeginTransactionBlock: unexpected state %s",
				 BlockStateAsString(s->blockState));
2826 2827
			break;
	}
2828 2829
}

2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
/*
 *	PrepareTransactionBlock
 *		This executes a PREPARE command.
 *
 * Since PREPARE may actually do a ROLLBACK, the result indicates what
 * happened: TRUE for PREPARE, FALSE for ROLLBACK.
 *
 * Note that we don't actually do anything here except change blockState.
 * The real work will be done in the upcoming PrepareTransaction().
 * We do it this way because it's not convenient to change memory context,
 * resource owner, etc while executing inside a Portal.
 */
bool
PrepareTransactionBlock(char *gid)
{
	TransactionState s;
B
Bruce Momjian 已提交
2846
	bool		result;
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880

	/* Set up to commit the current transaction */
	result = EndTransactionBlock();

	/* If successful, change outer tblock state to PREPARE */
	if (result)
	{
		s = CurrentTransactionState;

		while (s->parent != NULL)
			s = s->parent;

		if (s->blockState == TBLOCK_END)
		{
			/* Save GID where PrepareTransaction can find it again */
			prepareGID = MemoryContextStrdup(TopTransactionContext, gid);

			s->blockState = TBLOCK_PREPARE;
		}
		else
		{
			/*
			 * ignore case where we are not in a transaction;
			 * EndTransactionBlock already issued a warning.
			 */
			Assert(s->blockState == TBLOCK_STARTED);
			/* Don't send back a PREPARE result tag... */
			result = false;
		}
	}

	return result;
}

2881
/*
2882
 *	EndTransactionBlock
2883
 *		This executes a COMMIT command.
2884 2885 2886
 *
 * Since COMMIT may actually do a ROLLBACK, the result indicates what
 * happened: TRUE for COMMIT, FALSE for ROLLBACK.
2887 2888 2889 2890 2891
 *
 * Note that we don't actually do anything here except change blockState.
 * The real work will be done in the upcoming CommitTransactionCommand().
 * We do it this way because it's not convenient to change memory context,
 * resource owner, etc while executing inside a Portal.
2892
 */
2893
bool
2894
EndTransactionBlock(void)
2895
{
2896
	TransactionState s = CurrentTransactionState;
2897
	bool		result = false;
2898

2899 2900
	switch (s->blockState)
	{
B
Bruce Momjian 已提交
2901
			/*
2902 2903
			 * We are in a transaction block, so tell CommitTransactionCommand
			 * to COMMIT.
B
Bruce Momjian 已提交
2904
			 */
2905
		case TBLOCK_INPROGRESS:
2906 2907
			s->blockState = TBLOCK_END;
			result = true;
2908 2909
			break;

2910
			/*
2911 2912
			 * We are in a failed transaction block.  Tell
			 * CommitTransactionCommand it's time to exit the block.
2913 2914
			 */
		case TBLOCK_ABORT:
2915
			s->blockState = TBLOCK_ABORT_END;
2916
			break;
2917

2918
			/*
B
Bruce Momjian 已提交
2919 2920
			 * We are in a live subtransaction block.  Set up to subcommit all
			 * open subtransactions and then commit the main transaction.
2921 2922 2923 2924
			 */
		case TBLOCK_SUBINPROGRESS:
			while (s->parent != NULL)
			{
2925 2926 2927 2928 2929
				if (s->blockState == TBLOCK_SUBINPROGRESS)
					s->blockState = TBLOCK_SUBEND;
				else
					elog(FATAL, "EndTransactionBlock: unexpected state %s",
						 BlockStateAsString(s->blockState));
2930 2931
				s = s->parent;
			}
2932 2933 2934 2935 2936
			if (s->blockState == TBLOCK_INPROGRESS)
				s->blockState = TBLOCK_END;
			else
				elog(FATAL, "EndTransactionBlock: unexpected state %s",
					 BlockStateAsString(s->blockState));
2937 2938 2939
			result = true;
			break;

2940
			/*
B
Bruce Momjian 已提交
2941 2942 2943
			 * Here we are inside an aborted subtransaction.  Treat the COMMIT
			 * as ROLLBACK: set up to abort everything and exit the main
			 * transaction.
2944 2945
			 */
		case TBLOCK_SUBABORT:
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
			while (s->parent != NULL)
			{
				if (s->blockState == TBLOCK_SUBINPROGRESS)
					s->blockState = TBLOCK_SUBABORT_PENDING;
				else if (s->blockState == TBLOCK_SUBABORT)
					s->blockState = TBLOCK_SUBABORT_END;
				else
					elog(FATAL, "EndTransactionBlock: unexpected state %s",
						 BlockStateAsString(s->blockState));
				s = s->parent;
			}
			if (s->blockState == TBLOCK_INPROGRESS)
				s->blockState = TBLOCK_ABORT_PENDING;
			else if (s->blockState == TBLOCK_ABORT)
				s->blockState = TBLOCK_ABORT_END;
			else
				elog(FATAL, "EndTransactionBlock: unexpected state %s",
					 BlockStateAsString(s->blockState));
2964 2965
			break;

2966
			/*
2967 2968 2969 2970
			 * The user issued COMMIT when not inside a transaction.  Issue a
			 * WARNING, staying in TBLOCK_STARTED state.  The upcoming call to
			 * CommitTransactionCommand() will then close the transaction and
			 * put us back into the default state.
2971
			 */
2972
		case TBLOCK_STARTED:
2973 2974 2975
			ereport(WARNING,
					(errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
					 errmsg("there is no transaction in progress")));
2976
			result = true;
2977
			break;
2978

2979
			/* These cases are invalid. */
2980 2981
		case TBLOCK_DEFAULT:
		case TBLOCK_BEGIN:
2982
		case TBLOCK_SUBBEGIN:
2983
		case TBLOCK_END:
2984
		case TBLOCK_SUBEND:
2985 2986 2987
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
2988
		case TBLOCK_SUBABORT_PENDING:
2989 2990
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
2991
		case TBLOCK_PREPARE:
2992 2993
			elog(FATAL, "EndTransactionBlock: unexpected state %s",
				 BlockStateAsString(s->blockState));
2994
			break;
2995
	}
2996 2997

	return result;
2998 2999
}

3000
/*
3001
 *	UserAbortTransactionBlock
3002
 *		This executes a ROLLBACK command.
3003 3004
 *
 * As above, we don't actually do anything here except change blockState.
3005 3006
 */
void
3007
UserAbortTransactionBlock(void)
3008
{
3009 3010
	TransactionState s = CurrentTransactionState;

3011 3012 3013
	switch (s->blockState)
	{
			/*
B
Bruce Momjian 已提交
3014 3015 3016
			 * We are inside a transaction block and we got a ROLLBACK command
			 * from the user, so tell CommitTransactionCommand to abort and
			 * exit the transaction block.
3017
			 */
3018 3019
		case TBLOCK_INPROGRESS:
			s->blockState = TBLOCK_ABORT_PENDING;
3020 3021
			break;

3022
			/*
3023
			 * We are inside a failed transaction block and we got a ROLLBACK
B
Bruce Momjian 已提交
3024 3025 3026
			 * command from the user.  Abort processing is already done, so
			 * CommitTransactionCommand just has to cleanup and go back to
			 * idle state.
3027
			 */
3028 3029
		case TBLOCK_ABORT:
			s->blockState = TBLOCK_ABORT_END;
3030 3031
			break;

3032
			/*
B
Bruce Momjian 已提交
3033 3034
			 * We are inside a subtransaction.	Mark everything up to top
			 * level as exitable.
3035
			 */
3036
		case TBLOCK_SUBINPROGRESS:
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055
		case TBLOCK_SUBABORT:
			while (s->parent != NULL)
			{
				if (s->blockState == TBLOCK_SUBINPROGRESS)
					s->blockState = TBLOCK_SUBABORT_PENDING;
				else if (s->blockState == TBLOCK_SUBABORT)
					s->blockState = TBLOCK_SUBABORT_END;
				else
					elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
						 BlockStateAsString(s->blockState));
				s = s->parent;
			}
			if (s->blockState == TBLOCK_INPROGRESS)
				s->blockState = TBLOCK_ABORT_PENDING;
			else if (s->blockState == TBLOCK_ABORT)
				s->blockState = TBLOCK_ABORT_END;
			else
				elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
					 BlockStateAsString(s->blockState));
3056 3057 3058
			break;

			/*
B
Bruce Momjian 已提交
3059 3060
			 * The user issued ABORT when not inside a transaction. Issue a
			 * WARNING and go to abort state.  The upcoming call to
3061 3062
			 * CommitTransactionCommand() will then put us back into the
			 * default state.
3063 3064 3065 3066 3067
			 */
		case TBLOCK_STARTED:
			ereport(WARNING,
					(errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
					 errmsg("there is no transaction in progress")));
3068
			s->blockState = TBLOCK_ABORT_PENDING;
3069 3070
			break;

3071
			/* These cases are invalid. */
3072 3073
		case TBLOCK_DEFAULT:
		case TBLOCK_BEGIN:
3074
		case TBLOCK_SUBBEGIN:
3075 3076
		case TBLOCK_END:
		case TBLOCK_SUBEND:
3077 3078 3079
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
3080
		case TBLOCK_SUBABORT_PENDING:
3081 3082
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
3083
		case TBLOCK_PREPARE:
3084 3085 3086
			elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
				 BlockStateAsString(s->blockState));
			break;
3087
	}
3088 3089 3090 3091 3092 3093 3094 3095 3096
}

/*
 * DefineSavepoint
 *		This executes a SAVEPOINT command.
 */
void
DefineSavepoint(char *name)
{
B
Bruce Momjian 已提交
3097
	TransactionState s = CurrentTransactionState;
3098 3099 3100 3101 3102 3103 3104

	switch (s->blockState)
	{
		case TBLOCK_INPROGRESS:
		case TBLOCK_SUBINPROGRESS:
			/* Normal subtransaction start */
			PushTransaction();
B
Bruce Momjian 已提交
3105 3106
			s = CurrentTransactionState;		/* changed by push */

3107
			/*
B
Bruce Momjian 已提交
3108 3109
			 * Savepoint names, like the TransactionState block itself, live
			 * in TopTransactionContext.
3110
			 */
3111 3112
			if (name)
				s->name = MemoryContextStrdup(TopTransactionContext, name);
3113 3114
			break;

3115
			/* These cases are invalid. */
3116 3117 3118 3119
		case TBLOCK_DEFAULT:
		case TBLOCK_STARTED:
		case TBLOCK_BEGIN:
		case TBLOCK_SUBBEGIN:
3120 3121
		case TBLOCK_END:
		case TBLOCK_SUBEND:
3122 3123
		case TBLOCK_ABORT:
		case TBLOCK_SUBABORT:
3124 3125 3126
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
3127
		case TBLOCK_SUBABORT_PENDING:
3128 3129
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
3130
		case TBLOCK_PREPARE:
T
Tom Lane 已提交
3131
			elog(FATAL, "DefineSavepoint: unexpected state %s",
3132 3133 3134 3135 3136 3137 3138
				 BlockStateAsString(s->blockState));
			break;
	}
}

/*
 * ReleaseSavepoint
B
Bruce Momjian 已提交
3139
 *		This executes a RELEASE command.
3140 3141
 *
 * As above, we don't actually do anything here except change blockState.
3142 3143 3144 3145
 */
void
ReleaseSavepoint(List *options)
{
B
Bruce Momjian 已提交
3146
	TransactionState s = CurrentTransactionState;
3147
	TransactionState target,
B
Bruce Momjian 已提交
3148 3149 3150
				xact;
	ListCell   *cell;
	char	   *name = NULL;
3151 3152 3153

	switch (s->blockState)
	{
3154 3155 3156 3157
			/*
			 * We can't rollback to a savepoint if there is no savepoint
			 * defined.
			 */
3158 3159 3160 3161 3162 3163 3164
		case TBLOCK_INPROGRESS:
			ereport(ERROR,
					(errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
					 errmsg("no such savepoint")));
			break;

			/*
B
Bruce Momjian 已提交
3165 3166
			 * We are in a non-aborted subtransaction.	This is the only valid
			 * case.
3167 3168 3169 3170
			 */
		case TBLOCK_SUBINPROGRESS:
			break;

3171
			/* These cases are invalid. */
3172 3173 3174 3175
		case TBLOCK_DEFAULT:
		case TBLOCK_STARTED:
		case TBLOCK_BEGIN:
		case TBLOCK_SUBBEGIN:
3176
		case TBLOCK_END:
3177
		case TBLOCK_SUBEND:
3178 3179 3180 3181 3182
		case TBLOCK_ABORT:
		case TBLOCK_SUBABORT:
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
3183
		case TBLOCK_SUBABORT_PENDING:
3184 3185
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
3186
		case TBLOCK_PREPARE:
3187 3188 3189 3190 3191
			elog(FATAL, "ReleaseSavepoint: unexpected state %s",
				 BlockStateAsString(s->blockState));
			break;
	}

B
Bruce Momjian 已提交
3192
	foreach(cell, options)
3193
	{
B
Bruce Momjian 已提交
3194
		DefElem    *elem = lfirst(cell);
3195 3196 3197 3198 3199 3200 3201

		if (strcmp(elem->defname, "savepoint_name") == 0)
			name = strVal(elem->arg);
	}

	Assert(PointerIsValid(name));

3202
	for (target = s; PointerIsValid(target); target = target->parent)
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212
	{
		if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
			break;
	}

	if (!PointerIsValid(target))
		ereport(ERROR,
				(errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
				 errmsg("no such savepoint")));

3213 3214 3215 3216 3217 3218 3219 3220
	/* disallow crossing savepoint level boundaries */
	if (target->savepointLevel != s->savepointLevel)
		ereport(ERROR,
				(errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
				 errmsg("no such savepoint")));

	/*
	 * Mark "commit pending" all subtransactions up to the target
B
Bruce Momjian 已提交
3221 3222
	 * subtransaction.	The actual commits will happen when control gets to
	 * CommitTransactionCommand.
3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233
	 */
	xact = CurrentTransactionState;
	for (;;)
	{
		Assert(xact->blockState == TBLOCK_SUBINPROGRESS);
		xact->blockState = TBLOCK_SUBEND;
		if (xact == target)
			break;
		xact = xact->parent;
		Assert(PointerIsValid(xact));
	}
3234 3235 3236 3237
}

/*
 * RollbackToSavepoint
B
Bruce Momjian 已提交
3238
 *		This executes a ROLLBACK TO <savepoint> command.
3239 3240
 *
 * As above, we don't actually do anything here except change blockState.
3241 3242 3243 3244 3245 3246
 */
void
RollbackToSavepoint(List *options)
{
	TransactionState s = CurrentTransactionState;
	TransactionState target,
B
Bruce Momjian 已提交
3247 3248 3249
				xact;
	ListCell   *cell;
	char	   *name = NULL;
3250 3251 3252

	switch (s->blockState)
	{
B
Bruce Momjian 已提交
3253
			/*
3254
			 * We can't rollback to a savepoint if there is no savepoint
B
Bruce Momjian 已提交
3255 3256
			 * defined.
			 */
3257
		case TBLOCK_INPROGRESS:
3258
		case TBLOCK_ABORT:
3259 3260 3261 3262 3263 3264 3265 3266 3267
			ereport(ERROR,
					(errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
					 errmsg("no such savepoint")));
			break;

			/*
			 * There is at least one savepoint, so proceed.
			 */
		case TBLOCK_SUBINPROGRESS:
3268
		case TBLOCK_SUBABORT:
3269 3270
			break;

3271
			/* These cases are invalid. */
3272 3273 3274
		case TBLOCK_DEFAULT:
		case TBLOCK_STARTED:
		case TBLOCK_BEGIN:
3275
		case TBLOCK_SUBBEGIN:
3276 3277
		case TBLOCK_END:
		case TBLOCK_SUBEND:
3278 3279 3280
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
3281
		case TBLOCK_SUBABORT_PENDING:
3282 3283
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
3284
		case TBLOCK_PREPARE:
3285 3286 3287 3288 3289
			elog(FATAL, "RollbackToSavepoint: unexpected state %s",
				 BlockStateAsString(s->blockState));
			break;
	}

B
Bruce Momjian 已提交
3290
	foreach(cell, options)
3291
	{
B
Bruce Momjian 已提交
3292
		DefElem    *elem = lfirst(cell);
3293 3294 3295 3296 3297 3298 3299

		if (strcmp(elem->defname, "savepoint_name") == 0)
			name = strVal(elem->arg);
	}

	Assert(PointerIsValid(name));

3300
	for (target = s; PointerIsValid(target); target = target->parent)
3301 3302 3303 3304 3305 3306 3307 3308 3309 3310
	{
		if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
			break;
	}

	if (!PointerIsValid(target))
		ereport(ERROR,
				(errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
				 errmsg("no such savepoint")));

3311 3312 3313 3314 3315 3316
	/* disallow crossing savepoint level boundaries */
	if (target->savepointLevel != s->savepointLevel)
		ereport(ERROR,
				(errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
				 errmsg("no such savepoint")));

3317 3318
	/*
	 * Mark "abort pending" all subtransactions up to the target
B
Bruce Momjian 已提交
3319 3320
	 * subtransaction.	The actual aborts will happen when control gets to
	 * CommitTransactionCommand.
3321 3322
	 */
	xact = CurrentTransactionState;
3323
	for (;;)
3324
	{
3325 3326 3327 3328 3329 3330 3331 3332 3333
		if (xact == target)
			break;
		if (xact->blockState == TBLOCK_SUBINPROGRESS)
			xact->blockState = TBLOCK_SUBABORT_PENDING;
		else if (xact->blockState == TBLOCK_SUBABORT)
			xact->blockState = TBLOCK_SUBABORT_END;
		else
			elog(FATAL, "RollbackToSavepoint: unexpected state %s",
				 BlockStateAsString(xact->blockState));
3334 3335 3336
		xact = xact->parent;
		Assert(PointerIsValid(xact));
	}
3337 3338 3339 3340 3341 3342 3343 3344 3345

	/* And mark the target as "restart pending" */
	if (xact->blockState == TBLOCK_SUBINPROGRESS)
		xact->blockState = TBLOCK_SUBRESTART;
	else if (xact->blockState == TBLOCK_SUBABORT)
		xact->blockState = TBLOCK_SUBABORT_RESTART;
	else
		elog(FATAL, "RollbackToSavepoint: unexpected state %s",
			 BlockStateAsString(xact->blockState));
3346 3347 3348
}

/*
T
Tom Lane 已提交
3349 3350 3351 3352 3353 3354 3355 3356 3357 3358
 * BeginInternalSubTransaction
 *		This is the same as DefineSavepoint except it allows TBLOCK_STARTED
 *		state, and therefore it can safely be used in a function that might
 *		be called when not inside a BEGIN block.  Also, we automatically
 *		cycle through CommitTransactionCommand/StartTransactionCommand
 *		instead of expecting the caller to do it.
 */
void
BeginInternalSubTransaction(char *name)
{
B
Bruce Momjian 已提交
3359
	TransactionState s = CurrentTransactionState;
T
Tom Lane 已提交
3360 3361 3362 3363 3364 3365 3366 3367

	switch (s->blockState)
	{
		case TBLOCK_STARTED:
		case TBLOCK_INPROGRESS:
		case TBLOCK_SUBINPROGRESS:
			/* Normal subtransaction start */
			PushTransaction();
B
Bruce Momjian 已提交
3368 3369
			s = CurrentTransactionState;		/* changed by push */

T
Tom Lane 已提交
3370
			/*
B
Bruce Momjian 已提交
3371 3372
			 * Savepoint names, like the TransactionState block itself, live
			 * in TopTransactionContext.
T
Tom Lane 已提交
3373 3374
			 */
			if (name)
3375
				s->name = MemoryContextStrdup(TopTransactionContext, name);
T
Tom Lane 已提交
3376 3377
			break;

3378
			/* These cases are invalid. */
T
Tom Lane 已提交
3379 3380 3381
		case TBLOCK_DEFAULT:
		case TBLOCK_BEGIN:
		case TBLOCK_SUBBEGIN:
3382 3383
		case TBLOCK_END:
		case TBLOCK_SUBEND:
T
Tom Lane 已提交
3384 3385
		case TBLOCK_ABORT:
		case TBLOCK_SUBABORT:
3386 3387 3388
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
T
Tom Lane 已提交
3389
		case TBLOCK_SUBABORT_PENDING:
3390 3391
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
3392
		case TBLOCK_PREPARE:
T
Tom Lane 已提交
3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
			elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
				 BlockStateAsString(s->blockState));
			break;
	}

	CommitTransactionCommand();
	StartTransactionCommand();
}

/*
 * ReleaseCurrentSubTransaction
 *
 * RELEASE (ie, commit) the innermost subtransaction, regardless of its
 * savepoint name (if any).
 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
 */
void
ReleaseCurrentSubTransaction(void)
{
	TransactionState s = CurrentTransactionState;

	if (s->blockState != TBLOCK_SUBINPROGRESS)
		elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
			 BlockStateAsString(s->blockState));
3417
	Assert(s->state == TRANS_INPROGRESS);
T
Tom Lane 已提交
3418
	MemoryContextSwitchTo(CurTransactionContext);
3419
	CommitSubTransaction();
B
Bruce Momjian 已提交
3420
	s = CurrentTransactionState;	/* changed by pop */
3421
	Assert(s->state == TRANS_INPROGRESS);
T
Tom Lane 已提交
3422 3423 3424 3425
}

/*
 * RollbackAndReleaseCurrentSubTransaction
3426
 *
T
Tom Lane 已提交
3427 3428 3429
 * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
 * of its savepoint name (if any).
 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3430 3431
 */
void
T
Tom Lane 已提交
3432
RollbackAndReleaseCurrentSubTransaction(void)
3433
{
T
Tom Lane 已提交
3434
	TransactionState s = CurrentTransactionState;
3435

T
Tom Lane 已提交
3436 3437
	switch (s->blockState)
	{
B
Bruce Momjian 已提交
3438
			/* Must be in a subtransaction */
T
Tom Lane 已提交
3439
		case TBLOCK_SUBINPROGRESS:
3440
		case TBLOCK_SUBABORT:
T
Tom Lane 已提交
3441 3442
			break;

3443
			/* These cases are invalid. */
T
Tom Lane 已提交
3444 3445 3446
		case TBLOCK_DEFAULT:
		case TBLOCK_STARTED:
		case TBLOCK_BEGIN:
3447 3448
		case TBLOCK_SUBBEGIN:
		case TBLOCK_INPROGRESS:
T
Tom Lane 已提交
3449 3450
		case TBLOCK_END:
		case TBLOCK_SUBEND:
3451 3452 3453 3454
		case TBLOCK_ABORT:
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
T
Tom Lane 已提交
3455
		case TBLOCK_SUBABORT_PENDING:
3456 3457
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
3458
		case TBLOCK_PREPARE:
T
Tom Lane 已提交
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
			elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
				 BlockStateAsString(s->blockState));
			break;
	}

	/*
	 * Abort the current subtransaction, if needed.
	 */
	if (s->blockState == TBLOCK_SUBINPROGRESS)
		AbortSubTransaction();

	/* And clean it up, too */
3471 3472 3473 3474 3475 3476
	CleanupSubTransaction();

	s = CurrentTransactionState;	/* changed by pop */
	AssertState(s->blockState == TBLOCK_SUBINPROGRESS ||
				s->blockState == TBLOCK_INPROGRESS ||
				s->blockState == TBLOCK_STARTED);
3477 3478
}

3479
/*
3480
 *	AbortOutOfAnyTransaction
3481
 *
3482 3483 3484
 *	This routine is provided for error recovery purposes.  It aborts any
 *	active transaction or transaction block, leaving the system in a known
 *	idle state.
3485 3486
 */
void
3487
AbortOutOfAnyTransaction(void)
3488 3489 3490 3491
{
	TransactionState s = CurrentTransactionState;

	/*
3492
	 * Get out of any transaction or nested transaction
3493
	 */
B
Bruce Momjian 已提交
3494 3495
	do
	{
3496 3497 3498 3499 3500 3501 3502 3503 3504
		switch (s->blockState)
		{
			case TBLOCK_DEFAULT:
				/* Not in a transaction, do nothing */
				break;
			case TBLOCK_STARTED:
			case TBLOCK_BEGIN:
			case TBLOCK_INPROGRESS:
			case TBLOCK_END:
3505
			case TBLOCK_ABORT_PENDING:
3506
			case TBLOCK_PREPARE:
3507 3508 3509 3510 3511 3512
				/* In a transaction, so clean up */
				AbortTransaction();
				CleanupTransaction();
				s->blockState = TBLOCK_DEFAULT;
				break;
			case TBLOCK_ABORT:
3513
			case TBLOCK_ABORT_END:
3514 3515 3516 3517
				/* AbortTransaction already done, still need Cleanup */
				CleanupTransaction();
				s->blockState = TBLOCK_DEFAULT;
				break;
B
Bruce Momjian 已提交
3518

3519
				/*
B
Bruce Momjian 已提交
3520
				 * In a subtransaction, so clean it up and abort parent too
3521
				 */
3522
			case TBLOCK_SUBBEGIN:
3523 3524
			case TBLOCK_SUBINPROGRESS:
			case TBLOCK_SUBEND:
3525
			case TBLOCK_SUBABORT_PENDING:
3526
			case TBLOCK_SUBRESTART:
3527 3528
				AbortSubTransaction();
				CleanupSubTransaction();
B
Bruce Momjian 已提交
3529
				s = CurrentTransactionState;	/* changed by pop */
3530
				break;
3531

3532
			case TBLOCK_SUBABORT:
3533 3534
			case TBLOCK_SUBABORT_END:
			case TBLOCK_SUBABORT_RESTART:
3535
				/* As above, but AbortSubTransaction already done */
3536
				CleanupSubTransaction();
B
Bruce Momjian 已提交
3537
				s = CurrentTransactionState;	/* changed by pop */
3538 3539 3540
				break;
		}
	} while (s->blockState != TBLOCK_DEFAULT);
B
Bruce Momjian 已提交
3541

3542 3543
	/* Should be out of all subxacts now */
	Assert(s->parent == NULL);
3544 3545
}

3546 3547 3548
/*
 * IsTransactionBlock --- are we within a transaction block?
 */
3549
bool
3550
IsTransactionBlock(void)
3551
{
3552 3553
	TransactionState s = CurrentTransactionState;

3554
	if (s->blockState == TBLOCK_DEFAULT || s->blockState == TBLOCK_STARTED)
3555
		return false;
3556

3557 3558 3559
	return true;
}

3560 3561
/*
 * IsTransactionOrTransactionBlock --- are we within either a transaction
B
Bruce Momjian 已提交
3562
 * or a transaction block?	(The backend is only really "idle" when this
3563 3564 3565 3566 3567 3568 3569 3570 3571
 * returns false.)
 *
 * This should match up with IsTransactionBlock and IsTransactionState.
 */
bool
IsTransactionOrTransactionBlock(void)
{
	TransactionState s = CurrentTransactionState;

3572
	if (s->blockState == TBLOCK_DEFAULT)
3573 3574 3575 3576 3577
		return false;

	return true;
}

3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588
/*
 * TransactionBlockStatusCode - return status code to send in ReadyForQuery
 */
char
TransactionBlockStatusCode(void)
{
	TransactionState s = CurrentTransactionState;

	switch (s->blockState)
	{
		case TBLOCK_DEFAULT:
3589
		case TBLOCK_STARTED:
3590 3591
			return 'I';			/* idle --- not in transaction */
		case TBLOCK_BEGIN:
3592
		case TBLOCK_SUBBEGIN:
3593
		case TBLOCK_INPROGRESS:
3594
		case TBLOCK_SUBINPROGRESS:
3595
		case TBLOCK_END:
3596
		case TBLOCK_SUBEND:
3597
		case TBLOCK_PREPARE:
3598 3599
			return 'T';			/* in transaction */
		case TBLOCK_ABORT:
3600
		case TBLOCK_SUBABORT:
3601 3602 3603
		case TBLOCK_ABORT_END:
		case TBLOCK_SUBABORT_END:
		case TBLOCK_ABORT_PENDING:
3604
		case TBLOCK_SUBABORT_PENDING:
3605 3606
		case TBLOCK_SUBRESTART:
		case TBLOCK_SUBABORT_RESTART:
3607 3608 3609 3610
			return 'E';			/* in failed transaction */
	}

	/* should never get here */
3611 3612
	elog(FATAL, "invalid transaction block state: %s",
		 BlockStateAsString(s->blockState));
3613
	return 0;					/* keep compiler quiet */
3614
}
3615

3616 3617 3618 3619 3620 3621 3622 3623
/*
 * IsSubTransaction
 */
bool
IsSubTransaction(void)
{
	TransactionState s = CurrentTransactionState;

3624 3625 3626 3627
	if (s->nestingLevel >= 2)
		return true;

	return false;
3628 3629 3630 3631
}

/*
 * StartSubTransaction
3632 3633 3634 3635 3636 3637 3638 3639 3640
 *
 * If you're wondering why this is separate from PushTransaction: it's because
 * we can't conveniently do this stuff right inside DefineSavepoint.  The
 * SAVEPOINT utility command will be executed inside a Portal, and if we
 * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
 * the Portal will undo those settings.  So we make DefineSavepoint just
 * push a dummy transaction block, and when control returns to the main
 * idle loop, CommitTransactionCommand will be called, and we'll come here
 * to finish starting the subtransaction.
3641 3642 3643 3644 3645 3646 3647
 */
static void
StartSubTransaction(void)
{
	TransactionState s = CurrentTransactionState;

	if (s->state != TRANS_DEFAULT)
3648 3649
		elog(WARNING, "StartSubTransaction while in %s state",
			 TransStateAsString(s->state));
3650 3651 3652

	s->state = TRANS_START;

3653
	/*
3654 3655
	 * Initialize subsystems for new subtransaction
	 *
3656 3657 3658 3659
	 * must initialize resource-management stuff first
	 */
	AtSubStart_Memory();
	AtSubStart_ResourceOwner();
3660 3661
	AtSubStart_Inval();
	AtSubStart_Notify();
3662
	AfterTriggerBeginSubXact();
3663 3664 3665

	s->state = TRANS_INPROGRESS;

3666
	/*
B
Bruce Momjian 已提交
3667
	 * Call start-of-subxact callbacks
3668
	 */
3669 3670
	CallSubXactCallbacks(SUBXACT_EVENT_START_SUB, s->subTransactionId,
						 s->parent->subTransactionId);
3671

3672 3673 3674 3675 3676
	ShowTransactionState("StartSubTransaction");
}

/*
 * CommitSubTransaction
3677 3678 3679
 *
 *	The caller has to make sure to always reassign CurrentTransactionState
 *	if it has a local pointer to it after calling this function.
3680 3681 3682 3683 3684 3685 3686 3687 3688
 */
static void
CommitSubTransaction(void)
{
	TransactionState s = CurrentTransactionState;

	ShowTransactionState("CommitSubTransaction");

	if (s->state != TRANS_INPROGRESS)
3689 3690
		elog(WARNING, "CommitSubTransaction while in %s state",
			 TransStateAsString(s->state));
3691

3692
	/* Pre-commit processing goes here -- nothing to do at the moment */
3693

3694 3695
	s->state = TRANS_COMMIT;

3696
	/* Must CCI to ensure commands of subtransaction are seen as done */
3697
	CommandCounterIncrement();
3698 3699

	/* Mark subtransaction as subcommitted */
3700 3701 3702 3703 3704
	if (TransactionIdIsValid(s->transactionId))
	{
		RecordSubTransactionCommit();
		AtSubCommit_childXids();
	}
3705 3706

	/* Post-commit cleanup */
3707
	AfterTriggerEndSubXact(true);
3708 3709
	AtSubCommit_Portals(s->subTransactionId,
						s->parent->subTransactionId,
3710
						s->parent->curTransactionOwner);
3711 3712
	AtEOSubXact_LargeObject(true, s->subTransactionId,
							s->parent->subTransactionId);
3713
	AtSubCommit_Notify();
3714 3715
	AtEOSubXact_UpdateFlatFiles(true, s->subTransactionId,
								s->parent->subTransactionId);
3716

3717 3718
	CallSubXactCallbacks(SUBXACT_EVENT_COMMIT_SUB, s->subTransactionId,
						 s->parent->subTransactionId);
3719

3720 3721 3722
	ResourceOwnerRelease(s->curTransactionOwner,
						 RESOURCE_RELEASE_BEFORE_LOCKS,
						 true, false);
3723 3724
	AtEOSubXact_RelationCache(true, s->subTransactionId,
							  s->parent->subTransactionId);
3725
	AtEOSubXact_Inval(true);
3726
	AtSubCommit_smgr();
3727 3728 3729 3730 3731 3732 3733 3734 3735

	/*
	 * The only lock we actually release here is the subtransaction XID lock.
	 * The rest just get transferred to the parent resource owner.
	 */
	CurrentResourceOwner = s->curTransactionOwner;
	if (TransactionIdIsValid(s->transactionId))
		XactLockTableDelete(s->transactionId);

3736 3737 3738
	ResourceOwnerRelease(s->curTransactionOwner,
						 RESOURCE_RELEASE_LOCKS,
						 true, false);
3739 3740 3741 3742
	ResourceOwnerRelease(s->curTransactionOwner,
						 RESOURCE_RELEASE_AFTER_LOCKS,
						 true, false);

3743
	AtEOXact_GUC(true, true);
3744 3745 3746 3747 3748 3749 3750
	AtEOSubXact_SPI(true, s->subTransactionId);
	AtEOSubXact_on_commit_actions(true, s->subTransactionId,
								  s->parent->subTransactionId);
	AtEOSubXact_Namespace(true, s->subTransactionId,
						  s->parent->subTransactionId);
	AtEOSubXact_Files(true, s->subTransactionId,
					  s->parent->subTransactionId);
3751
	AtEOSubXact_HashTables(true, s->nestingLevel);
3752

3753
	/*
B
Bruce Momjian 已提交
3754 3755 3756
	 * We need to restore the upper transaction's read-only state, in case the
	 * upper is read-write while the child is read-only; GUC will incorrectly
	 * think it should leave the child state in place.
3757 3758 3759
	 */
	XactReadOnly = s->prevXactReadOnly;

3760 3761
	CurrentResourceOwner = s->parent->curTransactionOwner;
	CurTransactionResourceOwner = s->parent->curTransactionOwner;
3762
	ResourceOwnerDelete(s->curTransactionOwner);
3763 3764
	s->curTransactionOwner = NULL;

3765 3766 3767
	AtSubCommit_Memory();

	s->state = TRANS_DEFAULT;
3768 3769

	PopTransaction();
3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
}

/*
 * AbortSubTransaction
 */
static void
AbortSubTransaction(void)
{
	TransactionState s = CurrentTransactionState;

3780
	/* Prevent cancel/die interrupt while cleaning up */
3781 3782
	HOLD_INTERRUPTS();

3783 3784 3785
	/* Make sure we have a valid memory context and resource owner */
	AtSubAbort_Memory();
	AtSubAbort_ResourceOwner();
3786 3787 3788 3789

	/*
	 * Release any LW locks we might be holding as quickly as possible.
	 * (Regular locks, however, must be held till we finish aborting.)
B
Bruce Momjian 已提交
3790 3791
	 * Releasing LW locks is critical since we might try to grab them again
	 * while cleaning up!
3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803
	 *
	 * FIXME This may be incorrect --- Are there some locks we should keep?
	 * Buffer locks, for example?  I don't think so but I'm not sure.
	 */
	LWLockReleaseAll();

	AbortBufferIO();
	UnlockBuffers();

	LockWaitCancel();

	/*
3804
	 * check the current transaction state
3805
	 */
3806 3807 3808 3809 3810 3811 3812
	ShowTransactionState("AbortSubTransaction");

	if (s->state != TRANS_INPROGRESS)
		elog(WARNING, "AbortSubTransaction while in %s state",
			 TransStateAsString(s->state));

	s->state = TRANS_ABORT;
3813

3814
	/*
B
Bruce Momjian 已提交
3815 3816
	 * We can skip all this stuff if the subxact failed before creating a
	 * ResourceOwner...
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826
	 */
	if (s->curTransactionOwner)
	{
		AfterTriggerEndSubXact(false);
		AtSubAbort_Portals(s->subTransactionId,
						   s->parent->subTransactionId,
						   s->parent->curTransactionOwner);
		AtEOSubXact_LargeObject(false, s->subTransactionId,
								s->parent->subTransactionId);
		AtSubAbort_Notify();
3827 3828
		AtEOSubXact_UpdateFlatFiles(false, s->subTransactionId,
									s->parent->subTransactionId);
3829 3830 3831

		/* Advertise the fact that we aborted in pg_clog. */
		if (TransactionIdIsValid(s->transactionId))
3832
		{
3833
			RecordSubTransactionAbort();
3834 3835
			AtSubAbort_childXids();
		}
3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862

		/* Post-abort cleanup */
		CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
							 s->parent->subTransactionId);

		ResourceOwnerRelease(s->curTransactionOwner,
							 RESOURCE_RELEASE_BEFORE_LOCKS,
							 false, false);
		AtEOSubXact_RelationCache(false, s->subTransactionId,
								  s->parent->subTransactionId);
		AtEOSubXact_Inval(false);
		AtSubAbort_smgr();
		ResourceOwnerRelease(s->curTransactionOwner,
							 RESOURCE_RELEASE_LOCKS,
							 false, false);
		ResourceOwnerRelease(s->curTransactionOwner,
							 RESOURCE_RELEASE_AFTER_LOCKS,
							 false, false);

		AtEOXact_GUC(false, true);
		AtEOSubXact_SPI(false, s->subTransactionId);
		AtEOSubXact_on_commit_actions(false, s->subTransactionId,
									  s->parent->subTransactionId);
		AtEOSubXact_Namespace(false, s->subTransactionId,
							  s->parent->subTransactionId);
		AtEOSubXact_Files(false, s->subTransactionId,
						  s->parent->subTransactionId);
3863
		AtEOSubXact_HashTables(false, s->nestingLevel);
3864
	}
3865 3866

	/*
B
Bruce Momjian 已提交
3867 3868 3869
	 * Reset user id which might have been changed transiently.  Here we want
	 * to restore to the userid that was current at subxact entry. (As in
	 * AbortTransaction, we need not worry about the session userid.)
3870
	 *
B
Bruce Momjian 已提交
3871 3872 3873 3874 3875 3876
	 * Must do this after AtEOXact_GUC to handle the case where we entered the
	 * subxact inside a SECURITY DEFINER function (hence current and session
	 * userids were different) and then session auth was changed inside the
	 * subxact.  GUC will reset both current and session userids to the
	 * entry-time session userid.  This is right in every other scenario so it
	 * seems simplest to let GUC do that and fix it here.
3877 3878 3879
	 */
	SetUserId(s->currentUser);

3880
	/*
B
Bruce Momjian 已提交
3881 3882 3883
	 * Restore the upper transaction's read-only state, too.  This should be
	 * redundant with GUC's cleanup but we may as well do it for consistency
	 * with the commit case.
3884 3885 3886
	 */
	XactReadOnly = s->prevXactReadOnly;

3887 3888 3889 3890 3891
	RESUME_INTERRUPTS();
}

/*
 * CleanupSubTransaction
3892 3893 3894
 *
 *	The caller has to make sure to always reassign CurrentTransactionState
 *	if it has a local pointer to it after calling this function.
3895 3896 3897 3898 3899 3900 3901 3902 3903
 */
static void
CleanupSubTransaction(void)
{
	TransactionState s = CurrentTransactionState;

	ShowTransactionState("CleanupSubTransaction");

	if (s->state != TRANS_ABORT)
3904 3905
		elog(WARNING, "CleanupSubTransaction while in %s state",
			 TransStateAsString(s->state));
3906

3907
	AtSubCleanup_Portals(s->subTransactionId);
3908 3909 3910

	CurrentResourceOwner = s->parent->curTransactionOwner;
	CurTransactionResourceOwner = s->parent->curTransactionOwner;
3911 3912
	if (s->curTransactionOwner)
		ResourceOwnerDelete(s->curTransactionOwner);
3913 3914
	s->curTransactionOwner = NULL;

3915 3916 3917
	AtSubCleanup_Memory();

	s->state = TRANS_DEFAULT;
3918

3919
	PopTransaction();
3920 3921
}

3922 3923
/*
 * PushTransaction
3924
 *		Create transaction state stack entry for a subtransaction
3925 3926 3927
 *
 *	The caller has to make sure to always reassign CurrentTransactionState
 *	if it has a local pointer to it after calling this function.
3928 3929 3930 3931
 */
static void
PushTransaction(void)
{
B
Bruce Momjian 已提交
3932 3933
	TransactionState p = CurrentTransactionState;
	TransactionState s;
B
Bruce Momjian 已提交
3934
	Oid			currentUser;
3935 3936

	/*
B
Bruce Momjian 已提交
3937 3938
	 * At present, GetUserId cannot fail, but let's not assume that.  Get the
	 * ID before entering the critical code sequence.
3939 3940
	 */
	currentUser = GetUserId();
3941 3942 3943 3944 3945 3946 3947

	/*
	 * We keep subtransaction state nodes in TopTransactionContext.
	 */
	s = (TransactionState)
		MemoryContextAllocZero(TopTransactionContext,
							   sizeof(TransactionStateData));
B
Bruce Momjian 已提交
3948

3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960
	/*
	 * Assign a subtransaction ID, watching out for counter wraparound.
	 */
	currentSubTransactionId += 1;
	if (currentSubTransactionId == InvalidSubTransactionId)
	{
		currentSubTransactionId -= 1;
		pfree(s);
		ereport(ERROR,
				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
				 errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
	}
B
Bruce Momjian 已提交
3961

3962 3963 3964 3965
	/*
	 * We can now stack a minimally valid subtransaction without fear of
	 * failure.
	 */
B
Bruce Momjian 已提交
3966
	s->transactionId = InvalidTransactionId;	/* until assigned */
3967
	s->subTransactionId = currentSubTransactionId;
3968 3969
	s->parent = p;
	s->nestingLevel = p->nestingLevel + 1;
3970
	s->savepointLevel = p->savepointLevel;
3971 3972
	s->state = TRANS_DEFAULT;
	s->blockState = TBLOCK_SUBBEGIN;
3973 3974
	s->currentUser = currentUser;
	s->prevXactReadOnly = XactReadOnly;
3975

3976
	CurrentTransactionState = s;
3977 3978

	/*
B
Bruce Momjian 已提交
3979 3980 3981 3982
	 * AbortSubTransaction and CleanupSubTransaction have to be able to cope
	 * with the subtransaction from here on out; in particular they should not
	 * assume that it necessarily has a transaction context, resource owner,
	 * or XID.
3983 3984 3985 3986 3987 3988
	 */
}

/*
 * PopTransaction
 *		Pop back to parent transaction state
3989 3990 3991
 *
 *	The caller has to make sure to always reassign CurrentTransactionState
 *	if it has a local pointer to it after calling this function.
3992 3993 3994 3995 3996 3997 3998
 */
static void
PopTransaction(void)
{
	TransactionState s = CurrentTransactionState;

	if (s->state != TRANS_DEFAULT)
3999 4000
		elog(WARNING, "PopTransaction while in %s state",
			 TransStateAsString(s->state));
4001 4002 4003 4004 4005 4006 4007 4008 4009 4010

	if (s->parent == NULL)
		elog(FATAL, "PopTransaction with no parent");

	CurrentTransactionState = s->parent;

	/* Let's just make sure CurTransactionContext is good */
	CurTransactionContext = s->parent->curTransactionContext;
	MemoryContextSwitchTo(CurTransactionContext);

4011 4012 4013 4014
	/* Ditto for ResourceOwner links */
	CurTransactionResourceOwner = s->parent->curTransactionOwner;
	CurrentResourceOwner = s->parent->curTransactionOwner;

4015
	/* Free the old child structure */
4016 4017
	if (s->name)
		pfree(s->name);
4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028
	pfree(s);
}

/*
 * ShowTransactionState
 *		Debug support
 */
static void
ShowTransactionState(const char *str)
{
	/* skip work if message will definitely not be printed */
4029
	if (log_min_messages <= DEBUG3 || client_min_messages <= DEBUG3)
4030
	{
4031
		elog(DEBUG3, "%s", str);
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046
		ShowTransactionStateRec(CurrentTransactionState);
	}
}

/*
 * ShowTransactionStateRec
 *		Recursive subroutine for ShowTransactionState
 */
static void
ShowTransactionStateRec(TransactionState s)
{
	if (s->parent)
		ShowTransactionStateRec(s->parent);

	/* use ereport to suppress computation if msg will not be printed */
4047
	ereport(DEBUG3,
4048
			(errmsg_internal("name: %s; blockState: %13s; state: %7s, xid/subid/cid: %u/%u/%u, nestlvl: %d, children: %s",
B
Bruce Momjian 已提交
4049
							 PointerIsValid(s->name) ? s->name : "unnamed",
4050 4051
							 BlockStateAsString(s->blockState),
							 TransStateAsString(s->state),
4052 4053 4054
							 (unsigned int) s->transactionId,
							 (unsigned int) s->subTransactionId,
							 (unsigned int) currentCommandId,
4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065
							 s->nestingLevel,
							 nodeToString(s->childXids))));
}

/*
 * BlockStateAsString
 *		Debug support
 */
static const char *
BlockStateAsString(TBlockState blockState)
{
4066 4067
	switch (blockState)
	{
4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
		case TBLOCK_DEFAULT:
			return "DEFAULT";
		case TBLOCK_STARTED:
			return "STARTED";
		case TBLOCK_BEGIN:
			return "BEGIN";
		case TBLOCK_INPROGRESS:
			return "INPROGRESS";
		case TBLOCK_END:
			return "END";
		case TBLOCK_ABORT:
			return "ABORT";
4080 4081 4082 4083
		case TBLOCK_ABORT_END:
			return "ABORT END";
		case TBLOCK_ABORT_PENDING:
			return "ABORT PEND";
4084 4085
		case TBLOCK_PREPARE:
			return "PREPARE";
4086 4087 4088 4089 4090 4091 4092 4093
		case TBLOCK_SUBBEGIN:
			return "SUB BEGIN";
		case TBLOCK_SUBINPROGRESS:
			return "SUB INPROGRS";
		case TBLOCK_SUBEND:
			return "SUB END";
		case TBLOCK_SUBABORT:
			return "SUB ABORT";
4094 4095
		case TBLOCK_SUBABORT_END:
			return "SUB ABORT END";
4096 4097
		case TBLOCK_SUBABORT_PENDING:
			return "SUB ABRT PEND";
4098 4099 4100 4101
		case TBLOCK_SUBRESTART:
			return "SUB RESTART";
		case TBLOCK_SUBABORT_RESTART:
			return "SUB AB RESTRT";
4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112
	}
	return "UNRECOGNIZED";
}

/*
 * TransStateAsString
 *		Debug support
 */
static const char *
TransStateAsString(TransState state)
{
4113 4114
	switch (state)
	{
4115 4116 4117 4118
		case TRANS_DEFAULT:
			return "DEFAULT";
		case TRANS_START:
			return "START";
4119 4120
		case TRANS_INPROGRESS:
			return "INPROGR";
4121 4122 4123 4124
		case TRANS_COMMIT:
			return "COMMIT";
		case TRANS_ABORT:
			return "ABORT";
4125 4126
		case TRANS_PREPARE:
			return "PREPARE";
4127 4128 4129 4130 4131 4132 4133
	}
	return "UNRECOGNIZED";
}

/*
 * xactGetCommittedChildren
 *
B
Bruce Momjian 已提交
4134
 * Gets the list of committed children of the current transaction.	The return
4135 4136 4137 4138 4139
 * value is the number of child transactions.  *children is set to point to a
 * palloc'd array of TransactionIds.  If there are no subxacts, *children is
 * set to NULL.
 */
int
4140
xactGetCommittedChildren(TransactionId **ptr)
4141
{
B
Bruce Momjian 已提交
4142 4143 4144 4145
	TransactionState s = CurrentTransactionState;
	int			nchildren;
	TransactionId *children;
	ListCell   *p;
4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158

	nchildren = list_length(s->childXids);
	if (nchildren == 0)
	{
		*ptr = NULL;
		return 0;
	}

	children = (TransactionId *) palloc(nchildren * sizeof(TransactionId));
	*ptr = children;

	foreach(p, s->childXids)
	{
4159
		TransactionId child = lfirst_xid(p);
4160 4161

		*children++ = child;
4162 4163 4164 4165
	}

	return nchildren;
}
4166 4167 4168 4169 4170

/*
 *	XLOG support routines
 */

4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200
static void
xact_redo_commit(xl_xact_commit *xlrec, TransactionId xid)
{
	TransactionId *sub_xids;
	TransactionId max_xid;
	int			i;

	TransactionIdCommit(xid);

	/* Mark committed subtransactions as committed */
	sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
	TransactionIdCommitTree(xlrec->nsubxacts, sub_xids);

	/* Make sure nextXid is beyond any XID mentioned in the record */
	max_xid = xid;
	for (i = 0; i < xlrec->nsubxacts; i++)
	{
		if (TransactionIdPrecedes(max_xid, sub_xids[i]))
			max_xid = sub_xids[i];
	}
	if (TransactionIdFollowsOrEquals(max_xid,
									 ShmemVariableCache->nextXid))
	{
		ShmemVariableCache->nextXid = max_xid;
		TransactionIdAdvance(ShmemVariableCache->nextXid);
	}

	/* Make sure files supposed to be dropped are dropped */
	for (i = 0; i < xlrec->nrels; i++)
	{
4201
		XLogDropRelation(xlrec->xnodes[i]);
4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235
		smgrdounlink(smgropen(xlrec->xnodes[i]), false, true);
	}
}

static void
xact_redo_abort(xl_xact_abort *xlrec, TransactionId xid)
{
	TransactionId *sub_xids;
	TransactionId max_xid;
	int			i;

	TransactionIdAbort(xid);

	/* Mark subtransactions as aborted */
	sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
	TransactionIdAbortTree(xlrec->nsubxacts, sub_xids);

	/* Make sure nextXid is beyond any XID mentioned in the record */
	max_xid = xid;
	for (i = 0; i < xlrec->nsubxacts; i++)
	{
		if (TransactionIdPrecedes(max_xid, sub_xids[i]))
			max_xid = sub_xids[i];
	}
	if (TransactionIdFollowsOrEquals(max_xid,
									 ShmemVariableCache->nextXid))
	{
		ShmemVariableCache->nextXid = max_xid;
		TransactionIdAdvance(ShmemVariableCache->nextXid);
	}

	/* Make sure files supposed to be dropped are dropped */
	for (i = 0; i < xlrec->nrels; i++)
	{
4236
		XLogDropRelation(xlrec->xnodes[i]);
4237 4238 4239 4240
		smgrdounlink(smgropen(xlrec->xnodes[i]), false, true);
	}
}

4241 4242 4243
void
xact_redo(XLogRecPtr lsn, XLogRecord *record)
{
B
Bruce Momjian 已提交
4244
	uint8		info = record->xl_info & ~XLR_INFO_MASK;
4245 4246 4247

	if (info == XLOG_XACT_COMMIT)
	{
4248 4249
		xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);

4250 4251 4252 4253 4254
		xact_redo_commit(xlrec, record->xl_xid);
	}
	else if (info == XLOG_XACT_ABORT)
	{
		xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
4255

4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266
		xact_redo_abort(xlrec, record->xl_xid);
	}
	else if (info == XLOG_XACT_PREPARE)
	{
		/* the record contents are exactly the 2PC file */
		RecreateTwoPhaseFile(record->xl_xid,
							 XLogRecGetData(record), record->xl_len);
	}
	else if (info == XLOG_XACT_COMMIT_PREPARED)
	{
		xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) XLogRecGetData(record);
4267

4268 4269 4270 4271 4272 4273
		xact_redo_commit(&xlrec->crec, xlrec->xid);
		RemoveTwoPhaseFile(xlrec->xid, false);
	}
	else if (info == XLOG_XACT_ABORT_PREPARED)
	{
		xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) XLogRecGetData(record);
4274

4275 4276 4277 4278 4279 4280 4281 4282
		xact_redo_abort(&xlrec->arec, xlrec->xid);
		RemoveTwoPhaseFile(xlrec->xid, false);
	}
	else
		elog(PANIC, "xact_redo: unknown op code %u", info);
}

static void
4283
xact_desc_commit(StringInfo buf, xl_xact_commit *xlrec)
4284 4285 4286 4287
{
	struct tm  *tm = localtime(&xlrec->xtime);
	int			i;

4288
	appendStringInfo(buf, "%04u-%02u-%02u %02u:%02u:%02u",
B
Bruce Momjian 已提交
4289 4290
					 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
					 tm->tm_hour, tm->tm_min, tm->tm_sec);
4291 4292
	if (xlrec->nrels > 0)
	{
4293
		appendStringInfo(buf, "; rels:");
4294
		for (i = 0; i < xlrec->nrels; i++)
4295
		{
4296 4297
			RelFileNode rnode = xlrec->xnodes[i];

4298
			appendStringInfo(buf, " %u/%u/%u",
B
Bruce Momjian 已提交
4299
							 rnode.spcNode, rnode.dbNode, rnode.relNode);
4300
		}
4301
	}
4302
	if (xlrec->nsubxacts > 0)
4303
	{
4304
		TransactionId *xacts = (TransactionId *)
B
Bruce Momjian 已提交
4305
		&xlrec->xnodes[xlrec->nrels];
4306

4307
		appendStringInfo(buf, "; subxacts:");
4308
		for (i = 0; i < xlrec->nsubxacts; i++)
4309
			appendStringInfo(buf, " %u", xacts[i]);
4310 4311 4312 4313
	}
}

static void
4314
xact_desc_abort(StringInfo buf, xl_xact_abort *xlrec)
4315 4316 4317
{
	struct tm  *tm = localtime(&xlrec->xtime);
	int			i;
4318

4319
	appendStringInfo(buf, "%04u-%02u-%02u %02u:%02u:%02u",
B
Bruce Momjian 已提交
4320 4321
					 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
					 tm->tm_hour, tm->tm_min, tm->tm_sec);
4322 4323
	if (xlrec->nrels > 0)
	{
4324
		appendStringInfo(buf, "; rels:");
4325
		for (i = 0; i < xlrec->nrels; i++)
4326
		{
4327 4328
			RelFileNode rnode = xlrec->xnodes[i];

4329
			appendStringInfo(buf, " %u/%u/%u",
B
Bruce Momjian 已提交
4330
							 rnode.spcNode, rnode.dbNode, rnode.relNode);
4331
		}
4332
	}
4333 4334 4335
	if (xlrec->nsubxacts > 0)
	{
		TransactionId *xacts = (TransactionId *)
B
Bruce Momjian 已提交
4336
		&xlrec->xnodes[xlrec->nrels];
4337

4338
		appendStringInfo(buf, "; subxacts:");
4339
		for (i = 0; i < xlrec->nsubxacts; i++)
4340
			appendStringInfo(buf, " %u", xacts[i]);
4341
	}
4342 4343
}

V
WAL  
Vadim B. Mikheev 已提交
4344
void
4345
xact_desc(StringInfo buf, uint8 xl_info, char *rec)
V
WAL  
Vadim B. Mikheev 已提交
4346
{
B
Bruce Momjian 已提交
4347
	uint8		info = xl_info & ~XLR_INFO_MASK;
V
WAL  
Vadim B. Mikheev 已提交
4348 4349 4350

	if (info == XLOG_XACT_COMMIT)
	{
B
Bruce Momjian 已提交
4351
		xl_xact_commit *xlrec = (xl_xact_commit *) rec;
B
Bruce Momjian 已提交
4352

4353
		appendStringInfo(buf, "commit: ");
4354
		xact_desc_commit(buf, xlrec);
V
WAL  
Vadim B. Mikheev 已提交
4355 4356 4357
	}
	else if (info == XLOG_XACT_ABORT)
	{
B
Bruce Momjian 已提交
4358
		xl_xact_abort *xlrec = (xl_xact_abort *) rec;
V
WAL  
Vadim B. Mikheev 已提交
4359

4360
		appendStringInfo(buf, "abort: ");
4361 4362 4363 4364
		xact_desc_abort(buf, xlrec);
	}
	else if (info == XLOG_XACT_PREPARE)
	{
4365
		appendStringInfo(buf, "prepare");
4366 4367 4368 4369
	}
	else if (info == XLOG_XACT_COMMIT_PREPARED)
	{
		xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) rec;
B
Bruce Momjian 已提交
4370

4371
		appendStringInfo(buf, "commit %u: ", xlrec->xid);
4372 4373 4374 4375 4376
		xact_desc_commit(buf, &xlrec->crec);
	}
	else if (info == XLOG_XACT_ABORT_PREPARED)
	{
		xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) rec;
4377

4378
		appendStringInfo(buf, "abort %u: ", xlrec->xid);
4379
		xact_desc_abort(buf, &xlrec->arec);
V
WAL  
Vadim B. Mikheev 已提交
4380 4381
	}
	else
4382
		appendStringInfo(buf, "UNKNOWN");
V
WAL  
Vadim B. Mikheev 已提交
4383
}