After a lot of googling, I figured I'd ask this question. I have a piece of code which does the following against a postgres (9.2) database :
- Start transaction
- drop indexes (5 indexes) on the table
- insert a million rows into the table
- Re-create the indexes
- commit the transaction.
My reading of postgres tells me that I should be able to do that and still allow other users to select from the table (actually even use the existing indexes as they are not yet dropped for them) while this operation is ongoing.
What I'm actually finding is that all other queries on the table (they are all select queries) are stuck. Upon looking the pg_locks and pg_stat_activity table, i found that my transaction has created an AccessExclusiveLock on the table preventing other queries from running. Once this transaction finishes, all other queries execute just fine.
So, my question is - why is a create index/insert data creating an exclusive lock on the table? Should'nt it be using a less intrusive lock (like a SHARE lock for example)?
I'm basing this upon my reading of the offical documentation here - so I don't get flamed asking to RTFM from anybody :)
Thanks,
Jalpesh