Search code examples
sqlpostgresqltransactionsrdbms

Does Serializable isolation level block read queries in Postgres


I have table "Transactions", where customers accounts transactions will be hold. So I want to set transaction level Serializable to prevent all types of anomalies.

Lets assume transactions goes this way:

BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
INSERT INTO transaction_test (sum, currency) VALUES (333, '820');

I doubt whether this table will be accessible for users to read, because our Application needs to run multiple SELECTs from this table for statistics?

After some googling I'm confused a little bit:

So the question is: can the Transaction table be safely use for SELECT queries in parallel of being changed with transactions of SERIAL level isolation?


Solution

  • PostgreSQL does not guarantee SERIALIZABLE by locking out every potentially conflicting transactions. Rather, it takes special predicate locks, which don't block anything, but track what data a transaction has read. If a situation arises that could violate serializability, PostgreSQL would abort one of the transactions with a serialization error.

    Concurrent read-only transactions won't be blocked. As recommended by the documentation, you should wrap read-only queries in a SERIALIZABLE READ ONLY transaction for best performance.