PostgreSQL CREATE INDEX CONCURRENTLY等待列

时间:2016-08-11 07:04:54

标签: postgresql indexing

我试图在大表上创建索引:

 datid  |       datname        |  pid  | usesysid |         usename         |       application_name       | client_addr  | client_hostname | client_port |          backend_start         |          xact_start           |          query_start           |         state_change          | waiting | state  | backend_xid | backend_xmin  |                                           query                                           
 -------+----------------------+-------+----------+-------------------------+---  --------------------------+--------------+-----------------+-------------+------ -------------------------+-------------------------------+---------------------- ---------+-------------------------------+---------+--------+-------------+----- ---------+---------------------------------------------------------------------- ---------------------
  25439 | messengerdb          | 30692 |    25438 | messengerdb_rw          |  pgAdmin III - Przegl??darka | 10.167.12.52 |                 |       50593 | 2016-08-11 05:27:12.101452+02  | 2016-08-11 05:28:01.535943+02 | 2016-08-11 05:28:01.535943+02 | 2016-08-11 05:28:01.535958+02 | t       | active  |             |   1173740991 | CREATE INDEX CONCURRENTLY user_time_idx                                                   +
        |                      |       |          |                         |                              |              |                 |             |                               |                                |                               |                               |         |         |             |              |    ON core.conversations (user_id ASC NULLS LAST,  last_message_timestamp ASC NULLS LAST);+

这个查询有效吗?我担心“等待”栏目==='t'这是否意味着它正在等待锁定或某事?

1 个答案:

答案 0 :(得分:7)

并发创建索引可能需要很长时间,因为它不会从写入锁定表,并且等待其他事务完成。但是,如果您在事务中保持空闲的连接(例如,当客户端或应用程序在没有回滚/提交的情况下保持打开连接时),它可能会永远等待。

检查事务中是否存在空闲连接(您应该能够在进程列表中看到它们)。您还可以查看PostgreSQL日志。

PostgreSQL文档中的

Section about creating index concurrently可能会有所帮助。关于此link下的并发索引,还有一篇很好的文章。