[PATCH] Fall back to serializing when parallel apply runs out of DSM segments

Started by Baji Shaikabout 1 month ago8 messageshackers
Jump to latest
#1Baji Shaik
baji.pgdev@gmail.com

Hi,

When the leader apply worker launches a parallel apply worker,
pa_setup_dsm() checks dsm_create() for NULL and returns false, and the
caller then serializes the streamed transaction to a file and applies it
itself. That's what the docs [1]https://www.postgresql.org/docs/devel/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-PARAMS-WITH-STREAMING describe for streaming = parallel: "If no
parallel apply worker is free to handle streaming transactions then the
changes are written to temporary files and applied after the transaction
is committed."

But dsm_create() is called with flags = 0, so it never returns NULL. If
no DSM segment is available, it raises "too many dynamic shared memory
segments" instead. The leader exits, restarts, and fails again at the
same STREAM START, so replication makes no progress until segments are
freed:

ERROR: too many dynamic shared memory segments
CONTEXT: processing remote data for replication origin "pg_16394" during
message type "STREAM START" in transaction 665
LOG: background worker "logical replication apply worker" (PID 2514)
exited with exit code 1

I reproduced it by holding all DSM slots on the subscriber from a test
extension, then running a 100k-row transaction on the publisher with
logical_decoding_work_mem = 64kB. On master the apply worker restarted
13 times in a minute with nothing applied. With the patch the leader
serialized the transaction and applied all rows with no error.

The patch passes DSM_CREATE_NULL_IF_MAXSEGMENTS, as
InitializeParallelDSM() does, so the existing fallback is used. The code
dates from 216a784829c and the patch applies to 16 and later.

[1]: https://www.postgresql.org/docs/devel/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-PARAMS-WITH-STREAMING
https://www.postgresql.org/docs/devel/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-PARAMS-WITH-STREAMING

Thanks,
Baji Shaik

Attachments:

t254358_1
0001-Fall-back-to-serializing-when-parallel-apply-runs-ou.patchapplication/octet-stream; name=0001-Fall-back-to-serializing-when-parallel-apply-runs-ou.patchDownload+6-3
#2Zhijie Hou
houzhijie22@gmail.com
In reply to: Baji Shaik (#1)
Re: [PATCH] Fall back to serializing when parallel apply runs out of DSM segments

Hi,

On Sat, Oct 24, 2026 at 1:55 AM Baji Shaik <baji.pgdev@gmail.com> wrote:

Hi,

When the leader apply worker launches a parallel apply worker,
pa_setup_dsm() checks dsm_create() for NULL and returns false, and the
caller then serializes the streamed transaction to a file and applies it
itself. That's what the docs [1] describe for streaming = parallel: "If no
parallel apply worker is free to handle streaming transactions then the
changes are written to temporary files and applied after the transaction
is committed."

But dsm_create() is called with flags = 0, so it never returns NULL. If
no DSM segment is available, it raises "too many dynamic shared memory
segments" instead. The leader exits, restarts, and fails again at the
same STREAM START, so replication makes no progress until segments are
freed:

ERROR: too many dynamic shared memory segments
CONTEXT: processing remote data for replication origin "pg_16394" during message type "STREAM START" in transaction 665
LOG: background worker "logical replication apply worker" (PID 2514) exited with exit code 1

The fix looks good to me. Without the patch, the code that returns early when
DSM setup fails would be unreachable, so this patch at least improves that.

But I'm not sure we need to change the comments above those early returns, since
it's not this internal function's place to know whether the outer side should
apply the transaction or not.

BTW, I noticed the other caller of pa_setup_dsm() in dsa.c has a similar issue:
it doesn't pass this flag but does have a check for a NULL value, so that path
is also unreachable, but that could be fixed in a separate patch.

Best Regards,
Zhijie Hou

#3Baji Shaik
baji.pgdev@gmail.com
In reply to: Zhijie Hou (#2)
Re: [PATCH] Fall back to serializing when parallel apply runs out of DSM segments

Hello Zhijie,

On Sat, Oct 24, 2026 at 10:06 PM Zhijie Hou <houzhijie22@gmail.com> wrote:

But I'm not sure we need to change the comments above those early returns,
since
it's not this internal function's place to know whether the outer side
should
apply the transaction or not.

Thank you for the review and suggestion.
Agreed. v2 attached keeps the original comment.

BTW, I noticed the other caller of pa_setup_dsm() in dsa.c has a similar
issue:
it doesn't pass this flag but does have a check for a NULL value, so that
path
is also unreachable, but that could be fixed in a separate patch.

Right, make_new_segment() has the same issue. There it means
DSA_ALLOC_NO_OOM still errors out when we run out of segments, so
callers like pgstat never get to do their cleanup. Attached as 0002.

I think both should be backpatched. 0001 to 16, where parallel apply
was added, and 0002 to 14, since that code is the same on all supported
branches. Thoughts?

Thanks,
Baji Shaik

Attachments:

t254358_3
v2-0002-Make-DSA-honor-DSA_ALLOC_NO_OOM-when-out-of-DSM-s.patchapplication/octet-stream; name=v2-0002-Make-DSA-honor-DSA_ALLOC_NO_OOM-when-out-of-DSM-s.patchDownload+1-2
v2-0001-Fall-back-to-serializing-when-parallel-apply-runs.patchapplication/octet-stream; name=v2-0001-Fall-back-to-serializing-when-parallel-apply-runs.patchDownload+1-2
#4Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Baji Shaik (#3)
Re: [PATCH] Fall back to serializing when parallel apply runs out of DSM segments

Hi,

On Sun, Oct 25, 2026 at 8:24 AM Baji Shaik <baji.pgdev@gmail.com> wrote:

I think both should be backpatched. 0001 to 16, where parallel apply
was added, and 0002 to 14, since that code is the same on all supported
branches. Thoughts?

I have a slightly different opinion here. I understand that a leader
erroring out while launching parallely apply workers has costs.
However, DSM running out of segments, IMO, is rare in practice and the
issue is not reported from the field. Even if it is hit in practice,
the recovery from that error is automatic for the apply worker starts
again and callers of make_new_segment() would hit OOM or similar error
anyway. I would just apply it for HEAD and be done with it.

Both the patches LGTM. Removing the comment change made in v1-0001 is
a nice thing, IMHO, comments describing what the code does in words
are better avoided.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

#5Baji Shaik
baji.pgdev@gmail.com
In reply to: Bharath Rupireddy (#4)
Re: [PATCH] Fall back to serializing when parallel apply runs out of DSM segments

On Wed, Nov 11, 2026 at 11:46 AM Bharath Rupireddy <
bharath.rupireddyforpostgres@gmail.com> wrote:

I have a slightly different opinion here. I understand that a leader
erroring out while launching parallely apply workers has costs.
However, DSM running out of segments, IMO, is rare in practice and the
issue is not reported from the field. Even if it is hit in practice,
the recovery from that error is automatic for the apply worker starts
again and callers of make_new_segment() would hit OOM or similar error
anyway. I would just apply it for HEAD and be done with it.

Thanks for the review, Bharath. HEAD only works for me.
I will leave the back-patching decision to the committer.

Thanks,
Baji Shaik
Amazon Web Services: https://aws.amazon.com

#6Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Baji Shaik (#5)
Re: [PATCH] Fall back to serializing when parallel apply runs out of DSM segments

On Wed, Nov 11, 2026 at 10:37 AM Baji Shaik <baji.pgdev@gmail.com> wrote:

On Wed, Nov 11, 2026 at 11:46 AM Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> wrote:

I have a slightly different opinion here. I understand that a leader
erroring out while launching parallely apply workers has costs.
However, DSM running out of segments, IMO, is rare in practice and the
issue is not reported from the field. Even if it is hit in practice,
the recovery from that error is automatic for the apply worker starts
again and callers of make_new_segment() would hit OOM or similar error
anyway. I would just apply it for HEAD and be done with it.

Thanks for the review, Bharath. HEAD only works for me.
I will leave the back-patching decision to the committer.

I've reviewed both patches and both look good to me.

Regarding backpatching these patches, I'm inclined to backpatch both
of them. Regarding the parallel apply worker issue, it seems a bug to
me. As for the 0001 patch, the apply worker would restart even in the
problematic case, but it could error out again as it receives STREAM
START and cannot create a new DSM because all segments are still in
use. Therefore the replication would stop, which violates what the
documentation says.

Regarding the 0002 patch, it simply violates what DSA_ALLOC_NO_OOM
promises. IIUC make_new_segment() is implemented to exit gracefully
even in erroneous cases so that its caller can handle the situation. A
caller using DSA_ALLOC_NO_OOM gets an error if DSM slots are used up,
instead of InvalidDsaPointer.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#7Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Masahiko Sawada (#6)
Re: [PATCH] Fall back to serializing when parallel apply runs out of DSM segments

Hi,

On Tue, Nov 17, 2026 at 2:53 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

I've reviewed both patches and both look good to me.

Thanks for taking a look.

Regarding backpatching these patches, I'm inclined to backpatch both
of them. Regarding the parallel apply worker issue, it seems a bug to
me.

Regarding the 0002 patch, it simply violates what DSA_ALLOC_NO_OOM
promises. IIUC make_new_segment() is implemented to exit gracefully
even in erroneous cases so that its caller can handle the situation. A
caller using DSA_ALLOC_NO_OOM gets an error if DSM slots are used up,
instead of InvalidDsaPointer.

Agreed to both of the above. My understanding was that although these
are not behaving as expected, and so are bugs, I haven't seen such
issues in practice or reported from the field, and even if they were,
the errors are recoverable or reported to the callers. That said, I'm
fine to classify them as back-patchable here. Thanks.

I marked it as RfC.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

#8Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Bharath Rupireddy (#7)
Re: [PATCH] Fall back to serializing when parallel apply runs out of DSM segments

On Wed, Nov 18, 2026 at 9:42 AM Bharath Rupireddy
<bharath.rupireddyforpostgres@gmail.com> wrote:

Hi,

On Tue, Nov 17, 2026 at 2:53 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

I've reviewed both patches and both look good to me.

Thanks for taking a look.

Regarding backpatching these patches, I'm inclined to backpatch both
of them. Regarding the parallel apply worker issue, it seems a bug to
me.

Regarding the 0002 patch, it simply violates what DSA_ALLOC_NO_OOM
promises. IIUC make_new_segment() is implemented to exit gracefully
even in erroneous cases so that its caller can handle the situation. A
caller using DSA_ALLOC_NO_OOM gets an error if DSM slots are used up,
instead of InvalidDsaPointer.

Agreed to both of the above. My understanding was that although these
are not behaving as expected, and so are bugs, I haven't seen such
issues in practice or reported from the field, and even if they were,
the errors are recoverable or reported to the callers. That said, I'm
fine to classify them as back-patchable here. Thanks.

I marked it as RfC.

Pushed both patches.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com