mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-25 15:50:27 +08:00
Deparse a table into a set of SQL commands that can be used to reconstruct it. Together with column definiton it deparses constraints, indexes, triggers and rules as well. There are some table types that are not supported: temporary, partitioned, foreign, inherited and a table that uses options. Row security is also not supported.
25 lines
844 B
SQL
25 lines
844 B
SQL
-- This file and its contents are licensed under the Timescale License.
|
|
-- Please see the included NOTICE for copyright information and
|
|
-- LICENSE-TIMESCALE for a copy of the license.
|
|
|
|
-- deparse each table%, drop it and recreate it using deparse result
|
|
|
|
DO
|
|
$$DECLARE
|
|
tables CURSOR FOR
|
|
SELECT tablename, schemaname
|
|
FROM pg_tables
|
|
WHERE tablename LIKE 'table%' AND (schemaname = 'public' OR schemaname = 'myschema')
|
|
ORDER BY tablename;
|
|
deparse_stmt text;
|
|
tablename text;
|
|
BEGIN
|
|
FOR table_record IN tables
|
|
LOOP
|
|
tablename := format('%I.%I', table_record.schemaname, table_record.tablename);
|
|
EXECUTE format('SELECT _timescaledb_internal.get_tabledef(%L)', tablename) INTO deparse_stmt;
|
|
EXECUTE 'DROP TABLE ' || tablename;
|
|
EXECUTE deparse_stmt;
|
|
END LOOP;
|
|
END$$;
|