mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-15 18:13:18 +08:00
When restoring a database, people would encounter errors if the restore happened after telemetry has run. This is because a 'exported_uuid' field would then exist and people would encounter a "duplicate key value" when the restore tried to overwrite it. We fix this by moving this metadata to a different key in pre_restore and trying to move it back in post_restore. If the restore create an exported_uuid, that restored value is used and the moved version is simply deleted We also remove the error redirection in restore so that errors will show up in tests in the future. Fixes #1409.
42 lines
1.4 KiB
PL/PgSQL
42 lines
1.4 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 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_internal.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;
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION timescaledb_post_restore() RETURNS BOOL AS
|
|
$BODY$
|
|
DECLARE
|
|
db text;
|
|
BEGIN
|
|
SELECT current_database() INTO db;
|
|
EXECUTE format($$ALTER DATABASE %I SET timescaledb.restoring ='off'$$, db);
|
|
SET SESSION timescaledb.restoring='off';
|
|
PERFORM _timescaledb_internal.start_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;
|