diff --git a/contrib/pax_storage/src/test/regress/expected/misc_sanity.out b/contrib/pax_storage/src/test/regress/expected/misc_sanity.out index 9faaa27fc11..c88ab2025e3 100644 --- a/contrib/pax_storage/src/test/regress/expected/misc_sanity.out +++ b/contrib/pax_storage/src/test/regress/expected/misc_sanity.out @@ -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[] @@ -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 diff --git a/src/backend/catalog/gp_matview_aux.c b/src/backend/catalog/gp_matview_aux.c index 190a668883a..7fcd85c6d54 100644 --- a/src/backend/catalog/gp_matview_aux.c +++ b/src/backend/catalog/gp_matview_aux.c @@ -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" @@ -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. @@ -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)); @@ -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))); + table_close(matviewRel, NoLock); + values[Anum_gp_matview_aux_view_query - 1] = CStringGetTextDatum(viewsql); + if (skipdata) values[Anum_gp_matview_aux_datastatus - 1] = CharGetDatum(MV_DATA_STATUS_EXPIRED); else diff --git a/src/backend/optimizer/plan/aqumv.c b/src/backend/optimizer/plan/aqumv.c index 1a8f48e0668..d32c1049231 100644 --- a/src/backend/optimizer/plan/aqumv.c +++ b/src/backend/optimizer/plan/aqumv.c @@ -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. */ @@ -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)) @@ -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)); /* @@ -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; } diff --git a/src/include/catalog/gp_matview_aux.h b/src/include/catalog/gp_matview_aux.h index 1ba06b5f740..b1033d909ab 100644 --- a/src/include/catalog/gp_matview_aux.h +++ b/src/include/catalog/gp_matview_aux.h @@ -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; diff --git a/src/test/regress/expected/misc_sanity.out b/src/test/regress/expected/misc_sanity.out index 88ae3d4e927..9870214fd05 100644 --- a/src/test/regress/expected/misc_sanity.out +++ b/src/test/regress/expected/misc_sanity.out @@ -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[] @@ -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 -- diff --git a/src/test/singlenode_regress/expected/misc_sanity.out b/src/test/singlenode_regress/expected/misc_sanity.out index 88ae3d4e927..9870214fd05 100644 --- a/src/test/singlenode_regress/expected/misc_sanity.out +++ b/src/test/singlenode_regress/expected/misc_sanity.out @@ -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[] @@ -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 --