isolation.hxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Definitions for transaction isolation levels, and such.
  2. *
  3. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/isolation instead.
  4. *
  5. * Copyright (c) 2000-2022, Jeroen T. Vermeulen.
  6. *
  7. * See COPYING for copyright license. If you did not receive a file called
  8. * COPYING with this source code, please notify the distributor of this
  9. * mistake, or contact the author.
  10. */
  11. #ifndef PQXX_H_ISOLATION
  12. #define PQXX_H_ISOLATION
  13. #if !defined(PQXX_HEADER_PRE)
  14. # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
  15. #endif
  16. #include "pqxx/util.hxx"
  17. namespace pqxx
  18. {
  19. /// Should a transaction be read-only, or read-write?
  20. /** No, this is not an isolation level. So it really doesn't belong here.
  21. * But it's not really worth a separate header.
  22. */
  23. enum class write_policy
  24. {
  25. read_only,
  26. read_write
  27. };
  28. /// Transaction isolation levels.
  29. /** These are as defined in the SQL standard. But there are a few notes
  30. * specific to PostgreSQL.
  31. *
  32. * First, postgres does not support "read uncommitted." The lowest level you
  33. * can get is "read committed," which is better. PostgreSQL is built on the
  34. * MVCC paradigm, which guarantees "read committed" isolation without any
  35. * additional performance overhead, so there was no point in providing the
  36. * lower level.
  37. *
  38. * Second, "repeatable read" also makes more isolation guarantees than the
  39. * standard requires. According to the standard, this level prevents "dirty
  40. * reads" and "nonrepeatable reads," but not "phantom reads." In postgres,
  41. * it actually prevents all three.
  42. *
  43. * Third, "serializable" is only properly supported starting at postgres 9.1.
  44. * If you request "serializable" isolation on an older backend, you will get
  45. * the same isolation as in "repeatable read." It's better than the
  46. * "repeatable read" defined in the SQL standard, but not a complete
  47. * implementation of the standard's "serializable" isolation level.
  48. *
  49. * In general, a lower isolation level will allow more surprising interactions
  50. * between ongoing transactions, but improve performance. A higher level
  51. * gives you more protection from subtle concurrency bugs, but sometimes it
  52. * may not be possible to complete your transaction without avoiding paradoxes
  53. * in the data. In that case a transaction may fail, and the application will
  54. * have to re-do the whole thing based on the latest state of the database.
  55. * (If you want to retry your code in that situation, have a look at the
  56. * transactor framework.)
  57. *
  58. * Study the levels and design your application with the right level in mind.
  59. */
  60. enum isolation_level
  61. {
  62. // PostgreSQL only has the better isolation levels.
  63. // read_uncommitted,
  64. read_committed,
  65. repeatable_read,
  66. serializable,
  67. };
  68. } // namespace pqxx
  69. #endif