2013-03-04 01:23:31 +01:00
|
|
|
<!--
|
|
|
|
doc/src/sgml/ref/create_materialized_view.sgml
|
|
|
|
PostgreSQL documentation
|
|
|
|
-->
|
|
|
|
|
2017-10-20 03:16:39 +02:00
|
|
|
<refentry id="sql-creatematerializedview">
|
2014-02-24 03:25:35 +01:00
|
|
|
<indexterm zone="sql-creatematerializedview">
|
|
|
|
<primary>CREATE MATERIALIZED VIEW</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
2013-03-04 01:23:31 +01:00
|
|
|
<refmeta>
|
|
|
|
<refentrytitle>CREATE MATERIALIZED VIEW</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum>
|
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
|
|
|
<refname>CREATE MATERIALIZED VIEW</refname>
|
|
|
|
<refpurpose>define a new materialized view</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
<refsynopsisdiv>
|
|
|
|
<synopsis>
|
2014-12-13 19:56:09 +01:00
|
|
|
CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] <replaceable>table_name</replaceable>
|
2013-03-04 01:23:31 +01:00
|
|
|
[ (<replaceable>column_name</replaceable> [, ...] ) ]
|
2019-04-04 02:37:00 +02:00
|
|
|
[ USING <replaceable class="parameter">method</replaceable> ]
|
2017-10-09 04:00:57 +02:00
|
|
|
[ WITH ( <replaceable class="parameter">storage_parameter</replaceable> [= <replaceable class="parameter">value</replaceable>] [, ... ] ) ]
|
|
|
|
[ TABLESPACE <replaceable class="parameter">tablespace_name</replaceable> ]
|
2013-03-04 01:23:31 +01:00
|
|
|
AS <replaceable>query</replaceable>
|
|
|
|
[ WITH [ NO ] DATA ]
|
|
|
|
</synopsis>
|
|
|
|
</refsynopsisdiv>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>CREATE MATERIALIZED VIEW</command> defines a materialized view of
|
|
|
|
a query. The query is executed and used to populate the view at the time
|
2017-10-09 03:44:17 +02:00
|
|
|
the command is issued (unless <command>WITH NO DATA</command> is used) and may be
|
2013-03-04 01:23:31 +01:00
|
|
|
refreshed later using <command>REFRESH MATERIALIZED VIEW</command>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>CREATE MATERIALIZED VIEW</command> is similar to
|
2017-10-09 03:44:17 +02:00
|
|
|
<command>CREATE TABLE AS</command>, except that it also remembers the query used
|
2013-03-04 01:23:31 +01:00
|
|
|
to initialize the view, so that it can be refreshed later upon demand.
|
2013-03-22 19:27:34 +01:00
|
|
|
A materialized view has many of the same properties as a table, but there
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
is no support for temporary materialized views.
|
2013-03-04 01:23:31 +01:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
|
|
|
|
2014-12-13 20:16:16 +01:00
|
|
|
<variablelist>
|
2014-12-13 19:56:09 +01:00
|
|
|
<varlistentry>
|
2017-10-09 03:44:17 +02:00
|
|
|
<term><literal>IF NOT EXISTS</literal></term>
|
2014-12-13 19:56:09 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Do not throw an error if a materialized view with the same name already
|
|
|
|
exists. A notice is issued in this case. Note that there is no guarantee
|
|
|
|
that the existing materialized view is anything like the one that would
|
|
|
|
have been created.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2013-03-04 01:23:31 +01:00
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>table_name</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name (optionally schema-qualified) of the materialized view to be
|
|
|
|
created.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>column_name</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of a column in the new materialized view. If column names are
|
|
|
|
not provided, they are taken from the output column names of the query.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2019-04-04 02:37:00 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><literal>USING <replaceable class="parameter">method</replaceable></literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
This optional clause specifies the table access method to use to store
|
|
|
|
the contents for the new materialized view; the method needs be an
|
|
|
|
access method of type <literal>TABLE</literal>. See <xref
|
|
|
|
linkend="tableam"/> for more information. If this option is not
|
|
|
|
specified, the default table access method is chosen for the new
|
|
|
|
materialized view. See <xref linkend="guc-default-table-access-method"/>
|
|
|
|
for more information.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2013-03-04 01:23:31 +01:00
|
|
|
<varlistentry>
|
2017-10-09 04:00:57 +02:00
|
|
|
<term><literal>WITH ( <replaceable class="parameter">storage_parameter</replaceable> [= <replaceable class="parameter">value</replaceable>] [, ... ] )</literal></term>
|
2013-03-04 01:23:31 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
This clause specifies optional storage parameters for the new
|
|
|
|
materialized view; see <xref linkend="sql-createtable-storage-parameters"
|
2017-11-23 15:39:47 +01:00
|
|
|
endterm="sql-createtable-storage-parameters-title"/> for more
|
2013-03-22 19:27:34 +01:00
|
|
|
information. All parameters supported for <literal>CREATE
|
|
|
|
TABLE</literal> are also supported for <literal>CREATE MATERIALIZED
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
VIEW</literal>.
|
2017-11-23 15:39:47 +01:00
|
|
|
See <xref linkend="sql-createtable"/> for more information.
|
2013-03-04 01:23:31 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2017-10-09 04:00:57 +02:00
|
|
|
<term><literal>TABLESPACE <replaceable class="parameter">tablespace_name</replaceable></literal></term>
|
2013-03-04 01:23:31 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2017-10-09 04:00:57 +02:00
|
|
|
The <replaceable class="parameter">tablespace_name</replaceable> is the name
|
2013-03-04 01:23:31 +01:00
|
|
|
of the tablespace in which the new materialized view is to be created.
|
2017-11-23 15:39:47 +01:00
|
|
|
If not specified, <xref linkend="guc-default-tablespace"/> is consulted.
|
2013-03-04 01:23:31 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>query</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2017-11-23 15:39:47 +01:00
|
|
|
A <xref linkend="sql-select"/>, <link linkend="sql-table">TABLE</link>,
|
|
|
|
or <xref linkend="sql-values"/> command. This query will run within a
|
2013-07-13 00:21:22 +02:00
|
|
|
security-restricted operation; in particular, calls to functions that
|
|
|
|
themselves create temporary tables will fail.
|
2013-03-04 01:23:31 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2017-10-09 03:44:17 +02:00
|
|
|
<term><literal>WITH [ NO ] DATA</literal></term>
|
2013-03-04 01:23:31 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
This clause specifies whether or not the materialized view should be
|
|
|
|
populated at creation time. If not, the materialized view will be
|
|
|
|
flagged as unscannable and cannot be queried until <command>REFRESH
|
2017-10-09 03:44:17 +02:00
|
|
|
MATERIALIZED VIEW</command> is used.
|
2013-03-04 01:23:31 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>CREATE MATERIALIZED VIEW</command> is a
|
|
|
|
<productname>PostgreSQL</productname> extension.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>See Also</title>
|
|
|
|
|
|
|
|
<simplelist type="inline">
|
2017-11-23 15:39:47 +01:00
|
|
|
<member><xref linkend="sql-altermaterializedview"/></member>
|
|
|
|
<member><xref linkend="sql-createtableas"/></member>
|
|
|
|
<member><xref linkend="sql-createview"/></member>
|
|
|
|
<member><xref linkend="sql-dropmaterializedview"/></member>
|
|
|
|
<member><xref linkend="sql-refreshmaterializedview"/></member>
|
2013-03-04 01:23:31 +01:00
|
|
|
</simplelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
</refentry>
|