Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ ORDER BY 1, 2;
relname | attname | atttypid
--------------------------+--------------------+--------------
gp_configuration_history | desc | text
gp_matview_aux | view_query | pg_node_tree
gp_version_at_initdb | productversion | text
gp_warehouse | warehouse_name | text
pg_attribute | attacl | aclitem[]
Expand Down Expand Up @@ -137,7 +138,7 @@ ORDER BY 1, 2;
pg_task_run_history | return_message | text
pg_task_run_history | status | text
pg_task_run_history | username | text
(34 rows)
(35 rows)

-- start_ignore
-- system catalogs without primary keys
Expand Down
11 changes: 9 additions & 2 deletions src/backend/catalog/gp_matview_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "catalog/pg_type.h"
#include "catalog/indexing.h"
#include "cdb/cdbvars.h"
#include "commands/matview.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Expand Down Expand Up @@ -153,7 +154,6 @@ add_view_dependency(Oid mvoid)
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
}


/*
* InsertMatviewAuxEntry
* We also insert gp_matview_tables entry here to maintain view.
Expand All @@ -168,6 +168,8 @@ InsertMatviewAuxEntry(Oid mvoid, const Query *viewQuery, bool skipdata)
List *relids;
NameData mvname;
bool has_foreign = false;
Relation matviewRel;
char *viewsql;

Assert(OidIsValid(mvoid));

Expand All @@ -187,7 +189,12 @@ InsertMatviewAuxEntry(Oid mvoid, const Query *viewQuery, bool skipdata)
values[Anum_gp_matview_aux_mvname - 1] = NameGetDatum(&mvname);

values[Anum_gp_matview_aux_has_foreign - 1] = BoolGetDatum(has_foreign);


matviewRel = table_open(mvoid, NoLock);
viewsql = nodeToString((Node *) copyObject(get_matview_query(matviewRel)));
Comment thread
avamingli marked this conversation as resolved.
table_close(matviewRel, NoLock);
values[Anum_gp_matview_aux_view_query - 1] = CStringGetTextDatum(viewsql);
Comment thread
yjhjstz marked this conversation as resolved.

if (skipdata)
values[Anum_gp_matview_aux_datastatus - 1] = CharGetDatum(MV_DATA_STATUS_EXPIRED);
else
Expand Down
36 changes: 24 additions & 12 deletions src/backend/optimizer/plan/aqumv.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
RangeTblEntry *rte;
Oid origin_rel_oid;
RangeTblEntry *mvrte;
Relation ruleDesc;
Relation matviewRel;
SysScanDesc rcscan;
Relation mvauxDesc;
TupleDesc mvaux_tupdesc;
SysScanDesc mvscan;
HeapTuple tup;
Form_pg_rewrite rewrite_tup;
Form_gp_matview_aux mvaux_tup;
bool need_close = false;
PlannerInfo *subroot;
List *mv_final_tlist = NIL; /* Final target list we want to rewrite to. */
Expand Down Expand Up @@ -217,20 +218,24 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
if (relkind == RELKIND_FOREIGN_TABLE && !aqumv_allow_foreign_table)
return mv_final_rel;

ruleDesc = table_open(RewriteRelationId, AccessShareLock);
mvauxDesc = table_open(GpMatviewAuxId, AccessShareLock);
mvaux_tupdesc = RelationGetDescr(mvauxDesc);

rcscan = systable_beginscan(ruleDesc, InvalidOid, false,
mvscan = systable_beginscan(mvauxDesc, InvalidOid, false,
NULL, 0, NULL);

while (HeapTupleIsValid(tup = systable_getnext(rcscan)))
while (HeapTupleIsValid(tup = systable_getnext(mvscan)))
{
Datum view_query_datum;
char *view_query_str;
bool is_null;

CHECK_FOR_INTERRUPTS();
if (need_close)
table_close(matviewRel, AccessShareLock);

rewrite_tup = (Form_pg_rewrite) GETSTRUCT(tup);

matviewRel = table_open(rewrite_tup->ev_class, AccessShareLock);
mvaux_tup = (Form_gp_matview_aux) GETSTRUCT(tup);
matviewRel = table_open(mvaux_tup->mvoid, AccessShareLock);
need_close = true;

if (!RelationIsPopulated(matviewRel))
Expand All @@ -251,7 +256,14 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
/*
* Get a copy of view query to rewrite.
*/
viewQuery = copyObject(get_matview_query(matviewRel));
view_query_datum = heap_getattr(tup,
Anum_gp_matview_aux_view_query,
mvaux_tupdesc,
&is_null);

view_query_str = TextDatumGetCString(view_query_datum);
viewQuery = copyObject(stringToNode(view_query_str));
pfree(view_query_str);
Assert(IsA(viewQuery, Query));

/*
Expand Down Expand Up @@ -620,8 +632,8 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
}
if (need_close)
table_close(matviewRel, AccessShareLock);
systable_endscan(rcscan);
table_close(ruleDesc, AccessShareLock);
systable_endscan(mvscan);
table_close(mvauxDesc, AccessShareLock);

return current_rel;
}
Expand Down
4 changes: 4 additions & 0 deletions src/include/catalog/gp_matview_aux.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ CATALOG(gp_matview_aux,7153,GpMatviewAuxId)
bool has_foreign; /* view query has foreign tables? */
/* view's data status */
char datastatus;

#ifdef CATALOG_VARLEN /* variable-length fields start here */
pg_node_tree view_query BKI_FORCE_NOT_NULL;
#endif
} FormData_gp_matview_aux;


Expand Down
3 changes: 2 additions & 1 deletion src/test/regress/expected/misc_sanity.out
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ ORDER BY 1, 2;
relname | attname | atttypid
--------------------------+--------------------+--------------
gp_configuration_history | desc | text
gp_matview_aux | view_query | pg_node_tree
gp_version_at_initdb | productversion | text
gp_warehouse | warehouse_name | text
pg_attribute | attacl | aclitem[]
Expand Down Expand Up @@ -137,7 +138,7 @@ ORDER BY 1, 2;
pg_task_run_history | return_message | text
pg_task_run_history | status | text
pg_task_run_history | username | text
(34 rows)
(35 rows)

-- system catalogs without primary keys
--
Expand Down
3 changes: 2 additions & 1 deletion src/test/singlenode_regress/expected/misc_sanity.out
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ ORDER BY 1, 2;
relname | attname | atttypid
--------------------------+--------------------+--------------
gp_configuration_history | desc | text
gp_matview_aux | view_query | pg_node_tree
gp_version_at_initdb | productversion | text
gp_warehouse | warehouse_name | text
pg_attribute | attacl | aclitem[]
Expand Down Expand Up @@ -137,7 +138,7 @@ ORDER BY 1, 2;
pg_task_run_history | return_message | text
pg_task_run_history | status | text
pg_task_run_history | username | text
(34 rows)
(35 rows)

-- system catalogs without primary keys
--
Expand Down
Loading