diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c
index 928f9fe..7eb4a27 100644
--- a/src/backend/access/transam/multixact.c
+++ b/src/backend/access/transam/multixact.c
@@ -2578,6 +2578,116 @@ find_multixact_start(MultiXactId multi)
 }
 
 /*
+ * Returns an instantaneous snapshot of the current number of active
+ * multixacts and the number of members in the members SLRU area.
+ */
+static void
+ReadMultiXactCounts(uint32 *multixacts, MultiXactOffset *members)
+{
+	MultiXactOffset nextOffset;
+	MultiXactOffset oldestOffset;
+	MultiXactId oldestMultiXactId;
+	MultiXactId nextMultiXactId;
+
+	LWLockAcquire(MultiXactGenLock, LW_SHARED);
+	nextOffset = MultiXactState->nextOffset;
+	oldestMultiXactId = MultiXactState->oldestMultiXactId;
+	nextMultiXactId = MultiXactState->nextMXact;
+	LWLockRelease(MultiXactGenLock);
+	/*
+	 * XXX: Could we store oldestMultiXactMemberOffset in shmem and
+	 * pg_controdata, alongside oldestMultiXactId?
+	 */
+	oldestOffset = find_multixact_start(oldestMultiXactId);
+	*members = nextOffset - oldestOffset;
+	*multixacts = nextMultiXactId - oldestMultiXactId;
+}
+
+/*
+ * Computes a multixact age that we can use to trigger earlier wraparound
+ * vacuums than usual, if special action is required to avoid impending
+ * exhaustion of the address space of multixact members (caused by large
+ * multixacts).
+ *
+ * If less than a "safe member count" is active, then we return -1 to indicate
+ * that no special action needs to be taken.  This should always be the case
+ * for users who don't make use of large multixacts.
+ *
+ * If more than the "dangerous member count" is active, then we return a max
+ * freeze age of zero to trigger aggressive wraparound vacuuming.
+ *
+ * In between the safe and dangerous levels, we return the current number of
+ * active multixids scaled down linearly for higher usage fractions, so that
+ * vacuuming becomes more aggressive as the member SLRU grows, in the hope
+ * that different tables will be vacuumed at different times due to their
+ * varying relminmxid values.
+ *
+ * The safe_member_count threshold is based on the
+ * autovacuum_multixact_freeze_max_age GUC and a scaling factor (see below).
+ *
+ * The dangerous_member_count threshold is arbitrarily set at 75% of member
+ * addressing space.
+ */
+int
+MultiXactCheckMemberUsage(void)
+{
+	/*
+	 * The following constant controls the point at which the vacuum system
+	 * interprets autovacuum_multixact_freeze_max_age GUC differently.  If the
+	 * average size of active multixacts is below this number, then this
+	 * function should always return -1 so that no special action is taken to
+	 * prevent member space wraparound, because autovacuum should trigger
+	 * wraparound vacuums before the member space exceeds safe_member_count.
+	 * If the average size of active multixacts is above this number, then
+	 * member space usage should exceed safe_member_count before the usual
+	 * multixact ID wraparound prevention, so this function will begin to
+	 * return values that change the the behavior of autovacuum, so that
+	 * wraparound vacuums start sooner to reduce member space usage.
+	 */
+	const int avg_multixact_size_threshold = 3;
+
+	const MultiXactOffset dangerous_member_count = MaxMultiXactOffset -
+		(MaxMultiXactOffset / 4);
+
+	/* Avoid overflow if the GUC is set very high. */
+	const MultiXactOffset safe_member_count =
+		(autovacuum_multixact_freeze_max_age >= INT_MAX / avg_multixact_size_threshold
+		 ? dangerous_member_count
+		 : autovacuum_multixact_freeze_max_age * avg_multixact_size_threshold);
+
+	MultiXactOffset members;
+	uint32 multixacts;
+	double fraction;
+
+	ReadMultiXactCounts(&multixacts, &members);
+
+	if (members >= dangerous_member_count)
+	{
+		/* We need a wraparound vacuum for all tables now. */
+		return 0;
+	}
+
+	if (members <= safe_member_count)
+	{
+		/* There is no danger of member space wrap currently. */
+		return -1;
+	}
+
+	/*
+	 * Choose a cutoff age which is a fraction of the approximate current
+	 * number of active multixacts.  If we are using an amount of member
+	 * address space near safe_member_count, we use a number close to the
+	 * number of active multixacts, so that only tables with the oldest
+	 * relminmxid values become candidates for wraparound vacuums.  As we get
+	 * closer to dangerous_member_count, we use a number closer to zero, so
+	 * that more tables become candidates for wraparound vacuums.
+	 */
+	fraction = (double) (members - safe_member_count) /
+		(double) (dangerous_member_count - safe_member_count);
+	return (int) (multixacts * (1.0 - fraction));
+}
+
+/*
  * SlruScanDirectory callback.
  *		This callback deletes segments that are outside the range determined by
  *		the given page numbers.
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index 7ead161..1bd2aa1 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -105,10 +105,24 @@ ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel)
 	}
 	else
 	{
+		int safe_multixact_age = MultiXactCheckMemberUsage();
+		if (safe_multixact_age >= 0)
+		{
+			/*
+			 * Override the multixact freeze settings if we are running out of
+			 * member address space.
+			 */
+			params.multixact_freeze_table_age = safe_multixact_age;
+			params.multixact_freeze_min_age = safe_multixact_age / 2;
+		}
+		else
+		{
+			/* Use the default values. */
+			params.multixact_freeze_min_age = -1;
+			params.multixact_freeze_table_age = -1;
+		}
 		params.freeze_min_age = -1;
 		params.freeze_table_age = -1;
-		params.multixact_freeze_min_age = -1;
-		params.multixact_freeze_table_age = -1;
 	}
 
 	/* user-invoked vacuum is never "for wraparound" */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index be4cd1d..6441e94 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -297,10 +297,12 @@ static void do_autovacuum(void);
 static void FreeWorkerInfo(int code, Datum arg);
 
 static autovac_table *table_recheck_autovac(Oid relid, HTAB *table_toast_map,
-					  TupleDesc pg_class_desc);
+											TupleDesc pg_class_desc,
+											int safe_multixact_age);
 static void relation_needs_vacanalyze(Oid relid, AutoVacOpts *relopts,
 						  Form_pg_class classForm,
 						  PgStat_StatTabEntry *tabentry,
+						  int safe_multixact_age,
 						  bool *dovacuum, bool *doanalyze, bool *wraparound);
 
 static void autovacuum_do_vac_analyze(autovac_table *tab,
@@ -1077,6 +1079,7 @@ do_start_worker(void)
 	Oid			retval = InvalidOid;
 	MemoryContext tmpcxt,
 				oldcxt;
+	int safe_multixact_age;
 
 	/* return quickly when there are no free workers */
 	LWLockAcquire(AutovacuumLock, LW_SHARED);
@@ -1118,7 +1121,12 @@ do_start_worker(void)
 
 	/* Also determine the oldest datminmxid we will consider. */
 	recentMulti = ReadNextMultiXactId();
-	multiForceLimit = recentMulti - autovacuum_multixact_freeze_max_age;
+	safe_multixact_age = MultiXactCheckMemberUsage();
+	if (safe_multixact_age >= 0)
+		multiForceLimit = recentMulti - Min(autovacuum_freeze_max_age,
+											safe_multixact_age);
+	else
+		multiForceLimit = recentMulti - autovacuum_multixact_freeze_max_age;
 	if (multiForceLimit < FirstMultiXactId)
 		multiForceLimit -= FirstMultiXactId;
 
@@ -1881,6 +1889,7 @@ do_autovacuum(void)
 	BufferAccessStrategy bstrategy;
 	ScanKeyData key;
 	TupleDesc	pg_class_desc;
+	int safe_multixact_age;
 
 	/*
 	 * StartTransactionCommand and CommitTransactionCommand will automatically
@@ -1975,6 +1984,13 @@ do_autovacuum(void)
 	relScan = heap_beginscan_catalog(classRel, 0, NULL);
 
 	/*
+	 * Check if member space usage is in danger of being exhausted, so we can
+	 * pass the recommended cutoff age to relation_needs_vacanalyze and
+	 * table_recheck_autovac.
+	 */
+	safe_multixact_age = MultiXactCheckMemberUsage();
+
+	/*
 	 * On the first pass, we collect main tables to vacuum, and also the main
 	 * table relid to TOAST relid mapping.
 	 */
@@ -2001,6 +2017,7 @@ do_autovacuum(void)
 
 		/* Check if it needs vacuum or analyze */
 		relation_needs_vacanalyze(relid, relopts, classForm, tabentry,
+								  safe_multixact_age,
 								  &dovacuum, &doanalyze, &wraparound);
 
 		/*
@@ -2129,6 +2146,7 @@ do_autovacuum(void)
 											 shared, dbentry);
 
 		relation_needs_vacanalyze(relid, relopts, classForm, tabentry,
+								  safe_multixact_age,
 								  &dovacuum, &doanalyze, &wraparound);
 
 		/* ignore analyze for toast tables */
@@ -2235,7 +2253,8 @@ do_autovacuum(void)
 		 * the race condition is not closed but it is very small.
 		 */
 		MemoryContextSwitchTo(AutovacMemCxt);
-		tab = table_recheck_autovac(relid, table_toast_map, pg_class_desc);
+		tab = table_recheck_autovac(relid, table_toast_map, pg_class_desc,
+									safe_multixact_age);
 		if (tab == NULL)
 		{
 			/* someone else vacuumed the table, or it went away */
@@ -2442,7 +2461,8 @@ get_pgstat_tabentry_relid(Oid relid, bool isshared, PgStat_StatDBEntry *shared,
  */
 static autovac_table *
 table_recheck_autovac(Oid relid, HTAB *table_toast_map,
-					  TupleDesc pg_class_desc)
+					  TupleDesc pg_class_desc,
+					  int safe_multixact_age)
 {
 	Form_pg_class classForm;
 	HeapTuple	classTup;
@@ -2488,6 +2508,7 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
 										 shared, dbentry);
 
 	relation_needs_vacanalyze(relid, avopts, classForm, tabentry,
+							  safe_multixact_age,
 							  &dovacuum, &doanalyze, &wraparound);
 
 	/* ignore ANALYZE for toast tables */
@@ -2550,6 +2571,18 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
 			? avopts->multixact_freeze_table_age
 			: default_multixact_freeze_table_age;
 
+		/*
+		 * Override the multixact freeze settings if we are running out of
+		 * member address space.
+		 */
+		if (safe_multixact_age >= 0)
+		{
+			multixact_freeze_table_age = Min(safe_multixact_age,
+											 multixact_freeze_table_age);
+			multixact_freeze_min_age = Min(safe_multixact_age / 2,
+										   multixact_freeze_min_age);
+		}
+
 		tab = palloc(sizeof(autovac_table));
 		tab->at_relid = relid;
 		tab->at_vacoptions = VACOPT_SKIPTOAST |
@@ -2624,6 +2657,7 @@ relation_needs_vacanalyze(Oid relid,
 						  AutoVacOpts *relopts,
 						  Form_pg_class classForm,
 						  PgStat_StatTabEntry *tabentry,
+						  int safe_multixact_age,
  /* output params below */
 						  bool *dovacuum,
 						  bool *doanalyze,
@@ -2687,6 +2721,10 @@ relation_needs_vacanalyze(Oid relid,
 		? Min(relopts->multixact_freeze_max_age, autovacuum_multixact_freeze_max_age)
 		: autovacuum_multixact_freeze_max_age;
 
+	/* Special settings if we are running out of member address space. */
+	if (safe_multixact_age >= 0)
+		multixact_freeze_max_age = Min(multixact_freeze_max_age, safe_multixact_age);
+
 	av_enabled = (relopts ? relopts->enabled : true);
 
 	/* Force vacuum if table is at risk of wraparound */
diff --git a/src/include/access/multixact.h b/src/include/access/multixact.h
index 640b198..d86142d 100644
--- a/src/include/access/multixact.h
+++ b/src/include/access/multixact.h
@@ -126,6 +126,7 @@ extern void MultiXactAdvanceNextMXact(MultiXactId minMulti,
 						  MultiXactOffset minMultiOffset);
 extern void MultiXactAdvanceOldest(MultiXactId oldestMulti, Oid oldestMultiDB);
 extern void MultiXactSetSafeTruncate(MultiXactId safeTruncateMulti);
+extern int MultiXactCheckMemberUsage(void);
 
 extern void multixact_twophase_recover(TransactionId xid, uint16 info,
 						   void *recdata, uint32 len);
