| 1 |
# INSERT...ON CONFLICT DO UPDATE test |
| 2 |
# |
| 3 |
# Other INSERT...ON CONFLICT DO UPDATE isolation tests illustrate the "MVCC |
| 4 |
# violation" added to facilitate the feature, whereby a |
| 5 |
# not-visible-to-our-snapshot tuple can be updated by our command all the same. |
| 6 |
# This is generally needed to provide a guarantee of a successful INSERT or |
| 7 |
# UPDATE in READ COMMITTED mode. This MVCC violation is quite distinct from |
| 8 |
# the putative "MVCC violation" that has existed in PostgreSQL for many years, |
| 9 |
# the EvalPlanQual() mechanism, because that mechanism always starts from a |
| 10 |
# tuple that is visible to the command's MVCC snapshot. This test illustrates |
| 11 |
# a slightly distinct user-visible consequence of the same MVCC violation |
| 12 |
# generally associated with INSERT...ON CONFLICT DO UPDATE. The impact of the |
| 13 |
# MVCC violation goes a little beyond updating MVCC-invisible tuples. |
| 14 |
# |
| 15 |
# With INSERT...ON CONFLICT DO UPDATE, the UPDATE predicate is only evaluated |
| 16 |
# once, on this conclusively-locked tuple, and not any other version of the |
| 17 |
# same tuple. It is therefore possible (in READ COMMITTED mode) that the |
| 18 |
# predicate "fail to be satisfied" according to the command's MVCC snapshot. |
| 19 |
# It might simply be that there is no row version visible, but it's also |
| 20 |
# possible that there is some row version visible, but only as a version that |
| 21 |
# doesn't satisfy the predicate. If, however, the conclusively-locked version |
| 22 |
# satisfies the predicate, that's good enough, and the tuple is updated. The |
| 23 |
# MVCC-snapshot-visible row version is denied the opportunity to prevent the |
| 24 |
# UPDATE from taking place, because we don't walk the UPDATE chain in the usual |
| 25 |
# way. |
| 26 |
|
| 27 |
setup |
| 28 |
{ |
| 29 |
CREATE TABLE colors (key int4 PRIMARY KEY, color text, is_active boolean); |
| 30 |
INSERT INTO colors (key, color, is_active) VALUES(1, 'Red', false); |
| 31 |
INSERT INTO colors (key, color, is_active) VALUES(2, 'Green', false); |
| 32 |
INSERT INTO colors (key, color, is_active) VALUES(3, 'Blue', false); |
| 33 |
} |
| 34 |
|
| 35 |
teardown |
| 36 |
{ |
| 37 |
DROP TABLE colors; |
| 38 |
} |
| 39 |
|
| 40 |
session s1 |
| 41 |
setup |
| 42 |
{ |
| 43 |
BEGIN ISOLATION LEVEL READ COMMITTED; |
| 44 |
} |
| 45 |
step insert1 { |
| 46 |
WITH t AS ( |
| 47 |
INSERT INTO colors(key, color, is_active) |
| 48 |
VALUES(1, 'Brown', true), (2, 'Gray', true) |
| 49 |
ON CONFLICT (key) DO UPDATE |
| 50 |
SET color = EXCLUDED.color |
| 51 |
WHERE colors.is_active) |
| 52 |
SELECT * FROM colors ORDER BY key;} |
| 53 |
step select1surprise { SELECT * FROM colors ORDER BY key; } |
| 54 |
step c1 { COMMIT; } |
| 55 |
|
| 56 |
session s2 |
| 57 |
setup |
| 58 |
{ |
| 59 |
BEGIN ISOLATION LEVEL READ COMMITTED; |
| 60 |
} |
| 61 |
step update2 { UPDATE colors SET is_active = true WHERE key = 1; } |
| 62 |
step c2 { COMMIT; } |
| 63 |
|
| 64 |
# Perhaps surprisingly, the session 1 MVCC-snapshot-visible tuple (the tuple |
| 65 |
# with the pre-populated color 'Red') is denied the opportunity to prevent the |
| 66 |
# UPDATE from taking place -- only the conclusively-locked tuple version |
| 67 |
# matters, and so the tuple with key value 1 was updated to 'Brown' (but not |
| 68 |
# tuple with key value 2, since nothing changed there): |
| 69 |
permutation update2 insert1 c2 select1surprise c1 |