Skip to content

Commit af6be20

Browse files
committed
take into account WAL-restore xacts in Prescan
1 parent ef5ef89 commit af6be20

File tree

4 files changed

+68
-36
lines changed

4 files changed

+68
-36
lines changed

reinit.sh

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ reinit_master() {
1616
echo "max_wal_senders = 2" >> ./install/data/postgresql.conf
1717
echo "max_replication_slots = 2" >> ./install/data/postgresql.conf
1818

19-
echo '' > ./install/data/logfile
19+
# echo '' > ./install/data/logfile
2020

2121
echo 'local replication stas trust' >> ./install/data/pg_hba.conf
2222

@@ -211,28 +211,31 @@ make install > /dev/null
211211

212212

213213

214-
cat <<MSG
215-
###############################################################################
216-
# Check that we can commit after soft restart.
217-
# Here checkpoint happens before shutdown and no WAL replay will not occur
218-
# during start. So code should re-create memory state from files.
219-
###############################################################################
220-
MSG
214+
# cat <<MSG
215+
# ###############################################################################
216+
# # Check that we can commit after soft restart.
217+
# # Here checkpoint happens before shutdown and no WAL replay will not occur
218+
# # during start. So code should re-create memory state from files.
219+
# ###############################################################################
220+
# MSG
221221

222-
pkill -9 postgres
223-
reinit_master >> /dev/null
224-
psql <<SQL
225-
begin;
226-
insert into t values (42);
227-
savepoint s1;
228-
insert into t values (43);
229-
prepare transaction 'x';
230-
insert into t values (100);
231-
SQL
232-
./install/bin/pg_ctl -w -D ./install/data -l ./install/data/logfile restart
233-
psql <<SQL
234-
commit prepared 'x';
235-
SQL
222+
# pkill -9 postgres
223+
# reinit_master >> /dev/null
224+
# psql <<SQL
225+
# begin;
226+
# select txid_current();
227+
# insert into t values (42);
228+
# savepoint s1;
229+
# insert into t values (43);
230+
# prepare transaction 'x';
231+
# SQL
232+
# ./install/bin/pg_ctl -w -D ./install/data -l ./install/data/logfile restart
233+
# echo "xmin should be equal to xip of prepared tx"
234+
# psql <<SQL
235+
# insert into t values (44);
236+
# select txid_current_snapshot();
237+
# commit prepared 'x';
238+
# SQL
236239

237240

238241

@@ -248,24 +251,25 @@ pkill -9 postgres
248251
reinit_master >> /dev/null
249252
psql <<SQL
250253
begin;
254+
select txid_current();
251255
insert into t values (42);
252256
savepoint s1;
253257
insert into t values (43);
254-
select txid_current();
255258
prepare transaction 'x';
256259
SQL
257260
pkill -9 postgres
258261
./install/bin/pg_ctl -w -D ./install/data -l ./install/data/logfile start
262+
echo "xmin should be equal to xip of prepared tx"
259263
psql <<SQL
260-
select txid_current();
261264
insert into t values (44);
265+
select txid_current_snapshot();
262266
commit prepared 'x';
263267
SQL
264268

265269

266270

267271

268-
272+
# try to replay several tx with same name
269273

270274

271275

src/backend/access/transam/twophase.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#include "storage/smgr.h"
8585
#include "utils/builtins.h"
8686
#include "utils/memutils.h"
87+
#include "utils/snapmgr.h"
8788
#include "utils/timestamp.h"
8889

8990

@@ -1708,15 +1709,27 @@ TransactionId
17081709
PrescanPreparedTransactions(TransactionId **xids_p, int *nxids_p)
17091710
{
17101711
TransactionId origNextXid = ShmemVariableCache->nextXid;
1712+
1713+
/* By this we will take into account xacts restored to memory */
1714+
// TransactionId origNextXid = GetOldestSafeDecodingTransactionId();
1715+
17111716
TransactionId result = origNextXid;
17121717
DIR *cldir;
17131718
struct dirent *clde;
17141719
TransactionId *xids = NULL;
17151720
int nxids = 0;
17161721
int allocsize = 0;
17171722

1723+
17181724
fprintf(stderr, "===(%u) PrescanPreparedTransactions called. result = %u\n", getpid(), result);
17191725

1726+
/*
1727+
* Since we want to find minimum among prepared xacts we can use that function ignoring
1728+
* KnownAssignedXids.
1729+
* Other option is just to iterate here through the procarray.
1730+
*/
1731+
result = GetOldestActiveTransactionId();
1732+
17201733
cldir = AllocateDir(TWOPHASE_DIR);
17211734
while ((clde = ReadDir(cldir, TWOPHASE_DIR)) != NULL)
17221735
{
@@ -1952,22 +1965,32 @@ RecoverPreparedFromFiles(void)
19521965
TransactionId *subxids;
19531966
GlobalTransaction gxact;
19541967
int i;
1955-
PGXACT *pgxact;
1968+
// PGXACT *pgxact;
19561969

19571970

19581971
xid = (TransactionId) strtoul(clde->d_name, NULL, 16);
19591972

19601973
/* Already recovered from WAL? */
1961-
for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
1974+
if (TransactionIdIsInProgress(xid))
19621975
{
1963-
gxact = TwoPhaseState->prepXacts[i];
1964-
pgxact = &ProcGlobal->allPgXact[gxact->pgprocno];
1976+
fprintf(stderr, "! xid %x is in progress\n", xid);
1977+
continue;
1978+
}
1979+
1980+
// /* Already recovered from WAL? */
1981+
// for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
1982+
// {
1983+
// gxact = TwoPhaseState->prepXacts[i];
1984+
// pgxact = &ProcGlobal->allPgXact[gxact->pgprocno];
19651985

19661986

1967-
fprintf(stderr, "! %x ?= %x\n", xid, pgxact->xid);
1968-
if (xid == pgxact->xid)
1969-
goto next_file;
1970-
}
1987+
// fprintf(stderr, "! %x ?= %x\n", xid, pgxact->xid);
1988+
1989+
1990+
1991+
// if (xid == pgxact->xid)
1992+
// goto next_file;
1993+
// }
19711994

19721995
/* Already processed? */
19731996
if (TransactionIdDidCommit(xid) || TransactionIdDidAbort(xid))
@@ -2057,8 +2080,8 @@ RecoverPreparedFromFiles(void)
20572080
pfree(buf);
20582081
}
20592082

2060-
next_file:
2061-
continue;
2083+
// next_file:
2084+
// continue;
20622085

20632086
}
20642087
FreeDir(cldir);

src/backend/access/transam/xlog.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6563,7 +6563,10 @@ StartupXLOG(void)
65636563
InitRecoveryTransactionEnvironment();
65646564

65656565
if (wasShutdown)
6566+
{
6567+
fprintf(stderr, "=== PrescanPreparedTransactions #1\n");
65666568
oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
6569+
}
65676570
else
65686571
oldestActiveXID = checkPoint.oldestActiveXid;
65696572
Assert(TransactionIdIsValid(oldestActiveXID));
@@ -7163,6 +7166,7 @@ StartupXLOG(void)
71637166
XLogCtl->LogwrtRqst.Flush = EndOfLog;
71647167

71657168
/* Pre-scan prepared transactions to find out the range of XIDs present */
7169+
fprintf(stderr, "=== PrescanPreparedTransactions #2\n");
71667170
oldestActiveXID = PrescanPreparedTransactions(NULL, NULL);
71677171

71687172
/*
@@ -9258,6 +9262,7 @@ xlog_redo(XLogReaderState *record)
92589262
TransactionId latestCompletedXid;
92599263
RunningTransactionsData running;
92609264

9265+
fprintf(stderr, "=== PrescanPreparedTransactions #3\n");
92619266
oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
92629267

92639268
/*

src/backend/storage/ipc/procarray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,7 @@ GetOldestActiveTransactionId(void)
20612061
TransactionId oldestRunningXid;
20622062
int index;
20632063

2064-
Assert(!RecoveryInProgress());
2064+
// Assert(!RecoveryInProgress());
20652065

20662066
LWLockAcquire(ProcArrayLock, LW_SHARED);
20672067

0 commit comments

Comments
 (0)