In a PostgreSQL database, assuming there is enough disc space, can I create an index in a live production database without risk of downtime? In other words, are there locks or possible crash or data loss possible or something else with the creation of an index.
To be more precise, it’s an index on a JSONB sub-property in a 1Gb table:
create index on foo((bar->>'region'));
I’m currently hesitating in running this create index (it works without a problem on my machine with a database sample).
Advertisement
Answer
CREATE INDEX
will block writes to the table for the duration of the CREATE INDEX statement, but CREATE INDEX CONCURRENTLY
will not. The CONCURRENTLY
modifier does require more work, but in this case, it should be negligible (re-reading 1GB shortly after it is read for the initial scan).
Reads are not affected by either form of CREATE INDEX, other than competing for I/O at the operating system level.