Skip to content

Commit 8763d65

Browse files
committed
Show warning when downgrading with firstlast indexes present
Show warning when downgrading with firstlast indexes present and block upgrade following a downgrade with firstlast indexes. While being downgraded firstlast index columns will not be updated leading to potential incorrect results after upgrade. You can rebuild chunks with firstlast index like so: ``` DO $$ DECLARE chunk regclass; BEGIN FOR chunk IN SELECT relid FROM _timescaledb_catalog.compression_settings WHERE compress_relid IS NOT NULL AND index @> '[{"type": "firstlast"}]' LOOP PERFORM compress_chunk(chunk,recompress:=true); COMMIT; END LOOP; END $$; ```
1 parent bc0c703 commit 8763d65

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

.unreleased/pr_9967

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #9967 Block upgrade after downgrade with firstlast indexes present

sql/updates/latest-dev.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ BEGIN
1111
END;
1212
$$;
1313

14+
-- Block the update if any firstlast sparse indexes exist. This could only
15+
-- happen when downgrading and then upgrading again. Under this circumstance
16+
-- the firstlast sparse index might not be consistent for any chunks that
17+
-- were recompressed while the extension was downgraded.
18+
DO $$
19+
DECLARE
20+
affected text;
21+
BEGIN
22+
SELECT string_agg(DISTINCT relid::text, ', ')
23+
INTO affected
24+
FROM _timescaledb_catalog.compression_settings
25+
WHERE compress_relid IS NOT NULL AND index @> '[{"type": "firstlast"}]';
26+
27+
IF affected IS NOT NULL THEN
28+
RAISE EXCEPTION 'cannot upgrade because firstlast sparse indexes exist'
29+
USING
30+
DETAIL = format('The following chunks have firstlast sparse indexes: %s', affected),
31+
HINT = 'Recompress the chunks with firstlast indexes before upgrading.';
32+
END IF;
33+
END
34+
$$;
35+
1436
-- Rename legacy chunk-side constraints to the names the new code recomputes:
1537
-- FKs use the parent's name; unique/PK/exclusion/trigger use the deterministic
1638
-- "<chunk_id>_<parent>" form

sql/updates/reverse-dev.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11

2+
-- Warn about firstlast indexes when downgrading.
3+
DO $$
4+
DECLARE
5+
affected text;
6+
BEGIN
7+
SELECT string_agg(DISTINCT relid::text, ', ')
8+
INTO affected
9+
FROM _timescaledb_catalog.compression_settings
10+
WHERE index @> '[{"type": "firstlast"}]';
11+
12+
IF affected IS NOT NULL THEN
13+
RAISE WARNING 'Before upgrading again you have to recompress chunks with firstlast indexes.'
14+
USING
15+
DETAIL = format('The following chunk use firstlast sparse indexes: %s', affected);
16+
END IF;
17+
END
18+
$$;
19+
220
DROP VIEW IF EXISTS _timescaledb_catalog.chunk_constraint;
321
DROP FUNCTION IF EXISTS _timescaledb_functions.chunk_constraint_add_table_constraint( integer, name, name);
422

0 commit comments

Comments
 (0)