]> granicus.if.org Git - postgresql/blob - src/backend/access/transam/xact.c
pgindent run on all C files. Java run to follow. initdb/regression
[postgresql] / src / backend / access / transam / xact.c
1 /*-------------------------------------------------------------------------
2  *
3  * xact.c
4  *        top level transaction system support routines
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.113 2001/10/25 05:49:22 momjian Exp $
12  *
13  * NOTES
14  *              Transaction aborts can now occur two ways:
15  *
16  *              1)      system dies from some internal cause  (Assert, etc..)
17  *              2)      user types abort
18  *
19  *              These two cases used to be treated identically, but now
20  *              we need to distinguish them.  Why?      consider the following
21  *              two situations:
22  *
23  *                              case 1                                                  case 2
24  *                              ------                                                  ------
25  *              1) user types BEGIN                             1) user types BEGIN
26  *              2) user does something                  2) user does something
27  *              3) user does not like what              3) system aborts for some reason
28  *                 she sees and types ABORT
29  *
30  *              In case 1, we want to abort the transaction and return to the
31  *              default state.  In case 2, there may be more commands coming
32  *              our way which are part of the same transaction block and we have
33  *              to ignore these commands until we see an END transaction.
34  *              (or an ABORT! --djm)
35  *
36  *              Internal aborts are now handled by AbortTransactionBlock(), just as
37  *              they always have been, and user aborts are now handled by
38  *              UserAbortTransactionBlock().  Both of them rely on AbortTransaction()
39  *              to do all the real work.  The only difference is what state we
40  *              enter after AbortTransaction() does its work:
41  *
42  *              * AbortTransactionBlock() leaves us in TBLOCK_ABORT and
43  *              * UserAbortTransactionBlock() leaves us in TBLOCK_ENDABORT
44  *
45  *              Low-level transaction abort handling is divided into two phases:
46  *              * AbortTransaction() executes as soon as we realize the transaction
47  *                has failed.  It should release all shared resources (locks etc)
48  *                so that we do not delay other backends unnecessarily.
49  *              * CleanupTransaction() executes when we finally see a user COMMIT
50  *                or ROLLBACK command; it cleans things up and gets us out of
51  *                the transaction internally.  In particular, we mustn't destroy
52  *                TransactionCommandContext until this point.
53  *
54  *       NOTES
55  *              This file is an attempt at a redesign of the upper layer
56  *              of the V1 transaction system which was too poorly thought
57  *              out to describe.  This new system hopes to be both simpler
58  *              in design, simpler to extend and needs to contain added
59  *              functionality to solve problems beyond the scope of the V1
60  *              system.  (In particuler, communication of transaction
61  *              information between parallel backends has to be supported)
62  *
63  *              The essential aspects of the transaction system are:
64  *
65  *                              o  transaction id generation
66  *                              o  transaction log updating
67  *                              o  memory cleanup
68  *                              o  cache invalidation
69  *                              o  lock cleanup
70  *
71  *              Hence, the functional division of the transaction code is
72  *              based on what of the above things need to be done during
73  *              a start/commit/abort transaction.  For instance, the
74  *              routine AtCommit_Memory() takes care of all the memory
75  *              cleanup stuff done at commit time.
76  *
77  *              The code is layered as follows:
78  *
79  *                              StartTransaction
80  *                              CommitTransaction
81  *                              AbortTransaction
82  *                              CleanupTransaction
83  *
84  *              are provided to do the lower level work like recording
85  *              the transaction status in the log and doing memory cleanup.
86  *              above these routines are another set of functions:
87  *
88  *                              StartTransactionCommand
89  *                              CommitTransactionCommand
90  *                              AbortCurrentTransaction
91  *
92  *              These are the routines used in the postgres main processing
93  *              loop.  They are sensitive to the current transaction block state
94  *              and make calls to the lower level routines appropriately.
95  *
96  *              Support for transaction blocks is provided via the functions:
97  *
98  *                              StartTransactionBlock
99  *                              CommitTransactionBlock
100  *                              AbortTransactionBlock
101  *
102  *              These are invoked only in responce to a user "BEGIN", "END",
103  *              or "ABORT" command.  The tricky part about these functions
104  *              is that they are called within the postgres main loop, in between
105  *              the StartTransactionCommand() and CommitTransactionCommand().
106  *
107  *              For example, consider the following sequence of user commands:
108  *
109  *              1)              begin
110  *              2)              retrieve (foo.all)
111  *              3)              append foo (bar = baz)
112  *              4)              end
113  *
114  *              in the main processing loop, this results in the following
115  *              transaction sequence:
116  *
117  *                      /       StartTransactionCommand();
118  *              1) /    ProcessUtility();                               << begin
119  *                 \            StartTransactionBlock();
120  *                      \       CommitTransactionCommand();
121  *
122  *                      /       StartTransactionCommand();
123  *              2) <    ProcessQuery();                                 << retrieve (foo.all)
124  *                      \       CommitTransactionCommand();
125  *
126  *                      /       StartTransactionCommand();
127  *              3) <    ProcessQuery();                                 << append foo (bar = baz)
128  *                      \       CommitTransactionCommand();
129  *
130  *                      /       StartTransactionCommand();
131  *              4) /    ProcessUtility();                               << end
132  *                 \            CommitTransactionBlock();
133  *                      \       CommitTransactionCommand();
134  *
135  *              The point of this example is to demonstrate the need for
136  *              StartTransactionCommand() and CommitTransactionCommand() to
137  *              be state smart -- they should do nothing in between the calls
138  *              to StartTransactionBlock() and EndTransactionBlock() and
139  *              outside these calls they need to do normal start/commit
140  *              processing.
141  *
142  *              Furthermore, suppose the "retrieve (foo.all)" caused an abort
143  *              condition.      We would then want to abort the transaction and
144  *              ignore all subsequent commands up to the "end".
145  *              -cim 3/23/90
146  *
147  *-------------------------------------------------------------------------
148  */
149
150 /*
151  * Large object clean up added in CommitTransaction() to prevent buffer leaks.
152  * [PA, 7/17/98]
153  * [PA] is Pascal AndrĂ© <andre@via.ecp.fr>
154  */
155 #include "postgres.h"
156
157 #include <sys/time.h>
158
159 #include "access/gistscan.h"
160 #include "access/hash.h"
161 #include "access/nbtree.h"
162 #include "access/rtree.h"
163 #include "access/xact.h"
164 #include "catalog/heap.h"
165 #include "catalog/index.h"
166 #include "commands/async.h"
167 #include "commands/sequence.h"
168 #include "commands/trigger.h"
169 #include "executor/spi.h"
170 #include "libpq/be-fsstubs.h"
171 #include "miscadmin.h"
172 #include "storage/proc.h"
173 #include "storage/sinval.h"
174 #include "storage/smgr.h"
175 #include "utils/inval.h"
176 #include "utils/memutils.h"
177 #include "utils/portal.h"
178 #include "utils/catcache.h"
179 #include "utils/relcache.h"
180 #include "utils/temprel.h"
181
182 #include "pgstat.h"
183
184 extern bool SharedBufferChanged;
185
186 static void AbortTransaction(void);
187 static void AtAbort_Cache(void);
188 static void AtAbort_Locks(void);
189 static void AtAbort_Memory(void);
190 static void AtCleanup_Memory(void);
191 static void AtCommit_Cache(void);
192 static void AtCommit_LocalCache(void);
193 static void AtCommit_Locks(void);
194 static void AtCommit_Memory(void);
195 static void AtStart_Cache(void);
196 static void AtStart_Locks(void);
197 static void AtStart_Memory(void);
198 static void CleanupTransaction(void);
199 static void CommitTransaction(void);
200 static void RecordTransactionAbort(void);
201 static void StartTransaction(void);
202
203 /* ----------------
204  *              global variables holding the current transaction state.
205  * ----------------
206  */
207 static TransactionStateData CurrentTransactionStateData = {
208         0,                                                      /* transaction id */
209         FirstCommandId,                         /* command id */
210         0,                                                      /* scan command id */
211         0x0,                                            /* start time */
212         TRANS_DEFAULT,                          /* transaction state */
213         TBLOCK_DEFAULT                          /* transaction block state */
214 };
215
216 TransactionState CurrentTransactionState = &CurrentTransactionStateData;
217
218 /*
219  * User-tweakable parameters
220  */
221 int                     DefaultXactIsoLevel = XACT_READ_COMMITTED;
222 int                     XactIsoLevel;
223
224 int                     CommitDelay = 0;        /* precommit delay in microseconds */
225 int                     CommitSiblings = 5; /* number of concurrent xacts needed to
226                                                                  * sleep */
227
228 static void (*_RollbackFunc) (void *) = NULL;
229 static void *_RollbackData = NULL;
230
231 /* ----------------
232  *              catalog creation transaction bootstrapping flag.
233  *              This should be eliminated and added to the transaction
234  *              state stuff.  -cim 3/19/90
235  * ----------------
236  */
237 bool            AMI_OVERRIDE = false;
238
239 /* ----------------------------------------------------------------
240  *                                       transaction state accessors
241  * ----------------------------------------------------------------
242  */
243
244 /* --------------------------------
245  *              TranactionFlushEnabled()
246  *              SetTransactionFlushEnabled()
247  *
248  *              These are used to test and set the "TransactionFlushState"
249  *              varable.  If this variable is true (the default), then
250  *              the system will flush all dirty buffers to disk at the end
251  *              of each transaction.   If false then we are assuming the
252  *              buffer pool resides in stable main memory, in which case we
253  *              only do writes as necessary.
254  * --------------------------------
255  */
256 static int      TransactionFlushState = 1;
257
258 int
259 TransactionFlushEnabled(void)
260 {
261         return TransactionFlushState;
262 }
263
264 #ifdef NOT_USED
265 void
266 SetTransactionFlushEnabled(bool state)
267 {
268         TransactionFlushState = (state == true);
269 }
270
271
272 /* --------------------------------
273  *              IsTransactionState
274  *
275  *              This returns true if we are currently running a query
276  *              within an executing transaction.
277  * --------------------------------
278  */
279 bool
280 IsTransactionState(void)
281 {
282         TransactionState s = CurrentTransactionState;
283
284         switch (s->state)
285         {
286                 case TRANS_DEFAULT:
287                         return false;
288                 case TRANS_START:
289                         return true;
290                 case TRANS_INPROGRESS:
291                         return true;
292                 case TRANS_COMMIT:
293                         return true;
294                 case TRANS_ABORT:
295                         return true;
296         }
297
298         /*
299          * Shouldn't get here, but lint is not happy with this...
300          */
301         return false;
302 }
303 #endif
304
305 /* --------------------------------
306  *              IsAbortedTransactionBlockState
307  *
308  *              This returns true if we are currently running a query
309  *              within an aborted transaction block.
310  * --------------------------------
311  */
312 bool
313 IsAbortedTransactionBlockState(void)
314 {
315         TransactionState s = CurrentTransactionState;
316
317         if (s->blockState == TBLOCK_ABORT)
318                 return true;
319
320         return false;
321 }
322
323
324 /* --------------------------------
325  *              GetCurrentTransactionId
326  * --------------------------------
327  */
328 TransactionId
329 GetCurrentTransactionId(void)
330 {
331         TransactionState s = CurrentTransactionState;
332
333         return s->transactionIdData;
334 }
335
336
337 /* --------------------------------
338  *              GetCurrentCommandId
339  * --------------------------------
340  */
341 CommandId
342 GetCurrentCommandId(void)
343 {
344         TransactionState s = CurrentTransactionState;
345
346         return s->commandId;
347 }
348
349 CommandId
350 GetScanCommandId(void)
351 {
352         TransactionState s = CurrentTransactionState;
353
354         return s->scanCommandId;
355 }
356
357
358 /* --------------------------------
359  *              GetCurrentTransactionStartTime
360  * --------------------------------
361  */
362 AbsoluteTime
363 GetCurrentTransactionStartTime(void)
364 {
365         TransactionState s = CurrentTransactionState;
366
367         return s->startTime;
368 }
369
370
371 /* --------------------------------
372  *              GetCurrentTransactionStartTimeUsec
373  * --------------------------------
374  */
375 AbsoluteTime
376 GetCurrentTransactionStartTimeUsec(int *msec)
377 {
378         TransactionState s = CurrentTransactionState;
379
380         *msec = s->startTimeUsec;
381
382         return s->startTime;
383 }
384
385
386 /* --------------------------------
387  *              TransactionIdIsCurrentTransactionId
388  * --------------------------------
389  */
390 bool
391 TransactionIdIsCurrentTransactionId(TransactionId xid)
392 {
393         TransactionState s = CurrentTransactionState;
394
395         if (AMI_OVERRIDE)
396                 return false;
397
398         return TransactionIdEquals(xid, s->transactionIdData);
399 }
400
401
402 /* --------------------------------
403  *              CommandIdIsCurrentCommandId
404  * --------------------------------
405  */
406 bool
407 CommandIdIsCurrentCommandId(CommandId cid)
408 {
409         TransactionState s = CurrentTransactionState;
410
411         if (AMI_OVERRIDE)
412                 return false;
413
414         return (cid == s->commandId) ? true : false;
415 }
416
417 bool
418 CommandIdGEScanCommandId(CommandId cid)
419 {
420         TransactionState s = CurrentTransactionState;
421
422         if (AMI_OVERRIDE)
423                 return false;
424
425         return (cid >= s->scanCommandId) ? true : false;
426 }
427
428
429 /* --------------------------------
430  *              CommandCounterIncrement
431  * --------------------------------
432  */
433 void
434 CommandCounterIncrement(void)
435 {
436         CurrentTransactionStateData.commandId += 1;
437         if (CurrentTransactionStateData.commandId == FirstCommandId)
438                 elog(ERROR, "You may only have 2^32-1 commands per transaction");
439
440         CurrentTransactionStateData.scanCommandId = CurrentTransactionStateData.commandId;
441
442         /*
443          * make cache changes visible to me.  AtCommit_LocalCache() instead of
444          * AtCommit_Cache() is called here.
445          */
446         AtCommit_LocalCache();
447         AtStart_Cache();
448 }
449
450 void
451 SetScanCommandId(CommandId savedId)
452 {
453         CurrentTransactionStateData.scanCommandId = savedId;
454 }
455
456 /* ----------------------------------------------------------------
457  *                                              StartTransaction stuff
458  * ----------------------------------------------------------------
459  */
460
461 /* --------------------------------
462  *              AtStart_Cache
463  * --------------------------------
464  */
465 static void
466 AtStart_Cache(void)
467 {
468         AcceptInvalidationMessages();
469 }
470
471 /* --------------------------------
472  *              AtStart_Locks
473  * --------------------------------
474  */
475 static void
476 AtStart_Locks(void)
477 {
478         /*
479          * at present, it is unknown to me what belongs here -cim 3/18/90
480          *
481          * There isn't anything to do at the start of a xact for locks. -mer
482          * 5/24/92
483          */
484 }
485
486 /* --------------------------------
487  *              AtStart_Memory
488  * --------------------------------
489  */
490 static void
491 AtStart_Memory(void)
492 {
493         /*
494          * We shouldn't have any transaction contexts already.
495          */
496         Assert(TopTransactionContext == NULL);
497         Assert(TransactionCommandContext == NULL);
498
499         /*
500          * Create a toplevel context for the transaction.
501          */
502         TopTransactionContext =
503                 AllocSetContextCreate(TopMemoryContext,
504                                                           "TopTransactionContext",
505                                                           ALLOCSET_DEFAULT_MINSIZE,
506                                                           ALLOCSET_DEFAULT_INITSIZE,
507                                                           ALLOCSET_DEFAULT_MAXSIZE);
508
509         /*
510          * Create a statement-level context and make it active.
511          */
512         TransactionCommandContext =
513                 AllocSetContextCreate(TopTransactionContext,
514                                                           "TransactionCommandContext",
515                                                           ALLOCSET_DEFAULT_MINSIZE,
516                                                           ALLOCSET_DEFAULT_INITSIZE,
517                                                           ALLOCSET_DEFAULT_MAXSIZE);
518         MemoryContextSwitchTo(TransactionCommandContext);
519 }
520
521
522 /* ----------------------------------------------------------------
523  *                                              CommitTransaction stuff
524  * ----------------------------------------------------------------
525  */
526
527 /* --------------------------------
528  *              RecordTransactionCommit
529  *
530  *              Note: the two calls to BufferManagerFlush() exist to ensure
531  *                        that data pages are written before log pages.  These
532  *                        explicit calls should be replaced by a more efficient
533  *                        ordered page write scheme in the buffer manager
534  *                        -cim 3/18/90
535  * --------------------------------
536  */
537 void
538 RecordTransactionCommit(void)
539 {
540         TransactionId xid;
541         bool            leak;
542
543         leak = BufferPoolCheckLeak();
544
545         xid = GetCurrentTransactionId();
546
547         /*
548          * We needn't write anything in xlog or clog if the transaction was
549          * read-only, which we check by testing if it made any xlog entries.
550          */
551         if (MyLastRecPtr.xrecoff != 0)
552         {
553                 XLogRecData rdata;
554                 xl_xact_commit xlrec;
555                 XLogRecPtr      recptr;
556
557                 BufmgrCommit();
558
559                 xlrec.xtime = time(NULL);
560                 rdata.buffer = InvalidBuffer;
561                 rdata.data = (char *) (&xlrec);
562                 rdata.len = SizeOfXactCommit;
563                 rdata.next = NULL;
564
565                 START_CRIT_SECTION();
566
567                 /*
568                  * SHOULD SAVE ARRAY OF RELFILENODE-s TO DROP
569                  */
570                 recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT, &rdata);
571
572                 /*
573                  * Sleep before commit! So we can flush more than one commit
574                  * records per single fsync.  (The idea is some other backend may
575                  * do the XLogFlush while we're sleeping.  This needs work still,
576                  * because on most Unixen, the minimum select() delay is 10msec or
577                  * more, which is way too long.)
578                  *
579                  * We do not sleep if enableFsync is not turned on, nor if there are
580                  * fewer than CommitSiblings other backends with active
581                  * transactions.
582                  */
583                 if (CommitDelay > 0 && enableFsync &&
584                         CountActiveBackends() >= CommitSiblings)
585                 {
586                         struct timeval delay;
587
588                         delay.tv_sec = 0;
589                         delay.tv_usec = CommitDelay;
590                         (void) select(0, NULL, NULL, NULL, &delay);
591                 }
592
593                 XLogFlush(recptr);
594
595                 /* Break the chain of back-links in the XLOG records I output */
596                 MyLastRecPtr.xrecoff = 0;
597
598                 /* Mark the transaction committed in clog */
599                 TransactionIdCommit(xid);
600
601                 END_CRIT_SECTION();
602         }
603
604         /* Show myself as out of the transaction in PROC array */
605         MyProc->logRec.xrecoff = 0;
606
607         if (leak)
608                 ResetBufferPool(true);
609 }
610
611
612 /* --------------------------------
613  *              AtCommit_Cache
614  * --------------------------------
615  */
616 static void
617 AtCommit_Cache(void)
618 {
619         /*
620          * Make catalog changes visible to all backends.
621          */
622         AtEOXactInvalidationMessages(true);
623 }
624
625 /* --------------------------------
626  *              AtCommit_LocalCache
627  * --------------------------------
628  */
629 static void
630 AtCommit_LocalCache(void)
631 {
632         /*
633          * Make catalog changes visible to me for the next command.
634          */
635         CommandEndInvalidationMessages(true);
636 }
637
638 /* --------------------------------
639  *              AtCommit_Locks
640  * --------------------------------
641  */
642 static void
643 AtCommit_Locks(void)
644 {
645         /*
646          * XXX What if ProcReleaseLocks fails?  (race condition?)
647          *
648          * Then you're up a creek! -mer 5/24/92
649          */
650         ProcReleaseLocks(true);
651 }
652
653 /* --------------------------------
654  *              AtCommit_Memory
655  * --------------------------------
656  */
657 static void
658 AtCommit_Memory(void)
659 {
660         /*
661          * Now that we're "out" of a transaction, have the system allocate
662          * things in the top memory context instead of per-transaction
663          * contexts.
664          */
665         MemoryContextSwitchTo(TopMemoryContext);
666
667         /*
668          * Release all transaction-local memory.
669          */
670         Assert(TopTransactionContext != NULL);
671         MemoryContextDelete(TopTransactionContext);
672         TopTransactionContext = NULL;
673         TransactionCommandContext = NULL;
674 }
675
676 /* ----------------------------------------------------------------
677  *                                              AbortTransaction stuff
678  * ----------------------------------------------------------------
679  */
680
681 /* --------------------------------
682  *              RecordTransactionAbort
683  * --------------------------------
684  */
685 static void
686 RecordTransactionAbort(void)
687 {
688         TransactionId xid = GetCurrentTransactionId();
689
690         /*
691          * We needn't write anything in xlog or clog if the transaction was
692          * read-only, which we check by testing if it made any xlog entries.
693          *
694          * Extra check here is to catch case that we aborted partway through
695          * RecordTransactionCommit ...
696          */
697         if (MyLastRecPtr.xrecoff != 0 && !TransactionIdDidCommit(xid))
698         {
699                 XLogRecData rdata;
700                 xl_xact_abort xlrec;
701                 XLogRecPtr      recptr;
702
703                 xlrec.xtime = time(NULL);
704                 rdata.buffer = InvalidBuffer;
705                 rdata.data = (char *) (&xlrec);
706                 rdata.len = SizeOfXactAbort;
707                 rdata.next = NULL;
708
709                 START_CRIT_SECTION();
710
711                 /*
712                  * SHOULD SAVE ARRAY OF RELFILENODE-s TO DROP
713                  */
714                 recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT, &rdata);
715
716                 /*
717                  * There's no need for XLogFlush here, since the default
718                  * assumption would be that we aborted, anyway.
719                  */
720
721                 /* Mark the transaction aborted in clog */
722                 TransactionIdAbort(xid);
723
724                 END_CRIT_SECTION();
725         }
726
727         /* Break the chain of back-links in the XLOG records I output */
728         MyLastRecPtr.xrecoff = 0;
729         /* Show myself as out of the transaction in PROC array */
730         MyProc->logRec.xrecoff = 0;
731
732         /*
733          * Tell bufmgr and smgr to release resources.
734          */
735         ResetBufferPool(false);         /* false -> is abort */
736 }
737
738 /* --------------------------------
739  *              AtAbort_Cache
740  * --------------------------------
741  */
742 static void
743 AtAbort_Cache(void)
744 {
745         RelationCacheAbort();
746         AtEOXactInvalidationMessages(false);
747 }
748
749 /* --------------------------------
750  *              AtAbort_Locks
751  * --------------------------------
752  */
753 static void
754 AtAbort_Locks(void)
755 {
756         /*
757          * XXX What if ProcReleaseLocks() fails?  (race condition?)
758          *
759          * Then you're up a creek without a paddle! -mer
760          */
761         ProcReleaseLocks(false);
762 }
763
764
765 /* --------------------------------
766  *              AtAbort_Memory
767  * --------------------------------
768  */
769 static void
770 AtAbort_Memory(void)
771 {
772         /*
773          * Make sure we are in a valid context (not a child of
774          * TransactionCommandContext...).  Note that it is possible for this
775          * code to be called when we aren't in a transaction at all; go
776          * directly to TopMemoryContext in that case.
777          */
778         if (TransactionCommandContext != NULL)
779         {
780                 MemoryContextSwitchTo(TransactionCommandContext);
781
782                 /*
783                  * We do not want to destroy transaction contexts yet, but it
784                  * should be OK to delete any command-local memory.
785                  */
786                 MemoryContextResetAndDeleteChildren(TransactionCommandContext);
787         }
788         else
789                 MemoryContextSwitchTo(TopMemoryContext);
790 }
791
792
793 /* ----------------------------------------------------------------
794  *                                              CleanupTransaction stuff
795  * ----------------------------------------------------------------
796  */
797
798 /* --------------------------------
799  *              AtCleanup_Memory
800  * --------------------------------
801  */
802 static void
803 AtCleanup_Memory(void)
804 {
805         /*
806          * Now that we're "out" of a transaction, have the system allocate
807          * things in the top memory context instead of per-transaction
808          * contexts.
809          */
810         MemoryContextSwitchTo(TopMemoryContext);
811
812         /*
813          * Release all transaction-local memory.
814          */
815         if (TopTransactionContext != NULL)
816                 MemoryContextDelete(TopTransactionContext);
817         TopTransactionContext = NULL;
818         TransactionCommandContext = NULL;
819 }
820
821
822 /* ----------------------------------------------------------------
823  *                                              interface routines
824  * ----------------------------------------------------------------
825  */
826
827 /* --------------------------------
828  *              StartTransaction
829  *
830  * --------------------------------
831  */
832 static void
833 StartTransaction(void)
834 {
835         TransactionState s = CurrentTransactionState;
836
837         FreeXactSnapshot();
838         XactIsoLevel = DefaultXactIsoLevel;
839
840         /*
841          * Check the current transaction state.  If the transaction system is
842          * switched off, or if we're already in a transaction, do nothing.
843          * We're already in a transaction when the monitor sends a null
844          * command to the backend to flush the comm channel.  This is a hacky
845          * fix to a communications problem, and we keep having to deal with it
846          * here.  We should fix the comm channel code.  mao 080891
847          */
848         if (s->state == TRANS_INPROGRESS)
849                 return;
850
851         /*
852          * set the current transaction state information appropriately during
853          * start processing
854          */
855         s->state = TRANS_START;
856
857         SetReindexProcessing(false);
858
859         /*
860          * generate a new transaction id
861          */
862         s->transactionIdData = GetNewTransactionId();
863
864         XactLockTableInsert(s->transactionIdData);
865
866         /*
867          * initialize current transaction state fields
868          */
869         s->commandId = FirstCommandId;
870         s->scanCommandId = FirstCommandId;
871 #if NOT_USED
872         s->startTime = GetCurrentAbsoluteTime();
873 #endif
874         s->startTime = GetCurrentAbsoluteTimeUsec(&(s->startTimeUsec));
875
876         /*
877          * initialize the various transaction subsystems
878          */
879         AtStart_Memory();
880         AtStart_Cache();
881         AtStart_Locks();
882
883         /*
884          * Tell the trigger manager to we're starting a transaction
885          */
886         DeferredTriggerBeginXact();
887
888         /*
889          * done with start processing, set current transaction state to "in
890          * progress"
891          */
892         s->state = TRANS_INPROGRESS;
893
894 }
895
896 #ifdef NOT_USED
897 /* ---------------
898  * Tell me if we are currently in progress
899  * ---------------
900  */
901 bool
902 CurrentXactInProgress(void)
903 {
904         return CurrentTransactionState->state == TRANS_INPROGRESS;
905 }
906 #endif
907
908 /* --------------------------------
909  *              CommitTransaction
910  *
911  * --------------------------------
912  */
913 static void
914 CommitTransaction(void)
915 {
916         TransactionState s = CurrentTransactionState;
917
918         /*
919          * check the current transaction state
920          */
921         if (s->state != TRANS_INPROGRESS)
922                 elog(NOTICE, "CommitTransaction and not in in-progress state");
923
924         /*
925          * Tell the trigger manager that this transaction is about to be
926          * committed. He'll invoke all trigger deferred until XACT before we
927          * really start on committing the transaction.
928          */
929         DeferredTriggerEndXact();
930
931         /* Prevent cancel/die interrupt while cleaning up */
932         HOLD_INTERRUPTS();
933
934         /*
935          * set the current transaction state information appropriately during
936          * the abort processing
937          */
938         s->state = TRANS_COMMIT;
939
940         /*
941          * do commit processing
942          */
943
944         /* handle commit for large objects [ PA, 7/17/98 ] */
945         lo_commit(true);
946
947         /* NOTIFY commit must also come before lower-level cleanup */
948         AtCommit_Notify();
949
950         CloseSequences();
951         AtEOXact_portals();
952
953         /* Here is where we really truly commit. */
954         RecordTransactionCommit();
955
956         /*
957          * Let others know about no transaction in progress by me. Note that
958          * this must be done _before_ releasing locks we hold and _after_
959          * RecordTransactionCommit.
960          *
961          * LWLockAcquire(SInvalLock) is required: UPDATE with xid 0 is blocked by
962          * xid 1' UPDATE, xid 1 is doing commit while xid 2 gets snapshot - if
963          * xid 2' GetSnapshotData sees xid 1 as running then it must see xid 0
964          * as running as well or it will see two tuple versions - one deleted
965          * by xid 1 and one inserted by xid 0.  See notes in GetSnapshotData.
966          */
967         if (MyProc != (PROC *) NULL)
968         {
969                 /* Lock SInvalLock because that's what GetSnapshotData uses. */
970                 LWLockAcquire(SInvalLock, LW_EXCLUSIVE);
971                 MyProc->xid = InvalidTransactionId;
972                 MyProc->xmin = InvalidTransactionId;
973                 LWLockRelease(SInvalLock);
974         }
975
976         /*
977          * This is all post-commit cleanup.  Note that if an error is raised
978          * here, it's too late to abort the transaction.  This should be just
979          * noncritical resource releasing.
980          */
981
982         RelationPurgeLocalRelation(true);
983         AtEOXact_temp_relations(true);
984         smgrDoPendingDeletes(true);
985
986         AtEOXact_SPI();
987         AtEOXact_gist();
988         AtEOXact_hash();
989         AtEOXact_nbtree();
990         AtEOXact_rtree();
991         AtCommit_Cache();
992         AtCommit_Locks();
993         AtEOXact_CatCache(true);
994         AtCommit_Memory();
995         AtEOXact_Files();
996
997         SharedBufferChanged = false;            /* safest place to do it */
998
999         /* Count transaction commit in statistics collector */
1000         pgstat_count_xact_commit();
1001
1002         /*
1003          * done with commit processing, set current transaction state back to
1004          * default
1005          */
1006         s->state = TRANS_DEFAULT;
1007
1008         RESUME_INTERRUPTS();
1009 }
1010
1011 /* --------------------------------
1012  *              AbortTransaction
1013  *
1014  * --------------------------------
1015  */
1016 static void
1017 AbortTransaction(void)
1018 {
1019         TransactionState s = CurrentTransactionState;
1020
1021         /* Prevent cancel/die interrupt while cleaning up */
1022         HOLD_INTERRUPTS();
1023
1024         /*
1025          * Release any LW locks we might be holding as quickly as possible.
1026          * (Regular locks, however, must be held till we finish aborting.)
1027          * Releasing LW locks is critical since we might try to grab them
1028          * again while cleaning up!
1029          */
1030         LWLockReleaseAll();
1031
1032         /* Clean up buffer I/O and buffer context locks, too */
1033         AbortBufferIO();
1034         UnlockBuffers();
1035
1036         /*
1037          * Also clean up any open wait for lock, since the lock manager will
1038          * choke if we try to wait for another lock before doing this.
1039          */
1040         LockWaitCancel();
1041
1042         /*
1043          * check the current transaction state
1044          */
1045         if (s->state != TRANS_INPROGRESS)
1046                 elog(NOTICE, "AbortTransaction and not in in-progress state");
1047
1048         /*
1049          * set the current transaction state information appropriately during
1050          * the abort processing
1051          */
1052         s->state = TRANS_ABORT;
1053
1054         /*
1055          * Reset user id which might have been changed transiently
1056          */
1057         SetUserId(GetSessionUserId());
1058
1059         /*
1060          * do abort processing
1061          */
1062         DeferredTriggerAbortXact();
1063         lo_commit(false);                       /* 'false' means it's abort */
1064         AtAbort_Notify();
1065         CloseSequences();
1066         AtEOXact_portals();
1067
1068         /* Advertise the fact that we aborted in pg_clog. */
1069         RecordTransactionAbort();
1070
1071         /*
1072          * Let others know about no transaction in progress by me. Note that
1073          * this must be done _before_ releasing locks we hold and _after_
1074          * RecordTransactionAbort.
1075          */
1076         if (MyProc != (PROC *) NULL)
1077         {
1078                 /* Lock SInvalLock because that's what GetSnapshotData uses. */
1079                 LWLockAcquire(SInvalLock, LW_EXCLUSIVE);
1080                 MyProc->xid = InvalidTransactionId;
1081                 MyProc->xmin = InvalidTransactionId;
1082                 LWLockRelease(SInvalLock);
1083         }
1084
1085         RelationPurgeLocalRelation(false);
1086         AtEOXact_temp_relations(false);
1087         smgrDoPendingDeletes(false);
1088
1089         AtEOXact_SPI();
1090         AtEOXact_gist();
1091         AtEOXact_hash();
1092         AtEOXact_nbtree();
1093         AtEOXact_rtree();
1094         AtAbort_Cache();
1095         AtEOXact_CatCache(false);
1096         AtAbort_Memory();
1097         AtEOXact_Files();
1098         AtAbort_Locks();
1099
1100         SharedBufferChanged = false;            /* safest place to do it */
1101
1102         /* Count transaction abort in statistics collector */
1103         pgstat_count_xact_rollback();
1104
1105         /*
1106          * State remains TRANS_ABORT until CleanupTransaction().
1107          */
1108         RESUME_INTERRUPTS();
1109 }
1110
1111 /* --------------------------------
1112  *              CleanupTransaction
1113  *
1114  * --------------------------------
1115  */
1116 static void
1117 CleanupTransaction(void)
1118 {
1119         TransactionState s = CurrentTransactionState;
1120
1121         /*
1122          * State should still be TRANS_ABORT from AbortTransaction().
1123          */
1124         if (s->state != TRANS_ABORT)
1125                 elog(FATAL, "CleanupTransaction and not in abort state");
1126
1127         /*
1128          * do abort cleanup processing
1129          */
1130         AtCleanup_Memory();
1131
1132         /*
1133          * done with abort processing, set current transaction state back to
1134          * default
1135          */
1136         s->state = TRANS_DEFAULT;
1137 }
1138
1139 /* --------------------------------
1140  *              StartTransactionCommand
1141  * --------------------------------
1142  */
1143 void
1144 StartTransactionCommand(void)
1145 {
1146         TransactionState s = CurrentTransactionState;
1147
1148         switch (s->blockState)
1149         {
1150                         /*
1151                          * if we aren't in a transaction block, we just do our usual
1152                          * start transaction.
1153                          */
1154                 case TBLOCK_DEFAULT:
1155                         StartTransaction();
1156                         break;
1157
1158                         /*
1159                          * We should never experience this -- if we do it means the
1160                          * BEGIN state was not changed in the previous
1161                          * CommitTransactionCommand().  If we get it, we print a
1162                          * warning and change to the in-progress state.
1163                          */
1164                 case TBLOCK_BEGIN:
1165                         elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_BEGIN");
1166                         s->blockState = TBLOCK_INPROGRESS;
1167                         break;
1168
1169                         /*
1170                          * This is the case when are somewhere in a transaction block
1171                          * and about to start a new command.  For now we do nothing
1172                          * but someday we may do command-local resource
1173                          * initialization.
1174                          */
1175                 case TBLOCK_INPROGRESS:
1176                         break;
1177
1178                         /*
1179                          * As with BEGIN, we should never experience this if we do it
1180                          * means the END state was not changed in the previous
1181                          * CommitTransactionCommand().  If we get it, we print a
1182                          * warning, commit the transaction, start a new transaction
1183                          * and change to the default state.
1184                          */
1185                 case TBLOCK_END:
1186                         elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_END");
1187                         s->blockState = TBLOCK_DEFAULT;
1188                         CommitTransaction();
1189                         StartTransaction();
1190                         break;
1191
1192                         /*
1193                          * Here we are in the middle of a transaction block but one of
1194                          * the commands caused an abort so we do nothing but remain in
1195                          * the abort state.  Eventually we will get to the "END
1196                          * TRANSACTION" which will set things straight.
1197                          */
1198                 case TBLOCK_ABORT:
1199                         break;
1200
1201                         /*
1202                          * This means we somehow aborted and the last call to
1203                          * CommitTransactionCommand() didn't clear the state so we
1204                          * remain in the ENDABORT state and maybe next time we get to
1205                          * CommitTransactionCommand() the state will get reset to
1206                          * default.
1207                          */
1208                 case TBLOCK_ENDABORT:
1209                         elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_ENDABORT");
1210                         break;
1211         }
1212
1213         /*
1214          * We must switch to TransactionCommandContext before returning. This
1215          * is already done if we called StartTransaction, otherwise not.
1216          */
1217         Assert(TransactionCommandContext != NULL);
1218         MemoryContextSwitchTo(TransactionCommandContext);
1219 }
1220
1221 /* --------------------------------
1222  *              CommitTransactionCommand
1223  * --------------------------------
1224  */
1225 void
1226 CommitTransactionCommand(void)
1227 {
1228         TransactionState s = CurrentTransactionState;
1229
1230         switch (s->blockState)
1231         {
1232                         /*
1233                          * if we aren't in a transaction block, we just do our usual
1234                          * transaction commit
1235                          */
1236                 case TBLOCK_DEFAULT:
1237                         CommitTransaction();
1238                         break;
1239
1240                         /*
1241                          * This is the case right after we get a "BEGIN TRANSACTION"
1242                          * command, but the user hasn't done anything else yet, so we
1243                          * change to the "transaction block in progress" state and
1244                          * return.
1245                          */
1246                 case TBLOCK_BEGIN:
1247                         s->blockState = TBLOCK_INPROGRESS;
1248                         break;
1249
1250                         /*
1251                          * This is the case when we have finished executing a command
1252                          * someplace within a transaction block.  We increment the
1253                          * command counter and return.  Someday we may free resources
1254                          * local to the command.
1255                          *
1256                          * That someday is today, at least for memory allocated in
1257                          * TransactionCommandContext. - vadim 03/25/97
1258                          */
1259                 case TBLOCK_INPROGRESS:
1260                         CommandCounterIncrement();
1261                         MemoryContextResetAndDeleteChildren(TransactionCommandContext);
1262                         break;
1263
1264                         /*
1265                          * This is the case when we just got the "END TRANSACTION"
1266                          * statement, so we commit the transaction and go back to the
1267                          * default state.
1268                          */
1269                 case TBLOCK_END:
1270                         CommitTransaction();
1271                         s->blockState = TBLOCK_DEFAULT;
1272                         break;
1273
1274                         /*
1275                          * Here we are in the middle of a transaction block but one of
1276                          * the commands caused an abort so we do nothing but remain in
1277                          * the abort state.  Eventually we will get to the "END
1278                          * TRANSACTION" which will set things straight.
1279                          */
1280                 case TBLOCK_ABORT:
1281                         break;
1282
1283                         /*
1284                          * Here we were in an aborted transaction block which just
1285                          * processed the "END TRANSACTION" command from the user, so
1286                          * clean up and return to the default state.
1287                          */
1288                 case TBLOCK_ENDABORT:
1289                         CleanupTransaction();
1290                         s->blockState = TBLOCK_DEFAULT;
1291                         break;
1292         }
1293 }
1294
1295 /* --------------------------------
1296  *              AbortCurrentTransaction
1297  * --------------------------------
1298  */
1299 void
1300 AbortCurrentTransaction(void)
1301 {
1302         TransactionState s = CurrentTransactionState;
1303
1304         switch (s->blockState)
1305         {
1306                         /*
1307                          * if we aren't in a transaction block, we just do the basic
1308                          * abort & cleanup transaction.
1309                          */
1310                 case TBLOCK_DEFAULT:
1311                         AbortTransaction();
1312                         CleanupTransaction();
1313                         break;
1314
1315                         /*
1316                          * If we are in the TBLOCK_BEGIN it means something screwed up
1317                          * right after reading "BEGIN TRANSACTION" so we enter the
1318                          * abort state.  Eventually an "END TRANSACTION" will fix
1319                          * things.
1320                          */
1321                 case TBLOCK_BEGIN:
1322                         s->blockState = TBLOCK_ABORT;
1323                         AbortTransaction();
1324                         /* CleanupTransaction happens when we exit TBLOCK_ABORT */
1325                         break;
1326
1327                         /*
1328                          * This is the case when are somewhere in a transaction block
1329                          * which aborted so we abort the transaction and set the ABORT
1330                          * state.  Eventually an "END TRANSACTION" will fix things and
1331                          * restore us to a normal state.
1332                          */
1333                 case TBLOCK_INPROGRESS:
1334                         s->blockState = TBLOCK_ABORT;
1335                         AbortTransaction();
1336                         /* CleanupTransaction happens when we exit TBLOCK_ABORT */
1337                         break;
1338
1339                         /*
1340                          * Here, the system was fouled up just after the user wanted
1341                          * to end the transaction block so we abort the transaction
1342                          * and put us back into the default state.
1343                          */
1344                 case TBLOCK_END:
1345                         s->blockState = TBLOCK_DEFAULT;
1346                         AbortTransaction();
1347                         CleanupTransaction();
1348                         break;
1349
1350                         /*
1351                          * Here, we are already in an aborted transaction state and
1352                          * are waiting for an "END TRANSACTION" to come along and lo
1353                          * and behold, we abort again! So we just remain in the abort
1354                          * state.
1355                          */
1356                 case TBLOCK_ABORT:
1357                         break;
1358
1359                         /*
1360                          * Here we were in an aborted transaction block which just
1361                          * processed the "END TRANSACTION" command but somehow aborted
1362                          * again.. since we must have done the abort processing, we
1363                          * clean up and return to the default state.
1364                          */
1365                 case TBLOCK_ENDABORT:
1366                         CleanupTransaction();
1367                         s->blockState = TBLOCK_DEFAULT;
1368                         break;
1369         }
1370 }
1371
1372 /* ----------------------------------------------------------------
1373  *                                         transaction block support
1374  * ----------------------------------------------------------------
1375  */
1376 /* --------------------------------
1377  *              BeginTransactionBlock
1378  * --------------------------------
1379  */
1380 void
1381 BeginTransactionBlock(void)
1382 {
1383         TransactionState s = CurrentTransactionState;
1384
1385         /*
1386          * check the current transaction state
1387          */
1388         if (s->blockState != TBLOCK_DEFAULT)
1389                 elog(NOTICE, "BEGIN: already a transaction in progress");
1390
1391         /*
1392          * set the current transaction block state information appropriately
1393          * during begin processing
1394          */
1395         s->blockState = TBLOCK_BEGIN;
1396
1397         /*
1398          * do begin processing
1399          */
1400
1401         /*
1402          * done with begin processing, set block state to inprogress
1403          */
1404         s->blockState = TBLOCK_INPROGRESS;
1405 }
1406
1407 /* --------------------------------
1408  *              EndTransactionBlock
1409  * --------------------------------
1410  */
1411 void
1412 EndTransactionBlock(void)
1413 {
1414         TransactionState s = CurrentTransactionState;
1415
1416         /*
1417          * check the current transaction state
1418          */
1419         if (s->blockState == TBLOCK_INPROGRESS)
1420         {
1421                 /*
1422                  * here we are in a transaction block which should commit when we
1423                  * get to the upcoming CommitTransactionCommand() so we set the
1424                  * state to "END".      CommitTransactionCommand() will recognize this
1425                  * and commit the transaction and return us to the default state
1426                  */
1427                 s->blockState = TBLOCK_END;
1428                 return;
1429         }
1430
1431         if (s->blockState == TBLOCK_ABORT)
1432         {
1433                 /*
1434                  * here, we are in a transaction block which aborted and since the
1435                  * AbortTransaction() was already done, we do whatever is needed
1436                  * and change to the special "END ABORT" state.  The upcoming
1437                  * CommitTransactionCommand() will recognise this and then put us
1438                  * back in the default state.
1439                  */
1440                 s->blockState = TBLOCK_ENDABORT;
1441                 return;
1442         }
1443
1444         /*
1445          * here, the user issued COMMIT when not inside a transaction. Issue a
1446          * notice and go to abort state.  The upcoming call to
1447          * CommitTransactionCommand() will then put us back into the default
1448          * state.
1449          */
1450         elog(NOTICE, "COMMIT: no transaction in progress");
1451         AbortTransaction();
1452         s->blockState = TBLOCK_ENDABORT;
1453 }
1454
1455 /* --------------------------------
1456  *              AbortTransactionBlock
1457  * --------------------------------
1458  */
1459 #ifdef NOT_USED
1460 static void
1461 AbortTransactionBlock(void)
1462 {
1463         TransactionState s = CurrentTransactionState;
1464
1465         /*
1466          * check the current transaction state
1467          */
1468         if (s->blockState == TBLOCK_INPROGRESS)
1469         {
1470                 /*
1471                  * here we were inside a transaction block something screwed up
1472                  * inside the system so we enter the abort state, do the abort
1473                  * processing and then return. We remain in the abort state until
1474                  * we see an END TRANSACTION command.
1475                  */
1476                 s->blockState = TBLOCK_ABORT;
1477                 AbortTransaction();
1478                 return;
1479         }
1480
1481         /*
1482          * here, the user issued ABORT when not inside a transaction. Issue a
1483          * notice and go to abort state.  The upcoming call to
1484          * CommitTransactionCommand() will then put us back into the default
1485          * state.
1486          */
1487         elog(NOTICE, "ROLLBACK: no transaction in progress");
1488         AbortTransaction();
1489         s->blockState = TBLOCK_ENDABORT;
1490 }
1491 #endif
1492
1493 /* --------------------------------
1494  *              UserAbortTransactionBlock
1495  * --------------------------------
1496  */
1497 void
1498 UserAbortTransactionBlock(void)
1499 {
1500         TransactionState s = CurrentTransactionState;
1501
1502         /*
1503          * if the transaction has already been automatically aborted with an
1504          * error, and the user subsequently types 'abort', allow it.  (the
1505          * behavior is the same as if they had typed 'end'.)
1506          */
1507         if (s->blockState == TBLOCK_ABORT)
1508         {
1509                 s->blockState = TBLOCK_ENDABORT;
1510                 return;
1511         }
1512
1513         if (s->blockState == TBLOCK_INPROGRESS)
1514         {
1515                 /*
1516                  * here we were inside a transaction block and we got an abort
1517                  * command from the user, so we move to the abort state, do the
1518                  * abort processing and then change to the ENDABORT state so we
1519                  * will end up in the default state after the upcoming
1520                  * CommitTransactionCommand().
1521                  */
1522                 s->blockState = TBLOCK_ABORT;
1523                 AbortTransaction();
1524                 s->blockState = TBLOCK_ENDABORT;
1525                 return;
1526         }
1527
1528         /*
1529          * here, the user issued ABORT when not inside a transaction. Issue a
1530          * notice and go to abort state.  The upcoming call to
1531          * CommitTransactionCommand() will then put us back into the default
1532          * state.
1533          */
1534         elog(NOTICE, "ROLLBACK: no transaction in progress");
1535         AbortTransaction();
1536         s->blockState = TBLOCK_ENDABORT;
1537 }
1538
1539 /* --------------------------------
1540  *              AbortOutOfAnyTransaction
1541  *
1542  * This routine is provided for error recovery purposes.  It aborts any
1543  * active transaction or transaction block, leaving the system in a known
1544  * idle state.
1545  * --------------------------------
1546  */
1547 void
1548 AbortOutOfAnyTransaction(void)
1549 {
1550         TransactionState s = CurrentTransactionState;
1551
1552         /*
1553          * Get out of any low-level transaction
1554          */
1555         switch (s->state)
1556         {
1557                 case TRANS_START:
1558                 case TRANS_INPROGRESS:
1559                 case TRANS_COMMIT:
1560                         /* In a transaction, so clean up */
1561                         AbortTransaction();
1562                         CleanupTransaction();
1563                         break;
1564                 case TRANS_ABORT:
1565                         /* AbortTransaction already done, still need Cleanup */
1566                         CleanupTransaction();
1567                         break;
1568                 case TRANS_DEFAULT:
1569                         /* Not in a transaction, do nothing */
1570                         break;
1571         }
1572
1573         /*
1574          * Now reset the high-level state
1575          */
1576         s->blockState = TBLOCK_DEFAULT;
1577 }
1578
1579 bool
1580 IsTransactionBlock(void)
1581 {
1582         TransactionState s = CurrentTransactionState;
1583
1584         if (s->blockState == TBLOCK_INPROGRESS
1585                 || s->blockState == TBLOCK_ABORT
1586                 || s->blockState == TBLOCK_ENDABORT)
1587                 return true;
1588
1589         return false;
1590 }
1591
1592 void
1593 xact_redo(XLogRecPtr lsn, XLogRecord *record)
1594 {
1595         uint8           info = record->xl_info & ~XLR_INFO_MASK;
1596
1597         if (info == XLOG_XACT_COMMIT)
1598         {
1599                 TransactionIdCommit(record->xl_xid);
1600                 /* SHOULD REMOVE FILES OF ALL DROPPED RELATIONS */
1601         }
1602         else if (info == XLOG_XACT_ABORT)
1603         {
1604                 TransactionIdAbort(record->xl_xid);
1605                 /* SHOULD REMOVE FILES OF ALL FAILED-TO-BE-CREATED RELATIONS */
1606         }
1607         else
1608                 elog(STOP, "xact_redo: unknown op code %u", info);
1609 }
1610
1611 void
1612 xact_undo(XLogRecPtr lsn, XLogRecord *record)
1613 {
1614         uint8           info = record->xl_info & ~XLR_INFO_MASK;
1615
1616         if (info == XLOG_XACT_COMMIT)           /* shouldn't be called by XLOG */
1617                 elog(STOP, "xact_undo: can't undo committed xaction");
1618         else if (info != XLOG_XACT_ABORT)
1619                 elog(STOP, "xact_redo: unknown op code %u", info);
1620 }
1621
1622 void
1623 xact_desc(char *buf, uint8 xl_info, char *rec)
1624 {
1625         uint8           info = xl_info & ~XLR_INFO_MASK;
1626
1627         if (info == XLOG_XACT_COMMIT)
1628         {
1629                 xl_xact_commit *xlrec = (xl_xact_commit *) rec;
1630                 struct tm  *tm = localtime(&xlrec->xtime);
1631
1632                 sprintf(buf + strlen(buf), "commit: %04u-%02u-%02u %02u:%02u:%02u",
1633                                 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1634                                 tm->tm_hour, tm->tm_min, tm->tm_sec);
1635         }
1636         else if (info == XLOG_XACT_ABORT)
1637         {
1638                 xl_xact_abort *xlrec = (xl_xact_abort *) rec;
1639                 struct tm  *tm = localtime(&xlrec->xtime);
1640
1641                 sprintf(buf + strlen(buf), "abort: %04u-%02u-%02u %02u:%02u:%02u",
1642                                 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1643                                 tm->tm_hour, tm->tm_min, tm->tm_sec);
1644         }
1645         else
1646                 strcat(buf, "UNKNOWN");
1647 }
1648
1649 void
1650                         XactPushRollback(void (*func) (void *), void *data)
1651 {
1652 #ifdef XLOG_II
1653         if (_RollbackFunc != NULL)
1654                 elog(STOP, "XactPushRollback: already installed");
1655 #endif
1656
1657         _RollbackFunc = func;
1658         _RollbackData = data;
1659 }
1660
1661 void
1662 XactPopRollback(void)
1663 {
1664         _RollbackFunc = NULL;
1665 }