| 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-24 20:11:07 |
| Message-ID: | 6e5874ce-e680-4a0f-b4cf-5881a9197de9@vondra.me |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
I have looked at the last version of the patch v15 [1], so here's a
review. I tried to look for gaps / issues with this attempt to make the
original approach work. Unfortunately, I'm still rather skeptical about
this being workable :-(
1) When building, I get this compiler warning. I had to rebase the
patches, but I don't think it's causing this. I'm also not claiming it's
currently buggy, just that there's a warning:
inval.c: In function ‘AddParallelDmlInvalidationMessage’:
inval.c:398:52: warning: declaration of ‘msg’ shadows a previous local
[-Wshadow=local]
398 | SharedInvalidationMessage *msg = \
| ^~~
inval.c:579:9: note: in expansion of macro ‘ProcessMessageSubGroup’
579 | ProcessMessageSubGroup(group, RelCacheMsgs,
| ^~~~~~~~~~~~~~~~~~~~~~
inval.c:572:35: note: shadowed declaration is here
572 | SharedInvalidationMessage msg;
| ^~~
2) Overhead on first insert in a session (scaling-test.sql).
I think this was my main concern when starting the review - how much
more expensive will it make the first execution? For non-partitioned
targets it's hardly measurable, of course. But for partitioned tables
there's quite a bit of overhead. Even for moderate-sized partition
schemas with 10 or 100 partitions it's 2-4x slower. That's a lot (times
in ms):
pgbench -n -C -f insert.sql -T 10 -P 1 test
10 100 1000 10000
master 1.6 1.8 4.1 26.4
patched 2.0 3.7 23.5 235.3
This is including execution, so the impact on planning is even worse.
There's also memory overhead. The patch walks everything, and loads all
the partitions into relcache. That bloats the backend memory usage no
matter which partitions it actually inserts into. In the example with
10k partitions, that means 11MB -> 150MB increase.
Sure, if the session quickly touches all partitions. But many schemes
don't work like that, say when partitioned by time and inserting only
into current partition.
Of course, for fewer partitions that's goint to be negligible (both the
memory and timing overhead). But the overhead can be pretty significant
and I'm not comfortable with just ignoring it. IIRC it was one of the
reasons for reverting the PG14 attempt at this feature. Even if it
applies only to the first execution in a session (but see item 5).
I don't think this can be optimized further. AFAICS most of the time
is spent on building relcache entries for all the partitions, indexes,
partition constraints.
3) leader is still in parallel mode ...
The triggers, defaults, generated columns, constraints, ... is evaluated
in the leader, but that's still in parallel mode, after the command ID
has already been used. The first commit message says:
... parallel-restricted object merely means the construct must be
evaluated in the leader, which is where the INSERT happens anyway
But that's not quite correct. The parallel mode is entered in ExecutePlan,
above the ModifyTable node. So even the leader runs in parallel mode (or
rather the INSERT part).
AFAICS that means any CommandCounterIncrement() there fails with "cannot
start commands during a parallel operation". That covers every query
inside a VOLATILE (the default) function, new.ts := now() in a plpgsql
trigger, and every plpgsql EXCEPTION block.
I think it can cause all kinds of weird issues:
* errors in ordinary read-only code, some of them intermittent;
* silent data corruption when an exception handler swallows the error
* PARALLEL UNSAFE domain-constraint functions run in parallel mode
I'm attaching a couple reproducers (repro-cci.tgz), with AI-generate
examples (but I think it's correct, and the issue holds). Some are
triggering unexpected errors, some are causing data corruption etc.
I believe the "spilling" approach would need to deal with this too in
some way. Say, by only entering the parallel mode "below" the INSERT,
but that does not seem possible here (due to the interleaved execution).
4) The walk opens every partition/index with AccessShareLock, and then
immediately unlocks it after the check. Which has a cost, and the master
build does not need to do that (it only locks partitions that get rows).
In most cases it'll be fine, but say dropping old partitions (stuck
after a long-running query) would now block ingestion into new ones.
That's not great :-(
It's now also possible to get deadlocks like this
X: BEGIN; TRUNCATE parted_4999;
Y: INSERT INTO parted SELECT * FROM src1;
X: ALTER TABLE parted DETACH PARTITION parted_4999;
X => AccessExclusiveLock on parted_4999
Y => RowExclusiveLock on parted (parse analysis);
Y => the planner then waits for parted_4999
X => AccessExclusiveLock on parted: waits for Y
Maybe it's not a huge deal, but it's a significantly more extensive
locking that without the patch. It also contradics the statements that
these changes do not introduce new deadlock risks.
5) re-planning storms
AFAIK changing any function's PARALLEL label resets the cached value of
every table in the database and invalidates all cached INSERT ... SELECT
plans (including serial ones). That seems like a very good recipe to
trigger database-wide re-planning storm. Yes, we'd have to invalidate
some objects anyway, but doing it database-wide seems problematic.
Now, some simpler bugs:
6) RelationGetParallelDmlSafety() retries the walk without bound when an
invalidation arrives during it. That seems like a recipe for an infinite
loop - under debug_discard_caches the patch's own regression test never
finishes. But even without it, it seems like it could be running for a
an indefinite amount of time. I don't know what to do here, though. We
probably can't just error-out.
7) A pointer into the relcache TriggerDesc (tgqual) is used across a
catalog lookup, which can process invalidations and release the relcache
entry. Upstream fixed the same bug for PG14's version of this feature in
e2cda3c20a6.
8) Creating a sub-partition (with a partition-specific generated column,
or partitioned by an expression) doesn't invalidate the root's cached
value, and the executor rechecks only leaf partitions. That doesn't seem
right - existing may keep failing, or run a PARALLEL UNSAFE
partition-key function in parallel mode.
9) ON CONFLICT DO SELECT ... FOR UPDATE is allowed although row marks are
UNSAFE. I'm not sure if this is intended and expected to work, or
if it's a bug.
Finally, some notes / open questions:
10) Foreign tables are allowed targets, with no way for an FDW to opt out.
(This would not be a problem if the insert was not running in the
parallel mode, I think.)
11) pg_waldump prints unrecognized id -7 for SHAREDINVALPARALLELDML_ID.
regards
[1]
https://www.postgresql.org/message-id/0110fc81-549f-4e0c-a079-22b85b65e958%40vondra.me
--
Tomas Vondra
| Attachment | Content-Type | Size |
|---|---|---|
| repros-cci.tgz | application/x-compressed-tar | 4.5 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Álvaro Herrera | 2026-11-24 20:39:51 | Re: Avoid unnecessary StringInfo allocation in tablesync COPY buffer |
| Previous Message | Masahiko Sawada | 2026-11-24 20:10:12 | Re: Make COPY format extendable: Extract COPY TO format implementations |