Skip to content

Commit c4ebdf6

Browse files
committed
Fix handling of chunks with no contraints
When a catalog corruption occurs, and a chunk does not contain any dimension slices, we crash in ts_dimension_slice_cmp(). This patch adds a proper check and errors out before the code path is called.
1 parent ea22843 commit c4ebdf6

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

.unreleased/fix_6816

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #6816 Fix handling of chunks with no contraints

src/chunk_scan.c

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ ts_chunk_scan_by_chunk_ids(const Hyperspace *hs, const List *chunk_ids, unsigned
197197
Assert(cube->capacity > cube->num_slices);
198198
cube->slices[cube->num_slices++] = slice_copy;
199199
}
200+
201+
if (cube->num_slices == 0)
202+
{
203+
ereport(ERROR,
204+
(errcode(ERRCODE_INTERNAL_ERROR),
205+
errmsg("chunk %s has no dimension slices", get_rel_name(chunk->table_id))));
206+
}
207+
200208
ts_hypercube_slice_sort(cube);
201209
chunk->cube = cube;
202210
}

test/expected/catalog_corruption.out

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- This file and its contents are licensed under the Apache License 2.0.
2+
-- Please see the included NOTICE for copyright information and
3+
-- LICENSE-APACHE for a copy of the license.
4+
\c :TEST_DBNAME :ROLE_SUPERUSER
5+
--- Test handling of missing dimension slices
6+
CREATE TABLE dim_test(time TIMESTAMPTZ, device int);
7+
SELECT create_hypertable('dim_test', 'time', chunk_time_interval => INTERVAL '1 day');
8+
NOTICE: adding not-null constraint to column "time"
9+
create_hypertable
10+
-----------------------
11+
(1,public,dim_test,t)
12+
(1 row)
13+
14+
-- Create two chunks
15+
INSERT INTO dim_test values('2000-01-01 00:00:00', 1);
16+
INSERT INTO dim_test values('2020-01-01 00:00:00', 1);
17+
SELECT id AS dim_slice_id FROM _timescaledb_catalog.dimension_slice
18+
ORDER BY id DESC LIMIT 1
19+
\gset
20+
-- Delete the dimension slice for the second chunk
21+
DELETE FROM _timescaledb_catalog.chunk_constraint WHERE dimension_slice_id = :dim_slice_id;
22+
\set ON_ERROR_STOP 0
23+
-- Select data
24+
SELECT * FROM dim_test;
25+
ERROR: chunk _hyper_1_2_chunk has no dimension slices
26+
-- Select data using ordered append
27+
SELECT * FROM dim_test ORDER BY time;
28+
ERROR: chunk _hyper_1_2_chunk has no dimension slices
29+
\set ON_ERROR_STOP 1
30+
DROP TABLE dim_test;

test/sql/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(TEST_FILES
55
alter.sql
66
alternate_users.sql
77
baserel_cache.sql
8+
catalog_corruption.sql
89
chunks.sql
910
chunk_adaptive.sql
1011
chunk_utils.sql

test/sql/catalog_corruption.sql

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- This file and its contents are licensed under the Apache License 2.0.
2+
-- Please see the included NOTICE for copyright information and
3+
-- LICENSE-APACHE for a copy of the license.
4+
5+
\c :TEST_DBNAME :ROLE_SUPERUSER
6+
7+
--- Test handling of missing dimension slices
8+
CREATE TABLE dim_test(time TIMESTAMPTZ, device int);
9+
SELECT create_hypertable('dim_test', 'time', chunk_time_interval => INTERVAL '1 day');
10+
11+
-- Create two chunks
12+
INSERT INTO dim_test values('2000-01-01 00:00:00', 1);
13+
INSERT INTO dim_test values('2020-01-01 00:00:00', 1);
14+
15+
SELECT id AS dim_slice_id FROM _timescaledb_catalog.dimension_slice
16+
ORDER BY id DESC LIMIT 1
17+
\gset
18+
19+
-- Delete the dimension slice for the second chunk
20+
DELETE FROM _timescaledb_catalog.chunk_constraint WHERE dimension_slice_id = :dim_slice_id;
21+
22+
\set ON_ERROR_STOP 0
23+
24+
-- Select data
25+
SELECT * FROM dim_test;
26+
27+
-- Select data using ordered append
28+
SELECT * FROM dim_test ORDER BY time;
29+
30+
\set ON_ERROR_STOP 1
31+
32+
DROP TABLE dim_test;
33+

0 commit comments

Comments
 (0)