diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 8536332..7f4e154 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -1919,9 +1919,13 @@ compute_scalar_stats(VacAttrStatsP stats,
 	int			num_bins = stats->attr->attstattarget;
 	StdAnalyzeData *mystats = (StdAnalyzeData *) stats->extra_data;
 
+    /* set the maximum number of mcv's that we will store */
+    int         track_multiplier = 2;
+    int         max_num_mcv = num_mcv*track_multiplier + 1;
+
 	values = (ScalarItem *) palloc(samplerows * sizeof(ScalarItem));
 	tupnoLink = (int *) palloc(samplerows * sizeof(int));
-	track = (ScalarMCVItem *) palloc(num_mcv * sizeof(ScalarMCVItem));
+	track = (ScalarMCVItem *) palloc(max_num_mcv * sizeof(ScalarMCVItem));
 
 	SelectSortFunction(mystats->ltopr, false, &cmpFn, &cmpFlags);
 	fmgr_info(cmpFn, &f_cmpfn);
@@ -2034,7 +2038,7 @@ compute_scalar_stats(VacAttrStatsP stats,
 				if (dups_cnt > 1)
 				{
 					nmultiple++;
-					if (track_cnt < num_mcv ||
+					if (track_cnt < max_num_mcv ||
 						dups_cnt > track[track_cnt - 1].count)
 					{
 						/*
@@ -2045,7 +2049,7 @@ compute_scalar_stats(VacAttrStatsP stats,
 						 */
 						int			j;
 
-						if (track_cnt < num_mcv)
+						if (track_cnt < max_num_mcv)
 							track_cnt++;
 						for (j = track_cnt - 1; j > 0; j--)
 						{
@@ -2164,18 +2168,42 @@ compute_scalar_stats(VacAttrStatsP stats,
 				mincount = 2;
 			/* don't let threshold exceed 1/K, however */
 			maxmincount = (double) samplerows / (double) num_bins;
-			if (mincount > maxmincount)
-				mincount = maxmincount;
-			if (num_mcv > track_cnt)
-				num_mcv = track_cnt;
-			for (i = 0; i < num_mcv; i++)
+			for ( i = 0; i < max_num_mcv - 1 && i < track_cnt; i++ )
 			{
-				if (track[i].count < mincount)
-				{
-					num_mcv = i;
-					break;
-				}
+                /* 
+                 * if the count is high enough to overrun a histogram boundary, 
+                 * keep adding mcv's until we're below, or we have reached the 
+                 * hard limit of max_num_mcv - 1
+                 */
+                if ( track[i].count >= maxmincount )
+                {
+                    /* update maxmin to account for the previous mcv */
+                    maxmincount -= (double) track[i].count / (double) num_bins;
+                    continue;
+                }
+
+                if( i >= num_mcv )
+                    break;
+
+                if( track[i].count < mincount )
+                    break;
 			}
+
+            /* store the desired number of mcv's as determined by the loop */
+            num_mcv = i;
+
+            /* 
+             * finally, make sure that num bins makes sense. Despite the fact
+             * that we allow for track_multiplier the desired mcv values to be 
+             * tracked for really heavy tailed distributions it is possible that 
+             * we still havn't dropped below the maxmin threshhold. As such, if
+             * the last tracked value is above this threshhold, then make sure 
+             * that remaining hist values/nbins < last tracked value. 
+             */
+            if (num_mcv == max_num_mcv-1 && track[num_mcv].count > maxmincount)
+            {
+                num_bins = (int) num_bins*(maxmincount/track[num_mcv].count);
+            }
 		}
 
 		/* Generate MCV slot entry */
