diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index 5b712736e70617bf18fc31c4432eace752638c90..3c571d5fdfe72d4f7629779da9e4796ce25fcde7 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -1391,6 +1391,25 @@ dpow(PG_FUNCTION_ARGS)
 	float8		arg2 = PG_GETARG_FLOAT8(1);
 	float8		result;
 
+	/*
+	 * The POSIX spec says that NaN ^ 0 = 1, and 1 ^ NaN = 1, while all other
+	 * cases with NaN inputs yield NaN (with no error).  Many older platforms
+	 * get one or more of these cases wrong, so deal with them via explicit
+	 * logic rather than trusting pow(3).
+	 */
+	if (isnan(arg1))
+	{
+		if (isnan(arg2) || arg2 != 0.0)
+			PG_RETURN_FLOAT8(get_float8_nan());
+		PG_RETURN_FLOAT8(1.0);
+	}
+	if (isnan(arg2))
+	{
+		if (arg1 != 1.0)
+			PG_RETURN_FLOAT8(get_float8_nan());
+		PG_RETURN_FLOAT8(1.0);
+	}
+
 	/*
 	 * The SQL spec requires that we emit a particular SQLSTATE error code for
 	 * certain error conditions.  Specifically, we don't return a
@@ -1412,12 +1431,11 @@ dpow(PG_FUNCTION_ARGS)
 	 * and result == NaN for negative arg1 and very large arg2 (they must be
 	 * using something different from our floor() test to decide it's
 	 * invalid).  Other platforms (HPPA) return errno == ERANGE and a large
-	 * (HUGE_VAL) but finite result to signal overflow.  Also, some versions
-	 * of MSVC return errno == EDOM and result == NaN for NaN inputs.
+	 * (HUGE_VAL) but finite result to signal overflow.
 	 */
 	errno = 0;
 	result = pow(arg1, arg2);
-	if (errno == EDOM && isnan(result) && !isnan(arg1) && !isnan(arg2))
+	if (errno == EDOM && isnan(result))
 	{
 		if ((fabs(arg1) > 1 && arg2 >= 0) || (fabs(arg1) < 1 && arg2 < 0))
 			/* The sign of Inf is not significant in this case. */
diff --git a/src/test/regress/expected/float8-exp-three-digits-win32.out b/src/test/regress/expected/float8-exp-three-digits-win32.out
index 8e4cbb05042b2d3cbf199612805874579014b008..1d0e3a46f2d89bfd269c7927b2957a9822053c62 100644
--- a/src/test/regress/expected/float8-exp-three-digits-win32.out
+++ b/src/test/regress/expected/float8-exp-three-digits-win32.out
@@ -358,6 +358,12 @@ SELECT power(float8 'NaN', float8 'NaN');
    NaN
 (1 row)
 
+SELECT power(float8 '-1', float8 'NaN');
+ power 
+-------
+   NaN
+(1 row)
+
 SELECT power(float8 '1', float8 'NaN');
  power 
 -------
diff --git a/src/test/regress/expected/float8-small-is-zero.out b/src/test/regress/expected/float8-small-is-zero.out
index e3e004451c1cdbcef011a29a5ce5c63e358a203a..fa47c9d27593f3fe70989b53c7df583e556192d3 100644
--- a/src/test/regress/expected/float8-small-is-zero.out
+++ b/src/test/regress/expected/float8-small-is-zero.out
@@ -362,6 +362,12 @@ SELECT power(float8 'NaN', float8 'NaN');
    NaN
 (1 row)
 
+SELECT power(float8 '-1', float8 'NaN');
+ power 
+-------
+   NaN
+(1 row)
+
 SELECT power(float8 '1', float8 'NaN');
  power 
 -------
diff --git a/src/test/regress/expected/float8-small-is-zero_1.out b/src/test/regress/expected/float8-small-is-zero_1.out
index e9487cffa6430ba5d0ed99da61eda26bbf65ff29..15d48dfc399c74bb3e6402395dede08a69b05749 100644
--- a/src/test/regress/expected/float8-small-is-zero_1.out
+++ b/src/test/regress/expected/float8-small-is-zero_1.out
@@ -362,6 +362,12 @@ SELECT power(float8 'NaN', float8 'NaN');
    NaN
 (1 row)
 
+SELECT power(float8 '-1', float8 'NaN');
+ power 
+-------
+   NaN
+(1 row)
+
 SELECT power(float8 '1', float8 'NaN');
  power 
 -------
diff --git a/src/test/regress/expected/float8.out b/src/test/regress/expected/float8.out
index 1bf225ca2b67542c3bc218e31c8eafceb4a2eeeb..2d5f02474084a4f63d879bddc6a61f6c8c42175d 100644
--- a/src/test/regress/expected/float8.out
+++ b/src/test/regress/expected/float8.out
@@ -358,6 +358,12 @@ SELECT power(float8 'NaN', float8 'NaN');
    NaN
 (1 row)
 
+SELECT power(float8 '-1', float8 'NaN');
+ power 
+-------
+   NaN
+(1 row)
+
 SELECT power(float8 '1', float8 'NaN');
  power 
 -------
diff --git a/src/test/regress/sql/float8.sql b/src/test/regress/sql/float8.sql
index 2b2e175c6d7a489f4fecd46bbed6f26298722245..78bec137e2f8bbab6c80f21920a9512e8cb0bff7 100644
--- a/src/test/regress/sql/float8.sql
+++ b/src/test/regress/sql/float8.sql
@@ -111,6 +111,7 @@ SELECT power(float8 '144', float8 '0.5');
 SELECT power(float8 'NaN', float8 '0.5');
 SELECT power(float8 '144', float8 'NaN');
 SELECT power(float8 'NaN', float8 'NaN');
+SELECT power(float8 '-1', float8 'NaN');
 SELECT power(float8 '1', float8 'NaN');
 SELECT power(float8 'NaN', float8 '0');