Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to temporarily disable an index in Postgres?

I have got one index on a table that I would like to temporarily disable, I can't find any documentation suggesting that it's possible, though.

Reason: I've got an index that might be causing problems in queries unrelated to to any of the ones it was designed to speed up. It's a new index, and the system as a whole seems slower since it was introduced. I just want to be able to reliably eliminate it as the culprit, and this seems like the easiest way, other solution suggestions, as well as better question suggestions, are also welcome.

like image 849
quodlibetor Avatar asked May 26 '11 22:05

quodlibetor


People also ask

Can we disable index in PostgreSQL?

You can also disable indexscan to disable all indices. Also, make sure you are doing explain analyze on your queries.

How do you disable index?

Click the plus sign to expand the table on which you want to disable an index. Click the plus sign to expand the Indexes folder. Right-click the index you want to disable and select Disable. In the Disable Indexes dialog box, verify that the correct index is in the Indexes to disable grid and click OK.

Does dropping an index lock a table?

A normal DROP INDEX acquires an ACCESS EXCLUSIVE lock on the table, blocking other accesses until the index drop can be completed. With this option, the command instead waits until conflicting transactions have completed.

Can we create index on temporary table in PostgreSQL?

It's unfortunate that PostgreSQL can't currently temporarily index CTE terms. Other database products can do so, and it can be a huge performance win.


2 Answers

You can poke the system catalogue to disable an index:

update pg_index set indisvalid = false where indexrelid = 'test_pkey'::regclass 

This means that the index won't be used for queries but will still be updated. It's one of the flags used for concurrent index building. Note that I've only done a quick test to see if the index still seems to be updated, caveat emptor.

like image 50
araqnid Avatar answered Sep 21 '22 14:09

araqnid


begin; drop index foo_ndx; explain analyze select * from foo; rollback; 

I don't think there is a way to disable just one, though you can do this in a transaction to make recovering from it dead simple. You can also disable indexscan to disable all indices.

Also, make sure you are doing explain analyze on your queries.

like image 23
Seth Robertson Avatar answered Sep 20 '22 14:09

Seth Robertson