首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查PostgreSQL中是否存在索引

检查PostgreSQL中是否存在索引
EN

Stack Overflow用户
提问于 2017-08-31 21:51:57
回答 1查看 17.5K关注 0票数 11

我知道如何创建索引

代码语言:javascript
复制
CREATE INDEX ix_dsvtable
  ON public."DsVTable"
  USING btree
  (dind, sec, regind, datind);

如何检查索引是否已经存在?

我需要检查它们的存在,如果它们还不存在,就创建它们。

EN

Stack Overflow用户

回答已采纳

发布于 2017-08-31 22:06:59

您可以使用以下查询获取索引列表、索引的表和列:

代码语言:javascript
复制
select
    t.relname as table_name,
    i.relname as index_name,
    a.attname as column_name
from
    pg_class t,
    pg_class i,
    pg_index ix,
    pg_attribute a
where
    t.oid = ix.indrelid
    and i.oid = ix.indexrelid
    and a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    and t.relkind = 'r'
   -- and t.relname like 'mytable'
order by
    t.relname,
    i.relname;

从那里,您可以按索引名或涉及的列检查是否存在,并决定创建/跳过索引。

票数 34
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45983169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档