4141#include "parser/parse_oper.h"
4242#include "parser/parsetree.h"
4343#include "utils/lsyscache.h"
44- #include "utils/memutils.h"
4544#include "utils/syscache.h"
4645
4746
@@ -742,52 +741,19 @@ inheritance_planner(PlannerInfo *root)
742741List * rowMarks ;
743742List * tlist ;
744743PlannerInfo subroot ;
745- MemoryContext childcxt ;
746744ListCell * l ;
747745
748- /*
749- * Memory management here is a bit messy, because the planning process can
750- * scribble on both the query parsetree and the rangetable. We need to
751- * ensure that each call of grouping_planner gets an un-scribbled-on copy
752- * to start with, so we start by copying the given query tree each time
753- * (as a byproduct of adjust_appendrel_attrs). However, we also want to
754- * make sure that the modified rangetable ultimately gets propagated back
755- * to the master copy, to pick up any changes of the Query structures
756- * inside subquery RTEs. We do that by copying back the rangetable from
757- * the first successful child planning step. (We are effectively assuming
758- * that sub-Queries will get planned identically each time, or at least
759- * that the impacts on their rangetables will be the same each time.)
760- *
761- * Another consideration is that when there are a lot of child tables, we
762- * can eat a lot of memory this way. To fix that, we create a child
763- * memory context that can be reset between steps to recover memory, at
764- * the cost of having to copy the completed plan trees out to the parent
765- * context.
766- */
767- childcxt = AllocSetContextCreate (CurrentMemoryContext ,
768- "Inheritance child planning context" ,
769- ALLOCSET_DEFAULT_MINSIZE ,
770- ALLOCSET_DEFAULT_INITSIZE ,
771- ALLOCSET_DEFAULT_MAXSIZE );
772-
773746foreach (l ,root -> append_rel_list )
774747{
775748AppendRelInfo * appinfo = (AppendRelInfo * )lfirst (l );
776749Plan * subplan ;
777- MemoryContext oldcxt ;
778750
779751/* append_rel_list contains all append rels; ignore others */
780752if (appinfo -> parent_relid != parentRTindex )
781753continue ;
782754
783755/*
784- * Discard any cruft generated in previous loop iterations.
785- */
786- MemoryContextReset (childcxt );
787- oldcxt = MemoryContextSwitchTo (childcxt );
788-
789- /*
790- * Generate modified query (in childcxt) with this rel as target.
756+ * Generate modified query with this rel as target.
791757 */
792758memcpy (& subroot ,root ,sizeof (PlannerInfo ));
793759subroot .parse = (Query * )
@@ -801,32 +767,24 @@ inheritance_planner(PlannerInfo *root)
801767/* and we haven't created PlaceHolderInfos, either */
802768Assert (subroot .placeholder_list == NIL );
803769
804- /* Generate plan(also in childcxt) */
770+ /* Generate plan */
805771subplan = grouping_planner (& subroot ,0.0 /* retrieve all tuples */ );
806772
807- MemoryContextSwitchTo (oldcxt );
808-
809773/*
810774 * If this child rel was excluded by constraint exclusion, exclude it
811775 * from the plan.
812776 */
813777if (is_dummy_plan (subplan ))
814778continue ;
815779
816- /*
817- * Be sure to copy what we need out of the child context.
818- */
819- subplan = copyObject (subplan );
820-
821- /* Save rtable from first child to install in parent after the loop */
780+ /* Save rtable from first rel for use below */
822781if (subplans == NIL )
823- rtable = copyObject ( subroot .parse -> rtable ) ;
782+ rtable = subroot .parse -> rtable ;
824783
825784subplans = lappend (subplans ,subplan );
826785
827786/* Make sure any initplans from this rel get into the outer list */
828- root -> init_plans = list_concat (root -> init_plans ,
829- copyObject (subroot .init_plans ));
787+ root -> init_plans = list_concat (root -> init_plans ,subroot .init_plans );
830788
831789/* Build target-relations list for the executor */
832790resultRelations = lappend_int (resultRelations ,appinfo -> child_relid );
@@ -836,18 +794,14 @@ inheritance_planner(PlannerInfo *root)
836794{
837795List * rlist ;
838796
839- rlist = copyObject (subroot .parse -> returningList );
840797rlist = set_returning_clause_references (root -> glob ,
841- rlist ,
798+ subroot . parse -> returningList ,
842799subplan ,
843800appinfo -> child_relid );
844801returningLists = lappend (returningLists ,rlist );
845802}
846803}
847804
848- /* Done with child context */
849- MemoryContextDelete (childcxt );
850-
851805root -> resultRelations = resultRelations ;
852806
853807/* Mark result as unordered (probably unnecessary) */
@@ -870,7 +824,15 @@ inheritance_planner(PlannerInfo *root)
870824}
871825
872826/*
873- * Install modified rangetable from first child into parent parsetree.
827+ * Planning might have modified the rangetable, due to changes of the
828+ * Query structures inside subquery RTEs. We have to ensure that this
829+ * gets propagated back to the master copy. But can't do this until we
830+ * are done planning, because all the calls to grouping_planner need
831+ * virgin sub-Queries to work from. (We are effectively assuming that
832+ * sub-Queries will get planned identically each time, or at least that
833+ * the impacts on their rangetables will be the same each time.)
834+ *
835+ * XXX should clean this up someday
874836 */
875837parse -> rtable = rtable ;
876838