patternsqlMinor
In PostgreSQL, can data be lost by adding an index to a table which is being updated?
Viewed 0 times
postgresqlcanlostaddingbeingupdatedwhichindexdatatable
Problem
I'm not sure how to answer this from the documentation, and am unable to find an answer through searching.
I'm wondering: if there's an existing table which is being updated (having data added to it), could I lose any of the data by adding an index to the table?
I don't mind things slowing down a bit, but I don't want to lose data as a result of adding an index, due to the table being locked or something whilst the index is being created.
To be clear, there is an existing table within a database, data is being added to the table, and I would like to add an index to the table. I am unsure if I could lose some of the data being added to the table whilst the index is being made or something.
I'm wondering: if there's an existing table which is being updated (having data added to it), could I lose any of the data by adding an index to the table?
I don't mind things slowing down a bit, but I don't want to lose data as a result of adding an index, due to the table being locked or something whilst the index is being created.
To be clear, there is an existing table within a database, data is being added to the table, and I would like to add an index to the table. I am unsure if I could lose some of the data being added to the table whilst the index is being made or something.
Solution
No, you won't lose data.
While the CREATE INDEX is running, Postgres will block all write access to the table. So any INSERT/UPDATE or DELETE statement that is started after the CREATE INDEX, will be forced to wait until the index is created (and COMMITed).
The situation is different if you use CREATE INDEX CONCURRENTLY. In that case the write access is not blocked and the DML will work "as usual". The CREATE INDEX might need longer to finish though.
If something goes wrong curing CREATE INDEX CONCURRENTLY, then the index is marked unusable, but still no data is lost. One such situation is the creation of a UNIQUE index and while it's running duplicates are inserted into the table.
More details on the concurrently option can be found in the manual
While the CREATE INDEX is running, Postgres will block all write access to the table. So any INSERT/UPDATE or DELETE statement that is started after the CREATE INDEX, will be forced to wait until the index is created (and COMMITed).
The situation is different if you use CREATE INDEX CONCURRENTLY. In that case the write access is not blocked and the DML will work "as usual". The CREATE INDEX might need longer to finish though.
If something goes wrong curing CREATE INDEX CONCURRENTLY, then the index is marked unusable, but still no data is lost. One such situation is the creation of a UNIQUE index and while it's running duplicates are inserted into the table.
More details on the concurrently option can be found in the manual
Context
StackExchange Database Administrators Q#320274, answer score: 8
Revisions (0)
No revisions yet.