| 1 |
# Test deadlock resolution with parallel process groups. |
| 2 |
|
| 3 |
# It's fairly hard to get parallel worker processes to block on locks, |
| 4 |
# since generally they don't want any locks their leader didn't already |
| 5 |
# take. We cheat like mad here by creating aliases for advisory-lock |
| 6 |
# functions that are incorrectly marked parallel-safe so that they can |
| 7 |
# execute in a worker. |
| 8 |
|
| 9 |
# Note that we explicitly override any global settings of isolation level |
| 10 |
# or debug_parallel_query, to ensure we're testing what we intend to. |
| 11 |
|
| 12 |
# Otherwise, this is morally equivalent to deadlock-soft.spec: |
| 13 |
# Four-process deadlock with two hard edges and two soft edges. |
| 14 |
# d2 waits for e1 (soft edge), e1 waits for d1 (hard edge), |
| 15 |
# d1 waits for e2 (soft edge), e2 waits for d2 (hard edge). |
| 16 |
# The deadlock detector resolves the deadlock by reversing the d1-e2 edge, |
| 17 |
# unblocking d1. |
| 18 |
|
| 19 |
# However ... it's not actually that well-defined whether the deadlock |
| 20 |
# detector will prefer to unblock d1 or d2. It depends on which backend |
| 21 |
# is first to run DeadLockCheck after the deadlock condition is created: |
| 22 |
# that backend will search outwards from its own wait condition, and will |
| 23 |
# first find a loop involving the *other* lock. We encourage that to be |
| 24 |
# one of the d2a1 parallel workers, which will therefore unblock d1a2 |
| 25 |
# workers, by setting a shorter deadlock_timeout in session d2. But on |
| 26 |
# slow machines, one or more d1a2 workers may not yet have reached their |
| 27 |
# lock waits, so that they're not unblocked by the first DeadLockCheck. |
| 28 |
# The next DeadLockCheck may choose to unblock the d2a1 workers instead, |
| 29 |
# which would allow d2a1 to complete before d1a2, causing the test to |
| 30 |
# freeze up because isolationtester isn't expecting that completion order. |
| 31 |
# (In effect, we have an undetectable deadlock because d2 is waiting for |
| 32 |
# d1's completion, but on the client side.) To fix this, introduce an |
| 33 |
# additional lock (advisory lock 3), which is initially taken by d1 and |
| 34 |
# then d2a1 will wait for it after completing the main part of the test. |
| 35 |
# In this way, the deadlock detector can see that d1 must be completed |
| 36 |
# first, regardless of timing. |
| 37 |
|
| 38 |
setup |
| 39 |
{ |
| 40 |
-- The alias functions themselves. Really these return "void", but |
| 41 |
-- the implementation is such that we can declare them to return "int", |
| 42 |
-- and we will get a zero result. |
| 43 |
create function lock_share(bigint) returns int language internal as |
| 44 |
'pg_advisory_xact_lock_shared_int8' strict parallel safe; |
| 45 |
|
| 46 |
create function lock_excl(bigint) returns int language internal as |
| 47 |
'pg_advisory_xact_lock_int8' strict parallel safe; |
| 48 |
|
| 49 |
-- Inline-able wrappers that will produce an integer "1" result: |
| 50 |
create function lock_share(int,int) returns int language sql as |
| 51 |
'select 1 - lock_share($1)' parallel safe; |
| 52 |
|
| 53 |
create function lock_excl(int,int) returns int language sql as |
| 54 |
'select 1 - lock_excl($1)' parallel safe; |
| 55 |
|
| 56 |
create table bigt as select x from generate_series(1, 10000) x; |
| 57 |
analyze bigt; |
| 58 |
} |
| 59 |
|
| 60 |
teardown |
| 61 |
{ |
| 62 |
drop function lock_share(int,int); |
| 63 |
drop function lock_excl(int,int); |
| 64 |
drop table bigt; |
| 65 |
} |
| 66 |
|
| 67 |
session d1 |
| 68 |
setup { BEGIN isolation level repeatable read; |
| 69 |
SET debug_parallel_query = off; |
| 70 |
SET deadlock_timeout = '10s'; |
| 71 |
} |
| 72 |
# these locks will be taken in the leader, so they will persist: |
| 73 |
step d1a1 { SELECT lock_share(1,x), lock_excl(3,x) FROM bigt LIMIT 1; } |
| 74 |
# this causes all the parallel workers to take locks: |
| 75 |
step d1a2 { SET debug_parallel_query = on; |
| 76 |
SET parallel_setup_cost = 0; |
| 77 |
SET parallel_tuple_cost = 0; |
| 78 |
SET min_parallel_table_scan_size = 0; |
| 79 |
SET parallel_leader_participation = off; |
| 80 |
SET max_parallel_workers_per_gather = 3; |
| 81 |
SELECT sum(lock_share(2,x)) FROM bigt; } |
| 82 |
step d1c { COMMIT; } |
| 83 |
|
| 84 |
session d2 |
| 85 |
setup { BEGIN isolation level repeatable read; |
| 86 |
SET debug_parallel_query = off; |
| 87 |
SET deadlock_timeout = '10ms'; |
| 88 |
} |
| 89 |
# this lock will be taken in the leader, so it will persist: |
| 90 |
step d2a2 { select lock_share(2,x) FROM bigt LIMIT 1; } |
| 91 |
# this causes all the parallel workers to take locks; |
| 92 |
# after which, make the leader take lock 3 to prevent client-driven deadlock |
| 93 |
step d2a1 { SET debug_parallel_query = on; |
| 94 |
SET parallel_setup_cost = 0; |
| 95 |
SET parallel_tuple_cost = 0; |
| 96 |
SET min_parallel_table_scan_size = 0; |
| 97 |
SET parallel_leader_participation = off; |
| 98 |
SET max_parallel_workers_per_gather = 3; |
| 99 |
SELECT sum(lock_share(1,x)) FROM bigt; |
| 100 |
SET debug_parallel_query = off; |
| 101 |
RESET parallel_setup_cost; |
| 102 |
RESET parallel_tuple_cost; |
| 103 |
SELECT lock_share(3,x) FROM bigt LIMIT 1; } |
| 104 |
step d2c { COMMIT; } |
| 105 |
|
| 106 |
session e1 |
| 107 |
setup { BEGIN isolation level repeatable read; |
| 108 |
SET debug_parallel_query = on; |
| 109 |
SET deadlock_timeout = '10s'; |
| 110 |
} |
| 111 |
# this lock will be taken in a parallel worker, but we don't need it to persist |
| 112 |
step e1l { SELECT lock_excl(1,x) FROM bigt LIMIT 1; } |
| 113 |
step e1c { COMMIT; } |
| 114 |
|
| 115 |
session e2 |
| 116 |
setup { BEGIN isolation level repeatable read; |
| 117 |
SET debug_parallel_query = on; |
| 118 |
SET deadlock_timeout = '10s'; |
| 119 |
} |
| 120 |
# this lock will be taken in a parallel worker, but we don't need it to persist |
| 121 |
step e2l { SELECT lock_excl(2,x) FROM bigt LIMIT 1; } |
| 122 |
step e2c { COMMIT; } |
| 123 |
|
| 124 |
permutation d1a1 d2a2 e1l e2l d1a2 d2a1 d1c e1c d2c e2c |