在PostgreSQL(9.3)中,有没有一种简单的方法可以获取使用特定表的存储过程列表?
我正在更改几个表格,并需要修复使用它们的存储过程。
我正在更改几个表格,并需要修复使用它们的存储过程。
带有“thetable”文本的函数。
该查询返回函数名称、行号以及包含“thetable”的行:
select *
from (
select proname, row_number() over (partition by proname) as line, textline
from (
select proname, unnest(string_to_array(prosrc, chr(10))) textline
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where nspname = 'public'
and prosrc ilike '%thetable%'
) lines
) x
where textline ilike '%thetable%';
具有与thetable
相关联类型的任何参数或返回值的函数。
例如:
create function f2(rec thetable)...
create function f1() returns setof thetable...
这个查询会给出函数的名称、返回类型以及参数类型:
with rtype as (
select reltype
from pg_class
where relname = 'thetable')
select distinct on (proname) proname, prorettype, proargtypes
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
cross join rtype
where nspname = 'public'
and (
prorettype = reltype
or reltype::text = any(string_to_array(proargtypes::text, ' ')))
有一个较差的解决方案,即尝试在源代码中搜索特定的字符串。
postgres=# CREATE OR REPLACE FUNCTION foo()
RETURNS int AS $$
BEGIN
RETURN (SELECT a FROM t1);
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT oid::regprocedure FROM pg_proc WHERE prosrc ~ '\mt1\M';
┌───────┐
│ oid │
╞═══════╡
│ foo() │
└───────┘
(1 row)
\m \M
是正则表达式的约束条件,详见相关文档。
pg_get_functiondef()
。详情请见:http://stackoverflow.com/a/22191178/939860(当用作头文件中的行类型时,存在对系统中类型的依赖关系)。 - Erwin Brandstetter