REINDEX 是一个用于重建索引的实用命令。
REINDEX 在 PostgreSQL 7.0 中添加。
用法
进度跟踪
从 PostgreSQL 12 开始,可以通过视图 pg_stat_progress_create_index 跟踪重新索引进度。
变更历史
- PostgreSQL 17
- 添加了事件触发器支持(提交 f21848de)
- PostgreSQL 16
- PostgreSQL 14
- PostgreSQL 12
- 添加了
CONCURRENTLY选项(5dc92b84) - 可以使用
pg_stat_progress_create_index跟踪REINDEX的进度(提交 ab0dfc96)
- 添加了
- PostgreSQL 9.5
- PostgreSQL 8.1
- 添加了
REINDEX SYSTEM选项(提交 4cc7a93d)
- 添加了
- PostgreSQL 7.0
- 添加(提交 e3a97b37)
示例
重新索引表
appdb=> REINDEX (VERBOSE) TABLE object; INFO: index "object_pkey" was reindexed DETAIL: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.01 s INFO: index "object_class_id_ix" was reindexed DETAIL: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s REINDEX
重新索引数据库
在 PostgreSQL 15 及更早版本中,必须显式提供当前数据库名称
postgres=# REINDEX DATABASE;
ERROR: syntax error at or near ";"
LINE 1: REINDEX DATABASE;
^
postgres=# REINDEX DATABASE postgres;
REINDEX
无法重新索引其他数据库
postgres=# REINDEX DATABASE foo; ERROR: can only reindex the currently open database
参考资料
- PostgreSQL 文档: REINDEX
