2

PostgreSQL:

Adding an index to a table with big amount of data causes a lag for both SELECT and INSERT operations on that table, doesn't it?

While PostgreSQL does not support live (without lags) adding of indexes, how to solve the problem of adding an index to a table without lagging SELECT and INSERT (there is no UPDATEs on that table)?

My idea is to copy the table by small chunks to another table with the same structure, then in a transaction delete the original table and rename the new table to the original table name. How to ensure that the entire data is copied, while INSERT operations to the old table may happen?

asked Jul 24, 2020 at 19:08

1 Answer 1

2

See Building Indexes Concurrently in PostgreSQL documentation.

CONCURRENTLY option of CREATE INDEX solves the problem in an easy way. Just add it to CREATE INDEX.

Be aware that while CONCURRENTLY option avoids lags, it does slow down building the index and that CREATE INDEX with CONCURRENTLY cannot be used in a transaction.

answered Jul 24, 2020 at 19:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.