summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man7/CREATE_VIEW.7
blob: b40752e476ae712bb5b9e05e996be8e37fad9618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
'\" t
.\"     Title: CREATE VIEW
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 2023
.\"    Manual: PostgreSQL 15.5 Documentation
.\"    Source: PostgreSQL 15.5
.\"  Language: English
.\"
.TH "CREATE VIEW" "7" "2023" "PostgreSQL 15.5" "PostgreSQL 15.5 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
CREATE_VIEW \- define a new view
.SH "SYNOPSIS"
.sp
.nf
CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW \fIname\fR [ ( \fIcolumn_name\fR [, \&.\&.\&.] ) ]
    [ WITH ( \fIview_option_name\fR [= \fIview_option_value\fR] [, \&.\&.\&. ] ) ]
    AS \fIquery\fR
    [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
.fi
.SH "DESCRIPTION"
.PP
\fBCREATE VIEW\fR
defines a view of a query\&. The view is not physically materialized\&. Instead, the query is run every time the view is referenced in a query\&.
.PP
\fBCREATE OR REPLACE VIEW\fR
is similar, but if a view of the same name already exists, it is replaced\&. The new query must generate the same columns that were generated by the existing view query (that is, the same column names in the same order and with the same data types), but it may add additional columns to the end of the list\&. The calculations giving rise to the output columns may be completely different\&.
.PP
If a schema name is given (for example,
CREATE VIEW myschema\&.myview \&.\&.\&.) then the view is created in the specified schema\&. Otherwise it is created in the current schema\&. Temporary views exist in a special schema, so a schema name cannot be given when creating a temporary view\&. The name of the view must be distinct from the name of any other relation (table, sequence, index, view, materialized view, or foreign table) in the same schema\&.
.SH "PARAMETERS"
.PP
TEMPORARY or TEMP
.RS 4
If specified, the view is created as a temporary view\&. Temporary views are automatically dropped at the end of the current session\&. Existing permanent relations with the same name are not visible to the current session while the temporary view exists, unless they are referenced with schema\-qualified names\&.
.sp
If any of the tables referenced by the view are temporary, the view is created as a temporary view (whether
TEMPORARY
is specified or not)\&.
.RE
.PP
RECURSIVE
.RS 4
Creates a recursive view\&. The syntax
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE RECURSIVE VIEW [ \fIschema\fR \&. ] \fIview_name\fR (\fIcolumn_names\fR) AS SELECT \fI\&.\&.\&.\fR;
.fi
.if n \{\
.RE
.\}
.sp
is equivalent to
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW [ \fIschema\fR \&. ] \fIview_name\fR AS WITH RECURSIVE \fIview_name\fR (\fIcolumn_names\fR) AS (SELECT \fI\&.\&.\&.\fR) SELECT \fIcolumn_names\fR FROM \fIview_name\fR;
.fi
.if n \{\
.RE
.\}
.sp
A view column name list must be specified for a recursive view\&.
.RE
.PP
\fIname\fR
.RS 4
The name (optionally schema\-qualified) of a view to be created\&.
.RE
.PP
\fIcolumn_name\fR
.RS 4
An optional list of names to be used for columns of the view\&. If not given, the column names are deduced from the query\&.
.RE
.PP
WITH ( \fIview_option_name\fR [= \fIview_option_value\fR] [, \&.\&.\&. ] )
.RS 4
This clause specifies optional parameters for a view; the following parameters are supported:
.PP
check_option (enum)
.RS 4
This parameter may be either
local
or
cascaded, and is equivalent to specifying
WITH [ CASCADED | LOCAL ] CHECK OPTION
(see below)\&.
.RE
.PP
security_barrier (boolean)
.RS 4
This should be used if the view is intended to provide row\-level security\&. See
Section\ \&41.5
for full details\&.
.RE
.PP
security_invoker (boolean)
.RS 4
This option causes the underlying base relations to be checked against the privileges of the user of the view rather than the view owner\&. See the notes below for full details\&.
.RE
.sp
All of the above options can be changed on existing views using
\fBALTER VIEW\fR\&.
.RE
.PP
\fIquery\fR
.RS 4
A
\fBSELECT\fR
or
\fBVALUES\fR
command which will provide the columns and rows of the view\&.
.RE
.PP
WITH [ CASCADED | LOCAL ] CHECK OPTION
.RS 4
This option controls the behavior of automatically updatable views\&. When this option is specified,
\fBINSERT\fR
and
\fBUPDATE\fR
commands on the view will be checked to ensure that new rows satisfy the view\-defining condition (that is, the new rows are checked to ensure that they are visible through the view)\&. If they are not, the update will be rejected\&. If the
CHECK OPTION
is not specified,
\fBINSERT\fR
and
\fBUPDATE\fR
commands on the view are allowed to create rows that are not visible through the view\&. The following check options are supported:
.PP
LOCAL
.RS 4
New rows are only checked against the conditions defined directly in the view itself\&. Any conditions defined on underlying base views are not checked (unless they also specify the
CHECK OPTION)\&.
.RE
.PP
CASCADED
.RS 4
New rows are checked against the conditions of the view and all underlying base views\&. If the
CHECK OPTION
is specified, and neither
LOCAL
nor
CASCADED
is specified, then
CASCADED
is assumed\&.
.RE
.sp
The
CHECK OPTION
may not be used with
RECURSIVE
views\&.
.sp
Note that the
CHECK OPTION
is only supported on views that are automatically updatable, and do not have
INSTEAD OF
triggers or
INSTEAD
rules\&. If an automatically updatable view is defined on top of a base view that has
INSTEAD OF
triggers, then the
LOCAL CHECK OPTION
may be used to check the conditions on the automatically updatable view, but the conditions on the base view with
INSTEAD OF
triggers will not be checked (a cascaded check option will not cascade down to a trigger\-updatable view, and any check options defined directly on a trigger\-updatable view will be ignored)\&. If the view or any of its base relations has an
INSTEAD
rule that causes the
\fBINSERT\fR
or
\fBUPDATE\fR
command to be rewritten, then all check options will be ignored in the rewritten query, including any checks from automatically updatable views defined on top of the relation with the
INSTEAD
rule\&.
.RE
.SH "NOTES"
.PP
Use the
\fBDROP VIEW\fR
statement to drop views\&.
.PP
Be careful that the names and types of the view\*(Aqs columns will be assigned the way you want\&. For example:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW vista AS SELECT \*(AqHello World\*(Aq;
.fi
.if n \{\
.RE
.\}
.sp
is bad form because the column name defaults to
?column?; also, the column data type defaults to
text, which might not be what you wanted\&. Better style for a string literal in a view\*(Aqs result is something like:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW vista AS SELECT text \*(AqHello World\*(Aq AS hello;
.fi
.if n \{\
.RE
.\}
.PP
By default, access to the underlying base relations referenced in the view is determined by the permissions of the view owner\&. In some cases, this can be used to provide secure but restricted access to the underlying tables\&. However, not all views are secure against tampering; see
Section\ \&41.5
for details\&.
.PP
If the view has the
security_invoker
property set to
true, access to the underlying base relations is determined by the permissions of the user executing the query, rather than the view owner\&. Thus, the user of a security invoker view must have the relevant permissions on the view and its underlying base relations\&.
.PP
If any of the underlying base relations is a security invoker view, it will be treated as if it had been accessed directly from the original query\&. Thus, a security invoker view will always check its underlying base relations using the permissions of the current user, even if it is accessed from a view without the
security_invoker
property\&.
.PP
If any of the underlying base relations has
row\-level security
enabled, then by default, the row\-level security policies of the view owner are applied, and access to any additional relations referred to by those policies is determined by the permissions of the view owner\&. However, if the view has
security_invoker
set to
true, then the policies and permissions of the invoking user are used instead, as if the base relations had been referenced directly from the query using the view\&.
.PP
Functions called in the view are treated the same as if they had been called directly from the query using the view\&. Therefore, the user of a view must have permissions to call all functions used by the view\&. Functions in the view are executed with the privileges of the user executing the query or the function owner, depending on whether the functions are defined as
SECURITY INVOKER
or
SECURITY DEFINER\&. Thus, for example, calling
CURRENT_USER
directly in a view will always return the invoking user, not the view owner\&. This is not affected by the view\*(Aqs
security_invoker
setting, and so a view with
security_invoker
set to
false
is
\fInot\fR
equivalent to a
SECURITY DEFINER
function and those concepts should not be confused\&.
.PP
The user creating or replacing a view must have
USAGE
privileges on any schemas referred to in the view query, in order to look up the referenced objects in those schemas\&. Note, however, that this lookup only happens when the view is created or replaced\&. Therefore, the user of the view only requires the
USAGE
privilege on the schema containing the view, not on the schemas referred to in the view query, even for a security invoker view\&.
.PP
When
\fBCREATE OR REPLACE VIEW\fR
is used on an existing view, only the view\*(Aqs defining SELECT rule, plus any
WITH ( \&.\&.\&. )
parameters and its
CHECK OPTION
are changed\&. Other view properties, including ownership, permissions, and non\-SELECT rules, remain unchanged\&. You must own the view to replace it (this includes being a member of the owning role)\&.
.SS "Updatable Views"
.PP
Simple views are automatically updatable: the system will allow
\fBINSERT\fR,
\fBUPDATE\fR
and
\fBDELETE\fR
statements to be used on the view in the same way as on a regular table\&. A view is automatically updatable if it satisfies all of the following conditions:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
The view must have exactly one entry in its
FROM
list, which must be a table or another updatable view\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
The view definition must not contain
WITH,
DISTINCT,
GROUP BY,
HAVING,
LIMIT, or
OFFSET
clauses at the top level\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
The view definition must not contain set operations (UNION,
INTERSECT
or
EXCEPT) at the top level\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
The view\*(Aqs select list must not contain any aggregates, window functions or set\-returning functions\&.
.RE
.PP
An automatically updatable view may contain a mix of updatable and non\-updatable columns\&. A column is updatable if it is a simple reference to an updatable column of the underlying base relation; otherwise the column is read\-only, and an error will be raised if an
\fBINSERT\fR
or
\fBUPDATE\fR
statement attempts to assign a value to it\&.
.PP
If the view is automatically updatable the system will convert any
\fBINSERT\fR,
\fBUPDATE\fR
or
\fBDELETE\fR
statement on the view into the corresponding statement on the underlying base relation\&.
\fBINSERT\fR
statements that have an
ON CONFLICT UPDATE
clause are fully supported\&.
.PP
If an automatically updatable view contains a
WHERE
condition, the condition restricts which rows of the base relation are available to be modified by
\fBUPDATE\fR
and
\fBDELETE\fR
statements on the view\&. However, an
\fBUPDATE\fR
is allowed to change a row so that it no longer satisfies the
WHERE
condition, and thus is no longer visible through the view\&. Similarly, an
\fBINSERT\fR
command can potentially insert base\-relation rows that do not satisfy the
WHERE
condition and thus are not visible through the view (ON CONFLICT UPDATE
may similarly affect an existing row not visible through the view)\&. The
CHECK OPTION
may be used to prevent
\fBINSERT\fR
and
\fBUPDATE\fR
commands from creating such rows that are not visible through the view\&.
.PP
If an automatically updatable view is marked with the
security_barrier
property then all the view\*(Aqs
WHERE
conditions (and any conditions using operators which are marked as
LEAKPROOF) will always be evaluated before any conditions that a user of the view has added\&. See
Section\ \&41.5
for full details\&. Note that, due to this, rows which are not ultimately returned (because they do not pass the user\*(Aqs
WHERE
conditions) may still end up being locked\&.
\fBEXPLAIN\fR
can be used to see which conditions are applied at the relation level (and therefore do not lock rows) and which are not\&.
.PP
A more complex view that does not satisfy all these conditions is read\-only by default: the system will not allow an insert, update, or delete on the view\&. You can get the effect of an updatable view by creating
INSTEAD OF
triggers on the view, which must convert attempted inserts, etc\&. on the view into appropriate actions on other tables\&. For more information see
CREATE TRIGGER (\fBCREATE_TRIGGER\fR(7))\&. Another possibility is to create rules (see
CREATE RULE (\fBCREATE_RULE\fR(7))), but in practice triggers are easier to understand and use correctly\&.
.PP
Note that the user performing the insert, update or delete on the view must have the corresponding insert, update or delete privilege on the view\&. In addition, by default, the view\*(Aqs owner must have the relevant privileges on the underlying base relations, whereas the user performing the update does not need any permissions on the underlying base relations (see
Section\ \&41.5)\&. However, if the view has
security_invoker
set to
true, the user performing the update, rather than the view owner, must have the relevant privileges on the underlying base relations\&.
.SH "EXAMPLES"
.PP
Create a view consisting of all comedy films:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW comedies AS
    SELECT *
    FROM films
    WHERE kind = \*(AqComedy\*(Aq;
.fi
.if n \{\
.RE
.\}
.sp
This will create a view containing the columns that are in the
film
table at the time of view creation\&. Though
*
was used to create the view, columns added later to the table will not be part of the view\&.
.PP
Create a view with
LOCAL CHECK OPTION:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW universal_comedies AS
    SELECT *
    FROM comedies
    WHERE classification = \*(AqU\*(Aq
    WITH LOCAL CHECK OPTION;
.fi
.if n \{\
.RE
.\}
.sp
This will create a view based on the
comedies
view, showing only films with
kind = \*(AqComedy\*(Aq
and
classification = \*(AqU\*(Aq\&. Any attempt to
\fBINSERT\fR
or
\fBUPDATE\fR
a row in the view will be rejected if the new row doesn\*(Aqt have
classification = \*(AqU\*(Aq, but the film
kind
will not be checked\&.
.PP
Create a view with
CASCADED CHECK OPTION:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW pg_comedies AS
    SELECT *
    FROM comedies
    WHERE classification = \*(AqPG\*(Aq
    WITH CASCADED CHECK OPTION;
.fi
.if n \{\
.RE
.\}
.sp
This will create a view that checks both the
kind
and
classification
of new rows\&.
.PP
Create a view with a mix of updatable and non\-updatable columns:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW comedies AS
    SELECT f\&.*,
           country_code_to_name(f\&.country_code) AS country,
           (SELECT avg(r\&.rating)
            FROM user_ratings r
            WHERE r\&.film_id = f\&.id) AS avg_rating
    FROM films f
    WHERE f\&.kind = \*(AqComedy\*(Aq;
.fi
.if n \{\
.RE
.\}
.sp
This view will support
\fBINSERT\fR,
\fBUPDATE\fR
and
\fBDELETE\fR\&. All the columns from the
films
table will be updatable, whereas the computed columns
country
and
avg_rating
will be read\-only\&.
.PP
Create a recursive view consisting of the numbers from 1 to 100:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE RECURSIVE VIEW public\&.nums_1_100 (n) AS
    VALUES (1)
UNION ALL
    SELECT n+1 FROM nums_1_100 WHERE n < 100;
.fi
.if n \{\
.RE
.\}
.sp
Notice that although the recursive view\*(Aqs name is schema\-qualified in this
\fBCREATE\fR, its internal self\-reference is not schema\-qualified\&. This is because the implicitly\-created CTE\*(Aqs name cannot be schema\-qualified\&.
.SH "COMPATIBILITY"
.PP
\fBCREATE OR REPLACE VIEW\fR
is a
PostgreSQL
language extension\&. So is the concept of a temporary view\&. The
WITH ( \&.\&.\&. )
clause is an extension as well, as are security barrier views and security invoker views\&.
.SH "SEE ALSO"
ALTER VIEW (\fBALTER_VIEW\fR(7)), DROP VIEW (\fBDROP_VIEW\fR(7)), CREATE MATERIALIZED VIEW (\fBCREATE_MATERIALIZED_VIEW\fR(7))