diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index c9516e03fae..519cecef2f7 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -1446,7 +1446,7 @@ RecordTransactionCommit(void)
 	 * if all to-be-deleted tables are temporary though, since they are lost
 	 * anyway if we crash.)
 	 */
-	if ((wrote_xlog && markXidCommitted &&
+	if ((wrote_xlog &&
 		 synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
 		forceSyncCommit || nrels > 0)
 	{
@@ -1504,7 +1504,7 @@ RecordTransactionCommit(void)
 	 * Note that at this stage we have marked clog, but still show as running
 	 * in the procarray and continue to hold locks.
 	 */
-	if (wrote_xlog && markXidCommitted)
+	if (wrote_xlog)
 		SyncRepWaitForLSN(XactLastRecEnd, true);
 
 	/* remember end of last commit record */
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index f2ffd42a05f..7ef6b094b91 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -811,6 +811,17 @@ nextval_internal(Oid relid, bool check_permissions)
 
 		PageSetLSN(page, recptr);
 	}
+	else if (RelationNeedsWAL(seqrel))
+	{
+		XLogRecPtr	recptr = PageGetLSN(page);
+
+		/*
+		 * If the sequence WAL is more recent than WAL generated by our session,
+		 * update the position so that we wait until it's flushed.
+		 */
+		if (recptr > XactLastRecEnd)
+			XactLastRecEnd = recptr;
+	}
 
 	/* Now update sequence tuple to the intended final state */
 	seq->last_value = last;		/* last fetched number */
