Interview Q&A / PostgreSQL

IntermediateQuick answer1 min readUpdated 3 Oct 2026

What does CREATE INDEX CONCURRENTLY do and what are its trade-offs?

SHORT ANSWER

It builds the index without blocking writes to the table, by scanning the table in multiple passes. It takes longer, cannot run inside a transaction block, and can leave an INVALID index behind if it fails.

Explain it simply

A plain CREATE INDEX is like closing a shop to rearrange the shelves: no new orders can be inserted, updated or deleted until it finishes (reads still work). CREATE INDEX CONCURRENTLY rearranges shelves while the shop stays open. It has to walk through the table twice and wait for old transactions, so it is slower and uses more resources.

Example

sql
-- Blocks writes for the whole build
CREATE INDEX orders_date_idx ON orders (created_at);

-- Keeps writes flowing
CREATE INDEX CONCURRENTLY orders_date_idx ON orders (created_at);

-- Check for a failed build
SELECT indexrelid::regclass, indisvalid
FROM pg_index WHERE NOT indisvalid;

-- Clean up and retry
DROP INDEX CONCURRENTLY orders_date_idx;

Limits: it cannot run inside BEGIN ... COMMIT, so many migration tools need a setting to run it outside a transaction. If it fails, for instance on a unique violation, the index remains but is marked invalid and is not used.

What to say in the interview

"On a live production table I use CREATE INDEX CONCURRENTLY so writes are not blocked. It is slower, cannot run in a transaction, and a failure leaves an INVALID index that I must drop and recreate."

Common mistakes

  • Forgetting to check for invalid indexes after a failed concurrent build.

#postgresql#indexes#locks

Related questions

Practise more PostgreSQL questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open PostgreSQL quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.