@@ -127,17 +127,6 @@ CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW <replaceable class="PARAMETER">n
127127 <refsect1>
128128 <title>Notes</title>
129129
130- <para>
131- Currently, views are read only: the system will not allow an insert,
132- update, or delete on a view. You can get the effect of an updatable
133- view by creating <literal>INSTEAD</> triggers on the view, which
134- must convert attempted inserts, etc. on the view into
135- appropriate actions on other tables. For more information see
136- <xref linkend="sql-createtrigger">. Another possibility is to create
137- rules (see <xref linkend="sql-createrule">), but in practice triggers
138- are easier to understand and use correctly.
139- </para>
140-
141130 <para>
142131 Use the <xref linkend="sql-dropview">
143132 statement to drop views.
@@ -175,6 +164,105 @@ CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
175164 to replace it (this includes being a member of the owning role).
176165 </para>
177166
167+ <refsect2 id="SQL-CREATEVIEW-updatable-views">
168+ <title id="SQL-CREATEVIEW-updatable-views-title">Updatable Views</title>
169+
170+ <indexterm zone="sql-createview-updatable-views">
171+ <primary>updatable views</primary>
172+ </indexterm>
173+
174+ <para>
175+ Simple views are automatically updatable: the system will allow
176+ <command>INSERT</>, <command>UPDATE</> and <command>DELETE</> statements
177+ to be used on the view in the same way as on a regular table. A view is
178+ automatically updatable if it satisfies all of the following conditions:
179+
180+ <itemizedlist>
181+ <listitem>
182+ <para>
183+ The view must have exactly one entry in its <literal>FROM</> list,
184+ which must be a table or another updatable view.
185+ </para>
186+ </listitem>
187+
188+ <listitem>
189+ <para>
190+ The view definition must not contain <literal>WITH</>,
191+ <literal>DISTINCT</>, <literal>GROUP BY</>, <literal>HAVING</>,
192+ <literal>LIMIT</>, or <literal>OFFSET</> clauses at the top level.
193+ </para>
194+ </listitem>
195+
196+ <listitem>
197+ <para>
198+ The view definition must not contain set operations (<literal>UNION</>,
199+ <literal>INTERSECT</> or <literal>EXCEPT</>) at the top level.
200+ </para>
201+ </listitem>
202+
203+ <listitem>
204+ <para>
205+ All columns in the view's select list must be simple references to
206+ columns of the underlying relation. They cannot be expressions,
207+ literals or functions. System columns cannot be referenced, either.
208+ </para>
209+ </listitem>
210+
211+ <listitem>
212+ <para>
213+ No column of the underlying relation can appear more than once in
214+ the view's select list.
215+ </para>
216+ </listitem>
217+
218+ <listitem>
219+ <para>
220+ The view must not have the <literal>security_barrier</> property.
221+ </para>
222+ </listitem>
223+ </itemizedlist>
224+ </para>
225+
226+ <para>
227+ If the view is automatically updatable the system will convert any
228+ <command>INSERT</>, <command>UPDATE</> or <command>DELETE</> statement
229+ on the view into the corresponding statement on the underlying base
230+ relation.
231+ </para>
232+
233+ <para>
234+ If an automatically updatable view contains a <literal>WHERE</>
235+ condition, the condition restricts which rows of the base relation are
236+ available to be modified by <command>UPDATE</> and <command>DELETE</>
237+ statements on the view. However, an <command>UPDATE</> is allowed to
238+ change a row so that it no longer satisfies the <literal>WHERE</>
239+ condition, and thus is no longer visible through the view. Similarly,
240+ an <command>INSERT</> command can potentially insert base-relation rows
241+ that do not satisfy the <literal>WHERE</> condition and thus are not
242+ visible through the view.
243+ </para>
244+
245+ <para>
246+ A more complex view that does not satisfy all these conditions is
247+ read-only by default: the system will not allow an insert, update, or
248+ delete on the view. You can get the effect of an updatable view by
249+ creating <literal>INSTEAD OF</> triggers on the view, which must
250+ convert attempted inserts, etc. on the view into appropriate actions
251+ on other tables. For more information see <xref
252+ linkend="sql-createtrigger">. Another possibility is to create rules
253+ (see <xref linkend="sql-createrule">), but in practice triggers are
254+ easier to understand and use correctly.
255+ </para>
256+
257+ <para>
258+ Note that the user performing the insert, update or delete on the view
259+ must have the corresponding insert, update or delete privilege on the
260+ view. In addition the view's owner must have the relevant privileges on
261+ the underlying base relations, but the user performing the update does
262+ not need any permissions on the underlying base relations (see
263+ <xref linkend="rules-privileges">).
264+ </para>
265+ </refsect2>
178266 </refsect1>
179267
180268 <refsect1>
@@ -217,11 +305,15 @@ CREATE VIEW <replaceable class="parameter">name</replaceable> [ ( <replaceable c
217305 <term><literal>CHECK OPTION</literal></term>
218306 <listitem>
219307 <para>
220- This option has to do with updatable views. All
221- <command>INSERT</> and <command>UPDATE</> commands on the view
222- will be checked to ensure data satisfy the view-defining
223- condition (that is, the new data would be visible through the
224- view). If they do not, the update will be rejected.
308+ This option controls the behavior of automatically updatable views.
309+ When given, <command>INSERT</> and <command>UPDATE</> commands on
310+ the view will be checked to ensure new rows satisfy the
311+ view-defining condition (that is, the new rows would be visible
312+ through the view). If they do not, the update will be rejected.
313+ Without <literal>CHECK OPTION</literal>, <command>INSERT</> and
314+ <command>UPDATE</> commands on the view are allowed to create rows
315+ that are not visible through the view. (The latter behavior is the
316+ only one currently provided by <productname>PostgreSQL</>.)
225317 </para>
226318 </listitem>
227319 </varlistentry>
@@ -252,6 +344,7 @@ CREATE VIEW <replaceable class="parameter">name</replaceable> [ ( <replaceable c
252344 <command>CREATE OR REPLACE VIEW</command> is a
253345 <productname>PostgreSQL</productname> language extension.
254346 So is the concept of a temporary view.
347+ The <literal>WITH</> clause is an extension as well.
255348 </para>
256349 </refsect1>
257350
0 commit comments