加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在PostgreSQL中,如何查找哪个表使用特定的序列?

发布时间:2020-12-13 16:04:19 所属栏目:百科 来源:网络整理
导读:我有一个名为seque_post的序列. 我需要找出它正在使用的表格. 有没有办法编写一个会给出表名的查询? 我写了这个查询来查找序列: select *from pg_classwhere relname like 'seque_post' 有一个在那里提交reltoastrelid,根据manual给出: OID of the TOAST
我有一个名为seque_post的序列.

我需要找出它正在使用的表格.
有没有办法编写一个会给出表名的查询?

我写了这个查询来查找序列:

select *
from pg_class
where relname like 'seque_post'

有一个在那里提交reltoastrelid,根据manual给出:

OID of the TOAST table associated with this table,0 if none. The TOAST table stores large attributes “out of line” in a secondary table.

但我不知道如何从这里继续..建议?

解决方法

要查找序列与“相关”的表,您可以使用以下内容:

select seq_ns.nspname as sequence_schema,seq.relname as sequence_name,tab_ns.nspname as table_schema,tab.relname as related_table
from pg_class seq
  join pg_namespace seq_ns on seq.relnamespace = seq_ns.oid
  JOIN pg_depend d ON d.objid = seq.oid AND d.deptype = 'a' 
  JOIN pg_class tab ON d.objid = seq.oid AND d.refobjid = tab.oid
  JOIN pg_namespace tab_ns on tab.relnamespace = tab_ns.oid
where seq.relkind = 'S' 
  and seq.relname = 'seque_post'
  and seq_ns.nspname = 'public';

只是为了完成图片:

反过来(查找列的序列)更容易,因为Postgres有一个函数来查找列的序列:

select pg_get_serial_sequence('public.some_table','some_column');

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读