@@ -137,8 +137,6 @@ CREATE VIEW [ <replaceable>schema</replaceable> . ] <replaceable>view_name</repl
137137 This parameter may be either <literal>local</literal> or
138138 <literal>cascaded</literal>, and is equivalent to specifying
139139 <literal>WITH [ CASCADED | LOCAL ] CHECK OPTION</literal> (see below).
140- This option can be changed on existing views using <link
141- linkend="sql-alterview"><command>ALTER VIEW</command></link>.
142140 </para>
143141 </listitem>
144142 </varlistentry>
@@ -152,7 +150,22 @@ CREATE VIEW [ <replaceable>schema</replaceable> . ] <replaceable>view_name</repl
152150 </para>
153151 </listitem>
154152 </varlistentry>
155- </variablelist></para>
153+
154+ <varlistentry>
155+ <term><literal>security_invoker</literal> (<type>boolean</type>)</term>
156+ <listitem>
157+ <para>
158+ This option causes the underlying base relations to be checked
159+ against the privileges of the user of the view rather than the view
160+ owner. See the notes below for full details.
161+ </para>
162+ </listitem>
163+ </varlistentry>
164+ </variablelist>
165+
166+ All of the above options can be changed on existing views using <link
167+ linkend="sql-alterview"><command>ALTER VIEW</command></link>.
168+ </para>
156169 </listitem>
157170 </varlistentry>
158171
@@ -265,18 +278,74 @@ CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
265278 </para>
266279
267280 <para>
268- Access to tables referenced in the view is determined by permissions of
269- the view owner. In some cases, this can be used to provide secure but
270- restricted access to the underlying tables. However, not all views are
271- secure against tampering; see <xref linkend="rules-privileges"/> for
272- details. Functions called in the view are treated the same as if they had
273- been called directly from the query using the view. Therefore the user of
281+ By default, access to the underlying base relations referenced in the view
282+ is determined by the permissions of the view owner. In some cases, this
283+ can be used to provide secure but restricted access to the underlying
284+ tables. However, not all views are secure against tampering; see <xref
285+ linkend="rules-privileges"/> for details.
286+ </para>
287+
288+ <para>
289+ If the view has the <literal>security_invoker</literal> property set to
290+ <literal>true</literal>, access to the underlying base relations is
291+ determined by the permissions of the user executing the query, rather than
292+ the view owner. Thus, the user of a security invoker view must have the
293+ relevant permissions on the view and its underlying base relations.
294+ </para>
295+
296+ <para>
297+ If any of the underlying base relations is a security invoker view, it
298+ will be treated as if it had been accessed directly from the original
299+ query. Thus, a security invoker view will always check its underlying
300+ base relations using the permissions of the current user, even if it is
301+ accessed from a view without the <literal>security_invoker</literal>
302+ property.
303+ </para>
304+
305+ <para>
306+ If any of the underlying base relations has
307+ <link linkend="ddl-rowsecurity">row-level security</link> enabled, then
308+ by default, the row-level security policies of the view owner are applied,
309+ and access to any additional relations referred to by those policies is
310+ determined by the permissions of the view owner. However, if the view has
311+ <literal>security_invoker</literal> set to <literal>true</literal>, then
312+ the policies and permissions of the invoking user are used instead, as if
313+ the base relations had been referenced directly from the query using the
314+ view.
315+ </para>
316+
317+ <para>
318+ Functions called in the view are treated the same as if they had been
319+ called directly from the query using the view. Therefore, the user of
274320 a view must have permissions to call all functions used by the view.
321+ Functions in the view are executed with the privileges of the user
322+ executing the query or the function owner, depending on whether the
323+ functions are defined as <literal>SECURITY INVOKER</literal> or
324+ <literal>SECURITY DEFINER</literal>. Thus, for example, calling
325+ <literal>CURRENT_USER</literal> directly in a view will always return the
326+ invoking user, not the view owner. This is not affected by the view's
327+ <literal>security_invoker</literal> setting, and so a view with
328+ <literal>security_invoker</literal> set to <literal>false</literal> is
329+ <emphasis>not</emphasis> equivalent to a
330+ <literal>SECURITY DEFINER</literal> function and those concepts should not
331+ be confused.
332+ </para>
333+
334+ <para>
335+ The user creating or replacing a view must have <literal>USAGE</literal>
336+ privileges on any schemas referred to in the view query, in order to look
337+ up the referenced objects in those schemas. Note, however, that this
338+ lookup only happens when the view is created or replaced. Therefore, the
339+ user of the view only requires the <literal>USAGE</literal> privilege on
340+ the schema containing the view, not on the schemas referred to in the view
341+ query, even for a security invoker view.
275342 </para>
276343
277344 <para>
278- When <command>CREATE OR REPLACE VIEW</command> is used on an
279- existing view, only the view's defining SELECT rule is changed.
345+ When <command>CREATE OR REPLACE VIEW</command> is used on an existing
346+ view, only the view's defining SELECT rule, plus any
347+ <literal>WITH ( ... )</literal> parameters and its
348+ <literal>CHECK OPTION</literal> are changed.
280349 Other view properties, including ownership, permissions, and non-SELECT
281350 rules, remain unchanged. You must own the view
282351 to replace it (this includes being a member of the owning role).
@@ -387,10 +456,13 @@ CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
387456 <para>
388457 Note that the user performing the insert, update or delete on the view
389458 must have the corresponding insert, update or delete privilege on the
390- view. In addition the view's owner must have the relevant privileges on
391- the underlying base relations, but the user performing the update does
392- not need any permissions on the underlying base relations (see
393- <xref linkend="rules-privileges"/>).
459+ view. In addition, by default, the view's owner must have the relevant
460+ privileges on the underlying base relations, whereas the user performing
461+ the update does not need any permissions on the underlying base relations
462+ (see <xref linkend="rules-privileges"/>). However, if the view has
463+ <literal>security_invoker</literal> set to <literal>true</literal>, the
464+ user performing the update, rather than the view owner, must have the
465+ relevant privileges on the underlying base relations.
394466 </para>
395467 </refsect2>
396468 </refsect1>
@@ -486,7 +558,8 @@ UNION ALL
486558 <command>CREATE OR REPLACE VIEW</command> is a
487559 <productname>PostgreSQL</productname> language extension.
488560 So is the concept of a temporary view.
489- The <literal>WITH ( ... )</literal> clause is an extension as well.
561+ The <literal>WITH ( ... )</literal> clause is an extension as well, as are
562+ security barrier views and security invoker views.
490563 </para>
491564 </refsect1>
492565
0 commit comments