From 957d14548a3c7c2b089b4a8bb9ea753a0249e752 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 1 Jun 2019 12:22:38 -0500 Subject: [PATCH] Add Create An Index Without Locking The Table as a postgres til --- README.md | 3 ++- ...eate-an-index-without-locking-the-table.md | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 postgres/create-an-index-without-locking-the-table.md diff --git a/README.md b/README.md index d690c2f..5e770b4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_817 TILs and counting..._ +_818 TILs and counting..._ --- @@ -393,6 +393,7 @@ _817 TILs and counting..._ - [Convert A String To A Timestamp](postgres/convert-a-string-to-a-timestamp.md) - [Count Records By Type](postgres/count-records-by-type.md) - [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md) +- [Create An Index Without Locking The Table](postgres/create-an-index-without-locking-the-table.md) - [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md) - [Create Table Adds A Data Type](postgres/create-table-adds-a-data-type.md) - [Creating Conditional Constraints](postgres/creating-conditional-constraints.md) diff --git a/postgres/create-an-index-without-locking-the-table.md b/postgres/create-an-index-without-locking-the-table.md new file mode 100644 index 0000000..67dd233 --- /dev/null +++ b/postgres/create-an-index-without-locking-the-table.md @@ -0,0 +1,20 @@ +# Create An Index Without Locking The Table + +When creating an index for a column, the process of building the index will +lock the column's table. For small datasets this isn't a concern because the +index will take no time at all to create. For larger datasets, the lock could +last long enough to create meaningful downtime. This can all be avoided by +telling Postgres to build the index concurrently. + +```sql +create index concurrently idx_book_isbns on books(isbn); +``` + +Creating the index this way will take a bit longer and put more strain on +machine resources, but it allows concurrent inserts, updates, or deletes on the +table. In other words, you can add an index to a large table in a production +environment without bringing down your app. + +Read more about the [details and potential +caveats](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY) +in the docs.