Skip to content

Commit 5d6cdd7

Browse files
zildertimescale-automation
authored andcommitted
Fix column mapping in column stats calculation
Column stats calculation code mistakingly used column type from hypertable based on attribute number from chunk. In many cases it worked fine unless hypertable had dropped columns that new chunks didn't have. In that case column type could be identified incorrectly causing errors or segfaults. Fixes #7932 (cherry picked from commit d0fe737)
1 parent 6835350 commit 5d6cdd7

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

.unreleased/pr_7941

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes: #7941 Fix column mapping in column stats calculation
2+
Thanks: @pantonis for reporting the issue with column stats calculation

src/ts_catalog/chunk_column_stats.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ ts_chunk_column_stats_calculate(const Hypertable *ht, const Chunk *chunk)
763763
/* Get the attribute number in the HT for this column, and map to the chunk */
764764
attno = get_attnum(ht->main_table_relid, col_name);
765765
attno = ts_map_attno(ht->main_table_relid, chunk->table_id, attno);
766-
col_type = get_atttype(ht->main_table_relid, attno);
766+
col_type = get_atttype(chunk->table_id, attno);
767767

768768
/* calculate the min/max range for this column on this chunk */
769769
if (ts_chunk_get_minmax(chunk->table_id, col_type, attno, "column range", minmax))

tsl/test/expected/chunk_column_stats-15.out

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,46 @@ SELECT * FROM disable_chunk_skipping('sample_table1', 'sensor_id');
780780
(1 row)
781781

782782
ALTER TABLE sample_table1 ALTER COLUMN sensor_id TYPE TEXT;
783+
-- Check that column mapping between hypertable and chunk doesn't break when
784+
-- hypertable has dropped columns
785+
-- See: https://github.com/timescale/timescaledb/issues/7932
786+
CREATE TABLE sample_table (
787+
time TIMESTAMPTZ NOT NULL,
788+
sensor_id INTEGER,
789+
cpu NUMERIC,
790+
temperature BIGINT);
791+
CREATE INDEX ON sample_table (temperature);
792+
SELECT create_hypertable('sample_table', 'time', chunk_time_interval=>'7 days'::interval);
793+
create_hypertable
794+
---------------------------
795+
(4,public,sample_table,t)
796+
(1 row)
797+
798+
ALTER TABLE sample_table DROP COLUMN sensor_id;
799+
INSERT INTO sample_table VALUES
800+
(now(), 1, 366),
801+
(now(), 2, 501);
802+
ALTER TABLE sample_table SET (timescaledb.compress, timescaledb.compress_orderby='time');
803+
WARNING: there was some uncertainty picking the default segment by for the hypertable: Please make sure temperature is not a unique column and appropriate for a segment by
804+
NOTICE: default segment by for hypertable "sample_table" is set to "temperature"
805+
set timescaledb.enable_chunk_skipping = on;
806+
SELECT enable_chunk_skipping('sample_table', 'temperature');
807+
enable_chunk_skipping
808+
-----------------------
809+
(11,t)
810+
(1 row)
811+
812+
SELECT show_chunks('sample_table') AS "CH_NAME" order by 1 limit 1 \gset
813+
SELECT compress_chunk(:'CH_NAME');
814+
compress_chunk
815+
----------------------------------------
816+
_timescaledb_internal._hyper_4_8_chunk
817+
(1 row)
818+
819+
SELECT * FROM _timescaledb_catalog.chunk_column_stats;
820+
id | hypertable_id | chunk_id | column_name | range_start | range_end | valid
821+
----+---------------+----------+-------------+----------------------+---------------------+-------
822+
11 | 4 | 0 | temperature | -9223372036854775808 | 9223372036854775807 | t
823+
12 | 4 | 8 | temperature | 366 | 502 | t
824+
(2 rows)
825+

tsl/test/sql/chunk_column_stats.sql.in

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,24 @@ ALTER TABLE sample_table1 ALTER COLUMN sensor_id TYPE TEXT;
288288

289289
SELECT * FROM disable_chunk_skipping('sample_table1', 'sensor_id');
290290
ALTER TABLE sample_table1 ALTER COLUMN sensor_id TYPE TEXT;
291+
292+
-- Check that column mapping between hypertable and chunk doesn't break when
293+
-- hypertable has dropped columns
294+
-- See: https://github.com/timescale/timescaledb/issues/7932
295+
CREATE TABLE sample_table (
296+
time TIMESTAMPTZ NOT NULL,
297+
sensor_id INTEGER,
298+
cpu NUMERIC,
299+
temperature BIGINT);
300+
CREATE INDEX ON sample_table (temperature);
301+
SELECT create_hypertable('sample_table', 'time', chunk_time_interval=>'7 days'::interval);
302+
ALTER TABLE sample_table DROP COLUMN sensor_id;
303+
INSERT INTO sample_table VALUES
304+
(now(), 1, 366),
305+
(now(), 2, 501);
306+
ALTER TABLE sample_table SET (timescaledb.compress, timescaledb.compress_orderby='time');
307+
set timescaledb.enable_chunk_skipping = on;
308+
SELECT enable_chunk_skipping('sample_table', 'temperature');
309+
SELECT show_chunks('sample_table') AS "CH_NAME" order by 1 limit 1 \gset
310+
SELECT compress_chunk(:'CH_NAME');
311+
SELECT * FROM _timescaledb_catalog.chunk_column_stats;

0 commit comments

Comments
 (0)