<div dir="ltr">Hi,<div><br></div><div>Playing with new PG9.6rc1 / Pgis 2.3beta1, I found parallel query mode to be really efficient to process big tables.</div><div>Unfortunately, it is not possible to directly create tables with parallel plan (create table as select...) (see: <a href="https://wiki.postgresql.org/wiki/Parallel_Query">https://wiki.postgresql.org/wiki/Parallel_Query</a>).</div><div>It is possible, though, to use copy mode with psql feed to create a table with parallel plan enabled.</div><div><br></div><div>To allow creating tables directly in pure SQL script, I developed a small hack function that takes a SQL query and creates a table from it, using COPY command with psql PROGRAM executing the query.</div><div><br></div><div>Usage:</div><div><br></div><div><div><font face="monospace, monospace">select * from create_table_parallel(</font></div><div><font face="monospace, monospace">    'table_name',</font></div><div><font face="monospace, monospace">    'select <a href="http://p.id" target="_blank">p.id</a> as idparc, c.gid as idcarreau</font></div><div><font face="monospace, monospace">        st_intersection(p.geom, c.geom) as geom</font></div><div><font face="monospace, monospace">      from parcelle_sample2 p</font></div><div><font face="monospace, monospace">      join carreau_sample2 c on st_intersects(p.geom, c.geom)',</font></div><div><font face="monospace, monospace">    '/usr/local/pgsql-9.6/bin/psql -A -t -p 5439 -d nicolas -c',</font></div><div><font face="monospace, monospace">    8, -- workers</font></div><div><font face="monospace, monospace">    true);</font></div></div><div><br></div><div>Limitations:</div><div><br></div><div>• delimiter used for copy operation defaults to '|'</div><div>• a 'LIMIT 0' clause is inserted at the end of the passed query to create table structure: query to run cannot contain a LIMIT clause.</div><div>• The function is not safe, as it injects user parameters to build psql command, and it's not extensively tested.</div><div><br></div><div>Perfomance expected:</div><div><br></div><div>Depends on the number of workers configured and planned:</div><div>On a small dataset (~15 000 pg intersected with 360 000 pg), with 8 workers configured and 3 choosen by the planner, table creation took <b>24s vs 1m25s</b> with a traditionnal create table as select...</div><div><br></div><div>Nicolas</div></div>