v29-0004-Further-tweak-inheritance_planner-to-avoid-usele.patch

text/plain

Filename: v29-0004-Further-tweak-inheritance_planner-to-avoid-usele.patch
Type: text/plain
Part: 3
Message: Re: speeding up planning with partitions

Patch

Format: format-patch
Series: patch v29-0004
Subject: Further tweak inheritance_planner to avoid useless work
File+
src/backend/optimizer/plan/planner.c 21 1
From 24170c3cc2a0a1c22752fcdcd12ee6d51d7ef7de Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Fri, 8 Mar 2019 17:50:20 +0900
Subject: [PATCH v29 4/7] Further tweak inheritance_planner to avoid useless
 work

When it runs adjust_appendrel_attrs() on the query, there's no
need for the query's range table to contain child target RTEs as
they're just copied.  That both makes range_table_mutator() take
quite a while to finish as it has to iterate over potentially many
child RTEs.  Also, the copying of child target RTEs is pointless.
---
 src/backend/optimizer/plan/planner.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 01fc8bb446..cc48702f37 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -1318,6 +1318,7 @@ inheritance_planner(PlannerInfo *root)
 		RangeTblEntry *child_rte;
 		RelOptInfo *sub_final_rel;
 		Path	   *subpath;
+		List	   *parent_rtable;
 
 		/* append_rel_list contains all append rels; ignore others */
 		if (!bms_is_member(appinfo->parent_relid, parent_relids))
@@ -1344,11 +1345,30 @@ inheritance_planner(PlannerInfo *root)
 		 * adjust_appendrel_attrs, which copies the Query and changes
 		 * references to the parent RTE to refer to the current child RTE,
 		 * then fool around with subquery RTEs.
+		 *
+		 * In order to avoid range_table_mutator() uselessly spending time on
+		 * the child target RTEs that were added to query at the beginning of
+		 * this function, we swap the query's range table with the copy of the
+		 * range table before they were added (orig_table).
 		 */
+		parent_rtable = parent_parse->rtable;
+		parent_parse->rtable = orig_rtable;
 		subroot->parse = (Query *)
 			adjust_appendrel_attrs(parent_root,
-								   (Node *) parent_parse,
+								   (Node *) subroot->parse,
 								   1, &appinfo);
+		/*
+		 * We do however need to add those child target RTEs to the range
+		 * table so that query_planner can find this child RTE.  Other target
+		 * RTEs will not be accessed during this planning cycle, but we can't
+		 * just skip them.
+		 */
+		subroot->parse->rtable =
+			list_concat(subroot->parse->rtable,
+						list_copy_tail(parent_rtable,
+									   list_length(orig_rtable)));
+		/* Put it back for the next child's planning. */
+		parent_parse->rtable = parent_rtable;
 
 		/*
 		 * If there are securityQuals attached to the parent, move them to the
-- 
2.11.0