diff --git a/.unreleased/pr_9967 b/.unreleased/pr_9967 new file mode 100644 index 00000000000..1bcf34f1238 --- /dev/null +++ b/.unreleased/pr_9967 @@ -0,0 +1 @@ +Fixes: #9967 Block upgrade after downgrade with firstlast indexes present diff --git a/sql/updates/latest-dev.sql b/sql/updates/latest-dev.sql index abbd7c53d0f..22ae0dd279c 100644 --- a/sql/updates/latest-dev.sql +++ b/sql/updates/latest-dev.sql @@ -11,6 +11,28 @@ BEGIN END; $$; +-- Block the update if any firstlast sparse indexes exist. This could only +-- happen when downgrading and then upgrading again. Under this circumstance +-- the firstlast sparse index might not be consistent for any chunks that +-- were recompressed while the extension was downgraded. +DO $$ +DECLARE + affected text; +BEGIN + SELECT string_agg(DISTINCT relid::text, ', ') + INTO affected + FROM _timescaledb_catalog.compression_settings + WHERE compress_relid IS NOT NULL AND index @> '[{"type": "firstlast"}]'; + + IF affected IS NOT NULL THEN + RAISE EXCEPTION 'cannot upgrade because firstlast sparse indexes exist' + USING + DETAIL = format('The following chunks have firstlast sparse indexes: %s', affected), + HINT = 'Recompress the chunks with firstlast indexes before upgrading.'; + END IF; +END +$$; + -- Rename legacy chunk-side constraints to the names the new code recomputes: -- FKs use the parent's name; unique/PK/exclusion/trigger use the deterministic -- "_" form diff --git a/sql/updates/reverse-dev.sql b/sql/updates/reverse-dev.sql index 1457b3652bc..42a9b3de89e 100644 --- a/sql/updates/reverse-dev.sql +++ b/sql/updates/reverse-dev.sql @@ -1,4 +1,22 @@ +-- Warn about firstlast indexes when downgrading. +DO $$ +DECLARE + affected text; +BEGIN + SELECT string_agg(DISTINCT relid::text, ', ') + INTO affected + FROM _timescaledb_catalog.compression_settings + WHERE index @> '[{"type": "firstlast"}]'; + + IF affected IS NOT NULL THEN + RAISE WARNING 'Before upgrading again you have to recompress chunks with firstlast indexes.' + USING + DETAIL = format('The following chunk use firstlast sparse indexes: %s', affected); + END IF; +END +$$; + DROP VIEW IF EXISTS _timescaledb_catalog.chunk_constraint; DROP FUNCTION IF EXISTS _timescaledb_functions.chunk_constraint_add_table_constraint( integer, name, name);