@@ -166,12 +166,9 @@ typedef struct CopyStateData
166166 bool volatile_defexprs ; /* is any of defexprs volatile? */
167167 List * range_table ;
168168
169- PartitionDispatch * partition_dispatch_info ;
170- int num_dispatch ; /* Number of entries in the above array */
171- int num_partitions ; /* Number of members in the following arrays */
172- ResultRelInfo * * partitions ; /* Per partition result relation pointers */
173- TupleConversionMap * * partition_tupconv_maps ;
174- TupleTableSlot * partition_tuple_slot ;
169+ /* Tuple-routing support info */
170+ PartitionTupleRouting * partition_tuple_routing ;
171+
175172 TransitionCaptureState * transition_capture ;
176173 TupleConversionMap * * transition_tupconv_maps ;
177174
@@ -2472,28 +2469,10 @@ CopyFrom(CopyState cstate)
24722469 */
24732470 if (cstate -> rel -> rd_rel -> relkind == RELKIND_PARTITIONED_TABLE )
24742471 {
2475- PartitionDispatch * partition_dispatch_info ;
2476- ResultRelInfo * * partitions ;
2477- TupleConversionMap * * partition_tupconv_maps ;
2478- TupleTableSlot * partition_tuple_slot ;
2479- int num_parted ,
2480- num_partitions ;
2481-
2482- ExecSetupPartitionTupleRouting (NULL ,
2483- cstate -> rel ,
2484- 1 ,
2485- estate ,
2486- & partition_dispatch_info ,
2487- & partitions ,
2488- & partition_tupconv_maps ,
2489- & partition_tuple_slot ,
2490- & num_parted , & num_partitions );
2491- cstate -> partition_dispatch_info = partition_dispatch_info ;
2492- cstate -> num_dispatch = num_parted ;
2493- cstate -> partitions = partitions ;
2494- cstate -> num_partitions = num_partitions ;
2495- cstate -> partition_tupconv_maps = partition_tupconv_maps ;
2496- cstate -> partition_tuple_slot = partition_tuple_slot ;
2472+ PartitionTupleRouting * proute ;
2473+
2474+ proute = cstate -> partition_tuple_routing =
2475+ ExecSetupPartitionTupleRouting (NULL , cstate -> rel , 1 , estate );
24972476
24982477 /*
24992478 * If we are capturing transition tuples, they may need to be
@@ -2506,11 +2485,11 @@ CopyFrom(CopyState cstate)
25062485 int i ;
25072486
25082487 cstate -> transition_tupconv_maps = (TupleConversionMap * * )
2509- palloc0 (sizeof (TupleConversionMap * ) * cstate -> num_partitions );
2510- for (i = 0 ; i < cstate -> num_partitions ; ++ i )
2488+ palloc0 (sizeof (TupleConversionMap * ) * proute -> num_partitions );
2489+ for (i = 0 ; i < proute -> num_partitions ; ++ i )
25112490 {
25122491 cstate -> transition_tupconv_maps [i ] =
2513- convert_tuples_by_name (RelationGetDescr (cstate -> partitions [i ]-> ri_RelationDesc ),
2492+ convert_tuples_by_name (RelationGetDescr (proute -> partitions [i ]-> ri_RelationDesc ),
25142493 RelationGetDescr (cstate -> rel ),
25152494 gettext_noop ("could not convert row type" ));
25162495 }
@@ -2530,7 +2509,7 @@ CopyFrom(CopyState cstate)
25302509 if ((resultRelInfo -> ri_TrigDesc != NULL &&
25312510 (resultRelInfo -> ri_TrigDesc -> trig_insert_before_row ||
25322511 resultRelInfo -> ri_TrigDesc -> trig_insert_instead_row )) ||
2533- cstate -> partition_dispatch_info != NULL ||
2512+ cstate -> partition_tuple_routing != NULL ||
25342513 cstate -> volatile_defexprs )
25352514 {
25362515 useHeapMultiInsert = false;
@@ -2605,10 +2584,11 @@ CopyFrom(CopyState cstate)
26052584 ExecStoreTuple (tuple , slot , InvalidBuffer , false);
26062585
26072586 /* Determine the partition to heap_insert the tuple into */
2608- if (cstate -> partition_dispatch_info )
2587+ if (cstate -> partition_tuple_routing )
26092588 {
26102589 int leaf_part_index ;
26112590 TupleConversionMap * map ;
2591+ PartitionTupleRouting * proute = cstate -> partition_tuple_routing ;
26122592
26132593 /*
26142594 * Away we go ... If we end up not finding a partition after all,
@@ -2619,11 +2599,11 @@ CopyFrom(CopyState cstate)
26192599 * partition, respectively.
26202600 */
26212601 leaf_part_index = ExecFindPartition (resultRelInfo ,
2622- cstate -> partition_dispatch_info ,
2602+ proute -> partition_dispatch_info ,
26232603 slot ,
26242604 estate );
26252605 Assert (leaf_part_index >= 0 &&
2626- leaf_part_index < cstate -> num_partitions );
2606+ leaf_part_index < proute -> num_partitions );
26272607
26282608 /*
26292609 * If this tuple is mapped to a partition that is not same as the
@@ -2641,7 +2621,7 @@ CopyFrom(CopyState cstate)
26412621 * to the selected partition.
26422622 */
26432623 saved_resultRelInfo = resultRelInfo ;
2644- resultRelInfo = cstate -> partitions [leaf_part_index ];
2624+ resultRelInfo = proute -> partitions [leaf_part_index ];
26452625
26462626 /* We do not yet have a way to insert into a foreign partition */
26472627 if (resultRelInfo -> ri_FdwRoutine )
@@ -2688,7 +2668,7 @@ CopyFrom(CopyState cstate)
26882668 * We might need to convert from the parent rowtype to the
26892669 * partition rowtype.
26902670 */
2691- map = cstate -> partition_tupconv_maps [leaf_part_index ];
2671+ map = proute -> partition_tupconv_maps [leaf_part_index ];
26922672 if (map )
26932673 {
26942674 Relation partrel = resultRelInfo -> ri_RelationDesc ;
@@ -2700,7 +2680,7 @@ CopyFrom(CopyState cstate)
27002680 * point on. Use a dedicated slot from this point on until
27012681 * we're finished dealing with the partition.
27022682 */
2703- slot = cstate -> partition_tuple_slot ;
2683+ slot = proute -> partition_tuple_slot ;
27042684 Assert (slot != NULL );
27052685 ExecSetSlotDescriptor (slot , RelationGetDescr (partrel ));
27062686 ExecStoreTuple (tuple , slot , InvalidBuffer , true);
@@ -2852,34 +2832,8 @@ CopyFrom(CopyState cstate)
28522832 ExecCloseIndices (resultRelInfo );
28532833
28542834 /* Close all the partitioned tables, leaf partitions, and their indices */
2855- if (cstate -> partition_dispatch_info )
2856- {
2857- int i ;
2858-
2859- /*
2860- * Remember cstate->partition_dispatch_info[0] corresponds to the root
2861- * partitioned table, which we must not try to close, because it is
2862- * the main target table of COPY that will be closed eventually by
2863- * DoCopy(). Also, tupslot is NULL for the root partitioned table.
2864- */
2865- for (i = 1 ; i < cstate -> num_dispatch ; i ++ )
2866- {
2867- PartitionDispatch pd = cstate -> partition_dispatch_info [i ];
2868-
2869- heap_close (pd -> reldesc , NoLock );
2870- ExecDropSingleTupleTableSlot (pd -> tupslot );
2871- }
2872- for (i = 0 ; i < cstate -> num_partitions ; i ++ )
2873- {
2874- ResultRelInfo * resultRelInfo = cstate -> partitions [i ];
2875-
2876- ExecCloseIndices (resultRelInfo );
2877- heap_close (resultRelInfo -> ri_RelationDesc , NoLock );
2878- }
2879-
2880- /* Release the standalone partition tuple descriptor */
2881- ExecDropSingleTupleTableSlot (cstate -> partition_tuple_slot );
2882- }
2835+ if (cstate -> partition_tuple_routing )
2836+ ExecCleanupTupleRouting (cstate -> partition_tuple_routing );
28832837
28842838 /* Close any trigger target relations */
28852839 ExecCleanUpTriggerState (estate );
0 commit comments