Sub topic 10
Sequences and Functions
Assume you can connect to PostgreSQL and know how roles, schemas, and tables work. This chapter focuses on two object types that often need separate privileges: sequences and functions.
Sequence privileges
A sequence generates ordered values, commonly for an identity column or a manually generated key. A table privilege does not automatically grant access to the sequence behind an identity column.
PostgreSQL provides three sequence privileges:
| Privilege | Allows |
|---|---|
| `USAGE` | Calling `nextval` and `currval` |
| `SELECT` | Calling `currval` |
| `UPDATE` | Calling `nextval` and `setval` |
An application that inserts rows into a table with an identity column usually needs USAGE on the related sequence. Granting INSERT on the table alone may therefore be insufficient.
CREATE ROLE app_writer NOLOGIN;
CREATE ROLE alice LOGIN PASSWORD 'change-me';
GRANT app_writer TO alice;
CREATE SCHEMA shop;
CREATE TABLE shop.orders (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
customer text NOT NULL,
total numeric(10, 2) NOT NULL
);
GRANT USAGE ON SCHEMA shop TO app_writer;
GRANT INSERT, SELECT ON shop.orders TO app_writer;
-- The identity sequence has a system-generated name in this example.
GRANT USAGE ON SEQUENCE shop.orders_id_seq TO app_writer;For manually created sequences, use the same pattern:
CREATE SEQUENCE shop.invoice_number_seq;
GRANT USAGE ON SEQUENCE shop.invoice_number_seq TO app_writer;
SELECT nextval('shop.invoice_number_seq');The schema privilege still matters. A role generally needs USAGE on the containing schema before it can access objects in that schema.
Function privileges
A function is executable code stored in the database. The EXECUTE privilege controls who may call it. New functions normally grant EXECUTE to PUBLIC, which means every role can call them unless you revoke that privilege.
Function identity includes its argument types. This matters when functions are overloaded, because two functions with the same name but different arguments are separate objects.
CREATE FUNCTION shop.add_order(
p_customer text,
p_total numeric
)
RETURNS bigint
LANGUAGE sql
AS $$
INSERT INTO shop.orders (customer, total)
VALUES (p_customer, p_total)
RETURNING id;
$$;
REVOKE ALL ON FUNCTION shop.add_order(text, numeric) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION shop.add_order(text, numeric) TO app_writer;The function's SQL operations still require suitable privileges. Granting EXECUTE does not automatically grant access to tables or sequences used by the function.
A SECURITY DEFINER function runs with the privileges of its owner instead of its caller. It can provide controlled access to a protected operation, but its owner and search_path must be configured carefully.
Future sequences and functions
ALTER DEFAULT PRIVILEGES affects objects created later by a particular role. It does not change existing objects.
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT USAGE ON SEQUENCES TO app_writer;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT EXECUTE ON FUNCTIONS TO app_writer;The role named after FOR ROLE must be the role that creates the future objects.
Exercise
Why can a role have INSERT on shop.orders and still fail to insert a row whose id is generated by a sequence?
Show answer
INSERT on the table does not grant sequence privileges. The role also needs USAGE on the sequence, plus USAGE on the schema.