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.
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.
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
SELECT
clause replace the name of the tabled.refobjid::regclass as ref_object
with the name of the functiond.refobjid::regproc as ref_object
- In the
WHERE
clause replace the catalog of the referenced object fromd.refclassid = 'pg_class'::regclass
for tables, tod.refclassid = 'pg_proc'::regclass
for procedures (functions). Also change the object name fromd.refobjid = 't1'::regclass
tod.refobjid = 'f'::regproc
- In the
WHERE
clause, replace the name of the tablerefobjid = 't1'::regclass
with 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
:
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.
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.
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.
Example Data
The output for the example queries is based on these database objects and data.
Content feedback and comments