Skip to content

Commit bb6fef2

Browse files
avaminglimy-ship-it
authored andcommitted
[AQUMV] Store view query in gp_matview_aux for view matching.
Move materialized views SQLs for AQUMV from pg_rewrite to gp_matview_aux. This would allow for quicker access and filtering, bypassing the evaluation of the system's 200 built-in rule entries that are often irrelevant. before this commit: set enable_answer_query_using_materialized_views = off; explain(analyze, costs off, verbose) select count(a) from t_select; QUERY PLAN ------------------------------------------------------------------ Finalize Aggregate (actual time=0.000..0.000 rows=1 loops=1) Output: count(a) -> Gather Motion 3:1 (slice1; segments: 3) (actual time=0.000..0.000 rows=3 loops=1) Output: (PARTIAL count(a)) -> Partial Aggregate (actual time=0.000..0.000 rows=1 loops=1) Output: PARTIAL count(a) -> Seq Scan on public.t_select (actual time=0.000..0.000 rows=340 loops=1) Output: a Settings: enable_answer_query_using_materialized_views = 'off', optimizer = 'off' Planning Time: 0.462 ms (slice0) Executor memory: 114K bytes. (slice1) Executor memory: 112K bytes avg x 3x(0) workers, 112K bytes max (seg0). Memory used: 128000kB Optimizer: Postgres query optimizer Execution Time: 1.898 ms (15 rows) enable_answer_query_using_materialized_views = off Planning Time: 0.462 ms after this commit: explain(analyze, costs off, verbose) select count(a) from t_select; QUERY PLAN ----------------------------------------------------------------------- Finalize Aggregate (actual time=4.000..4.000 rows=1 loops=1) Output: count(a) -> Gather Motion 3:1 (slice1; segments: 3) (actual time=4.000..4.000 rows=3 loops=1) Output: (PARTIAL count(a)) -> Partial Aggregate (actual time=4.000..4.000 rows=1 loops=1) Output: PARTIAL count(a) -> Seq Scan on public.t_select (actual time=4.000..4.000 rows=0 loops=1) Output: a Settings: enable_answer_query_using_materialized_views = 'on', optimizer = 'off' Planning Time: 0.623 ms (slice0) Executor memory: 114K bytes. (slice1) Executor memory: 112K bytes avg x 3x(0) workers, 112K bytes max (seg0). Memory used: 128000kB Optimizer: Postgres query optimizer Execution Time: 1.670 ms (15 rows) enable_answer_query_using_materialized_views = off Planning Time: 0.623 ms Authored-by: Zhang Mingli [email protected]
1 parent 1595fb9 commit bb6fef2

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed

contrib/pax_storage/src/test/regress/expected/misc_sanity.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ ORDER BY 1, 2;
104104
relname | attname | atttypid
105105
--------------------------+--------------------+--------------
106106
gp_configuration_history | desc | text
107+
gp_matview_aux | view_query | pg_node_tree
107108
gp_version_at_initdb | productversion | text
108109
gp_warehouse | warehouse_name | text
109110
pg_attribute | attacl | aclitem[]
@@ -137,7 +138,7 @@ ORDER BY 1, 2;
137138
pg_task_run_history | return_message | text
138139
pg_task_run_history | status | text
139140
pg_task_run_history | username | text
140-
(34 rows)
141+
(35 rows)
141142

142143
-- start_ignore
143144
-- system catalogs without primary keys

src/backend/catalog/gp_matview_aux.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "catalog/pg_type.h"
4242
#include "catalog/indexing.h"
4343
#include "cdb/cdbvars.h"
44+
#include "commands/matview.h"
4445
#include "utils/array.h"
4546
#include "utils/builtins.h"
4647
#include "utils/rel.h"
@@ -153,7 +154,6 @@ add_view_dependency(Oid mvoid)
153154
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
154155
}
155156

156-
157157
/*
158158
* InsertMatviewAuxEntry
159159
* We also insert gp_matview_tables entry here to maintain view.
@@ -168,6 +168,8 @@ InsertMatviewAuxEntry(Oid mvoid, const Query *viewQuery, bool skipdata)
168168
List *relids;
169169
NameData mvname;
170170
bool has_foreign = false;
171+
Relation matviewRel;
172+
char *viewsql;
171173

172174
Assert(OidIsValid(mvoid));
173175

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

189191
values[Anum_gp_matview_aux_has_foreign - 1] = BoolGetDatum(has_foreign);
190-
192+
193+
matviewRel = table_open(mvoid, NoLock);
194+
viewsql = nodeToString((Node *) copyObject(get_matview_query(matviewRel)));
195+
table_close(matviewRel, NoLock);
196+
values[Anum_gp_matview_aux_view_query - 1] = CStringGetTextDatum(viewsql);
197+
191198
if (skipdata)
192199
values[Anum_gp_matview_aux_datastatus - 1] = CharGetDatum(MV_DATA_STATUS_EXPIRED);
193200
else

src/backend/optimizer/plan/aqumv.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,12 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
147147
RangeTblEntry *rte;
148148
Oid origin_rel_oid;
149149
RangeTblEntry *mvrte;
150-
Relation ruleDesc;
151150
Relation matviewRel;
152-
SysScanDesc rcscan;
151+
Relation mvauxDesc;
152+
TupleDesc mvaux_tupdesc;
153+
SysScanDesc mvscan;
153154
HeapTuple tup;
154-
Form_pg_rewrite rewrite_tup;
155+
Form_gp_matview_aux mvaux_tup;
155156
bool need_close = false;
156157
PlannerInfo *subroot;
157158
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
217218
if (relkind == RELKIND_FOREIGN_TABLE && !aqumv_allow_foreign_table)
218219
return mv_final_rel;
219220

220-
ruleDesc = table_open(RewriteRelationId, AccessShareLock);
221+
mvauxDesc = table_open(GpMatviewAuxId, AccessShareLock);
222+
mvaux_tupdesc = RelationGetDescr(mvauxDesc);
221223

222-
rcscan = systable_beginscan(ruleDesc, InvalidOid, false,
224+
mvscan = systable_beginscan(mvauxDesc, InvalidOid, false,
223225
NULL, 0, NULL);
224226

225-
while (HeapTupleIsValid(tup = systable_getnext(rcscan)))
227+
while (HeapTupleIsValid(tup = systable_getnext(mvscan)))
226228
{
229+
Datum view_query_datum;
230+
char *view_query_str;
231+
bool is_null;
232+
227233
CHECK_FOR_INTERRUPTS();
228234
if (need_close)
229235
table_close(matviewRel, AccessShareLock);
230236

231-
rewrite_tup = (Form_pg_rewrite) GETSTRUCT(tup);
232-
233-
matviewRel = table_open(rewrite_tup->ev_class, AccessShareLock);
237+
mvaux_tup = (Form_gp_matview_aux) GETSTRUCT(tup);
238+
matviewRel = table_open(mvaux_tup->mvoid, AccessShareLock);
234239
need_close = true;
235240

236241
if (!RelationIsPopulated(matviewRel))
@@ -251,7 +256,14 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
251256
/*
252257
* Get a copy of view query to rewrite.
253258
*/
254-
viewQuery = copyObject(get_matview_query(matviewRel));
259+
view_query_datum = heap_getattr(tup,
260+
Anum_gp_matview_aux_view_query,
261+
mvaux_tupdesc,
262+
&is_null);
263+
264+
view_query_str = TextDatumGetCString(view_query_datum);
265+
viewQuery = copyObject(stringToNode(view_query_str));
266+
pfree(view_query_str);
255267
Assert(IsA(viewQuery, Query));
256268

257269
/*
@@ -620,8 +632,8 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
620632
}
621633
if (need_close)
622634
table_close(matviewRel, AccessShareLock);
623-
systable_endscan(rcscan);
624-
table_close(ruleDesc, AccessShareLock);
635+
systable_endscan(mvscan);
636+
table_close(mvauxDesc, AccessShareLock);
625637

626638
return current_rel;
627639
}

src/include/catalog/gp_matview_aux.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ CATALOG(gp_matview_aux,7153,GpMatviewAuxId)
4444
bool has_foreign; /* view query has foreign tables? */
4545
/* view's data status */
4646
char datastatus;
47+
48+
#ifdef CATALOG_VARLEN /* variable-length fields start here */
49+
pg_node_tree view_query BKI_FORCE_NOT_NULL;
50+
#endif
4751
} FormData_gp_matview_aux;
4852

4953

src/test/regress/expected/misc_sanity.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ ORDER BY 1, 2;
104104
relname | attname | atttypid
105105
--------------------------+--------------------+--------------
106106
gp_configuration_history | desc | text
107+
gp_matview_aux | view_query | pg_node_tree
107108
gp_version_at_initdb | productversion | text
108109
gp_warehouse | warehouse_name | text
109110
pg_attribute | attacl | aclitem[]
@@ -137,7 +138,7 @@ ORDER BY 1, 2;
137138
pg_task_run_history | return_message | text
138139
pg_task_run_history | status | text
139140
pg_task_run_history | username | text
140-
(34 rows)
141+
(35 rows)
141142

142143
-- system catalogs without primary keys
143144
--

src/test/singlenode_regress/expected/misc_sanity.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ ORDER BY 1, 2;
104104
relname | attname | atttypid
105105
--------------------------+--------------------+--------------
106106
gp_configuration_history | desc | text
107+
gp_matview_aux | view_query | pg_node_tree
107108
gp_version_at_initdb | productversion | text
108109
gp_warehouse | warehouse_name | text
109110
pg_attribute | attacl | aclitem[]
@@ -137,7 +138,7 @@ ORDER BY 1, 2;
137138
pg_task_run_history | return_message | text
138139
pg_task_run_history | status | text
139140
pg_task_run_history | username | text
140-
(34 rows)
141+
(35 rows)
141142

142143
-- system catalogs without primary keys
143144
--

0 commit comments

Comments
 (0)