diff options
author | Kevin Grittner <kgrittn@postgresql.org> | 2013-03-03 18:23:31 -0600 |
---|---|---|
committer | Kevin Grittner <kgrittn@postgresql.org> | 2013-03-03 18:23:31 -0600 |
commit | 3bf3ab8c563699138be02f9dc305b7b77a724307 (patch) | |
tree | a36ddfded0bea88ee863595f58f62661cc61948b /src/backend/commands/view.c | |
parent | b15a6da29217b14f02895af1d9271e84415a91ae (diff) | |
download | postgresql-3bf3ab8c563699138be02f9dc305b7b77a724307.tar.gz postgresql-3bf3ab8c563699138be02f9dc305b7b77a724307.zip |
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.
Diffstat (limited to 'src/backend/commands/view.c')
-rw-r--r-- | src/backend/commands/view.c | 68 |
1 files changed, 13 insertions, 55 deletions
diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c index 4d10f80ec45..aba6944bdfa 100644 --- a/src/backend/commands/view.c +++ b/src/backend/commands/view.c @@ -36,57 +36,6 @@ static void checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc); -static bool isViewOnTempTable_walker(Node *node, void *context); - -/*--------------------------------------------------------------------- - * isViewOnTempTable - * - * Returns true iff any of the relations underlying this view are - * temporary tables. - *--------------------------------------------------------------------- - */ -static bool -isViewOnTempTable(Query *viewParse) -{ - return isViewOnTempTable_walker((Node *) viewParse, NULL); -} - -static bool -isViewOnTempTable_walker(Node *node, void *context) -{ - if (node == NULL) - return false; - - if (IsA(node, Query)) - { - Query *query = (Query *) node; - ListCell *rtable; - - foreach(rtable, query->rtable) - { - RangeTblEntry *rte = lfirst(rtable); - - if (rte->rtekind == RTE_RELATION) - { - Relation rel = heap_open(rte->relid, AccessShareLock); - char relpersistence = rel->rd_rel->relpersistence; - - heap_close(rel, AccessShareLock); - if (relpersistence == RELPERSISTENCE_TEMP) - return true; - } - } - - return query_tree_walker(query, - isViewOnTempTable_walker, - context, - QTW_IGNORE_JOINALIASES); - } - - return expression_tree_walker(node, - isViewOnTempTable_walker, - context); -} /*--------------------------------------------------------------------- * DefineVirtualRelation @@ -506,7 +455,7 @@ DefineView(ViewStmt *stmt, const char *queryString) */ view = copyObject(stmt->view); /* don't corrupt original command */ if (view->relpersistence == RELPERSISTENCE_PERMANENT - && isViewOnTempTable(viewParse)) + && isQueryUsingTempRelation(viewParse)) { view->relpersistence = RELPERSISTENCE_TEMP; ereport(NOTICE, @@ -530,6 +479,17 @@ DefineView(ViewStmt *stmt, const char *queryString) */ CommandCounterIncrement(); + StoreViewQuery(viewOid, viewParse, stmt->replace); + + return viewOid; +} + +/* + * Use the rules system to store the query for the view. + */ +void +StoreViewQuery(Oid viewOid, Query *viewParse, bool replace) +{ /* * The range table of 'viewParse' does not contain entries for the "OLD" * and "NEW" relations. So... add them! @@ -539,7 +499,5 @@ DefineView(ViewStmt *stmt, const char *queryString) /* * Now create the rules associated with the view. */ - DefineViewRules(viewOid, viewParse, stmt->replace); - - return viewOid; + DefineViewRules(viewOid, viewParse, replace); } |