假设我想创建一个索引。这需要一些时间。我使用 pgadmin。假设当查询正在执行时,pgadmin 由于某种原因崩溃(例如计算机重新启动)。
该索引的状态是什么。它会继续创建并最终在某个时候成功创建,还是会立即失败,或者会在一段时间后失败?
有什么方法可以检查正在创建的索引的状态是什么? (我使用的是 postgres 版本 10.x)
最佳答案
如果我们不知道应用程序崩溃的原因,就很难回答这个问题。如果它不是由服务器故障引起的,则很可能索引已正确创建。您可以通过查询系统目录 pg_index
来检查这一点。您必须知道索引名称,例如:
select indexrelid::regclass, indisvalid
from pg_index
where indexrelid::regclass::text = 'my_table_unique_col_key'
indisvalid - If true, the index is currently valid for queries. False means the index is possibly incomplete: it must still be modified by INSERT/UPDATE operations, but it cannot safely be used for queries. If it is unique, the uniqueness property is not guaranteed true either.
如果索引尚未创建(或由于查询失败而根本没有创建),则上述查询不返回任何行。您可以检查创建索引的查询是否仍在运行(请参阅 Dynamic Statistics Views ):
select *
from pg_stat_activity
where query ilike 'create index%'
关于postgresql - 在完成命令 postgresql 之前关闭客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57994875/