I had a huge query running on postgres and one of the join tables always did a sequential scan. The index on the column exists and postgres just didn't use it. I did a vacuum analyze and then postgres query plan immediately changes to doing an index scan.
My question is what is the most efficient way to be doing a vacuum analyze. Does it lock tables ? If it does, how do you vacuum analyze live production tables ?
You can run analyze only, no need to run vacuum also. Sintax will be :
ANALYZE [ VERBOSE ] [ table_name [ ( column_name [, ...] ) ] ]
In the documentation it states :
ANALYZE requires only a read lock on the target table, so it can run in parallel with other activity on the table.
You can find more information here:
http://www.postgresql.org/docs/9.4/static/sql-analyze.html
https://wiki.postgresql.org/wiki/Introduction_to_VACUUM,_ANALYZE,_EXPLAIN,_and_COUNT
Does a vacuum analyze lock tables ?
No, it's the "FULL VACUUM" command that locks tables.