Working with View Dependencies
If there are view dependencies on a table you must use the CASCADE keyword to drop it. Also, you cannot alter the table if there are view dependencies on it. This example shows a view dependency on a table.
CREATE TABLE t (id integer PRIMARY KEY);CREATE VIEW v AS SELECT * FROM t;DROP TABLE t;ERROR: cannot drop table t because other objects depend on itDETAIL: view v depends on table tHINT: Use DROP ... CASCADE to drop the dependent objects too.ALTER TABLE t DROP id;ERROR: cannot drop column id of table t because other objects depend on itDETAIL: view v depends on column id of table tHINT: Use DROP ... CASCADE to drop the dependent objects too.
As the previous example shows, altering a table can be quite a challenge if there is a deep hierarchy of views, because you have to create the views in the correct order. You cannot create a view unless all the objects it requires are present.
You can use view dependency information when you want to alter a table that is referenced by a view. For example, you might want to change a table’s column data type from integer to bigint because you realize you need to store larger numbers. However, you cannot do that if there are views that use the column. You first have to drop those views, then change the column and then run all the CREATE VIEW statements to create the views again.
Finding View Dependencies
The following example queries list view information on dependencies on tables and columns.
- Finding Direct View Dependencies on a Table
- Finding Direct Dependencies on a Table Column
- Listing View Schemas
- Listing View Definitions
- Listing Nested Views
The example output is based on the Example Data at the end of this topic.
Also, you can use the first example query Finding Direct View Dependencies on a Table to find dependencies on user-defined functions (or procedures). The query uses the catalog table pg_class that contains information about tables and views. For functions, you can use the catalog table pg_proc to get information about functions.
For detailed information about the system catalog tables that store view information, see About View Storage in Greenplum Database.
Finding Direct View Dependencies on a Table
To find out which views directly depend on table t1, create a query that performs a join among the catalog tables that contain the dependency information, and qualify the query to return only view dependencies.
SELECT v.oid::regclass AS view,d.refobjid::regclass AS ref_object -- name of table-- d.refobjid::regproc AS ref_object -- name of functionFROM pg_depend AS d -- objects that depend on a tableJOIN pg_rewrite AS r -- rules depending on a tableON r.oid = d.objidJOIN pg_class AS v -- views for the rulesON v.oid = r.ev_classWHERE v.relkind = 'v' -- filter views only-- dependency must be a rule depending on a relationAND d.classid = 'pg_rewrite'::regclassAND d.deptype = 'n' -- normal dependency-- qualify objectAND d.refclassid = 'pg_class'::regclass -- dependent tableAND d.refobjid = 't1'::regclass-- AND d.refclassid = 'pg_proc'::regclass -- dependent function-- AND d.refobjid = 'f'::regproc;view | ref_object------------+------------v1 | t1v2 | t1v2 | t1v3 | t1mytest.vt1 | t1mytest.v2a | t1mytest.v2a | t1(7 rows)
The query performs casts to the regclass object identifier type. For information about object identifier types, see the PostgeSQL documentation on Object Identifier Types.
In some cases, the views are listed multiple times because the view references multiple table columns. You can remove those duplicates using DISTINCT.
You can alter the query to find views with direct dependencies on the function f.
- In the
SELECTclause replace the name of the tabled.refobjid::regclass as ref_objectwith the name of the functiond.refobjid::regproc as ref_object - In the
WHEREclause replace the catalog of the referenced object fromd.refclassid = 'pg_class'::regclassfor tables, tod.refclassid = 'pg_proc'::regclassfor procedures (functions). Also change the object name fromd.refobjid = 't1'::regclasstod.refobjid = 'f'::regproc - In the
WHEREclause, replace the name of the tablerefobjid = 't1'::regclasswith the name of the functionrefobjid = 'f'::regproc.
In the example query, the changes have been commented out (prefixed with --). You can comment out the lines for the table and enable the lines for the function.
Finding Direct Dependencies on a Table Column
You can modify the previous query to find those views that depend on a certain table column, which can be useful if you are planning to drop a column (adding a column to the base table is never a problem). The query uses the table column information in the pg_attribute catalog table.
This query finds the views that depend on the column id of table t1:
SELECT v.oid::regclass AS view,d.refobjid::regclass AS ref_object, -- name of tablea.attname AS col_name -- column nameFROM pg_attribute AS a -- columns for a tableJOIN pg_depend AS d -- objects that depend on a columnON d.refobjsubid = a.attnum AND d.refobjid = a.attrelidJOIN pg_rewrite AS r -- rules depending on the columnON r.oid = d.objidJOIN pg_class AS v -- views for the rulesON v.oid = r.ev_classWHERE v.relkind = 'v' -- filter views only-- dependency must be a rule depending on a relationAND d.classid = 'pg_rewrite'::regclassAND d.refclassid = 'pg_class'::regclassAND d.deptype = 'n' -- normal dependencyAND a.attrelid = 't1'::regclassAND a.attname = 'id';view | ref_object | col_name------------+------------+----------v1 | t1 | idv2 | t1 | idmytest.vt1 | t1 | idmytest.v2a | t1 | id(4 rows)
Listing View Schemas
If you have created views in multiple schemas, you can also list views, each view’s schema, and the table referenced by the view. The query retrieves the schema from the catalog table pg_namespace and excludes the system schemas pg_catalog, information_schema, and gp_toolkit. Also, the query does not list a view if the view refers to itself.
SELECT v.oid::regclass AS view,ns.nspname AS schema, -- view schema,d.refobjid::regclass AS ref_object -- name of tableFROM pg_depend AS d -- objects that depend on a tableJOIN pg_rewrite AS r -- rules depending on a tableON r.oid = d.objidJOIN pg_class AS v -- views for the rulesON v.oid = r.ev_classJOIN pg_namespace AS ns -- schema informationON ns.oid = v.relnamespaceWHERE v.relkind = 'v' -- filter views only-- dependency must be a rule depending on a relationAND d.classid = 'pg_rewrite'::regclassAND d.refclassid = 'pg_class'::regclass -- referenced objects in pg_class (tables and views)AND d.deptype = 'n' -- normal dependency-- qualify objectAND ns.nspname NOT IN ('pg_catalog', 'information_schema', 'gp_toolkit') -- system schemasAND NOT (v.oid = d.refobjid) -- not self-referencing dependency;view | schema | ref_object------------+--------+------------v1 | public | t1v2 | public | t1v2 | public | t1v2 | public | v1v3 | public | t1vm1 | public | mytest.tm1mytest.vm1 | mytest | t1vm2 | public | mytest.tm1mytest.v2a | mytest | t1mytest.v2a | mytest | t1mytest.v2a | mytest | v1(11 rows)
Listing View Definitions
This query lists the views that depend on t1, the column referenced, and the view definition. The CREATE VIEW command is created by adding the appropriate text to the view definition.
SELECT v.relname AS view,d.refobjid::regclass as ref_object,d.refobjsubid as ref_col,'CREATE VIEW ' || v.relname || ' AS ' || pg_get_viewdef(v.oid) AS view_defFROM pg_depend AS dJOIN pg_rewrite AS rON r.oid = d.objidJOIN pg_class AS vON v.oid = r.ev_classWHERE NOT (v.oid = d.refobjid)AND d.refobjid = 't1'::regclassORDER BY d.refobjsubid;view | ref_object | ref_col | view_def------+------------+---------+--------------------------------------------v1 | t1 | 1 | CREATE VIEW v1 AS SELECT max(t1.id) AS id+| | | FROM t1;v2a | t1 | 1 | CREATE VIEW v2a AS SELECT t1.val +| | | FROM (t1 +| | | JOIN v1 USING (id));vt1 | t1 | 1 | CREATE VIEW vt1 AS SELECT t1.id +| | | FROM t1 +| | | WHERE (t1.id < 3);v2 | t1 | 1 | CREATE VIEW v2 AS SELECT t1.val +| | | FROM (t1 +| | | JOIN v1 USING (id));v2a | t1 | 2 | CREATE VIEW v2a AS SELECT t1.val +| | | FROM (t1 +| | | JOIN v1 USING (id));v3 | t1 | 2 | CREATE VIEW v3 AS SELECT (t1.val || f()) +| | | FROM t1;v2 | t1 | 2 | CREATE VIEW v2 AS SELECT t1.val +| | | FROM (t1 +| | | JOIN v1 USING (id));(7 rows)
Listing Nested Views
This CTE query lists information about views that reference another view.
The WITH clause in this CTE query selects all the views in the user schemas. The main SELECT statement finds all views that reference another view.
WITH views AS ( SELECT v.relname AS view,d.refobjid AS ref_object,v.oid AS view_oid,ns.nspname AS namespaceFROM pg_depend AS dJOIN pg_rewrite AS rON r.oid = d.objidJOIN pg_class AS vON v.oid = r.ev_classJOIN pg_namespace AS nsON ns.oid = v.relnamespaceWHERE v.relkind = 'v'AND ns.nspname NOT IN ('pg_catalog', 'information_schema', 'gp_toolkit') -- exclude system schemasAND d.deptype = 'n' -- normal dependencyAND NOT (v.oid = d.refobjid) -- not a self-referencing dependency)SELECT views.view, views.namespace AS schema,views.ref_object::regclass AS ref_view,ref_nspace.nspname AS ref_schemaFROM viewsJOIN pg_depend as depON dep.refobjid = views.view_oidJOIN pg_class AS classON views.ref_object = class.oidJOIN pg_namespace AS ref_nspaceON class.relnamespace = ref_nspace.oidWHERE class.relkind = 'v'AND dep.deptype = 'n';view | schema | ref_view | ref_schema------+--------+----------+------------v2 | public | v1 | publicv2a | mytest | v1 | public
Example Data
The output for the example queries is based on these database objects and data.
CREATE TABLE t1 (id integer PRIMARY KEY,val text NOT NULL);INSERT INTO t1 VALUES(1, 'one'), (2, 'two'), (3, 'three');CREATE FUNCTION f() RETURNS textLANGUAGE sql AS 'SELECT ''suffix''::text';CREATE VIEW v1 ASSELECT max(id) AS idFROM t1;CREATE VIEW v2 ASSELECT t1.valFROM t1 JOIN v1 USING (id);CREATE VIEW v3 ASSELECT val || f()FROM t1;CREATE VIEW v5 ASSELECT f() ;CREATE SCHEMA mytest ;CREATE TABLE mytest.tm1 (id integer PRIMARY KEY,val text NOT NULL);INSERT INTO mytest.tm1 VALUES(1, 'one'), (2, 'two'), (3, 'three');CREATE VIEW vm1 ASSELECT id FROM mytest.tm1 WHERE id < 3 ;CREATE VIEW mytest.vm1 ASSELECT id FROM public.t1 WHERE id < 3 ;CREATE VIEW vm2 ASSELECT max(id) AS idFROM mytest.tm1;CREATE VIEW mytest.v2a ASSELECT t1.valFROM public.t1 JOIN public.v1 USING (id);