2929#include "catalog/pg_authid.h"
3030#include "miscadmin.h"
3131#include "optimizer/cost.h"
32+ #include "optimizer/prep.h"
3233#include "optimizer/restrictinfo.h"
3334#include "rewrite/rewriteManip.h"
3435#include "utils/typcache.h"
@@ -290,6 +291,7 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
290291 if ((prel = get_pathman_relation_info (rte -> relid )) != NULL )
291292 {
292293 Relation parent_rel ; /* parent's relation (heap) */
294+ PlanRowMark * parent_rowmark ; /* parent's rowmark */
293295 Oid * children ; /* selected children oids */
294296 List * ranges , /* a list of IndexRanges */
295297 * wrappers ; /* a list of WrapperNodes */
@@ -305,6 +307,9 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
305307 /* Make copy of partitioning expression and fix Var's varno attributes */
306308 part_expr = PrelExpressionForRelid (prel , rti );
307309
310+ /* Get partitioning-related clauses (do this before append_child_relation()) */
311+ part_clauses = get_partitioning_clauses (rel -> baserestrictinfo , prel , rti );
312+
308313 if (prel -> parttype == PT_RANGE )
309314 {
310315 /*
@@ -382,19 +387,25 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
382387 /* Parent has already been locked by rewriter */
383388 parent_rel = heap_open (rte -> relid , NoLock );
384389
385- /* Add parent if asked to */
386- if (prel -> enable_parent )
387- append_child_relation (root , parent_rel , rti , 0 , rte -> relid , NULL );
390+ parent_rowmark = get_plan_rowmark (root -> rowMarks , rti );
388391
389392 /*
390- * Iterate all indexes in rangeset and append corresponding child relations .
393+ * WARNING: 'prel' might become invalid after append_child_relation() .
391394 */
395+
396+ /* Add parent if asked to */
397+ if (prel -> enable_parent )
398+ append_child_relation (root , parent_rel , parent_rowmark ,
399+ rti , 0 , rte -> relid , NULL );
400+
401+ /* Iterate all indexes in rangeset and append child relations */
392402 foreach (lc , ranges )
393403 {
394404 IndexRange irange = lfirst_irange (lc );
395405
396406 for (i = irange_lower (irange ); i <= irange_upper (irange ); i ++ )
397- append_child_relation (root , parent_rel , rti , i , children [i ], wrappers );
407+ append_child_relation (root , parent_rel , parent_rowmark ,
408+ rti , i , children [i ], wrappers );
398409 }
399410
400411 /* Now close parent relation */
@@ -424,9 +435,6 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
424435 pg_pathman_enable_runtime_merge_append ))
425436 return ;
426437
427- /* Get partitioning-related clauses */
428- part_clauses = get_partitioning_clauses (rel -> baserestrictinfo , prel , rti );
429-
430438 /* Skip if there's no PARAMs in partitioning-related clauses */
431439 if (!clause_contains_params ((Node * ) part_clauses ))
432440 return ;
0 commit comments