]> begriffs open source - ai-pg/blob - full-docs/txt/sql-createpolicy.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / sql-createpolicy.txt
1
2 CREATE POLICY
3
4    CREATE POLICY — define a new row-level security policy for a table
5
6 Synopsis
7
8 CREATE POLICY name ON table_name
9     [ AS { PERMISSIVE | RESTRICTIVE } ]
10     [ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
11     [ TO { role_name | PUBLIC | CURRENT_ROLE | CURRENT_USER | SESSION_USER } [,
12 ...] ]
13     [ USING ( using_expression ) ]
14     [ WITH CHECK ( check_expression ) ]
15
16 Description
17
18    The CREATE POLICY command defines a new row-level security policy for a
19    table. Note that row-level security must be enabled on the table (using
20    ALTER TABLE ... ENABLE ROW LEVEL SECURITY) in order for created
21    policies to be applied.
22
23    A policy grants the permission to select, insert, update, or delete
24    rows that match the relevant policy expression. Existing table rows are
25    checked against the expression specified in USING, while new rows that
26    would be created via INSERT or UPDATE are checked against the
27    expression specified in WITH CHECK. When a USING expression returns
28    true for a given row then that row is visible to the user, while if
29    false or null is returned then the row is not visible. When a WITH
30    CHECK expression returns true for a row then that row is inserted or
31    updated, while if false or null is returned then an error occurs.
32
33    For INSERT, UPDATE, and MERGE statements, WITH CHECK expressions are
34    enforced after BEFORE triggers are fired, and before any actual data
35    modifications are made. Thus a BEFORE ROW trigger may modify the data
36    to be inserted, affecting the result of the security policy check. WITH
37    CHECK expressions are enforced before any other constraints.
38
39    Policy names are per-table. Therefore, one policy name can be used for
40    many different tables and have a definition for each table which is
41    appropriate to that table.
42
43    Policies can be applied for specific commands or for specific roles.
44    The default for newly created policies is that they apply for all
45    commands and roles, unless otherwise specified. Multiple policies may
46    apply to a single command; see below for more details. Table 300
47    summarizes how the different types of policy apply to specific
48    commands.
49
50    For policies that can have both USING and WITH CHECK expressions (ALL
51    and UPDATE), if no WITH CHECK expression is defined, then the USING
52    expression will be used both to determine which rows are visible
53    (normal USING case) and which new rows will be allowed to be added
54    (WITH CHECK case).
55
56    If row-level security is enabled for a table, but no applicable
57    policies exist, a “default deny” policy is assumed, so that no rows
58    will be visible or updatable.
59
60 Parameters
61
62    name
63           The name of the policy to be created. This must be distinct from
64           the name of any other policy for the table.
65
66    table_name
67           The name (optionally schema-qualified) of the table the policy
68           applies to.
69
70    PERMISSIVE
71           Specify that the policy is to be created as a permissive policy.
72           All permissive policies which are applicable to a given query
73           will be combined together using the Boolean “OR” operator. By
74           creating permissive policies, administrators can add to the set
75           of records which can be accessed. Policies are permissive by
76           default.
77
78    RESTRICTIVE
79           Specify that the policy is to be created as a restrictive
80           policy. All restrictive policies which are applicable to a given
81           query will be combined together using the Boolean “AND”
82           operator. By creating restrictive policies, administrators can
83           reduce the set of records which can be accessed as all
84           restrictive policies must be passed for each record.
85
86           Note that there needs to be at least one permissive policy to
87           grant access to records before restrictive policies can be
88           usefully used to reduce that access. If only restrictive
89           policies exist, then no records will be accessible. When a mix
90           of permissive and restrictive policies are present, a record is
91           only accessible if at least one of the permissive policies
92           passes, in addition to all the restrictive policies.
93
94    command
95           The command to which the policy applies. Valid options are ALL,
96           SELECT, INSERT, UPDATE, and DELETE. ALL is the default. See
97           below for specifics regarding how these are applied.
98
99    role_name
100           The role(s) to which the policy is to be applied. The default is
101           PUBLIC, which will apply the policy to all roles.
102
103    using_expression
104           Any SQL conditional expression (returning boolean). The
105           conditional expression cannot contain any aggregate or window
106           functions. This expression will be added to queries that refer
107           to the table if row-level security is enabled. Rows for which
108           the expression returns true will be visible. Any rows for which
109           the expression returns false or null will not be visible to the
110           user (in a SELECT), and will not be available for modification
111           (in an UPDATE or DELETE). Such rows are silently suppressed; no
112           error is reported.
113
114    check_expression
115           Any SQL conditional expression (returning boolean). The
116           conditional expression cannot contain any aggregate or window
117           functions. This expression will be used in INSERT and UPDATE
118           queries against the table if row-level security is enabled. Only
119           rows for which the expression evaluates to true will be allowed.
120           An error will be thrown if the expression evaluates to false or
121           null for any of the records inserted or any of the records that
122           result from the update. Note that the check_expression is
123           evaluated against the proposed new contents of the row, not the
124           original contents.
125
126 Per-Command Policies
127
128    ALL #
129           Using ALL for a policy means that it will apply to all commands,
130           regardless of the type of command. If an ALL policy exists and
131           more specific policies exist, then both the ALL policy and the
132           more specific policy (or policies) will be applied.
133           Additionally, ALL policies will be applied to both the selection
134           side of a query and the modification side, using the USING
135           expression for both cases if only a USING expression has been
136           defined.
137
138           As an example, if an UPDATE is issued, then the ALL policy will
139           be applicable both to what the UPDATE will be able to select as
140           rows to be updated (applying the USING expression), and to the
141           resulting updated rows, to check if they are permitted to be
142           added to the table (applying the WITH CHECK expression, if
143           defined, and the USING expression otherwise). If an INSERT or
144           UPDATE command attempts to add rows to the table that do not
145           pass the ALL policy's WITH CHECK expression, the entire command
146           will be aborted.
147
148    SELECT #
149           Using SELECT for a policy means that it will apply to SELECT
150           queries and whenever SELECT permissions are required on the
151           relation the policy is defined for. The result is that only
152           those records from the relation that pass the SELECT policy will
153           be returned during a SELECT query, and that queries that require
154           SELECT permissions, such as UPDATE, will also only see those
155           records that are allowed by the SELECT policy. A SELECT policy
156           cannot have a WITH CHECK expression, as it only applies in cases
157           where records are being retrieved from the relation.
158
159    INSERT #
160           Using INSERT for a policy means that it will apply to INSERT
161           commands and MERGE commands that contain INSERT actions. Rows
162           being inserted that do not pass this policy will result in a
163           policy violation error, and the entire INSERT command will be
164           aborted. An INSERT policy cannot have a USING expression, as it
165           only applies in cases where records are being added to the
166           relation.
167
168           Note that INSERT with ON CONFLICT DO UPDATE checks INSERT
169           policies' WITH CHECK expressions only for rows appended to the
170           relation by the INSERT path.
171
172    UPDATE #
173           Using UPDATE for a policy means that it will apply to UPDATE,
174           SELECT FOR UPDATE and SELECT FOR SHARE commands, as well as
175           auxiliary ON CONFLICT DO UPDATE clauses of INSERT commands.
176           MERGE commands containing UPDATE actions are affected as well.
177           Since UPDATE involves pulling an existing record and replacing
178           it with a new modified record, UPDATE policies accept both a
179           USING expression and a WITH CHECK expression. The USING
180           expression determines which records the UPDATE command will see
181           to operate against, while the WITH CHECK expression defines
182           which modified rows are allowed to be stored back into the
183           relation.
184
185           Any rows whose updated values do not pass the WITH CHECK
186           expression will cause an error, and the entire command will be
187           aborted. If only a USING clause is specified, then that clause
188           will be used for both USING and WITH CHECK cases.
189
190           Typically an UPDATE command also needs to read data from columns
191           in the relation being updated (e.g., in a WHERE clause or a
192           RETURNING clause, or in an expression on the right hand side of
193           the SET clause). In this case, SELECT rights are also required
194           on the relation being updated, and the appropriate SELECT or ALL
195           policies will be applied in addition to the UPDATE policies.
196           Thus the user must have access to the row(s) being updated
197           through a SELECT or ALL policy in addition to being granted
198           permission to update the row(s) via an UPDATE or ALL policy.
199
200           When an INSERT command has an auxiliary ON CONFLICT DO UPDATE
201           clause, if the UPDATE path is taken, the row to be updated is
202           first checked against the USING expressions of any UPDATE
203           policies, and then the new updated row is checked against the
204           WITH CHECK expressions. Note, however, that unlike a standalone
205           UPDATE command, if the existing row does not pass the USING
206           expressions, an error will be thrown (the UPDATE path will never
207           be silently avoided).
208
209    DELETE #
210           Using DELETE for a policy means that it will apply to DELETE
211           commands. Only rows that pass this policy will be seen by a
212           DELETE command. There can be rows that are visible through a
213           SELECT that are not available for deletion, if they do not pass
214           the USING expression for the DELETE policy.
215
216           In most cases a DELETE command also needs to read data from
217           columns in the relation that it is deleting from (e.g., in a
218           WHERE clause or a RETURNING clause). In this case, SELECT rights
219           are also required on the relation, and the appropriate SELECT or
220           ALL policies will be applied in addition to the DELETE policies.
221           Thus the user must have access to the row(s) being deleted
222           through a SELECT or ALL policy in addition to being granted
223           permission to delete the row(s) via a DELETE or ALL policy.
224
225           A DELETE policy cannot have a WITH CHECK expression, as it only
226           applies in cases where records are being deleted from the
227           relation, so that there is no new row to check.
228
229    Table 300. Policies Applied by Command Type
230    Command SELECT/ALL policy INSERT/ALL policy UPDATE/ALL policy
231    DELETE/ALL policy
232    USING expression WITH CHECK expression USING expression WITH CHECK
233    expression USING expression
234    SELECT Existing row — — — —
235    SELECT FOR UPDATE/SHARE Existing row — Existing row — —
236    INSERT / MERGE ... THEN INSERT — New row — — —
237    INSERT ... RETURNING New row ^[a] New row — — —
238    UPDATE / MERGE ... THEN UPDATE Existing & new rows ^[a] — Existing row
239    New row —
240    DELETE Existing row ^[a] — — — Existing row
241    ON CONFLICT DO UPDATE Existing & new rows — Existing row New row —
242
243    ^[a] If read access is required to the existing or new row (for
244    example, a WHERE or RETURNING clause that refers to columns from the
245    relation).
246
247 Application of Multiple Policies
248
249    When multiple policies of different command types apply to the same
250    command (for example, SELECT and UPDATE policies applied to an UPDATE
251    command), then the user must have both types of permissions (for
252    example, permission to select rows from the relation as well as
253    permission to update them). Thus the expressions for one type of policy
254    are combined with the expressions for the other type of policy using
255    the AND operator.
256
257    When multiple policies of the same command type apply to the same
258    command, then there must be at least one PERMISSIVE policy granting
259    access to the relation, and all of the RESTRICTIVE policies must pass.
260    Thus all the PERMISSIVE policy expressions are combined using OR, all
261    the RESTRICTIVE policy expressions are combined using AND, and the
262    results are combined using AND. If there are no PERMISSIVE policies,
263    then access is denied.
264
265    Note that, for the purposes of combining multiple policies, ALL
266    policies are treated as having the same type as whichever other type of
267    policy is being applied.
268
269    For example, in an UPDATE command requiring both SELECT and UPDATE
270    permissions, if there are multiple applicable policies of each type,
271    they will be combined as follows:
272 expression from RESTRICTIVE SELECT/ALL policy 1
273 AND
274 expression from RESTRICTIVE SELECT/ALL policy 2
275 AND
276 ...
277 AND
278 (
279   expression from PERMISSIVE SELECT/ALL policy 1
280   OR
281   expression from PERMISSIVE SELECT/ALL policy 2
282   OR
283   ...
284 )
285 AND
286 expression from RESTRICTIVE UPDATE/ALL policy 1
287 AND
288 expression from RESTRICTIVE UPDATE/ALL policy 2
289 AND
290 ...
291 AND
292 (
293   expression from PERMISSIVE UPDATE/ALL policy 1
294   OR
295   expression from PERMISSIVE UPDATE/ALL policy 2
296   OR
297   ...
298 )
299
300 Notes
301
302    You must be the owner of a table to create or change policies for it.
303
304    While policies will be applied for explicit queries against tables in
305    the database, they are not applied when the system is performing
306    internal referential integrity checks or validating constraints. This
307    means there are indirect ways to determine that a given value exists.
308    An example of this is attempting to insert a duplicate value into a
309    column that is a primary key or has a unique constraint. If the insert
310    fails then the user can infer that the value already exists. (This
311    example assumes that the user is permitted by policy to insert records
312    which they are not allowed to see.) Another example is where a user is
313    allowed to insert into a table which references another, otherwise
314    hidden table. Existence can be determined by the user inserting values
315    into the referencing table, where success would indicate that the value
316    exists in the referenced table. These issues can be addressed by
317    carefully crafting policies to prevent users from being able to insert,
318    delete, or update records at all which might possibly indicate a value
319    they are not otherwise able to see, or by using generated values (e.g.,
320    surrogate keys) instead of keys with external meanings.
321
322    Generally, the system will enforce filter conditions imposed using
323    security policies prior to qualifications that appear in user queries,
324    in order to prevent inadvertent exposure of the protected data to
325    user-defined functions which might not be trustworthy. However,
326    functions and operators marked by the system (or the system
327    administrator) as LEAKPROOF may be evaluated before policy expressions,
328    as they are assumed to be trustworthy.
329
330    Since policy expressions are added to the user's query directly, they
331    will be run with the rights of the user running the overall query.
332    Therefore, users who are using a given policy must be able to access
333    any tables or functions referenced in the expression or they will
334    simply receive a permission denied error when attempting to query the
335    table that has row-level security enabled. This does not change how
336    views work, however. As with normal queries and views, permission
337    checks and policies for the tables which are referenced by a view will
338    use the view owner's rights and any policies which apply to the view
339    owner, except if the view is defined using the security_invoker option
340    (see CREATE VIEW).
341
342    No separate policy exists for MERGE. Instead, the policies defined for
343    SELECT, INSERT, UPDATE, and DELETE are applied while executing MERGE,
344    depending on the actions that are performed.
345
346    Additional discussion and practical examples can be found in
347    Section 5.9.
348
349 Compatibility
350
351    CREATE POLICY is a PostgreSQL extension.
352
353 See Also
354
355    ALTER POLICY, DROP POLICY, ALTER TABLE