diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 02523b8a5fc41e10477a1aa19d35d71c1bbfcaf2..e40e0470fb710445e13b0c258b2075fe8a8a529d 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -14,7 +14,7 @@
  * Copyright (c) 1998-2007, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.105 2007/06/15 20:56:50 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.106 2007/07/09 16:13:57 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2435,7 +2435,10 @@ numeric_stddev_internal(ArrayType *transarray,
 	}
 	else
 	{
-		mul_var(&vN, &vNminus1, &vNminus1, 0);	/* N * (N - 1) */
+		if (sample)
+			mul_var(&vN, &vNminus1, &vNminus1, 0);	/* N * (N - 1) */
+		else
+			mul_var(&vN, &vN, &vNminus1, 0);		/* N * N */
 		rscale = select_div_scale(&vsumX2, &vNminus1);
 		div_var(&vsumX2, &vNminus1, &vsumX, rscale, true);		/* variance */
 		if (!variance)
diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out
index 34b720f0f433974b234fb876a2421f2ea06cc8e8..74635479e486e8c8c473c2e7513311eee883d617 100644
--- a/src/test/regress/expected/aggregates.out
+++ b/src/test/regress/expected/aggregates.out
@@ -102,7 +102,7 @@ SELECT var_samp(b) FROM aggtest;
 SELECT stddev_pop(b::numeric) FROM aggtest;
     stddev_pop    
 ------------------
- 151.389361431288
+ 131.107032862199
 (1 row)
 
 SELECT stddev_samp(b::numeric) FROM aggtest;
@@ -114,7 +114,7 @@ SELECT stddev_samp(b::numeric) FROM aggtest;
 SELECT var_pop(b::numeric) FROM aggtest;
       var_pop       
 --------------------
- 22918.738754573025
+ 17189.054065929769
 (1 row)
 
 SELECT var_samp(b::numeric) FROM aggtest;