Skip to content

Commit b58c041

Browse files
committed
redo/undo support functions and cleanups.
1 parent e18a862 commit b58c041

File tree

15 files changed

+724
-165
lines changed

15 files changed

+724
-165
lines changed

src/backend/access/heap/heapam.c

+157-96
Large diffs are not rendered by default.

src/backend/access/nbtree/nbtree.c

+28-32
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
1414
* IDENTIFICATION
15-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtree.c,v 1.65 2000/10/13 12:05:20 vadim Exp $
15+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtree.c,v 1.66 2000/10/20 11:01:03 vadim Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -1261,44 +1261,40 @@ _bt_del_item(Relation reln, Buffer buffer, BTItem btitem, bool insert,
12611261
}
12621262

12631263
lp = PageGetItemId(page, offno);
1264-
if (ItemIdDeleted(lp)) /* marked for deletion */
1265-
{
1266-
if (!InRecovery)
1267-
elog(STOP, "btree_%s_undo: deleted target tuple in rollback",
1268-
(insert) ? "insert" : "split");
1269-
}
1270-
else if (InRecovery) /* check heap tuple */
1264+
1265+
if (InRecovery) /* check heap tuple */
12711266
{
1272-
int result;
1273-
CommandId cid;
1274-
RelFileNode hnode;
1275-
Size hsize = (insert) ? SizeOfBtreeInsert : SizeOfBtreeSplit;
1276-
1277-
memcpy(&cid, (char*)xlrec + hsize, sizeof(CommandId));
1278-
memcpy(&hnode, (char*)xlrec + hsize + sizeof(CommandId), sizeof(RelFileNode));
1279-
result = XLogIsOwnerOfTuple(hnode, &(btitem->bti_itup.t_tid),
1280-
record->xl_xid, cid);
1281-
if (result < 0) /* not owner */
1267+
if (!ItemIdDeleted(lp))
12821268
{
1283-
UnlockAndReleaseBuffer(buffer);
1284-
return;
1285-
}
1286-
}
1287-
else if (! BufferIsUpdatable(buffer)) /* normal rollback */
1288-
{
1289-
lp->lp_flags |= LP_DELETE;
1290-
MarkBufferForCleanup(buffer, IndexPageCleanup);
1291-
return;
1292-
}
1269+
int result;
1270+
CommandId cid;
1271+
RelFileNode hnode;
1272+
Size hsize = (insert) ? SizeOfBtreeInsert : SizeOfBtreeSplit;
12931273

1294-
PageIndexTupleDelete(page, offno);
1295-
if (InRecovery)
1296-
{
1274+
memcpy(&cid, (char*)xlrec + hsize, sizeof(CommandId));
1275+
memcpy(&hnode, (char*)xlrec + hsize + sizeof(CommandId), sizeof(RelFileNode));
1276+
result = XLogIsOwnerOfTuple(hnode, &(btitem->bti_itup.t_tid),
1277+
record->xl_xid, cid);
1278+
if (result < 0) /* not owner */
1279+
{
1280+
UnlockAndReleaseBuffer(buffer);
1281+
return;
1282+
}
1283+
}
1284+
PageIndexTupleDelete(page, offno);
12971285
pageop = (BTPageOpaque) PageGetSpecialPointer(page);
12981286
pageop->btpo_flags |= BTP_REORDER;
1287+
UnlockAndWriteBuffer(buffer);
1288+
return;
12991289
}
1300-
UnlockAndWriteBuffer(buffer);
13011290

1291+
/* normal rollback */
1292+
if (ItemIdDeleted(lp)) /* marked for deletion ?! */
1293+
elog(STOP, "btree_%s_undo: deleted target tuple in rollback",
1294+
(insert) ? "insert" : "split");
1295+
1296+
lp->lp_flags |= LP_DELETE;
1297+
MarkBufferForCleanup(buffer, IndexPageCleanup);
13021298
return;
13031299
}
13041300

src/backend/access/transam/xact.c

+105-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.72 2000/10/11 21:28:17 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.73 2000/10/20 11:01:04 vadim Exp $
1212
*
1313
* NOTES
1414
* Transaction aborts can now occur two ways:
@@ -154,6 +154,8 @@
154154
*/
155155
#include "postgres.h"
156156

157+
#include <sys/time.h>
158+
157159
#include "access/nbtree.h"
158160
#include "catalog/heap.h"
159161
#include "catalog/index.h"
@@ -215,6 +217,19 @@ TransactionState CurrentTransactionState = &CurrentTransactionStateData;
215217
int DefaultXactIsoLevel = XACT_READ_COMMITTED;
216218
int XactIsoLevel;
217219

220+
#ifdef XLOG
221+
#include "access/xlogutils.h"
222+
223+
int CommitDelay;
224+
225+
void xact_redo(XLogRecPtr lsn, XLogRecord *record);
226+
void xact_undo(XLogRecPtr lsn, XLogRecord *record);
227+
228+
static void (*_RollbackFunc)(void*) = NULL;
229+
static void *_RollbackData = NULL;
230+
231+
#endif
232+
218233
/* ----------------
219234
* info returned when the system is disabled
220235
*
@@ -676,6 +691,28 @@ RecordTransactionCommit()
676691
*/
677692
TransactionIdCommit(xid);
678693

694+
#ifdef XLOG
695+
{
696+
xl_xact_commit xlrec;
697+
struct timeval delay;
698+
XLogRecPtr recptr;
699+
700+
xlrec.xtime = time(NULL);
701+
/*
702+
* MUST SAVE ARRAY OF RELFILENODE-s TO DROP
703+
*/
704+
recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT,
705+
(char*) &xlrec, SizeOfXactCommit, NULL, 0);
706+
707+
/*
708+
* Sleep before commit! So we can flush more than one
709+
* commit records per single fsync.
710+
*/
711+
delay.tv_sec = 0;
712+
delay.tv_usec = CommitDelay;
713+
(void) select(0, NULL, NULL, NULL, &delay);
714+
}
715+
#endif
679716
/*
680717
* Now write the log info to the disk too.
681718
*/
@@ -785,6 +822,18 @@ RecordTransactionAbort()
785822
if (SharedBufferChanged && !TransactionIdDidCommit(xid))
786823
TransactionIdAbort(xid);
787824

825+
#ifdef XLOG
826+
if (SharedBufferChanged)
827+
{
828+
xl_xact_abort xlrec;
829+
XLogRecPtr recptr;
830+
831+
xlrec.xtime = time(NULL);
832+
recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT,
833+
(char*) &xlrec, SizeOfXactAbort, NULL, 0);
834+
}
835+
#endif
836+
788837
/*
789838
* Tell bufmgr and smgr to release resources.
790839
*/
@@ -1123,10 +1172,13 @@ AbortTransaction()
11231172
AtEOXact_SPI();
11241173
AtEOXact_nbtree();
11251174
AtAbort_Cache();
1126-
AtAbort_Locks();
11271175
AtAbort_Memory();
11281176
AtEOXact_Files();
11291177

1178+
/* Here we'll rollback xaction changes */
1179+
1180+
AtAbort_Locks();
1181+
11301182
SharedBufferChanged = false; /* safest place to do it */
11311183

11321184
/* ----------------
@@ -1663,3 +1715,54 @@ IsTransactionBlock()
16631715

16641716
return false;
16651717
}
1718+
1719+
#ifdef XLOG
1720+
1721+
void
1722+
xact_redo(XLogRecPtr lsn, XLogRecord *record)
1723+
{
1724+
uint8 info = record->xl_info & ~XLR_INFO_MASK;
1725+
1726+
if (info == XLOG_XACT_COMMIT)
1727+
{
1728+
xl_xact_commit *xlrec = (xl_xact_commit*) XLogRecGetData(record);
1729+
1730+
XLogMarkCommitted(record->xl_xid);
1731+
/* MUST REMOVE FILES OF ALL DROPPED RELATIONS */
1732+
}
1733+
else if (info == XLOG_XACT_ABORT)
1734+
{
1735+
XLogMarkAborted(record->xl_xid);
1736+
}
1737+
else
1738+
elog(STOP, "xact_redo: unknown op code %u", info);
1739+
}
1740+
1741+
void
1742+
xact_undo(XLogRecPtr lsn, XLogRecord *record)
1743+
{
1744+
uint8 info = record->xl_info & ~XLR_INFO_MASK;
1745+
1746+
if (info == XLOG_XACT_COMMIT) /* shouldn't be called by XLOG */
1747+
elog(STOP, "xact_undo: can't undo committed xaction");
1748+
else if (info != XLOG_XACT_ABORT)
1749+
elog(STOP, "xact_redo: unknown op code %u", info);
1750+
}
1751+
1752+
void
1753+
XactPushRollback(void (*func) (void *), void* data)
1754+
{
1755+
if (_RollbackFunc != NULL)
1756+
elog(STOP, "XactPushRollback: already installed");
1757+
1758+
_RollbackFunc = func;
1759+
_RollbackData = data;
1760+
}
1761+
1762+
void
1763+
XactPopRollback(void)
1764+
{
1765+
_RollbackFunc = NULL;
1766+
}
1767+
1768+
#endif

src/backend/access/transam/xlog.c

+2-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v 1.17 2000/07/04 01:49:43 vadim Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v 1.18 2000/10/20 11:01:04 vadim Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -40,6 +40,7 @@ char ControlFilePath[MAXPGPATH];
4040
uint32 XLOGbuffers = 0;
4141
XLogRecPtr MyLastRecPtr = {0, 0};
4242
bool StopIfError = false;
43+
bool InRecovery = false;
4344

4445
SPINLOCK ControlFileLockId;
4546
SPINLOCK XidGenLockId;
@@ -163,17 +164,6 @@ typedef struct CheckPoint
163164
#define NextBufIdx(curridx) \
164165
((curridx == XLogCtl->XLogCacheBlck) ? 0 : (curridx + 1))
165166

166-
#define XLByteLT(left, right) \
167-
(right.xlogid > left.xlogid || \
168-
(right.xlogid == left.xlogid && right.xrecoff > left.xrecoff))
169-
170-
#define XLByteLE(left, right) \
171-
(right.xlogid > left.xlogid || \
172-
(right.xlogid == left.xlogid && right.xrecoff >= left.xrecoff))
173-
174-
#define XLByteEQ(left, right) \
175-
(right.xlogid == left.xlogid && right.xrecoff == left.xrecoff)
176-
177167
#define InitXLBuffer(curridx) (\
178168
XLogCtl->xlblocks[curridx].xrecoff = \
179169
(XLogCtl->xlblocks[Insert->curridx].xrecoff == XLogFileSize) ? \

0 commit comments

Comments
 (0)