Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to add index to a postgres table without locking up the db?

I have a postgres table with 3 columns that has 4 million rows and growing. When I created the table I intended to create a btree index on one of the columns, but I forgot to do so. I can use the CREATE INDEX command to create the index now, but I think that will lock up the db. Since I have data coming in every second, I would like to avoid that. I noticed that there is an option called CONCURRENTLY. Is that what I should use? Does it have any other effects that I should be aware of?

Thanks!

like image 450
user1657624 Avatar asked Oct 22 '25 13:10

user1657624


1 Answers

Yes you can use CREATE INDEX CONCURRENTLY idx_xyz ON sometable(key_a, key_b);

However caveats apply: you have to look yourself, whether it succeded, and possibly re-try.

like image 158
knitti Avatar answered Oct 25 '25 20:10

knitti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!