DROP ROLE IF EXISTS creator;
DROP ROLE IF EXISTS alice;
DROP ROLE IF EXISTS myreaders;

-- Create unprivileged role to managed roles.
CREATE ROLE creator CREATEROLE;
ALTER ROLE creator SET createrole_self_grant TO 'inherit,set';

-- Helper to check the feature.
CREATE OR REPLACE FUNCTION check_membership() RETURNS VOID AS $$
BEGIN
	IF NOT pg_has_role('alice', 'myreaders', 'USAGE') THEN
		RAISE EXCEPTION 'MEMBERSHIP LOST !!';
	END IF;
END $$ LANGUAGE plpgsql;

-- Create role unprivileged.
SET ROLE creator;
CREATE ROLE myreaders;
CREATE ROLE alice LOGIN IN ROLE myreaders;
SELECT check_membership();

-- Drop creator.
RESET ROLE;
REASSIGN OWNED BY creator TO CURRENT_ROLE;
DROP OWNED BY creator;
DROP ROLE creator;
SELECT check_membership();
