# Test SKIP LOCKED with an updated tuple chain, race case with wait at
# control point #42

setup
{
  CREATE TABLE foo (
	id int PRIMARY KEY,
	data text NOT NULL
  );
  INSERT INTO foo VALUES (1, 'x'), (2, 'x');
}

teardown
{
  DROP TABLE foo;
}

session "s1"
setup		{ 
                  -- we hold a lock that s2c can wait for
                  SELECT pg_advisory_lock(42);
                  BEGIN;
		}
step "s1a"	{ SELECT * FROM foo WHERE pg_advisory_lock(0) IS NOT NULL ORDER BY id LIMIT 1 FOR UPDATE SKIP LOCKED; }
step "s1b"	{ COMMIT; }

session "s2"
step "s2a"	{ 
		  -- first, block s1a from running after it has taken its snapshot
		  SELECT pg_advisory_lock(0); 
		}
step "s2b"	{
		  -- generate up update chain and commit
		  UPDATE foo SET data = data WHERE id = 1;
		}
step "s2c"      {
		  -- unblock s1a so that it starts running but make it wait at control point 43
		  SELECT pg_advisory_lock(43);
		  SELECT pg_advisory_unlock(0);
		}
step "s2d"      {
		  -- wait for s1a to reach wait point 42
		  SELECT pg_advisory_lock(42);
		  -- now lock the tuple and hold the lock
		  BEGIN;
		  UPDATE foo SET data = data WHERE id = 1;
		  -- and finally allow s1a to continue
		  SELECT pg_advisory_unlock(43);
		}
step "s2e"	{ COMMIT; }

permutation "s2a" "s1a" "s2b" "s2c" "s2d" "s1b" "s2e"
