Re: How can I get list of views that are using given column in table?

From: Thom Brown <thom(at)linux(dot)com>
To: depesz(at)depesz(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How can I get list of views that are using given column in table?
Date: 2012-02-20 17:45:31
Message-ID: CAA-aLv7bPu1E3GqyV0iyw=HL3OkKZofN1EuNWCweG5x-WPHrxw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 20 February 2012 17:29, hubert depesz lubaczewski <depesz(at)depesz(dot)com> wrote:
> On Mon, Feb 20, 2012 at 01:06:29PM +0000, Thom Brown wrote:
>> You could try this:
>>
>> SELECT distinct dependee.relname
>> FROM pg_depend
>> JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid
>> JOIN pg_class as dependee ON pg_rewrite.ev_class = dependee.oid
>> JOIN pg_class as dependent ON pg_depend.refobjid = dependent.oid
>> JOIN pg_attribute ON pg_depend.refobjid = pg_attribute.attrelid
>>     AND pg_depend.refobjsubid = pg_attribute.attnum
>> WHERE dependent.relname = <tablename>
>> AND pg_attribute.attnum > 0
>> AND pg_attribute.attname = <columnname>;
>
> thanks. took me a while to understand it, so decided to make it a bit
> shorter, and change the join order to the order of data flow:
>
> SELECT
>    distinct r.ev_class::regclass
> FROM
>    pg_attribute    as a
>    join pg_depend  as d on d.refobjid = a.attrelid AND d.refobjsubid = a.attnum
>    join pg_rewrite as r ON d.objid = r.oid
> WHERE
>    pg_attribute.attrelid = '<table name>'::regclass
>    AND pg_attribute.attname = '<column name>';
>
> but the logic in here is the same as in your query.

Yes, regclass will allow you to take a couple shortcuts and I'm not
sure why I didn't do that. You'd need to correct your WHERE clause
though to use the 'a' alias you created. I'd imagine that if you were
going to use such a query regularly, you'd need to add some extra
considerations into it to ensure you're not matching anything
incorrectly. I only say this because I hadn't really put too much
thought into the query. I don't know if it may inadvertently match
non-view objects.

Glad it helped in some way though.

--
Thom

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rainer Pruy 2012-02-20 18:12:02 Re: How to split up phone numbers?
Previous Message hubert depesz lubaczewski 2012-02-20 17:29:50 Re: How can I get list of views that are using given column in table?