如何在 Postgres 中获取序列名称列表?

2024-04-01

我想获取 Postgres 中的序列名称列表。

在 Oracle 中,我可以使用:

select sequence_name 
from user_sequences

但在 Postgres 中,当我使用该语句时,它总是会抛出错误:未找到用户序列.

我怎样才能在 Postgres 中做到这一点?


您可以使用:

select sequence_schema, sequence_name
from information_schema.sequences;

这将返回一个序列列表无障碍给当前用户,而不是其他用户owned by him.

如果你想列出序列owned由当前用户您需要加入pg_class, pg_namespace and pg_user:

select n.nspname as sequence_schema, 
       c.relname as sequence_name,
       u.usename as owner
from pg_class c 
  join pg_namespace n on n.oid = c.relnamespace
  join pg_user u on u.usesysid = c.relowner
where c.relkind = 'S'
  and u.usename = current_user;

在 Postgres 中,用户可以在多个模式中拥有对象(例如序列),而不仅仅是“他自己的”,因此您还需要检查在哪个模式中创建序列。

手册中的更多详细信息:

  • https://www.postgresql.org/docs/current/static/infoschema-sequences.html https://www.postgresql.org/docs/current/static/infoschema-sequences.html
  • https://www.postgresql.org/docs/current/static/catalog-pg-class.html https://www.postgresql.org/docs/current/static/catalog-pg-class.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Postgres 中获取序列名称列表? 的相关文章

随机推荐