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

Commit 118e99c

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 46912d9 commit 118e99c

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/backend/commands/async.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@
137137
#include "utils/builtins.h"
138138
#include "utils/memutils.h"
139139
#include "utils/ps_status.h"
140+
#include "utils/snapmgr.h"
140141
#include "utils/timestamp.h"
142+
#include "utils/tqual.h"
141143

142144

143145
/*
@@ -387,7 +389,8 @@ static bool SignalBackends(void);
387389
static void asyncQueueReadAllNotifications(void);
388390
static bool asyncQueueProcessPageEntries(volatile QueuePosition *current,
389391
QueuePosition stop,
390-
char *page_buffer);
392+
char *page_buffer,
393+
Snapshot snapshot);
391394
static void asyncQueueAdvanceTail(void);
392395
static void ProcessIncomingNotify(void);
393396
static bool AsyncExistsPendingNotify(const char *channel, const char *payload);
@@ -798,7 +801,7 @@ PreCommit_Notify(void)
798801
}
799802
}
800803

801-
/* Queue any pending notifies */
804+
/* Queue any pending notifies (must happen after the above) */
802805
if (pendingNotifies)
803806
{
804807
ListCell *nextNotify;
@@ -987,7 +990,9 @@ Exec_ListenPreCommit(void)
987990
* have already committed before we started to LISTEN.
988991
*
989992
* Note that we are not yet listening on anything, so we won't deliver any
990-
* notification to the frontend.
993+
* notification to the frontend. Also, although our transaction might
994+
* have executed NOTIFY, those message(s) aren't queued yet so we can't
995+
* see them in the queue.
991996
*
992997
* This will also advance the global tail pointer if possible.
993998
*/
@@ -1744,6 +1749,7 @@ asyncQueueReadAllNotifications(void)
17441749
volatile QueuePosition pos;
17451750
QueuePosition oldpos;
17461751
QueuePosition head;
1752+
Snapshot snapshot;
17471753
bool advanceTail;
17481754

17491755
/* page_buffer must be adequately aligned, so use a union */
@@ -1767,6 +1773,9 @@ asyncQueueReadAllNotifications(void)
17671773
return;
17681774
}
17691775

1776+
/* Get snapshot we'll use to decide which xacts are still in progress */
1777+
snapshot = RegisterSnapshot(GetLatestSnapshot());
1778+
17701779
/*----------
17711780
* Note that we deliver everything that we see in the queue and that
17721781
* matches our _current_ listening state.
@@ -1854,7 +1863,8 @@ asyncQueueReadAllNotifications(void)
18541863
* while sending the notifications to the frontend.
18551864
*/
18561865
reachedStop = asyncQueueProcessPageEntries(&pos, head,
1857-
page_buffer.buf);
1866+
page_buffer.buf,
1867+
snapshot);
18581868
} while (!reachedStop);
18591869
}
18601870
PG_CATCH();
@@ -1882,6 +1892,9 @@ asyncQueueReadAllNotifications(void)
18821892
/* If we were the laziest backend, try to advance the tail pointer */
18831893
if (advanceTail)
18841894
asyncQueueAdvanceTail();
1895+
1896+
/* Done with snapshot */
1897+
UnregisterSnapshot(snapshot);
18851898
}
18861899

18871900
/*
@@ -1903,7 +1916,8 @@ asyncQueueReadAllNotifications(void)
19031916
static bool
19041917
asyncQueueProcessPageEntries(volatile QueuePosition *current,
19051918
QueuePosition stop,
1906-
char *page_buffer)
1919+
char *page_buffer,
1920+
Snapshot snapshot)
19071921
{
19081922
bool reachedStop = false;
19091923
bool reachedEndOfPage;
@@ -1928,7 +1942,7 @@ asyncQueueProcessPageEntries(volatile QueuePosition *current,
19281942
/* Ignore messages destined for other databases */
19291943
if (qe->dboid == MyDatabaseId)
19301944
{
1931-
if (TransactionIdIsInProgress(qe->xid))
1945+
if (XidInMVCCSnapshot(qe->xid, snapshot))
19321946
{
19331947
/*
19341948
* The source transaction is still in progress, so we can't
@@ -1939,10 +1953,15 @@ asyncQueueProcessPageEntries(volatile QueuePosition *current,
19391953
* this advance-then-back-up behavior when dealing with an
19401954
* uncommitted message.)
19411955
*
1942-
* Note that we must test TransactionIdIsInProgress before we
1943-
* test TransactionIdDidCommit, else we might return a message
1944-
* from a transaction that is not yet visible to snapshots;
1945-
* compare the comments at the head of tqual.c.
1956+
* Note that we must test XidInMVCCSnapshot before we test
1957+
* TransactionIdDidCommit, else we might return a message from
1958+
* a transaction that is not yet visible to snapshots; compare
1959+
* the comments at the head of tqual.c.
1960+
*
1961+
* Also, while our own xact won't be listed in the snapshot,
1962+
* we need not check for TransactionIdIsCurrentTransactionId
1963+
* because our transaction cannot (yet) have queued any
1964+
* messages.
19461965
*/
19471966
*current = thisentry;
19481967
reachedStop = true;

src/backend/utils/time/tqual.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@
8181
SnapshotData SnapshotSelfData = {HeapTupleSatisfiesSelf};
8282
SnapshotData SnapshotAnyData = {HeapTupleSatisfiesAny};
8383

84-
/* local functions */
85-
static bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
8684

8785
/*
8886
* SetHintBits()
@@ -1479,10 +1477,10 @@ HeapTupleIsSurelyDead(HeapTuple htup, TransactionId OldestXmin)
14791477
* Note: GetSnapshotData never stores either top xid or subxids of our own
14801478
* backend into a snapshot, so these xids will not be reported as "running"
14811479
* by this function. This is OK for current uses, because we always check
1482-
* TransactionIdIsCurrentTransactionId first, except for known-committed
1483-
* XIDs which could not be ours anyway.
1480+
* TransactionIdIsCurrentTransactionId first, except when it's known the
1481+
* XID could not be ours anyway.
14841482
*/
1485-
static bool
1483+
bool
14861484
XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
14871485
{
14881486
uint32 i;

src/include/utils/tqual.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ extern HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup,
7878
TransactionId OldestXmin, Buffer buffer);
7979
extern bool HeapTupleIsSurelyDead(HeapTuple htup,
8080
TransactionId OldestXmin);
81+
extern bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
8182

8283
extern void HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer,
8384
uint16 infomask, TransactionId xid);

0 commit comments

Comments
 (0)