Skip to content

Commit 83f61f2

Browse files
Block compaction in Repeatable Read
Compaction cannot safely verify results in Repeatable Read due to stale snapshots. Error early with ERRCODE_FEATURE_NOT_SUPPORTED.
1 parent 8e8e1c5 commit 83f61f2

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

tsl/src/compression/recompress.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ tsl_compact_chunk(PG_FUNCTION_ARGS)
257257

258258
ts_feature_flag_check(FEATURE_HYPERTABLE_COMPRESSION);
259259
TS_PREVENT_FUNC_IF_READ_ONLY();
260+
261+
if (IsolationUsesXactSnapshot())
262+
{
263+
ereport(ERROR,
264+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
265+
errmsg("compact_chunk is not supported in REPEATABLE READ or SERIALIZABLE "
266+
"isolation level")));
267+
}
268+
260269
Chunk *chunk = ts_chunk_get_by_relid(uncompressed_relid, true);
261270

262271
ts_hypertable_permissions_check(chunk->hypertable_relid, GetUserId());

tsl/test/expected/compact_chunk.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics') c
178178
ERROR: cannot execute compact_chunk() in a read-only transaction
179179
\set ON_ERROR_STOP 1
180180
SET default_transaction_read_only TO off;
181+
-- compact_chunk in REPEATABLE READ must fail
182+
\set ON_ERROR_STOP 0
183+
BEGIN ISOLATION LEVEL REPEATABLE READ;
184+
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics') chunk;
185+
ERROR: compact_chunk is not supported in REPEATABLE READ or SERIALIZABLE isolation level
186+
COMMIT;
187+
\set ON_ERROR_STOP 1
181188
-- Create a hypertable with a segmentby column.
182189
-- Each segment ('d1', 'd2') will have its own set of batches, and
183190
-- compact_chunk should handle overlaps per segment independently.

tsl/test/sql/compact_chunk.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics') c
136136

137137
SET default_transaction_read_only TO off;
138138

139+
-- compact_chunk in REPEATABLE READ must fail
140+
\set ON_ERROR_STOP 0
141+
BEGIN ISOLATION LEVEL REPEATABLE READ;
142+
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics') chunk;
143+
COMMIT;
144+
\set ON_ERROR_STOP 1
145+
139146
-- Create a hypertable with a segmentby column.
140147
-- Each segment ('d1', 'd2') will have its own set of batches, and
141148
-- compact_chunk should handle overlaps per segment independently.

0 commit comments

Comments
 (0)