---
title: "A new index froze every write: CREATE INDEX CONCURRENTLY in PostgreSQL"
description: "A plain CREATE INDEX locks the table against writes until it finishes. With CONCURRENTLY writes continue and the build takes longer, but it can't run inside a transaction, and a failure leaves an INVALID index to drop."
url: https://ihsanalapsi.dev/articles/postgresql-create-index-concurrently
lang: en
date: 2026-11-27
updated: 2026-11-27
tags: ["Web development", "Developer tools"]
author: Ihsan Alapsi (إحسان العبسي)
---
# A new index froze every write: CREATE INDEX CONCURRENTLY in PostgreSQL

You want to speed up a slow query, so you add an index with one line on the live orders table. A minute later, every customer trying to check out is stuck, and there is no error anywhere in the code. By the end of this article you will know why building an index blocks writes, how to build it with `CONCURRENTLY` while the table keeps working, and what to do if the build fails halfway.

## Why does a plain CREATE INDEX block writes?

A plain `CREATE INDEX` locks the table against writes for as long as the index is being built. Reads continue, but every insert, update or delete waits until it finishes.

It is like closing the storeroom door to take inventory: customers can see the shelves, but nobody can put anything in or take anything out. [The PostgreSQL CREATE INDEX documentation](https://www.postgresql.org/docs/current/sql-createindex.html) says transactions that try to write block until the build is finished, and that very large tables can take many hours to be indexed. On a live database, that means orders stop.

## What does CONCURRENTLY do?

`CONCURRENTLY` builds the index without locking out writes, so the app keeps working. The price is that the build takes significantly longer.

According to the same documentation, PostgreSQL performs two scans of the table instead of one, and waits for all existing transactions that could modify or use the index to finish. The index is first entered as "invalid," and after the two scans it is marked "valid" and ready.

```sql
CREATE INDEX CONCURRENTLY orders_customer_id_idx
    ON orders (customer_id);
```

## What trips people up with CONCURRENTLY?

The most common mistake is that `CONCURRENTLY` doesn't run inside a transaction. The documentation says a regular `CREATE INDEX` can be performed within a transaction block, but `CREATE INDEX CONCURRENTLY` cannot.

This is where migration tools that wrap every file in a transaction on their own get stuck: the line fails, or you drop `CONCURRENTLY` to get it through, and you are back to the lock. Check how your tool runs a single file outside a transaction.

Two more points from the same documentation:

1. Only one concurrent index build can occur on a table at a time, and the table's schema can't be modified during the build.
2. With a `UNIQUE` index, the uniqueness constraint is already enforced against other transactions when the second scan begins, so duplicate errors can show up in other queries before the index is ready.

## What do you do if the build fails?

If the build fails, an "INVALID" index stays on the table. Drop it and build again, or rebuild it with `REINDEX INDEX CONCURRENTLY`.

The documentation says a problem such as a deadlock or a uniqueness violation in a unique index makes the command fail and leaves an invalid index behind. Queries ignore it, but it still costs on every update. The psql `\d` command reports it as `INVALID`:

```sql
DROP INDEX orders_customer_id_idx;
CREATE INDEX CONCURRENTLY orders_customer_id_idx
    ON orders (customer_id);
```

## Where do you start now?

The lesson for us as developers: an index speeds up reads, but building it is itself a write operation on a live table, and it needs the same care.

Open your latest migration with a `CREATE INDEX` on a large table now, and check whether it uses `CONCURRENTLY` and whether your tool wraps it in a transaction. Then read about [partial indexes in PostgreSQL](/articles/postgresql-partial-indexes) to build a smaller, faster index, and [UPSERT with ON CONFLICT](/articles/postgresql-upsert-on-conflict), since it relies on a unique index. And if you are building your path in programming, [the Programmer Roadmap 2026](/products/roadmap-2026) helps you order the steps. Which of your tables will need its next index without a lock?

## FAQ

**Do reads stop too during a plain CREATE INDEX?**

No. According to the PostgreSQL documentation, other transactions can still read the table, but inserts, updates and deletes wait until the index build finishes.

**Can I build two indexes CONCURRENTLY on the same table at once?**

No. The documentation says only one concurrent index build can occur on a table at a time, and the table's schema can't be modified during the build.

**Does an INVALID index hurt if I leave it?**

Yes. Queries ignore it, but every change to the table still pays to update it. And if it was UNIQUE and failed in the second scan, it keeps enforcing uniqueness.
