获取 PostgreSQL 中视图/表所依赖的表列表
在 PostgreSQL 中,有没有一种方法可以根据外键的使用和对给定表的访问来获取视图/表所依赖的所有表?
基本上,我希望能够使用脚本复制视图/表的结构,并希望能够自动获取我还需要复制的表列表,以便一切正常工作。
此回复 似乎正朝着正确的方向前进,但没有给我我期望/需要的结果。有什么建议吗?
In PostgreSQL, is there a way to get all of the tables that a view/table depends on based on its use of foreign keys and access to a given table?
Basically, I want to be able to copy the structure of a view/table using a script and want to be able to automatically get the list of tables that I would also need to copy in order for everything to still work right.
This response appears to be headed in the right direction, but doesn't give me the results that I expect/need. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Andy Lester 提供的信息,我能够提出以下查询来检索我需要的信息。
获取外键引用的表:
获取表中的视图或规则引用的表:
Using the info from Andy Lester, I was able to come up with the following queries to retrieve the information that I needed.
Get Tables that Foreign Keys refer to:
Get Tables that a View or Rules from a Table refer to:
假设您已正确设置外键,请使用 pg_dump 转储表定义。
Assuming you have your foreign keys set up correctly, use
pg_dump
to dump the table definitions.在 psql 中,将
+
添加到通常的\d
中将为您提供“引用者”列表以及表定义。In psql, adding
+
to the usual\d
gives you a "Referenced by" list along with the table definition.我认为这是一个非常糟糕的主意。只需复制整个数据库,我认为应用程序想要拥有所有数据,而不仅仅是一张表中的数据。
更重要的是,还有触发器,它们可能依赖于某些表,但要知道,您必须进行不那么容易的代码分析。
I think it is a quite bad idea. Just copy the whole database, I think that the application wants to have all data, not only data from one table.
What's more, there are also triggers, that could depend on some tables, but to know that you'd have to make not so easy code analysis.