对GP下一个schema的满足特定条件的表赋权的函数,(这里特定条件指table_owner,表名的模式):
CREATE OR REPLACE FUNCTION setTablePrivilege(schemaname text, role_name text, table_pattern text, table_owner text)
RETURNS text AS
$BODY$
DECLARE
sqlstr text;
aTable RECORD;
privilegeStr text;
fullname text;
BEGIN
sqlstr:='select schemaname,tablename
from pg_tables
where schemaname ='''||schemaname||''' and tablename like '''||table_pattern||''' and tableowner='''||table_owner||'''' --注意,这里有一个额外的单引号用来表示转义
;
FOR aTable IN execute sqlstr LOOP
fullname := aTable.schemaname||'.'||aTable.tablename;
privilegeStr :='grant select on '|| fullname || ' to ' || role_name;
execute privilegeStr;
end loop;
return 'ok';
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
使用方法:
select setTablePrivilege('my_schema', 'gp_admin_role', '%', 'gpadmin');
这里“%”表示不限定表名。