From b2cd1ec2a6e7fae61c2c4c9ed7594c83c17b8c5e Mon Sep 17 00:00:00 2001 From: Ante Kresic Date: Fri, 24 Nov 2023 09:53:21 +0100 Subject: [PATCH] Reset compressor between recompression runs If we reuse the compressor to recompress multiple sets of tuples, internal state gets left behind from the previous run which can contain invalid data. Resetting the compressor first iteration field between runs fixes this. --- tsl/src/compression/api.c | 3 +++ tsl/src/compression/compression.c | 6 ++++++ tsl/src/compression/compression.h | 1 + 3 files changed, 10 insertions(+) diff --git a/tsl/src/compression/api.c b/tsl/src/compression/api.c index 4bb53a9c608..b84550eb416 100644 --- a/tsl/src/compression/api.c +++ b/tsl/src/compression/api.c @@ -1500,6 +1500,7 @@ tsl_recompress_chunk_segmentwise(PG_FUNCTION_ARGS) tuplesort_performsort(segment_tuplesortstate); + row_compressor_reset(&row_compressor); recompress_segment(segment_tuplesortstate, uncompressed_chunk_rel, &row_compressor); /* now any pointers returned will be garbage */ @@ -1556,6 +1557,7 @@ tsl_recompress_chunk_segmentwise(PG_FUNCTION_ARGS) uncompressed_chunk_rel, current_segment); tuplesort_performsort(segment_tuplesortstate); + row_compressor_reset(&row_compressor); recompress_segment(segment_tuplesortstate, uncompressed_chunk_rel, &row_compressor); tuplesort_end(segment_tuplesortstate); @@ -1582,6 +1584,7 @@ tsl_recompress_chunk_segmentwise(PG_FUNCTION_ARGS) if (unmatched_rows_exist) { tuplesort_performsort(segment_tuplesortstate); + row_compressor_reset(&row_compressor); row_compressor_append_sorted_rows(&row_compressor, segment_tuplesortstate, RelationGetDescr(uncompressed_chunk_rel)); diff --git a/tsl/src/compression/compression.c b/tsl/src/compression/compression.c index f6659faf5c5..b87e7f030f5 100644 --- a/tsl/src/compression/compression.c +++ b/tsl/src/compression/compression.c @@ -1292,6 +1292,12 @@ row_compressor_flush(RowCompressor *row_compressor, CommandId mycid, bool change MemoryContextReset(row_compressor->per_row_ctx); } +void +row_compressor_reset(RowCompressor *row_compressor) +{ + row_compressor->first_iteration = true; +} + void row_compressor_finish(RowCompressor *row_compressor) { diff --git a/tsl/src/compression/compression.h b/tsl/src/compression/compression.h index db72789f9b7..bc871e01340 100644 --- a/tsl/src/compression/compression.h +++ b/tsl/src/compression/compression.h @@ -359,6 +359,7 @@ extern void row_compressor_init(RowCompressor *row_compressor, TupleDesc uncompr const ColumnCompressionInfo **column_compression_info, int16 *column_offsets, int16 num_columns_in_compressed_table, bool need_bistate, bool reset_sequence, int insert_options); +extern void row_compressor_reset(RowCompressor *row_compressor); extern void row_compressor_finish(RowCompressor *row_compressor); extern void row_compressor_append_sorted_rows(RowCompressor *row_compressor, Tuplesortstate *sorted_rel, TupleDesc sorted_desc);