
-----------------prepare data------------------
--large data table 'x' prepare 2e tuples
create table x(a int, b int, c int);
create index on x(a);
insert into x select generate_series(1,200000000),floor(random()*(10001-1)+1),floor(random()*(10001-1)+1);

--tenk1 data prepare
CREATE TABLE tenk1 (
        unique1         int4,
        unique2         int4,
        two             int4,
        four            int4,
        ten             int4,
        twenty          int4,
        hundred         int4,
        thousand        int4,
        twothousand     int4,
        fivethous       int4,
        tenthous        int4,
        odd             int4,
        even            int4,
        stringu1        name,
        stringu2        name,
        string4         name
);
CREATE INDEX tenk1_unique1 ON tenk1 USING btree(unique1 int4_ops);

CREATE INDEX tenk1_unique2 ON tenk1 USING btree(unique2 int4_ops);

CREATE INDEX tenk1_hundred ON tenk1 USING btree(hundred int4_ops);

CREATE INDEX tenk1_thous_tenthous ON tenk1 (thousand, tenthous);

INSERT INTO tenk1 values(generate_series(1,10000000),generate_series(1,10000000),floor(random()*(3-1)+1),floor(random()*(5-1)+1),floor(random()*(11-1)+1),floor(random()*(21-1)+1),floor(random()*(101-1)+1),floor(random()*(1001-1)+1),floor(random()*(2001-1)+1),floor(random()*(5001-1)+1),floor(random()*(10001-1)+1),100,111,upper(substring(md5(random()::varchar),2,6)),upper(substring(md5(random()::varchar),2,6)),upper(substring(md5(random()::varchar),2,6)));

create table test_uni3 as select generate_series(1,100000) as a,generate_series(1,100000) as b,generate_series(1,100000) as c;


------------------begin test-------------------
-- Test NO 1. CTAS select from table, parallel insert
explain analyze verbose create table test(a, b, c) as select a,a*2,a*8 from x where a%8=0;

-- Test NO 2. Append plan, parallel insert
explain analyze verbose create table test(a, b, c) as select a,a*2,a*8 from test_uni3 union all select a,a*2,a*8 from x where b%8=0;

-- Test NO 3. initial plan under Gather node, parallel insert
explain analyze verbose create table test(a,b,c) as select a,b,(select distinct unique1 from tenk1 where unique2=1) from x where b%10=0;

-- Test NO 4. CTAS table, parallel insert
explain analyze verbose create table test(a,b,c) as table tenk1;

-- Test NO 5. CTAS select from execute, parallel insert
prepare prep_stmt as select a,a*2,a*8 from x where a%8=0;
explain analyze verbose create table test(a,b,c) as execute prep_stmt;