Do not use partial indexes for compression

They refer only to a subset of the table.
This commit is contained in:
Alexander Kuzmenkov 2023-11-06 12:33:07 +01:00
parent 2422f51f69
commit c34fd0b06c
4 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1 @@
Fixes: #6280 Potential data loss when compressing a table with a partial index that matches compression order.

View File

@ -291,6 +291,17 @@ compress_chunk(Oid in_table, Oid out_table, const ColumnCompressionInfo **column
Oid index_oid = lfirst_oid(lc);
Relation index_rel = index_open(index_oid, AccessShareLock);
IndexInfo *index_info = BuildIndexInfo(index_rel);
if (index_info->ii_Predicate != 0)
{
/*
* Can't use partial indexes for compression because they refer
* only to a subset of all rows.
*/
index_close(index_rel, AccessShareLock);
continue;
}
int previous_direction = NoMovementScanDirection;
int current_direction = NoMovementScanDirection;

View File

@ -912,6 +912,43 @@ SELECT decompress_chunk(show_chunks('tab1'));
(4 rows)
DROP INDEX idx_asc_null_first;
-- Can't use partial indexes for compression because they refer only to a subset of the table.
create index predicate on tab1(id, c1, time nulls first) where c2 = 0;
select count(*) from tab1;
count
-------
62400
(1 row)
select compress_chunk(show_chunks('tab1'));
INFO: compress_chunk_tuplesort_start
INFO: compress_chunk_tuplesort_start
INFO: compress_chunk_tuplesort_start
INFO: compress_chunk_tuplesort_start
compress_chunk
----------------------------------------
_timescaledb_internal._hyper_1_1_chunk
_timescaledb_internal._hyper_1_2_chunk
_timescaledb_internal._hyper_1_3_chunk
_timescaledb_internal._hyper_1_4_chunk
(4 rows)
select decompress_chunk(show_chunks('tab1'));
decompress_chunk
----------------------------------------
_timescaledb_internal._hyper_1_1_chunk
_timescaledb_internal._hyper_1_2_chunk
_timescaledb_internal._hyper_1_3_chunk
_timescaledb_internal._hyper_1_4_chunk
(4 rows)
select count(*) from tab1;
count
-------
62400
(1 row)
drop index predicate;
--Tear down
DROP TABLE tab1;
DROP TABLE tab2;

View File

@ -251,6 +251,14 @@ SELECT compress_chunk(show_chunks('tab1'));
SELECT decompress_chunk(show_chunks('tab1'));
DROP INDEX idx_asc_null_first;
-- Can't use partial indexes for compression because they refer only to a subset of the table.
create index predicate on tab1(id, c1, time nulls first) where c2 = 0;
select count(*) from tab1;
select compress_chunk(show_chunks('tab1'));
select decompress_chunk(show_chunks('tab1'));
select count(*) from tab1;
drop index predicate;
--Tear down
DROP TABLE tab1;
DROP TABLE tab2;