\set id random_zipfian(1, 5000000, 2.5)
\set read_id random(1, 5000000)
\set val random(1, 1000)

BEGIN;
  SELECT pg_advisory_xact_lock(:id);
  SELECT pg_advisory_xact_lock(:id+1);

  SELECT balance, payload FROM stress_victim WHERE id = :read_id;

  SELECT count(*) FROM stress_victim WHERE id BETWEEN :id AND :id + 5;

  UPDATE stress_victim SET balance = :val WHERE id = :id;

  UPDATE stress_shadow SET balance = :val WHERE id = :id;
 
  DELETE FROM stress_victim WHERE id = :id + 1;
  DELETE FROM stress_shadow WHERE id = :id + 1;
 
  INSERT INTO stress_victim (id, balance, payload)
  VALUES (:id + 1, :val, 'new')
  ON CONFLICT (id) DO UPDATE SET balance = EXCLUDED.balance, payload = EXCLUDED.payload;
 
  INSERT INTO stress_shadow (id, balance, payload)
  VALUES (:id + 1, :val, 'new')
  ON CONFLICT (id) DO UPDATE SET balance = EXCLUDED.balance, payload = EXCLUDED.payload;
COMMIT;