Skip to content

Conversation

@avamingli
Copy link
Contributor

This commit introduces support for partitioned tables in the AQUMV (Answer Query Using Materialized Views) feature. Users can now create materialized views based on partitioned tables, which may include multiple levels of child partitions. When a query is executed on the root partition table, AQUMV will automatically rewrite the query to utilize the materialized view, thereby improving query performance by avoiding direct access to the partitioned tables.

Example:
We have a partition table par with children tables of multiple level.

select * from pg_partition_tree('par');
        relid        | parentrelid | isleaf | level
---------------------+-------------+--------+-------
 par                 |             | f      |     0
 par_1_prt_1         | par         | f      |     1
 par_1_prt_2         | par         | f      |     1
 par_1_prt_1_2_prt_1 | par_1_prt_1 | t      |     2
 par_1_prt_1_2_prt_2 | par_1_prt_1 | t      |     2
 par_1_prt_2_2_prt_1 | par_1_prt_2 | t      |     2
 par_1_prt_2_2_prt_2 | par_1_prt_2 | t      |     2

create materialized view mv_par as select count(*) as column_1 from par;

When execute:

  select count(*) from par;

AQUMV will rewrite the SQL to:

  select column_1 from mv_par;

Answer the results from materialized view mv_par instead of querying on par and its partitions.

explain(costs off, verbose) select count(*) from par;
                          QUERY PLAN
---------------------------------------------------------------
 Gather Motion 3:1  (slice1; segments: 3)
   Output: column_1
   ->  Seq Scan on public.mv_par
         Output: column_1
 Settings: enable_answer_query_using_materialized_views = 'on'
 Optimizer: Postgres query optimizer
(6 rows)

This enhancement significantly boosts query performance in OLAP environments where partitioned tables are extensively used, allowing users to fully leverage the benefits of materialized views.

Authored-by: Zhang Mingli [email protected]

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


…les.

This commit introduces support for partitioned tables in the AQUMV
(Answer Query Using Materialized Views) feature. Users can now create
materialized views based on partitioned tables, which may include
multiple levels of child partitions. When a query is executed on the
root partition table, AQUMV will automatically rewrite the query to
utilize the materialized view, thereby improving query performance by
avoiding direct access to the partitioned tables.

Example:
We have a partition table par with children tables of multiple level.

select * from pg_partition_tree('par');
        relid        | parentrelid | isleaf | level
---------------------+-------------+--------+-------
 par                 |             | f      |     0
 par_1_prt_1         | par         | f      |     1
 par_1_prt_2         | par         | f      |     1
 par_1_prt_1_2_prt_1 | par_1_prt_1 | t      |     2
 par_1_prt_1_2_prt_2 | par_1_prt_1 | t      |     2
 par_1_prt_2_2_prt_1 | par_1_prt_2 | t      |     2
 par_1_prt_2_2_prt_2 | par_1_prt_2 | t      |     2

create materialized view mv_par as select count(*) as column_1 from par;

When execute:
  select count(*) from par;

AQUMV will rewrite the SQL to:
  select column_1 from mv_par;

Answer the results from materialized view mv_par instead of querying on
par and its partitions.

explain(costs off, verbose) select count(*) from par;
                          QUERY PLAN
---------------------------------------------------------------
 Gather Motion 3:1  (slice1; segments: 3)
   Output: column_1
   ->  Seq Scan on public.mv_par
         Output: column_1
 Settings: enable_answer_query_using_materialized_views = 'on'
 Optimizer: Postgres query optimizer
(6 rows)

This enhancement significantly boosts query performance in OLAP
environments where partitioned tables are extensively used, allowing
users to fully leverage the benefits of materialized views.

Authored-by: Zhang Mingli [email protected]
@my-ship-it my-ship-it requested a review from yjhjstz March 4, 2025 02:33
@my-ship-it my-ship-it merged commit 878e6fd into apache:main Mar 12, 2025
23 checks passed
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.

4 participants