获取 PostgreSQL 中视图/表所依赖的表列表

发布于 2024-10-03 19:08:26 字数 328 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬心 2024-10-10 19:08:27

使用 Andy Lester 提供的信息,我能够提出以下查询来检索我需要的信息。

获取外键引用的表:

SELECT cl2.relname AS ref_table
FROM pg_constraint as co
JOIN pg_class AS cl1 ON co.conrelid=cl1.oid
JOIN pg_class AS cl2 ON co.confrelid=cl2.oid
WHERE co.contype='f' AND cl1.relname='TABLENAME'
ORDER BY cl2.relname;

获取表中的视图或规则引用的表:

SELECT cl_d.relname AS ref_table
FROM pg_rewrite AS r
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
JOIN pg_depend AS d ON r.oid=d.objid
JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid
WHERE cl_d.relkind IN ('r','v') AND cl_r.relname='TABLENAME'
GROUP BY cl_d.relname
ORDER BY cl_d.relname;

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:

SELECT cl2.relname AS ref_table
FROM pg_constraint as co
JOIN pg_class AS cl1 ON co.conrelid=cl1.oid
JOIN pg_class AS cl2 ON co.confrelid=cl2.oid
WHERE co.contype='f' AND cl1.relname='TABLENAME'
ORDER BY cl2.relname;

Get Tables that a View or Rules from a Table refer to:

SELECT cl_d.relname AS ref_table
FROM pg_rewrite AS r
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
JOIN pg_depend AS d ON r.oid=d.objid
JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid
WHERE cl_d.relkind IN ('r','v') AND cl_r.relname='TABLENAME'
GROUP BY cl_d.relname
ORDER BY cl_d.relname;
嘿看小鸭子会跑 2024-10-10 19:08:27

假设您已正确设置外键,请使用 pg_dump 转储表定义。

pg_dump -s -t TABLENAME

Assuming you have your foreign keys set up correctly, use pg_dump to dump the table definitions.

pg_dump -s -t TABLENAME
瘫痪情歌 2024-10-10 19:08:27

在 psql 中,将 + 添加到通常的 \d 中将为您提供“引用者”列表以及表定义。

\d+ tablename

In psql, adding + to the usual \d gives you a "Referenced by" list along with the table definition.

\d+ tablename
萌逼全场 2024-10-10 19:08:27

我认为这是一个非常糟糕的主意。只需复制整个数据库,我认为应用程序想要拥有所有数据,而不仅仅是一张表中的数据。
更重要的是,还有触发器,它们可能依赖于某些表,但要知道,您必须进行不那么容易的代码分析。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文