@@ -248,7 +248,7 @@ fini_result_parts_storage(ResultPartsStorage *parts_storage)
248248
249249/* Find a ResultRelInfo for the partition using ResultPartsStorage */
250250ResultRelInfoHolder *
251- scan_result_parts_storage (Oid partid , ResultPartsStorage * parts_storage )
251+ scan_result_parts_storage (ResultPartsStorage * parts_storage , Oid partid )
252252{
253253#define CopyToResultRelInfo (field_name ) \
254254 ( child_result_rel_info->field_name = parts_storage->base_rri->field_name )
@@ -383,6 +383,32 @@ scan_result_parts_storage(Oid partid, ResultPartsStorage *parts_storage)
383383 return rri_holder ;
384384}
385385
386+ /* Refresh PartRelationInfo for the partition in storage */
387+ void
388+ refresh_result_parts_storage (ResultPartsStorage * parts_storage , Oid partid )
389+ {
390+ if (partid == PrelParentRelid (parts_storage -> prel ))
391+ {
392+ close_pathman_relation_info (parts_storage -> prel );
393+ parts_storage -> prel = get_pathman_relation_info (partid );
394+ shout_if_prel_is_invalid (partid , parts_storage -> prel , PT_ANY );
395+ }
396+ else
397+ {
398+ ResultRelInfoHolder * rri_holder ;
399+
400+ rri_holder = hash_search (parts_storage -> result_rels_table ,
401+ (const void * ) & partid ,
402+ HASH_FIND , NULL );
403+
404+ if (rri_holder && rri_holder -> prel )
405+ {
406+ close_pathman_relation_info (rri_holder -> prel );
407+ rri_holder -> prel = get_pathman_relation_info (partid );
408+ shout_if_prel_is_invalid (partid , rri_holder -> prel , PT_ANY );
409+ }
410+ }
411+ }
386412
387413/* Build tuple conversion map (e.g. parent has a dropped column) */
388414TupleConversionMap *
@@ -486,10 +512,9 @@ select_partition_for_insert(ResultPartsStorage *parts_storage,
486512 {
487513 /* Prepare expression context */
488514 ResetExprContext (expr_context );
489-
490- /* Execute expression */
491515 expr_context -> ecxt_scantuple = slot ;
492516
517+ /* Execute expression */
493518 value = ExecEvalExprCompat (expr_state , expr_context ,
494519 & isnull , mult_result_handler );
495520
@@ -515,36 +540,21 @@ select_partition_for_insert(ResultPartsStorage *parts_storage,
515540 else partition_relid = parts [0 ];
516541
517542 /* Get ResultRelationInfo holder for the selected partition */
518- result = scan_result_parts_storage (partition_relid , parts_storage );
543+ result = scan_result_parts_storage (parts_storage , partition_relid );
519544
520545 /* Somebody has dropped or created partitions */
521- if (! PrelIsFresh ( prel ) && ( nparts == 0 || result == NULL ))
546+ if (( nparts == 0 || result == NULL ) && ! PrelIsFresh ( prel ))
522547 {
523- /* Compare original and current Oids */
524- Oid relid1 = PrelParentRelid (parts_storage -> prel ),
525- relid2 = PrelParentRelid (prel );
526-
527- /* Reopen 'prel' to make it fresh again */
528- close_pathman_relation_info (prel );
529- prel = get_pathman_relation_info (parent_relid );
530-
531- /* Store new 'prel' */
532- if (relid1 == relid2 )
533- {
534- shout_if_prel_is_invalid (parent_relid , prel , PT_ANY );
535- parts_storage -> prel = prel ;
536- }
537- else if (result && result -> prel )
538- /* TODO: WTF? this is a new RRI, not the one we used before */
539- result -> prel = prel ;
548+ /* Try building a new 'prel' for this relation */
549+ refresh_result_parts_storage (parts_storage , parent_relid );
540550 }
541551
542552 /* This partition is a parent itself */
543553 if (result && result -> prel )
544554 {
545555 prel = result -> prel ;
546556 expr_state = result -> prel_expr_state ;
547- parent_relid = PrelParentRelid ( prel ) ;
557+ parent_relid = result -> partid ;
548558 compute_value = true;
549559
550560 /* Repeat with a new dispatch */
@@ -735,14 +745,9 @@ partition_filter_exec(CustomScanState *node)
735745 if (!TupIsNull (slot ))
736746 {
737747 MemoryContext old_mcxt ;
738- PartRelationInfo * prel ;
739748 ResultRelInfoHolder * rri_holder ;
740749 ResultRelInfo * resultRelInfo ;
741750
742- /* Fetch PartRelationInfo for this partitioned relation */
743- if ((prel = get_pathman_relation_info (state -> partitioned_table )) == NULL )
744- return slot ; /* table is not partitioned anymore */
745-
746751 /* Switch to per-tuple context */
747752 old_mcxt = MemoryContextSwitchTo (GetPerTupleMemoryContext (estate ));
748753
@@ -779,9 +784,6 @@ partition_filter_exec(CustomScanState *node)
779784 slot = state -> tup_convert_slot ;
780785 }
781786
782- /* Don't forget to close 'prel'! */
783- close_pathman_relation_info (prel );
784-
785787 return slot ;
786788 }
787789
0 commit comments