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.

Kindindex
Example LanguageSQL

📝 Syntax

CREATE INDEX CONCURRENTLY <index_name> ON <table_name> ( <column_name> )

⚙️ Parameters

index_nameName of the new index. It cannot clash with a schema name or with an existing index in the table.
table_nameThe table to index.
column_nameThe 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏