Skip to content

Commit

Permalink
Fix update script for tables with dropped columns
Browse files Browse the repository at this point in the history
(cherry picked from commit fa5c0a9)
  • Loading branch information
svenklemm committed Feb 14, 2024
1 parent c4c1d92 commit 80f5090
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .unreleased/pr_6636
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion sql/updates/2.13.1--2.14.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/sql/updates/post.compression.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
29 changes: 23 additions & 6 deletions test/sql/updates/setup.compression.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit 80f5090

Please sign in to comment.