Skip to content

Commit b91a28c

Browse files
committed
Replay works for soft and hard restart
1 parent 4d63969 commit b91a28c

File tree

5 files changed

+53
-51
lines changed

5 files changed

+53
-51
lines changed

reinit.sh

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,34 @@ reinit_slave() {
4040

4141
make install > /dev/null
4242

43+
44+
cat <<MSG
45+
###############################################################################
46+
# Check that we can commit after soft restart.
47+
# Here checkpoint happens before shutdown and no WAL replay will not occur
48+
# during start. So code should re-create memory state from files.
49+
###############################################################################
50+
MSG
51+
52+
pkill -9 postgres
53+
reinit_master >> /dev/null
54+
psql <<SQL
55+
begin;
56+
insert into t values (42);
57+
prepare transaction 'x';
58+
SQL
59+
./install/bin/pg_ctl -w -D ./install/data -l ./install/data/logfile restart
60+
psql <<SQL
61+
commit prepared 'x';
62+
SQL
63+
64+
65+
4366
cat <<MSG
4467
###############################################################################
45-
# Check that we can commit after hard restart
68+
# Check that we can commit after hard restart.
69+
# On startup WAL replay will re-create memory for global transactions that
70+
# happend after last checkpoint and stored. After that
4671
###############################################################################
4772
MSG
4873

@@ -61,23 +86,6 @@ SQL
6186

6287

6388

64-
# cat <<MSG
65-
# ###############################################################################
66-
# # Check that we can commit after soft restart
67-
# ###############################################################################
68-
# MSG
69-
70-
# pkill -9 postgres
71-
# reinit_master >> /dev/null
72-
# psql <<SQL
73-
# begin;
74-
# insert into t values (42);
75-
# prepare transaction 'x';
76-
# SQL
77-
# ./install/bin/pg_ctl -w -D ./install/data -l ./install/data/logfile restart
78-
# psql <<SQL
79-
# commit prepared 'x';
80-
# SQL
8189

8290

8391

src/backend/access/transam/twophase.c

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,21 +1848,21 @@ StandbyRecoverPreparedTransactions(bool overwriteOK)
18481848
}
18491849

18501850
/*
1851-
* RecoverPreparedTransactions
1851+
* RecoverPreparedFromFiles
18521852
*
18531853
* Scan the pg_twophase directory and reload shared-memory state for each
18541854
* prepared transaction (reacquire locks, etc). This is run during database
18551855
* startup.
18561856
*/
18571857
void
1858-
RecoverPreparedTransactions(void)
1858+
RecoverPreparedFromFiles(void)
18591859
{
18601860
char dir[MAXPGPATH];
18611861
DIR *cldir;
18621862
struct dirent *clde;
18631863
bool overwriteOK = false;
18641864

1865-
fprintf(stderr, "===(%u) RecoverPreparedTransactions called\n", getpid());
1865+
fprintf(stderr, "===(%u) RecoverPreparedFromFiles called\n", getpid());
18661866

18671867
snprintf(dir, MAXPGPATH, "%s", TWOPHASE_DIR);
18681868

@@ -1879,9 +1879,23 @@ RecoverPreparedTransactions(void)
18791879
TransactionId *subxids;
18801880
GlobalTransaction gxact;
18811881
int i;
1882+
PGXACT *pgxact;
1883+
18821884

18831885
xid = (TransactionId) strtoul(clde->d_name, NULL, 16);
18841886

1887+
/* Already recovered from WAL? */
1888+
for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
1889+
{
1890+
gxact = TwoPhaseState->prepXacts[i];
1891+
pgxact = &ProcGlobal->allPgXact[gxact->pgprocno];
1892+
1893+
1894+
fprintf(stderr, "! %x ?= %x\n", xid, pgxact->xid);
1895+
if (xid == pgxact->xid)
1896+
goto next_file;
1897+
}
1898+
18851899
/* Already processed? */
18861900
if (TransactionIdDidCommit(xid) || TransactionIdDidAbort(xid))
18871901
{
@@ -1969,13 +1983,17 @@ RecoverPreparedTransactions(void)
19691983

19701984
pfree(buf);
19711985
}
1986+
1987+
next_file:
1988+
continue;
1989+
19721990
}
19731991
FreeDir(cldir);
19741992
}
19751993

19761994

19771995
void
1978-
RecoverPreparedTransaction(XLogReaderState *record)
1996+
RecoverPreparedFromXLOG(XLogReaderState *record)
19791997
{
19801998
bool overwriteOK = false;
19811999
TransactionId xid = XLogRecGetXid(record);
@@ -1986,31 +2004,7 @@ RecoverPreparedTransaction(XLogReaderState *record)
19862004
GlobalTransaction gxact;
19872005
int i;
19882006

1989-
fprintf(stderr, "===(%u) RecoverPreparedTransactioNN called\n", getpid());
1990-
1991-
// /* Already processed? */
1992-
// if (TransactionIdDidCommit(xid) || TransactionIdDidAbort(xid))
1993-
// {
1994-
// ereport(WARNING,
1995-
// (errmsg("removing stale two-phase state file \"%s\"",
1996-
// clde->d_name)));
1997-
// RemoveTwoPhaseFile(xid, true);
1998-
// continue;
1999-
// }
2000-
2001-
// /* Read and validate file */
2002-
// buf = ReadTwoPhaseFile(xid, true);
2003-
// if (buf == NULL)
2004-
// {
2005-
// ereport(WARNING,
2006-
// (errmsg("removing corrupt two-phase state file \"%s\"",
2007-
// clde->d_name)));
2008-
// RemoveTwoPhaseFile(xid, true);
2009-
// continue;
2010-
// }
2011-
2012-
// ereport(LOG,
2013-
// (errmsg("recovering prepared transaction %u", xid)));
2007+
fprintf(stderr, "===(%u) RecoverPreparedFromXLOG called\n", getpid());
20142008

20152009
/* Deconstruct header */
20162010
hdr = (TwoPhaseFileHeader *) buf;

src/backend/access/transam/xact.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5601,7 +5601,7 @@ xact_redo(XLogReaderState *record)
56015601
// (char *) XLogRecGetData(record), XLogRecGetDataLen(record));
56025602
fprintf(stderr, "=== Recovering tx %lx : %lx \n", record->ReadRecPtr, record->EndRecPtr);
56035603

5604-
RecoverPreparedTransaction(record);
5604+
RecoverPreparedFromXLOG(record);
56055605
}
56065606
else if (info == XLOG_XACT_ASSIGNMENT)
56075607
{

src/backend/access/transam/xlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7351,7 +7351,7 @@ StartupXLOG(void)
73517351
TrimMultiXact();
73527352

73537353
/* Reload shared-memory state for prepared transactions */
7354-
RecoverPreparedTransactions();
7354+
RecoverPreparedFromFiles();
73557355

73567356
/*
73577357
* Shutdown the recovery environment. This must occur after

src/include/access/twophase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ extern bool StandbyTransactionIdIsPrepared(TransactionId xid);
4747
extern TransactionId PrescanPreparedTransactions(TransactionId **xids_p,
4848
int *nxids_p);
4949
extern void StandbyRecoverPreparedTransactions(bool overwriteOK);
50-
extern void RecoverPreparedTransactions(void);
51-
extern void RecoverPreparedTransaction(XLogReaderState *record);
50+
extern void RecoverPreparedFromFiles(void);
51+
extern void RecoverPreparedFromXLOG(XLogReaderState *record);
5252

5353
extern void RecreateTwoPhaseFile(TransactionId xid, void *content, int len);
5454
extern void RemoveTwoPhaseFile(TransactionId xid, bool giveWarning);

0 commit comments

Comments
 (0)