From 340bebc57ac9e18d8c6a52b829002b4bf6091718 Mon Sep 17 00:00:00 2001 From: Vitaly Burovoy Date: Mon, 3 Jul 2023 20:00:13 +0000 Subject: [PATCH] Use sql/tables/*.sql for an update script [5/5] Use table creation DDL in generated upgrade script. Only empty lines, comments and unnecessary type cast ("::regclass") are changed in the reference file: updates/pg_partman--4.7.3--5.0.0-beta.sql --- Makefile | 4 + sql/_for_update/01.tables.sql | 192 ---------------------- sql/_for_update/04.tables-copy-back.sql | 98 +++++++++++ updates/pg_partman--4.7.3--5.0.0-beta.sql | 12 +- 4 files changed, 107 insertions(+), 199 deletions(-) create mode 100644 sql/_for_update/04.tables-copy-back.sql diff --git a/Makefile b/Makefile index 37abb15..9255c8f 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ sql/$(EXTENSION)--$(EXTVERSION).sql: | $(wildcard sql/types/*.sql sql/tables/*.s FUNCS_DIR := sql/functions PROCS_DIR := sql/procedures +TABLS_DIR := sql/tables INTRM_DIR := sql/_for_update NEW_FUNCS := calculate_time_partition_info.sql @@ -94,6 +95,9 @@ PROC_ALL_FILES := $(sort \ # TEMPORARY: hard to read; required to check existing migration is not touched; later will be simplified. updates/$(EXTENSION)--$(PREV_VERSION)--$(EXTVERSION).sql: | \ $(INTRM_DIR)/01.tables.sql \ + $(TABLS_DIR)/part_config.sql \ + $(TABLS_DIR)/part_config_sub.sql \ + $(INTRM_DIR)/04.tables-copy-back.sql \ $(INTRM_DIR)/08.hdr_new_functions.sql \ $(addprefix $(FUNCS_DIR)/,$(NEW_FUNCS)) \ $(INTRM_DIR)/08.hdr_altr_functions.sql \ diff --git a/sql/_for_update/01.tables.sql b/sql/_for_update/01.tables.sql index 80c350d..bf68b3e 100644 --- a/sql/_for_update/01.tables.sql +++ b/sql/_for_update/01.tables.sql @@ -204,195 +204,3 @@ BEGIN RETURN v_result; END $$; - -CREATE TABLE @extschema@.part_config ( - parent_table text NOT NULL - , control text NOT NULL - , partition_interval text NOT NULL - , partition_type text NOT NULL - , premake int NOT NULL DEFAULT 4 - , automatic_maintenance text NOT NULL DEFAULT 'on' - , template_table text - , retention text - , retention_schema text - , retention_keep_index boolean NOT NULL DEFAULT true - , retention_keep_table boolean NOT NULL DEFAULT true - , epoch text NOT NULL DEFAULT 'none' - , constraint_cols text[] - , optimize_constraint int NOT NULL DEFAULT 30 - , infinite_time_partitions boolean NOT NULL DEFAULT false - , datetime_string text - , jobmon boolean NOT NULL DEFAULT true - , sub_partition_set_full boolean NOT NULL DEFAULT false - , undo_in_progress boolean NOT NULL DEFAULT false - , inherit_privileges boolean DEFAULT false - , constraint_valid boolean DEFAULT true NOT NULL - , subscription_refresh text - , ignore_default_data boolean NOT NULL DEFAULT true - , default_table boolean DEFAULT true - , date_trunc_interval text - , CONSTRAINT part_config_parent_table_pkey PRIMARY KEY (parent_table) - , CONSTRAINT positive_premake_check CHECK (premake > 0) -); - -CREATE INDEX part_config_type_idx ON @extschema@.part_config (partition_type); -SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config'::regclass, ''); - -ALTER TABLE @extschema@.part_config ADD CONSTRAINT control_constraint_col_chk CHECK ((constraint_cols @> ARRAY[control]) <> true); -ALTER TABLE @extschema@.part_config ADD CONSTRAINT retention_schema_not_empty_chk CHECK (retention_schema <> ''); - -ALTER TABLE @extschema@.part_config -ADD CONSTRAINT part_config_automatic_maintenance_check -CHECK (@extschema@.check_automatic_maintenance_value(automatic_maintenance)); - -ALTER TABLE @extschema@.part_config -ADD CONSTRAINT part_config_epoch_check -CHECK (@extschema@.check_epoch_type(epoch)); - -ALTER TABLE @extschema@.part_config -ADD CONSTRAINT part_config_type_check -CHECK (@extschema@.check_partition_type(partition_type)); - -CREATE TABLE @extschema@.part_config_sub ( - sub_parent text - , sub_control text NOT NULL - , sub_partition_interval text NOT NULL - , sub_partition_type text NOT NULL - , sub_premake int NOT NULL DEFAULT 4 - , sub_automatic_maintenance text NOT NULL DEFAULT 'on' - , sub_template_table text - , sub_retention text - , sub_retention_schema text - , sub_retention_keep_index boolean NOT NULL DEFAULT true - , sub_retention_keep_table boolean NOT NULL DEFAULT true - , sub_epoch text NOT NULL DEFAULT 'none' - , sub_constraint_cols text[] - , sub_optimize_constraint int NOT NULL DEFAULT 30 - , sub_infinite_time_partitions boolean NOT NULL DEFAULT false - , sub_jobmon boolean NOT NULL DEFAULT true - , sub_inherit_privileges boolean DEFAULT false - , sub_constraint_valid boolean DEFAULT true NOT NULL - , sub_subscription_refresh text - , sub_ignore_default_data boolean NOT NULL DEFAULT true - , sub_default_table boolean default true - , sub_date_trunc_interval TEXT - , CONSTRAINT part_config_sub_pkey PRIMARY KEY (sub_parent) - , CONSTRAINT part_config_sub_sub_parent_fkey FOREIGN KEY (sub_parent) REFERENCES @extschema@.part_config (parent_table) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED - , CONSTRAINT positive_premake_check CHECK (sub_premake > 0) -); -SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config_sub'::regclass, ''); - -ALTER TABLE @extschema@.part_config_sub ADD CONSTRAINT control_constraint_col_chk CHECK ((sub_constraint_cols @> ARRAY[sub_control]) <> true); - -ALTER TABLE @extschema@.part_config_sub ADD CONSTRAINT retention_schema_not_empty_chk CHECK (sub_retention_schema <> ''); - -ALTER TABLE @extschema@.part_config_sub -ADD CONSTRAINT part_config_sub_automatic_maintenance_check -CHECK (@extschema@.check_automatic_maintenance_value(sub_automatic_maintenance)); - -ALTER TABLE @extschema@.part_config_sub -ADD CONSTRAINT part_config_sub_epoch_check -CHECK (@extschema@.check_epoch_type(sub_epoch)); - -ALTER TABLE @extschema@.part_config_sub -ADD CONSTRAINT part_config_sub_type_check -CHECK (@extschema@.check_partition_type(sub_partition_type)); - -INSERT INTO @extschema@.part_config ( - parent_table - , control - , partition_interval - , partition_type - , premake - , automatic_maintenance - , template_table - , retention - , retention_schema - , retention_keep_index - , retention_keep_table - , epoch - , constraint_cols - , optimize_constraint - , infinite_time_partitions - , datetime_string - , jobmon - , sub_partition_set_full - , undo_in_progress - , inherit_privileges - , constraint_valid - , subscription_refresh - , ignore_default_data -) -SELECT - parent_table - , control - , partition_interval - , CASE WHEN partition_type = 'native' THEN 'range' ELSE partition_type END - , premake - , automatic_maintenance - , template_table - , retention - , retention_schema - , retention_keep_index - , retention_keep_table - , epoch - , constraint_cols - , optimize_constraint - , infinite_time_partitions - , datetime_string - , jobmon - , sub_partition_set_full - , undo_in_progress - , inherit_privileges - , constraint_valid - , subscription_refresh - , ignore_default_data -FROM @extschema@.part_config_pre_500_data; - - -INSERT INTO @extschema@.part_config_sub ( - sub_parent - , sub_control - , sub_partition_interval - , sub_partition_type - , sub_premake - , sub_automatic_maintenance - , sub_template_table - , sub_retention - , sub_retention_schema - , sub_retention_keep_index - , sub_retention_keep_table - , sub_epoch - , sub_constraint_cols - , sub_optimize_constraint - , sub_infinite_time_partitions - , sub_jobmon - , sub_inherit_privileges - , sub_constraint_valid - , sub_subscription_refresh - , sub_date_trunc_interval - , sub_ignore_default_data -) -SELECT - sub_parent - , sub_control - , sub_partition_interval - , CASE WHEN sub_partition_type = 'native' THEN 'range' ELSE sub_partition_type END - , sub_premake - , sub_automatic_maintenance - , sub_template_table - , sub_retention - , sub_retention_schema - , sub_retention_keep_index - , sub_retention_keep_table - , sub_epoch - , sub_constraint_cols - , sub_optimize_constraint - , sub_infinite_time_partitions - , sub_jobmon - , sub_inherit_privileges - , sub_constraint_valid - , sub_subscription_refresh - , sub_date_trunc_interval - , sub_ignore_default_data -FROM @extschema@.part_config_sub_pre_500_data; diff --git a/sql/_for_update/04.tables-copy-back.sql b/sql/_for_update/04.tables-copy-back.sql new file mode 100644 index 0000000..52722a4 --- /dev/null +++ b/sql/_for_update/04.tables-copy-back.sql @@ -0,0 +1,98 @@ +INSERT INTO @extschema@.part_config ( + parent_table + , control + , partition_interval + , partition_type + , premake + , automatic_maintenance + , template_table + , retention + , retention_schema + , retention_keep_index + , retention_keep_table + , epoch + , constraint_cols + , optimize_constraint + , infinite_time_partitions + , datetime_string + , jobmon + , sub_partition_set_full + , undo_in_progress + , inherit_privileges + , constraint_valid + , subscription_refresh + , ignore_default_data +) +SELECT + parent_table + , control + , partition_interval + , CASE WHEN partition_type = 'native' THEN 'range' ELSE partition_type END + , premake + , automatic_maintenance + , template_table + , retention + , retention_schema + , retention_keep_index + , retention_keep_table + , epoch + , constraint_cols + , optimize_constraint + , infinite_time_partitions + , datetime_string + , jobmon + , sub_partition_set_full + , undo_in_progress + , inherit_privileges + , constraint_valid + , subscription_refresh + , ignore_default_data +FROM @extschema@.part_config_pre_500_data; + + +INSERT INTO @extschema@.part_config_sub ( + sub_parent + , sub_control + , sub_partition_interval + , sub_partition_type + , sub_premake + , sub_automatic_maintenance + , sub_template_table + , sub_retention + , sub_retention_schema + , sub_retention_keep_index + , sub_retention_keep_table + , sub_epoch + , sub_constraint_cols + , sub_optimize_constraint + , sub_infinite_time_partitions + , sub_jobmon + , sub_inherit_privileges + , sub_constraint_valid + , sub_subscription_refresh + , sub_date_trunc_interval + , sub_ignore_default_data +) +SELECT + sub_parent + , sub_control + , sub_partition_interval + , CASE WHEN sub_partition_type = 'native' THEN 'range' ELSE sub_partition_type END + , sub_premake + , sub_automatic_maintenance + , sub_template_table + , sub_retention + , sub_retention_schema + , sub_retention_keep_index + , sub_retention_keep_table + , sub_epoch + , sub_constraint_cols + , sub_optimize_constraint + , sub_infinite_time_partitions + , sub_jobmon + , sub_inherit_privileges + , sub_constraint_valid + , sub_subscription_refresh + , sub_date_trunc_interval + , sub_ignore_default_data +FROM @extschema@.part_config_sub_pre_500_data; diff --git a/updates/pg_partman--4.7.3--5.0.0-beta.sql b/updates/pg_partman--4.7.3--5.0.0-beta.sql index 8f41632..15c2822 100644 --- a/updates/pg_partman--4.7.3--5.0.0-beta.sql +++ b/updates/pg_partman--4.7.3--5.0.0-beta.sql @@ -204,7 +204,6 @@ BEGIN RETURN v_result; END $$; - CREATE TABLE @extschema@.part_config ( parent_table text NOT NULL , control text NOT NULL @@ -234,10 +233,10 @@ CREATE TABLE @extschema@.part_config ( , CONSTRAINT part_config_parent_table_pkey PRIMARY KEY (parent_table) , CONSTRAINT positive_premake_check CHECK (premake > 0) ); - CREATE INDEX part_config_type_idx ON @extschema@.part_config (partition_type); -SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config'::regclass, ''); +SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config', ''); +-- Ensure the control column cannot be one of the additional constraint columns. ALTER TABLE @extschema@.part_config ADD CONSTRAINT control_constraint_col_chk CHECK ((constraint_cols @> ARRAY[control]) <> true); ALTER TABLE @extschema@.part_config ADD CONSTRAINT retention_schema_not_empty_chk CHECK (retention_schema <> ''); @@ -252,7 +251,7 @@ CHECK (@extschema@.check_epoch_type(epoch)); ALTER TABLE @extschema@.part_config ADD CONSTRAINT part_config_type_check CHECK (@extschema@.check_partition_type(partition_type)); - +-- FK set deferrable because create_parent() & create_sub_parent() inserts to this table before part_config CREATE TABLE @extschema@.part_config_sub ( sub_parent text , sub_control text NOT NULL @@ -280,10 +279,10 @@ CREATE TABLE @extschema@.part_config_sub ( , CONSTRAINT part_config_sub_sub_parent_fkey FOREIGN KEY (sub_parent) REFERENCES @extschema@.part_config (parent_table) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED , CONSTRAINT positive_premake_check CHECK (sub_premake > 0) ); -SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config_sub'::regclass, ''); +SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config_sub', ''); +-- Ensure the control column cannot be one of the additional constraint columns. ALTER TABLE @extschema@.part_config_sub ADD CONSTRAINT control_constraint_col_chk CHECK ((sub_constraint_cols @> ARRAY[sub_control]) <> true); - ALTER TABLE @extschema@.part_config_sub ADD CONSTRAINT retention_schema_not_empty_chk CHECK (sub_retention_schema <> ''); ALTER TABLE @extschema@.part_config_sub @@ -297,7 +296,6 @@ CHECK (@extschema@.check_epoch_type(sub_epoch)); ALTER TABLE @extschema@.part_config_sub ADD CONSTRAINT part_config_sub_type_check CHECK (@extschema@.check_partition_type(sub_partition_type)); - INSERT INTO @extschema@.part_config ( parent_table , control