【问题标题】:Checking for existence of index in PostgreSQL [duplicate]检查PostgreSQL中是否存在索引[重复]
【发布时间】:2017-08-31 13:51:56
【问题描述】:

我知道如何创建索引

CREATE INDEX ix_dsvtable
  ON public."DsVTable"
  USING btree
  (dind, sec, regind, datind);

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

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

【问题讨论】:

  • create index if not exists ...

标签: sql postgresql indexing


【解决方案1】:

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

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;

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

【讨论】:

  • 这对我有用。谢谢!
  • 我还想知道索引是否是主索引,所以我添加了一个条件“and ix.indisprimary = 'f'”
  • 此查询不会打印物化视图的索引...不过 +1。
猜你喜欢
  • 2012-11-01
  • 2021-07-22
  • 2016-05-03
  • 2012-11-30
  • 2015-09-16
  • 2015-02-13
  • 2013-05-28
  • 2010-09-20
  • 1970-01-01
相关资源
最近更新 更多