diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
new file mode 100644
index 5f21fcb..3d6ce47
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -1766,6 +1766,7 @@ static void compute_scalar_stats(VacAttr
 					 double totalrows);
 static int	compare_scalars(const void *a, const void *b, void *arg);
 static int	compare_mcvs(const void *a, const void *b);
+static double get_mincount_for_mcv_list(int samplerows, double totalrows);
 
 
 /*
@@ -2183,10 +2184,9 @@ compute_distinct_stats(VacAttrStatsP sta
 		 * Decide how many values are worth storing as most-common values. If
 		 * we are able to generate a complete MCV list (all the values in the
 		 * sample will fit, and we think these are all the ones in the table),
-		 * then do so.  Otherwise, store only those values that are
-		 * significantly more common than the (estimated) average. We set the
-		 * threshold rather arbitrarily at 25% more than average, with at
-		 * least 2 instances in the sample.
+		 * then do so.  Otherwise, store only those values that appear
+		 * sufficiently often in the sample that it is reasonable to
+		 * extrapolate their sample frequencies to the entire table.
 		 *
 		 * Note: the first of these cases is meant to address columns with
 		 * small, fixed sets of possible values, such as boolean or enum
@@ -2196,7 +2196,7 @@ compute_distinct_stats(VacAttrStatsP sta
 		 * the MCV list is not complete, it's generally worth being more
 		 * selective, and not just filling it all the way up to the stats
 		 * target.  So for an incomplete list, we try to take only MCVs that
-		 * are significantly more common than average.
+		 * are common enough to produce reasonably accurate estimates.
 		 */
 		if (track_cnt < track_max && toowide_cnt == 0 &&
 			stats->stadistinct > 0 &&
@@ -2207,19 +2207,11 @@ compute_distinct_stats(VacAttrStatsP sta
 		}
 		else
 		{
-			double		ndistinct_table = stats->stadistinct;
-			double		avgcount,
-						mincount;
+			double		mincount;
+
+			/* Keep only the values common enough to give decent estimates */
+			mincount = get_mincount_for_mcv_list(samplerows, totalrows);
 
-			/* Re-extract estimate of # distinct nonnull values in table */
-			if (ndistinct_table < 0)
-				ndistinct_table = -ndistinct_table * totalrows;
-			/* estimate # occurrences in sample of a typical nonnull value */
-			avgcount = (double) nonnull_cnt / ndistinct_table;
-			/* set minimum threshold count to store a value */
-			mincount = avgcount * 1.25;
-			if (mincount < 2)
-				mincount = 2;
 			if (num_mcv > track_cnt)
 				num_mcv = track_cnt;
 			for (i = 0; i < num_mcv; i++)
@@ -2557,15 +2549,9 @@ compute_scalar_stats(VacAttrStatsP stats
 		 * Decide how many values are worth storing as most-common values. If
 		 * we are able to generate a complete MCV list (all the values in the
 		 * sample will fit, and we think these are all the ones in the table),
-		 * then do so.  Otherwise, store only those values that are
-		 * significantly more common than the (estimated) average. We set the
-		 * threshold rather arbitrarily at 25% more than average, with at
-		 * least 2 instances in the sample.  Also, we won't suppress values
-		 * that have a frequency of at least 1/K where K is the intended
-		 * number of histogram bins; such values might otherwise cause us to
-		 * emit duplicate histogram bin boundaries.  (We might end up with
-		 * duplicate histogram entries anyway, if the distribution is skewed;
-		 * but we prefer to treat such values as MCVs if at all possible.)
+		 * then do so.  Otherwise, store only those values that appear
+		 * sufficiently often in the sample that it is reasonable to
+		 * extrapolate their sample frequencies to the entire table.
 		 *
 		 * Note: the first of these cases is meant to address columns with
 		 * small, fixed sets of possible values, such as boolean or enum
@@ -2575,7 +2561,7 @@ compute_scalar_stats(VacAttrStatsP stats
 		 * the MCV list is not complete, it's generally worth being more
 		 * selective, and not just filling it all the way up to the stats
 		 * target.  So for an incomplete list, we try to take only MCVs that
-		 * are significantly more common than average.
+		 * are common enough to produce reasonably accurate estimates.
 		 */
 		if (track_cnt == ndistinct && toowide_cnt == 0 &&
 			stats->stadistinct > 0 &&
@@ -2586,24 +2572,11 @@ compute_scalar_stats(VacAttrStatsP stats
 		}
 		else
 		{
-			double		ndistinct_table = stats->stadistinct;
-			double		avgcount,
-						mincount,
-						maxmincount;
+			double		mincount;
+
+			/* Keep only the values common enough to give decent estimates */
+			mincount = get_mincount_for_mcv_list(samplerows, totalrows);
 
-			/* Re-extract estimate of # distinct nonnull values in table */
-			if (ndistinct_table < 0)
-				ndistinct_table = -ndistinct_table * totalrows;
-			/* estimate # occurrences in sample of a typical nonnull value */
-			avgcount = (double) nonnull_cnt / ndistinct_table;
-			/* set minimum threshold count to store a value */
-			mincount = avgcount * 1.25;
-			if (mincount < 2)
-				mincount = 2;
-			/* don't let threshold exceed 1/K, however */
-			maxmincount = (double) values_cnt / (double) num_bins;
-			if (mincount > maxmincount)
-				mincount = maxmincount;
 			if (num_mcv > track_cnt)
 				num_mcv = track_cnt;
 			for (i = 0; i < num_mcv; i++)
@@ -2878,3 +2851,67 @@ compare_mcvs(const void *a, const void *
 
 	return da - db;
 }
+
+/*
+ * Determine the minimum number of times a value needs to appear in the sample
+ * for it to be included in the MCV list.
+ */
+static double
+get_mincount_for_mcv_list(int samplerows, double totalrows)
+{
+	/*----------
+	 * We want to keep only values that appear sufficiently often in the
+	 * sample that it is reasonable to extrapolate their sample frequencies to
+	 * the entire table.  We do this by placing an upper bound on the relative
+	 * standard error of the sample frequency, so that any estimates the
+	 * planner generates from the MCV statistics can be expected to be
+	 * reasonably accurate.
+	 *
+	 * Since we are sampling without replacement, the sample frequency of a
+	 * particular value is described by a hypergeometric distribution.  A
+	 * common rule of thumb when estimating errors in this situation is to
+	 * require at least 10 instances of the value in the sample, in which case
+	 * the distribution can be approximated by a normal distribution, and
+	 * standard error analysis techniques can be applied.  Given a sample size
+	 * of n, a population size of N, and a sample frequency of p=cnt/n, the
+	 * standard error of the proportion p is given by
+	 *		SE = sqrt(p*(1-p)/n) * sqrt((N-n)/(N-1))
+	 * where the second term is the finite population correction.  To get
+	 * reasonably accurate planner estimates, we impose an upper bound on the
+	 * relative standard error of 20% -- i.e., SE/p < 0.2.  This 20% relative
+	 * error bound is fairly arbitrary, but has been found empirically to work
+	 * well.  Rearranging this formula gives a lower bound on the number of
+	 * instances of the value seen:
+	 *		cnt > n*(N-n) / (N-n+0.04*n*(N-1))
+	 * This bound is at most 25, and approaches 0 as n approaches 0 or N. The
+	 * case where n approaches 0 cannot happen in practice, since the sample
+	 * size is at least 300.  The case where n approaches N corresponds to
+	 * sampling the whole the table, in which case it is reasonable to keep
+	 * the whole MCV list (have no lower bound), so it makes sense to apply
+	 * this formula for all inputs, even though the above derivation is
+	 * technically only valid when the right hand side is at least around 10.
+	 *
+	 * An alternative way to look at this formula is as follows -- assume that
+	 * the number of instances of the value seen scales up to the entire
+	 * table, so that the population count is K=N*cnt/n. Then the distribution
+	 * in the sample is a hypergeometric distribution parameterised by N, n
+	 * and K, and the bound above is mathematically equivalent to demanding
+	 * that the standard deviation of that distribution is less than 20% of
+	 * its mean.  Thus the relative errors in any planner estimates produced
+	 * from the MCV statistics are likely to be not too large.
+	 *----------
+	 */
+	double		n = samplerows;
+	double		N = totalrows;
+	double		numer,
+				denom;
+
+	numer = n * (N - n);
+	denom = N - n + 0.04 * n * (N - 1);
+
+	/* Guard against division by zero (possible if n = N = 1) */
+	if (denom == 0)
+		return 0;
+
+	return numer / denom;
+}
