Skip to content

Commit 957d145

Browse files
committed
Add Create An Index Without Locking The Table as a postgres til
1 parent 930e180 commit 957d145

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_817 TILs and counting..._
13+
_818 TILs and counting..._
1414

1515
---
1616

@@ -393,6 +393,7 @@ _817 TILs and counting..._
393393
- [Convert A String To A Timestamp](postgres/convert-a-string-to-a-timestamp.md)
394394
- [Count Records By Type](postgres/count-records-by-type.md)
395395
- [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md)
396+
- [Create An Index Without Locking The Table](postgres/create-an-index-without-locking-the-table.md)
396397
- [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md)
397398
- [Create Table Adds A Data Type](postgres/create-table-adds-a-data-type.md)
398399
- [Creating Conditional Constraints](postgres/creating-conditional-constraints.md)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Create An Index Without Locking The Table
2+
3+
When creating an index for a column, the process of building the index will
4+
lock the column's table. For small datasets this isn't a concern because the
5+
index will take no time at all to create. For larger datasets, the lock could
6+
last long enough to create meaningful downtime. This can all be avoided by
7+
telling Postgres to build the index concurrently.
8+
9+
```sql
10+
create index concurrently idx_book_isbns on books(isbn);
11+
```
12+
13+
Creating the index this way will take a bit longer and put more strain on
14+
machine resources, but it allows concurrent inserts, updates, or deletes on the
15+
table. In other words, you can add an index to a large table in a production
16+
environment without bringing down your app.
17+
18+
Read more about the [details and potential
19+
caveats](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY)
20+
in the docs.

0 commit comments

Comments
 (0)