diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 4fffb76e557..c312de7d593 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -1733,7 +1733,10 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
 		i = Anum_pg_statistic_stavalues1 - 1;
 		for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
 		{
-			if (stats->numvalues[k] > 0)
+			/* XXX ugly hack to allow zero-length MCELEM values arrays */
+			if (stats->numvalues[k] > 0 ||
+				(stats->numvalues[k] == 0 &&
+				 stats->stakind[k] == STATISTIC_KIND_MCELEM))
 			{
 				ArrayType  *arry;
 
diff --git a/src/backend/utils/adt/array_typanalyze.c b/src/backend/utils/adt/array_typanalyze.c
index 6f61629b977..9c6dd2852ab 100644
--- a/src/backend/utils/adt/array_typanalyze.c
+++ b/src/backend/utils/adt/array_typanalyze.c
@@ -497,6 +497,15 @@ compute_array_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
 		 * If we obtained more elements than we really want, get rid of those
 		 * with least frequencies.  The easiest way is to qsort the array into
 		 * descending frequency order and truncate the array.
+		 *
+		 * On the other extreme, we might have found no elements with
+		 * frequency above the cutoff.  Then there's nothing to store so far
+		 * as element values go, but we still want to record the cutoff
+		 * frequency, since array_selfuncs.c can use that as an upper bound on
+		 * the frequency of non-MCVs.  (Note: what we store is the minimum
+		 * frequency that would have been accepted as a valid MCE.  In
+		 * particular, this ensures we don't create a bogus stats entry with
+		 * frequency zero.)
 		 */
 		if (num_mcelem < track_len)
 		{
@@ -506,10 +515,14 @@ compute_array_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
 			minfreq = sort_table[num_mcelem - 1]->frequency;
 		}
 		else
+		{
 			num_mcelem = track_len;
+			if (track_len == 0)
+				minfreq = maxfreq = cutoff_freq + 1;
+		}
 
 		/* Generate MCELEM slot entry */
-		if (num_mcelem > 0)
+		if (num_mcelem >= 0)
 		{
 			MemoryContext old_context;
 			Datum	   *mcelem_values;
