Sobes.tech
Senior

What is the difference between Repeatable Read and Serializable in PostgreSQL?

sobes.tech AI

Answer from AI

In PostgreSQL, transaction isolation levels determine how data modifications are visible to different transactions and what anomalies can occur.

  • Repeatable Read guarantees that within a single transaction, all SELECT queries will see the same snapshot of data — meaning the data will not change during the transaction. This prevents non-repeatable reads and phantom reads in the classical sense, but phantoms can still occur in some cases due to MVCC implementation.

  • Serializable is the strictest isolation level. It guarantees that the result of executing concurrent transactions is equivalent to some serial order of their execution. In PostgreSQL, this is achieved through conflict serialization mechanisms and transaction rollbacks when necessary (serialization failures).

Key differences:

  • Repeatable Read prevents reading modified data within a transaction but does not guarantee full serialization — phantoms are possible.
  • Serializable prevents all anomalies, including phantoms, ensuring full serialization but may lead to transaction rollbacks in case of conflicts.

Thus, Serializable provides stricter consistency but can potentially reduce performance due to conflicts.