PostgreSQL/greenplum query depends on the table View

Source: Internet
Author: User

In PostgreSQL and greenplum, we often need to recreate a table. To facilitate rollback, we usually rename the original table and then recreate the original table.

In this way, there will be a problem: the View dependent on the original table or the rename table is not dependent on the new table. This is because the view is defined based on the table oid. After the original table rename, the oId does not change, which leads to a table with incorrect view dependency.

It is troublesome to query a view that depends on a table. You can use the pg_depend data dictionary to query the view, but it is inconvenient to use.

The following provides a view to facilitate query of views dependent on tables.

1. Define a function to quickly convert the oId to schemaname. tablename.

 
Create or replace function public. tabname_oid (a oid) returns textas $ return plpy.exe cute ("select B. nspname | '. '|. relname as tabname from pg_class A, pg_namespace B wherea. relnamespace = B. oid and. oid = % s "% (A) [0] ['tabname']; $ language plpythonu;

2. Create a query view

Create view public. views_on_tables as select tabname_oid (C. ev_class) viewname, tabname_oid (PC. oid) tablename from pg_depend A, pg_depend B, pg_class PC, pg_rewrite c Where. refclassid = 1259 and B. deptype = 'I' and. classid = 2618 and. objid = B. objid and. classid = B. classid and. refclassid = B. refclassid and. refobjid <> B. refobjid and PC. oid =. refobjid and C. oid = B. objid group by C. ev_class, PC. oid;

3. The effect is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.