@@ -157,6 +157,9 @@ static ArrayType *analyze_segmentby_candidates(ColumnAnalysis *candidates, int n
157157static ArrayType * analyze_and_get_segmentby (CompressionSettings * settings ,
158158 RowCompressor * compressor );
159159
160+ static void compressor_apply_segmentby_and_rebuild (RowCompressor * old_compressor ,
161+ BulkWriter * old_bulk_writer );
162+
160163/********************
161164 ** compress_chunk **
162165 ********************/
@@ -1063,6 +1066,9 @@ tsl_compressor_add_slot(RowCompressor *compressor, BulkWriter *bulk_writer, Tupl
10631066void
10641067tsl_compressor_flush (RowCompressor * compressor , BulkWriter * bulk_writer )
10651068{
1069+ fprintf (stderr , "flush compressor at %p\n" , compressor );
1070+ mybt ();
1071+
10661072 if (compressor -> sort_state )
10671073 {
10681074 if (compressor -> tuples_to_sort )
@@ -1071,15 +1077,7 @@ tsl_compressor_flush(RowCompressor *compressor, BulkWriter *bulk_writer)
10711077
10721078 if (compressor -> needs_analyze_segmentby )
10731079 {
1074- RowCompressor * new_compressor = NULL ;
1075- BulkWriter * new_bulk_writer = NULL ;
1076- tsl_compressor_apply_segmentby_and_rebuild (compressor ,
1077- bulk_writer ,
1078- & new_compressor ,
1079- & new_bulk_writer );
1080- * compressor = * new_compressor ;
1081- compressor -> needs_analyze_segmentby = false;
1082- * bulk_writer = * new_bulk_writer ;
1080+ compressor_apply_segmentby_and_rebuild (compressor , bulk_writer );
10831081 }
10841082
10851083 TupleTableSlot * slot = MakeTupleTableSlot (compressor -> in_desc , & TTSOpsMinimalTuple );
@@ -1116,8 +1114,11 @@ tsl_compressor_flush(RowCompressor *compressor, BulkWriter *bulk_writer)
11161114}
11171115
11181116void
1119- tsl_compressor_free (RowCompressor * compressor , BulkWriter * bulk_writer )
1117+ tsl_compressor_close (RowCompressor * compressor , BulkWriter * bulk_writer )
11201118{
1119+ fprintf (stderr , "free compressor at %p\n" , compressor );
1120+ mybt ();
1121+
11211122 if (compressor -> sort_state )
11221123 {
11231124 tuplesort_end (compressor -> sort_state );
@@ -1139,14 +1140,10 @@ tsl_compressor_free(RowCompressor *compressor, BulkWriter *bulk_writer)
11391140 * Determine the segmentby column from tuples in Tuplesortstate in the RowCompressor,
11401141 * then rebuild the compressed chunk and compressor to use it.
11411142 */
1142- void
1143- tsl_compressor_apply_segmentby_and_rebuild (RowCompressor * old_compressor ,
1144- BulkWriter * old_bulk_writer ,
1145- RowCompressor * * output_compressor ,
1146- BulkWriter * * output_bulk_writer )
1143+ static void
1144+ compressor_apply_segmentby_and_rebuild (RowCompressor * old_compressor , BulkWriter * old_bulk_writer )
11471145{
1148- * output_bulk_writer = old_bulk_writer ;
1149- * output_compressor = old_compressor ;
1146+ old_compressor -> needs_analyze_segmentby = false;
11501147 if (old_compressor -> sort_state == NULL || old_compressor -> tuples_to_sort == 0 )
11511148 {
11521149 return ;
@@ -1225,19 +1222,20 @@ tsl_compressor_apply_segmentby_and_rebuild(RowCompressor *old_compressor,
12251222 /* Initialize the new bulk writer and compressor against the new compressed relation */
12261223 Relation out_rel = table_open (new_compressed_chunk -> table_id , RowExclusiveLock );
12271224
1228- BulkWriter * new_bulk_writer = bulk_writer_alloc (out_rel , /* insert_options = */ 0 );
1225+ BulkWriter new_bulk_writer = bulk_writer_build (out_rel , /* insert_options = */ 0 );
12291226
1230- RowCompressor * new_compressor = palloc0 ( sizeof ( RowCompressor )) ;
1231- row_compressor_init (new_compressor ,
1227+ RowCompressor new_compressor ;
1228+ row_compressor_init (& new_compressor ,
12321229 settings ,
12331230 RelationGetDescr (in_rel ),
12341231 RelationGetDescr (out_rel ));
12351232
1236- MemoryContext old_context = MemoryContextSwitchTo (new_compressor -> row_compressor_context );
1237- new_compressor -> sort_state = compression_create_tuplesort_state (settings , in_rel , false);
1233+ MemoryContext old_context = MemoryContextSwitchTo (new_compressor . row_compressor_context );
1234+ new_compressor . sort_state = compression_create_tuplesort_state (settings , in_rel , false);
12381235 MemoryContextSwitchTo (old_context );
12391236
1240- new_compressor -> tuple_sort_limit = old_compressor -> tuple_sort_limit ;
1237+ new_compressor .tuple_sort_limit = old_compressor -> tuple_sort_limit ;
1238+ new_compressor .needs_analyze_segmentby = false;
12411239
12421240 /* Transfer from old sort state into the new one with segmentby settings */
12431241 TupleTableSlot * slot = MakeTupleTableSlot (old_compressor -> in_desc , & TTSOpsMinimalTuple );
@@ -1247,22 +1245,22 @@ tsl_compressor_apply_segmentby_and_rebuild(RowCompressor *old_compressor,
12471245 slot ,
12481246 NULL /*=abbrev*/ ))
12491247 {
1250- tuplesort_puttupleslot (new_compressor -> sort_state , slot );
1251- new_compressor -> tuples_to_sort ++ ;
1248+ tuplesort_puttupleslot (new_compressor . sort_state , slot );
1249+ new_compressor . tuples_to_sort ++ ;
12521250 }
12531251
12541252 ExecDropSingleTupleTableSlot (slot );
12551253
1256- tsl_compressor_free (old_compressor , old_bulk_writer );
1254+ tsl_compressor_close (old_compressor , old_bulk_writer );
12571255
12581256 ts_chunk_drop (old_compressed_chunk , DROP_RESTRICT , -1 );
12591257
1260- tuplesort_performsort (new_compressor -> sort_state );
1258+ tuplesort_performsort (new_compressor . sort_state );
12611259
12621260 table_close (in_rel , NoLock );
12631261
1264- * output_bulk_writer = new_bulk_writer ;
1265- * output_compressor = new_compressor ;
1262+ * old_bulk_writer = new_bulk_writer ;
1263+ * old_compressor = new_compressor ;
12661264}
12671265
12681266/*
@@ -1278,6 +1276,8 @@ tsl_compressor_init(Relation in_rel, BulkWriter **bulk_writer, bool sort, int so
12781276 CompressionSettings * settings = ts_compression_settings_get (in_rel -> rd_id );
12791277 Relation out_rel = table_open (settings -> fd .compress_relid , RowExclusiveLock );
12801278 RowCompressor * compressor = palloc0 (sizeof (RowCompressor ));
1279+ fprintf (stderr , "alloc compressor at %p\n" , compressor );
1280+ mybt ();
12811281 row_compressor_init (compressor , settings , RelationGetDescr (in_rel ), RelationGetDescr (out_rel ));
12821282
12831283 * bulk_writer = bulk_writer_alloc (out_rel , 0 );
@@ -1999,13 +1999,7 @@ BulkWriter *
19991999bulk_writer_alloc (Relation out_rel , int insert_options )
20002000{
20012001 BulkWriter * writer = palloc (sizeof (BulkWriter ));
2002- writer -> out_rel = out_rel ;
2003- writer -> indexstate = CatalogOpenIndexes (out_rel );
2004- writer -> mycid = GetCurrentCommandId (true);
2005- writer -> bistate = GetBulkInsertState ();
2006- writer -> estate = CreateExecutorState ();
2007- writer -> insert_options = insert_options ;
2008-
2002+ * writer = bulk_writer_build (out_rel , insert_options );
20092003 return writer ;
20102004}
20112005
0 commit comments