Skip to content

Support AQUMV exact-match for multi-table JOIN queries#1606

Open
avamingli wants to merge 1 commit intoapache:mainfrom
avamingli:aqumv_join
Open

Support AQUMV exact-match for multi-table JOIN queries#1606
avamingli wants to merge 1 commit intoapache:mainfrom
avamingli:aqumv_join

Conversation

@avamingli
Copy link
Contributor

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

Fixes #ISSUE_Number

What does this PR do?

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context

CI Skip Instructions


@avamingli avamingli requested review from my-ship-it and yjhjstz March 5, 2026 08:02
@avamingli
Copy link
Contributor Author

This commit implements exact-match rewriting, step2 of Materialized views with JOINs when Answer Query? .
The next step is non-exact-match rewriting which is the hardest part and is on my TODO list.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant