Sub topic 3
Sequences, functions, and keeping access under control
This chapter assumes you can create roles, run GRANT and REVOKE, and use psql. It continues the shop schema and the app_owner, app_readonly, and app_writer roles from earlier chapters.
Sequences and functions
A sequence is an object that hands out numbers, such as the values behind an identity column. Inserting into a table with an identity column needs USAGE on its sequence. PostgreSQL grants EXECUTE on new functions to PUBLIC by default, so you usually revoke that first.
GRANT USAGE ON ALL SEQUENCES IN SCHEMA shop TO app_writer;
CREATE FUNCTION shop.order_count() RETURNS bigint
LANGUAGE sql STABLE
AS $$ SELECT count(*) FROM shop.orders $$;
REVOKE EXECUTE ON FUNCTION shop.order_count() FROM PUBLIC;
GRANT EXECUTE ON FUNCTION shop.order_count() TO app_readonly;ALTER DEFAULT PRIVILEGES for future objects
GRANT affects only objects that already exist. ALTER DEFAULT PRIVILEGES sets the grants that objects created later by a given role will receive.
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT SELECT ON TABLES TO app_readonly;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT SELECT, INSERT, UPDATE ON TABLES TO app_writer;Inspecting who can do what
Use has_table_privilege and has_function_privilege to test effective access. In psql, \dp shop.* lists table privileges and \ddp lists default privileges.
SELECT has_table_privilege('alice', 'shop.orders', 'SELECT') AS can_select;
SELECT has_function_privilege('app_readonly', 'shop.order_count()', 'EXECUTE');Predefined roles
Predefined roles are built-in group roles. In PostgreSQL 14 and later, pg_read_all_data grants read access to all tables and pg_monitor grants access to monitoring views. Use them when a role needs broad read access and you do not want to manage per-object grants.
CREATE ROLE reporting LOGIN PASSWORD 'change-me';
GRANT pg_read_all_data TO reporting;Designing a least-privilege setup
app_owner(NOLOGIN) owns the objects.app_readonlyandapp_writer(NOLOGIN) hold the privileges.- People and services get LOGIN roles that are members of one group.
Beyond GRANT: a first look at row-level security
Row-level security (RLS) filters which rows a role can see. The role still needs the table privilege. The table owner bypasses policies unless you add FORCE ROW LEVEL SECURITY.
ALTER TABLE shop.orders ENABLE ROW LEVEL SECURITY;
CREATE POLICY own_orders ON shop.orders
FOR SELECT
TO app_readonly
USING (sales_rep = current_user);Dropping roles with REASSIGN OWNED and DROP OWNED
A role that owns objects or holds privileges cannot be dropped directly. Reassign its objects first, then remove its privileges.
REASSIGN OWNED BY alice TO app_owner;
DROP OWNED BY alice;
DROP ROLE alice;Exercise
Exercise
Why does hasfunctionprivilege('appreadonly', 'shop.ordercount()', 'EXECUTE') return true after the GRANT above, even if PUBLIC had its EXECUTE right revoked?
Show answer
Because the GRANT gives app_readonly EXECUTE directly. A role can run a function when it has EXECUTE from PUBLIC or from its own grant, and this check tests the role's effective access.