/*
 * Utility functions used by unit tests 
 *
 * Row lock checks
 */

CREATE TYPE _locks_entry AS (
	ctid		tid ,
	shared		BOOLEAN ,
	exclusive	BOOLEAN
);

CREATE OR REPLACE FUNCTION _get_locks_on(
			IN _table		TEXT )
		RETURNS SETOF _locks_entry
		STRICT VOLATILE
	AS $$

DECLARE
	_page		INT;
	_pages		INT;
	_record		RECORD;
	_return		_locks_entry;

BEGIN
	SELECT INTO _pages pg_relation_size( _table ) / 8192;

	FOR _page IN 0 .. ( _pages - 1 )
	LOOP
	
		FOR _record IN SELECT t_ctid , t_infomask
				FROM heap_page_items( get_raw_page( _table , _page ) )
				WHERE t_xmax::text::int > ( txid_current( ) & x'ffffffff'::bigint )
		LOOP
			_return := ROW( _record.t_ctid ,
				_record.t_infomask & x'80'::int <> 0 ,
				_record.t_infomask & x'40'::int <> 0 );
			RETURN NEXT _return;
		END LOOP;

	END LOOP;
END;
$$ LANGUAGE PLPGSQL;