-- generate a deadlock
-- deadlock detection runs about every second,
-- hence we should expect 1 tps
BEGIN;
SELECT NEXTVAL('pgbench_deadlock_seq') AS myid \gset
\set odd :myid % 2 = 1
\if :odd
-- wait for EVEN to get its id as well
CALL pgbench_deadlock_wait(:myid+1);
UPDATE pgbench_deadlock_data SET myid = :myid WHERE cid = 1;
-- barrier
SELECT NEXTVAL('pgbench_deadlock_seq');
CALL pgbench_deadlock_wait(:myid+3);
UPDATE pgbench_deadlock_data SET myid = :myid WHERE cid = 2;
\else
-- EVEN
UPDATE pgbench_deadlock_data SET myid = :myid WHERE cid = 2;
-- barrier
SELECT NEXTVAL('pgbench_deadlock_seq');
CALL pgbench_deadlock_wait(:myid+2);
UPDATE pgbench_deadlock_data SET myid = :myid WHERE cid = 1;
\endif
-- RESET by the deadlock winner
\set i debug(:myid)
UPDATE pgbench_deadlock_data SET myid = 0;
COMMIT;
