Support AQUMV exact-match for multi-table JOIN queries#1606
Open
avamingli wants to merge 2 commits intoapache:mainfrom
Open
Support AQUMV exact-match for multi-table JOIN queries#1606avamingli wants to merge 2 commits intoapache:mainfrom
avamingli wants to merge 2 commits intoapache:mainfrom
Conversation
Contributor
Author
|
This commit implements exact-match rewriting, step2 of Materialized views with JOINs when Answer Query? . |
yjhjstz
reviewed
Mar 6, 2026
Add a new AQUMV code path that rewrites multi-table JOIN queries to
scan materialized views when the query exactly matches the MV
definition. This compares the saved raw parse tree against stored
viewQuery from gp_matview_aux, bypassing the single-table AQUMV
logic entirely.
This enables significant query acceleration for common analytical
patterns: instead of repeatedly computing expensive multi-table joins
at query time, the planner can directly read pre-computed results
from the materialized view, turning O(N*M) join operations into a
simple sequential scan.
For example, given:
CREATE MATERIALIZED VIEW mv AS
SELECT t1.a, t2.b FROM t1 JOIN t2 ON t1.a = t2.a;
-- Before (GUC off): original join plan
Gather Motion 3:1
-> Hash Join
Hash Cond: (t1.a = t2.a)
-> Seq Scan on t1
-> Hash
-> Seq Scan on t2
-- After (GUC on): rewritten to MV scan
Gather Motion 3:1
-> Seq Scan on mv
yjhjstz
reviewed
Mar 9, 2026
| viewQuery->hasAggs = false; | ||
| viewQuery->groupClause = NIL; | ||
| viewQuery->havingQual = NULL; | ||
| viewQuery->sortClause = NIL; |
Member
There was a problem hiding this comment.
sortClause is cleared. Since MV scans don't guarantee order, results may come back unsorted ?
yjhjstz
reviewed
Mar 9, 2026
src/backend/optimizer/plan/aqumv.c
Outdated
| * so a zeroed struct of that size works correctly (both fields NIL). | ||
| */ | ||
| { | ||
| char clean_qp_extra[2 * sizeof(List *)]; |
Member
There was a problem hiding this comment.
clean_qp_extra hardcodes 2 * sizeof(List *) assuming the private standard_qp_extra struct layout. If that struct changes, query_planner reads garbage memory. Should expose the struct properly in a header?
yjhjstz
reviewed
Mar 9, 2026
| return false; | ||
| if (!equal(raw_parse->limitCount, viewQuery->limitCount)) | ||
| return false; | ||
| if (!equal(raw_parse->limitOffset, viewQuery->limitOffset)) |
yjhjstz
reviewed
Mar 9, 2026
| } | ||
|
|
||
| /* Compare GROUP BY, HAVING, ORDER BY, DISTINCT, LIMIT */ | ||
| if (!equal(raw_parse->groupClause, viewQuery->groupClause)) |
Fix three issues in aqumv_query_is_exact_match(): - Add groupDistinct comparison (GROUP BY vs GROUP BY DISTINCT) - Add limitOption comparison (LIMIT vs FETCH FIRST WITH TIES) - Clear qp_extra in-place via aqumv_context->qp_extra instead of allocating a local char array; move standard_qp_extra typedef to planner.h so aqumv.c can reference the proper struct type Add test cases 26-28 to verify the new comparisons: - LIMIT vs FETCH FIRST WITH TIES non-match and exact match - GROUP BY DISTINCT vs GROUP BY non-match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a new AQUMV code path that rewrites multi-table JOIN queries to scan materialized views when the query exactly matches the MV definition. This compares the saved raw parse tree against stored viewQuery from gp_matview_aux, bypassing the single-table AQUMV logic entirely.
This enables significant query acceleration for common analytical patterns: instead of repeatedly computing expensive multi-table joins at query time, the planner can directly read pre-computed results from the materialized view, turning O(N*M) join operations into a simple sequential scan.
For example, given:
Fixes #ISSUE_Number
What does this PR do?
Type of Change
Breaking Changes
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelImpact
Performance:
User-facing changes:
Dependencies:
Checklist
Additional Context
CI Skip Instructions