| 1 |
# INSERT ... ON CONFLICT test verifying that speculative insertion |
| 2 |
# failures are handled |
| 3 |
# |
| 4 |
# Does this by using advisory locks controlling progress of |
| 5 |
# insertions. By waiting when building the index keys, it's possible |
| 6 |
# to schedule concurrent INSERT ON CONFLICTs so that there will always |
| 7 |
# be a speculative conflict. |
| 8 |
|
| 9 |
setup |
| 10 |
{ |
| 11 |
CREATE OR REPLACE FUNCTION blurt_and_lock_123(text) RETURNS text IMMUTABLE LANGUAGE plpgsql AS $$ |
| 12 |
BEGIN |
| 13 |
RAISE NOTICE 'blurt_and_lock_123() called for % in session %', $1, current_setting('spec.session')::int; |
| 14 |
|
| 15 |
-- depending on lock state, wait for lock 2 or 3 |
| 16 |
IF pg_try_advisory_xact_lock(current_setting('spec.session')::int, 1) THEN |
| 17 |
RAISE NOTICE 'acquiring advisory lock on 2'; |
| 18 |
PERFORM pg_advisory_xact_lock(current_setting('spec.session')::int, 2); |
| 19 |
ELSE |
| 20 |
RAISE NOTICE 'acquiring advisory lock on 3'; |
| 21 |
PERFORM pg_advisory_xact_lock(current_setting('spec.session')::int, 3); |
| 22 |
END IF; |
| 23 |
RETURN $1; |
| 24 |
END;$$; |
| 25 |
|
| 26 |
CREATE OR REPLACE FUNCTION blurt_and_lock_4(text) RETURNS text IMMUTABLE LANGUAGE plpgsql AS $$ |
| 27 |
BEGIN |
| 28 |
RAISE NOTICE 'blurt_and_lock_4() called for % in session %', $1, current_setting('spec.session')::int; |
| 29 |
RAISE NOTICE 'acquiring advisory lock on 4'; |
| 30 |
PERFORM pg_advisory_xact_lock(current_setting('spec.session')::int, 4); |
| 31 |
RETURN $1; |
| 32 |
END;$$; |
| 33 |
|
| 34 |
CREATE OR REPLACE FUNCTION ctoast_large_val() RETURNS text LANGUAGE SQL AS $$ select string_agg(encode(sha256(g::text::bytea),'hex'), '')::text from generate_series(1, 133) g $$; |
| 35 |
|
| 36 |
CREATE TABLE upserttest(key text, data text); |
| 37 |
|
| 38 |
CREATE UNIQUE INDEX upserttest_key_uniq_idx ON upserttest((blurt_and_lock_123(key))); |
| 39 |
} |
| 40 |
|
| 41 |
teardown |
| 42 |
{ |
| 43 |
DROP TABLE upserttest; |
| 44 |
} |
| 45 |
|
| 46 |
session controller |
| 47 |
setup |
| 48 |
{ |
| 49 |
SET default_transaction_isolation = 'read committed'; |
| 50 |
} |
| 51 |
step controller_locks {SELECT pg_advisory_lock(sess, lock), sess, lock FROM generate_series(1, 2) a(sess), generate_series(1,3) b(lock);} |
| 52 |
step controller_unlock_1_1 { SELECT pg_advisory_unlock(1, 1); } |
| 53 |
step controller_unlock_2_1 { SELECT pg_advisory_unlock(2, 1); } |
| 54 |
step controller_unlock_1_2 { SELECT pg_advisory_unlock(1, 2); } |
| 55 |
step controller_unlock_2_2 { SELECT pg_advisory_unlock(2, 2); } |
| 56 |
step controller_unlock_1_3 { SELECT pg_advisory_unlock(1, 3); } |
| 57 |
step controller_unlock_2_3 { SELECT pg_advisory_unlock(2, 3); } |
| 58 |
step controller_lock_2_4 { SELECT pg_advisory_lock(2, 4); } |
| 59 |
step controller_unlock_2_4 { SELECT pg_advisory_unlock(2, 4); } |
| 60 |
step controller_show {SELECT * FROM upserttest; } |
| 61 |
step controller_show_count {SELECT COUNT(*) FROM upserttest; } |
| 62 |
step controller_print_speculative_locks { |
| 63 |
SELECT pa.application_name, locktype, mode, granted |
| 64 |
FROM pg_locks pl JOIN pg_stat_activity pa USING (pid) |
| 65 |
WHERE |
| 66 |
locktype IN ('spectoken', 'transactionid') |
| 67 |
AND pa.datname = current_database() |
| 68 |
AND pa.application_name LIKE 'isolation/insert-conflict-specconflict/s%' |
| 69 |
ORDER BY 1, 2, 3, 4; |
| 70 |
} |
| 71 |
|
| 72 |
session s1 |
| 73 |
setup |
| 74 |
{ |
| 75 |
SET default_transaction_isolation = 'read committed'; |
| 76 |
SET spec.session = 1; |
| 77 |
} |
| 78 |
step s1_begin { BEGIN; } |
| 79 |
step s1_create_non_unique_index { CREATE INDEX upserttest_key_idx ON upserttest((blurt_and_lock_4(key))); } |
| 80 |
step s1_confirm_index_order { SELECT 'upserttest_key_uniq_idx'::regclass::int8 < 'upserttest_key_idx'::regclass::int8; } |
| 81 |
step s1_upsert { INSERT INTO upserttest(key, data) VALUES('k1', 'inserted s1') ON CONFLICT (blurt_and_lock_123(key)) DO UPDATE SET data = upserttest.data || ' with conflict update s1'; } |
| 82 |
step s1_insert_toast { INSERT INTO upserttest VALUES('k2', ctoast_large_val()) ON CONFLICT DO NOTHING; } |
| 83 |
step s1_commit { COMMIT; } |
| 84 |
step s1_noop { } |
| 85 |
|
| 86 |
session s2 |
| 87 |
setup |
| 88 |
{ |
| 89 |
SET default_transaction_isolation = 'read committed'; |
| 90 |
SET spec.session = 2; |
| 91 |
} |
| 92 |
step s2_begin { BEGIN; } |
| 93 |
step s2_upsert { INSERT INTO upserttest(key, data) VALUES('k1', 'inserted s2') ON CONFLICT (blurt_and_lock_123(key)) DO UPDATE SET data = upserttest.data || ' with conflict update s2'; } |
| 94 |
step s2_insert_toast { INSERT INTO upserttest VALUES('k2', ctoast_large_val()) ON CONFLICT DO NOTHING; } |
| 95 |
step s2_commit { COMMIT; } |
| 96 |
step s2_noop { } |
| 97 |
|
| 98 |
# Test that speculative locks are correctly acquired and released, s2 |
| 99 |
# inserts, s1 updates. |
| 100 |
permutation |
| 101 |
# acquire a number of locks, to control execution flow - the |
| 102 |
# blurt_and_lock_123 function acquires advisory locks that allow us to |
| 103 |
# continue after a) the optimistic conflict probe b) after the |
| 104 |
# insertion of the speculative tuple. |
| 105 |
controller_locks |
| 106 |
controller_show |
| 107 |
s1_upsert s2_upsert |
| 108 |
controller_show |
| 109 |
# Switch both sessions to wait on the other lock next time (the speculative insertion) |
| 110 |
controller_unlock_1_1 controller_unlock_2_1 |
| 111 |
# Allow both sessions to continue |
| 112 |
controller_unlock_1_3 controller_unlock_2_3 |
| 113 |
controller_show |
| 114 |
# Allow the second session to finish insertion |
| 115 |
controller_unlock_2_2 |
| 116 |
# This should now show a successful insertion |
| 117 |
controller_show |
| 118 |
# Allow the first session to finish insertion |
| 119 |
controller_unlock_1_2 |
| 120 |
# This should now show a successful UPSERT |
| 121 |
controller_show |
| 122 |
|
| 123 |
# Test that speculative locks are correctly acquired and released, s1 |
| 124 |
# inserts, s2 updates. |
| 125 |
permutation |
| 126 |
# acquire a number of locks, to control execution flow - the |
| 127 |
# blurt_and_lock_123 function acquires advisory locks that allow us to |
| 128 |
# continue after a) the optimistic conflict probe b) after the |
| 129 |
# insertion of the speculative tuple. |
| 130 |
controller_locks |
| 131 |
controller_show |
| 132 |
s1_upsert s2_upsert |
| 133 |
controller_show |
| 134 |
# Switch both sessions to wait on the other lock next time (the speculative insertion) |
| 135 |
controller_unlock_1_1 controller_unlock_2_1 |
| 136 |
# Allow both sessions to continue |
| 137 |
controller_unlock_1_3 controller_unlock_2_3 |
| 138 |
controller_show |
| 139 |
# Allow the first session to finish insertion |
| 140 |
controller_unlock_1_2 |
| 141 |
# This should now show a successful insertion |
| 142 |
controller_show |
| 143 |
# Allow the second session to finish insertion |
| 144 |
controller_unlock_2_2 |
| 145 |
# This should now show a successful UPSERT |
| 146 |
controller_show |
| 147 |
|
| 148 |
# Test that speculatively inserted toast rows do not cause conflicts. |
| 149 |
# s1 inserts successfully, s2 does not. |
| 150 |
permutation |
| 151 |
# acquire a number of locks, to control execution flow - the |
| 152 |
# blurt_and_lock_123 function acquires advisory locks that allow us to |
| 153 |
# continue after a) the optimistic conflict probe b) after the |
| 154 |
# insertion of the speculative tuple. |
| 155 |
controller_locks |
| 156 |
controller_show |
| 157 |
s1_insert_toast s2_insert_toast |
| 158 |
controller_show |
| 159 |
# Switch both sessions to wait on the other lock next time (the speculative insertion) |
| 160 |
controller_unlock_1_1 controller_unlock_2_1 |
| 161 |
# Allow both sessions to continue |
| 162 |
controller_unlock_1_3 controller_unlock_2_3 |
| 163 |
controller_show |
| 164 |
# Allow the first session to finish insertion |
| 165 |
controller_unlock_1_2 |
| 166 |
# This should now show that 1 additional tuple was inserted successfully |
| 167 |
controller_show_count |
| 168 |
# Allow the second session to finish insertion and kill the speculatively inserted tuple |
| 169 |
controller_unlock_2_2 |
| 170 |
# This should show the same number of tuples as before s2 inserted |
| 171 |
controller_show_count |
| 172 |
|
| 173 |
# Test that speculative locks are correctly acquired and released, s2 |
| 174 |
# inserts, s1 updates. With the added complication that transactions |
| 175 |
# don't immediately commit. |
| 176 |
permutation |
| 177 |
# acquire a number of locks, to control execution flow - the |
| 178 |
# blurt_and_lock_123 function acquires advisory locks that allow us to |
| 179 |
# continue after a) the optimistic conflict probe b) after the |
| 180 |
# insertion of the speculative tuple. |
| 181 |
controller_locks |
| 182 |
controller_show |
| 183 |
s1_begin s2_begin |
| 184 |
s1_upsert s2_upsert |
| 185 |
controller_show |
| 186 |
# Switch both sessions to wait on the other lock next time (the speculative insertion) |
| 187 |
controller_unlock_1_1 controller_unlock_2_1 |
| 188 |
# Allow both sessions to continue |
| 189 |
controller_unlock_1_3 controller_unlock_2_3 |
| 190 |
controller_show |
| 191 |
# Allow the first session to finish insertion |
| 192 |
controller_unlock_1_2 |
| 193 |
# But the change isn't visible yet, nor should the second session continue |
| 194 |
controller_show |
| 195 |
# Allow the second session to finish insertion, but it's blocked |
| 196 |
controller_unlock_2_2 |
| 197 |
controller_show |
| 198 |
# But committing should unblock |
| 199 |
s1_commit |
| 200 |
controller_show |
| 201 |
s2_commit |
| 202 |
controller_show |
| 203 |
|
| 204 |
# Test that speculative wait is performed if a session sees a speculatively |
| 205 |
# inserted tuple. A speculatively inserted tuple is one which has been inserted |
| 206 |
# both into the table and the unique index but has yet to *complete* the |
| 207 |
# speculative insertion |
| 208 |
permutation |
| 209 |
# acquire a number of advisory locks to control execution flow - the |
| 210 |
# blurt_and_lock_123 function acquires advisory locks that allow us to |
| 211 |
# continue after a) the optimistic conflict probe and b) after the |
| 212 |
# insertion of the speculative tuple. |
| 213 |
# blurt_and_lock_4 acquires an advisory lock which allows us to pause |
| 214 |
# execution c) before completing the speculative insertion |
| 215 |
|
| 216 |
# create the second index here to avoid affecting the other |
| 217 |
# permutations. |
| 218 |
s1_create_non_unique_index |
| 219 |
# confirm that the insertion into the unique index will happen first |
| 220 |
s1_confirm_index_order |
| 221 |
controller_locks |
| 222 |
controller_show |
| 223 |
s2_begin |
| 224 |
# Both sessions wait on advisory locks |
| 225 |
# (but don't show s2_upsert as complete till we've seen all of s1's notices) |
| 226 |
s1_upsert s2_upsert (s1_upsert notices 10) |
| 227 |
controller_show |
| 228 |
# Switch both sessions to wait on the other lock next time (the speculative insertion) |
| 229 |
controller_unlock_1_1 controller_unlock_2_1 |
| 230 |
# Allow both sessions to do the optimistic conflict probe and do the |
| 231 |
# speculative insertion into the table |
| 232 |
# They will then be waiting on another advisory lock when they attempt to |
| 233 |
# update the index |
| 234 |
controller_unlock_1_3 controller_unlock_2_3 |
| 235 |
controller_show |
| 236 |
# take lock to block second session after inserting in unique index but |
| 237 |
# before completing the speculative insert |
| 238 |
controller_lock_2_4 |
| 239 |
# Allow the second session to move forward |
| 240 |
controller_unlock_2_2 |
| 241 |
# This should still not show a successful insertion |
| 242 |
controller_show |
| 243 |
# Allow the first session to continue, it should perform speculative wait |
| 244 |
controller_unlock_1_2 |
| 245 |
# Should report s1 is waiting on speculative lock |
| 246 |
controller_print_speculative_locks |
| 247 |
# Allow s2 to insert into the non-unique index and complete. s1 will |
| 248 |
# no longer wait on speculative lock, but proceed to wait on the |
| 249 |
# transaction to finish. The no-op step is needed to ensure that |
| 250 |
# we don't advance to the reporting step until s2_upsert has completed. |
| 251 |
controller_unlock_2_4 s2_noop |
| 252 |
# Should report that s1 is now waiting for s2 to commit |
| 253 |
controller_print_speculative_locks |
| 254 |
# Once s2 commits, s1 is finally free to continue to update |
| 255 |
s2_commit s1_noop |
| 256 |
# This should now show a successful UPSERT |
| 257 |
controller_show |
| 258 |
# Ensure no unexpected locks survive |
| 259 |
controller_print_speculative_locks |