Re: Parallel INSERT SELECT take 2

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: "Zhijie Hou (Fujitsu)" <houzj(dot)fnst(at)fujitsu(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, Dilip Kumar <dilipbalaut(at)gmail(dot)com>, Greg Nancarrow <gregn4422(at)gmail(dot)com>
Subject: Re: Parallel INSERT SELECT take 2
Date: 2026-11-26 12:55:16
Message-ID: e82c9331-34dc-42f0-82e6-de625d7f5e80@vondra.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

For comparison, here's an early PoC of the buffering idea. I've realized
we kinda already have this buffering capability - Materialize spills
tuples to tuplestore (but not eagerly). Having an executor node seems
like a convenient approach, because it then can be part of the regular
query planning etc. So I gave that a try.

0001 introduces a new TupleBuffer node (path + plan), mimicking what
Materialize does, but accumulating tuples eagerly (more like Sort), and
also taking over the parallel mode setup (instead of ExecutePlan). But
it's just an infrastructure (planner/executor/explain), the node is not
used anywhere yet.

0002 integrates the node into INSERT ... SELECT queries. With it enabled
(the GUC is "off" by default) a plan changes from

QUERY PLAN
-----------------------------------------
Insert on t
-> Subquery Scan on unnamed_subquery
-> HashAggregate
Group Key: s.a
-> Seq Scan on s
(5 rows)

to

QUERY PLAN
------------------------------------------------------------------
Insert on t
-> Subquery Scan on unnamed_subquery
-> TupleBuffer
-> Finalize GroupAggregate
Group Key: s.a
-> Gather Merge
Workers Planned: 2
-> Sort
Sort Key: s.a
-> Partial HashAggregate
Group Key: s.a
-> Parallel Seq Scan on s
(12 rows)

In EXPLAIN ANALYZE it prints more info (how much disk space etc.).

I think this works pretty well, but there are some problems to solve.
The main problem is that during planning we don't know for sure if the
plan actually is parallel. We can know for the "main" plan, but if there
are alternative subplans, then we don't know until after setrefs. The
actual plan gets selected by fix_alternative_subplan(). So we don't even
know for sure in create_plan().

The PoC deals with that by switching the buffer to "passthrough" mode
when the ultimate plan happens to be serial, i.e. the tuples are
returned immediately, without writing it to the tuplestore.

Maybe it's be simpler with a tuplestore integrated into the ModifyTable
node, not sure yet. It's also possible I missed something, and there's a
simpler way to handle this.

Another thing I'm not very happy with is the es_parallel_insert_select
flag, and root->glob->parallelInsert. It seems a bit cumbersome to me,
but I haven't found a better solution yet.

One thing that occurred to me is that the TupleBuffer might be useful
more widely. There are places where we disallow parallel query because
the execution might get suspended (say, when a client uses cursors,
which some libraries do automatically). If the client consumes just a
tiny fraction of tuples, that makes perfect sense. But if the client
consumes all tuples, maybe it'd be better to add TupleBuffer on top, and
then use parallel execution. Just a wild idea, though.

regards

--
Tomas Vondra

Attachment Content-Type Size
v1-PoC-buffer-0001-Add-TupleBuffer-an-eager-materializati.patch text/x-patch 30.5 KB
v1-PoC-buffer-0002-Allow-parallel-plans-for-the-SELECT-in.patch text/x-patch 36.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message vignesh C 2026-11-26 12:57:37 Re: Wrong table in "conflicting or redundant WHERE clauses / column lists" publicaiton error
Previous Message Álvaro Herrera 2026-11-26 12:45:23 Re: reltuples kept decreasing with each autovacuum run Re: BUG #18773