Skip to content

Commit 69bc245

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 13d2ed9 commit 69bc245

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
@@ -136,7 +136,9 @@
136136
#include "utils/builtins.h"
137137
#include "utils/memutils.h"
138138
#include "utils/ps_status.h"
139+
#include "utils/snapmgr.h"
139140
#include "utils/timestamp.h"
141+
#include "utils/tqual.h"
140142

141143

142144
/*
@@ -385,7 +387,8 @@ static bool SignalBackends(void);
385387
static void asyncQueueReadAllNotifications(void);
386388
static bool asyncQueueProcessPageEntries(volatile QueuePosition *current,
387389
QueuePosition stop,
388-
char *page_buffer);
390+
char *page_buffer,
391+
Snapshot snapshot);
389392
static void asyncQueueAdvanceTail(void);
390393
static void ProcessIncomingNotify(void);
391394
static void NotifyMyFrontEnd(const char *channel,
@@ -796,7 +799,7 @@ PreCommit_Notify(void)
796799
}
797800
}
798801

799-
/* Queue any pending notifies */
802+
/* Queue any pending notifies (must happen after the above) */
800803
if (pendingNotifies)
801804
{
802805
ListCell *nextNotify;
@@ -985,7 +988,9 @@ Exec_ListenPreCommit(void)
985988
* have already committed before we started to LISTEN.
986989
*
987990
* Note that we are not yet listening on anything, so we won't deliver any
988-
* notification to the frontend.
991+
* notification to the frontend. Also, although our transaction might
992+
* have executed NOTIFY, those message(s) aren't queued yet so we can't
993+
* see them in the queue.
989994
*
990995
* This will also advance the global tail pointer if possible.
991996
*/
@@ -1715,6 +1720,7 @@ asyncQueueReadAllNotifications(void)
17151720
volatile QueuePosition pos;
17161721
QueuePosition oldpos;
17171722
QueuePosition head;
1723+
Snapshot snapshot;
17181724
bool advanceTail;
17191725

17201726
/* page_buffer must be adequately aligned, so use a union */
@@ -1738,6 +1744,9 @@ asyncQueueReadAllNotifications(void)
17381744
return;
17391745
}
17401746

1747+
/* Get snapshot we'll use to decide which xacts are still in progress */
1748+
snapshot = RegisterSnapshot(GetLatestSnapshot());
1749+
17411750
/*----------
17421751
* Note that we deliver everything that we see in the queue and that
17431752
* matches our _current_ listening state.
@@ -1825,7 +1834,8 @@ asyncQueueReadAllNotifications(void)
18251834
* while sending the notifications to the frontend.
18261835
*/
18271836
reachedStop = asyncQueueProcessPageEntries(&pos, head,
1828-
page_buffer.buf);
1837+
page_buffer.buf,
1838+
snapshot);
18291839
} while (!reachedStop);
18301840
}
18311841
PG_CATCH();
@@ -1853,6 +1863,9 @@ asyncQueueReadAllNotifications(void)
18531863
/* If we were the laziest backend, try to advance the tail pointer */
18541864
if (advanceTail)
18551865
asyncQueueAdvanceTail();
1866+
1867+
/* Done with snapshot */
1868+
UnregisterSnapshot(snapshot);
18561869
}
18571870

18581871
/*
@@ -1874,7 +1887,8 @@ asyncQueueReadAllNotifications(void)
18741887
static bool
18751888
asyncQueueProcessPageEntries(volatile QueuePosition *current,
18761889
QueuePosition stop,
1877-
char *page_buffer)
1890+
char *page_buffer,
1891+
Snapshot snapshot)
18781892
{
18791893
bool reachedStop = false;
18801894
bool reachedEndOfPage;
@@ -1899,7 +1913,7 @@ asyncQueueProcessPageEntries(volatile QueuePosition *current,
18991913
/* Ignore messages destined for other databases */
19001914
if (qe->dboid == MyDatabaseId)
19011915
{
1902-
if (TransactionIdIsInProgress(qe->xid))
1916+
if (XidInMVCCSnapshot(qe->xid, snapshot))
19031917
{
19041918
/*
19051919
* The source transaction is still in progress, so we can't
@@ -1910,10 +1924,15 @@ asyncQueueProcessPageEntries(volatile QueuePosition *current,
19101924
* this advance-then-back-up behavior when dealing with an
19111925
* uncommitted message.)
19121926
*
1913-
* Note that we must test TransactionIdIsInProgress before we
1914-
* test TransactionIdDidCommit, else we might return a message
1915-
* from a transaction that is not yet visible to snapshots;
1916-
* compare the comments at the head of tqual.c.
1927+
* Note that we must test XidInMVCCSnapshot before we test
1928+
* TransactionIdDidCommit, else we might return a message from
1929+
* a transaction that is not yet visible to snapshots; compare
1930+
* the comments at the head of tqual.c.
1931+
*
1932+
* Also, while our own xact won't be listed in the snapshot,
1933+
* we need not check for TransactionIdIsCurrentTransactionId
1934+
* because our transaction cannot (yet) have queued any
1935+
* messages.
19171936
*/
19181937
*current = thisentry;
19191938
reachedStop = true;

src/backend/utils/time/tqual.c

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

77-
/* local functions */
78-
static bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
7977

8078
/*
8179
* SetHintBits()
@@ -1435,10 +1433,11 @@ HeapTupleIsSurelyDead(HeapTuple htup, TransactionId OldestXmin)
14351433
*
14361434
* Note: GetSnapshotData never stores either top xid or subxids of our own
14371435
* backend into a snapshot, so these xids will not be reported as "running"
1438-
* by this function. This is OK for current uses, because we actually only
1439-
* apply this for known-committed XIDs.
1436+
* by this function. This is OK for current uses, because we always check
1437+
* TransactionIdIsCurrentTransactionId first, except when it's known the
1438+
* XID could not be ours anyway.
14401439
*/
1441-
static bool
1440+
bool
14421441
XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
14431442
{
14441443
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(HeapTuple htup,
8585
TransactionId OldestXmin, Buffer buffer);
8686
extern bool HeapTupleIsSurelyDead(HeapTuple htup,
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)