035_standby_logical_decoding might fail due to FATAL message lost inside libpq
Hello hackers,
A recent buildfarm failure [1]https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=francolin&dt=2026-05-11%2012%3A25%3A04:
38/273 recovery - postgresql:recovery/035_standby_logical_decoding ERROR 74.36s exit status 1
[14:34:25.522](0.001s) ok 65 - pg_recvlogical exited non-zero
[14:34:25.522](0.000s) not ok 66 - slot has been invalidated
[14:34:25.523](0.000s) # Failed test 'slot has been invalidated'
# at /home/bf/bf-build/francolin/REL_16_STABLE/pgsql/src/test/recovery/t/035_standby_logical_decoding.pl line 118.
[14:34:25.523](0.001s) # 'pg_recvlogical: error: could not send replication command "START_REPLICATION
SLOT "drop_db_activeslot" LOGICAL 0/0 ("include-xids" '0', "skip-empty-xacts" '1')": server closed the connection
unexpectedly
# This probably means the server terminated abnormally
# before or while processing the request.
# pg_recvlogical: error: disconnected
# '
# doesn't match '(?^:conflict with recovery)'
[14:34:25.675](0.152s) ok 67 - otherslot on standby not dropped
duplicated one that happened the last year [2]https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=kestrel&dt=2025-08-04%2005%3A45%3A46 and had no explanation so
far. This time I've managed to reproduce it with the check_slots_dropped()
repeated in the loop (the patch is attached).
With extra logging added, I could see:
[08:47:52.385](0.000s) not ok 114 - slot has been invalidated
[08:47:52.385](0.000s) # Failed test 'slot has been invalidated'
# at /home/user/postgresql/src/test/recovery_9/t/035_standby_logical_decoding.pl line 120.
[08:47:52.385](0.000s) # '!!!pqsecure_raw_read| len=16384 n=554 errno=0
buf=760000002b00030002000000015f70715f2e746573745f70726f746f636f6c5f6e65676f74696174696f6e00520000000800000000530000001b496e74657276616c5374796c6500706f7374677265730053000000207365617263685f7061746800222475736572222c207075626c696300530000001469735f737570657275736572006f6e0053000000237374616e646172645f636f6e666f726d696e675f737472696e6773006f6e00530000001f73657373696f6e5f617574686f72697a6174696f6e0075736572005300000019636c69656e745f656e636f64696e67005554463800530000001b7365727665725f76657273696f6e003139646576656c
# !!!pqsecure_raw_read| len=16384 n=85 errno=0
buf=540000002300017365745f636f6e6669670000000000000000000019ffffffffffff0000440000000a000100000000430000000d53454c45435420310053000000117365617263685f7061746800005a0000000549
# !!!pqsecure_raw_read| len=16384 n=76 errno=0
buf=540000002c0001646174615f6469726563746f72795f6d6f64650000000000000000000019ffffffffffff0000440000000e00010000000430373030430000000953484f57005a0000000549
# !!!pqsecure_raw_read| len=16384 n=198 errno=0
buf=540000006f000473797374656d69640000000000000000000019ffffffffffff000074696d656c696e6500000000000000000000140008ffffffff0000786c6f67706f730000000000000000000019ffffffffffff000064626e616d650000000000000000000019ffffffffffff0000440000003a0004000000133736343038333834313938393731313334343000000001310000000a302f30373138433739380000000674657374646243000000144944454e544946595f53595354454d005a0000000549
# !!!pqsecure_raw_read| len=16384 n=181 errno=0
buf=45000000b453464154414c0056464154414c00433537503034004d7465726d696e6174696e6720636f6e6e656374696f6e2064756520746f20636f6e666c6963742077697468207265636f766572790044557365722077617320636f6e6e656374656420746f20612064617461626173652074686174206d7573742062652064726f707065642e0046706f7374677265732e63004c3332313300527265706f72745f7265636f766572795f636f6e666c6963740000
### FATAL...57P04...terminating connection due to conflict with recovery
# !!!pqsecure_raw_read| len=16384 n=0 errno=0 buf=
# !!!pqsecure_raw_read| len=16384 n=0 errno=0 buf=
# pg_recvlogical: error: could not send replication command "START_REPLICATION SLOT "drop_db_activeslot" LOGICAL
0/00000000 ("include-xids" '0', "skip-empty-xacts" '1')": server closed the connection unexpectedly
# This probably means the server terminated abnormally
# before or while processing the request.
# pg_recvlogical: error: disconnected
# '
# doesn't match '(?^:conflict with recovery)'
That is, the FATAL message was received from the server, but libpq lost it inside.
A successful run shows this:
!!!check_pg_recvlogical_stderr:
!!!pqsecure_raw_read| len=16384 n=554 errno=0
buf=760000002b00030002000000015f70715f2e746573745f70726f746f636f6c5f6e65676f74696174696f6e00520000000800000000530000001b496e74657276616c5374796c6500706f7374677265730053000000207365617263685f7061746800222475736572222c207075626c696300530000001469735f737570657275736572006f6e0053000000237374616e646172645f636f6e666f726d696e675f737472696e6773006f6e00530000001f73657373696f6e5f617574686f72697a6174696f6e0075736572005300000019636c69656e745f656e636f64696e67005554463800530000001b7365727665725f76657273696f6e003139646576656c
!!!pqsecure_raw_read| len=16384 n=85 errno=0
buf=540000002300017365745f636f6e6669670000000000000000000019ffffffffffff0000440000000a000100000000430000000d53454c45435420310053000000117365617263685f7061746800005a0000000549
!!!pqsecure_raw_read| len=16384 n=76 errno=0
buf=540000002c0001646174615f6469726563746f72795f6d6f64650000000000000000000019ffffffffffff0000440000000e00010000000430373030430000000953484f57005a0000000549
!!!pqsecure_raw_read| len=16384 n=198 errno=0
buf=540000006f000473797374656d69640000000000000000000019ffffffffffff000074696d656c696e6500000000000000000000140008ffffffff0000786c6f67706f730000000000000000000019ffffffffffff000064626e616d650000000000000000000019ffffffffffff0000440000003a0004000000133736343038333834313938393731313334343000000001310000000a302f30364432393539380000000674657374646243000000144944454e544946595f53595354454d005a0000000549
!!!pqsecure_raw_read| len=16384 n=8 errno=0 buf=5700000007000000
!!!pqsecure_raw_read| len=16384 n=46 errno=0
buf=64000000166b0000000006d295980002f5019f6590590064000000166b0000000006d295980002f5019f6590c100
!!!pqsecure_raw_read| len=16384 n=46 errno=0
buf=64000000166b0000000006d295980002f5019f66df5b0064000000166b0000000006d295d00002f5019f66e01d00
!!!pqsecure_raw_read| len=16384 n=46 errno=0
buf=64000000166b0000000006d295d00002f5019f6719390064000000166b0000000006d296a00002f5019f67197c00
!!!pqsecure_raw_read| len=16384 n=23 errno=0 buf=64000000166b0000000006d296a00002f5019f671b3300
!!!pqsecure_raw_read| len=16384 n=23 errno=0 buf=64000000166b0000000006d297800002f5019f671bc900
!!!pqsecure_raw_read| len=16384 n=46 errno=0
buf=64000000166b0000000006d297800002f5019f671c970064000000166b0000000006d298000002f5019f671cbf00
!!!pqsecure_raw_read| len=16384 n=181 errno=0
buf=45000000b453464154414c0056464154414c00433537503034004d7465726d696e6174696e6720636f6e6e656374696f6e2064756520746f20636f6e666c6963742077697468207265636f766572790044557365722077617320636f6e6e656374656420746f20612064617461626173652074686174206d7573742062652064726f707065642e0046706f7374677265732e63004c3332313300527265706f72745f7265636f766572795f636f6e666c6963740000
### FATAL...57P04...terminating connection due to conflict with recovery
pg_recvlogical: error: unexpected termination of replication stream: FATAL: terminating connection due to conflict with
recovery
DETAIL: User was connected to a database that must be dropped.
pg_recvlogical: error: disconnected
Thus, FATAL is received by pg_recvlogical as expected, if it follows
CopyBothResponse in the stream.
Having this knowledge, I could reproduce such behavior simply with:
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -1501,6 +1501,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
ReplicationSlotAcquire(cmd->slotname, true, true);
+pg_usleep(1000000);
/*
* Force a disconnect, so that the decoding code doesn't need to care
* about an eventual switch from running in recovery, to running in a
# +++ tap check in src/test/recovery +++
t/035_standby_logical_decoding.pl .. 69/?
# Failed test 'slot has been invalidated'
# at t/035_standby_logical_decoding.pl line 119.
# 'pg_recvlogical: error: could not send replication command "START_REPLICATION SLOT
"drop_db_activeslot" LOGICAL 0/00000000 ("include-xids" '0', "skip-empty-xacts" '1')": server closed the connection
unexpectedly
# This probably means the server terminated abnormally
# before or while processing the request.
# pg_recvlogical: error: disconnected
# '
# doesn't match '(?^:conflict with recovery)'
t/035_standby_logical_decoding.pl .. 88/? # Looks like you failed 1 test of 89.
Reproduced on REL_16_STABLE (where this test was landed) .. master.
[1]: https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=francolin&dt=2026-05-11%2012%3A25%3A04
[2]: https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=kestrel&dt=2025-08-04%2005%3A45%3A46
Best regards,
Alexander
Attachments:
debug-035_standby_logical_decoding.patchtext/x-patch; charset=UTF-8; name=debug-035_standby_logical_decoding.patchDownload+44-0
Hi Alexander,
I applied your reproducer atop my HEAD (12d00048), but I could not reproduce.
In the first place, the postgres could not do the meson setup command after patching.
I got the below error:
```
Configuring Makefile.global using configuration
../postgres/src/test/meson.build:8:0: ERROR: Non-existent build file 'src/test/recovery_1/meson.build'
```
The build can be done if I restore changes in src/meson.build, but I could not
reproduce the failure if I run 035_standby_logicaldecoding.pl. Am I missing something?
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Hello Kuroda-san,
27.05.2026 08:43, Hayato Kuroda (Fujitsu) wrote:
Hi Alexander,
I applied your reproducer atop my HEAD (12d00048), but I could not reproduce.
...The build can be done if I restore changes in src/meson.build, but I could not
reproduce the failure if I run 035_standby_logicaldecoding.pl. Am I missing something?
I'm sorry, it looks like I had messed up with the patch. Please try the fixed one.
It makes the unmodified 035_standby_logical_decoding.pl fail for me as below:
CPPFLAGS="-O0" ./configure -q --enable-debug --enable-tap-tests --enable-injection-points && make -s -j8 &&
PROVE_TESTS="t/035*" make -s check -C src/test/recovery/
...
# pg_recvlogical: error: could not send replication command "START_REPLICATION SLOT "drop_db_activeslot" LOGICAL
0/00000000 ("include-xids" '0', "skip-empty-xacts" '1')": server closed the connection unexpectedly
# This probably means the server terminated abnormally
# before or while processing the request.
# pg_recvlogical: error: disconnected
# '
# doesn't match '(?^:conflict with recovery)'
t/035_standby_logical_decoding.pl .. 87/? # Looks like you failed 1 test of 89.
t/035_standby_logical_decoding.pl .. Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/89 subtests
Best regards,
Alexander
Dear Alexander,
Thanks for sharing and sorry for late reply. I was busy for the company work.
I analyzed the test failure and found a candidate reason.
Per my understanding, this can happen when the FATAL error is reported while
PQexec() is still running in StreamLogicalLog(). This behavior seems to come from
the current PQexecFinish() semantics for backward compatibility, so I do not
think we should fix the libpq code. Instead, the test should be adjusted.
The attached patch shows my analysis. It adds a sleep in StartLogicalReplication()
based on your reproducer, debug logs in PQexecFinish(), and debug output in the
test function check_pg_recvlogical_stderr(). Please apply the patch and run the
test once to follow the behavior described below.
[1]: ``` [22:21:11.808](0.000s) ok 78 - slot has been invalidated PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=FATAL: terminating connection due to conflict with recovery DETAIL: User was connected to a database that must be dropped.
Note that the test passed because the debug log itself contained terminating
connection due to conflict with recovery. However, the following line shows that
pg_recvlogical actually reported server closed the connection unexpectedly, which
is the wrong output.
The whole output in [1]``` [22:21:11.808](0.000s) ok 78 - slot has been invalidated PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=FATAL: terminating connection due to conflict with recovery DETAIL: User was connected to a database that must be dropped. indicates that two PGRES_FATAL_ERROR results were
consumed by PQexec() / PQexecFinish(), but only the second one was returned to
the pg_recvlogical layer. This is the root cause. However, this seems to be
intentional behavior: the comment in PQexecFinish() says that only the last
result is returned for backward compatibility.
pg_usleep() is needed for the reproducer because the issue can happen only when
the FATAL error is reported during the PQexec() call for `START_REPLICATION ...`.
[2]: ``` [22:43:45.508](0.000s) ok 78 - slot has been invalidated PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) pg_recvlogical: debug: after PQexec pg_recvlogical: error: unexpected termination of replication stream: FATAL: terminating connection due to conflict with recovery DETAIL: User was connected to a database that must be dropped. ```
PQexec() had already returned PGRES_COPY_BOTH. Then PQgetCopyData(conn, ©buf, 1)
detected the FATAL error, and the subsequent code reported it as an unexpected
termination of the replication stream.
To address this issue, I think the test should ensure that pg_recvlogical has
finished running PQexec() and entered the COPY BOTH receive loop. Let me
investigate that a bit more.
How do you feel the analysis?
[1]: ``` [22:21:11.808](0.000s) ok 78 - slot has been invalidated PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=FATAL: terminating connection due to conflict with recovery DETAIL: User was connected to a database that must be dropped.
```
[22:21:11.808](0.000s) ok 78 - slot has been invalidated
PQexecFinish loop: errMsg=(null)
PQexecFinish return: errMsg=(null)
PQexecFinish loop: errMsg=(null)
PQexecFinish return: errMsg=(null)
PQexecFinish loop: errMsg=(null)
PQexecFinish return: errMsg=(null)
PQexecFinish loop: errMsg=FATAL: terminating connection due to conflict with recovery
DETAIL: User was connected to a database that must be dropped.
PQexecFinish loop: errMsg=server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
PQexecFinish return: errMsg=server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
pg_recvlogical: debug: after PQexec
pg_recvlogical: error: could not send replication command "START_REPLICATION SLOT "drop_db_activeslot" LOGICAL 0/00000000 ("include-xids" '0', "skip-empty-xacts" '1')": server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
```
[2]: ``` [22:43:45.508](0.000s) ok 78 - slot has been invalidated PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) PQexecFinish loop: errMsg=(null) PQexecFinish return: errMsg=(null) pg_recvlogical: debug: after PQexec pg_recvlogical: error: unexpected termination of replication stream: FATAL: terminating connection due to conflict with recovery DETAIL: User was connected to a database that must be dropped. ```
```
[22:43:45.508](0.000s) ok 78 - slot has been invalidated
PQexecFinish loop: errMsg=(null)
PQexecFinish return: errMsg=(null)
PQexecFinish loop: errMsg=(null)
PQexecFinish return: errMsg=(null)
PQexecFinish loop: errMsg=(null)
PQexecFinish return: errMsg=(null)
PQexecFinish loop: errMsg=(null)
PQexecFinish return: errMsg=(null)
pg_recvlogical: debug: after PQexec
pg_recvlogical: error: unexpected termination of replication stream: FATAL: terminating connection due to conflict with recovery
DETAIL: User was connected to a database that must be dropped.
```
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments:
0001-Analyze-035_standby_logical_decoding-failure.patchapplication/octet-stream; name=0001-Analyze-035_standby_logical_decoding-failure.patchDownload+10-1
Hi Alexander,
While sleeping, I came up with the idea; instead of waiting until the slot is
active, we can wait pg_recvlogical receives the ack from the walsender and enters
the loop. Attached patch implemented like that.
On my env, the patch could resolve the issue. How do you feel?
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Hello Kuroda-san,
25.06.2026 05:04, Hayato Kuroda (Fujitsu) wrote:
While sleeping, I came up with the idea; instead of waiting until the slot is
active, we can wait pg_recvlogical receives the ack from the walsender and enters
the loop. Attached patch implemented like that.On my env, the patch could resolve the issue. How do you feel?
Thank you for the fix!
I agree with your idea -- It makes sense to me to modify make_slot_active()
so that it returns the handle of pg_recvlogical after it reaches the
desired state from its own perspective.
With the the above repro modification applied, the fixed test passes for
me too.
Best regards,
Alexander
Hi hackers,
Ping - I registered this thread in CF [1]https://commitfest.postgresql.org/patch/7247/ not to forget.
This tries to fix a flaky test.
[1]: https://commitfest.postgresql.org/patch/7247/
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Hi,
On Wed, Sep 02, 2026 at 01:47:00AM +0000, Hayato Kuroda (Fujitsu) wrote:
Hi hackers,
Ping
Thanks! ;-)
This tries to fix a flaky test.
'--option' => 'skip-empty-xacts=1',
'--file' => '-',
+ '--verbose',
Adding verbose does not add too much noise to the output so that's ok for me.
It is also needed for:
+ ok(pump_until($slot_user_handle, $pump_timeout, $to_stderr,
+ qr/streaming initiated/),
+ "pg_recvlogical started streaming");
Waiting for "streaming initiated" makes sense to me.
The patch LGTM.
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
Dear Bertrand,
The patch LGTM.
That's great. I changed the status to "Ready for Committer".
Best regards,
Hayato Kuroda
FUJITSU LIMITED
On Wed, Sep 2, 2026 at 2:42 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
That's great. I changed the status to "Ready for Committer".
I've started reviewing this patch since it's marked as ready for committer.
I have two review comments.
+ my $pump_timeout =
+ IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default);
This timer is never started or passed to IPC::Run::start(), so it seems
it can never expire. Right? If so, isn't it better to reuse the existing
timeout for pump_until()?
+ ok(pump_until($slot_user_handle, $pump_timeout, $to_stderr,
+ qr/streaming initiated/),
+ "pg_recvlogical started streaming");
This allows the test to continue even if pump_until() returns false.
Since streaming is a prerequisite for the subsequent steps, we should
use or die(), as the old code did?
Attached 0001 addresses these comments.
I also found a pre-existing typo: the slots are created with the prefix
"drop_db_", but their removal is checked using "drop_db". Since
those names never existed, the checks pass even if the actual slots
remain. Attached 0002 fixes this.
Regards,
--
Fujii Masao
Attachments:
t139668_10v2-0002-Fix-slot-names-in-the-standby-slot-removal-check.patchapplication/octet-stream; name=v2-0002-Fix-slot-names-in-the-standby-slot-removal-check.patchDownload+1-2
v2-0001-Fix-a-race-in-035_standby_logical_decoding.pl.patchapplication/octet-stream; name=v2-0001-Fix-a-race-in-035_standby_logical_decoding.pl.patchDownload+9-8
nocfbot-v2-0001-PG16_PG17-Fix-a-race-in-035_standby_logical_decoding.pl.patchapplication/octet-stream; name=nocfbot-v2-0001-PG16_PG17-Fix-a-race-in-035_standby_logical_decoding.pl.patchDownload+9-9
Hi Fujii-san,
I've started reviewing this patch since it's marked as ready for committer.
Thanks!
+ my $pump_timeout = + IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default);This timer is never started or passed to IPC::Run::start(), so it seems
it can never expire. Right? If so, isn't it better to reuse the existing
timeout for pump_until()?
I referred the existing code in the same file, like [1]``` # check that we are decoding pre and post promotion inserted rows # with pg_recvlogical that has started before the promotion my $pump_timeout = IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default);.
But you seem right - perl doc [2]https://metacpan.org/pod/IPC::Run describes start() can register a timer object
to the harness and starts it, pump() checks the result. pump() internally calls
start() but calling it alone is not enough because timer must be registered.
It meant existing codes might also be fixed, right?
+ ok(pump_until($slot_user_handle, $pump_timeout, $to_stderr, + qr/streaming initiated/), + "pg_recvlogical started streaming");This allows the test to continue even if pump_until() returns false.
Since streaming is a prerequisite for the subsequent steps, we should
use or die(), as the old code did?
+1.
I also found a pre-existing typo: the slots are created with the prefix
"drop_db_", but their removal is checked using "drop_db". Since
those names never existed, the checks pass even if the actual slots
remain. Attached 0002 fixes this.
Good catch, LGTM.
[1]: ``` # check that we are decoding pre and post promotion inserted rows # with pg_recvlogical that has started before the promotion my $pump_timeout = IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default);
```
# check that we are decoding pre and post promotion inserted rows
# with pg_recvlogical that has started before the promotion
my $pump_timeout = IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default);
ok(pump_until($handle, $pump_timeout, \$stdout, qr/^.*COMMIT.*COMMIT$/s),
'got 2 COMMIT from pg_recvlogical output');
```
[2]: https://metacpan.org/pod/IPC::Run
Best regards,
Hayato Kuroda
FUJITSU LIMITED
On Wed, Oct 21, 2026 at 3:37 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
It meant existing codes might also be fixed, right?
Yes, probably. I haven't looked at the referenced code in detail yet, though..
Good catch, LGTM.
Thanks for the review! I've pushed the patches.
Regards,
--
Fujii Masao