CREATE INDEX CONCURRENTLY (Build an Index Without Blocking Writes)
Last updated: 2026-11-04
CREATE INDEX CONCURRENTLY (Build an Index Without Blocking Writes)
A plain CREATE INDEX holds a SHARE lock on the table, blocking INSERT / UPDATE / DELETE — potentially for a long time on a big table. The CONCURRENTLY variant scans the table twice: it first commits an index marked INVALID, then waits for concurrent transactions to finish before marking it valid. It only takes SHARE UPDATE EXCLUSIVE, so reads and writes continue. The price is a slower build with more CPU and disk IO, plus a leftover INVALID index that must be cleaned up manually if the build fails.
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE INDEX CONCURRENTLY <index_name> ON <table_name> ( <column_name> )
⚙️ Parameters
| index_name | Name of the new index. It cannot clash with a schema name or with an existing index in the table. |
|---|---|
| table_name | The table to index. |
| column_name | The indexed column(s). CONCURRENTLY applies to CREATE INDEX; it is not available for every variant of index creation. |
Returns:No return value. On success the command tag is CREATE INDEX.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
CREATE TABLE
INSERT 0 20000
ANALYZE
CREATE INDEX
-- EXPLAIN: the new index takes effect immediately and the query uses an Index Scan
Index Scan using idx_concurrent_email on idx_demo_concurrent (cost=0.29..8.30 rows=1 width=4)
Index Cond: (email = 'user42@example.com'::text)
Index Searches: 1
Buffers: shared hit=3
Planning Time: 0.211 ms
Execution Time: 0.162 ms
(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.
Note: CONCURRENTLY must run outside a transaction block. Executing this script statement by statement in psql works fine; but if the whole script is submitted as one transaction (wrapped in BEGIN / COMMIT, or by a client that batches statements into a single submission), PostgreSQL reports:
ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block.)
📌 Key Points
- CONCURRENTLY cannot run inside a transaction block and must be committed on its own; placing it between BEGIN / COMMIT raises CREATE INDEX CONCURRENTLY cannot run inside a transaction block.
- Submitting the whole script as one batch in browser PGlite hits the same error — run that statement on its own.
- CONCURRENTLY scans the table twice, so it is slower than a plain CREATE INDEX and burns extra CPU and disk IO while it runs.
- If a CONCURRENTLY build fails it leaves behind an INVALID index: queries cannot use it, yet it still slows writes, so remove it with DROP INDEX.
- CONCURRENTLY is available for CREATE INDEX; CREATE UNIQUE INDEX CONCURRENTLY is subject to the same transaction-block restriction.
- Run ANALYZE after the build so the planner works with fresh statistics.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhy does adding CONCURRENTLY produce "cannot run inside a transaction block"?
ACONCURRENTLY has to advance the build across transaction boundaries — commit an INVALID index first, then mark it valid once concurrent transactions finish — which is impossible inside a transaction block. Commit that statement on its own instead of wrapping it in BEGIN / COMMIT.
QWhat do I do when a CONCURRENTLY build fails?
AA failed CONCURRENTLY build leaves an INVALID index behind: unusable by queries but still slowing writes. Inspect it via indisvalid in pg_index or \d+ on the table, then DROP INDEX it and retry.