@@ -66,7 +66,9 @@ int pg_pathman_insert_into_fdw = PF_FDW_INSERT_POSTGRES;
6666CustomScanMethods partition_filter_plan_methods ;
6767CustomExecMethods partition_filter_exec_methods ;
6868
69-
69+ static ExprState * prepare_expr_state (Node * expr ,
70+ Oid relid ,
71+ EState * estate );
7072static void prepare_rri_for_insert (EState * estate ,
7173 ResultRelInfoHolder * rri_holder ,
7274 const ResultPartsStorage * rps_storage ,
@@ -455,8 +457,6 @@ select_partition_for_insert(ExprContext *econtext, ExprState *expr_state,
455457 }
456458 else selected_partid = parts [0 ];
457459
458- /* Replace parent table with a suitable partition */
459- /* TODO: write a correct comment */
460460 old_mcxt = MemoryContextSwitchTo (estate -> es_query_cxt );
461461 rri_holder = scan_result_parts_storage (selected_partid , parts_storage );
462462
@@ -471,35 +471,13 @@ select_partition_for_insert(ExprContext *econtext, ExprState *expr_state,
471471
472472 /* Build an expression state if not yet */
473473 if (!rri_holder -> expr_state )
474- {
475- MemoryContext tmp_mcxt ;
476- Node * expr ;
477- Index varno = 1 ;
478- ListCell * lc ;
479-
480- /* Change varno in Vars according to range table */
481- expr = copyObject (subprel -> expr );
482- foreach (lc , estate -> es_range_table )
483- {
484- RangeTblEntry * entry = lfirst (lc );
485- if (entry -> relid == selected_partid )
486- {
487- if (varno > 1 )
488- ChangeVarNodes (expr , 1 , varno , 0 );
489- break ;
490- }
491- varno += 1 ;
492- }
493-
494- /* Prepare state for expression execution */
495- tmp_mcxt = MemoryContextSwitchTo (estate -> es_query_cxt );
496- rri_holder -> expr_state = ExecInitExpr ((Expr * ) expr , NULL );
497- MemoryContextSwitchTo (tmp_mcxt );
498- }
474+ rri_holder -> expr_state = prepare_expr_state (subprel -> expr ,
475+ selected_partid ,
476+ estate );
499477
500478 Assert (rri_holder -> expr_state != NULL );
501479
502- /* Dive in */
480+ /* Recursively search for subpartitions */
503481 rri_holder = select_partition_for_insert (econtext , rri_holder -> expr_state ,
504482 subprel ,
505483 parts_storage ,
@@ -516,6 +494,38 @@ select_partition_for_insert(ExprContext *econtext, ExprState *expr_state,
516494 return rri_holder ;
517495}
518496
497+ static ExprState *
498+ prepare_expr_state (Node * expr ,
499+ Oid relid ,
500+ EState * estate )
501+ {
502+ ExprState * expr_state ;
503+ MemoryContext old_mcxt ;
504+ Index varno = 1 ;
505+ Node * expr_copy ;
506+ ListCell * lc ;
507+
508+ /* Change varno in Vars according to range table */
509+ expr_copy = copyObject (expr );
510+ foreach (lc , estate -> es_range_table )
511+ {
512+ RangeTblEntry * entry = lfirst (lc );
513+ if (entry -> relid == relid )
514+ {
515+ if (varno > 1 )
516+ ChangeVarNodes (expr_copy , 1 , varno , 0 );
517+ break ;
518+ }
519+ varno += 1 ;
520+ }
521+
522+ /* Prepare state for expression execution */
523+ old_mcxt = MemoryContextSwitchTo (estate -> es_query_cxt );
524+ expr_state = ExecInitExpr ((Expr * ) expr_copy , NULL );
525+ MemoryContextSwitchTo (old_mcxt );
526+
527+ return expr_state ;
528+ }
519529
520530/*
521531 * --------------------------------
@@ -596,40 +606,22 @@ partition_filter_create_scan_state(CustomScan *node)
596606void
597607partition_filter_begin (CustomScanState * node , EState * estate , int eflags )
598608{
599- Index varno = 1 ;
600- Node * expr ;
601- MemoryContext old_mcxt ;
602609 PartitionFilterState * state = (PartitionFilterState * ) node ;
603- const PartRelationInfo * prel ;
604- ListCell * lc ;
605610
606611 /* It's convenient to store PlanState in 'custom_ps' */
607612 node -> custom_ps = list_make1 (ExecInitNode (state -> subplan , estate , eflags ));
608613
609614 if (state -> expr_state == NULL )
610615 {
616+ const PartRelationInfo * prel ;
617+
611618 /* Fetch PartRelationInfo for this partitioned relation */
612619 prel = get_pathman_relation_info (state -> partitioned_table );
613620 Assert (prel != NULL );
614621
615- /* Change varno in Vars according to range table */
616- expr = copyObject (prel -> expr );
617- foreach (lc , estate -> es_range_table )
618- {
619- RangeTblEntry * entry = lfirst (lc );
620- if (entry -> relid == state -> partitioned_table )
621- {
622- if (varno > 1 )
623- ChangeVarNodes (expr , 1 , varno , 0 );
624- break ;
625- }
626- varno += 1 ;
627- }
628-
629- /* Prepare state for expression execution */
630- old_mcxt = MemoryContextSwitchTo (estate -> es_query_cxt );
631- state -> expr_state = ExecInitExpr ((Expr * ) expr , NULL );
632- MemoryContextSwitchTo (old_mcxt );
622+ state -> expr_state = prepare_expr_state (prel -> expr ,
623+ state -> partitioned_table ,
624+ estate );
633625 }
634626
635627 /* Init ResultRelInfo cache */
0 commit comments