Skip to content

Commit

Permalink
Skip expression columns when validating index for compression
Browse files Browse the repository at this point in the history
When enabling compression on a hypertable, we check the unique indexes
defined on it to provide performance improvement hints in case the
unique index columns are not specified as segmentby columns as well.
However this check threw an error when expression columns were present
in the index, preventing the user from enabling compression.
This patch fixes this by simply ignoring the expression columns in the
index, since we cannot currently segment by an expression.

Fixes #6205
  • Loading branch information
konskov committed Oct 18, 2023
1 parent 8f3bb0b commit ef3ad47
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions .unreleased/PR_6209
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #6209 Allow enabling compression on hypertable with unique expression index
2 changes: 2 additions & 0 deletions tsl/src/compression/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,8 @@ validate_existing_indexes(Hypertable *ht, CompressColInfo *colinfo, Bitmapset *i
for (int i = 0; i < index->indnkeyatts; i++)
{
int attno = index->indkey.values[i];
if (attno == 0)
continue; /* skip check for expression column */
Form_hypertable_compression col_def = get_col_info_for_attnum(ht, colinfo, attno);
Ensure(col_def, "missing column definition for unique index");
if (col_def->segmentby_column_index < 1 && col_def->orderby_column_index < 1)
Expand Down
17 changes: 17 additions & 0 deletions tsl/test/expected/compression_ddl.out
Original file line number Diff line number Diff line change
Expand Up @@ -2392,3 +2392,20 @@ EXPLAIN (COSTS OFF) SELECT * FROM space_part ORDER BY time;
-> Seq Scan on _hyper_36_135_chunk
(35 rows)

-- test creation of unique expression index does not interfere with enabling compression
-- github issue 6205
create table mytab (col1 varchar(100) not null, col2 integer not null,
value double precision not null default 0, arrival_ts timestamptz not null, departure_ts timestamptz not null
default current_timestamp);
select create_hypertable('mytab', 'departure_ts');
WARNING: column type "character varying" used for "col1" does not follow best practices
create_hypertable
---------------------
(38,public,mytab,t)
(1 row)

create unique index myidx_unique ON
mytab (lower(col1::text), col2, departure_ts, arrival_ts);
alter table mytab set (timescaledb.compress);
WARNING: column "col2" should be used for segmenting or ordering
WARNING: column "arrival_ts" should be used for segmenting or ordering
11 changes: 11 additions & 0 deletions tsl/test/sql/compression_ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,14 @@ EXPLAIN (COSTS OFF) SELECT * FROM space_part ORDER BY time;
INSERT INTO space_part VALUES
('2022-01-01 00:02', 1, 1, 1);
EXPLAIN (COSTS OFF) SELECT * FROM space_part ORDER BY time;

-- test creation of unique expression index does not interfere with enabling compression
-- github issue 6205
create table mytab (col1 varchar(100) not null, col2 integer not null,
value double precision not null default 0, arrival_ts timestamptz not null, departure_ts timestamptz not null
default current_timestamp);

select create_hypertable('mytab', 'departure_ts');
create unique index myidx_unique ON
mytab (lower(col1::text), col2, departure_ts, arrival_ts);
alter table mytab set (timescaledb.compress);

0 comments on commit ef3ad47

Please sign in to comment.