From 463c085852be921199b7a6d0987378d55145e41c Mon Sep 17 00:00:00 2001
From: Andrey Lepikhov <a.lepikhov@postgrespro.ru>
Date: Mon, 11 Jul 2022 12:25:43 +0500
Subject: [PATCH] Use Index path with the best selectivity estimation

---
 src/backend/optimizer/util/pathnode.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c
index 483c4f4137..a8d7f22343 100644
--- a/src/backend/optimizer/util/pathnode.c
+++ b/src/backend/optimizer/util/pathnode.c
@@ -207,6 +207,20 @@ compare_path_costs_fuzzily(Path *path1, Path *path2, double fuzz_factor)
 		/* ... but path2 fuzzily worse on startup, so path1 wins */
 		return COSTS_BETTER1;
 	}
+	if (IsA(path1, IndexPath) && IsA(path2, IndexPath))
+	{
+		/*
+		 * Couldn't differ value of the paths. Last chance - if these paths
+		 * are index paths - use the path with the lower selectivity value.
+		 */
+		if (((IndexPath *) path1)->indexselectivity <
+			((IndexPath *) path2)->indexselectivity)
+			return COSTS_BETTER1;
+
+		if (((IndexPath *) path1)->indexselectivity >
+			((IndexPath *) path2)->indexselectivity)
+			return COSTS_BETTER2;
+	}
 	/* fuzzily the same on both costs */
 	return COSTS_EQUAL;
 
-- 
2.34.1

