Besides these potential, documented causes:
- a deadlock
- hitting
statement_timeout - for a unique index: a uniqueness violation
- for expression and partial indexes: expression evaluation errors
- Are there more?
- How could a
CREATE INDEX CONCURRENTLYstatement, with its SHARE UPDATE EXCLUSIVE lock, end up in a deadlock, mentioned in the documentation above? - Is
lock_timeoutactually needed, whenCREATE INDEX CONCURRENTLYis the only DDL statement to be run? Isn't it ok to just 'wait it out' until the lock is acquired, as the statement won't be blocking regular CRUD operations anyway. I can only think of one problematic scenario: if the incoming data distribution varies enough that not runningANALYZE(which would be blocked) would impact the planner.
How to cause a deadlock with
CREATE INDEX CONCURRENTLY:Session 1:
Session 2:
Session 3:
Session 1:
On my PostgreSQL v16, the deadlock detector will cancel sessions 1 and 2.
The exhaustive list when
CREATE INDEX CONCURRENTLYwill fail:This is a fool's errand. You will never be able to enumerate all possible causes. Let me give you a few to show how pointless the exercise is:
CREATE INDEX CONCURRENTLYhitslock_timeoutthe database server crashes due to hardware failure
the network connection gets interrupted
the server runs out of disk space
Any other runtime error will serve.
To be honest, I don't see the point behind that question. When you run
CREATE INDEX CONCURRENTLY, you check if it succeeds or not. If not, drop the invalid index. Alternatively, if you are in the habit of creating indexes frequently, schedule a regular job that gets rid of all invalid indexes.