【问题标题】:PostgreSQL: How to list all stored functions that access specific tablePostgreSQL:如何列出访问特定表的所有存储函数
【发布时间】:2015-08-17 08:16:28
【问题描述】:

对于给定的表,我如何找到使用该表中任何字段的所有函数? 假设我有带有 (studentid, studentname) 的学生表

我想要一个使用 studentid 和 studentname 的所有函数和触发器的列表。

我的问题与List stored functions that reference a table in PostgreSQL 类似,但那里给出的代码不起作用...n.nspname 是在与 Schema not to table 相关的条件下给出的。

【问题讨论】:

  • 您的表名在prosrc 列中,您必须使用like 或regex 查询该列。这与函数所在的架构无关。
  • 表有列,而不是字段...顺便说一句,information_schema.ROUTINE_TABLE_USAGE?
  • id jarlh 呢?
  • @jarlh postgresql 没有information_schema.routine_table_usage
  • @RadekPostołowicz,我明白了。感谢您的信息。

标签: sql postgresql


【解决方案1】:

也许不是很精确,但是这个查询应该可以工作。

SELECT routine_schema, routine_name 
FROM information_schema.routines 
WHERE routine_definition ~ 'student' 
AND routine_definition ~ 'studentid' 
AND routine_definition ~ 'studentname';

根据您编写程序的方式,您能否应用更精确的正则表达式。例如,如果您总是以这种形式编写表和列:table.column,您是否可以使用此表达式:

... WHERE routine_definition ~ 'student\.studentid' 
AND routine_definition ~ 'student\.studentname'

【讨论】:

  • 由于 sql 和 plpgsql 不区分大小写,因此明智的做法是使用不区分大小写的运算符 ~* 进行搜索。另外请记住,函数可能依赖于使用动态执行 sql 的表,如果没有人工审核,可能很难识别。
猜你喜欢
  • 2010-11-23
  • 2010-12-06
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
相关资源
最近更新 更多