Re: Slow information_schema.views

From: Oliver Kohll - Mailing Lists <oliver(dot)lists(at)gtwm(dot)co(dot)uk>
To: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
Cc: "pgsql-general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Slow information_schema.views
Date: 2012-03-22 10:36:56
Message-ID: 2131B8B1-B601-4791-AD43-55D590355AC9@gtwm.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On 22 Mar 2012, at 10:17, Albe Laurenz wrote:

>> Or is there a better way of finding view dependencies? I see there's a
> pg_catalog entry for tables
>> that a view depends on but that's not what I'm after.
>
> You can use pg_depend and pg_rewrite as follows:
>
> SELECT DISTINCT r.ev_class::regclass
> FROM pg_depend d JOIN
> pg_rewrite r ON (d.objid = r.oid)
> WHERE d.classid = 'pg_rewrite'::regclass
> AND d.refclassid = 'pg_class'::regclass
> AND r.ev_class <> d.refobjid
> AND d.refobjid::regclass::text LIKE '%myviewname%';
>
> I didn't test it very much, so play around with it a little before
> you trust it.
>
> I don't know if it will perform better in your case, but it should
> return more appropriate results
> (you don't want to find VIEW dummy AS SELECT * FROM t WHERE a =
> 'myviewname').
>
> Yours,
> Laurenz Albe

Thank you - I did come to a similar method yesterday following some pointers from previous messages but I'm glad to have some confirmation it's the right direction. It does perform an order of magnitude faster for me, from 500ms down to under 20ms. My exact query is

SELECT distinct dependent.relname
FROM pg_depend
JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid
JOIN pg_class as dependent ON pg_rewrite.ev_class = dependent.oid
JOIN pg_class as dependee ON pg_depend.refobjid = dependee.oid
WHERE dependee.relname = 'myviewname'
AND dependent.relname != 'myviewname'

Haven't tested this much yet either. I'll compare yours to mine and check the differences.

Regards
Oliver
www.agilebase.co.uk

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alexander Reichstadt 2012-03-22 10:42:23 Re: Cannot store special chars using c lib
Previous Message Albe Laurenz 2012-03-22 10:17:24 Re: Slow information_schema.views