3
Row level security policy
Determines the visibility and mutability of table rows
the predicate is calculated for each row using the initiating user’s privileges
the client can only access the rows for which the predicate is true
Predicate for existing rows (USING)
used by SELECT, UPDATE, DELETE commands
policy violation does not trigger an error
(unless the row_security parameter is unset)
Predicate for new rows (WITH CHECK)
used by INSERT, UPDATE commands
if omitted, the first predicate is used
policy violation triggers an error
Row level security (RLS) policies allow the system administrator to control
user access to a table at the level of individual rows. This mechanism is also
known as Fine-Grained Access Control.
It it a supplemental tool, as the role must have necessary privileges to
access the table in the first place.
Row level security policies determine if a certain user should be able to
read or modify a certain row by calculating one of two predicates (binary
expressions) for each of the queried rows. The predicate result determines
if the user would be allowed to access the row.
The first predicate is used for existing rows. It is used by the commands
SELECT, UPDATE, and DELETE. If the predicate for a given row does not
return true (that is, the function returns false or NULL), the row isn't included
into the client's result set. As a gross oversimplification, you can imagine
that the predicate is simply appended to the WHERE clause of the query.
In reality, however, it is more complicated.
If the row_security parameter value is set to off, a false predicate for even a
single row will result in an query error. This is useful when making a logical
backup to ensure that all rows of all tables got in.
The second predicate determines the visibility of new rows. It is used by the
INSERT and UPDATE commands and always throws an error if the policy is
violated.