Session in aborted transaction misses effective_wal_level change

Started by Sergei Patiakinabout 2 months ago13 messageshackers
Jump to latest
#1Sergei Patiakin
sergei.patiakin@enterprisedb.com

For an aborted transaction, there is today a gap between the
AtEOXact_LogicalCtl call and the XID being cleared. If an
effective_wal_level change occurs during that gap, the change will be
missed. That means the next transaction on that session will not be
logically decoded.

Attaching a reproducer bash script that demonstrates the missing decoded change.
Attaching a fix that moves the AtEOXact_LogicalCtl call later, closing the gap.

The issue was observed on master (b69356cd789) and REL_19_STABLE
(67c01b84788). It seems it was introduced in 67c20979ce7.

repro.sh output without patch:

rows committed:
1
2
3

changes decoded from slot s:
table public.t: INSERT: a[integer]:1
table public.t: INSERT: a[integer]:3

repro.sh output with patch:

rows committed:
1
2
3

changes decoded from slot s:
table public.t: INSERT: a[integer]:1
table public.t: INSERT: a[integer]:2
table public.t: INSERT: a[integer]:3

Best regards,
Sergei

Attachments:

t254013_1
v1-0001-Handle-effective_wal_level-change-in-aborted-tran.patchapplication/octet-stream; name=v1-0001-Handle-effective_wal_level-change-in-aborted-tran.patchDownload+3-2
repro.shtext/x-sh; charset=US-ASCII; name=repro.shDownload
#2Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Sergei Patiakin (#1)
Re: Session in aborted transaction misses effective_wal_level change

Hi,

On Wed, Sep 30, 2026 at 10:19 AM Sergei Patiakin
<sergei.patiakin@enterprisedb.com> wrote:

For an aborted transaction, there is today a gap between the
AtEOXact_LogicalCtl call and the XID being cleared. If an
effective_wal_level change occurs during that gap, the change will be
missed. That means the next transaction on that session will not be
logically decoded.

Attaching a reproducer bash script that demonstrates the missing decoded change.
Attaching a fix that moves the AtEOXact_LogicalCtl call later, closing the gap.

Thank you for the report. I agree it's an issue that we should fix for v19.

The fix looks good to me. We need to check and update XLogLogicalInfo
in every place where we reset the top-level transaction id.

I've updated the patch with the regression tests. Please review it.

Regards,

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

Attachments:

t254013_2
v1-0001-Fix-deferred-XLogLogicalInfo-update-after-a-faile.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Fix-deferred-XLogLogicalInfo-update-after-a-faile.patchDownload+44-2
#3Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Masahiko Sawada (#2)
RE: Session in aborted transaction misses effective_wal_level change

Dear Sawada-san, Serigei,

Good catch, I have also been seeing and considering the test, but Sawada-san is faster.

The fix looks good to me. We need to check and update XLogLogicalInfo
in every place where we reset the top-level transaction id.

I've updated the patch with the regression tests. Please review it.

Confirmed the test fails on HEAD and pass after the patch.
There might be idea to put the function after the "nParallelCurrentXids = 0;"
even in the CommitTransaction() and PrepareTransaction(), which is same as
CleanupTransaction(). But any places are OK for me.

LGTM.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#4Sergei Patiakin
sergei.patiakin@enterprisedb.com
In reply to: Hayato Kuroda (Fujitsu) (#3)
Re: Session in aborted transaction misses effective_wal_level change

I've updated the patch with the regression tests. Please review it.

The updated patch looks good, thanks.

Best regards,
Sergei

#5Shlok Kyal
shlok.kyal.oss@gmail.com
In reply to: Masahiko Sawada (#2)
Re: Session in aborted transaction misses effective_wal_level change

On Thu, 1 Oct 2026 at 08:56, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

Hi,

On Wed, Sep 30, 2026 at 10:19 AM Sergei Patiakin
<sergei.patiakin@enterprisedb.com> wrote:

For an aborted transaction, there is today a gap between the
AtEOXact_LogicalCtl call and the XID being cleared. If an
effective_wal_level change occurs during that gap, the change will be
missed. That means the next transaction on that session will not be
logically decoded.

Attaching a reproducer bash script that demonstrates the missing decoded change.
Attaching a fix that moves the AtEOXact_LogicalCtl call later, closing the gap.

Thank you for the report. I agree it's an issue that we should fix for v19.

The fix looks good to me. We need to check and update XLogLogicalInfo
in every place where we reset the top-level transaction id.

I've updated the patch with the regression tests. Please review it.

Hi Sawada-san, Sergei,

I am able to reproduce the issue on HEAD and PG19.
The patch applies on both the branches and it addresses the issue.

Just a minor comment, the patch requires a pgperltidy run. Otherwise patch LGTM.

Thanks,
Shlok Kyal

#6Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#3)
Re: Session in aborted transaction misses effective_wal_level change

On Wed, Sep 30, 2026 at 11:28 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Dear Sawada-san, Serigei,

Good catch, I have also been seeing and considering the test, but Sawada-san is faster.

The fix looks good to me. We need to check and update XLogLogicalInfo
in every place where we reset the top-level transaction id.

I've updated the patch with the regression tests. Please review it.

Confirmed the test fails on HEAD and pass after the patch.
There might be idea to put the function after the "nParallelCurrentXids = 0;"
even in the CommitTransaction() and PrepareTransaction(), which is same as
CleanupTransaction(). But any places are OK for me.

LGTM.

Thank you for reviewing the patch.

I'd rather move AtEOXact_LogicalCtl() to before resetting
CurrentResourceOwner. I think it should work fine too and caon deal
with your concern.

I've attached the updated patch. I'm going to push it on Monday,
barring objections.

Regards,

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

Attachments:

t254013_6
v2-0001-Fix-deferred-XLogLogicalInfo-update-after-a-faile.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Fix-deferred-XLogLogicalInfo-update-after-a-faile.patchDownload+46-3
#7Bertrand Drouvot
bertranddrouvot.pg@gmail.com
In reply to: Masahiko Sawada (#6)
Re: Session in aborted transaction misses effective_wal_level change

Hi Sawada-san,

On Fri, Oct 02, 2026 at 01:54:04PM -0700, Masahiko Sawada wrote:

On Wed, Sep 30, 2026 at 11:28 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Dear Sawada-san, Serigei,

Good catch, I have also been seeing and considering the test, but Sawada-san is faster.

The fix looks good to me. We need to check and update XLogLogicalInfo
in every place where we reset the top-level transaction id.

I've updated the patch with the regression tests. Please review it.

Confirmed the test fails on HEAD and pass after the patch.
There might be idea to put the function after the "nParallelCurrentXids = 0;"
even in the CommitTransaction() and PrepareTransaction(), which is same as
CleanupTransaction(). But any places are OK for me.

LGTM.

Thank you for reviewing the patch.

I'd rather move AtEOXact_LogicalCtl() to before resetting
CurrentResourceOwner. I think it should work fine too and caon deal
with your concern.

I wonder if it wouldn't make more sense to keep this after XactTopFullTransactionId
is reset, as in v1?

Indeed, couldn't a barrier be processed during AtCleanup_Memory(), while the
XID is still valid, and set XLogLogicalInfoUpdatePending again after this call?

Unless I'm missing something, I don't see AtEOXact_LogicalCtl() itself relying
on CurrentResourceOwner. Is there some other reason why it needs to be called
before CurrentResourceOwner is reset?

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

#8Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Bertrand Drouvot (#7)
RE: Session in aborted transaction misses effective_wal_level change

Dear Bertrand, Sawada-san,

I wonder if it wouldn't make more sense to keep this after
XactTopFullTransactionId
is reset, as in v1?

Indeed, couldn't a barrier be processed during AtCleanup_Memory(), while the
XID is still valid, and set XLogLogicalInfoUpdatePending again after this call?

Per my research the function calls MemoryContextCallResetCallbacks(), which can
calls the arbitrary functions from core and extensions. So It's possible that a
random callback calls CHECK_FOR_INTERRUPTS() and consumes a barrier.

I still prefer to put after resetting the XactTopFullTransactionId.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#9Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#8)
Re: Session in aborted transaction misses effective_wal_level change

On Sun, Oct 4, 2026 at 8:35 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Dear Bertrand, Sawada-san,

I wonder if it wouldn't make more sense to keep this after
XactTopFullTransactionId
is reset, as in v1?

Indeed, couldn't a barrier be processed during AtCleanup_Memory(), while the
XID is still valid, and set XLogLogicalInfoUpdatePending again after this call?

Per my research the function calls MemoryContextCallResetCallbacks(), which can
calls the arbitrary functions from core and extensions. So It's possible that a
random callback calls CHECK_FOR_INTERRUPTS() and consumes a barrier.

I still prefer to put after resetting the XactTopFullTransactionId.

Thank you for the comments. I'm now convinced that we can call it
after resetting the top-level XID. I was concerned about the
inconsistency between the AtEOXact_LogicalCtl() calls in
CommitTransaction() and CleanupTransaction(), but it's fine for them
to differ, since we're holding interrupts in the former but not in the
latter. Calling AtEOXact_LogicalCtl() after resetting the top-level
XID would be more bullet-proof, so let's go with this approach.

I've attached the updated patch. Please review it.

Regards,

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

Attachments:

t254013_9
v3-0001-Fix-deferred-XLogLogicalInfo-update-after-a-faile.patchtext/x-patch; charset=US-ASCII; name=v3-0001-Fix-deferred-XLogLogicalInfo-update-after-a-faile.patchDownload+46-3
#10Bertrand Drouvot
bertranddrouvot.pg@gmail.com
In reply to: Masahiko Sawada (#9)
Re: Session in aborted transaction misses effective_wal_level change

Hi Sawada-san,

On Mon, Oct 05, 2026 at 11:01:50AM -0700, Masahiko Sawada wrote:

On Sun, Oct 4, 2026 at 8:35 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Dear Bertrand, Sawada-san,

I wonder if it wouldn't make more sense to keep this after
XactTopFullTransactionId
is reset, as in v1?

Indeed, couldn't a barrier be processed during AtCleanup_Memory(), while the
XID is still valid, and set XLogLogicalInfoUpdatePending again after this call?

Per my research the function calls MemoryContextCallResetCallbacks(), which can
calls the arbitrary functions from core and extensions. So It's possible that a
random callback calls CHECK_FOR_INTERRUPTS() and consumes a barrier.

I still prefer to put after resetting the XactTopFullTransactionId.

Thank you for the comments. I'm now convinced that we can call it
after resetting the top-level XID. I was concerned about the
inconsistency between the AtEOXact_LogicalCtl() calls in
CommitTransaction() and CleanupTransaction(), but it's fine for them
to differ, since we're holding interrupts in the former but not in the
latter. Calling AtEOXact_LogicalCtl() after resetting the top-level
XID would be more bullet-proof, so let's go with this approach.

Yeah, thanks!

I've attached the updated patch. Please review it.

LGTM.

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

#11Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Masahiko Sawada (#9)
RE: Session in aborted transaction misses effective_wal_level change

Dear Sawada-san,

Thank you for the comments. I'm now convinced that we can call it
after resetting the top-level XID. I was concerned about the
inconsistency between the AtEOXact_LogicalCtl() calls in
CommitTransaction() and CleanupTransaction(), but it's fine for them
to differ, since we're holding interrupts in the former but not in the
latter. Calling AtEOXact_LogicalCtl() after resetting the top-level
XID would be more bullet-proof, so let's go with this approach.

I've attached the updated patch. Please review it.

Understood, LGTM.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#12Sergei Patiakin
sergei.patiakin@enterprisedb.com
In reply to: Hayato Kuroda (Fujitsu) (#11)
Re: Session in aborted transaction misses effective_wal_level change

Hi Masahiko,

I've attached the updated patch. Please review it.

Looks good. I confirmed the v3 test changes fail on `master` and pass
with the v3 code changes.

Best regards,
Sergei

#13Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Sergei Patiakin (#12)
Re: Session in aborted transaction misses effective_wal_level change

On Tue, Oct 6, 2026 at 2:19 AM Sergei Patiakin
<sergei.patiakin@enterprisedb.com> wrote:

Hi Masahiko,

I've attached the updated patch. Please review it.

Looks good. I confirmed the v3 test changes fail on `master` and pass
with the v3 code changes.

Thank you for reviewing the patch! Pushed.

Regards,

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