@@ -39,13 +39,13 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
3939 </para>
4040
4141 <para>
42- A policy grants theability toSELECT, INSERT, UPDATE , orDELETE rows
43- which match the relevant policy expression. Existing table rows are
44- checked against the expression specified via USING, while new rows that
45- would be created via INSERT or UPDATE are checked against the expression
46- specified via WITH CHECK. When a USING expression returns true for a given
42+ A policy grants thepermission toselect, insert, update , ordelete rows
43+ that match the relevant policy expression. Existing table rows are
44+ checked against the expression specified via<literal> USING</literal> , while new rows that
45+ would be created via<literal> INSERT</literal> or<literal> UPDATE</literal> are checked against the expression
46+ specified via<literal> WITH CHECK</literal> . When a<literal> USING</literal> expression returns true for a given
4747 row then that row is visible to the user, while if a false or null is
48- returned then the row is not visible. When a WITH CHECK expression
48+ returned then the row is not visible. When a<literal> WITH CHECK</literal> expression
4949 returns true for a row then that row is added, while if a false or null is
5050 returned then an error occurs.
5151 </para>
@@ -56,20 +56,21 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
5656 in order to prevent the inadvertent exposure of the protected data to
5757 user-defined functions which might not be trustworthy. However,
5858 functions and operators marked by the system (or the system
59- administrator) as LEAKPROOF may be evaluated before policy
59+ administrator) as<literal> LEAKPROOF</literal> may be evaluated before policy
6060 expressions, as they are assumed to be trustworthy.
6161 </para>
6262
6363 <para>
64- For INSERT and UPDATE queries, WITH CHECK expressions are enforced after
65- BEFORE triggers are fired, and before any data modifications are made.
66- Thus a BEFORE ROW trigger may modify the data to be inserted, affecting
67- the result of the security policy check. WITH CHECK expressions are
68- enforced before any other constraints.
64+ For <command>INSERT</command> and <command>UPDATE</command> statements,
65+ <literal>WITH CHECK</literal> expressions are enforced after
66+ <literal>BEFORE</literal> triggers are fired, and before any data modifications are made.
67+ Thus a <literal>BEFORE ROW</literal> trigger may modify the data to be inserted, affecting
68+ the result of the security policy check. <literal>WITH CHECK</literal> expressions are
69+ enforced before any other constraints.
6970 </para>
7071
7172 <para>
72- Policy names are per-table, therefore one policy name can be used for many
73+ Policy names are per-table. Therefore, one policy name can be used for many
7374 different tables and have a definition for each table which is appropriate to
7475 that table.
7576 </para>
@@ -78,46 +79,19 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
7879 Policies can be applied for specific commands or for specific roles. The
7980 default for newly created policies is that they apply for all commands and
8081 roles, unless otherwise specified. If multiple policies apply to a given
81- query , they will be combined usingOR (although <literal>ON CONFLICT DO
82+ statement , they will be combined using<quote>or</quote> (although <literal>ON CONFLICT DO
8283 UPDATE</> and <literal>INSERT</> policies are not combined in this way, but
8384 rather enforced as noted at each stage of <literal>ON CONFLICT</> execution).
84- Further, for commands which can have both USING and WITH CHECK policies (ALL
85- and UPDATE), if no WITH CHECK policy is defined then the USING policy will be
86- used for both what rows are visible (normal USING case) and which rows will
87- be allowed to be added (WITH CHECK case).
8885 </para>
8986
9087 <para>
91- Note that while policies will be applied for explicit queries against tables
92- in the system, they are not applied when the system is performing internal
93- referential integrity checks or validating constraints. This means there are
94- indirect ways to determine that a given value exists. An example of this is
95- attempting to insert a duplicate value into a column which is the primary key
96- or has a unique constraint. If the insert fails then the user can infer that
97- the value already exists (this example assumes that the user is permitted by
98- policy to insert records which they are not allowed to see). Another example
99- is where a user is allowed to insert into a table which references another,
100- otherwise hidden table. Existence can be determined by the user inserting
101- values into the referencing table, where success would indicate that the
102- value exists in the referenced table. These issues can be addressed by
103- carefully crafting policies which prevent users from being able to insert,
104- delete, or update records at all which might possibly indicate a value they
105- are not otherwise able to see, or by using generated values (e.g.: surrogate
106- keys) instead.
88+ Further, for commands that can have both <literal>USING</literal>
89+ and <literal>WITH CHECK</literal> policies (<literal>ALL</literal>
90+ and <literal>UPDATE</literal>), if no <literal>WITH CHECK</literal> policy
91+ is defined, then the <literal>USING</literal> policy will be used for both
92+ what rows are visible (normal <literal>USING</literal> case) and which rows
93+ will be allowed to be added (<literal>WITH CHECK</literal> case).
10794 </para>
108-
109- <para>
110- Regarding how policy expressions interact with the user: as the expressions
111- are added to the user's query directly, they will be run with the rights of
112- the user running the overall query. Therefore, users who are using a given
113- policy must be able to access any tables or functions referenced in the
114- expression or they will simply receive a permission denied error when
115- attempting to query the table that has row-level security enabled. This does not change how views
116- work, however. As with normal queries and views, permission checks and
117- policies for the tables which are referenced by a view will use the view
118- owner's rights and any policies which apply to the view owner.
119- </para>
120-
12195 </refsect1>
12296
12397 <refsect1>
@@ -194,15 +168,14 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
194168 the table if row level security is enabled and only rows where the
195169 expression evaluates to true will be allowed. An error will be thrown
196170 if the expression evaluates to false or null for any of the records
197- inserted or any of the recordswhich result from the update.
171+ inserted or any of the recordsthat result from the update.
198172 </para>
199173 </listitem>
200174 </varlistentry>
201175
202176 </variablelist>
203- </refsect1>
204177
205- <refsect1 >
178+ <refsect2 >
206179 <title>Per-Command policies</title>
207180
208181 <variablelist>
@@ -216,20 +189,21 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
216189 <literal>ALL</literal> policy exists and more specific policies
217190 exist, then both the <literal>ALL</literal> policy and the more
218191 specific policy (or policies) will be combined using
219- <literal>OR</literal >, as usual for overlapping policies.
192+ <quote>or</quote >, as usual for overlapping policies.
220193 Additionally, <literal>ALL</literal> policies will be applied to
221194 both the selection side of a query and the modification side, using
222- the USING policy for both if only a USING policy has been defined.
223-
195+ the <literal>USING</literal> policy for both if only a <literal>USING</literal> policy has been defined.
196+ </para>
197+ <para>
224198 As an example, if an <literal>UPDATE</literal> is issued, then the
225199 <literal>ALL</literal> policy will be applicable to both what the
226200 <literal>UPDATE</literal> will be able to select out as rows to be
227- updated (with the USING expression being applied), and it will be
228- applied to rowswhich result from the <literal>UPDATE</literal>
201+ updated (with the<literal> USING</literal> expression being applied), and it will be
202+ applied to rowsthat result from the <literal>UPDATE</literal>
229203 statement, to check if they are permitted to be added to the table
230- (using the WITH CHECK expression, if defined, and the USING expression
231- otherwise). If an INSERT or UPDATE command attempts to add rows to
232- the tablewhich do not pass the <literal>ALL</literal> WITH CHECK
204+ (using the<literal> WITH CHECK</literal> expression, if defined, and the<literal> USING</literal> expression
205+ otherwise). If an<command> INSERT</command> or<command> UPDATE</command> command attempts to add rows to
206+ the tablethat do not pass the <literal>ALL</literal><literal> WITH CHECK</literal>
233207 expression, the entire command will be aborted. Note that if only a
234208 <literal>USING</literal> clause is specified then that clause will be
235209 used for both <literal>USING</literal> and
@@ -244,9 +218,9 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
244218 <para>
245219 Using <literal>SELECT</literal> for a policy means that it will apply
246220 to <literal>SELECT</literal> commands. The result is that only those
247- records from the relationwhich pass the <literal>SELECT</literal>
221+ records from the relationthat pass the <literal>SELECT</literal>
248222 policy will be returned, even if other records exist in the relation.
249- The <literal>SELECT</literal> policy only accepts the USING expression
223+ The <literal>SELECT</literal> policy only accepts the<literal> USING</literal> expression
250224 as it only ever applies in cases where records are being retrieved from
251225 the relation.
252226 </para>
@@ -258,18 +232,18 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
258232 <listitem>
259233 <para>
260234 Using <literal>INSERT</literal> for a policy means that it will apply
261- to <literal>INSERT</literal> commands. Rows being insertedwhich do
262- not pass this policy will result in a policy violationERROR and the
235+ to <literal>INSERT</literal> commands. Rows being insertedthat do
236+ not pass this policy will result in a policy violationerror, and the
263237 entire <literal>INSERT</literal> command will be aborted. The
264- <literal>INSERT</literal> policy only accepts the WITH CHECK expression
238+ <literal>INSERT</literal> policy only accepts the<literal> WITH CHECK</literal> expression
265239 as it only ever applies in cases where records are being added to the
266240 relation.
267241 </para>
268242 <para>
269243 Note that <literal>INSERT</literal> with <literal>ON CONFLICT DO
270244 UPDATE</literal> requires that any <literal>INSERT</literal> policy
271- WITH CHECK expression passes for any rows appended to the relation by
272- the INSERT path only.
245+ <literal> WITH CHECK</literal> expression passes for any rows appended to the relation by
246+ the<literal> INSERT</literal> path only.
273247 </para>
274248 </listitem>
275249 </varlistentry>
@@ -291,8 +265,8 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
291265 defines what rows are allowed to be added back into the relation
292266 (similar to the <literal>INSERT</literal> policy). Any rows whose
293267 resulting values do not pass the <literal>WITH CHECK</literal>
294- expression will cause anERROR and the entire command will be aborted.
295- Note that if only a <literal>USING</literal> clause is specified then
268+ expression will cause anerror, and the entire command will be aborted.
269+ Note that if only a <literal>USING</literal> clause is specified, then
296270 that clause will be used for both <literal>USING</literal> and
297271 <literal>WITH CHECK</literal> cases.
298272 </para>
@@ -304,11 +278,11 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
304278 <literal>UPDATE</literal> policy must always pass when the
305279 <literal>UPDATE</literal> path is taken. Any existing row that
306280 necessitates that the <literal>UPDATE</literal> path be taken must pass
307- the (UPDATE or ALL) <literal>USING</literal> qualifications (combined
308- using <literal>OR</literal >), which are always enforced as WITH CHECK
309- options in this context (the <literal>UPDATE</literal> path will
281+ the (<literal> UPDATE</literal> or<literal> ALL</literal> ) <literal>USING</literal> qualifications (combined
282+ using <quote>or</quote >), which are always enforced as<literal> WITH CHECK</literal>
283+ options in this context. (The <literal>UPDATE</literal> path will
310284 <emphasis>never</> be silently avoided; an error will be thrown
311- instead). Finally, the final row appended to the relation must pass
285+ instead.) Finally, the final row appended to the relation must pass
312286 any <literal>WITH CHECK</literal> options that a conventional
313287 <literal>UPDATE</literal> is required to pass.
314288 </para>
@@ -320,21 +294,22 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
320294 <listitem>
321295 <para>
322296 Using <literal>DELETE</literal> for a policy means that it will apply
323- to <literal>DELETE</literal> commands. Only rowswhich pass this
324- policy will be seen by a <literal>DELETE</literal> command.Rows may
325- be visible through a <literal>SELECT</literal>which are not seen by a
326- <literal>DELETE</literal>,as they do not pass the USING expression
327- for the <literal>DELETE</literal>, and rowswhich are not visible
328- through the <literal>SELECT</literal> policy may be deleted if they
329- pass the <literal>DELETE</literal> USING policy. The
330- <literal>DELETE</literal> policy only accepts the USING expression as
297+ to <literal>DELETE</literal> commands. Only rowsthat pass this
298+ policy will be seen by a <literal>DELETE</literal> command.There can be rows
299+ that are visible through a <literal>SELECT</literal>that are not seen by a
300+ <literal>DELETE</literal>,if they do not pass the<literal> USING</literal> expression
301+ for the <literal>DELETE</literal>. Conversely, there can be rowsthat are not visible
302+ through the <literal>SELECT</literal> policybut may be deleted if they
303+ pass the <literal>DELETE</literal><literal> USING</literal> policy. The
304+ <literal>DELETE</literal> policy only accepts the<literal> USING</literal> expression as
331305 it only ever applies in cases where records are being extracted from
332306 the relation for deletion.
333307 </para>
334308 </listitem>
335309 </varlistentry>
336310
337311 </variablelist>
312+ </refsect2>
338313 </refsect1>
339314
340315 <refsect1>
@@ -345,11 +320,35 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
345320 </para>
346321
347322 <para>
348- In order to maintain <firstterm>referential integrity</firstterm> between
349- two related tables, policies are not applied when the system performs
350- checks on foreign key constraints.
323+ Note that while policies will be applied for explicit queries against tables
324+ in the system, they are not applied when the system is performing internal
325+ referential integrity checks or validating constraints. This means there are
326+ indirect ways to determine that a given value exists. An example of this is
327+ attempting to insert a duplicate value into a column which is the primary key
328+ or has a unique constraint. If the insert fails then the user can infer that
329+ the value already exists. (This example assumes that the user is permitted by
330+ policy to insert records which they are not allowed to see.) Another example
331+ is where a user is allowed to insert into a table which references another,
332+ otherwise hidden table. Existence can be determined by the user inserting
333+ values into the referencing table, where success would indicate that the
334+ value exists in the referenced table. These issues can be addressed by
335+ carefully crafting policies that prevent users from being able to insert,
336+ delete, or update records at all which might possibly indicate a value they
337+ are not otherwise able to see, or by using generated values (e.g., surrogate
338+ keys) instead.
351339 </para>
352340
341+ <para>
342+ Regarding how policy expressions interact with the user: as the expressions
343+ are added to the user's query directly, they will be run with the rights of
344+ the user running the overall query. Therefore, users who are using a given
345+ policy must be able to access any tables or functions referenced in the
346+ expression or they will simply receive a permission denied error when
347+ attempting to query the table that has row-level security enabled. This does not change how views
348+ work, however. As with normal queries and views, permission checks and
349+ policies for the tables which are referenced by a view will use the view
350+ owner's rights and any policies which apply to the view owner.
351+ </para>
353352 </refsect1>
354353
355354 <refsect1>