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

Commit abce8dc

Browse files
committed
Reconsider when to wait for WAL flushes/syncrep during commit.
Up to now RecordTransactionCommit() waited for WAL to be flushed (if synchronous_commit != off) and to be synchronously replicated (if enabled), even if a transaction did not have a xid assigned. The primary reason for that is that sequence's nextval() did not assign a xid, but are worthwhile to wait for on commit. This can be problematic because sometimes read only transactions do write WAL, e.g. HOT page prune records. That then could lead to read only transactions having to wait during commit. Not something people expect in a read only transaction. This lead to such strange symptoms as backends being seemingly stuck during connection establishment when all synchronous replicas are down. Especially annoying when said stuck connection is the standby trying to reconnect to allow syncrep again... This behavior also is involved in a rather complicated <= 9.4 bug where the transaction started by catchup interrupt processing waited for syncrep using latches, but didn't get the wakeup because it was already running inside the same overloaded signal handler. Fix the issue here doesn't properly solve that issue, merely papers over the problems. In 9.5 catchup interrupts aren't processed out of signal handlers anymore. To fix all this, make nextval() acquire a top level xid, and only wait for transaction commit if a transaction both acquired a xid and emitted WAL records. If only a xid has been assigned we don't uselessly want to wait just because of writes to temporary/unlogged tables; if only WAL has been written we don't want to wait just because of HOT prunes. The xid assignment in nextval() is unlikely to cause overhead in real-world workloads. For one it only happens SEQ_LOG_VALS/32 values anyway, for another only usage of nextval() without using the result in an insert or similar is affected. Discussion: 20150223165359.GF30784@awork2.anarazel.de, 369698E947874884A77849D8FE3680C2@maumau, 5CF4ABBA67674088B3941894E22A0D25@maumau Per complaint from maumau and Thom Brown Backpatch all the way back; 9.0 doesn't have syncrep, but it seems better to be consistent behavior across all maintained branches.
1 parent 4651e37 commit abce8dc

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/backend/access/transam/xact.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -996,10 +996,9 @@ RecordTransactionCommit(void)
996996

997997
/*
998998
* If we didn't create XLOG entries, we're done here; otherwise we
999-
* should flush those entries the same as a commit record. (An
1000-
* example of a possible record that wouldn't cause an XID to be
1001-
* assigned is a sequence advance record due to nextval() --- we want
1002-
* to flush that to disk before reporting commit.)
999+
* should trigger flushing those entries the same as a commit record
1000+
* would. This will primarily happen for HOT pruning and the like; we
1001+
* want these to be flushed to disk in due time.
10031002
*/
10041003
if (!wrote_xlog)
10051004
goto cleanup;
@@ -1122,11 +1121,13 @@ RecordTransactionCommit(void)
11221121
/*
11231122
* Check if we want to commit asynchronously. We can allow the XLOG flush
11241123
* to happen asynchronously if synchronous_commit=off, or if the current
1125-
* transaction has not performed any WAL-logged operation. The latter
1126-
* case can arise if the current transaction wrote only to temporary
1127-
* and/or unlogged tables. In case of a crash, the loss of such a
1128-
* transaction will be irrelevant since temp tables will be lost anyway,
1129-
* and unlogged tables will be truncated. (Given the foregoing, you might
1124+
* transaction has not performed any WAL-logged operation or didn't assign
1125+
* a xid. The transaction can end up not writing any WAL, even if it has
1126+
* a xid, if it only wrote to temporary and/or unlogged tables. It can
1127+
* end up having written WAL without an xid if it did HOT pruning. In
1128+
* case of a crash, the loss of such a transaction will be irrelevant;
1129+
* temp tables will be lost anyway, unlogged tables will be truncated and
1130+
* HOT pruning will be done again later. (Given the foregoing, you might
11301131
* think that it would be unnecessary to emit the XLOG record at all in
11311132
* this case, but we don't currently try to do that. It would certainly
11321133
* cause problems at least in Hot Standby mode, where the
@@ -1142,7 +1143,8 @@ RecordTransactionCommit(void)
11421143
* if all to-be-deleted tables are temporary though, since they are lost
11431144
* anyway if we crash.)
11441145
*/
1145-
if ((wrote_xlog && synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
1146+
if ((wrote_xlog && markXidCommitted &&
1147+
synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
11461148
forceSyncCommit || nrels > 0)
11471149
{
11481150
XLogFlush(XactLastRecEnd);
@@ -1191,12 +1193,15 @@ RecordTransactionCommit(void)
11911193
latestXid = TransactionIdLatest(xid, nchildren, children);
11921194

11931195
/*
1194-
* Wait for synchronous replication, if required.
1196+
* Wait for synchronous replication, if required. Similar to the decision
1197+
* above about using committing asynchronously we only want to wait if
1198+
* this backend assigned a xid and wrote WAL. No need to wait if a xid
1199+
* was assigned due to temporary/unlogged tables or due to HOT pruning.
11951200
*
11961201
* Note that at this stage we have marked clog, but still show as running
11971202
* in the procarray and continue to hold locks.
11981203
*/
1199-
if (wrote_xlog)
1204+
if (wrote_xlog && markXidCommitted)
12001205
SyncRepWaitForLSN(XactLastRecEnd);
12011206

12021207
/* Reset XactLastRecEnd until the next transaction writes something */

src/backend/commands/sequence.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "access/htup_details.h"
1818
#include "access/multixact.h"
1919
#include "access/transam.h"
20+
#include "access/xact.h"
2021
#include "access/xlogutils.h"
2122
#include "catalog/dependency.h"
2223
#include "catalog/namespace.h"
@@ -347,6 +348,10 @@ fill_seq_with_data(Relation rel, HeapTuple tuple)
347348
*/
348349
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
349350

351+
/* check the comment above nextval_internal()'s equivalent call. */
352+
if (RelationNeedsWAL(rel))
353+
GetTopTransactionId();
354+
350355
START_CRIT_SECTION();
351356

352357
{
@@ -447,6 +452,10 @@ AlterSequence(AlterSeqStmt *stmt)
447452
/* Note that we do not change the currval() state */
448453
elm->cached = elm->last;
449454

455+
/* check the comment above nextval_internal()'s equivalent call. */
456+
if (RelationNeedsWAL(seqrel))
457+
GetTopTransactionId();
458+
450459
/* Now okay to update the on-disk tuple */
451460
START_CRIT_SECTION();
452461

@@ -692,6 +701,16 @@ nextval_internal(Oid relid)
692701

693702
last_used_seq = elm;
694703

704+
/*
705+
* If something needs to be WAL logged, acquire an xid, so this
706+
* transaction's commit will trigger a WAL flush and wait for
707+
* syncrep. It's sufficient to ensure the toplevel transaction has a xid,
708+
* no need to assign xids subxacts, that'll already trigger a appropriate
709+
* wait. (Have to do that here, so we're outside the critical section)
710+
*/
711+
if (logit && RelationNeedsWAL(seqrel))
712+
GetTopTransactionId();
713+
695714
/* ready to change the on-disk (or really, in-buffer) tuple */
696715
START_CRIT_SECTION();
697716

@@ -885,6 +904,10 @@ do_setval(Oid relid, int64 next, bool iscalled)
885904
/* In any case, forget any future cached numbers */
886905
elm->cached = elm->last;
887906

907+
/* check the comment above nextval_internal()'s equivalent call. */
908+
if (RelationNeedsWAL(seqrel))
909+
GetTopTransactionId();
910+
888911
/* ready to change the on-disk (or really, in-buffer) tuple */
889912
START_CRIT_SECTION();
890913

0 commit comments

Comments
 (0)