Skip to content

Commit 9b17d8b

Browse files
committed
Add Inspect Progress Of Long-Running Create Index as a Postgres til
1 parent c9e4f0f commit 9b17d8b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1192 TILs and counting..._
13+
_1193 TILs and counting..._
1414

1515
---
1616

@@ -606,6 +606,7 @@ _1192 TILs and counting..._
606606
- [Include All Queries In The Log File](postgres/include-all-queries-in-the-log-file.md)
607607
- [Insert A Bunch Of Records With Generate Series](postgres/insert-a-bunch-of-records-with-generate-series.md)
608608
- [Insert Just The Defaults](postgres/insert-just-the-defaults.md)
609+
- [Inspect Progress Of Long-Running Create Index](postgres/inspect-progress-of-long-running-create-index.md)
609610
- [Install Postgres With uuid-ossp Using asdf](postgres/install-postgres-with-uuid-ossp-using-asdf.md)
610611
- [Integers In Postgres](postgres/integers-in-postgres.md)
611612
- [Intervals Of Time By Week](postgres/intervals-of-time-by-week.md)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Inspect Progress Of Long-Running Create Index
2+
3+
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 on p.pid = a.pid
29+
left join pg_stat_all_indexes ai on ai.relid = p.relid and ai.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

Comments
 (0)