diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 8aef998..e1f6408 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -99,6 +99,7 @@ SyncRepWaitForLSN(XLogRecPtr XactCommitLSN)
 {
 	char 		*new_status = NULL;
 	const char *old_status;
+	bool		sync_standbys_defined;
 
 	/*
 	 * Fast exit if user has not requested sync replication, or
@@ -110,6 +111,7 @@ SyncRepWaitForLSN(XLogRecPtr XactCommitLSN)
 
 	Assert(SHMQueueIsDetached(&(MyProc->syncRepLinks)));
 	Assert(WalSndCtl != NULL);
+	Assert(MyProc->syncRepState == SYNC_REP_NOT_WAITING);
 
 	/* Reset the latch before adding ourselves to the queue. */
 	ResetLatch(&MyProc->waitLatch);
@@ -119,22 +121,25 @@ SyncRepWaitForLSN(XLogRecPtr XactCommitLSN)
 	 * ourselves to the queue.
 	 */
 	LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
-	Assert(MyProc->syncRepState == SYNC_REP_NOT_WAITING);
-	if (!WalSndCtl->sync_standbys_defined)
-	{
-		/*
-		 * We don't wait for sync rep if WalSndCtl->sync_standbys_defined is
-		 * not set.  See SyncRepUpdateSyncStandbysDefined.
-		 */
-		LWLockRelease(SyncRepLock);
-		return;
-	}
+	sync_standbys_defined = WalSndCtl->sync_standbys_defined;
 	MyProc->waitLSN = XactCommitLSN;
 	MyProc->syncRepState = SYNC_REP_WAITING;
 	SyncRepQueueInsert();
 	Assert(SyncRepQueueIsOrderedByLSN());
 	LWLockRelease(SyncRepLock);
 
+	/*
+	 * We don't wait for sync rep if WalSndCtl->sync_standbys_defined is
+	 * not set.  See SyncRepUpdateSyncStandbysDefined.  However, since this
+	 * is an unlikely scenario, we release the lock before doing the test,
+	 * so as not to slow down the common case.
+	 */
+	if (!sync_standbys_defined)
+	{
+		SyncRepCancelWait();
+		return;
+	}
+
 	/* Alter ps display to show waiting for sync rep. */
 	if (update_process_title)
 	{
