simplify_useful_pathkeys.txt

text/plain

Filename: simplify_useful_pathkeys.txt
Type: text/plain
Part: 0
Message: Re: [PATCH] Incremental sort (was: PoC: Partial sort)
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index 32bf734820..49d4990e66 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -2748,7 +2748,6 @@ static List *
 get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel)
 {
 	List	   *useful_pathkeys_list = NIL;
-	ListCell   *lc;
 
 	/*
 	 * Considering query_pathkeys is always worth it, because it might allow us
@@ -2756,29 +2755,27 @@ get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel)
 	 */
 	if (root->query_pathkeys)
 	{
-		bool		query_pathkeys_ok = true;
+		ListCell   *lc;
+		List	   *pathkeys = NIL;
 
 		foreach(lc, root->query_pathkeys)
 		{
 			PathKey    *pathkey = (PathKey *) lfirst(lc);
 			EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
-			Expr	   *em_expr;
 
 			/*
-			 * We can't use incremental sort for pathkeys containing volatile
-			 * expressions. We could walk the exppression itself, but checking
-			 * ec_has_volatile here saves some cycles.
+			 * We can't build Incremental Sort when pathkeys contain elements
+			 * without an EC member not contained in the current relation, so
+			 * just ignore anything after the first such pathkey.
 			 */
-			if (pathkey_ec->ec_has_volatile ||
-				!(em_expr = find_em_expr_for_rel(pathkey_ec, rel)))
-			{
-				query_pathkeys_ok = false;
+			if (!find_em_expr_for_rel(pathkey_ec, rel))
 				break;
-			}
+
+			pathkeys = lappend(pathkeys, pathkey);
 		}
 
-		if (query_pathkeys_ok)
-			useful_pathkeys_list = list_make1(list_copy(root->query_pathkeys));
+		if (pathkeys)
+			useful_pathkeys_list = lappend(useful_pathkeys_list, pathkeys);
 	}
 
 	return useful_pathkeys_list;