为什么我的 "CREATE INDEX CONCURRENTLY .." 命令被 "SELECT FROM" 查询阻止了? (Postgres 9.6)

Why is my "CREATE INDEX CONCURRENTLY .." command blocked by a "SELECT FROM" query? (Postgres 9.6)

我正在 运行 迁移以创建索引,但迁移被另一个查询阻止。在发现有另一个查询阻止了我的迁移后,我解决了这个问题;取消阻塞查询后,我能够成功 运行 迁移。

(我的服务器在 Linux 上使用 Postgres 9.6)

以下是我发现我的迁移被阻止的原因:

SELECT
    activity.pid,
    activity.usename,
    activity.query,
    blocking.pid AS blocking_id,
    blocking.query AS blocking_query
FROM pg_stat_activity AS activity
JOIN pg_stat_activity AS blocking ON blocking.pid = ANY(pg_blocking_pids(activity.pid));

返回以下结果:

| pid | usename | query                                                                           | blocking_id | blocking_query                                                              |
|-----+---------+---------------------------------------------------------------------------------+-------------+-----------------------------------------------------------------------------|
| 123 | my_user | CREATE  INDEX CONCURRENTLY "idx_orders_on_widget_id" ON "orders"  ("widget_id") | 456         | SELECT  "customers".* FROM "customers" WHERE "customers"."id" =  LIMIT  |

我的迁移(CREATE INDEX CONCURRENTLY .. 查询)怎么可能被 SELECT .. FROM .. 查询阻止了?即使阻塞查询的进程 运行 处于僵尸状态,我也不明白我的索引创建查询如何被“SELECT .. FROM ..”查询阻塞。

任何人都可以提供有关这如何可能的见解吗?

如果有帮助,这是我的架构(针对此问题进行了简化):

订单

id

widget_id

customer_id(FK 到 Customers

客户

id

company_id(FK 到 Companies

公司

id

一些补充说明:

The documentation 说:

PostgreSQL supports building indexes without locking out writes. This method is invoked by specifying the CONCURRENTLY option of CREATE INDEX. When this option is used, PostgreSQL must perform two scans of the table, and in addition it must wait for all existing transactions that could potentially modify or use the index to terminate.