summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man7/CREATE_VIEW.7
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:17:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:17:33 +0000
commit5e45211a64149b3c659b90ff2de6fa982a5a93ed (patch)
tree739caf8c461053357daa9f162bef34516c7bf452 /doc/src/sgml/man7/CREATE_VIEW.7
parentInitial commit. (diff)
downloadpostgresql-15-5e45211a64149b3c659b90ff2de6fa982a5a93ed.tar.xz
postgresql-15-5e45211a64149b3c659b90ff2de6fa982a5a93ed.zip
Adding upstream version 15.5.upstream/15.5
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'doc/src/sgml/man7/CREATE_VIEW.7')
-rw-r--r--doc/src/sgml/man7/CREATE_VIEW.7545
1 files changed, 545 insertions, 0 deletions
diff --git a/doc/src/sgml/man7/CREATE_VIEW.7 b/doc/src/sgml/man7/CREATE_VIEW.7
new file mode 100644
index 0000000..b40752e
--- /dev/null
+++ b/doc/src/sgml/man7/CREATE_VIEW.7
@@ -0,0 +1,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))