Skip to content

Commit

Permalink
Keep track of catalog version
Browse files Browse the repository at this point in the history
This patch stores the current catalog version in an internal
table to allow us to verify catalog and code version match.
When doing dump/restore people occasionally report very unusual
errors and during investigation it is discovered that they loaded
a dump from an older version and run it with a later code version.
This allows to detect mismatches between installed code version
and loaded dump version. The version number in the metadata table
will be kept updated in upgrade and downgrade scripts.
  • Loading branch information
svenklemm committed Oct 11, 2023
1 parent 6304c7a commit 72b0895
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sql/metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ INSERT INTO _timescaledb_catalog.metadata
SELECT 'uuid', _timescaledb_functions.generate_uuid(), TRUE ON CONFLICT DO NOTHING;
INSERT INTO _timescaledb_catalog.metadata
SELECT 'install_timestamp', now(), TRUE ON CONFLICT DO NOTHING;

-- Install catalog version on database installation and upgrade.
-- This allows us to detect catalog mismatches in dump/restore cycle.
INSERT INTO _timescaledb_catalog.metadata
SELECT 'timescaledb_version', '@PROJECT_VERSION_MOD@', FALSE ON CONFLICT (key) DO UPDATE SET value = excluded.VALUE;

10 changes: 10 additions & 0 deletions sql/updates/pre-update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ DROP TRIGGER IF EXISTS "0_cache_inval" ON _timescaledb_catalog.chunk_constraint;
DROP TRIGGER IF EXISTS "0_cache_inval" ON _timescaledb_catalog.dimension_slice;
DROP TRIGGER IF EXISTS "0_cache_inval" ON _timescaledb_catalog.dimension;

DO $$
DECLARE
catalog_version TEXT;
BEGIN
IF EXISTS(SELECT FROM _timescaledb_catalog.metadata WHERE key='timescaledb_version' AND value <> '@UPDATE_FROM_VERSION@') THEN
SELECT value INTO catalog_version FROM _timescaledb_catalog.metadata WHERE key='timescaledb_version';
RAISE EXCEPTION 'catalog version mismatch, expected "%" seen "%"', '@UPDATE_FROM_VERSION@', catalog_version;
END IF;
END$$;

-- Since we want to call the new version of restart_background_workers we
-- create a function that points to that version. The proper restart_background_workers
-- may either be in _timescaledb_internal or in _timescaledb_functions
Expand Down

0 comments on commit 72b0895

Please sign in to comment.