查找表或视图的依赖对象
背景
在 PostgreSQL 中删除(或替换)对象时,如果存在依赖关系,则删除将会失败(不指定 CASCADE)。
问题
数据库返回的错误消息未列出依赖对象。
解决方案示例
该查询可能类似于:
SELECT * FROM information_schema i, pg_depend pd WHERE
i.object_id = pd.object_id AND
i.object_type = 'TABLE' AND
i.object_schema = 'public' AND
i.object_name = 'table_with_dependents';
缺少 objid
。
相关
- http://postgresql.1045698.n5.nabble.com/ information-schema-problem-td2144069.html
- http://www.alberton.info/postgresql_meta_info。 html
问题
如何按名称和类型生成依赖对象的列表?
Background
When dropping (or replacing) objects in PostgreSQL, if there are dependencies, the drop will fail (without specifying CASCADE
).
Problem
The error message returned by the database does not list the dependent objects.
Example Solution
The query might look something like:
SELECT * FROM information_schema i, pg_depend pd WHERE
i.object_id = pd.object_id AND
i.object_type = 'TABLE' AND
i.object_schema = 'public' AND
i.object_name = 'table_with_dependents';
The objid
is missing.
Related
- http://postgresql.1045698.n5.nabble.com/information-schema-problem-td2144069.html
- http://www.alberton.info/postgresql_meta_info.html
Question
How do you generate a list of dependent objects by name and type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
建议的解决方案对我来说不起作用,但 postgresql 9.1.4
有效:
The suggested solution didn't work for me with postgresql 9.1.4
this worked:
简单的方法是:
The easy way is:
在查询中包含嵌套视图,如下所示:
如果您关心特定表属性,请将其添加到递归 CTE 的顶部:
Include nested views in the query as follows:
If you care about specific table attributes add this to the top portion of the recursive CTE:
对于 PostgreSQL 9.3 及更高版本,请使用以下视图和函数来显示任何用户对象依赖性。我还更新了https://wiki.postgresql.org/wiki/Pg_depend_display。
For PostgreSQL 9.3 onward use the following view and functions to show any user object dependency. I also updated https://wiki.postgresql.org/wiki/Pg_depend_display.
根据 Kong Man 的答案,下面的 CTE 返回与给定表/视图交互的所有表和视图。在 PostgreSQL 15.3 中测试。
用法示例,在 CTE 末尾过滤特定架构和表:
返回引用它的所有表和视图:
Building off of Kong Man's answer, the below CTE returns all the tables and views that interact with a given table/view. Tested in PostgreSQL 15.3.
Example usage, filtering for a specific schema and table at the end of the CTE:
Returns all the tables and views that reference it:
pg_constraint
包含数据库中的所有约束,您可以使用confrelid
和conrelid
列出所有依赖表的oid
外键约束查询如下所示
select confrelid,conrelid from pg_constraint where contype='f';
pg_constraint
contains all constrains in the database you can list theoid
of dependent tables usingconfrelid
andconrelid
from all all the foreign key constraintsquery looks like this
select confrelid,conrelid from pg_constraint where contype='f';