Skip to content

Commit 3bf3ab8

Browse files
committed
Add a materialized view relations.
A materialized view has a rule just like a view and a heap and other physical properties like a table. The rule is only used to populate the table, references in queries refer to the materialized data. This is a minimal implementation, but should still be useful in many cases. Currently data is only populated "on demand" by the CREATE MATERIALIZED VIEW and REFRESH MATERIALIZED VIEW statements. It is expected that future releases will add incremental updates with various timings, and that a more refined concept of defining what is "fresh" data will be developed. At some point it may even be possible to have queries use a materialized in place of references to underlying tables, but that requires the other above-mentioned features to be working first. Much of the documentation work by Robert Haas. Review by Noah Misch, Thom Brown, Robert Haas, Marko Tiikkaja Security review by KaiGai Kohei, with a decision on how best to implement sepgsql still pending.
1 parent b15a6da commit 3bf3ab8

File tree

103 files changed

+4240
-438
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+4240
-438
lines changed

contrib/oid2name/oid2name.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ sql_exec_dumpalltables(PGconn *conn, struct options * opts)
444444
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
445445
" LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),"
446446
" pg_catalog.pg_tablespace t "
447-
"WHERE relkind IN ('r'%s%s) AND "
447+
"WHERE relkind IN ('r', 'm'%s%s) AND "
448448
" %s"
449449
" t.oid = CASE"
450450
" WHEN reltablespace <> 0 THEN reltablespace"
@@ -515,7 +515,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
515515
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n"
516516
" LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),\n"
517517
" pg_catalog.pg_tablespace t \n"
518-
"WHERE relkind IN ('r', 'i', 'S', 't') AND \n"
518+
"WHERE relkind IN ('r', 'm', 'i', 'S', 't') AND \n"
519519
" t.oid = CASE\n"
520520
" WHEN reltablespace <> 0 THEN reltablespace\n"
521521
" ELSE dattablespace\n"

contrib/pg_upgrade/info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
282282
"CREATE TEMPORARY TABLE info_rels (reloid) AS SELECT c.oid "
283283
"FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
284284
" ON c.relnamespace = n.oid "
285-
"WHERE relkind IN ('r', 'i'%s) AND "
285+
"WHERE relkind IN ('r', 'm', 'i'%s) AND "
286286
/* exclude possible orphaned temp tables */
287287
" ((n.nspname !~ '^pg_temp_' AND "
288288
" n.nspname !~ '^pg_toast_temp_' AND "

contrib/pg_upgrade/pg_upgrade.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ set_frozenxids(void)
525525
PQclear(executeQueryOrDie(conn,
526526
"UPDATE pg_catalog.pg_class "
527527
"SET relfrozenxid = '%u' "
528-
/* only heap and TOAST are vacuumed */
529-
"WHERE relkind IN ('r', 't')",
528+
/* only heap, materialized view, and TOAST are vacuumed */
529+
"WHERE relkind IN ('r', 'm', 't')",
530530
old_cluster.controldata.chkpnt_nxtxid));
531531
PQfinish(conn);
532532

contrib/pg_upgrade/version_old_8_3.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ old_8_3_check_for_tsquery_usage(ClusterInfo *cluster)
145145
"FROM pg_catalog.pg_class c, "
146146
" pg_catalog.pg_namespace n, "
147147
" pg_catalog.pg_attribute a "
148+
/* materialized views didn't exist in 8.3, so no need to check 'm' */
148149
"WHERE c.relkind = 'r' AND "
149150
" c.oid = a.attrelid AND "
150151
" NOT a.attisdropped AND "
@@ -323,6 +324,7 @@ old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode)
323324
"FROM pg_catalog.pg_class c, "
324325
" pg_catalog.pg_namespace n, "
325326
" pg_catalog.pg_attribute a "
327+
/* materialized views didn't exist in 8.3, so no need to check 'm' */
326328
"WHERE c.relkind = 'r' AND "
327329
" c.oid = a.attrelid AND "
328330
" NOT a.attisdropped AND "
@@ -343,6 +345,7 @@ old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode)
343345
"FROM pg_catalog.pg_class c, " \
344346
" pg_catalog.pg_namespace n, " \
345347
" pg_catalog.pg_attribute a " \
348+
/* materialized views didn't exist in 8.3, so no need to check 'm' */ \
346349
"WHERE c.relkind = 'r' AND " \
347350
" c.oid = a.attrelid AND " \
348351
" NOT a.attisdropped AND " \

contrib/pgstattuple/pgstattuple.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
216216
switch (rel->rd_rel->relkind)
217217
{
218218
case RELKIND_RELATION:
219+
case RELKIND_MATVIEW:
219220
case RELKIND_TOASTVALUE:
220221
case RELKIND_SEQUENCE:
221222
return pgstat_heap(rel, fcinfo);

contrib/vacuumlo/vacuumlo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ vacuumlo(const char *database, const struct _param * param)
209209
strcat(buf, " AND a.atttypid = t.oid ");
210210
strcat(buf, " AND c.relnamespace = s.oid ");
211211
strcat(buf, " AND t.typname in ('oid', 'lo') ");
212-
strcat(buf, " AND c.relkind = 'r'");
212+
strcat(buf, " AND c.relkind in ('r', 'm')");
213213
strcat(buf, " AND s.nspname !~ '^pg_'");
214214
res = PQexec(conn, buf);
215215
if (PQresultStatus(res) != PGRES_TUPLES_OK)

doc/src/sgml/catalogs.sgml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,8 +1597,8 @@
15971597
The catalog <structname>pg_class</structname> catalogs tables and most
15981598
everything else that has columns or is otherwise similar to a
15991599
table. This includes indexes (but see also
1600-
<structname>pg_index</structname>), sequences, views, composite types,
1601-
and TOAST tables; see <structfield>relkind</>.
1600+
<structname>pg_index</structname>), sequences, views, materialized
1601+
views, composite types, and TOAST tables; see <structfield>relkind</>.
16021602
Below, when we mean all of these
16031603
kinds of objects we speak of <quote>relations</quote>. Not all
16041604
columns are meaningful for all relation types.
@@ -1789,8 +1789,9 @@
17891789
<entry></entry>
17901790
<entry>
17911791
<literal>r</> = ordinary table, <literal>i</> = index,
1792-
<literal>S</> = sequence, <literal>v</> = view, <literal>c</> =
1793-
composite type, <literal>t</> = TOAST table,
1792+
<literal>S</> = sequence, <literal>v</> = view,
1793+
<literal>m</> = materialized view,
1794+
<literal>c</> = composite type, <literal>t</> = TOAST table,
17941795
<literal>f</> = foreign table
17951796
</entry>
17961797
</row>

doc/src/sgml/func.sgml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13743,6 +13743,10 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1374313743
<primary>pg_tablespace_location</primary>
1374413744
</indexterm>
1374513745

13746+
<indexterm>
13747+
<primary>pg_relation_is_scannable</primary>
13748+
</indexterm>
13749+
1374613750
<indexterm>
1374713751
<primary>pg_typeof</primary>
1374813752
</indexterm>
@@ -13867,29 +13871,29 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1386713871
<row>
1386813872
<entry><literal><function>pg_get_viewdef(<parameter>view_name</parameter>)</function></literal></entry>
1386913873
<entry><type>text</type></entry>
13870-
<entry>get underlying <command>SELECT</command> command for view (<emphasis>deprecated</emphasis>)</entry>
13874+
<entry>get underlying <command>SELECT</command> command for view or materialized view (<emphasis>deprecated</emphasis>)</entry>
1387113875
</row>
1387213876
<row>
1387313877
<entry><literal><function>pg_get_viewdef(<parameter>view_name</parameter>, <parameter>pretty_bool</>)</function></literal></entry>
1387413878
<entry><type>text</type></entry>
13875-
<entry>get underlying <command>SELECT</command> command for view (<emphasis>deprecated</emphasis>)</entry>
13879+
<entry>get underlying <command>SELECT</command> command for view or materialized view (<emphasis>deprecated</emphasis>)</entry>
1387613880
</row>
1387713881
<row>
1387813882
<entry><literal><function>pg_get_viewdef(<parameter>view_oid</parameter>)</function></literal></entry>
1387913883
<entry><type>text</type></entry>
13880-
<entry>get underlying <command>SELECT</command> command for view</entry>
13884+
<entry>get underlying <command>SELECT</command> command for view or materialized view</entry>
1388113885
</row>
1388213886
<row>
1388313887
<entry><literal><function>pg_get_viewdef(<parameter>view_oid</parameter>, <parameter>pretty_bool</>)</function></literal></entry>
1388413888
<entry><type>text</type></entry>
13885-
<entry>get underlying <command>SELECT</command> command for view</entry>
13889+
<entry>get underlying <command>SELECT</command> command for view or materialized view</entry>
1388613890
</row>
1388713891
<row>
1388813892
<entry><literal><function>pg_get_viewdef(<parameter>view_oid</parameter>, <parameter>wrap_column_int</>)</function></literal></entry>
1388913893
<entry><type>text</type></entry>
13890-
<entry>get underlying <command>SELECT</command> command for view;
13891-
lines with fields are wrapped to specified number of columns,
13892-
pretty-printing is implied</entry>
13894+
<entry>get underlying <command>SELECT</command> command for view or
13895+
materialized view; lines with fields are wrapped to specified
13896+
number of columns, pretty-printing is implied</entry>
1389313897
</row>
1389413898
<row>
1389513899
<entry><literal><function>pg_options_to_table(<parameter>reloptions</parameter>)</function></literal></entry>
@@ -13906,6 +13910,11 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1390613910
<entry><type>text</type></entry>
1390713911
<entry>get the path in the file system that this tablespace is located in</entry>
1390813912
</row>
13913+
<row>
13914+
<entry><literal><function>pg_relation_is_scannable(<parameter>relation_oid</parameter>)</function></literal></entry>
13915+
<entry><type>boolean</type></entry>
13916+
<entry>is the relation scannable; a materialized view which has not been loaded will not be scannable</entry>
13917+
</row>
1390913918
<row>
1391013919
<entry><literal><function>pg_typeof(<parameter>any</parameter>)</function></literal></entry>
1391113920
<entry><type>regtype</type></entry>

doc/src/sgml/ref/allfiles.sgml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Complete list of usable sgml source files in this directory.
2121
<!ENTITY alterIndex SYSTEM "alter_index.sgml">
2222
<!ENTITY alterLanguage SYSTEM "alter_language.sgml">
2323
<!ENTITY alterLargeObject SYSTEM "alter_large_object.sgml">
24+
<!ENTITY alterMaterializedView SYSTEM "alter_materialized_view.sgml">
2425
<!ENTITY alterOperator SYSTEM "alter_operator.sgml">
2526
<!ENTITY alterOperatorClass SYSTEM "alter_opclass.sgml">
2627
<!ENTITY alterOperatorFamily SYSTEM "alter_opfamily.sgml">
@@ -63,6 +64,7 @@ Complete list of usable sgml source files in this directory.
6364
<!ENTITY createGroup SYSTEM "create_group.sgml">
6465
<!ENTITY createIndex SYSTEM "create_index.sgml">
6566
<!ENTITY createLanguage SYSTEM "create_language.sgml">
67+
<!ENTITY createMaterializedView SYSTEM "create_materialized_view.sgml">
6668
<!ENTITY createOperator SYSTEM "create_operator.sgml">
6769
<!ENTITY createOperatorClass SYSTEM "create_opclass.sgml">
6870
<!ENTITY createOperatorFamily SYSTEM "create_opfamily.sgml">
@@ -102,6 +104,7 @@ Complete list of usable sgml source files in this directory.
102104
<!ENTITY dropGroup SYSTEM "drop_group.sgml">
103105
<!ENTITY dropIndex SYSTEM "drop_index.sgml">
104106
<!ENTITY dropLanguage SYSTEM "drop_language.sgml">
107+
<!ENTITY dropMaterializedView SYSTEM "drop_materialized_view.sgml">
105108
<!ENTITY dropOperator SYSTEM "drop_operator.sgml">
106109
<!ENTITY dropOperatorClass SYSTEM "drop_opclass.sgml">
107110
<!ENTITY dropOperatorFamily SYSTEM "drop_opfamily.sgml">
@@ -136,6 +139,7 @@ Complete list of usable sgml source files in this directory.
136139
<!ENTITY prepare SYSTEM "prepare.sgml">
137140
<!ENTITY prepareTransaction SYSTEM "prepare_transaction.sgml">
138141
<!ENTITY reassignOwned SYSTEM "reassign_owned.sgml">
142+
<!ENTITY refreshMaterializedView SYSTEM "refresh_materialized_view.sgml">
139143
<!ENTITY reindex SYSTEM "reindex.sgml">
140144
<!ENTITY releaseSavepoint SYSTEM "release_savepoint.sgml">
141145
<!ENTITY reset SYSTEM "reset.sgml">

doc/src/sgml/ref/alter_extension.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ALTER EXTENSION <replaceable class="PARAMETER">name</replaceable> DROP <replacea
3939
FOREIGN DATA WRAPPER <replaceable class="PARAMETER">object_name</replaceable> |
4040
FOREIGN TABLE <replaceable class="PARAMETER">object_name</replaceable> |
4141
FUNCTION <replaceable class="PARAMETER">function_name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] ) |
42+
MATERIALIZED VIEW <replaceable class="PARAMETER">object_name</replaceable> |
4243
OPERATOR <replaceable class="PARAMETER">operator_name</replaceable> (<replaceable class="PARAMETER">left_type</replaceable>, <replaceable class="PARAMETER">right_type</replaceable>) |
4344
OPERATOR CLASS <replaceable class="PARAMETER">object_name</replaceable> USING <replaceable class="parameter">index_method</replaceable> |
4445
OPERATOR FAMILY <replaceable class="PARAMETER">object_name</replaceable> USING <replaceable class="parameter">index_method</replaceable> |

0 commit comments

Comments
 (0)