summaryrefslogtreecommitdiff
path: root/src/backend/access/transam/transam.c
blob: b26243b4257f8f4536766f07cdf556fc502fd2fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*-------------------------------------------------------------------------
 *
 * transam.c
 *	  postgres transaction log/time interface routines
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/access/transam/transam.c,v 1.32 1999/09/18 19:06:21 tgl Exp $
 *
 * NOTES
 *	  This file contains the high level access-method interface to the
 *	  transaction system.
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "access/heapam.h"
#include "catalog/catname.h"

static int	RecoveryCheckingEnabled(void);
static void TransRecover(Relation logRelation);
static bool TransactionLogTest(TransactionId transactionId, XidStatus status);
static void TransactionLogUpdate(TransactionId transactionId,
					 XidStatus status);

/* ----------------
 *	  global variables holding pointers to relations used
 *	  by the transaction system.  These are initialized by
 *	  InitializeTransactionLog().
 * ----------------
 */

Relation	LogRelation = (Relation) NULL;
Relation	VariableRelation = (Relation) NULL;

/* ----------------
 *		global variables holding cached transaction id's and statuses.
 * ----------------
 */
TransactionId cachedTestXid;
XidStatus	cachedTestXidStatus;

/* ----------------
 *		transaction system constants
 * ----------------
 */
/* ----------------------------------------------------------------
 *		transaction system constants
 *
 *		read the comments for GetNewTransactionId in order to
 *		understand the initial values for AmiTransactionId and
 *		FirstTransactionId. -cim 3/23/90
 * ----------------------------------------------------------------
 */
TransactionId NullTransactionId = (TransactionId) 0;

TransactionId AmiTransactionId = (TransactionId) 512;

TransactionId FirstTransactionId = (TransactionId) 514;

/* ----------------
 *		transaction recovery state variables
 *
 *		When the transaction system is initialized, we may
 *		need to do recovery checking.  This decision is decided
 *		by the postmaster or the user by supplying the backend
 *		with a special flag.  In general, we want to do recovery
 *		checking whenever we are running without a postmaster
 *		or when the number of backends running under the postmaster
 *		goes from zero to one. -cim 3/21/90
 * ----------------
 */
int			RecoveryCheckingEnableState = 0;

/* ------------------
 *		spinlock for oid generation
 * -----------------
 */
extern int	OidGenLockId;


/* ----------------
 *		recovery checking accessors
 * ----------------
 */
static int
RecoveryCheckingEnabled(void)
{
	return RecoveryCheckingEnableState;
}

#ifdef NOT_USED
static void
SetRecoveryCheckingEnabled(bool state)
{
	RecoveryCheckingEnableState = (state == true);
}

#endif

/* ----------------------------------------------------------------
 *		postgres log access method interface
 *
 *		TransactionLogTest
 *		TransactionLogUpdate
 *		========
 *		   these functions do work for the interface
 *		   functions - they search/retrieve and append/update
 *		   information in the log and time relations.
 * ----------------------------------------------------------------
 */

/* --------------------------------
 *		TransactionLogTest
 * --------------------------------
 */

static bool						/* true/false: does transaction id have
								 * specified status? */
TransactionLogTest(TransactionId transactionId, /* transaction id to test */
				   XidStatus status)	/* transaction status */
{
	BlockNumber blockNumber;
	XidStatus	xidstatus;		/* recorded status of xid */
	bool		fail = false;	/* success/failure */

	/* ----------------
	 *	during initialization consider all transactions
	 *	as having been committed
	 * ----------------
	 */
	if (!RelationIsValid(LogRelation))
		return (bool) (status == XID_COMMIT);

	/* ----------------
	 *	 before going to the buffer manager, check our single
	 *	 item cache to see if we didn't just check the transaction
	 *	 status a moment ago.
	 * ----------------
	 */
	if (TransactionIdEquals(transactionId, cachedTestXid))
		return (bool)
			(status == cachedTestXidStatus);

	/* ----------------
	 *	compute the item pointer corresponding to the
	 *	page containing our transaction id.  We save the item in
	 *	our cache to speed up things if we happen to ask for the
	 *	same xid's status more than once.
	 * ----------------
	 */
	TransComputeBlockNumber(LogRelation, transactionId, &blockNumber);
	xidstatus = TransBlockNumberGetXidStatus(LogRelation,
											 blockNumber,
											 transactionId,
											 &fail);

	if (!fail)
	{
		/*
		 * DO NOT cache status for transactions in unknown state !!!
		 */
		if (xidstatus == XID_COMMIT || xidstatus == XID_ABORT)
		{
			TransactionIdStore(transactionId, &cachedTestXid);
			cachedTestXidStatus = xidstatus;
		}
		return (bool) (status == xidstatus);
	}

	/* ----------------
	 *	  here the block didn't contain the information we wanted
	 * ----------------
	 */
	elog(ERROR, "TransactionLogTest: failed to get xidstatus");

	/*
	 * so lint is happy...
	 */
	return false;
}

/* --------------------------------
 *		TransactionLogUpdate
 * --------------------------------
 */
static void
TransactionLogUpdate(TransactionId transactionId,		/* trans id to update */
					 XidStatus status)	/* new trans status */
{
	BlockNumber blockNumber;
	bool		fail = false;	/* success/failure */

	/* ----------------
	 *	during initialization we don't record any updates.
	 * ----------------
	 */
	if (!RelationIsValid(LogRelation))
		return;

	/* ----------------
	 *	update the log relation
	 * ----------------
	 */
	TransComputeBlockNumber(LogRelation, transactionId, &blockNumber);
	TransBlockNumberSetXidStatus(LogRelation,
								 blockNumber,
								 transactionId,
								 status,
								 &fail);

	/*
	 * update (invalidate) our single item TransactionLogTest cache.
	 *
	 * if (status != XID_COMMIT)
	 *
	 * What's the hell ?! Why != XID_COMMIT ?!
	 */
	TransactionIdStore(transactionId, &cachedTestXid);
	cachedTestXidStatus = status;

}

/* ----------------------------------------------------------------
 *					 transaction recovery code
 * ----------------------------------------------------------------
 */

/* --------------------------------
 *		TransRecover
 *
 *		preform transaction recovery checking.
 *
 *		Note: this should only be preformed if no other backends
 *			  are running.	This is known by the postmaster and
 *			  conveyed by the postmaster passing a "do recovery checking"
 *			  flag to the backend.
 *
 *		here we get the last recorded transaction from the log,
 *		get the "last" and "next" transactions from the variable relation
 *		and then preform some integrity tests:
 *
 *		1) No transaction may exist higher then the "next" available
 *		   transaction recorded in the variable relation.  If this is the
 *		   case then it means either the log or the variable relation
 *		   has become corrupted.
 *
 *		2) The last committed transaction may not be higher then the
 *		   next available transaction for the same reason.
 *
 *		3) The last recorded transaction may not be lower then the
 *		   last committed transaction.	(the reverse is ok - it means
 *		   that some transactions have aborted since the last commit)
 *
 *		Here is what the proper situation looks like.  The line
 *		represents the data stored in the log.	'c' indicates the
 *		transaction was recorded as committed, 'a' indicates an
 *		abortted transaction and '.' represents information not
 *		recorded.  These may correspond to in progress transactions.
 *
 *			 c	c  a  c  .	.  a  .  .	.  .  .  .	.  .  .  .
 *					  |					|
 *					 last			   next
 *
 *		Since "next" is only incremented by GetNewTransactionId() which
 *		is called when transactions are started.  Hence if there
 *		are commits or aborts after "next", then it means we committed
 *		or aborted BEFORE we started the transaction.  This is the
 *		rational behind constraint (1).
 *
 *		Likewise, "last" should never greater then "next" for essentially
 *		the same reason - it would imply we committed before we started.
 *		This is the reasoning for (2).
 *
 *		(3) implies we may never have a situation such as:
 *
 *			 c	c  a  c  .	.  a  c  .	.  .  .  .	.  .  .  .
 *					  |					|
 *					 last			   next
 *
 *		where there is a 'c' greater then "last".
 *
 *		Recovery checking is more difficult in the case where
 *		several backends are executing concurrently because the
 *		transactions may be executing in the other backends.
 *		So, we only do recovery stuff when the backend is explicitly
 *		passed a flag on the command line.
 * --------------------------------
 */
static void
TransRecover(Relation logRelation)
{
#ifdef NOT_USED
	/* ----------------
	 *	  first get the last recorded transaction in the log.
	 * ----------------
	 */
	TransGetLastRecordedTransaction(logRelation, logLastXid, &fail);
	if (fail == true)
		elog(ERROR, "TransRecover: failed TransGetLastRecordedTransaction");

	/* ----------------
	 *	  next get the "last" and "next" variables
	 * ----------------
	 */
	VariableRelationGetLastXid(&varLastXid);
	VariableRelationGetNextXid(&varNextXid);

	/* ----------------
	 *	  intregity test (1)
	 * ----------------
	 */
	if (TransactionIdIsLessThan(varNextXid, logLastXid))
		elog(ERROR, "TransRecover: varNextXid < logLastXid");

	/* ----------------
	 *	  intregity test (2)
	 * ----------------
	 */

	/* ----------------
	 *	  intregity test (3)
	 * ----------------
	 */

	/* ----------------
	 *	here we have a valid "
	 *
	 *			**** RESUME HERE ****
	 * ----------------
	 */
	varNextXid = TransactionIdDup(varLastXid);
	TransactionIdIncrement(&varNextXid);

	VarPut(var, VAR_PUT_LASTXID, varLastXid);
	VarPut(var, VAR_PUT_NEXTXID, varNextXid);
#endif
}

/* ----------------------------------------------------------------
 *						Interface functions
 *
 *		InitializeTransactionLog
 *		========
 *		   this function (called near cinit) initializes
 *		   the transaction log, time and variable relations.
 *
 *		TransactionId DidCommit
 *		TransactionId DidAbort
 *		TransactionId IsInProgress
 *		========
 *		   these functions test the transaction status of
 *		   a specified transaction id.
 *
 *		TransactionId Commit
 *		TransactionId Abort
 *		TransactionId SetInProgress
 *		========
 *		   these functions set the transaction status
 *		   of the specified xid. TransactionIdCommit() also
 *		   records the current time in the time relation
 *		   and updates the variable relation counter.
 *
 * ----------------------------------------------------------------
 */

/*
 * InitializeTransactionLog
 *		Initializes transaction logging.
 */
void
InitializeTransactionLog(void)
{
	Relation	logRelation;
	MemoryContext oldContext;

	/* ----------------
	 *	  don't do anything during bootstrapping
	 * ----------------
	 */
	if (AMI_OVERRIDE)
		return;

	/* ----------------
	 *	 disable the transaction system so the access methods
	 *	 don't interfere during initialization.
	 * ----------------
	 */
	OverrideTransactionSystem(true);

	/* ----------------
	 *	make sure allocations occur within the top memory context
	 *	so that our log management structures are protected from
	 *	garbage collection at the end of every transaction.
	 * ----------------
	 */
	oldContext = MemoryContextSwitchTo(TopMemoryContext);

	/* ----------------
	 *	 first open the log and time relations
	 *	 (these are created by amiint so they are guaranteed to exist)
	 * ----------------
	 */
	logRelation = heap_openr(LogRelationName, NoLock);
	Assert(logRelation != NULL);
	VariableRelation = heap_openr(VariableRelationName, NoLock);
	Assert(VariableRelation != NULL);

	/* ----------------
	 *	 XXX TransactionLogUpdate requires that LogRelation
	 *	 is valid so we temporarily set it so we can initialize
	 *	 things properly. This could be done cleaner.
	 * ----------------
	 */
	LogRelation = logRelation;

	/* ----------------
	 *	 if we have a virgin database, we initialize the log
	 *	 relation by committing the AmiTransactionId (id 512) and we
	 *	 initialize the variable relation by setting the next available
	 *	 transaction id to FirstTransactionId (id 514).  OID initialization
	 *	 happens as a side effect of bootstrapping in varsup.c.
	 * ----------------
	 */
	SpinAcquire(OidGenLockId);
	if (!TransactionIdDidCommit(AmiTransactionId))
	{

		/* ----------------
		 *	SOMEDAY initialize the information stored in
		 *			the headers of the log/variable relations.
		 * ----------------
		 */
		TransactionLogUpdate(AmiTransactionId, XID_COMMIT);
		TransactionIdStore(AmiTransactionId, &cachedTestXid);
		cachedTestXidStatus = XID_COMMIT;
		VariableRelationPutNextXid(FirstTransactionId);

	}
	else if (RecoveryCheckingEnabled())
	{
		/* ----------------
		 *		if we have a pre-initialized database and if the
		 *		perform recovery checking flag was passed then we
		 *		do our database integrity checking.
		 * ----------------
		 */
		TransRecover(logRelation);
	}
	LogRelation = (Relation) NULL;
	SpinRelease(OidGenLockId);

	/* ----------------
	 *	now re-enable the transaction system
	 * ----------------
	 */
	OverrideTransactionSystem(false);

	/* ----------------
	 *	instantiate the global variables
	 * ----------------
	 */
	LogRelation = logRelation;

	/* ----------------
	 *	restore the memory context to the previous context
	 *	before we return from initialization.
	 * ----------------
	 */
	MemoryContextSwitchTo(oldContext);
}

/* --------------------------------
 *		TransactionId DidCommit
 *		TransactionId DidAbort
 *		TransactionId IsInProgress
 * --------------------------------
 */

/*
 * TransactionIdDidCommit
 *		True iff transaction associated with the identifier did commit.
 *
 * Note:
 *		Assumes transaction identifier is valid.
 */
bool							/* true if given transaction committed */
TransactionIdDidCommit(TransactionId transactionId)
{
	if (AMI_OVERRIDE)
		return true;

	return TransactionLogTest(transactionId, XID_COMMIT);
}

/*
 * TransactionIdDidAborted
 *		True iff transaction associated with the identifier did abort.
 *
 * Note:
 *		Assumes transaction identifier is valid.
 *		XXX Is this unneeded?
 */
bool							/* true if given transaction aborted */
TransactionIdDidAbort(TransactionId transactionId)
{
	if (AMI_OVERRIDE)
		return false;

	return TransactionLogTest(transactionId, XID_ABORT);
}

/*
 * Now this func in shmem.c and gives quality answer by scanning
 * PROC structures of all running backend. - vadim 11/26/96
 *
 * Old comments:
 * true if given transaction neither committed nor aborted

bool
TransactionIdIsInProgress(TransactionId transactionId)
{
	if (AMI_OVERRIDE)
		return false;

	return TransactionLogTest(transactionId, XID_INPROGRESS);
}
 */

/* --------------------------------
 *		TransactionId Commit
 *		TransactionId Abort
 *		TransactionId SetInProgress
 * --------------------------------
 */

/*
 * TransactionIdCommit
 *		Commits the transaction associated with the identifier.
 *
 * Note:
 *		Assumes transaction identifier is valid.
 */
void
TransactionIdCommit(TransactionId transactionId)
{
	if (AMI_OVERRIDE)
		return;

	TransactionLogUpdate(transactionId, XID_COMMIT);
}

/*
 * TransactionIdAbort
 *		Aborts the transaction associated with the identifier.
 *
 * Note:
 *		Assumes transaction identifier is valid.
 */
void
TransactionIdAbort(TransactionId transactionId)
{
	if (AMI_OVERRIDE)
		return;

	TransactionLogUpdate(transactionId, XID_ABORT);
}