From 6da44715c992718782aa634e16df10ee1fe7ecdf Mon Sep 17 00:00:00 2001 From: tt Date: Wed, 1 Apr 2020 18:08:52 +0200 Subject: [PATCH 7/7] extra patch --- src/backend/optimizer/path/allpaths.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 4a72dcf8bc..0534bb24c5 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2757,7 +2757,7 @@ get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel) if (root->query_pathkeys) { ListCell *lc; - List *pathkeys = NIL; + int npathkeys = 0; /* useful pathkeys */ foreach(lc, root->query_pathkeys) { @@ -2778,11 +2778,21 @@ get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel) if (!find_em_expr_for_rel(pathkey_ec, rel)) break; - pathkeys = lappend(pathkeys, pathkey); + npathkeys++; } - if (pathkeys) - useful_pathkeys_list = lappend(useful_pathkeys_list, pathkeys); + /* + * The whole query_pathkeys list matches, so append it directly, to allow + * comparing pathkeys easily by comparing list pointer. If we have to truncate + * the pathkeys, we gotta do a copy though. + */ + if (npathkeys == list_length(root->query_pathkeys)) + useful_pathkeys_list = lappend(useful_pathkeys_list, + root->query_pathkeys); + else if (npathkeys > 0) + useful_pathkeys_list = lappend(useful_pathkeys_list, + list_truncate(list_copy(root->query_pathkeys), + npathkeys)); } return useful_pathkeys_list; -- 2.24.1