I want to run the following code block in a transaction so that if any of the sql statements fail the entire transaction is aborted. If I run the following block as it is, does it run in a transaction or do I need to run it inside BEGIN; ... COMMIT;
DO $$
DECLARE
readonly_exists int;
BEGIN
SELECT COUNT(*) INTO readonly_exists FROM information_schema.enabled_roles
WHERE role_name = 'readonly';
IF readonly_exists = 0 THEN
<SQL STATEMENT 1>
<SQL STATEMENT 2>
<SQL STATEMENT 3>
ELSE
RAISE EXCEPTION 'readonly role already exists';
END IF;
END$$;