繁体   English   中英

如何从postgres数据库中删除所有索引表?

[英]How to delete all index tables from postgres database?

我的数据库中有很多索引表。 我想删除它们,只索引很大的那些表。 如何删除它们?

我可以

select relname from pg_class where relkind='i'; and drop index

但我认为此查询还将删除一些系统表。 如何在不影响数据库功能的情况下做到这一点?

如果您使用pg_class查找所有索引,则需要将其连接到pg_namespace并根据存储表(和索引)的模式进行过滤。

但是,使用pg_indexes要容易pg_indexes

select schemaname, 
       indexname, 
       tablename, 
       format('drop index %I.%I;', schemaname, indexname) as drop_statement
from pg_indexes
where schemaname not in ('pg_catalog', 'pg_toast');

但是,这还将向您显示用于主键的索引。

如果要排除主键索引,则可以使用以下方法:

select s.nspname as schemaname,
       i.relname as indexname,
       t.relname as tablename,
       format('drop index %I.%I;', s.nspname, i.relname) as drop_statement
from pg_index idx
  join pg_class i on i.oid = idx.indexrelid
  join pg_class t on t.oid = idx.indrelid
  join pg_namespace s on i.relnamespace = s.oid
where s.nspname  not in ('pg_catalog', 'pg_toast')
  and not idx.indisprimary;

如果您还想排除唯一索引,只需在where条件中添加and not idx.indisunique

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM