Best practices for performing DDL operations on dependency views
This topic describes the limitations that you may encounter when you modify base tables and views that are depended on by views in AnalyticDB for PostgreSQL, analyzes the impact of such modifications on dependency views, and describes how to quickly and safely modify a base table structure or a view.
Usage notes
This feature is supported only by AnalyticDB for PostgreSQL Enterprise Edition instances.
All views involved in this topic are dependency views that depend on base tables.
View the dependency relationships between base tables and views
To view the dependency relationships between base tables and views, you must create a function for querying dependency information. This section uses the base tables and views shown in the following figure as an example to describe how to view the dependency relationships between base tables and views.
Run the following SQL statements to create the base tables and dependency views in the preceding figure.
CREATE TABLE cities(id INT, name TEXT, info TEXT); CREATE TABLE products(id INT, info TEXT); CREATE TABLE sales(product_id INT, city_id INT, money NUMERIC(10,2)); CREATE VIEW all_info AS SELECT * FROM cities c JOIN sales s ON c.id = s.city_id; CREATE VIEW ps_info AS SELECT * FROM products p JOIN sales s ON p.id = s.product_id; CREATE VIEW psc_info AS SELECT * FROM all_info a JOIN ps_info p ON p.product_id = s.product_id;Run the following SQL statements to create a function for querying dependency information.
CREATE OR REPLACE FUNCTION get_table_dependencies(input_schema TEXT, input_table TEXT) RETURNS TABLE( depth INT, dependent_schema TEXT, dependent_view TEXT, dependent_oid OID, source_schema TEXT, source_table TEXT ) AS $$ BEGIN RETURN QUERY WITH RECURSIVE find_views AS ( SELECT DISTINCT n.nspname::TEXT as dependent_schema, -- Ensure that the column is of the TEXT type cl.relname::TEXT as dependent_view, -- Convert to the TEXT type cl.oid as dependent_oid, -- Convert the OID to TEXT nb.nspname::TEXT as source_schema, -- Convert to the TEXT type c.relname::TEXT as source_table, -- Convert to the TEXT type 1 as depth FROM pg_depend d JOIN pg_rewrite r ON d.objid = r.oid JOIN pg_class cl ON r.ev_class = cl.oid JOIN pg_class c ON d.refobjid = c.oid JOIN pg_namespace n ON cl.relnamespace = n.oid JOIN pg_namespace nb ON c.relnamespace = nb.oid WHERE d.deptype = 'n' AND d.classid='pg_rewrite'::regclass AND cl.relkind IN ('v', 'm') AND nb.nspname = input_schema AND c.relname = input_table UNION ALL SELECT n.nspname::TEXT, cl.relname::TEXT, cl.oid, nb.nspname::TEXT, c.relname::TEXT, fv.depth + 1 FROM pg_depend d JOIN pg_rewrite r ON d.objid = r.oid JOIN pg_class cl ON r.ev_class = cl.oid JOIN pg_class c ON d.refobjid = c.oid JOIN pg_namespace n ON cl.relnamespace = n.oid JOIN pg_namespace nb ON c.relnamespace = nb.oid JOIN find_views fv ON fv.dependent_oid = c.oid WHERE d.deptype = 'n' AND d.classid='pg_rewrite'::regclass AND cl.relkind IN ('v', 'm') AND cl.oid <> c.oid ) SELECT DISTINCT fv.depth, fv.dependent_schema, fv.dependent_view, fv.dependent_oid, fv.source_schema, fv.source_table FROM find_views fv ORDER BY fv.depth; END; $$ LANGUAGE plpgsql;The parameters are described as follows:
Parameter
Description
depth
The dependency level on the intermediate view or base table. The depth of a view that directly depends on the base table is 1.
dependent_schema
The schema where the queried dependency view resides.
dependent_view
The name of the queried dependency view.
dependent_oid
The OID of the dependency view.
source_schema
The schema where the depended-on table or view resides.
source_table
The name of the depended-on table or dependency view.
Run the following SQL statement to query the relationship between the base table and the dependency views.
AS SELECT * FROM get_table_dependencies('public', 'sales'); depth | dependent_schema | dependent_view | dependent_oid | source_schema | source_table -------+------------------+----------------+---------------+---------------+-------------- 1 | public | all_info | 26635 | public | sales 1 | public | ps_info | 26644 | public | sales 2 | public | psc_info | 26648 | public | all_info 2 | public | psc_info | 26648 | public | ps_info (4 ROWS)
Limitations on modifying base tables or views
Limitations on modifying base tables
If you modify the data type of a referenced column in a base table, the data type of the view cannot be identified.
When a dependency view is created, the type of each column in the view is recorded in the system table. Therefore, if you modify the data type of a referenced column in the base table, the data type of the corresponding column in the dependency view cannot be identified. The base table fields that are not referenced are not affected.
If you modify a referenced column in the base table, the view column name does not change.
When a dependency view is created, the system table stores the sequence number dependency of the base table column name. Modifying the base table column name does not affect dependency determination. Therefore, after you change the base table column name, the column name of the dependency view does not change accordingly, because the sequence number relationship of the original column name has been recorded in the system table.
If you drop a referenced column in the base table, the view is dropped.
If you use the CASCADE option, the dependency views that reference the column are dropped together.
For more base table DDL limitations, see the following table.
ALTER DDL | Impact | Remarks |
RENAME | No impact | When you query the definition of the dependency view, the rename of the depended-on base table or intermediate view is updated accordingly. |
RENAME COLUMN | No impact | When you query the definition of the dependency view, the corresponding SELECT column is updated, but the column name of the view itself is not modified. |
RENAME CONSTRAINT | No impact | Renames a constraint. |
SET SCHEMA | No impact | When you query the definition of the dependency view, the SCHEMA information is updated automatically. |
ATTACH PARTITION | No impact | None |
DETACH PARTITION | No impact | None |
DROP COLUMN referenced column | Has impact | Cascading deletion directly drops the view. |
ALTER COLUMN TYPE | Has impact | Restricted at the syntax level. |
ALTER COLUMN SET DEFAULT | No impact | None |
ALTER COLUMN DROP DEFAULT | No impact | None |
ALTER COLUMN SET/DROP NOT NULL | No impact | None |
ALTER COLUMN ADD/SET/DROP GENERATED AS IDENTITY | No impact | Converts a column into an auto-increment column. |
ALTER COLUMN SET STATISTICS | No impact | None |
ALTER COLUMN RESET/SET ( attribute_option = value) | No impact | None |
ALTER COLUMN SET STORAGE | No impact | None |
ADD table_constraint | No impact | None |
DROP table_constraint | No impact | None |
ADD table_constraint_using_index | No impact | None |
ENABLE TRIGGER | No impact | None |
ENABLE RULE | No impact | None |
INHERIT | No impact | None |
REDACTION POLICY | No impact | Depends on the permissions of the final user who performs the query. |
Limitations on modifying views
Modifying a view is different from modifying a base table. The ALTER command for a view can be used only to modify the DEFAULT value of columns. The common modification method is to rebuild the view by using CREATE OR REPLACE.
It is prohibited to add or drop any column of a view.
Adding or dropping any column of a view is equivalent to updating the view definition. Even if the column is not referenced, the sequence number relationship between the columns and the base table also changes. This makes the dependency relationships recorded in the system table unavailable. Therefore, this behavior is prohibited.
Appending columns to a view is allowed.
You can use CREATE OR REPLACE to append columns to a view. Appended columns do not affect the order of existing columns, and the dependency views can still find the corresponding columns through the system table information. Therefore, this operation can be performed normally.
It is prohibited to drop the last column of a view.
Dropping the last column of a view is prohibited.
It is prohibited to modify the data type of a column in a view.
Modifying the field name of a view column is not allowed.
For more view modification limitations, see the following table.
Structure change | Impact | Remarks |
Add an intermediate column | Has impact | Adding a column by directly modifying the intermediate view definition is prohibited if another view depends on the intermediate view. |
Append a column | No impact | None |
Drop a column | Has impact | Dropping a column by directly modifying the intermediate view definition is prohibited if another view depends on the intermediate view. |
Modify the data type of a referenced column | Has impact | Fields that are not referenced are not affected. |
Modify a field name | Has impact | Modifying the field name of an intermediate view column is not allowed. |
Modify base tables or views
Modification procedure
Export the DDL statements of the related views (select one of the following methods as needed).
Modify the data type of column b in the public.test base table or intermediate view: run SELECT record_dependency_ddl('public','test','b') to export the DDL information of the related dependency views.
Drop the referenced column b from the public.test base table: run SELECT record_dependency_ddl ('public','test','b') to export the DDL information of the related dependency views.
Add a column to a non-final position of an intermediate view: obtain all dependency views related to the view, and run SELECT record_dependency_ddl('public','test','') to export the DDL information of the related dependency views.
Drop a column from an intermediate view: obtain all dependency views related to the view, and run SELECT record_dependency_ddl('public','test','') to export the DDL information of the related dependency views.
Modify the field name of a column in an intermediate view: obtain all dependency views related to the view, and run SELECT record_dependency_ddl('public','test','') to export the DDL information of the related dependency views.
DROP DDL and CREATE DDL: you can view the DROP DDL in the temp_drop_dependency_ddl temporary table and the CREATE DDL in the temp_create_dependency_ddl temporary table.
Run the record_dependency_ddl function: each time the record_dependency_ddl function is run, the temporary table information is refreshed. Temporary tables exist only in the current session and disappear after the connection is disconnected or the database is switched.
Run the DROP command.
Run SELECT drop_dependency_table() to execute the DROP DDL. If the DROP operation fails, view the temp_drop_dependency_ddl temporary table and manually execute the DDL statements.
Modify the base table data type or add columns.
Run the CREATE command (select one of the following methods as needed).
Modify the data type of column b in the public.test base table or intermediate view: run SELECT create_dependency_table() to execute the CREATE DDL. If the CREATE operation fails, view the temp_create_dependency_ddl temporary table and manually execute the DDL statements.
Drop the referenced column b from the public.test base table: view the temp_create_dependency_ddl temporary table, modify the DDL of the dependency views that reference column b, and manually execute the DDL statements.
Add a column to a non-final position of an intermediate view: run SELECT create_dependency_table() to execute the CREATE DDL. If the CREATE operation fails, view the temp_create_dependency_ddl temporary table and manually execute the DDL statements.
Drop a column from an intermediate view: if the column is not depended on by other views, run SELECT create_dependency_table() to execute the CREATE DDL. If the CREATE operation fails, view the temp_create_dependency_ddl temporary table and manually execute the DDL statements.
Drop a column from an intermediate view: if the column is depended on by other views, view the temp_create_dependency_ddl temporary table, modify the DDL information of the dependency views that reference the column, and manually execute the DDL statements.
Modify the field name of a column in an intermediate view: if the column is not depended on by other views, run SELECT create_dependency_table() to execute the CREATE DDL. If the CREATE operation fails, view the temp_create_dependency_ddl temporary table and manually execute the DDL statements.
Modify the field name of a column in an intermediate view: if the column is depended on by other views, view the temp_create_dependency_ddl temporary table, modify the DDL information of the dependency views that reference the column, and manually execute the DDL statements.
Example
This section uses the base tables and views shown in the following figure as an example to describe how to safely modify base tables and views.
Run the following SQL statements to create the base table and views.
CREATE TABLE test(a INT, b INT, c INT); CREATE VIEW v1 AS SELECT a, b FROM test; CREATE VIEW v2 AS SELECT b, c FROM test; CREATE VIEW v3 AS SELECT test.a, v2.c FROM test JOIN v2 ON test.b = v2.b; List OF relations Schema | Name | Type | Owner | Storage --------+------+-------+------------+--------- public | test | TABLE | adbpgadmin | heap public | v1 | VIEW | adbpgadmin | public | v2 | VIEW | adbpgadmin | public | v3 | VIEW | adbpgadmin | (4 ROWS)Create functions for modifying the base table or views. Before the base table or views are modified, these functions drop all dependency views and record the DDL statements of the dependency views. After the modification is complete, you can directly rebuild the dependency views. The specific functions and operations are as follows.
CREATE OR REPLACE FUNCTION public.record_dependency_ddl(schema_name TEXT, table_name TEXT, column_name TEXT) RETURNS VOID AS $$ DECLARE view_info RECORD; combine_ddl TEXT := ''; drop_ddl TEXT := '---- DROP VIEW ---' || E'\n'; drop_str TEXT; full_name TEXT; ddl_line TEXT; BEGIN CREATE TEMP TABLE IF NOT EXISTS temp_drop_dependency_ddl ( ddl_statement TEXT NOT NULL ); CREATE TEMP TABLE IF NOT EXISTS temp_create_dependency_ddl ( ddl_statement TEXT NOT NULL ); TRUNCATE temp_drop_dependency_ddl; TRUNCATE temp_create_dependency_ddl; IF column_name != '' THEN FOR view_info IN WITH RECURSIVE find_views AS ( SELECT DISTINCT n.nspname AS dependent_schema, cl.relname AS dependent_view, cl.oid AS dependent_oid, cl.relkind AS dependent_type, nb.nspname AS source_schema, c.relname AS source_table, 1 AS depth FROM pg_depend d JOIN pg_rewrite r ON d.objid = r.oid JOIN pg_class cl ON r.ev_class = cl.oid JOIN pg_class c ON d.refobjid = c.oid JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = d.refobjsubid JOIN pg_namespace n ON cl.relnamespace = n.oid JOIN pg_namespace nb ON c.relnamespace = nb.oid WHERE d.deptype = 'n' AND d.classid='pg_rewrite'::regclass AND (cl.relkind = 'v' OR cl.relkind = 'm') -- 'v' means the source object is a view AND nb.nspname = schema_name -- schema of the table AND c.relname = table_name -- name of the table AND a.attname = column_name -- name of the column UNION ALL SELECT n.nspname, cl.relname, cl.oid, cl.relkind, nb.nspname, c.relname, fv.depth + 1 FROM pg_depend d JOIN pg_rewrite r ON d.objid = r.oid JOIN pg_class cl ON r.ev_class = cl.oid JOIN pg_class c ON d.refobjid = c.oid JOIN pg_namespace n ON cl.relnamespace = n.oid JOIN pg_namespace nb ON c.relnamespace = nb.oid JOIN find_views fv ON fv.dependent_oid = c.oid WHERE d.deptype = 'n' AND d.classid='pg_rewrite'::regclass AND (cl.relkind = 'v' OR cl.relkind = 'm') -- 'v' means the source object is a view AND cl.oid <> c.oid ) SELECT DISTINCT depth, dependent_schema, dependent_view, dependent_type FROM find_views ORDER BY depth LOOP full_name := view_info.dependent_schema || '.' || view_info.dependent_view; FOR ddl_line IN SELECT dump_table_ddl(full_name) LOOP IF ddl_line LIKE 'CREATE MATERIALIZED VIEW%' THEN ddl_line := REPLACE(ddl_line, 'VIEW', 'VIEW IF NOT EXISTS'); ELSIF ddl_line LIKE 'CREATE VIEW%' THEN ddl_line := REPLACE(ddl_line, 'CREATE VIEW', 'CREATE OR REPLACE VIEW'); END IF; combine_ddl := combine_ddl || ddl_line || E'\n'; END LOOP; IF view_info.depth = 1 THEN drop_str := 'DROP VIEW '; IF view_info.dependent_type = 'm' THEN drop_str := 'DROP MATERIALIZED VIEW '; END IF; drop_ddl := drop_ddl || drop_str || 'IF EXISTS ' || full_name || ' CASCADE;' || E'\n'; END IF; END LOOP; ELSE FOR view_info IN WITH RECURSIVE find_views AS ( SELECT DISTINCT n.nspname AS dependent_schema, cl.relname AS dependent_view, cl.oid AS dependent_oid, cl.relkind AS dependent_type, nb.nspname AS source_schema, c.relname AS source_table, 1 AS depth FROM pg_depend d JOIN pg_rewrite r ON d.objid = r.oid JOIN pg_class cl ON r.ev_class = cl.oid JOIN pg_class c ON d.refobjid = c.oid JOIN pg_namespace n ON cl.relnamespace = n.oid JOIN pg_namespace nb ON c.relnamespace = nb.oid WHERE d.deptype = 'n' AND d.classid='pg_rewrite'::regclass AND (cl.relkind = 'v' OR cl.relkind = 'm') -- 'v' means the source object is a view AND nb.nspname = schema_name -- schema of the table AND c.relname = table_name -- name of the table UNION ALL SELECT n.nspname, cl.relname, cl.oid, cl.relkind, nb.nspname, c.relname, fv.depth + 1 FROM pg_depend d JOIN pg_rewrite r ON d.objid = r.oid JOIN pg_class cl ON r.ev_class = cl.oid JOIN pg_class c ON d.refobjid = c.oid JOIN pg_namespace n ON cl.relnamespace = n.oid JOIN pg_namespace nb ON c.relnamespace = nb.oid JOIN find_views fv ON fv.dependent_oid = c.oid WHERE d.deptype = 'n' AND d.classid='pg_rewrite'::regclass AND (cl.relkind = 'v' OR cl.relkind = 'm') -- 'v' means the source object is a view AND cl.oid <> c.oid ) SELECT DISTINCT depth, dependent_schema, dependent_view, dependent_type FROM find_views ORDER BY depth LOOP full_name := view_info.dependent_schema || '.' || view_info.dependent_view; FOR ddl_line IN SELECT dump_table_ddl(full_name) LOOP IF ddl_line LIKE 'CREATE MATERIALIZED VIEW%' THEN ddl_line := REPLACE(ddl_line, 'VIEW', 'VIEW IF NOT EXISTS'); ELSIF ddl_line LIKE 'CREATE VIEW%' THEN ddl_line := REPLACE(ddl_line, 'CREATE VIEW', 'CREATE OR REPLACE VIEW'); END IF; combine_ddl := combine_ddl || ddl_line || E'\n'; END LOOP; IF view_info.depth = 1 THEN drop_str := 'DROP VIEW '; IF view_info.dependent_type = 'm' THEN drop_str := 'DROP MATERIALIZED VIEW '; END IF; drop_ddl := drop_ddl || drop_str || 'IF EXISTS ' || full_name || ' CASCADE;' || E'\n'; END IF; END LOOP; END IF; INSERT INTO temp_drop_dependency_ddl VALUES (drop_ddl); INSERT INTO temp_create_dependency_ddl VALUES (combine_ddl); END; $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION public.drop_dependency_table() RETURNS VOID AS $$ DECLARE ddl_text TEXT; BEGIN FOR ddl_text IN SELECT ddl_statement FROM temp_drop_dependency_ddl LOOP EXECUTE ddl_text; END LOOP; END; $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION public.create_dependency_table() RETURNS VOID AS $$ DECLARE ddl_text TEXT; BEGIN FOR ddl_text IN SELECT ddl_statement FROM temp_create_dependency_ddl LOOP EXECUTE ddl_text; END LOOP; SET search_path TO PUBLIC; END; $$ LANGUAGE plpgsql;NoteThese functions can be used only when the dependency view definitions do not change. If you also need to modify the dependency views, you must manually modify and rebuild the views based on the exported DDL statements.
Run the record_dependency_ddl function to record the DROP command information and CREATE command information of the dependency views. These commands are stored in the temp_drop_dependency_ddl and temp_create_dependency_ddl temporary tables respectively. The following example queries the views that depend on column b of the public.test table.
SELECT record_dependency_ddl('public','test','b'); NOTICE: TABLE doesn't have 'DISTRIBUTED BY' clause -- Using column named 'ddl_statement' as the Greenplum Database data distribution key for this table. HINT: The 'DISTRIBUTED BY' clause determines the distribution OF data. Make sure COLUMN(s) chosen ARE the optimal data distribution key TO minimize skew. NOTICE: TABLE doesn't have 'DISTRIBUTED BY' clause -- Using column named 'ddl_statement' as the Greenplum Database data distribution key for this table. HINT: The 'DISTRIBUTED BY' clause determines the distribution OF data. Make sure COLUMN(s) chosen ARE the optimal data distribution key TO minimize skew. record_dependency_ddl ----------------------- (1 row) SELECT * FROM temp_drop_dependency_ddl ; ddl_statement ---------------------------------------- ---- DROP VIEW --- + DROP VIEW IF EXISTS public.v1 CASCADE;+ DROP VIEW IF EXISTS public.v2 CASCADE;+ DROP VIEW IF EXISTS public.v3 CASCADE;+ (1 ROW) SELECT * FROM temp_create_dependency_ddl ; ddl_statement ------------------------------------------------------------ -- + -- Greenplum Database database dump + -- + -- Dumped from database version 12.12 + -- Dumped by pg_dump version 12.12 + SET gp_default_storage_options = ''; + SET statement_timeout = 0; + SET lock_timeout = 0; + SET idle_in_transaction_session_timeout = 0; + SET idle_session_timeout = 0; + SET client_encoding = 'UTF8'; + SET standard_conforming_strings = on; + SELECT pg_catalog.set_config('search_path', '', false); + SET check_function_bodies = false; + SET xmloption = content; + SET client_min_messages = warning; + SET row_security = off; + -- + -- Name: v1; Type: VIEW; Schema: public; Owner: adbpgadmin+ -- + CREATE OR REPLACE VIEW public.v1 AS + SELECT test.a, + test.b + FROM public.test; + + ALTER TABLE public.v1 OWNER TO adbpgadmin; + + -- + -- Greenplum Database database dump complete + -- + + -- + -- Greenplum Database database dump + -- + -- Dumped from database version 12.12 + -- Dumped by pg_dump version 12.12 + SET gp_default_storage_options = ''; + SET statement_timeout = 0; + SET lock_timeout = 0; + SET idle_in_transaction_session_timeout = 0; + SET idle_session_timeout = 0; + SET client_encoding = 'UTF8'; + SET standard_conforming_strings = on; + SELECT pg_catalog.set_config('search_path', '', false); + SET check_function_bodies = false; + SET xmloption = content; + SET client_min_messages = warning; + SET row_security = off; + -- + -- Name: v2; Type: VIEW; Schema: public; Owner: adbpgadmin+ -- + CREATE OR REPLACE VIEW public.v2 AS + SELECT test.b, + test.c + FROM public.test; + + ALTER TABLE public.v2 OWNER TO adbpgadmin; + -- + -- Greenplum Database database dump complete + -- + + -- + -- Greenplum Database database dump + -- + -- Dumped from database version 12.12 + -- Dumped by pg_dump version 12.12 + SET gp_default_storage_options = ''; + SET statement_timeout = 0; + SET lock_timeout = 0; + SET idle_in_transaction_session_timeout = 0; + SET idle_session_timeout = 0; + SET client_encoding = 'UTF8'; + SET standard_conforming_strings = on; + SELECT pg_catalog.set_config('search_path', '', FALSE); + SET check_function_bodies = FALSE; + SET xmloption = content; + SET client_min_messages = warning; + SET row_security = off; + -- + -- Name: v3; Type: VIEW; Schema: public; Owner: adbpgadmin+ -- + CREATE OR REPLACE VIEW public.v3 AS + SELECT test.a, + v2.c + FROM (public.test + JOIN public.v2 ON ((test.b = v2.b))); + + ALTER TABLE public.v3 OWNER TO adbpgadmin; + -- + -- Greenplum Database database dump complete + -- + + -- + -- Greenplum Database database dump + -- + -- Dumped from database version 12.12 + -- Dumped by pg_dump version 12.12 + SET gp_default_storage_options = ''; + SET statement_timeout = 0; + SET lock_timeout = 0; + SET idle_in_transaction_session_timeout = 0; + SET idle_session_timeout = 0; + SET client_encoding = 'UTF8'; + SET standard_conforming_strings = on; + SELECT pg_catalog.set_config('search_path', '', false); + SET check_function_bodies = false; + SET xmloption = content; + SET client_min_messages = warning; + SET row_security = off; + -- + -- Name: v3; Type: VIEW; Schema: public; Owner: adbpgadmin+ -- + CREATE OR REPLACE VIEW public.v3 AS + SELECT test.a, + v2.c + FROM (public.test + JOIN public.v2 ON ((test.b = v2.b))); + + ALTER TABLE public.v3 OWNER TO adbpgadmin; + -- + -- Greenplum Database database dump complete + -- + (1 ROW)Run the drop_dependency_table function to drop all dependency views. After modifying column b of the test base table, run the create_dependency_table function to rebuild the dependency views.
SELECT drop_dependency_table(); NOTICE: DROP cascades TO VIEW v3 NOTICE: VIEW "v3" does NOT exist, skipping drop_dependency_table ----------------------- (1 ROW) SELECT create_dependency_table(); create_dependency_table ------------------------- (1 ROW)