From d8f933bc27576dc35c98e68c2a75196cc3ae51de Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Mon, 12 Feb 2024 11:40:21 +0100 Subject: [PATCH] Fix update script for tables with dropped columns (cherry picked from commit fa5c0a9b22ae7bd07c8a9e5578038a2dccd380a4) --- .unreleased/pr_6636 | 2 ++ sql/updates/2.13.1--2.14.0.sql | 2 +- test/sql/updates/post.compression.sql | 2 +- test/sql/updates/setup.compression.sql | 29 ++++++++++++++++++++------ 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 .unreleased/pr_6636 diff --git a/.unreleased/pr_6636 b/.unreleased/pr_6636 new file mode 100644 index 00000000000..a3520ba6655 --- /dev/null +++ b/.unreleased/pr_6636 @@ -0,0 +1,2 @@ +Fixes: #6636 Fixes extension update of compressed hypertables with dropped columns +Thanks: @anajavi for reporting an issue with extension update of compressed hypertables diff --git a/sql/updates/2.13.1--2.14.0.sql b/sql/updates/2.13.1--2.14.0.sql index 5f4c8c4fb79..f77cd202c2d 100644 --- a/sql/updates/2.13.1--2.14.0.sql +++ b/sql/updates/2.13.1--2.14.0.sql @@ -376,7 +376,7 @@ BEGIN -- remove columns from the compressed hypertable (but not chunks) FOR column_name IN - SELECT attname FROM pg_attribute WHERE attrelid = hypertable AND attnum > 0 + SELECT attname FROM pg_attribute WHERE attrelid = hypertable AND attnum > 0 AND NOT attisdropped LOOP cmd := format('ALTER TABLE %s DROP COLUMN %I', hypertable, column_name); EXECUTE cmd; diff --git a/test/sql/updates/post.compression.sql b/test/sql/updates/post.compression.sql index b6d948a7798..c418090d198 100644 --- a/test/sql/updates/post.compression.sql +++ b/test/sql/updates/post.compression.sql @@ -4,7 +4,7 @@ SELECT * FROM compress ORDER BY time DESC, small_cardinality, large_cardinality, some_double, some_int, some_custom, some_bool; -INSERT INTO compress +INSERT INTO compress(time, small_cardinality, large_cardinality, some_double, some_int, some_custom, some_bool) SELECT g, 'QW', g::text, 2, 0, (100,4)::custom_type_for_compression, false FROM generate_series('2019-11-01 00:00'::timestamp, '2019-12-15 00:00'::timestamp, '1 day') g; diff --git a/test/sql/updates/setup.compression.sql b/test/sql/updates/setup.compression.sql index 17a6be2b9a6..cbba9d1ec35 100644 --- a/test/sql/updates/setup.compression.sql +++ b/test/sql/updates/setup.compression.sql @@ -8,6 +8,7 @@ CREATE TABLE compress ( time TIMESTAMPTZ NOT NULL, small_cardinality TEXT NULL, large_cardinality TEXT NULL, + dropped TEXT NULL, some_double DOUBLE PRECISION NULL, some_int integer NULL, some_custom custom_type_for_compression NULL, @@ -17,20 +18,36 @@ CREATE TABLE compress ( SELECT table_name FROM create_hypertable( 'compress', 'time'); INSERT INTO compress -SELECT g, 'POR', g::text, 75.0, 40, (1,2)::custom_type_for_compression, true +SELECT g, 'POR', g::text, 'lint', 75.0, 40, (1,2)::custom_type_for_compression, true FROM generate_series('2018-12-01 00:00'::timestamp, '2018-12-31 00:00'::timestamp, '1 day') g; INSERT INTO compress -SELECT g, 'POR', NULL, NULL, NULL, NULL, NULL +SELECT g, 'POR', NULL, 'lint', NULL, NULL, NULL, NULL FROM generate_series('2018-11-01 00:00'::timestamp, '2018-12-31 00:00'::timestamp, '1 day') g; INSERT INTO compress -SELECT g, 'POR', g::text, 94.0, 45, (3,4)::custom_type_for_compression, true +SELECT g, 'POR', g::text, 'lint', 94.0, 45, (3,4)::custom_type_for_compression, true FROM generate_series('2018-11-01 00:00'::timestamp, '2018-12-15 00:00'::timestamp, '1 day') g; -ALTER TABLE compress SET (timescaledb.compress, timescaledb.compress_segmentby='small_cardinality'); - -SELECT count(compress_chunk(ch, true)) FROM show_chunks('compress') ch; +DO $$ +DECLARE + ts_minor int := (SELECT (string_to_array(extversion,'.'))[2]::int FROM pg_extension WHERE extname = 'timescaledb'); +BEGIN + + -- support for dropping columns on compressed hypertables was added in 2.10.0 + -- so we drop before compressing if the version is before 2.10.0 + IF ts_minor < 10 THEN + ALTER TABLE compress DROP COLUMN dropped; + END IF; + + ALTER TABLE compress SET (timescaledb.compress, timescaledb.compress_segmentby='small_cardinality'); + PERFORM count(compress_chunk(ch, true)) FROM show_chunks('compress') ch; + + IF ts_minor >= 10 THEN + ALTER TABLE compress DROP COLUMN dropped; + END IF; +END +$$; \if :WITH_ROLES GRANT SELECT ON compress TO tsdbadmin;