pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: houzj.fnst@fujitsu.com <houzj.fnst@fujitsu.com>
To: Greg Nancarrow <gregn4422@gmail.com>
Cc: Amit Kapila <amit.kapila16@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Cc: vignesh C <vignesh21@gmail.com>
Cc: David Rowley <dgrowleyml@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: tsunakawa.takay@fujitsu.com <tsunakawa.takay@fujitsu.com>
Cc: tanghy.fnst@fujitsu.com <tanghy.fnst@fujitsu.com>
Cc: Amit Langote <amitlangote09@gmail.com>
Subject: RE: Parallel INSERT (INTO ... SELECT ...)
Date: Thu, 11 Mar 2021 13:01:42 +0000
Message-ID: <OS3PR01MB571802ACDB91AF35ADC97A7F94909@OS3PR01MB5718.jpnprd01.prod.outlook.com> (raw)
In-Reply-To: <CAJcOf-ed4k2TAucEOF2n1+zHGFiB2fwOGrsE_2i8kXuHZfM8=Q@mail.gmail.com>
References: <CAA4eK1+WBN99GOc8MwHfFoa7c-ssy+gYcbGVL3b8+A04pQKwWA@mail.gmail.com>
	<CAJcOf-ed4k2TAucEOF2n1+zHGFiB2fwOGrsE_2i8kXuHZfM8=Q@mail.gmail.com>

> I guess to have the finer granularity we'd have to go with enable_parallel_insert,
> which then would mean possibly having to later add enable_parallel_update,
> should parallel update have similar potential overhead in the parallel-safety
> checks (which to me, looks like it could, and parallel delete may not ...)
> 
> It's a shame there is no "set" type for GUC options.
> e.g.
> enable_parallel_dml='insert,update'
> Maybe that's going too far.
> 
> > 2. Should we keep the default value of GUC to on or off? It is
> > currently off. I am fine keeping it off for this release and we can
> > always turn it on in the later releases if required. Having said that,
> > I see the value in keeping it on because in many cases Insert ...
> > Select will be used for large data and there we will see a benefit of
> > parallelism and users facing trouble (who have a very large number of
> > partitions with less data to query) can still disable the parallel
> > insert for that particular table. Also, the other benefit of keeping
> > it on till at least the beta period is that this functionality will
> > get tested and if we found reports of regression then we can turn it
> > off for this release as well.
> >
> 
> I'd agree to keeping it on by default (and it can be selectively turned off for a
> particular table using the reloption, if needed).

Thanks, attaching new version patch keeping the default value of guc option to ON.

Best regards,
houzj


Attachments: download all (2 files, .tar.gz)

v26-0003-Parallel-SELECT-for-INSERT-INTO-.-SELECT-advanced-tests.patch (application/octet-stream, 45.2K) download From cef775a1fed73c884a676057aa527da0f1ae0ce9 Mon Sep 17 00:00:00 2001 From: Amit Kapila <akapila@postgresql.org> Date: Sat, 6 Mar 2021 15:23:57 +0530 Subject: [PATCH v25 3/3] Parallel SELECT for "INSERT INTO ... SELECT ..." -advanced tests. --- src/test/regress/expected/insert_parallel.out | 633 +++++++++++++++++- src/test/regress/sql/insert_parallel.sql | 302 ++++++++- 2 files changed, 931 insertions(+), 4 deletions(-) diff --git a/src/test/regress/expected/insert_parallel.out b/src/test/regress/expected/insert_parallel.out index f8d6dd8b4b..c070aa8afa 100644 --- a/src/test/regress/expected/insert_parallel.out +++ b/src/test/regress/expected/insert_parallel.out @@ -11,14 +11,36 @@ create or replace function fullname_parallel_unsafe(f text, l text) returns text return f || l; end; $$ language plpgsql immutable parallel unsafe; +create or replace function fullname_parallel_safe(f text, l text) returns text as $$ + begin + return f || l; + end; +$$ language plpgsql immutable parallel safe; create or replace function fullname_parallel_restricted(f text, l text) returns text as $$ begin return f || l; end; $$ language plpgsql immutable parallel restricted; +create or replace function lastname_startswithe_u(last_name text) returns boolean as $$ + begin + return substring(last_name from 1 for 1) = 'e'; + end; +$$ language plpgsql immutable parallel unsafe; +create or replace function lastname_startswithe_s(last_name text) returns boolean as $$ + begin + return substring(last_name from 1 for 1) = 'e'; + end; +$$ language plpgsql immutable parallel safe; +create or replace function lastname_startswithe_r(last_name text) returns boolean as $$ + begin + return substring(last_name from 1 for 1) = 'e'; + end; +$$ language plpgsql immutable parallel restricted; create table names(index int, first_name text, last_name text); create table names2(index int, first_name text, last_name text); create index names2_fullname_idx on names2 (fullname_parallel_unsafe(first_name, last_name)); +create table names3(index int, first_name text, last_name text); +create index names3_fullname_idx on names3 (fullname_parallel_safe(first_name, last_name)); create table names4(index int, first_name text, last_name text); create index names4_fullname_idx on names4 (fullname_parallel_restricted(first_name, last_name)); insert into names values @@ -179,6 +201,33 @@ insert into test_data1 select * from test_data where a = 10 returning a as data; 10 (1 row) +-- +-- Test INSERT with RETURNING clause (ordered SELECT). +-- (should create plan with parallel SELECT, GatherMerge parent node) +-- +truncate test_data1; +explain (costs off) insert into test_data1 select * from test_data where a <= 5 order by a returning a as data; + QUERY PLAN +-------------------------------------------------- + Insert on test_data1 + -> Gather Merge + Workers Planned: 3 + -> Sort + Sort Key: test_data.a + -> Parallel Seq Scan on test_data + Filter: (a <= 5) +(7 rows) + +insert into test_data1 select * from test_data where a <= 5 order by a returning a as data; + data +------ + 1 + 2 + 3 + 4 + 5 +(5 rows) + -- -- Test INSERT into a table with a foreign key. -- (Insert into a table with a foreign key is parallel-restricted, @@ -202,6 +251,86 @@ select count(*), sum(unique1) from para_insert_f1; 10000 | 49995000 (1 row) +-- +-- Test INSERT with underlying query, leader participation disabled +-- +set parallel_leader_participation = off; +truncate para_insert_p1 cascade; +NOTICE: truncate cascades to table "para_insert_f1" +explain (costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 <= 2500; + QUERY PLAN +----------------------------------------- + Insert on para_insert_p1 + -> Gather + Workers Planned: 4 + -> Parallel Seq Scan on tenk1 + Filter: (unique1 <= 2500) +(5 rows) + +insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 <= 2500; +select count(*), sum(unique1) from para_insert_p1; + count | sum +-------+--------- + 2501 | 3126250 +(1 row) + +select * from para_insert_p1 where unique1 >= 2490 order by unique1; + unique1 | stringu1 +---------+---------- + 2490 | URAAAA + 2491 | VRAAAA + 2492 | WRAAAA + 2493 | XRAAAA + 2494 | YRAAAA + 2495 | ZRAAAA + 2496 | ASAAAA + 2497 | BSAAAA + 2498 | CSAAAA + 2499 | DSAAAA + 2500 | ESAAAA +(11 rows) + +-- +-- Test INSERT with underlying query, leader participation disabled +-- and no workers available +set max_parallel_workers=0; +truncate para_insert_p1 cascade; +NOTICE: truncate cascades to table "para_insert_f1" +explain (costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 <= 2500; + QUERY PLAN +----------------------------------------- + Insert on para_insert_p1 + -> Gather + Workers Planned: 4 + -> Parallel Seq Scan on tenk1 + Filter: (unique1 <= 2500) +(5 rows) + +insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 <= 2500; +select count(*), sum(unique1) from para_insert_p1; + count | sum +-------+--------- + 2501 | 3126250 +(1 row) + +select * from para_insert_p1 where unique1 >= 2490 order by unique1; + unique1 | stringu1 +---------+---------- + 2490 | URAAAA + 2491 | VRAAAA + 2492 | WRAAAA + 2493 | XRAAAA + 2494 | YRAAAA + 2495 | ZRAAAA + 2496 | ASAAAA + 2497 | BSAAAA + 2498 | CSAAAA + 2499 | DSAAAA + 2500 | ESAAAA +(11 rows) + +reset parallel_leader_participation; +reset max_parallel_workers; -- -- Test INSERT with ON CONFLICT ... DO UPDATE ... -- (should not create a parallel plan) @@ -226,6 +355,208 @@ explain (costs off) insert into test_conflict_table(id, somedata) select a, a fr -> Seq Scan on test_data (4 rows) +-- +-- Test INSERT with parallelized aggregate +-- +create table tenk1_avg_data(count int, avg_unique1 int, avg_stringu1_len int); +explain (costs off) insert into tenk1_avg_data select count(*), avg(unique1), avg(length(stringu1)) from tenk1; + QUERY PLAN +---------------------------------------------------------- + Insert on tenk1_avg_data + -> Subquery Scan on "*SELECT*" + -> Finalize Aggregate + -> Gather + Workers Planned: 4 + -> Partial Aggregate + -> Parallel Seq Scan on tenk1 +(7 rows) + +insert into tenk1_avg_data select count(*), avg(unique1), avg(length(stringu1)) from tenk1; +select * from tenk1_avg_data; + count | avg_unique1 | avg_stringu1_len +-------+-------------+------------------ + 10000 | 5000 | 6 +(1 row) + +-- +-- Test INSERT with parallel bitmap heap scan +-- +set enable_seqscan to off; +set enable_indexscan to off; +truncate para_insert_p1 cascade; +NOTICE: truncate cascades to table "para_insert_f1" +explain (costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 >= 7500; + QUERY PLAN +------------------------------------------------------ + Insert on para_insert_p1 + -> Gather + Workers Planned: 4 + -> Parallel Bitmap Heap Scan on tenk1 + Recheck Cond: (unique1 >= 7500) + -> Bitmap Index Scan on tenk1_unique1 + Index Cond: (unique1 >= 7500) +(7 rows) + +insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 >= 7500; +-- select some values to verify that the insert worked +select * from para_insert_p1 where unique1 >= 9990 order by unique1; + unique1 | stringu1 +---------+---------- + 9990 | GUAAAA + 9991 | HUAAAA + 9992 | IUAAAA + 9993 | JUAAAA + 9994 | KUAAAA + 9995 | LUAAAA + 9996 | MUAAAA + 9997 | NUAAAA + 9998 | OUAAAA + 9999 | PUAAAA +(10 rows) + +reset enable_seqscan; +reset enable_indexscan; +-- +-- Test INSERT with parallel append +-- +create table a_star_data(aa int); +explain (costs off) insert into a_star_data select aa from a_star where aa > 10; + QUERY PLAN +-------------------------------------------------------- + Insert on a_star_data + -> Gather + Workers Planned: 3 + -> Parallel Append + -> Parallel Seq Scan on d_star a_star_4 + Filter: (aa > 10) + -> Parallel Seq Scan on f_star a_star_6 + Filter: (aa > 10) + -> Parallel Seq Scan on e_star a_star_5 + Filter: (aa > 10) + -> Parallel Seq Scan on b_star a_star_2 + Filter: (aa > 10) + -> Parallel Seq Scan on c_star a_star_3 + Filter: (aa > 10) + -> Parallel Seq Scan on a_star a_star_1 + Filter: (aa > 10) +(16 rows) + +insert into a_star_data select aa from a_star where aa > 10; +select count(aa), sum(aa) from a_star_data; + count | sum +-------+----- + 16 | 300 +(1 row) + +-- +-- Test INSERT with parallel index scan +-- +set enable_seqscan to off; +set enable_bitmapscan to off; +set min_parallel_index_scan_size=0; +truncate para_insert_p1 cascade; +NOTICE: truncate cascades to table "para_insert_f1" +explain (costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 >= 500; + QUERY PLAN +-------------------------------------------------------------- + Insert on para_insert_p1 + -> Gather + Workers Planned: 4 + -> Parallel Index Scan using tenk1_unique1 on tenk1 + Index Cond: (unique1 >= 500) +(5 rows) + +insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 >= 500; +-- select some values to verify that the parallel insert worked +select count(*), sum(unique1) from para_insert_p1; + count | sum +-------+---------- + 9500 | 49870250 +(1 row) + +select * from para_insert_p1 where unique1 >= 9990 order by unique1; + unique1 | stringu1 +---------+---------- + 9990 | GUAAAA + 9991 | HUAAAA + 9992 | IUAAAA + 9993 | JUAAAA + 9994 | KUAAAA + 9995 | LUAAAA + 9996 | MUAAAA + 9997 | NUAAAA + 9998 | OUAAAA + 9999 | PUAAAA +(10 rows) + +-- +-- Test INSERT with parallel index-only scan +-- +truncate para_insert_p1 cascade; +NOTICE: truncate cascades to table "para_insert_f1" +explain (costs off) insert into para_insert_p1 select unique1 from tenk1 where unique1 >= 500; + QUERY PLAN +------------------------------------------------------------------- + Insert on para_insert_p1 + -> Gather + Workers Planned: 4 + -> Parallel Index Only Scan using tenk1_unique1 on tenk1 + Index Cond: (unique1 >= 500) +(5 rows) + +insert into para_insert_p1 select unique1 from tenk1 where unique1 >= 500; +-- select some values to verify that the parallel insert worked +select count(*), sum(unique1) from para_insert_p1; + count | sum +-------+---------- + 9500 | 49870250 +(1 row) + +select unique1 from para_insert_p1 where unique1 >= 9990 order by unique1; + unique1 +--------- + 9990 + 9991 + 9992 + 9993 + 9994 + 9995 + 9996 + 9997 + 9998 + 9999 +(10 rows) + +reset min_parallel_index_scan_size; +reset enable_seqscan; +reset enable_bitmapscan; +-- +-- Test INSERT with parallel-safe index expression +-- (should create a parallel plan) +-- +explain (costs off) insert into names3 select * from names; + QUERY PLAN +---------------------------------------- + Insert on names3 + -> Gather + Workers Planned: 3 + -> Parallel Seq Scan on names +(4 rows) + +insert into names3 select * from names; +select * from names3 order by fullname_parallel_safe(first_name, last_name); + index | first_name | last_name +-------+------------+------------- + 7 | alan | turing + 1 | albert | einstein + 3 | erwin | schrodinger + 6 | isaac | newton + 4 | leonhard | euler + 2 | niels | bohr + 8 | richard | feynman + 5 | stephen | hawking +(8 rows) + -- -- Test INSERT with parallel-unsafe index expression -- (should not create a parallel plan) @@ -322,6 +653,51 @@ insert into names7 select * from names order by last_name returning last_name || turing, alan (8 rows) +-- +-- Test INSERT with parallel-safe index predicate +-- (should create a parallel plan) +-- +create table names8 (like names); +create index names8_lastname_partial_idx on names8(index, last_name) where lastname_startswithe_s(last_name); +explain (costs off) insert into names8 select * from names; + QUERY PLAN +---------------------------------------- + Insert on names8 + -> Gather + Workers Planned: 3 + -> Parallel Seq Scan on names +(4 rows) + +insert into names8 select * from names; +-- +-- Test INSERT with parallel-unsafe index predicate +-- (should not create a parallel plan) +-- +create table names9 (like names); +create index names9_lastname_partial_idx on names9(index, last_name) where lastname_startswithe_u(last_name); +explain (costs off) insert into names9 select * from names; + QUERY PLAN +------------------------- + Insert on names9 + -> Seq Scan on names +(2 rows) + +-- +-- Test INSERT with parallel-restricted index predicate +-- (should create a parallel plan) +-- +create table names10 (like names); +create index names10_lastname_partial_idx on names10(index, last_name) where lastname_startswithe_r(last_name); +explain (costs off) insert into names10 select * from names; + QUERY PLAN +---------------------------------------- + Insert on names10 + -> Gather + Workers Planned: 3 + -> Parallel Seq Scan on names +(4 rows) + +insert into names10 select * from names; -- -- Test INSERT into temporary table with underlying query. -- (Insert into a temp table is parallel-restricted; @@ -342,6 +718,40 @@ insert into temp_names select * from names; -- Test INSERT with column defaults -- -- +-- a: no default +-- b: unsafe default +-- c: restricted default +-- d: safe default +-- +-- +-- No column defaults, should use parallel SELECT +-- +explain (costs off) insert into testdef(a,b,c,d) select a,a*2,a*4,a*8 from test_data; + QUERY PLAN +-------------------------------------------- + Insert on testdef + -> Gather + Workers Planned: 3 + -> Parallel Seq Scan on test_data +(4 rows) + +insert into testdef(a,b,c,d) select a,a*2,a*4,a*8 from test_data; +select * from testdef order by a; + a | b | c | d +----+----+----+---- + 1 | 2 | 4 | 8 + 2 | 4 | 8 | 16 + 3 | 6 | 12 | 24 + 4 | 8 | 16 | 32 + 5 | 10 | 20 | 40 + 6 | 12 | 24 | 48 + 7 | 14 | 28 | 56 + 8 | 16 | 32 | 64 + 9 | 18 | 36 | 72 + 10 | 20 | 40 | 80 +(10 rows) + +truncate testdef; -- -- Parallel unsafe column default, should not use a parallel plan -- @@ -380,6 +790,35 @@ select * from testdef order by a; 10 | 20 | 10 | 80 (10 rows) +truncate testdef; +-- +-- Parallel safe column default, should use parallel SELECT +-- +explain (costs off) insert into testdef(a,b,c) select a,a*2,a*4 from test_data; + QUERY PLAN +-------------------------------------------- + Insert on testdef + -> Gather + Workers Planned: 3 + -> Parallel Seq Scan on test_data +(4 rows) + +insert into testdef(a,b,c) select a,a*2,a*4 from test_data; +select * from testdef order by a; + a | b | c | d +----+----+----+---- + 1 | 2 | 4 | 20 + 2 | 4 | 8 | 20 + 3 | 6 | 12 | 20 + 4 | 8 | 16 | 20 + 5 | 10 | 20 | 20 + 6 | 12 | 24 | 20 + 7 | 14 | 28 | 20 + 8 | 16 | 32 | 20 + 9 | 18 | 36 | 20 + 10 | 20 | 40 | 20 +(10 rows) + truncate testdef; -- -- Parallel restricted and unsafe column defaults, should not use a parallel plan @@ -438,6 +877,64 @@ select count(*) from parttable1_2; 5000 (1 row) +-- +-- Test INSERT into partition with parallel-unsafe partition key support function +-- (should not create a parallel plan) +-- +create function my_int4_sort(int4,int4) returns int language sql + as $$ select case when $1 = $2 then 0 when $1 > $2 then 1 else -1 end; $$; +create operator class test_int4_ops for type int4 using btree as + operator 1 < (int4,int4), operator 2 <= (int4,int4), + operator 3 = (int4,int4), operator 4 >= (int4,int4), + operator 5 > (int4,int4), function 1 my_int4_sort(int4,int4); +create table partkey_unsafe_key_supp_fn_t (a int4, b name) partition by range (a test_int4_ops); +create table partkey_unsafe_key_supp_fn_t_1 partition of partkey_unsafe_key_supp_fn_t for values from (0) to (5000); +create table partkey_unsafe_key_supp_fn_t_2 partition of partkey_unsafe_key_supp_fn_t for values from (5000) to (10000); +explain (costs off) insert into partkey_unsafe_key_supp_fn_t select unique1, stringu1 from tenk1; + QUERY PLAN +---------------------------------------- + Insert on partkey_unsafe_key_supp_fn_t + -> Seq Scan on tenk1 +(2 rows) + +-- +-- Test INSERT into partition with parallel-unsafe partition key expression +-- (should not create a parallel plan) +-- +create table partkey_unsafe_key_expr_t (a int4, b name) partition by range ((fullname_parallel_unsafe('',a::varchar))); +explain (costs off) insert into partkey_unsafe_key_expr_t select unique1, stringu1 from tenk1; + QUERY PLAN +------------------------------------- + Insert on partkey_unsafe_key_expr_t + -> Seq Scan on tenk1 +(2 rows) + +-- +-- Test INSERT into table with parallel-safe check constraint +-- (should create a parallel plan) +-- +create or replace function check_a(a int4) returns boolean as $$ + begin + return (a >= 0 and a <= 9999); + end; +$$ language plpgsql parallel safe; +create table table_check_a(a int4 check (check_a(a)), b name); +explain (costs off) insert into table_check_a select unique1, stringu1 from tenk1; + QUERY PLAN +---------------------------------------- + Insert on table_check_a + -> Gather + Workers Planned: 4 + -> Parallel Seq Scan on tenk1 +(4 rows) + +insert into table_check_a select unique1, stringu1 from tenk1; +select count(*), sum(a) from table_check_a; + count | sum +-------+---------- + 10000 | 49995000 +(1 row) + -- -- Test INSERT into table with parallel-unsafe check constraint -- (should not create a parallel plan) @@ -456,16 +953,24 @@ explain (costs off) insert into table_check_b(a,b,c) select unique1, unique2, st (2 rows) -- --- Test INSERT into table with parallel-safe after stmt-level triggers +-- Test INSERT into table with parallel-safe before+after stmt-level triggers -- (should create a parallel SELECT plan; triggers should fire) -- create table names_with_safe_trigger (like names); +create or replace function insert_before_trigger_safe() returns trigger as $$ + begin + raise notice 'hello from insert_before_trigger_safe'; + return new; + end; +$$ language plpgsql parallel safe; create or replace function insert_after_trigger_safe() returns trigger as $$ begin raise notice 'hello from insert_after_trigger_safe'; return new; end; $$ language plpgsql parallel safe; +create trigger insert_before_trigger_safe before insert on names_with_safe_trigger + for each statement execute procedure insert_before_trigger_safe(); create trigger insert_after_trigger_safe after insert on names_with_safe_trigger for each statement execute procedure insert_after_trigger_safe(); explain (costs off) insert into names_with_safe_trigger select * from names; @@ -478,18 +983,27 @@ explain (costs off) insert into names_with_safe_trigger select * from names; (4 rows) insert into names_with_safe_trigger select * from names; +NOTICE: hello from insert_before_trigger_safe NOTICE: hello from insert_after_trigger_safe -- --- Test INSERT into table with parallel-unsafe after stmt-level triggers +-- Test INSERT into table with parallel-unsafe before+after stmt-level triggers -- (should not create a parallel plan; triggers should fire) -- create table names_with_unsafe_trigger (like names); +create or replace function insert_before_trigger_unsafe() returns trigger as $$ + begin + raise notice 'hello from insert_before_trigger_unsafe'; + return new; + end; +$$ language plpgsql parallel unsafe; create or replace function insert_after_trigger_unsafe() returns trigger as $$ begin raise notice 'hello from insert_after_trigger_unsafe'; return new; end; $$ language plpgsql parallel unsafe; +create trigger insert_before_trigger_unsafe before insert on names_with_unsafe_trigger + for each statement execute procedure insert_before_trigger_unsafe(); create trigger insert_after_trigger_unsafe after insert on names_with_unsafe_trigger for each statement execute procedure insert_after_trigger_unsafe(); explain (costs off) insert into names_with_unsafe_trigger select * from names; @@ -500,8 +1014,43 @@ explain (costs off) insert into names_with_unsafe_trigger select * from names; (2 rows) insert into names_with_unsafe_trigger select * from names; +NOTICE: hello from insert_before_trigger_unsafe NOTICE: hello from insert_after_trigger_unsafe -- +-- Test INSERT into table with parallel-restricted before+after stmt-level trigger +-- (should create a parallel plan with parallel SELECT; +-- stmt-level before+after triggers should fire) +-- +create table names_with_restricted_trigger (like names); +create or replace function insert_before_trigger_restricted() returns trigger as $$ + begin + raise notice 'hello from insert_before_trigger_restricted'; + return new; + end; +$$ language plpgsql parallel restricted; +create or replace function insert_after_trigger_restricted() returns trigger as $$ + begin + raise notice 'hello from insert_after_trigger_restricted'; + return new; + end; +$$ language plpgsql parallel restricted; +create trigger insert_before_trigger_restricted before insert on names_with_restricted_trigger + for each statement execute procedure insert_before_trigger_restricted(); +create trigger insert_after_trigger_restricted after insert on names_with_restricted_trigger + for each statement execute procedure insert_after_trigger_restricted(); +explain (costs off) insert into names_with_restricted_trigger select * from names; + QUERY PLAN +----------------------------------------- + Insert on names_with_restricted_trigger + -> Gather + Workers Planned: 3 + -> Parallel Seq Scan on names +(4 rows) + +insert into names_with_restricted_trigger select * from names; +NOTICE: hello from insert_before_trigger_restricted +NOTICE: hello from insert_after_trigger_restricted +-- -- Test INSERT into partition with parallel-unsafe trigger -- (should not create a parallel plan) -- @@ -550,15 +1099,54 @@ explain (costs off) execute q; Filter: ((a % 2) = 0) (3 rows) +-- +-- Test INSERT into table with TOAST column +-- +create table insert_toast_table(index int4, data text); +create table insert_toast_table_data (like insert_toast_table); +insert into insert_toast_table_data select i, rpad('T', 16384, 'ABCDEFGH') from generate_series(1,20) as i; +explain (costs off) insert into insert_toast_table select index, data from insert_toast_table_data; + QUERY PLAN +---------------------------------------------------------- + Insert on insert_toast_table + -> Gather + Workers Planned: 3 + -> Parallel Seq Scan on insert_toast_table_data +(4 rows) + +insert into insert_toast_table select index, data from insert_toast_table_data; +select count(*) as row_count, sum(length(data)) as total_data_length from insert_toast_table; + row_count | total_data_length +-----------+------------------- + 20 | 327680 +(1 row) + +truncate insert_toast_table; -- -- Test INSERT into table having a DOMAIN column with a CHECK constraint -- create function sql_is_distinct_from_u(anyelement, anyelement) returns boolean language sql parallel unsafe as 'select $1 is distinct from $2 limit 1'; +create or replace function sql_is_distinct_from_r(a anyelement, b anyelement) returns boolean as $$ + begin + return (a <> b); + end; +$$ language plpgsql parallel restricted; +create or replace function sql_is_distinct_from_s(a anyelement, b anyelement) returns boolean as $$ + begin + return (a <> b); + end; +$$ language plpgsql parallel safe; create domain inotnull_u int check (sql_is_distinct_from_u(value, null)); +create domain inotnull_r int + check (sql_is_distinct_from_r(value, null)); +create domain inotnull_s int + check (sql_is_distinct_from_s(value, null)); create table dom_table_u (x inotnull_u, y int); +create table dom_table_r (x inotnull_r, y int); +create table dom_table_s (x inotnull_s, y int); -- Test INSERT into table having a DOMAIN column with parallel-unsafe CHECK constraint explain (costs off) insert into dom_table_u select unique1, unique2 from tenk1; QUERY PLAN @@ -567,6 +1155,41 @@ explain (costs off) insert into dom_table_u select unique1, unique2 from tenk1; -> Seq Scan on tenk1 (2 rows) +-- Test INSERT into table having a DOMAIN column with parallel-restricted CHECK constraint +explain (costs off) insert into dom_table_r select unique1, unique2 from tenk1; + QUERY PLAN +---------------------------------------- + Insert on dom_table_r + -> Gather + Workers Planned: 4 + -> Parallel Seq Scan on tenk1 +(4 rows) + +insert into dom_table_r select unique1, unique2 from tenk1; +select count(*), sum(x) as sum_x, sum(y) as sum_y from dom_table_r; + count | sum_x | sum_y +-------+----------+---------- + 10000 | 49995000 | 49995000 +(1 row) + +-- Test INSERT into table having a DOMAIN column with parallel-safe CHECK constraint +-- NOTE: Currently max_parallel_hazard() regards CoerceToDomain as parallel-restricted +explain (costs off) insert into dom_table_s select unique1, unique2 from tenk1; + QUERY PLAN +---------------------------------------- + Insert on dom_table_s + -> Gather + Workers Planned: 4 + -> Parallel Seq Scan on tenk1 +(4 rows) + +insert into dom_table_s select unique1, unique2 from tenk1; +select count(*), sum(x) as sum_x, sum(y) as sum_y from dom_table_s; + count | sum_x | sum_y +-------+----------+---------- + 10000 | 49995000 | 49995000 +(1 row) + rollback; -- -- Clean up anything not created in the transaction @@ -574,6 +1197,8 @@ rollback; drop table names; drop index names2_fullname_idx; drop table names2; +drop index names3_fullname_idx; +drop table names3; drop index names4_fullname_idx; drop table names4; drop table testdef; @@ -582,4 +1207,8 @@ drop function bdefault_unsafe; drop function cdefault_restricted; drop function ddefault_safe; drop function fullname_parallel_unsafe; +drop function fullname_parallel_safe; drop function fullname_parallel_restricted; +drop function lastname_startswithe_u; +drop function lastname_startswithe_s; +drop function lastname_startswithe_r; diff --git a/src/test/regress/sql/insert_parallel.sql b/src/test/regress/sql/insert_parallel.sql index 70333a90b3..6577d5593d 100644 --- a/src/test/regress/sql/insert_parallel.sql +++ b/src/test/regress/sql/insert_parallel.sql @@ -15,15 +15,41 @@ create or replace function fullname_parallel_unsafe(f text, l text) returns text end; $$ language plpgsql immutable parallel unsafe; +create or replace function fullname_parallel_safe(f text, l text) returns text as $$ + begin + return f || l; + end; +$$ language plpgsql immutable parallel safe; + create or replace function fullname_parallel_restricted(f text, l text) returns text as $$ begin return f || l; end; $$ language plpgsql immutable parallel restricted; +create or replace function lastname_startswithe_u(last_name text) returns boolean as $$ + begin + return substring(last_name from 1 for 1) = 'e'; + end; +$$ language plpgsql immutable parallel unsafe; + +create or replace function lastname_startswithe_s(last_name text) returns boolean as $$ + begin + return substring(last_name from 1 for 1) = 'e'; + end; +$$ language plpgsql immutable parallel safe; + +create or replace function lastname_startswithe_r(last_name text) returns boolean as $$ + begin + return substring(last_name from 1 for 1) = 'e'; + end; +$$ language plpgsql immutable parallel restricted; + create table names(index int, first_name text, last_name text); create table names2(index int, first_name text, last_name text); create index names2_fullname_idx on names2 (fullname_parallel_unsafe(first_name, last_name)); +create table names3(index int, first_name text, last_name text); +create index names3_fullname_idx on names3 (fullname_parallel_safe(first_name, last_name)); create table names4(index int, first_name text, last_name text); create index names4_fullname_idx on names4 (fullname_parallel_restricted(first_name, last_name)); @@ -139,6 +165,14 @@ create table test_data1(like test_data); explain (costs off) insert into test_data1 select * from test_data where a = 10 returning a as data; insert into test_data1 select * from test_data where a = 10 returning a as data; +-- +-- Test INSERT with RETURNING clause (ordered SELECT). +-- (should create plan with parallel SELECT, GatherMerge parent node) +-- +truncate test_data1; +explain (costs off) insert into test_data1 select * from test_data where a <= 5 order by a returning a as data; +insert into test_data1 select * from test_data where a <= 5 order by a returning a as data; + -- -- Test INSERT into a table with a foreign key. -- (Insert into a table with a foreign key is parallel-restricted, @@ -150,6 +184,29 @@ insert into para_insert_f1 select unique1, stringu1 from tenk1; -- select some values to verify that the insert worked select count(*), sum(unique1) from para_insert_f1; +-- +-- Test INSERT with underlying query, leader participation disabled +-- +set parallel_leader_participation = off; +truncate para_insert_p1 cascade; +explain (costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 <= 2500; +insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 <= 2500; +select count(*), sum(unique1) from para_insert_p1; +select * from para_insert_p1 where unique1 >= 2490 order by unique1; + +-- +-- Test INSERT with underlying query, leader participation disabled +-- and no workers available +set max_parallel_workers=0; +truncate para_insert_p1 cascade; +explain (costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 <= 2500; +insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 <= 2500; +select count(*), sum(unique1) from para_insert_p1; +select * from para_insert_p1 where unique1 >= 2490 order by unique1; + +reset parallel_leader_participation; +reset max_parallel_workers; + -- -- Test INSERT with ON CONFLICT ... DO UPDATE ... -- (should not create a parallel plan) @@ -159,6 +216,70 @@ explain (costs off) insert into test_conflict_table(id, somedata) select a, a fr insert into test_conflict_table(id, somedata) select a, a from test_data; explain (costs off) insert into test_conflict_table(id, somedata) select a, a from test_data ON CONFLICT(id) DO UPDATE SET somedata = EXCLUDED.somedata + 1; +-- +-- Test INSERT with parallelized aggregate +-- +create table tenk1_avg_data(count int, avg_unique1 int, avg_stringu1_len int); +explain (costs off) insert into tenk1_avg_data select count(*), avg(unique1), avg(length(stringu1)) from tenk1; +insert into tenk1_avg_data select count(*), avg(unique1), avg(length(stringu1)) from tenk1; +select * from tenk1_avg_data; + +-- +-- Test INSERT with parallel bitmap heap scan +-- +set enable_seqscan to off; +set enable_indexscan to off; +truncate para_insert_p1 cascade; +explain (costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 >= 7500; +insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 >= 7500; +-- select some values to verify that the insert worked +select * from para_insert_p1 where unique1 >= 9990 order by unique1; +reset enable_seqscan; +reset enable_indexscan; + +-- +-- Test INSERT with parallel append +-- +create table a_star_data(aa int); +explain (costs off) insert into a_star_data select aa from a_star where aa > 10; +insert into a_star_data select aa from a_star where aa > 10; +select count(aa), sum(aa) from a_star_data; + +-- +-- Test INSERT with parallel index scan +-- +set enable_seqscan to off; +set enable_bitmapscan to off; +set min_parallel_index_scan_size=0; + +truncate para_insert_p1 cascade; +explain (costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 >= 500; +insert into para_insert_p1 select unique1, stringu1 from tenk1 where unique1 >= 500; +-- select some values to verify that the parallel insert worked +select count(*), sum(unique1) from para_insert_p1; +select * from para_insert_p1 where unique1 >= 9990 order by unique1; + +-- +-- Test INSERT with parallel index-only scan +-- +truncate para_insert_p1 cascade; +explain (costs off) insert into para_insert_p1 select unique1 from tenk1 where unique1 >= 500; +insert into para_insert_p1 select unique1 from tenk1 where unique1 >= 500; +-- select some values to verify that the parallel insert worked +select count(*), sum(unique1) from para_insert_p1; +select unique1 from para_insert_p1 where unique1 >= 9990 order by unique1; + +reset min_parallel_index_scan_size; +reset enable_seqscan; +reset enable_bitmapscan; + +-- +-- Test INSERT with parallel-safe index expression +-- (should create a parallel plan) +-- +explain (costs off) insert into names3 select * from names; +insert into names3 select * from names; +select * from names3 order by fullname_parallel_safe(first_name, last_name); -- -- Test INSERT with parallel-unsafe index expression @@ -195,6 +316,31 @@ create table names7 (like names); explain (costs off) insert into names7 select * from names order by last_name returning last_name || ', ' || first_name as last_name_then_first_name; insert into names7 select * from names order by last_name returning last_name || ', ' || first_name as last_name_then_first_name; +-- +-- Test INSERT with parallel-safe index predicate +-- (should create a parallel plan) +-- +create table names8 (like names); +create index names8_lastname_partial_idx on names8(index, last_name) where lastname_startswithe_s(last_name); +explain (costs off) insert into names8 select * from names; +insert into names8 select * from names; + +-- +-- Test INSERT with parallel-unsafe index predicate +-- (should not create a parallel plan) +-- +create table names9 (like names); +create index names9_lastname_partial_idx on names9(index, last_name) where lastname_startswithe_u(last_name); +explain (costs off) insert into names9 select * from names; + +-- +-- Test INSERT with parallel-restricted index predicate +-- (should create a parallel plan) +-- +create table names10 (like names); +create index names10_lastname_partial_idx on names10(index, last_name) where lastname_startswithe_r(last_name); +explain (costs off) insert into names10 select * from names; +insert into names10 select * from names; -- -- Test INSERT into temporary table with underlying query. @@ -209,6 +355,19 @@ insert into temp_names select * from names; -- Test INSERT with column defaults -- -- +-- a: no default +-- b: unsafe default +-- c: restricted default +-- d: safe default +-- + +-- +-- No column defaults, should use parallel SELECT +-- +explain (costs off) insert into testdef(a,b,c,d) select a,a*2,a*4,a*8 from test_data; +insert into testdef(a,b,c,d) select a,a*2,a*4,a*8 from test_data; +select * from testdef order by a; +truncate testdef; -- -- Parallel unsafe column default, should not use a parallel plan @@ -223,6 +382,14 @@ insert into testdef(a,b,d) select a,a*2,a*8 from test_data; select * from testdef order by a; truncate testdef; +-- +-- Parallel safe column default, should use parallel SELECT +-- +explain (costs off) insert into testdef(a,b,c) select a,a*2,a*4 from test_data; +insert into testdef(a,b,c) select a,a*2,a*4 from test_data; +select * from testdef order by a; +truncate testdef; + -- -- Parallel restricted and unsafe column defaults, should not use a parallel plan -- @@ -255,6 +422,46 @@ insert into parttable1 select unique1,stringu1 from tenk1; select count(*) from parttable1_1; select count(*) from parttable1_2; +-- +-- Test INSERT into partition with parallel-unsafe partition key support function +-- (should not create a parallel plan) +-- +create function my_int4_sort(int4,int4) returns int language sql + as $$ select case when $1 = $2 then 0 when $1 > $2 then 1 else -1 end; $$; + +create operator class test_int4_ops for type int4 using btree as + operator 1 < (int4,int4), operator 2 <= (int4,int4), + operator 3 = (int4,int4), operator 4 >= (int4,int4), + operator 5 > (int4,int4), function 1 my_int4_sort(int4,int4); + +create table partkey_unsafe_key_supp_fn_t (a int4, b name) partition by range (a test_int4_ops); +create table partkey_unsafe_key_supp_fn_t_1 partition of partkey_unsafe_key_supp_fn_t for values from (0) to (5000); +create table partkey_unsafe_key_supp_fn_t_2 partition of partkey_unsafe_key_supp_fn_t for values from (5000) to (10000); + +explain (costs off) insert into partkey_unsafe_key_supp_fn_t select unique1, stringu1 from tenk1; + +-- +-- Test INSERT into partition with parallel-unsafe partition key expression +-- (should not create a parallel plan) +-- +create table partkey_unsafe_key_expr_t (a int4, b name) partition by range ((fullname_parallel_unsafe('',a::varchar))); +explain (costs off) insert into partkey_unsafe_key_expr_t select unique1, stringu1 from tenk1; + +-- +-- Test INSERT into table with parallel-safe check constraint +-- (should create a parallel plan) +-- +create or replace function check_a(a int4) returns boolean as $$ + begin + return (a >= 0 and a <= 9999); + end; +$$ language plpgsql parallel safe; + +create table table_check_a(a int4 check (check_a(a)), b name); +explain (costs off) insert into table_check_a select unique1, stringu1 from tenk1; +insert into table_check_a select unique1, stringu1 from tenk1; +select count(*), sum(a) from table_check_a; + -- -- Test INSERT into table with parallel-unsafe check constraint -- (should not create a parallel plan) @@ -269,37 +476,78 @@ create table table_check_b(a int4, b name check (check_b_unsafe(b)), c name); explain (costs off) insert into table_check_b(a,b,c) select unique1, unique2, stringu1 from tenk1; -- --- Test INSERT into table with parallel-safe after stmt-level triggers +-- Test INSERT into table with parallel-safe before+after stmt-level triggers -- (should create a parallel SELECT plan; triggers should fire) -- create table names_with_safe_trigger (like names); +create or replace function insert_before_trigger_safe() returns trigger as $$ + begin + raise notice 'hello from insert_before_trigger_safe'; + return new; + end; +$$ language plpgsql parallel safe; create or replace function insert_after_trigger_safe() returns trigger as $$ begin raise notice 'hello from insert_after_trigger_safe'; return new; end; $$ language plpgsql parallel safe; +create trigger insert_before_trigger_safe before insert on names_with_safe_trigger + for each statement execute procedure insert_before_trigger_safe(); create trigger insert_after_trigger_safe after insert on names_with_safe_trigger for each statement execute procedure insert_after_trigger_safe(); explain (costs off) insert into names_with_safe_trigger select * from names; insert into names_with_safe_trigger select * from names; -- --- Test INSERT into table with parallel-unsafe after stmt-level triggers +-- Test INSERT into table with parallel-unsafe before+after stmt-level triggers -- (should not create a parallel plan; triggers should fire) -- create table names_with_unsafe_trigger (like names); +create or replace function insert_before_trigger_unsafe() returns trigger as $$ + begin + raise notice 'hello from insert_before_trigger_unsafe'; + return new; + end; +$$ language plpgsql parallel unsafe; create or replace function insert_after_trigger_unsafe() returns trigger as $$ begin raise notice 'hello from insert_after_trigger_unsafe'; return new; end; $$ language plpgsql parallel unsafe; +create trigger insert_before_trigger_unsafe before insert on names_with_unsafe_trigger + for each statement execute procedure insert_before_trigger_unsafe(); create trigger insert_after_trigger_unsafe after insert on names_with_unsafe_trigger for each statement execute procedure insert_after_trigger_unsafe(); explain (costs off) insert into names_with_unsafe_trigger select * from names; insert into names_with_unsafe_trigger select * from names; +-- +-- Test INSERT into table with parallel-restricted before+after stmt-level trigger +-- (should create a parallel plan with parallel SELECT; +-- stmt-level before+after triggers should fire) +-- +create table names_with_restricted_trigger (like names); +create or replace function insert_before_trigger_restricted() returns trigger as $$ + begin + raise notice 'hello from insert_before_trigger_restricted'; + return new; + end; +$$ language plpgsql parallel restricted; +create or replace function insert_after_trigger_restricted() returns trigger as $$ + begin + raise notice 'hello from insert_after_trigger_restricted'; + return new; + end; +$$ language plpgsql parallel restricted; +create trigger insert_before_trigger_restricted before insert on names_with_restricted_trigger + for each statement execute procedure insert_before_trigger_restricted(); +create trigger insert_after_trigger_restricted after insert on names_with_restricted_trigger + for each statement execute procedure insert_after_trigger_restricted(); +explain (costs off) insert into names_with_restricted_trigger select * from names; +insert into names_with_restricted_trigger select * from names; + -- -- Test INSERT into partition with parallel-unsafe trigger -- (should not create a parallel plan) @@ -333,6 +581,17 @@ function make_table_bar(); -- should create a non-parallel plan explain (costs off) execute q; +-- +-- Test INSERT into table with TOAST column +-- +create table insert_toast_table(index int4, data text); +create table insert_toast_table_data (like insert_toast_table); +insert into insert_toast_table_data select i, rpad('T', 16384, 'ABCDEFGH') from generate_series(1,20) as i; +explain (costs off) insert into insert_toast_table select index, data from insert_toast_table_data; +insert into insert_toast_table select index, data from insert_toast_table_data; +select count(*) as row_count, sum(length(data)) as total_data_length from insert_toast_table; +truncate insert_toast_table; + -- -- Test INSERT into table having a DOMAIN column with a CHECK constraint -- @@ -340,15 +599,48 @@ create function sql_is_distinct_from_u(anyelement, anyelement) returns boolean language sql parallel unsafe as 'select $1 is distinct from $2 limit 1'; +create or replace function sql_is_distinct_from_r(a anyelement, b anyelement) returns boolean as $$ + begin + return (a <> b); + end; +$$ language plpgsql parallel restricted; + +create or replace function sql_is_distinct_from_s(a anyelement, b anyelement) returns boolean as $$ + begin + return (a <> b); + end; +$$ language plpgsql parallel safe; + create domain inotnull_u int check (sql_is_distinct_from_u(value, null)); +create domain inotnull_r int + check (sql_is_distinct_from_r(value, null)); + +create domain inotnull_s int + check (sql_is_distinct_from_s(value, null)); + create table dom_table_u (x inotnull_u, y int); +create table dom_table_r (x inotnull_r, y int); +create table dom_table_s (x inotnull_s, y int); -- Test INSERT into table having a DOMAIN column with parallel-unsafe CHECK constraint explain (costs off) insert into dom_table_u select unique1, unique2 from tenk1; +-- Test INSERT into table having a DOMAIN column with parallel-restricted CHECK constraint +explain (costs off) insert into dom_table_r select unique1, unique2 from tenk1; +insert into dom_table_r select unique1, unique2 from tenk1; +select count(*), sum(x) as sum_x, sum(y) as sum_y from dom_table_r; + +-- Test INSERT into table having a DOMAIN column with parallel-safe CHECK constraint +-- NOTE: Currently max_parallel_hazard() regards CoerceToDomain as parallel-restricted +explain (costs off) insert into dom_table_s select unique1, unique2 from tenk1; +insert into dom_table_s select unique1, unique2 from tenk1; +select count(*), sum(x) as sum_x, sum(y) as sum_y from dom_table_s; + + + rollback; @@ -359,6 +651,8 @@ rollback; drop table names; drop index names2_fullname_idx; drop table names2; +drop index names3_fullname_idx; +drop table names3; drop index names4_fullname_idx; drop table names4; drop table testdef; @@ -368,4 +662,8 @@ drop function bdefault_unsafe; drop function cdefault_restricted; drop function ddefault_safe; drop function fullname_parallel_unsafe; +drop function fullname_parallel_safe; drop function fullname_parallel_restricted; +drop function lastname_startswithe_u; +drop function lastname_startswithe_s; +drop function lastname_startswithe_r; -- 2.27.0
v26-0002-Add-new-GUC-option-enable_parallel_insert-boolean.patch (application/octet-stream, 19.8K) download From 35b6d0aff37a34aa41da7f80f19b78a200b61ddd Mon Sep 17 00:00:00 2001 From: "houzj.fnst" <houzj.fnst@cn.fujitsu.com> Date: Thu, 11 Mar 2021 09:11:22 +0800 Subject: [PATCH] Add new GUC option enable_parallel_insert (boolean) and table option "parallel_insert_enabled" (boolean). The current implementation of parallel SELECT for "INSERT INTO ... SELECT ..." may incur non-negligible overhead in the additional parallel-safety checks that it performs, even when, in the end, those checks determine that parallelism can't be used. This is normally only ever a problem for large complex tables, particularly in the case of when the target table has a large number of partitions. To address this potential isse, a new GUC option "enable_parallel_insert" is added, to allow parallel insert to be enabled/disabled. The default is on. In addition to the GUC option, the user may want a mechanism for specifying parallel insert with finer granularity, to enable/disable the use of parallel insert for specific tables. The new table option "parallel_insert_enabled" allows this. The default is true. Author: "Hou, Zhijie" <houzj.fnst@cn.fujitsu.com> Discussion: https://www.postgresql.org/message-id/flat/CAA4eK1K-cW7svLC2D7DHoGHxdAdg3P37BLgebqBOC2ZLc9a6QQ%40mail.gmail.com --- doc/src/sgml/config.sgml | 23 ++++++++ doc/src/sgml/ref/alter_table.sgml | 2 +- doc/src/sgml/ref/create_table.sgml | 27 +++++++++ src/backend/access/common/reloptions.c | 25 +++++++-- src/backend/optimizer/path/costsize.c | 2 + src/backend/optimizer/util/clauses.c | 29 +++++++++- src/backend/utils/misc/guc.c | 11 ++++ src/backend/utils/misc/postgresql.conf.sample | 1 + src/bin/psql/tab-complete.c | 1 + src/include/optimizer/cost.h | 1 + src/include/utils/rel.h | 23 ++++++++ src/test/regress/expected/insert_parallel.out | 56 ++++++++++++++++++- src/test/regress/expected/sysviews.out | 3 +- src/test/regress/sql/insert_parallel.sql | 44 ++++++++++++++- 14 files changed, 233 insertions(+), 15 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 967de73596..9264b5b542 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -5072,6 +5072,29 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class=" </listitem> </varlistentry> + <varlistentry id="guc-enable-parallel-insert" xreflabel="enable_parallel_insert"> + <term><varname>enable_parallel_insert</varname> (<type>boolean</type>) + <indexterm> + <primary><varname>enable_parallel_insert</varname> configuration parameter</primary> + </indexterm> + </term> + <listitem> + <para> + Enables or disables the query planner's use of parallel plans for + <command>INSERT</command> commands. The default is <literal>on</literal>. + When enabled, the planner performs additional parallel-safety checks + on the target table's attributes and indexes, in order to determine + if it's safe to use a parallel plan for <command>INSERT</command>. In cases + such as when the target table has a large number of partitions, and + particularly also when that table uses something parallel-unsafe that + prevents parallelism, the overhead of these checks may become + prohibitively high. To address this potential overhead in these cases, + this option can be used to disable the use of parallel plans for + <command>INSERT</command>. + </para> + </listitem> + </varlistentry> + </variablelist> </sect2> <sect2 id="runtime-config-query-constants"> diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml index c25ef5abd6..99a56f856c 100644 --- a/doc/src/sgml/ref/alter_table.sgml +++ b/doc/src/sgml/ref/alter_table.sgml @@ -722,7 +722,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM <para> <literal>SHARE UPDATE EXCLUSIVE</literal> lock will be taken for fillfactor, toast and autovacuum storage parameters, as well as the - planner parameter <varname>parallel_workers</varname>. + planner parameter <varname>parallel_workers</varname> and <varname>parallel_insert_enabled</varname>. </para> </listitem> </varlistentry> diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml index 3b2b227683..00aa243b7f 100644 --- a/doc/src/sgml/ref/create_table.sgml +++ b/doc/src/sgml/ref/create_table.sgml @@ -1408,6 +1408,33 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM </listitem> </varlistentry> + <varlistentry id="reloption-parallel-insert-enabled" xreflabel="parallel_insert_enabled"> + <term><literal>parallel_insert_enabled</literal> (<type>boolean</type>) + <indexterm> + <primary><varname>parallel_insert_enabled</varname> storage parameter</primary> + </indexterm> + </term> + <listitem> + <para> + Enables or disables the query planner's use of parallel insert for + this table. When enabled (and provided that + <xref linkend="guc-enable-parallel-insert"/> is also <literal>true</literal>), + the planner performs additional parallel-safety checks on the table's + attributes and indexes, in order to determine if it's safe to use a + parallel plan for <command>INSERT</command>. The default is + <literal>true</literal>. + In cases such as when the table has a large number of partitions, and + particularly also when that table uses a parallel-unsafe feature that + prevents parallelism, the overhead of these checks may become prohibitively + high. To address this potential overhead in these cases, this option can be + used to disable the use of parallel insert for this table. + Note that if the target table of the parallel insert is partitioned, the + <literal>parallel_insert_enabled</literal> option values of the partitions are + ignored. + </para> + </listitem> + </varlistentry> + <varlistentry id="reloption-autovacuum-enabled" xreflabel="autovacuum_enabled"> <term><literal>autovacuum_enabled</literal>, <literal>toast.autovacuum_enabled</literal> (<type>boolean</type>) <indexterm> diff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.c index c687d3ee9e..b582a7b87d 100644 --- a/src/backend/access/common/reloptions.c +++ b/src/backend/access/common/reloptions.c @@ -168,6 +168,15 @@ static relopt_bool boolRelOpts[] = }, true }, + { + { + "parallel_insert_enabled", + "Enables \"parallel insert\" feature for this table", + RELOPT_KIND_HEAP | RELOPT_KIND_PARTITIONED, + ShareUpdateExclusiveLock + }, + true + }, /* list terminator */ {{NULL}} }; @@ -1859,7 +1868,9 @@ default_reloptions(Datum reloptions, bool validate, relopt_kind kind) {"vacuum_index_cleanup", RELOPT_TYPE_BOOL, offsetof(StdRdOptions, vacuum_index_cleanup)}, {"vacuum_truncate", RELOPT_TYPE_BOOL, - offsetof(StdRdOptions, vacuum_truncate)} + offsetof(StdRdOptions, vacuum_truncate)}, + {"parallel_insert_enabled", RELOPT_TYPE_BOOL, + offsetof(StdRdOptions, parallel_insert_enabled)} }; return (bytea *) build_reloptions(reloptions, validate, kind, @@ -1961,13 +1972,15 @@ build_local_reloptions(local_relopts *relopts, Datum options, bool validate) bytea * partitioned_table_reloptions(Datum reloptions, bool validate) { - /* - * There are no options for partitioned tables yet, but this is able to do - * some validation. - */ + static const relopt_parse_elt tab[] = { + {"parallel_insert_enabled", RELOPT_TYPE_BOOL, + offsetof(PartitionedOptions, parallel_insert_enabled)} + }; + return (bytea *) build_reloptions(reloptions, validate, RELOPT_KIND_PARTITIONED, - 0, NULL, 0); + sizeof(PartitionedOptions), + tab, lengthof(tab)); } /* diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index a25b674a19..c81e2cf244 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -129,6 +129,8 @@ Cost disable_cost = 1.0e10; int max_parallel_workers_per_gather = 2; +bool enable_parallel_insert = true; + bool enable_seqscan = true; bool enable_indexscan = true; bool enable_indexonlyscan = true; diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index c416263749..46c6e65c49 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1272,8 +1272,10 @@ target_rel_chk_constr_max_parallel_hazard(Relation rel, * * It's not possible in the following cases: * - * 1) INSERT...ON CONFLICT...DO UPDATE - * 2) INSERT without SELECT + * 1) enable_parallel_insert is off + * 2) INSERT...ON CONFLICT...DO UPDATE + * 3) INSERT without SELECT + * 4) the reloption parallel_insert_enabled is set to off * * (Note: we don't do in-depth parallel-safety checks here, we do only the * cheaper tests that can quickly exclude obvious cases for which @@ -1284,12 +1286,17 @@ bool is_parallel_allowed_for_modify(Query *parse) { bool hasSubQuery; + bool parallel_enabled; RangeTblEntry *rte; ListCell *lc; + Relation rel; if (!IsModifySupportedInParallelMode(parse->commandType)) return false; + if (!enable_parallel_insert) + return false; + /* * UPDATE is not currently supported in parallel-mode, so prohibit * INSERT...ON CONFLICT...DO UPDATE... @@ -1320,7 +1327,23 @@ is_parallel_allowed_for_modify(Query *parse) } } - return hasSubQuery; + if (!hasSubQuery) + return false; + + /* + * Check if parallel_insert_enabled is enabled for the target table, + * if not, skip the safety checks. + * + * (Note: if the target table is partitioned, the parallel_insert_enabled + * option setting of the partitions are ignored). + */ + rte = rt_fetch(parse->resultRelation, parse->rtable); + rel = table_open(rte->relid, NoLock); + + parallel_enabled = RelationGetParallelInsert(rel, true); + table_close(rel, NoLock); + + return parallel_enabled; } /***************************************************************************** diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 3fd1a5fbe2..3e45cf627c 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -2049,6 +2049,17 @@ static struct config_bool ConfigureNamesBool[] = NULL, NULL, NULL }, + { + {"enable_parallel_insert", PGC_USERSET, QUERY_TUNING_METHOD, + gettext_noop("Enables the planner's use of parallel plans for INSERT commands."), + NULL, + GUC_EXPLAIN + }, + &enable_parallel_insert, + true, + NULL, NULL, NULL + }, + /* End-of-list marker */ { {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index ee06528bb0..ac804f4877 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -371,6 +371,7 @@ #enable_partitionwise_aggregate = off #enable_parallel_hash = on #enable_partition_pruning = on +#enable_parallel_insert = on # - Planner Cost Constants - diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 9f0208ac49..2ac98bb10f 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1118,6 +1118,7 @@ static const char *const table_storage_parameters[] = { "autovacuum_vacuum_threshold", "fillfactor", "log_autovacuum_min_duration", + "parallel_insert_enabled", "parallel_workers", "toast.autovacuum_enabled", "toast.autovacuum_freeze_max_age", diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h index 1be93be098..22e6db96b6 100644 --- a/src/include/optimizer/cost.h +++ b/src/include/optimizer/cost.h @@ -47,6 +47,7 @@ typedef enum /* parameter variables and flags (see also optimizer.h) */ extern PGDLLIMPORT Cost disable_cost; extern PGDLLIMPORT int max_parallel_workers_per_gather; +extern PGDLLIMPORT bool enable_parallel_insert; extern PGDLLIMPORT bool enable_seqscan; extern PGDLLIMPORT bool enable_indexscan; extern PGDLLIMPORT bool enable_indexonlyscan; diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h index 10b63982c0..982e3db1a8 100644 --- a/src/include/utils/rel.h +++ b/src/include/utils/rel.h @@ -306,6 +306,7 @@ typedef struct StdRdOptions int parallel_workers; /* max number of parallel workers */ bool vacuum_index_cleanup; /* enables index vacuuming and cleanup */ bool vacuum_truncate; /* enables vacuum to truncate a relation */ + bool parallel_insert_enabled; /* enables planner's use of parallel insert */ } StdRdOptions; #define HEAP_MIN_FILLFACTOR 10 @@ -423,6 +424,28 @@ typedef struct ViewOptions ((ViewOptions *) (relation)->rd_options)->check_option == \ VIEW_OPTION_CHECK_OPTION_CASCADED) +/* + * PartitionedOptions + * Contents of rd_options for partitioned tables + */ +typedef struct PartitionedOptions +{ + int32 vl_len_; /* varlena header (do not touch directly!) */ + bool parallel_insert_enabled; /* enables planner's use of parallel insert */ +} PartitionedOptions; + +/* + * RelationGetParallelInsert + * Returns the relation's parallel_insert_enabled reloption setting. + * Note multiple eval of argument! + */ +#define RelationGetParallelInsert(relation, defaultpd) \ + ((relation)->rd_options ? \ + (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ? \ + ((PartitionedOptions *) (relation)->rd_options)->parallel_insert_enabled :\ + ((StdRdOptions *) (relation)->rd_options)->parallel_insert_enabled) : \ + (defaultpd)) + /* * RelationIsValid * True iff relation descriptor is valid. diff --git a/src/test/regress/expected/insert_parallel.out b/src/test/regress/expected/insert_parallel.out index d5fae79031..cb89453a14 100644 --- a/src/test/regress/expected/insert_parallel.out +++ b/src/test/regress/expected/insert_parallel.out @@ -63,12 +63,45 @@ set max_parallel_workers_per_gather=4; create table para_insert_p1 ( unique1 int4 PRIMARY KEY, stringu1 name -); +) with (parallel_insert_enabled = off); create table para_insert_f1 ( unique1 int4 REFERENCES para_insert_p1(unique1), stringu1 name ); -- +-- Disable guc option enable_parallel_insert +-- +set enable_parallel_insert = off; +-- Test INSERT with underlying query when enable_parallel_insert=off and reloption.parallel_insert_enabled=off. +-- (should create plan with serial INSERT + SELECT) +-- +explain(costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1; + QUERY PLAN +-------------------------- + Insert on para_insert_p1 + -> Seq Scan on tenk1 +(2 rows) + +-- +-- Enable guc option enable_parallel_insert +-- +set enable_parallel_insert = on; +-- +-- Test INSERT with underlying query when enable_parallel_insert=on and reloption.parallel_insert_enabled=off. +-- (should create plan with serial INSERT + SELECT) +-- +explain(costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1; + QUERY PLAN +-------------------------- + Insert on para_insert_p1 + -> Seq Scan on tenk1 +(2 rows) + +-- +-- Enable reloption parallel_insert_enabled +-- +alter table para_insert_p1 set (parallel_insert_enabled = on); +-- -- Test INSERT with underlying query. -- (should create plan with parallel SELECT, Gather parent node) -- @@ -364,9 +397,28 @@ explain (costs off) insert into testdef(a,d) select a,a*8 from test_data; -- -- Test INSERT into partition with underlying query. -- -create table parttable1 (a int, b name) partition by range (a); +create table parttable1 (a int, b name) partition by range (a) with (parallel_insert_enabled=off); create table parttable1_1 partition of parttable1 for values from (0) to (5000); create table parttable1_2 partition of parttable1 for values from (5000) to (10000); +-- +-- Test INSERT into partition when reloption.parallel_insert_enabled=off +-- (should not create a parallel plan) +-- +explain (costs off) insert into parttable1 select unique1,stringu1 from tenk1; + QUERY PLAN +------------------------- + Insert on parttable1 + -> Seq Scan on tenk1 +(2 rows) + +-- +-- Enable reloption parallel_insert_enabled +-- +alter table parttable1 set (parallel_insert_enabled = on); +-- +-- Test INSERT into partition when reloption.parallel_insert_enabled=on +-- (should create a parallel plan) +-- explain (costs off) insert into parttable1 select unique1,stringu1 from tenk1; QUERY PLAN ---------------------------------------- diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 6d048e309c..a62bf5dc92 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -107,13 +107,14 @@ select name, setting from pg_settings where name like 'enable%'; enable_nestloop | on enable_parallel_append | on enable_parallel_hash | on + enable_parallel_insert | on enable_partition_pruning | on enable_partitionwise_aggregate | off enable_partitionwise_join | off enable_seqscan | on enable_sort | on enable_tidscan | on -(18 rows) +(19 rows) -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail diff --git a/src/test/regress/sql/insert_parallel.sql b/src/test/regress/sql/insert_parallel.sql index 70ad31a087..5092990a5b 100644 --- a/src/test/regress/sql/insert_parallel.sql +++ b/src/test/regress/sql/insert_parallel.sql @@ -79,13 +79,38 @@ set max_parallel_workers_per_gather=4; create table para_insert_p1 ( unique1 int4 PRIMARY KEY, stringu1 name -); +) with (parallel_insert_enabled = off); create table para_insert_f1 ( unique1 int4 REFERENCES para_insert_p1(unique1), stringu1 name ); +-- +-- Disable guc option enable_parallel_insert +-- +set enable_parallel_insert = off; + +-- Test INSERT with underlying query when enable_parallel_insert=off and reloption.parallel_insert_enabled=off. +-- (should create plan with serial INSERT + SELECT) +-- +explain(costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1; + +-- +-- Enable guc option enable_parallel_insert +-- +set enable_parallel_insert = on; + +-- +-- Test INSERT with underlying query when enable_parallel_insert=on and reloption.parallel_insert_enabled=off. +-- (should create plan with serial INSERT + SELECT) +-- +explain(costs off) insert into para_insert_p1 select unique1, stringu1 from tenk1; + +-- +-- Enable reloption parallel_insert_enabled +-- +alter table para_insert_p1 set (parallel_insert_enabled = on); -- -- Test INSERT with underlying query. @@ -210,10 +235,25 @@ explain (costs off) insert into testdef(a,d) select a,a*8 from test_data; -- -- Test INSERT into partition with underlying query. -- -create table parttable1 (a int, b name) partition by range (a); +create table parttable1 (a int, b name) partition by range (a) with (parallel_insert_enabled=off); create table parttable1_1 partition of parttable1 for values from (0) to (5000); create table parttable1_2 partition of parttable1 for values from (5000) to (10000); +-- +-- Test INSERT into partition when reloption.parallel_insert_enabled=off +-- (should not create a parallel plan) +-- +explain (costs off) insert into parttable1 select unique1,stringu1 from tenk1; + +-- +-- Enable reloption parallel_insert_enabled +-- +alter table parttable1 set (parallel_insert_enabled = on); + +-- +-- Test INSERT into partition when reloption.parallel_insert_enabled=on +-- (should create a parallel plan) +-- explain (costs off) insert into parttable1 select unique1,stringu1 from tenk1; insert into parttable1 select unique1,stringu1 from tenk1; select count(*) from parttable1_1; -- 2.18.4

view thread (363+ messages)  latest in thread

Message-ID: <OS3PR01MB571802ACDB91AF35ADC97A7F94909@OS3PR01MB5718.jpnprd01.prod.outlook.com>
Permalink:  ../OS3PR01MB571802ACDB91AF35ADC97A7F94909@OS3PR01MB5718.jpnprd01.prod.outlook.com/
Also on:    postgresql.org/message-id/OS3PR01MB571802ACDB91AF35ADC97A7F94909@OS3PR01MB5718.jpnprd01.prod.outlook.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: houzj.fnst@fujitsu.com, gregn4422@gmail.com, amit.kapila16@gmail.com, vignesh21@gmail.com, dgrowleyml@gmail.com, tgl@sss.pgh.pa.us, tsunakawa.takay@fujitsu.com, tanghy.fnst@fujitsu.com, amitlangote09@gmail.com
  Subject: Re: Parallel INSERT (INTO ... SELECT ...)
  In-Reply-To: <OS3PR01MB571802ACDB91AF35ADC97A7F94909@OS3PR01MB5718.jpnprd01.prod.outlook.com>
  References: <CAA4eK1+WBN99GOc8MwHfFoa7c-ssy+gYcbGVL3b8+A04pQKwWA@mail.gmail.com> <CAJcOf-ed4k2TAucEOF2n1+zHGFiB2fwOGrsE_2i8kXuHZfM8=Q@mail.gmail.com> <OS3PR01MB571802ACDB91AF35ADC97A7F94909@OS3PR01MB5718.jpnprd01.prod.outlook.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox