timescaledb/sql/restoring.sql
Sven Klemm a664e685cd Keep track of catalog version
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.
2023-10-14 22:28:21 +02:00

53 lines
2.0 KiB
PL/PgSQL

-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
CREATE OR REPLACE FUNCTION @extschema@.timescaledb_pre_restore() RETURNS BOOL AS
$BODY$
DECLARE
db text;
BEGIN
SELECT current_database() INTO db;
EXECUTE format($$ALTER DATABASE %I SET timescaledb.restoring ='on'$$, db);
SET SESSION timescaledb.restoring = 'on';
PERFORM _timescaledb_functions.stop_background_workers();
--exported uuid may be included in the dump so backup the version
UPDATE _timescaledb_catalog.metadata SET key='exported_uuid_bak' WHERE key='exported_uuid';
RETURN true;
END
$BODY$
LANGUAGE PLPGSQL SET search_path TO pg_catalog, pg_temp;
CREATE OR REPLACE FUNCTION @extschema@.timescaledb_post_restore() RETURNS BOOL AS
$BODY$
DECLARE
db text;
catalog_version text;
BEGIN
SELECT m.value INTO catalog_version FROM pg_extension x
JOIN _timescaledb_catalog.metadata m ON m.key='timescaledb_version'
WHERE x.extname='timescaledb' AND x.extversion <> m.value;
-- check that a loaded dump is compatible with the currently running code
IF FOUND THEN
RAISE EXCEPTION 'catalog version mismatch, expected "%" seen "%"', '@PROJECT_VERSION_MOD@', catalog_version;
END IF;
SELECT current_database() INTO db;
EXECUTE format($$ALTER DATABASE %I RESET timescaledb.restoring $$, db);
-- we cannot use reset here because the reset_val might not be off
SET timescaledb.restoring TO off;
PERFORM _timescaledb_functions.restart_background_workers();
--try to restore the backed up uuid, if the restore did not set one
INSERT INTO _timescaledb_catalog.metadata
SELECT 'exported_uuid', value, include_in_telemetry FROM _timescaledb_catalog.metadata WHERE key='exported_uuid_bak'
ON CONFLICT DO NOTHING;
DELETE FROM _timescaledb_catalog.metadata WHERE key='exported_uuid_bak';
RETURN true;
END
$BODY$
LANGUAGE PLPGSQL SET search_path TO pg_catalog, pg_temp;