10000 Fix low-probability loss of NOTIFY messages due to XID wraparound. · yazun/postgres@525b09a · GitHub
[go: up one dir, main page]

Skip to content

Commit 525b09a

Browse files
committed
Fix low-probability loss of NOTIFY messages due to XID wraparound.
Up to now async.c has used TransactionIdIsInProgress() to detect whether a notify message's source transaction is still running. However, that function has a quick-exit path that reports that XIDs before RecentXmin are no longer running. If a listening backend is doing nothing but listening, and not running any queries, there is nothing that will advance its value of RecentXmin. Once 2 billion transactions elapse, the RecentXmin check causes active transactions to be reported as not running. If they aren't committed yet according to CLOG, async.c decides they aborted and discards their messages. The timing for that is a bit tight but it can happen when multiple backends are sending notifies concurrently. The net symptom therefore is that a sufficiently-long-surviving listen-only backend starts to miss some fraction of NOTIFY traffic, but only under heavy load. The only function that updates RecentXmin is GetSnapshotData(). A brute-force fix would therefore be to take a snapshot before processing incoming notify messages. But that would add cycles, as well as contention for the ProcArrayLock. We can be smarter: having taken the snapshot, let's use that to check for running XIDs, and not call TransactionIdIsInProgress() at all. In this way we reduce the number of ProcArrayLock acquisitions from one per message to one per notify interrupt; that's the same under light load but should be a benefit under heavy load. Light testing says that this change is a wash performance-wise for normal loads. I looked around for other callers of TransactionIdIsInProgress() that might be at similar risk, and didn't find any; all of them are inside transactions that presumably have already taken a snapshot. Problem report and diagnosis by Marko Tiikkaja, patch by me. Back-patch to all supported branches, since it's been like this since 9.0. Discussion: https://postgr.es/m/20170926182935.14128.65278@wrigleys.postgresql.org
1 parent 6d2ef1c commit 525b09a

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/backend/commands/async.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@
133133
#include "utils/builtins.h"
134134
#include "utils/memutils.h"
135135
#include "utils/ps_status.h"
136+
#include "utils/snapmgr.h"
136137
#include "utils/timestamp.h"
138+
#include "utils/tqual.h"
137139

138140

139141
/*
@@ -388,7 +390,8 @@ static bool SignalBackends(void);
388390
static void asyncQueueReadAllNotifications(void);
389391
static bool asyncQueueProcessPageEntries(volatile QueuePosition *current,
390392
QueuePosition stop,
391-
char *page_buffer);
393+
char *page_buffer,
394+
Snapshot snapshot);
392395
static void asyncQueueAdvanceTail(void);
393396
static void ProcessIncomingNotify(void);
394397
static void NotifyMyFrontEnd(const char *channel,
@@ -799,7 +802,7 @@ PreCommit_Notify(void)
799802
}
800803
}
801804

802-
/* Queue any pending notifies */
805+
/* Queue any pending notifies (must happen after the above) */
803806
if (pendingNotifies)
804807
{
805808
ListCell *nextNotify;
@@ -988,7 +991,9 @@ Exec_ListenPreCommit(void)
988991
* have already committed before we started to LISTEN.
989992
*
990993
* Note that we are not yet listening on anything, so we won't deliver any
991-
* notification to the frontend.
994+
* notification to the frontend. Also, although our transaction might
995+
* have executed NOTIFY, those message(s) aren't queued yet so we can't
996+
* see them in the queue.
992997
*
993998
* This will also advance the global tail pointer if possible.
994999
*/
@@ -1837,6 +1842,7 @@ asyncQueueReadAllNotifications(void)
18371842
volatile QueuePosition pos;
18381843
QueuePosition oldpos;
18391844
QueuePosition head;
1845+
Snapshot snapshot;
18401846
bool advanceTail;
18411847

18421848
/* page_buffer must be adequately aligned, so use a union */
@@ -1860,6 +1866,9 @@ asyncQueueReadAllNotifications(void)
18601866
return;
18611867
}
18621868

1869+
/* Get snapshot we'll use to decide which xacts are still in progress */
1870+
snapshot = RegisterSnapshot(GetLatestSnapshot());
1871+
18631872
/*----------
18641873
* Note that we deliver everything that we see in the queue and that
18651874
* matches our _current_ listening state.
@@ -1947,7 +1956,8 @@ asyncQueueReadAllNotifications(void)
19471956
* while sending the notifications to the frontend.
19481957
*/
19491958
reachedStop = asyncQueueProcessPageEntries(&pos, head,
1950-
page_buffer.buf);
1959+
page_buffer.buf,
1960+
snapshot);
19511961
} while (!reachedStop);
19521962
}
19531963
PG_CATCH();
@@ -1975,6 +1985,9 @@ asyncQueueReadAllNotifications(void)
19751985
/* If we were the laziest backend, try to advance the tail pointer */
19761986
if (advanceTail)
19771987
asyncQueueAdvanceTail();
1988+
1989+
/* Done with snapshot */
1990+
UnregisterSnapshot(snapshot);
19781991
}
19791992

19801993
/*
@@ -1996,7 +2009,8 @@ asyncQueueReadAllNotifications(void)
19962009
static bool
19972010
asyncQueueProcessPageEntries(volatile QueuePosition *current,
19982011
QueuePosition stop,
1999-
char *page_buffer)
2012+
char *page_buffer,
2013+
Snapshot snapshot)
20002014
{
20012015
bool reachedStop = false;
20022016
bool reachedEndOfPage;
@@ -2021,7 +2035,7 @@ asyncQueueProcessPageEntries(volatile QueuePosition *current,
20212035
/* Ignore messages destined for other databases */
20222036
if (qe->dboid == MyDatabaseId)
20232037
{
2024-
if (TransactionIdIsInProgress(qe->xid))
2038+
if (XidInMVCCSnapshot(qe->xid, snapshot))
20252039
{
20262040
/*
20272041
* The source transaction is still in progress, so we can't
@@ -2032,10 +2046,15 @@ asyncQueueProcessPageEntries(volatile QueuePosition *current,
20322046
* this advance-then-back-up behavior when dealing with an
20332047
* uncommitted message.)
20342048
*
2035-
* Note that we must test TransactionIdIsInProgress before we
2036-
* test TransactionIdDidCommit, else we might return a message
2037-
* from a transaction that is not yet visible to snapshots;
2038-
* compare the comments at the head of tqual.c.
2049+
* Note that we must test XidInMVCCSnapshot before we test
2050+
* TransactionIdDidCommit, else we might return a message from
2051+
* a transaction that is not yet visible to snapshots; compare
2052+
* the comments at the head of tqual.c.
2053+
*
2054+
* Also, while our own xact won't be listed in the snapshot,
2055+
* we need not check for TransactionIdIsCurrentTransactionId
2056+
* because our transaction cannot (yet) have queued any
2057+
* messages.
20392058
*/
20402059
*current = thisentry;
20412060
reachedStop = true;

src/backend/utils/time/tqual.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ SnapshotData SnapshotSelfData = {HeapTupleSatisfiesSelf};
7272
SnapshotData SnapshotAnyData = {HeapTupleSatisfiesAny};
7373
SnapshotData SnapshotToastData = {HeapTupleSatisfiesToast};
7474

75-
/* local functions */
76-
static bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
7775

7876

7977
/*
@@ -1282,10 +1280,11 @@ HeapTupleIsSurelyDead(HeapTupleHeader tuple, TransactionId OldestXmin)
12821280
*
12831281
* Note: GetSnapshotData never stores either top xid or subxids of our own
12841282
* backend into a snapshot, so these xids will not be reported as "running"
1285-
* by this function. This is OK for current uses, because we actually only
1286-
* apply this for known-committed XIDs.
1283+
* by this function. This is OK for current uses, because we always check
1284+
* TransactionIdIsCurrentTransactionId first, except when it's known the
1285+
* XID could not be ours anyway.
12871286
*/
1288-
static bool
1287+
bool
12891288
XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
12901289
{
12911290
uint32 i;

src/include/utils/tqual.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extern HTSV_Result HeapTupleSatisfiesVacuum(HeapTupleHeader tuple,
8585
TransactionId OldestXmin, Buffer buffer);
8686
extern bool HeapTupleIsSurelyDead(HeapTupleHeader tuple,
8787
TransactionId OldestXmin);
88+
extern bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
8889

8990
extern void HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer,
9091
uint16 infomask, TransactionId xid);

0 commit comments

Comments
 (0)
0