Skip to content

Instantly share code, notes, and snippets.

@adunstan
Created September 1, 2011 19:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adunstan/1186988 to your computer and use it in GitHub Desktop.
Save adunstan/1186988 to your computer and use it in GitHub Desktop.
See which tables are called in a view
CREATE OR REPLACE FUNCTION public.viewtables(viewname text)
RETURNS SETOF text
LANGUAGE plperl
AS $function$
my $viewname = shift;
my $rv = spi_exec_query("explain (format yaml, verbose) SELECT * FROM $viewname");
my $resp = $rv->{rows}[0]->{'QUERY PLAN'};
my %tbls;
while ($resp =~ /Relation Name: (".*")\n\s+Schema: (".*")\n/g)
{
$tbls{"$2.$1"} = 1;
}
return [sort keys %tbls];
$function$
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment