From ae0c8d09fb05d39c4f2c548bfc4720dadbd83532 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed, 7 Jun 2006 17:08:07 +0000
Subject: [PATCH] Remove "fuzzy comparison" logic in qsort comparison function
 for choose_bitmap_and().  It was way too fuzzy --- per comment, it was meant
 to be 1% relative difference, but was actually coded as 0.01 absolute
 difference, thus causing selectivities of say 0.001 and 0.000000000001 to be
 treated as equal.  I believe this thinko explains Maxim Boguk's recent
 complaint.  While we could change it to a relative test coded like
 compare_fuzzy_path_costs(), there's a bigger problem here, which is that any
 fuzziness at all renders the comparison function non-transitive, which could
 confuse qsort() to the point of delivering completely wrong results.  So
 forget the whole thing and just do an exact comparison.

---
 src/backend/optimizer/path/indxpath.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index eaf4e1cb337..75f2487757e 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.207 2006/06/06 17:59:57 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.208 2006/06/07 17:08:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -674,20 +674,18 @@ bitmap_path_comparator(const void *a, const void *b)
 	Cost		bcost;
 	Selectivity aselec;
 	Selectivity bselec;
-	Selectivity diff;
 
 	cost_bitmap_tree_node(pa, &acost, &aselec);
 	cost_bitmap_tree_node(pb, &bcost, &bselec);
 
 	/*
-	 * Since selectivities are often pretty crude, don't put blind faith
-	 * in them; if the selectivities are within 1% of being the same, treat
-	 * them as equal and sort by cost instead.
+	 * If selectivities are the same, sort by cost.  (Note: there used to be
+	 * logic here to do "fuzzy comparison", but that's a bad idea because it
+	 * fails to be transitive, which will confuse qsort terribly.)
 	 */
-	diff = aselec - bselec;
-	if (diff < -0.01)
+	if (aselec < bselec)
 		return -1;
-	if (diff > 0.01)
+	if (aselec > bselec)
 		return 1;
 
 	if (acost < bcost)
-- 
GitLab