Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5a1dfde

Browse files
committed
Make use FullTransactionId in 2PC filenames
Switch from using TransactionId to FullTransactionId in naming of 2PC files. Transaction state file in the pg_twophase directory now have extra 8 bytes in the name to address an epoch of a given xid. Author: Maxim Orlov, Aleksander Alekseev, Alexander Korotkov, Teodor Sigaev Author: Nikita Glukhov, Pavel Borisov, Yura Sokolov Reviewed-by: Jacob Champion, Heikki Linnakangas, Alexander Korotkov Reviewed-by: Japin Li, Pavel Borisov, Tom Lane, Peter Eisentraut, Andres Freund Reviewed-by: Andrey Borodin, Dilip Kumar, Aleksander Alekseev Discussion: https://postgr.es/m/CACG%3DezZe1NQSCnfHOr78AtAZxJZeCvxrts0ygrxYwe%3DpyyjVWA%40mail.gmail.com Discussion: https://postgr.es/m/CAJ7c6TPDOYBYrnCAeyndkBktO0WG2xSdYduTF0nxq%2BvfkmTF5Q%40mail.gmail.com
1 parent 2cdf131 commit 5a1dfde

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/backend/access/transam/twophase.c

+45-5
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,46 @@ TwoPhaseGetDummyProc(TransactionId xid, bool lock_held)
942942
/* State file support */
943943
/************************************************************************/
944944

945-
#define TwoPhaseFilePath(path, xid) \
946-
snprintf(path, MAXPGPATH, TWOPHASE_DIR "/%08X", xid)
945+
/*
946+
* Compute the FullTransactionId for the given TransactionId.
947+
*
948+
* The wrap logic is safe here because the span of active xids cannot exceed one
949+
* epoch at any given time.
950+
*/
951+
static inline FullTransactionId
952+
AdjustToFullTransactionId(TransactionId xid)
953+
{
954+
FullTransactionId nextFullXid;
955+
TransactionId nextXid;
956+
uint32 epoch;
957+
958+
Assert(TransactionIdIsValid(xid));
959+
960+
LWLockAcquire(XidGenLock, LW_SHARED);
961+
nextFullXid = ShmemVariableCache->nextXid;
962+
LWLockRelease(XidGenLock);
963+
964+
nextXid = XidFromFullTransactionId(nextFullXid);
965+
epoch = EpochFromFullTransactionId(nextFullXid);
966+
if (unlikely(xid > nextXid))
967+
{
968+
/* Wraparound occured, must be from a prev epoch. */
969+
Assert(epoch > 0);
970+
epoch--;
971+
}
972+
973+
return FullTransactionIdFromEpochAndXid(epoch, xid);
974+
}
975+
976+
static inline int
977+
TwoPhaseFilePath(char *path, TransactionId xid)
978+
{
979+
FullTransactionId fxid = AdjustToFullTransactionId(xid);
980+
981+
return snprintf(path, MAXPGPATH, TWOPHASE_DIR "/%08X%08X",
982+
EpochFromFullTransactionId(fxid),
983+
XidFromFullTransactionId(fxid));
984+
}
947985

948986
/*
949987
* 2PC state file format:
@@ -1882,13 +1920,15 @@ restoreTwoPhaseData(void)
18821920
cldir = AllocateDir(TWOPHASE_DIR);
18831921
while ((clde = ReadDir(cldir, TWOPHASE_DIR)) != NULL)
18841922
{
1885-
if (strlen(clde->d_name) == 8 &&
1886-
strspn(clde->d_name, "0123456789ABCDEF") == 8)
1923+
if (strlen(clde->d_name) == 16 &&
1924+
strspn(clde->d_name, "0123456789ABCDEF") == 16)
18871925
{
18881926
TransactionId xid;
1927+
FullTransactionId fxid;
18891928
char *buf;
18901929

1891-
xid = (TransactionId) strtoul(clde->d_name, NULL, 16);
1930+
fxid = FullTransactionIdFromU64(strtou64(clde->d_name, NULL, 16));
1931+
xid = XidFromFullTransactionId(fxid);
18921932

18931933
buf = ProcessTwoPhaseBuffer(xid, InvalidXLogRecPtr,
18941934
true, false, false);

0 commit comments

Comments
 (0)