]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
btree_gist: fix NaN handling in float4/float8 opclasses.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 1 Jul 2026 17:27:22 +0000 (13:27 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 1 Jul 2026 17:27:22 +0000 (13:27 -0400)
The float4 and float8 btree_gist opclasses compared keys with raw C
operators (==, <, >).  IEEE 754 makes every comparison involving NaN
false, so GiST disagreed with the regular float comparison operators
and with the btree opclass, which uses float[4|8]_cmp_internal()
(so that all NaNs are equal and NaN sorts after every non-NaN value).

In addition, the penalty and distance functions were not careful
about NaNs, and the penalty functions could also misbehave for IEEE
infinities.  Wrong answers from the penalty functions would probably
do no more than make the index non-optimal, but the distance mistakes
were visible from SQL.

To fix, make the comparison functions rely on the same NaN-aware
comparison functions the core code uses, and rewrite the penalty
and distance functions to follow the rules that NaNs are equal
but maximally far away from non-NaNs.  The penalty_num() code was
formerly shared between integral and float cases, but I chose to make
two copies so that the integral cases are not saddled with the extra
logic for NaNs and infinities/overflows.  I also rewrote it as static
inline functions instead of an unreadable and uncommented macro.

The float penalty functions were previously unreached by the
regression tests, so add new test cases to exercise them.

There's no on-disk format change, but users who have NaN entries
in a btree_gist index would be well advised to reindex it.

Bug: #19501
Bug: #19524
Reported-by: Man Zeng <zengman@halodbtech.com>
Reported-by: Yuelin Wang <3020001251@tju.edu.cn>
Author: Bill Kim <billkimjh@gmail.com>
Co-authored-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19501-3bff3bbc97f1e7c9@postgresql.org
Discussion: https://postgr.es/m/19524-9559d302c8455664@postgresql.org
Discussion: https://postgr.es/m/CAMQXxcgbtD2LXfX0tpgvOizxP-XxrCHV2ZDy4By_TZnJMsxXWQ@mail.gmail.com
Backpatch-through: 14

contrib/btree_gist/btree_float4.c
contrib/btree_gist/btree_float8.c
contrib/btree_gist/btree_utils_num.h
contrib/btree_gist/data/float4.data
contrib/btree_gist/data/float8.data
contrib/btree_gist/expected/float4.out
contrib/btree_gist/expected/float8.out
contrib/btree_gist/expected/numeric.out
contrib/btree_gist/sql/float4.sql
contrib/btree_gist/sql/float8.sql

index 84ca5eee5012cf49466beb4a170578a4dc15ffcb..602949c20557d96b965de4fe914f403ab58d3bca 100644 (file)
@@ -25,30 +25,36 @@ PG_FUNCTION_INFO_V1(gbt_float4_distance);
 PG_FUNCTION_INFO_V1(gbt_float4_penalty);
 PG_FUNCTION_INFO_V1(gbt_float4_same);
 
+/*
+ * Use the NaN-aware comparators from utils/float.h, so that our results
+ * will agree with standard btree indexes.  Note that penalty and distance
+ * functions below must also cope with NaNs, in particular with the policy
+ * that all NaNs are equal.
+ */
 static bool
 gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float4 *) a) > *((const float4 *) b));
+       return float4_gt(*((const float4 *) a), *((const float4 *) b));
 }
 static bool
 gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float4 *) a) >= *((const float4 *) b));
+       return float4_ge(*((const float4 *) a), *((const float4 *) b));
 }
 static bool
 gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float4 *) a) == *((const float4 *) b));
+       return float4_eq(*((const float4 *) a), *((const float4 *) b));
 }
 static bool
 gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float4 *) a) <= *((const float4 *) b));
+       return float4_le(*((const float4 *) a), *((const float4 *) b));
 }
 static bool
 gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float4 *) a) < *((const float4 *) b));
+       return float4_lt(*((const float4 *) a), *((const float4 *) b));
 }
 
 static int
@@ -56,22 +62,33 @@ gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
 {
        float4KEY  *ia = (float4KEY *) (((const Nsrt *) a)->t);
        float4KEY  *ib = (float4KEY *) (((const Nsrt *) b)->t);
+       int                     res;
 
-       if (ia->lower == ib->lower)
-       {
-               if (ia->upper == ib->upper)
-                       return 0;
-
-               return (ia->upper > ib->upper) ? 1 : -1;
-       }
-
-       return (ia->lower > ib->lower) ? 1 : -1;
+       res = float4_cmp_internal(ia->lower, ib->lower);
+       if (res != 0)
+               return res;
+       return float4_cmp_internal(ia->upper, ib->upper);
 }
 
 static float8
 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return GET_FLOAT_DISTANCE(float4, a, b);
+       float8          arg1 = *(const float4 *) a;
+       float8          arg2 = *(const float4 *) b;
+       float8          r;
+
+       r = arg1 - arg2;
+       /* needn't consider isinf case here, must be due to input infinity */
+       if (unlikely(isnan(r)))
+       {
+               if (isnan(arg1) && isnan(arg2))
+                       r = 0.0;                        /* treat NaNs as equal */
+               else if (isnan(arg1) || isnan(arg2))
+                       r = get_float8_infinity();      /* max dist for NaN vs non-NaN */
+               else
+                       r = 0.0;                        /* must be Inf - Inf case */
+       }
+       return fabs(r);
 }
 
 
@@ -101,7 +118,15 @@ float4_dist(PG_FUNCTION_ARGS)
        r = a - b;
        if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
                float_overflow_error();
-
+       if (unlikely(isnan(r)))
+       {
+               if (isnan(a) && isnan(b))
+                       r = 0.0;                        /* treat NaNs as equal */
+               else if (isnan(a) || isnan(b))
+                       r = get_float4_infinity();      /* max dist for NaN vs non-NaN */
+               else
+                       r = 0.0;                        /* must be Inf - Inf case */
+       }
        PG_RETURN_FLOAT4(fabsf(r));
 }
 
@@ -187,7 +212,7 @@ gbt_float4_penalty(PG_FUNCTION_ARGS)
        float4KEY  *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
        float      *result = (float *) PG_GETARG_POINTER(2);
 
-       penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
+       float_penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
 
        PG_RETURN_POINTER(result);
 }
index 081a719b0061c535e251a4e51fa71004e5728bac..f740d8ecd8557eb9d0a09121c0f997638363b891 100644 (file)
@@ -26,30 +26,36 @@ PG_FUNCTION_INFO_V1(gbt_float8_penalty);
 PG_FUNCTION_INFO_V1(gbt_float8_same);
 
 
+/*
+ * Use the NaN-aware comparators from utils/float.h, so that our results
+ * will agree with standard btree indexes.  Note that penalty and distance
+ * functions below must also cope with NaNs, in particular with the policy
+ * that all NaNs are equal.
+ */
 static bool
 gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float8 *) a) > *((const float8 *) b));
+       return float8_gt(*((const float8 *) a), *((const float8 *) b));
 }
 static bool
 gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float8 *) a) >= *((const float8 *) b));
+       return float8_ge(*((const float8 *) a), *((const float8 *) b));
 }
 static bool
 gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float8 *) a) == *((const float8 *) b));
+       return float8_eq(*((const float8 *) a), *((const float8 *) b));
 }
 static bool
 gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float8 *) a) <= *((const float8 *) b));
+       return float8_le(*((const float8 *) a), *((const float8 *) b));
 }
 static bool
 gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
 {
-       return (*((const float8 *) a) < *((const float8 *) b));
+       return float8_lt(*((const float8 *) a), *((const float8 *) b));
 }
 
 static int
@@ -57,16 +63,12 @@ gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
 {
        float8KEY  *ia = (float8KEY *) (((const Nsrt *) a)->t);
        float8KEY  *ib = (float8KEY *) (((const Nsrt *) b)->t);
+       int                     res;
 
-       if (ia->lower == ib->lower)
-       {
-               if (ia->upper == ib->upper)
-                       return 0;
-
-               return (ia->upper > ib->upper) ? 1 : -1;
-       }
-
-       return (ia->lower > ib->lower) ? 1 : -1;
+       res = float8_cmp_internal(ia->lower, ib->lower);
+       if (res != 0)
+               return res;
+       return float8_cmp_internal(ia->upper, ib->upper);
 }
 
 static float8
@@ -79,6 +81,15 @@ gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
        r = arg1 - arg2;
        if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
                float_overflow_error();
+       if (unlikely(isnan(r)))
+       {
+               if (isnan(arg1) && isnan(arg2))
+                       r = 0.0;                        /* treat NaNs as equal */
+               else if (isnan(arg1) || isnan(arg2))
+                       r = get_float8_infinity();      /* max dist for NaN vs non-NaN */
+               else
+                       r = 0.0;                        /* must be Inf - Inf case */
+       }
        return fabs(r);
 }
 
@@ -109,7 +120,15 @@ float8_dist(PG_FUNCTION_ARGS)
        r = a - b;
        if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
                float_overflow_error();
-
+       if (unlikely(isnan(r)))
+       {
+               if (isnan(a) && isnan(b))
+                       r = 0.0;                        /* treat NaNs as equal */
+               else if (isnan(a) || isnan(b))
+                       r = get_float8_infinity();      /* max dist for NaN vs non-NaN */
+               else
+                       r = 0.0;                        /* must be Inf - Inf case */
+       }
        PG_RETURN_FLOAT8(fabs(r));
 }
 
@@ -194,7 +213,7 @@ gbt_float8_penalty(PG_FUNCTION_ARGS)
        float8KEY  *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
        float      *result = (float *) PG_GETARG_POINTER(2);
 
-       penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
+       float_penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
 
        PG_RETURN_POINTER(result);
 }
index 11e892311eeb3398a30f7082ec3fdeefd066caf3..6d2d656c8d4da3ec4cea8fd419978c99bf6b637c 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "access/gist.h"
 #include "btree_gist.h"
+#include "utils/float.h"
 #include "utils/rel.h"
 
 typedef char GBT_NUMKEY;
@@ -59,21 +60,124 @@ typedef struct
 
 
 /*
- * Note: The factor 0.49 in following macro avoids floating point overflows
+ * Compute penalty for expanding a range olower..oupper to nlower..nupper.
+ *
+ * Although the arguments are declared double, they must not be NaN nor
+ * large enough to risk overflows in the calculations herein.  We only
+ * actually use this for integral data types, so there's no hazard.
+ */
+static inline float
+penalty_num_impl(double olower, double oupper,
+                                double nlower, double nupper,
+                                int natts)
+{
+       float           result = 0.0F;
+       double          tmp = 0.0;
+
+       /* Add penalty for expanding upper bound */
+       if (nupper > oupper)
+               tmp += nupper - oupper;
+       /* Add penalty for expanding lower bound */
+       if (olower > nlower)
+               tmp += olower - nlower;
+       if (tmp > 0.0)
+       {
+               /* Ensure result is non-zero, even if next step underflows to zero */
+               result += FLT_MIN;
+               /* Scale penalty to 0 .. 1 */
+               result += (float) (tmp / (tmp + (oupper - olower)));
+               /* Scale to 0 .. FLT_MAX / (natts + 1) */
+               result *= FLT_MAX / (natts + 1);
+       }
+       return result;
+}
+
+/*
+ * As above, but the input values are float4 or float8, so we must cope
+ * with NaNs, infinities, and overflows.
+ */
+static inline float
+float_penalty_num_impl(double olower, double oupper,
+                                          double nlower, double nupper,
+                                          int natts)
+{
+       float           result = 0.0F;
+       double          tmp = 0.0;
+
+       /* Add penalty for expanding upper bound */
+       if (float8_gt(nupper, oupper))
+       {
+               double          delta = nupper - oupper;
+
+               if (unlikely(isnan(delta)))
+               {
+                       /* oupper couldn't be NaN here, see float8_gt */
+                       if (isnan(nupper))
+                               delta = FLT_MAX;        /* max penalty for NaN vs non-NaN */
+                       else
+                               delta = 0.0;    /* must be Inf - Inf case */
+               }
+               else if (delta > FLT_MAX)
+                       delta = FLT_MAX;        /* clamp to FLT_MAX, esp for infinity */
+               tmp += delta;
+       }
+       /* Add penalty for expanding lower bound */
+       if (float8_gt(olower, nlower))
+       {
+               double          delta = olower - nlower;
+
+               if (unlikely(isnan(delta)))
+               {
+                       /* nlower couldn't be NaN here, see float8_gt */
+                       if (isnan(olower))
+                               delta = FLT_MAX;        /* max penalty for NaN vs non-NaN */
+                       else
+                               delta = 0.0;    /* must be Inf - Inf case */
+               }
+               else if (delta > FLT_MAX)
+                       delta = FLT_MAX;        /* clamp to FLT_MAX, esp for infinity */
+               tmp += delta;
+       }
+       if (tmp > 0.0)
+       {
+               double          delta = oupper - olower;
+
+               /* Clamp delta (the original range size) to 0 .. FLT_MAX */
+               if (unlikely(isnan(delta)))
+               {
+                       /* here, we must deal with olower possibly being NaN */
+                       if (isnan(oupper) && isnan(olower))
+                               delta = 0.0;    /* treat NaNs as equal */
+                       else if (isnan(oupper) || isnan(olower))
+                               delta = FLT_MAX;        /* max penalty for NaN vs non-NaN */
+                       else
+                               delta = 0.0;    /* must be Inf - Inf case */
+               }
+               else if (delta > FLT_MAX)
+                       delta = FLT_MAX;        /* clamp to FLT_MAX, esp for infinity */
+               /* Ensure result is non-zero, even if next step underflows to zero */
+               result += FLT_MIN;
+               /* Scale penalty to 0 .. 1 */
+               result += (float) (tmp / (tmp + delta));
+               /* Scale to 0 .. FLT_MAX / (natts + 1) */
+               result *= FLT_MAX / (natts + 1);
+       }
+       return result;
+}
+
+/*
+ * These macros provide backwards-compatible notation for callers.
  */
 #define penalty_num(result,olower,oupper,nlower,nupper) do { \
-  double       tmp = 0.0F; \
-  (*(result))  = 0.0F; \
-  if ( (nupper) > (oupper) ) \
-         tmp += ( ((double)nupper)*0.49F - ((double)oupper)*0.49F ); \
-  if ( (olower) > (nlower)  ) \
-         tmp += ( ((double)olower)*0.49F - ((double)nlower)*0.49F ); \
-  if (tmp > 0.0F) \
-  { \
-       (*(result)) += FLT_MIN; \
-       (*(result)) += (float) ( ((double)(tmp)) / ( (double)(tmp) + ( ((double)(oupper))*0.49F - ((double)(olower))*0.49F ) ) ); \
-       (*(result)) *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1)); \
-  } \
+       GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0); \
+       *(result) = penalty_num_impl(olower, oupper, nlower, nupper, \
+                                                                entry->rel->rd_att->natts); \
+} while (0)
+
+#define float_penalty_num(result,olower,oupper,nlower,nupper) do { \
+       GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0); \
+       *(result) = float_penalty_num_impl(olower, oupper, nlower, nupper, \
+                                                                          entry->rel->rd_att->natts); \
 } while (0)
 
 
@@ -87,6 +191,7 @@ typedef struct
         (ivp)->day * (24.0 * SECS_PER_HOUR) + \
         (ivp)->month * (30.0 * SECS_PER_DAY))
 
+/* This macro is not safe to use with actual float inputs, only integers */
 #define GET_FLOAT_DISTANCE(t, arg1, arg2)      fabs( ((float8) *((const t *) (arg1))) - ((float8) *((const t *) (arg2))) )
 
 
index 947955e468009c4f7bcf66552170a5aa11b77d18..af7d09f00d6443d07d3c6d68396b2b4d96ffd470 100644 (file)
 \N
 2972.381398
 220.199877
+Infinity
+-Infinity
+NaN
 3542.561032
 -2168.024176
 -3305.714558
index ff21226e0665384cb14c9e0cdd292685a0d9c37a..b60e22f957f2e81c1d39e84318c3140a7d0c11e0 100644 (file)
 27770.539968
 13275.355549
 -4267.695804
+Infinity
+-Infinity
+NaN
 \N
 \N
 38915.525185
index dfe732049e6d6b7b7b5541ca25047c0b55978657..a917b79a63bc75d938499365137f4104f5c217f2 100644 (file)
@@ -5,13 +5,13 @@ SET enable_seqscan=on;
 SELECT count(*) FROM float4tmp WHERE a <  -179.0;
  count 
 -------
-   244
+   245
 (1 row)
 
 SELECT count(*) FROM float4tmp WHERE a <= -179.0;
  count 
 -------
-   245
+   246
 (1 row)
 
 SELECT count(*) FROM float4tmp WHERE a  = -179.0;
@@ -23,13 +23,13 @@ SELECT count(*) FROM float4tmp WHERE a  = -179.0;
 SELECT count(*) FROM float4tmp WHERE a >= -179.0;
  count 
 -------
-   303
+   305
 (1 row)
 
 SELECT count(*) FROM float4tmp WHERE a >  -179.0;
  count 
 -------
-   302
+   304
 (1 row)
 
 SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
@@ -45,13 +45,13 @@ SET enable_seqscan=off;
 SELECT count(*) FROM float4tmp WHERE a <  -179.0::float4;
  count 
 -------
-   244
+   245
 (1 row)
 
 SELECT count(*) FROM float4tmp WHERE a <= -179.0::float4;
  count 
 -------
-   245
+   246
 (1 row)
 
 SELECT count(*) FROM float4tmp WHERE a  = -179.0::float4;
@@ -63,13 +63,13 @@ SELECT count(*) FROM float4tmp WHERE a  = -179.0::float4;
 SELECT count(*) FROM float4tmp WHERE a >= -179.0::float4;
  count 
 -------
-   303
+   305
 (1 row)
 
 SELECT count(*) FROM float4tmp WHERE a >  -179.0::float4;
  count 
 -------
-   302
+   304
 (1 row)
 
 EXPLAIN (COSTS OFF)
@@ -89,3 +89,38 @@ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
  -158.17741 | 20.822586
 (3 rows)
 
+-- EXCLUDE constraint must block a duplicate NaN, same as it does for finite
+-- values.
+CREATE TABLE float4excl (a float4, EXCLUDE USING gist (a WITH =));
+INSERT INTO float4excl VALUES ('NaN'::float4);
+INSERT INTO float4excl VALUES ('NaN'::float4);  -- expect: violates EXCLUDE
+ERROR:  conflicting key value violates exclusion constraint "float4excl_a_excl"
+DETAIL:  Key (a)=(NaN) conflicts with existing key (a)=(NaN).
+SELECT count(*) FROM float4excl;
+ count 
+-------
+     1
+(1 row)
+
+-- Test double-column index
+CREATE INDEX float4idx2 ON float4tmp USING gist ( a, abs(a) );
+EXPLAIN (COSTS OFF)
+SELECT count(*) FROM float4tmp WHERE abs(a) = 179.0::float4;
+                    QUERY PLAN                    
+--------------------------------------------------
+ Aggregate
+   ->  Bitmap Heap Scan on float4tmp
+         Recheck Cond: (abs(a) = '179'::real)
+         ->  Bitmap Index Scan on float4idx2
+               Index Cond: (abs(a) = '179'::real)
+(5 rows)
+
+SELECT count(*) FROM float4tmp WHERE abs(a) = 179.0::float4;
+ count 
+-------
+     1
+(1 row)
+
+RESET enable_seqscan;
+RESET enable_indexscan;
+RESET enable_bitmapscan;
index ebd0ef3d689a14a40e6ca991352753065c764eba..194bd210ac60ff1929889cdd2a79a1655f782913 100644 (file)
@@ -5,13 +5,13 @@ SET enable_seqscan=on;
 SELECT count(*) FROM float8tmp WHERE a <  -1890.0;
  count 
 -------
-   237
+   238
 (1 row)
 
 SELECT count(*) FROM float8tmp WHERE a <= -1890.0;
  count 
 -------
-   238
+   239
 (1 row)
 
 SELECT count(*) FROM float8tmp WHERE a  = -1890.0;
@@ -23,13 +23,13 @@ SELECT count(*) FROM float8tmp WHERE a  = -1890.0;
 SELECT count(*) FROM float8tmp WHERE a >= -1890.0;
  count 
 -------
-   307
+   309
 (1 row)
 
 SELECT count(*) FROM float8tmp WHERE a >  -1890.0;
  count 
 -------
-   306
+   308
 (1 row)
 
 SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
@@ -45,13 +45,13 @@ SET enable_seqscan=off;
 SELECT count(*) FROM float8tmp WHERE a <  -1890.0::float8;
  count 
 -------
-   237
+   238
 (1 row)
 
 SELECT count(*) FROM float8tmp WHERE a <= -1890.0::float8;
  count 
 -------
-   238
+   239
 (1 row)
 
 SELECT count(*) FROM float8tmp WHERE a  = -1890.0::float8;
@@ -63,13 +63,13 @@ SELECT count(*) FROM float8tmp WHERE a  = -1890.0::float8;
 SELECT count(*) FROM float8tmp WHERE a >= -1890.0::float8;
  count 
 -------
-   307
+   309
 (1 row)
 
 SELECT count(*) FROM float8tmp WHERE a >  -1890.0::float8;
  count 
 -------
-   306
+   308
 (1 row)
 
 EXPLAIN (COSTS OFF)
@@ -89,3 +89,38 @@ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
   -1769.73634 | 120.26366000000007
 (3 rows)
 
+-- EXCLUDE constraint must block a duplicate NaN, same as it does for finite
+-- values.
+CREATE TABLE float8excl (a float8, EXCLUDE USING gist (a WITH =));
+INSERT INTO float8excl VALUES ('NaN'::float8);
+INSERT INTO float8excl VALUES ('NaN'::float8);  -- expect: violates EXCLUDE
+ERROR:  conflicting key value violates exclusion constraint "float8excl_a_excl"
+DETAIL:  Key (a)=(NaN) conflicts with existing key (a)=(NaN).
+SELECT count(*) FROM float8excl;
+ count 
+-------
+     1
+(1 row)
+
+-- Test double-column index
+CREATE INDEX float8idx2 ON float8tmp USING gist ( a, abs(a) );
+EXPLAIN (COSTS OFF)
+SELECT count(*) FROM float8tmp WHERE abs(a) = 1890.0::float8;
+                          QUERY PLAN                           
+---------------------------------------------------------------
+ Aggregate
+   ->  Bitmap Heap Scan on float8tmp
+         Recheck Cond: (abs(a) = '1890'::double precision)
+         ->  Bitmap Index Scan on float8idx2
+               Index Cond: (abs(a) = '1890'::double precision)
+(5 rows)
+
+SELECT count(*) FROM float8tmp WHERE abs(a) = 1890.0::float8;
+ count 
+-------
+     1
+(1 row)
+
+RESET enable_seqscan;
+RESET enable_indexscan;
+RESET enable_bitmapscan;
index ae839b8ec839d2598b1dd03262365ea14861c405..34c1e568063f2e0a560b8975c40b5c6973c61a08 100644 (file)
@@ -7,13 +7,13 @@ SET enable_seqscan=on;
 SELECT count(*) FROM numerictmp WHERE a <  -1890.0;
  count 
 -------
-   505
+   506
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a <= -1890.0;
  count 
 -------
-   506
+   507
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a  = -1890.0;
@@ -25,37 +25,37 @@ SELECT count(*) FROM numerictmp WHERE a  = -1890.0;
 SELECT count(*) FROM numerictmp WHERE a >= -1890.0;
  count 
 -------
-   597
+   599
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a >  -1890.0;
  count 
 -------
-   596
+   598
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a <  'NaN' ;
  count 
 -------
-  1100
+  1102
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a <= 'NaN' ;
  count 
 -------
-  1102
+  1105
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a  = 'NaN' ;
  count 
 -------
-     2
+     3
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a >= 'NaN' ;
  count 
 -------
-     2
+     3
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a >  'NaN' ;
@@ -67,13 +67,13 @@ SELECT count(*) FROM numerictmp WHERE a >  'NaN' ;
 SELECT count(*) FROM numerictmp WHERE a <  0 ;
  count 
 -------
-   523
+   524
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a <= 0 ;
  count 
 -------
-   526
+   527
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a  = 0 ;
@@ -85,13 +85,13 @@ SELECT count(*) FROM numerictmp WHERE a  = 0 ;
 SELECT count(*) FROM numerictmp WHERE a >= 0 ;
  count 
 -------
-   579
+   581
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a >  0 ;
  count 
 -------
-   576
+   578
 (1 row)
 
 CREATE INDEX numericidx ON numerictmp USING gist ( a );
@@ -99,13 +99,13 @@ SET enable_seqscan=off;
 SELECT count(*) FROM numerictmp WHERE a <  -1890.0;
  count 
 -------
-   505
+   506
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a <= -1890.0;
  count 
 -------
-   506
+   507
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a  = -1890.0;
@@ -117,37 +117,37 @@ SELECT count(*) FROM numerictmp WHERE a  = -1890.0;
 SELECT count(*) FROM numerictmp WHERE a >= -1890.0;
  count 
 -------
-   597
+   599
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a >  -1890.0;
  count 
 -------
-   596
+   598
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a <  'NaN' ;
  count 
 -------
-  1100
+  1102
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a <= 'NaN' ;
  count 
 -------
-  1102
+  1105
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a  = 'NaN' ;
  count 
 -------
-     2
+     3
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a >= 'NaN' ;
  count 
 -------
-     2
+     3
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a >  'NaN' ;
@@ -159,13 +159,13 @@ SELECT count(*) FROM numerictmp WHERE a >  'NaN' ;
 SELECT count(*) FROM numerictmp WHERE a <  0 ;
  count 
 -------
-   523
+   524
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a <= 0 ;
  count 
 -------
-   526
+   527
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a  = 0 ;
@@ -177,13 +177,13 @@ SELECT count(*) FROM numerictmp WHERE a  = 0 ;
 SELECT count(*) FROM numerictmp WHERE a >= 0 ;
  count 
 -------
-   579
+   581
 (1 row)
 
 SELECT count(*) FROM numerictmp WHERE a >  0 ;
  count 
 -------
-   576
+   578
 (1 row)
 
 -- Test index-only scans
index 3da1ce953c8d1471d49236d65e4ab88c447c5460..71de5d5cf49d745b18cef5178214f48f9e510792 100644 (file)
@@ -35,3 +35,20 @@ SELECT count(*) FROM float4tmp WHERE a >  -179.0::float4;
 EXPLAIN (COSTS OFF)
 SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
 SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
+
+-- EXCLUDE constraint must block a duplicate NaN, same as it does for finite
+-- values.
+CREATE TABLE float4excl (a float4, EXCLUDE USING gist (a WITH =));
+INSERT INTO float4excl VALUES ('NaN'::float4);
+INSERT INTO float4excl VALUES ('NaN'::float4);  -- expect: violates EXCLUDE
+SELECT count(*) FROM float4excl;
+
+-- Test double-column index
+CREATE INDEX float4idx2 ON float4tmp USING gist ( a, abs(a) );
+EXPLAIN (COSTS OFF)
+SELECT count(*) FROM float4tmp WHERE abs(a) = 179.0::float4;
+SELECT count(*) FROM float4tmp WHERE abs(a) = 179.0::float4;
+
+RESET enable_seqscan;
+RESET enable_indexscan;
+RESET enable_bitmapscan;
index e1e819b37f98c18c50b6eb1eb00752d6ef5fe695..a0fc84f94bb8148248034166fed5a9eb22b0665c 100644 (file)
@@ -35,3 +35,20 @@ SELECT count(*) FROM float8tmp WHERE a >  -1890.0::float8;
 EXPLAIN (COSTS OFF)
 SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
 SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
+
+-- EXCLUDE constraint must block a duplicate NaN, same as it does for finite
+-- values.
+CREATE TABLE float8excl (a float8, EXCLUDE USING gist (a WITH =));
+INSERT INTO float8excl VALUES ('NaN'::float8);
+INSERT INTO float8excl VALUES ('NaN'::float8);  -- expect: violates EXCLUDE
+SELECT count(*) FROM float8excl;
+
+-- Test double-column index
+CREATE INDEX float8idx2 ON float8tmp USING gist ( a, abs(a) );
+EXPLAIN (COSTS OFF)
+SELECT count(*) FROM float8tmp WHERE abs(a) = 1890.0::float8;
+SELECT count(*) FROM float8tmp WHERE abs(a) = 1890.0::float8;
+
+RESET enable_seqscan;
+RESET enable_indexscan;
+RESET enable_bitmapscan;