diff --git a/src/postgres/src/backend/storage/lmgr/predicate.c b/src/postgres/src/backend/storage/lmgr/predicate.c index e8390311d03f..e1e4b31ea80f 100644 --- a/src/postgres/src/backend/storage/lmgr/predicate.c +++ b/src/postgres/src/backend/storage/lmgr/predicate.c @@ -204,6 +204,8 @@ #include "utils/snapmgr.h" #include "utils/tqual.h" +#include "pg_yb_utils.h" + /* Uncomment the next line to test the graceful degradation code. */ /* #define TEST_OLDSERXID */ @@ -1608,8 +1610,28 @@ GetSerializableTransactionSnapshot(Snapshot snapshot) * A special optimization is available for SERIALIZABLE READ ONLY * DEFERRABLE transactions -- we can wait for a suitable snapshot and * thereby avoid all SSI overhead once it's running. + * + * YB: PG uses SSI to implement serializable isolation level. + * YB uses 2PL (i.e., two phase locking) for the same. + * Both YB and PG expose a different functionality using + * the same DEFERRABLE keyword. + * + * In PG, READ ONLY DEFERRABLE allows a read only transaction + * to avoid serialization errors with concurrent serializable + * transactions, by waiting for the concurrent transactions to + * complete first. + * + * In YB, there is no cycle detection algorithm. Instead READ ONLY + * serializable transactions are essentially equivalent to a + * READ ONLY snapshot isolation transaction (see #23213). + * YB uses DEFERRABLE to allow READ ONLY transactions to wait out + * the maximum possible clock skew (i.e., max_clock_skew_usec) + * so as to avoid read restart errors. + * + * Given this difference, YB need not wait for a safe snapshot + * i.e., concurrent transactions need not be waited for. */ - if (XactReadOnly && XactDeferrable) + if (!YBTransactionsEnabled() && XactReadOnly && XactDeferrable) return GetSafeSnapshot(snapshot); return GetSerializableTransactionSnapshotInt(snapshot, diff --git a/src/postgres/src/test/isolation/expected/modify-transaction-characteristics.out b/src/postgres/src/test/isolation/expected/modify-transaction-characteristics.out index 97ba5e902c3f..6d1c9e3a72c5 100644 --- a/src/postgres/src/test/isolation/expected/modify-transaction-characteristics.out +++ b/src/postgres/src/test/isolation/expected/modify-transaction-characteristics.out @@ -507,12 +507,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_rc_method2 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -522,12 +521,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_rc_method3_part1 s2_method3_part2 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -538,12 +536,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_rc_method4 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -553,12 +550,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_rr_method1 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -568,12 +564,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_rr_method2 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -583,12 +578,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_rr_method3_part1 s2_method3_part2 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -599,12 +593,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_rr_method4 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -614,12 +607,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_sr_method1 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -629,12 +621,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_sr_method2 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -644,12 +635,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_sr_method3_part1 s2_method3_part2 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -660,12 +650,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s2_begin_sr_method4 s2_switch_to_sr s2_read_only s2_deferrable s1_begin_sr_method1 s1_update s2_select s1_commit s2_commit @@ -675,12 +664,11 @@ step s2_read_only: SET TRANSACTION READ ONLY; step s2_deferrable: SET TRANSACTION DEFERRABLE; step s1_begin_sr_method1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; step s1_update: UPDATE test SET v=v+1 WHERE k=1; -step s2_select: SELECT * FROM test; -step s1_commit: COMMIT; -step s2_select: <... completed> +step s2_select: SELECT * FROM test; k v -1 3 +1 2 +step s1_commit: COMMIT; step s2_commit: COMMIT; starting permutation: s1_begin_rc_method1 s1_switch_to_sr s1_read_only s1_read_write s2_update s1_commit