获取视图中使用的函数列表

tho*_*jah 5 postgresql view functions dependencies

假设我有一个这样的函数:

create function house_analysis(ingeo geometry)
  returns table(count_all numeric, count_important numeric) as
$$
  select count(*), count(*) filter (where h.import_flag)
  from house_table h where st_intersects(ingeo, h.geom)
$$ language sql stable;
Run Code Online (Sandbox Code Playgroud)

我定义了一个这样的视图:

create or replace view postzone_analysis as (
  select p.zipcode, ha.count_all, ha.count_important
  from postzone_table p, house_analysis(p.geom) ha
);
Run Code Online (Sandbox Code Playgroud)

问题是:

如何pg_catalog.*使用我的视图(postzone_analysis或其视图)查询系统目录 ( ) 以oid获取其中使用的函数的列表?他们的pg_proc.oid价值观很好。

我知道数据库会跟踪,因为我无法删除该函数,但在pg_depend.

数据库是 PostgreSQL 9.5。

(现实生活中的情况要复杂得多 - 它被缩小为最低可行的例子。视图调用就像 6 个分析函数,它结合了来自不同来源的数据,并且有多个基于不同区域类的视图。 )

Erw*_*ter 3

在 Postgres 中,视图被实现为表加重写规则。详细信息请参见手册的“视图和规则系统”一章。

棘手的部分:视图本身不依赖于所涉及的函数,只有重写规则才依赖。所以这个查询应该是你的解决方案:

SELECT r.ev_class::regclass AS view, d.refobjid::regprocedure AS function
FROM   pg_rewrite r
JOIN   pg_depend  d ON d.objid = r.oid 
                   AND d.refclassid = 'pg_proc'::regclass  -- only functions
WHERE  r.ev_class = 'v123'::regclass;  -- name of view here (optionally schema-qualified)
Run Code Online (Sandbox Code Playgroud)

返回所有用户定义的函数(不包括内置函数)。

该查询假设您没有为视图定义任何其他规则。否则用 过滤rulename = '_RETURN',这是视图重写规则的默认名称。

转换为regprocedure(不仅仅是regproc)返回带有参数类型的函数,唯一地标识它。如果当前名称search_path不会以其他方式解析,则该名称会自动进行模式限定。