return snapshot;
        }
 
-       /* Maintain serializable global xmin info. */
-       if (!TransactionIdIsValid(PredXact->SxactGlobalXmin))
-       {
-               Assert(PredXact->SxactGlobalXminCount == 0);
-               PredXact->SxactGlobalXmin = snapshot->xmin;
-               PredXact->SxactGlobalXminCount = 1;
-               SerialSetActiveSerXmin(snapshot->xmin);
-       }
-       else if (TransactionIdEquals(snapshot->xmin, PredXact->SxactGlobalXmin))
-       {
-               Assert(PredXact->SxactGlobalXminCount > 0);
-               PredXact->SxactGlobalXminCount++;
-       }
-       else
-       {
-               Assert(TransactionIdFollows(snapshot->xmin, PredXact->SxactGlobalXmin));
-       }
-
        /* Initialize the structure. */
        sxact->vxid = vxid;
        sxact->SeqNo.lastCommitBeforeSnapshot = PredXact->LastSxactCommitSeqNo;
                                SetPossibleUnsafeConflict(sxact, othersxact);
                        }
                }
+
+               /*
+                * If we didn't find any possibly unsafe conflicts because every
+                * uncommitted writable transaction turned out to be doomed, then we
+                * can "opt out" immediately.  See comments above the earlier check for
+                * PredXact->WritableSxactCount == 0.
+                */
+               if (dlist_is_empty(&sxact->possibleUnsafeConflicts))
+               {
+                       ReleasePredXact(sxact);
+                       LWLockRelease(SerializableXactHashLock);
+                       return snapshot;
+               }
        }
        else
        {
                           (MaxBackends + max_prepared_xacts));
        }
 
+       /* Maintain serializable global xmin info. */
+       if (!TransactionIdIsValid(PredXact->SxactGlobalXmin))
+       {
+               Assert(PredXact->SxactGlobalXminCount == 0);
+               PredXact->SxactGlobalXmin = snapshot->xmin;
+               PredXact->SxactGlobalXminCount = 1;
+               SerialSetActiveSerXmin(snapshot->xmin);
+       }
+       else if (TransactionIdEquals(snapshot->xmin, PredXact->SxactGlobalXmin))
+       {
+               Assert(PredXact->SxactGlobalXminCount > 0);
+               PredXact->SxactGlobalXminCount++;
+       }
+       else
+       {
+               Assert(TransactionIdFollows(snapshot->xmin, PredXact->SxactGlobalXmin));
+       }
+
        MySerializableXact = sxact;
        MyXactDidWrite = false;         /* haven't written anything yet */