@@ -155,15 +155,17 @@ static bool batches_overlap_firstlast(RecompressContext *recompress_ctx, Datum *
155155static void decompress_batch_to_tuplesort (TupleTableSlot * slot , TupleDesc tupdesc ,
156156 RowDecompressor * decompressor ,
157157 Tuplesortstate * recompress_tuplesortstate ,
158- Relation compressed_chunk_rel , Snapshot snapshot );
158+ Relation compressed_chunk_rel , Snapshot snapshot ,
159+ int * processed_batches );
159160static bool compact_chunk_find_overlapping_batches (Relation compressed_chunk_rel ,
160161 IndexScanDesc index_scan ,
161162 RecompressContext * recompress_ctx ,
162163 CompactChunkScanState * state );
163164static bool compact_chunk_recompress_overlapping_batches (
164165 Relation compressed_chunk_rel , IndexScanDesc index_scan , Snapshot snapshot ,
165166 RecompressContext * recompress_ctx , CompactChunkScanState * state , RowCompressor * compressor ,
166- RowDecompressor * decompressor , Tuplesortstate * recompress_tuplesortstate , BulkWriter * writer );
167+ RowDecompressor * decompressor , Tuplesortstate * recompress_tuplesortstate , BulkWriter * writer ,
168+ int max_batches );
167169static void try_updating_chunk_status (Chunk * uncompressed_chunk , Relation uncompressed_chunk_rel );
168170
169171/*
@@ -272,7 +274,10 @@ tsl_compact_chunk(PG_FUNCTION_ARGS)
272274 ts_chunk_get_table_name (chunk ))));
273275 }
274276
275- uncompressed_relid = compact_chunk_impl (chunk );
277+ int max_batches = PG_GETARG_INT32 (1 );
278+ Assert (max_batches >= 0 );
279+
280+ uncompressed_relid = compact_chunk_impl (chunk , max_batches );
276281
277282 PG_RETURN_OID (uncompressed_relid );
278283}
@@ -1061,7 +1066,8 @@ static void
10611066decompress_batch_to_tuplesort (TupleTableSlot * slot , TupleDesc tupdesc ,
10621067 RowDecompressor * decompressor ,
10631068 Tuplesortstate * recompress_tuplesortstate ,
1064- Relation compressed_chunk_rel , Snapshot snapshot )
1069+ Relation compressed_chunk_rel , Snapshot snapshot ,
1070+ int * processed_batches )
10651071{
10661072 bool should_free ;
10671073 HeapTuple compressed_tuple = ExecFetchSlotHeapTuple (slot , false, & should_free );
@@ -1092,6 +1098,11 @@ decompress_batch_to_tuplesort(TupleTableSlot *slot, TupleDesc tupdesc,
10921098 {
10931099 heap_freetuple (compressed_tuple );
10941100 }
1101+
1102+ if (processed_batches )
1103+ {
1104+ (* processed_batches )++ ;
1105+ }
10951106}
10961107
10971108/*
@@ -1169,14 +1180,17 @@ static bool
11691180compact_chunk_recompress_overlapping_batches (
11701181 Relation compressed_chunk_rel , IndexScanDesc index_scan , Snapshot snapshot ,
11711182 RecompressContext * recompress_ctx , CompactChunkScanState * state , RowCompressor * compressor ,
1172- RowDecompressor * decompressor , Tuplesortstate * recompress_tuplesortstate , BulkWriter * writer )
1183+ RowDecompressor * decompressor , Tuplesortstate * recompress_tuplesortstate , BulkWriter * writer ,
1184+ int max_batches )
11731185{
11741186 TupleTableSlot * previous_compressed_slot = table_slot_create (compressed_chunk_rel , NULL );
11751187 TupleTableSlot * compressed_slot = table_slot_create (compressed_chunk_rel , NULL );
11761188
11771189 TupleDesc compressed_rel_tupdesc = RelationGetDescr (compressed_chunk_rel );
11781190 bool overlapping = false;
11791191 bool found_overlaps = false;
1192+ /* Counts decompressed batches */
1193+ int processed_batches = 0 ;
11801194
11811195 /*
11821196 * The find pass identified the first overlapping pair. Fetch both batches by
@@ -1200,7 +1214,8 @@ compact_chunk_recompress_overlapping_batches(
12001214 decompressor ,
12011215 recompress_tuplesortstate ,
12021216 compressed_chunk_rel ,
1203- snapshot );
1217+ snapshot ,
1218+ & processed_batches );
12041219
12051220 found = table_index_fetch_tuple (index_scan -> xs_heapfetch ,
12061221 & state -> previous_tid ,
@@ -1214,7 +1229,8 @@ compact_chunk_recompress_overlapping_batches(
12141229 decompressor ,
12151230 recompress_tuplesortstate ,
12161231 compressed_chunk_rel ,
1217- snapshot );
1232+ snapshot ,
1233+ & processed_batches );
12181234
12191235 overlapping = true;
12201236 found_overlaps = true;
@@ -1248,6 +1264,12 @@ compact_chunk_recompress_overlapping_batches(
12481264 compressor ,
12491265 writer );
12501266 overlapping = false;
1267+
1268+ /* Check only after a full flush so we never leave partial work */
1269+ if (max_batches > 0 && processed_batches >= max_batches )
1270+ {
1271+ break ;
1272+ }
12511273 }
12521274
12531275 ItemPointerCopy (& index_scan -> xs_heaptid , & state -> previous_tid );
@@ -1285,7 +1307,8 @@ compact_chunk_recompress_overlapping_batches(
12851307 decompressor ,
12861308 recompress_tuplesortstate ,
12871309 compressed_chunk_rel ,
1288- snapshot );
1310+ snapshot ,
1311+ & processed_batches );
12891312
12901313 overlapping = true;
12911314 found_overlaps = true;
@@ -1296,7 +1319,8 @@ compact_chunk_recompress_overlapping_batches(
12961319 decompressor ,
12971320 recompress_tuplesortstate ,
12981321 compressed_chunk_rel ,
1299- snapshot );
1322+ snapshot ,
1323+ & processed_batches );
13001324
13011325 CommandCounterIncrement ();
13021326 }
@@ -1307,6 +1331,12 @@ compact_chunk_recompress_overlapping_batches(
13071331 recompress_segment (recompress_tuplesortstate , compressed_chunk_rel , compressor , writer );
13081332 overlapping = false;
13091333 CommandCounterIncrement ();
1334+
1335+ /* Check only after a full flush so we never leave partial work */
1336+ if (max_batches > 0 && processed_batches >= max_batches )
1337+ {
1338+ break ;
1339+ }
13101340 }
13111341
13121342 ItemPointerCopy (& index_scan -> xs_heaptid , & state -> previous_tid );
@@ -1325,7 +1355,7 @@ compact_chunk_recompress_overlapping_batches(
13251355}
13261356
13271357Oid
1328- compact_chunk_impl (Chunk * uncompressed_chunk )
1358+ compact_chunk_impl (Chunk * uncompressed_chunk , int max_batches )
13291359{
13301360 Oid uncompressed_chunk_id = uncompressed_chunk -> fd .relid ;
13311361
@@ -1496,7 +1526,8 @@ compact_chunk_impl(Chunk *uncompressed_chunk)
14961526 & compressor ,
14971527 & decompressor ,
14981528 recompress_tuplesortstate ,
1499- & writer );
1529+ & writer ,
1530+ max_batches );
15001531 row_compressor_close (& compressor );
15011532 row_decompressor_close (& decompressor );
15021533 tuplesort_end (recompress_tuplesortstate );
0 commit comments