DO
$$DECLARE msg varchar(100);
BEGIN
CREATE TEMP TABLE priv(priv_name varchar(50)) ON COMMIT DROP;



SELECT  'User does not have ' || MIN(priv_name) || ' privilege'
INTO    msg
FROM    (
        SELECT  priv_name
        FROM    (
                SELECT  priv_name
                FROM    (
                        SELECT  'r'::varchar(1) as priv, 'SELECT SEQUENCE'::varchar(20) as priv_name
                        UNION ALL
                        SELECT  'w'::varchar(1) as priv, 'UPDATE SEQUENCE'::varchar(20) as priv_name
                        UNION ALL
                        SELECT  'U'::varchar(1) as priv, 'USAGE SEQUENCE'::varchar(20) as priv_name) as ls_priv
                where not exists  (
                                  select 1
                                  from (
                                        select  min(substring(split_part(privl::text, current_user || '=', 2)
                                                                from 1
                                                                for strpos(split_part(privl::text, current_user || '=', 2), '/') - 1)) as priv
                                        from    (
                                                SELECT array_to_string(d.defaclacl, E'\n') AS privl
                                                FROM pg_default_acl d
                                                  LEFT JOIN pg_namespace n ON n.oid = d.defaclnamespace
                                                WHERE d.defaclobjtype = 'S'
                                                AND position((current_user || '=') in d.defaclacl::text) > 0) as seq_priv) as ex_priv
                                  where strpos(priv, ls_priv.priv) > 0)
                ) AS seq_priv

        UNION ALL

        SELECT  priv_name
        FROM    (
                SELECT  priv_name
                FROM    (
                        SELECT  'r'::varchar(1) as priv, 'SELECT TABLE'::varchar(20) as priv_name
                        UNION ALL
                        SELECT  'a'::varchar(1) as priv, 'INSERT TABLE'::varchar(20) as priv_name
                        UNION ALL
                        SELECT  'w'::varchar(1) as priv, 'UPDATE TABLE'::varchar(20) as priv_name
                        UNION ALL
                        SELECT  'D'::varchar(1) as priv, 'TRUNCATE TABLE'::varchar(20) as priv_name
                        UNION ALL
                        SELECT  'x'::varchar(1) as priv, 'REFERENCES TABLE'::varchar(20) as priv_name
                        UNION ALL
                        SELECT  't'::varchar(1) as priv, 'TRIGGER TABLE'::varchar(20) as priv_name
                        UNION ALL
                        SELECT  'd'::varchar(1) as priv, 'DELETE TABLE'::varchar(20) as priv_name) as ls_priv
                where not exists  (
                                  select 1
                                  from (
                                        select  min(substring(split_part(privl::text, current_user || '=', 2)
                                                                from 1
                                                                for strpos(split_part(privl::text, current_user || '=', 2), '/') - 1)) as priv
                                        from    (
                                                SELECT array_to_string(d.defaclacl, E'\n') AS privl
                                                FROM pg_default_acl d
                                                  LEFT JOIN pg_namespace n ON n.oid = d.defaclnamespace
                                                WHERE d.defaclobjtype = 'r'
                                                AND position((current_user || '=') in d.defaclacl::text) > 0) as tbl_priv) as ex_priv
                                  where strpos(priv, ls_priv.priv) > 0)
                ) AS tbl_priv

        UNION ALL

        SELECT  priv_name
        FROM    (
                SELECT  priv_name
                FROM    (
                        SELECT  'X'::varchar(1) as priv, 'EXECUTE FUNCTION'::varchar(20) as priv_name) as ls_priv
                where not exists  (
                                  select 1
                                  from (
                                        select  min(substring(split_part(privl::text, current_user || '=', 2)
                                                                from 1
                                                                for strpos(split_part(privl::text, current_user || '=', 2), '/') - 1)) as priv
                                        from    (
                                                SELECT array_to_string(d.defaclacl, E'\n') AS privl
                                                FROM pg_default_acl d
                                                  LEFT JOIN pg_namespace n ON n.oid = d.defaclnamespace
                                                WHERE d.defaclobjtype = 'f'
                                                AND position((current_user || '=') in d.defaclacl::text) > 0) as tunc_priv) as ex_priv
                                  where strpos(priv, ls_priv.priv) > 0)
                ) AS fun_priv) AS avail_priv
;

IF msg IS NOT NULL THEN
  RAISE EXCEPTION USING MESSAGE = msg, ERRCODE = 'P0001';
END IF;
END$$ LANGUAGE plpgsql;