You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Strategically applied indexes are an important part of keeping queries against
4
+
a database fast. Initially applying those indexes—especially for large tables
5
+
in production—can take a bit of time.
6
+
7
+
The `create index` call doesn't provide any indication of progress. So if
8
+
applying an index takes minutes or even hours, it can be disconcerting.
9
+
10
+
Is it still working or is it locked up? How far along is it?
11
+
12
+
Postgres tracks the index creation process in `pg_stat_progress_create_index`
13
+
and we can query that table.
14
+
15
+
```sql
16
+
select
17
+
now()::time(0),
18
+
a.query,
19
+
p.phase,
20
+
p.blocks_total,
21
+
p.blocks_done,
22
+
p.tuples_total,
23
+
p.tuples_done,
24
+
ai.schemaname,
25
+
ai.relname,
26
+
ai.indexrelname
27
+
from pg_stat_progress_create_index p
28
+
join pg_stat_activity a onp.pid=a.pid
29
+
left join pg_stat_all_indexes ai onai.relid=p.relidandai.indexrelid=p.index_relid;
30
+
```
31
+
32
+
There are a bunch of phases that Postgres goes through to create the index,
33
+
especially if it is being created `concurrently`. The `blocks_done` and
34
+
`tuples_done` numbers will keep ticking along, giving you an indication that
35
+
the process is proceeding.
36
+
37
+
[source one](https://dba.stackexchange.com/a/249784) and [source two](https://www.depesz.com/2019/04/18/waiting-for-postgresql-12-report-progress-of-create-index-operations/)
0 commit comments