It cannot run inside a transaction block. If it fails, the half-built index stays with indisvalid = false and must be dropped.
CREATE INDEX CONCURRENTLY
A way to build an index without blocking writes, at the cost of more passes and a possible INVALID index on failure.