diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 070df29..cbf3538 100644
*** a/src/backend/commands/analyze.c
--- b/src/backend/commands/analyze.c
***************
*** 2079,2095 ****
  						denom,
  						stadistinct;
  
! 			numer = (double) samplerows *(double) d;
  
! 			denom = (double) (samplerows - f1) +
! 				(double) f1 *(double) samplerows / totalrows;
  
  			stadistinct = numer / denom;
  			/* Clamp to sane range in case of roundoff error */
  			if (stadistinct < (double) d)
  				stadistinct = (double) d;
! 			if (stadistinct > totalrows)
! 				stadistinct = totalrows;
  			stats->stadistinct = floor(stadistinct + 0.5);
  		}
  
--- 2079,2099 ----
  						denom,
  						stadistinct;
  
! 			double		samplerows_nonnull = samplerows - null_cnt;
! 			double		totalrows_nonnull
! 							= totalrows * (1.0 - stats->stanullfrac);
  
! 			numer = samplerows_nonnull * (double) d;
! 
! 			denom = (samplerows_nonnull - f1) +
! 				(double) f1 * samplerows_nonnull / totalrows_nonnull;
  
  			stadistinct = numer / denom;
  			/* Clamp to sane range in case of roundoff error */
  			if (stadistinct < (double) d)
  				stadistinct = (double) d;
! 			if (stadistinct > totalrows_nonnull)
! 				stadistinct = totalrows_nonnull;
  			stats->stadistinct = floor(stadistinct + 0.5);
  		}
  
***************
*** 2120,2146 ****
  		}
  		else
  		{
  			double		ndistinct = stats->stadistinct;
- 			double		avgcount,
- 						mincount;
  
  			if (ndistinct < 0)
! 				ndistinct = -ndistinct * totalrows;
! 			/* estimate # of occurrences in sample of a typical value */
! 			avgcount = (double) samplerows / ndistinct;
! 			/* 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++)
  			{
  				if (track[i].count < mincount)
  				{
  					num_mcv = i;
  					break;
  				}
  			}
  		}
  
--- 2124,2160 ----
  		}
  		else
  		{
+ 			/*
+ 			 * Starting number of values: samplerows sans nulls and too wide
+ 			 * ones.
+ 			 */
+ 			int			sample_cnt = nonnull_cnt - toowide_cnt;
  			double		ndistinct = stats->stadistinct;
  
  			if (ndistinct < 0)
! 				ndistinct = -ndistinct * sample_cnt;
! 
  			if (num_mcv > track_cnt)
  				num_mcv = track_cnt;
  			for (i = 0; i < num_mcv; i++)
  			{
+ 				double		avgcount,
+ 							mincount;
+ 
+ 				/* estimate # of occurrences in sample of a typical value */
+ 				avgcount = (double) sample_cnt / ndistinct;
+ 
+ 				/* set minimum threshold count to store a value */
+ 				mincount = 1.25 * avgcount;
  				if (track[i].count < mincount)
  				{
  					num_mcv = i;
  					break;
  				}
+ 
+ 				/* Narrow our view of samples and distincts left */
+ 				sample_cnt -= track[i].count;
+ 				ndistinct--;
  			}
  		}
  
***************
*** 2428,2444 ****
  						denom,
  						stadistinct;
  
! 			numer = (double) samplerows *(double) d;
  
! 			denom = (double) (samplerows - f1) +
! 				(double) f1 *(double) samplerows / totalrows;
  
  			stadistinct = numer / denom;
  			/* Clamp to sane range in case of roundoff error */
  			if (stadistinct < (double) d)
  				stadistinct = (double) d;
! 			if (stadistinct > totalrows)
! 				stadistinct = totalrows;
  			stats->stadistinct = floor(stadistinct + 0.5);
  		}
  
--- 2442,2462 ----
  						denom,
  						stadistinct;
  
! 			double		samplerows_nonnull = samplerows - null_cnt;
! 			double		totalrows_nonnull
! 							= totalrows * (1.0 - stats->stanullfrac);
! 
! 			numer = samplerows_nonnull * (double) d;
  
! 			denom = (samplerows_nonnull - f1) +
! 				(double) f1 * samplerows_nonnull / totalrows_nonnull;
  
  			stadistinct = numer / denom;
  			/* Clamp to sane range in case of roundoff error */
  			if (stadistinct < (double) d)
  				stadistinct = (double) d;
! 			if (stadistinct > totalrows_nonnull)
! 				stadistinct = totalrows_nonnull;
  			stats->stadistinct = floor(stadistinct + 0.5);
  		}
  
***************
*** 2464,2469 ****
--- 2482,2489 ----
  		 * 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.)
+ 		 * We also decrease ndistinct in the process such that going forward
+ 		 * it refers to the number of distinct values left for the histogram.
  		 */
  		if (track_cnt == ndistinct && toowide_cnt == 0 &&
  			stats->stadistinct > 0 &&
***************
*** 2471,2505 ****
  		{
  			/* Track list includes all values seen, and all will fit */
  			num_mcv = track_cnt;
  		}
  		else
  		{
! 			double		ndistinct = stats->stadistinct;
! 			double		avgcount,
! 						mincount,
! 						maxmincount;
  
- 			if (ndistinct < 0)
- 				ndistinct = -ndistinct * totalrows;
- 			/* estimate # of occurrences in sample of a typical value */
- 			avgcount = (double) samplerows / ndistinct;
- 			/* 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) 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++)
  			{
! 				if (track[i].count < mincount)
  				{
! 					num_mcv = i;
! 					break;
  				}
  			}
  		}
  
--- 2491,2548 ----
  		{
  			/* Track list includes all values seen, and all will fit */
  			num_mcv = track_cnt;
+ 
+ 			/* Nothing left for the histogram */
+ 			num_hist = 0;
+ 			ndistinct = 0;
  		}
  		else
  		{
! 			/*
! 			 * Starting number of values left for the histogram: samplerows
! 			 * sans nulls and too wide ones.
! 			 */
! 			int			sample_cnt = values_cnt;
! 
! 			num_hist = ndistinct;
! 			if (num_hist > num_bins)
! 				num_hist = num_bins + 1;
  
  			if (num_mcv > track_cnt)
  				num_mcv = track_cnt;
  			for (i = 0; i < num_mcv; i++)
  			{
! 				if (num_hist >= 2)
  				{
! 					double		avgcount,
! 								mincount,
! 								maxmincount;
! 
! 					/* estimate # of occurrences in sample of a typical value */
! 					avgcount = (double) sample_cnt / (double) ndistinct;
! 
! 					/* set minimum threshold count to store a value */
! 					mincount = 1.25 * avgcount;
! 
! 					/* don't let threshold exceed 1/K, however */
! 					maxmincount = (sample_cnt - 1) / (double) (num_hist - 1);
! 					if (mincount > maxmincount)
! 						mincount = maxmincount;
! 					if (track[i].count < mincount)
! 					{
! 						num_mcv = i;
! 						break;
! 					}
  				}
+ 
+ 				/* Narrow our view of samples left for the histogram */
+ 				sample_cnt -= track[i].count;
+ 				ndistinct--;
+ 
+ 				/* Recalculate histogram size due to lower ndistinct */
+ 				num_hist = ndistinct;
+ 				if (num_hist > num_bins)
+ 					num_hist = num_bins + 1;
  			}
  		}
  
***************
*** 2542,2550 ****
  		 * values not accounted for in the MCV list.  (This ensures the
  		 * histogram won't collapse to empty or a singleton.)
  		 */
- 		num_hist = ndistinct - num_mcv;
- 		if (num_hist > num_bins)
- 			num_hist = num_bins + 1;
  		if (num_hist >= 2)
  		{
  			MemoryContext old_context;
--- 2585,2590 ----
