--
-- Hot Standby Test Framework
--

-- We create a test table that can be used to listen for events
-- that occur on the primary, offering a simple mechanism for
-- one-way asynchronous communication between primary and standby.
--
-- On the primary we create tables, functions and data required
-- for a test, then register the test. This then issues an xlog
-- switch so that WAL records will move quickly to the standby.
-- Once the test registration record is visible on the standby 
-- we know we are ready to perform that test.

DROP TABLE IF EXISTS standby_test;
CREATE TABLE standby_test
( 
	test_name	TEXT NOT NULL PRIMARY KEY
);

DROP FUNCTION register_standby_test(p_test_name TEXT);

CREATE OR REPLACE FUNCTION register_standby_test(p_test_name TEXT)
RETURNS VOID
LANGUAGE PLPGSQL
AS $$
DECLARE
BEGIN
		INSERT INTO standby_test VALUES (p_test_name);
END;
$$;

CREATE OR REPLACE FUNCTION wait_for_test_registration
(p_test_name TEXT
,p_maxwait   INTEGER)
RETURNS BOOLEAN
LANGUAGE PLPGSQL
AS $$
DECLARE
	test_found	boolean;
	sleep_wait  INTEGER := 0;
BEGIN

	WHILE TRUE LOOP

		test_found := false;

		/*
		 * Poll to see if our test is registered.
		 */
		SELECT true INTO test_found
		FROM standby_test
		WHERE test_name = p_test_name;

		IF test_found THEN
			RETURN TRUE;
		ELSE
			sleep_wait := sleep_wait + 1;
			IF sleep_wait > p_maxwait THEN
				RETURN FALSE;
			ELSE
				PERFORM pg_sleep(1);
			END IF;
		END IF;

	END LOOP;
END;
$$;

select register_standby_test('test1');

select wait_for_test_registration('test1', 5);

select wait_for_test_registration('test2', 5);

