Skip to content

Commit f192e1b

Browse files
committed
Fix ordering of XIDs in ProcArrayApplyRecoveryInfo
Commit 8431e29 reworked ProcArrayApplyRecoveryInfo to sort XIDs before adding them to KnownAssignedXids. But the XIDs are sorted using xidComparator, which compares the XIDs simply as uint32 values, not logically. KnownAssignedXidsAdd() however expects XIDs in logical order, and calls TransactionIdFollowsOrEquals() to enforce that. If there are XIDs for which the two orderings disagree, an error is raised and the recovery fails/restarts. Hitting this issue is fairly easy - you just need two transactions, one started before the 4B limit (e.g. XID 4294967290), the other sometime after it (e.g. XID 1000). Logically (4294967290 <= 1000) but when compared using xidComparator we try to add them in the opposite order. Which makes KnownAssignedXidsAdd() fail with an error like this: ERROR: out-of-order XID insertion in KnownAssignedXids This only happens during replica startup, while processing RUNNING_XACTS records to build the snapshot. Once we reach STANDBY_SNAPSHOT_READY, we skip these records. So this does not affect already running replicas, but if you restart (or create) a replica while there are transactions with XIDs for which the two orderings disagree, you may hit this. Long-running transactions and frequent replica restarts increase the likelihood of hitting this issue. Once the replica gets into this state, it can't be started (even if the old transactions are terminated). Fixed by sorting the XIDs logically - this is fine because we're dealing with normal XIDs (because it's XIDs assigned to backends) and from the same wraparound epoch (otherwise the backends could not be running at the same time on the primary node). So there are no problems with the triangle inequality, which is why xidComparator compares raw values. Investigation and root cause analysis by Abhijit Menon-Sen. Patch by me. This issue is present in all releases since 9.4, however releases up to 9.6 are EOL already so backpatch to 10 only. Reviewed-by: Abhijit Menon-Sen Reviewed-by: Alvaro Herrera Backpatch-through: 10 Discussion: https://postgr.es/m/36b8a501-5d73-277c-4972-f58a4dce088a%40enterprisedb.com
1 parent c9cfc86 commit f192e1b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/backend/storage/ipc/procarray.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,8 +1164,13 @@ ProcArrayApplyRecoveryInfo(RunningTransactions running)
11641164
/*
11651165
* Sort the array so that we can add them safely into
11661166
* KnownAssignedXids.
1167+
*
1168+
* We have to sort them logically, because in KnownAssignedXidsAdd we
1169+
* call TransactionIdFollowsOrEquals and so on. But we know these XIDs
1170+
* come from RUNNING_XACTS, which means there are only normal XIDs from
1171+
* the same epoch, so this is safe.
11671172
*/
1168-
qsort(xids, nxids, sizeof(TransactionId), xidComparator);
1173+
qsort(xids, nxids, sizeof(TransactionId), xidLogicalComparator);
11691174

11701175
/*
11711176
* Add the sorted snapshot into KnownAssignedXids. The running-xacts

src/backend/utils/adt/xid.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,32 @@ xidComparator(const void *arg1, const void *arg2)
145145
return 0;
146146
}
147147

148+
/*
149+
* xidLogicalComparator
150+
* qsort comparison function for XIDs
151+
*
152+
* This is used to compare only XIDs from the same epoch (e.g. for backends
153+
* running at the same time). So there must be only normal XIDs, so there's
154+
* no issue with triangle inequality.
155+
*/
156+
int
157+
xidLogicalComparator(const void *arg1, const void *arg2)
158+
{
159+
TransactionId xid1 = *(const TransactionId *) arg1;
160+
TransactionId xid2 = *(const TransactionId *) arg2;
161+
162+
Assert(TransactionIdIsNormal(xid1));
163+
Assert(TransactionIdIsNormal(xid2));
164+
165+
if (TransactionIdPrecedes(xid1, xid2))
166+
return -1;
167+
168+
if (TransactionIdPrecedes(xid2, xid1))
169+
return 1;
170+
171+
return 0;
172+
}
173+
148174
Datum
149175
xid8toxid(PG_FUNCTION_ARGS)
150176
{

src/include/utils/builtins.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extern void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len);
8787

8888
/* xid.c */
8989
extern int xidComparator(const void *arg1, const void *arg2);
90+
extern int xidLogicalComparator(const void *arg1, const void *arg2);
9091

9192
/* inet_cidr_ntop.c */
9293
extern char *pg_inet_cidr_ntop(int af, const void *src, int bits,

0 commit comments

Comments
 (0)