Function security context and least privilege
Overview
PostgreSQL SECURITY INVOKER uses the caller’s privileges; SECURITY DEFINER executes with the function owner’s privileges. Use invoker by default and restrict definer functions to carefully reviewed operations. A function that returns only public fields is not by itself an authorization boundary. SQLite callbacks execute inside the application process and have no PostgreSQL role context.
First example
SELECT id,fn_label(name,price) FROM fn_product ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1, "Pen: 100 cents" ], [ 2, "Paper: 200 cents" ], [ 3, "Tape: 300 cents" ]]How it works
The first query calls a pure label callback that returns only a name and price. The second proves that cost remains readable from the base table in an unrestricted SQLite connection. A separate validator test installs a connection authorizer that denies reads of the cost column; it is not a database role system. PostgreSQL definer functions need a controlled owner, trusted search_path (with pg_temp last), schema-qualified objects, restricted EXECUTE grants and no unsafe dynamic SQL. Revoke the default PUBLIC EXECUTE privilege and grant the intended role in the same transaction as creation. Validate arguments and return only necessary information. The separate PostgreSQL lab demonstrates a constrained read-only interface; it is not executed here. Do not register callbacks that read arbitrary files or invoke commands for untrusted SQL.
Practical example
SELECT id,cost FROM fn_product ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1, 60 ], [ 2, 120 ], [ 3, 180 ]]Practice and mistakes
Practice: explain why fn_label(name,price) cannot prevent SELECT cost. Hint 1: projection differs from authorization. Hint 2: inspect the role lab and the cost-denial edge test. Expect unrestricted SELECT to succeed in the default SQLite fixture. Mistake: giving a privileged function to PUBLIC or trusting an attacker-writable search path.