NumericVar  result;
 
    /*
-    * Handle NaN
+    * Handle NaN cases.  We follow the POSIX spec for pow(3), which says that
+    * NaN ^ 0 = 1, and 1 ^ NaN = 1, while all other cases with NaN inputs
+    * yield NaN (with no error).
     */
-   if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
+   if (NUMERIC_IS_NAN(num1))
+   {
+       if (!NUMERIC_IS_NAN(num2))
+       {
+           init_var_from_num(num2, &arg2);
+           if (cmp_var(&arg2, &const_zero) == 0)
+               PG_RETURN_NUMERIC(make_result(&const_one));
+       }
        PG_RETURN_NUMERIC(make_result(&const_nan));
+   }
+   if (NUMERIC_IS_NAN(num2))
+   {
+       init_var_from_num(num1, &arg1);
+       if (cmp_var(&arg1, &const_one) == 0)
+           PG_RETURN_NUMERIC(make_result(&const_one));
+       PG_RETURN_NUMERIC(make_result(&const_nan));
+   }
 
    /*
     * Initialize things
 
  0.0000000000000000
 (1 row)
 
+-- NaNs
+select 'NaN'::numeric ^ 'NaN'::numeric;
+ ?column? 
+----------
+      NaN
+(1 row)
+
+select 'NaN'::numeric ^ 0;
+ ?column? 
+----------
+        1
+(1 row)
+
+select 'NaN'::numeric ^ 1;
+ ?column? 
+----------
+      NaN
+(1 row)
+
+select 0 ^ 'NaN'::numeric;
+ ?column? 
+----------
+      NaN
+(1 row)
+
+select 1 ^ 'NaN'::numeric;
+ ?column? 
+----------
+        1
+(1 row)
+
 -- invalid inputs
 select 0.0 ^ (-12.34);
 ERROR:  zero raised to a negative power is undefined
 
 select 12.34 ^ 0.0;
 select 0.0 ^ 12.34;
 
+-- NaNs
+select 'NaN'::numeric ^ 'NaN'::numeric;
+select 'NaN'::numeric ^ 0;
+select 'NaN'::numeric ^ 1;
+select 0 ^ 'NaN'::numeric;
+select 1 ^ 'NaN'::numeric;
+
 -- invalid inputs
 select 0.0 ^ (-12.34);
 select (-12.34) ^ 1.2;