Skip to content

Commit 9e7ccd9

Browse files
committed
Enable parallelism in REFRESH MATERIALIZED VIEW.
Pass CURSOR_OPT_PARALLEL_OK to pg_plan_query() so that parallel plans are considered when running the underlying SELECT query. This wasn't done in commit e9baa5e, which did this for CREATE MATERIALIZED VIEW, because it wasn't yet known to be safe. Since REFRESH always inserts into a freshly created table before later merging or swapping the data into place with separate operations, we can enable such plans here too. Author: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> Reviewed-by: Hou, Zhijie <houzj.fnst@cn.fujitsu.com> Reviewed-by: Luc Vlaming <luc@swarm64.com> Reviewed-by: Thomas Munro <thomas.munro@gmail.com> Discussion: https://postgr.es/m/CALj2ACXg-4hNKJC6nFnepRHYT4t5jJVstYvri%2BtKQHy7ydcr8A%40mail.gmail.com
1 parent fbe4cb3 commit 9e7ccd9

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

doc/src/sgml/parallel.sgml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,27 @@ EXPLAIN SELECT * FROM pgbench_accounts WHERE filler LIKE '%x%';
144144
The query writes any data or locks any database rows. If a query
145145
contains a data-modifying operation either at the top level or within
146146
a CTE, no parallel plans for that query will be generated. As an
147-
exception, the commands <literal>CREATE TABLE ... AS</literal>, <literal>SELECT
148-
INTO</literal>, and <literal>CREATE MATERIALIZED VIEW</literal> which create a new
149-
table and populate it can use a parallel plan. Another exception is the command
150-
<literal>INSERT INTO ... SELECT ...</literal> which can use a parallel plan for
151-
the underlying <literal>SELECT</literal> part of the query.
147+
exception, the following commands which create a new table and populate
148+
it can use a parallel plan for the underlying <literal>SELECT</literal>
149+
part of the query:
150+
151+
<itemizedlist>
152+
<listitem>
153+
<para><command>CREATE TABLE ... AS</command></para>
154+
</listitem>
155+
<listitem>
156+
<para><command>SELECT INTO</command></para>
157+
</listitem>
158+
<listitem>
159+
<para><command>INSERT INTO ... SELECT</command></para>
160+
</listitem>
161+
<listitem>
162+
<para><command>CREATE MATERIALIZED VIEW</command></para>
163+
</listitem>
164+
<listitem>
165+
<para><command>REFRESH MATERIALIZED VIEW</command></para>
166+
</listitem>
167+
</itemizedlist>
152168
</para>
153169
</listitem>
154170

src/backend/commands/matview.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ refresh_matview_datafill(DestReceiver *dest, Query *query,
402402
CHECK_FOR_INTERRUPTS();
403403

404404
/* Plan the query which will generate data for the refresh. */
405-
plan = pg_plan_query(query, queryString, 0, NULL);
405+
plan = pg_plan_query(query, queryString, CURSOR_OPT_PARALLEL_OK, NULL);
406406

407407
/*
408408
* Use a snapshot with an updated command ID to ensure this query sees

src/test/regress/expected/write_parallel.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ explain (costs off) create materialized view parallel_mat_view as
5858

5959
create materialized view parallel_mat_view as
6060
select length(stringu1) from tenk1 group by length(stringu1);
61+
create unique index on parallel_mat_view(length);
62+
refresh materialized view parallel_mat_view;
63+
refresh materialized view concurrently parallel_mat_view;
6164
drop materialized view parallel_mat_view;
6265
prepare prep_stmt as select length(stringu1) from tenk1 group by length(stringu1);
6366
explain (costs off) create table parallel_write as execute prep_stmt;

src/test/regress/sql/write_parallel.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ explain (costs off) create materialized view parallel_mat_view as
3030
select length(stringu1) from tenk1 group by length(stringu1);
3131
create materialized view parallel_mat_view as
3232
select length(stringu1) from tenk1 group by length(stringu1);
33+
create unique index on parallel_mat_view(length);
34+
refresh materialized view parallel_mat_view;
35+
refresh materialized view concurrently parallel_mat_view;
3336
drop materialized view parallel_mat_view;
3437

3538
prepare prep_stmt as select length(stringu1) from tenk1 group by length(stringu1);

0 commit comments

Comments
 (0)