我像这样在Postgres中创建了一个索引:
CREATE UNIQUE INDEX my_index_name
ON my_table USING btree (custid,
date_trunc('day'::text, timezone('UTC'::text, somedate)),
firstname,
middlename,
lastname);
我监视了可用磁盘空间以获取对索引创建进度的估计,我希望看到可用空间减少,表明该进程正在完成其工作。问题是停机40分钟后,卡住了25分钟,然后又开始消耗磁盘空间:
[当它似乎卡住时,我检查了运行时间长的进程,以查看是否有什么阻塞它(不太可能,这是其他人没有使用的数据库副本),并且我发现有3个相同的“ CREATE INDEX”进程。
这就是我想问的:
这是我发出的用于查看长时间运行的进程的命令,该进程解开后,只有进程18511继续运行:
my_user => SELECT pid, now() - pg_stat_activity.query_start AS duration, query, state
FROM pg_stat_activity
WHERE (now() - query_start) > interval '5 minutes' AND state != 'idle'
ORDER by 2 DESC;
-[ RECORD 1 ]-------------------------------------------------------------------------------------------------------------------------------------------------------------
pid | 18511
duration | 01:04:37.969599
query | CREATE UNIQUE INDEX my_index_name ON my_table USING btree (custid, date_trunc('day'::text, timezone('UTC'::text, somedate)), firstname, middlename, lastname);
state | active
-[ RECORD 2 ]-------------------------------------------------------------------------------------------------------------------------------------------------------------
pid | 12712
duration | 01:04:37.969599
query | CREATE UNIQUE INDEX my_index_name ON my_table USING btree (custid, date_trunc('day'::text, timezone('UTC'::text, somedate)), firstname, middlename, lastname);
state | active
-[ RECORD 3 ]-------------------------------------------------------------------------------------------------------------------------------------------------------------
pid | 12713
duration | 01:04:37.969599
query | CREATE UNIQUE INDEX my_index_name ON my_table USING btree (custid, date_trunc('day'::text, timezone('UTC'::text, somedate)), firstname, middlename, lastname);
state | active
建立索引有几个步骤:扫描表,对条目进行排序等等。并非所有这些步骤都会消耗磁盘空间。例如,排序不应消耗越来越多的存储空间。
简而言之,一切看起来都应该正确。