From b7a3ded18d1303e64951439437bcd5925d5c6f44 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@bowt.ie>
Date: Mon, 20 Dec 2021 16:58:40 -0800
Subject: [PATCH v1] Always trigger failsafe in single user mode.

---
 src/backend/access/heap/vacuumlazy.c | 12 ++++++++----
 src/backend/commands/vacuum.c        |  6 +++++-
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index db6becfed..2ba882951 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -2605,6 +2605,14 @@ lazy_check_wraparound_failsafe(LVRelState *vacrel)
 		vacrel->do_index_cleanup = false;
 		vacrel->do_rel_truncate = false;
 
+		/* Stop applying cost limits from this point on */
+		VacuumCostActive = false;
+		VacuumCostBalance = 0;
+
+		/* Single user mode uses failsafe as standard, so no WARNING needed */
+		if (!IsUnderPostmaster)
+			return true;
+
 		ereport(WARNING,
 				(errmsg("bypassing nonessential maintenance of table \"%s.%s.%s\" as a failsafe after %d index scans",
 						get_database_name(MyDatabaseId),
@@ -2615,10 +2623,6 @@ lazy_check_wraparound_failsafe(LVRelState *vacrel)
 				 errhint("Consider increasing configuration parameter \"maintenance_work_mem\" or \"autovacuum_work_mem\".\n"
 						 "You might also need to consider other ways for VACUUM to keep up with the allocation of transaction IDs.")));
 
-		/* Stop applying cost limits from this point on */
-		VacuumCostActive = false;
-		VacuumCostBalance = 0;
-
 		return true;
 	}
 
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index 5c4bc15b4..217bb6965 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -1153,7 +1153,8 @@ vacuum_set_xid_limits(Relation rel,
 /*
  * vacuum_xid_failsafe_check() -- Used by VACUUM's wraparound failsafe
  * mechanism to determine if its table's relfrozenxid and relminmxid are now
- * dangerously far in the past.
+ * dangerously far in the past.  This is assumed to always be the case when
+ * the backend is run in single user mode.
  *
  * Input parameters are the target relation's relfrozenxid and relminmxid.
  *
@@ -1203,6 +1204,9 @@ vacuum_xid_failsafe_check(TransactionId relfrozenxid, MultiXactId relminmxid)
 		return true;
 	}
 
+	if (!IsUnderPostmaster)
+		return true;
+
 	return false;
 }
 
-- 
2.30.2

