BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

Поиск
Список
Период
Сортировка
Искать

BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

От:
PG Bug reporting form <noreply@postgresql.org>
Дата:
The following bug has been logged on the website:

Bug reference:      19795
Logged by:          Brar Piening
Email address:      brar@gmx.de
PostgreSQL version: 18.6
Operating system:   Windows 11 x64 (EDB installer builds)
Description:        

A START_REPLICATION option without a value crashes the walsender when the
output plugin is pgoutput and the option is proto_version or
publication_names.
The postmaster then restarts all sessions.
A role with REPLICATION is enough; it doesn't need to be a superuser.

Steps, on a fresh cluster with wal_level = logical:

  -- as a superuser
  CREATE PUBLICATION pub FOR ALL TABLES;
  SELECT pg_create_logical_replication_slot('s', 'pgoutput');
  CREATE ROLE repuser LOGIN REPLICATION;

  pg_recvlogical -d postgres -U repuser --slot=s --start -o proto_version \
      -f - --no-loop

(Without --no-loop, pg_recvlogical sends the command again after a few
seconds.)

pg_recvlogical sends START_REPLICATION SLOT "s" LOGICAL 0/0
("proto_version")
and reports:

  pg_recvlogical: error: could not send replication command
  "START_REPLICATION SLOT "s" LOGICAL 0/0 ("proto_version")": server closed
  the connection unexpectedly

Server log (18.6):

  LOG:  client backend (PID 21132) was terminated by exception 0xC0000005
  DETAIL:  Failed process was running: START_REPLICATION SLOT "s" LOGICAL
0/0 ("proto_version")
  LOG:  terminating any other active server processes
  LOG:  all server processes terminated; reinitializing
  LOG:  database system was not properly shut down; automatic recovery in
progress
  ...
  LOG:  database system is ready to accept connections

"-o publication_names" crashes the same way. I reproduced both on 14.24,
15.19, 16.15, 17.11 and 18.6, all Windows builds, where the crash shows up
as an access violation.

Expected: an error, as for pgoutput's origin option without a value:

  ERROR:  origin requires a parameter
  CONTEXT:  slot "s", output plugin "pgoutput", in the startup callback

Cause: the replication grammar accepts an option without a value and leaves
DefElem.arg NULL (repl_gram.y, plugin_opt_arg: "| /* EMPTY */ { $$ = NULL;
}").
parse_output_parameters() in pgoutput.c allows this ("Assert(defel->arg ==
NULL || IsA(defel->arg, String))"), but for proto_version and
publication_names it calls strVal(defel->arg), which dereferences the NULL
pointer.
The other options are read with defGetBoolean() (a missing value means true)
or defGetString() (origin, which raises the error above).
The SQL functions such as pg_logical_slot_get_changes() can't produce a NULL
arg, since logicalfuncs.c wraps every value in makeString().
So only the replication protocol path crashes.
test_decoding checks for a NULL arg itself.

Suggested fix: read both options with defGetString(defel), as origin does,
on all supported branches.

I found this while working on Npgsql (the .NET driver), whose generic
logical replication API currently sends an option without a value when the
caller passes null.




Re: BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Вложения:
Hi,

On Sun, Oct 25, 2026 at 9:31 PM PG Bug reporting form
 wrote:
>
> The following bug has been logged on the website:
>
> Bug reference:      19795
> Logged by:          Brar Piening
> Email address:      brar@gmx.de
> PostgreSQL version: 18.6
> Operating system:   Windows 11 x64 (EDB installer builds)
> Description:
>
> A START_REPLICATION option without a value crashes the walsender when the
> output plugin is pgoutput and the option is proto_version or
> publication_names.
> The postmaster then restarts all sessions.
> A role with REPLICATION is enough; it doesn't need to be a superuser.
>
> Steps, on a fresh cluster with wal_level = logical:
>
>   -- as a superuser
>   CREATE PUBLICATION pub FOR ALL TABLES;
>   SELECT pg_create_logical_replication_slot('s', 'pgoutput');
>   CREATE ROLE repuser LOGIN REPLICATION;
>
>   pg_recvlogical -d postgres -U repuser --slot=s --start -o proto_version \
>       -f - --no-loop
>
> (Without --no-loop, pg_recvlogical sends the command again after a few
> seconds.)
>
> pg_recvlogical sends START_REPLICATION SLOT "s" LOGICAL 0/0
> ("proto_version")
> and reports:
>
>   pg_recvlogical: error: could not send replication command
>   "START_REPLICATION SLOT "s" LOGICAL 0/0 ("proto_version")": server closed
>   the connection unexpectedly
>
> Server log (18.6):
>
>   LOG:  client backend (PID 21132) was terminated by exception 0xC0000005
>   DETAIL:  Failed process was running: START_REPLICATION SLOT "s" LOGICAL
> 0/0 ("proto_version")
>   LOG:  terminating any other active server processes
>   LOG:  all server processes terminated; reinitializing
>   LOG:  database system was not properly shut down; automatic recovery in
> progress
>   ...
>   LOG:  database system is ready to accept connections
>
> "-o publication_names" crashes the same way. I reproduced both on 14.24,
> 15.19, 16.15, 17.11 and 18.6, all Windows builds, where the crash shows up
> as an access violation.
>
> Expected: an error, as for pgoutput's origin option without a value:
>
>   ERROR:  origin requires a parameter
>   CONTEXT:  slot "s", output plugin "pgoutput", in the startup callback
>
> Cause: the replication grammar accepts an option without a value and leaves
> DefElem.arg NULL (repl_gram.y, plugin_opt_arg: "| /* EMPTY */ { $$ = NULL;
> }").
> parse_output_parameters() in pgoutput.c allows this ("Assert(defel->arg ==
> NULL || IsA(defel->arg, String))"), but for proto_version and
> publication_names it calls strVal(defel->arg), which dereferences the NULL
> pointer.
> The other options are read with defGetBoolean() (a missing value means true)
> or defGetString() (origin, which raises the error above).
> The SQL functions such as pg_logical_slot_get_changes() can't produce a NULL
> arg, since logicalfuncs.c wraps every value in makeString().
> So only the replication protocol path crashes.
> test_decoding checks for a NULL arg itself.
>
> Suggested fix: read both options with defGetString(defel), as origin does,
> on all supported branches.
>
> I found this while working on Npgsql (the .NET driver), whose generic
> logical replication API currently sends an option without a value when the
> caller passes null.

Thanks for the report and analysis. I could reproduce the crash, and I
agree with the suggested fix.

Attached is a patch that uses defGetString() for "proto_version" and
"publication_names" and adds a simple test in 030_pg_recvlogical.pl
to confirm it works.

Best Regards,
Zhijie Hou

RE: BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Hi,

> Attached is a patch that uses defGetString() for "proto_version" and
> "publication_names" and adds a simple test in 030_pg_recvlogical.pl
> to confirm it works.

Thanks for the patch. One question:

```
			if (parsed > PG_UINT32_MAX)
				ereport(ERROR,
						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
						 errmsg("proto_version \"%s\" out of range",
								strVal(defel->arg))));
```

Here strVal() is still used, but I feel it's better to refer `val` here. We can completely remove
the function call.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Re: BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

От:
Kirill Reshke <reshkekirill@gmail.com>
Дата:
On Mon, 26 Oct 2026 at 07:56, Hayato Kuroda (Fujitsu)
 wrote:
>
> Hi,
>
> > Attached is a patch that uses defGetString() for "proto_version" and
> > "publication_names" and adds a simple test in 030_pg_recvlogical.pl
> > to confirm it works.
>
> Thanks for the patch. One question:
>
> ```
>                         if (parsed > PG_UINT32_MAX)
>                                 ereport(ERROR,
>                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
>                                                  errmsg("proto_version \"%s\" out of range",
>                                                                 strVal(defel->arg))));
> ```
>
> Here strVal() is still used, but I feel it's better to refer `val` here. We can completely remove
> the function call.
>
> Best regards,
> Hayato Kuroda
> FUJITSU LIMITED


Hi!
Looks like a real issue. I reviewed a patch, I think this is a natural
follow-up for [0]. the CREATE_REPLICATION_SLOT/ALTER_REPLICATION_SLOT
option parser only uses defGetString/defGetBoolean - pgoutput is the
only crasher.

Also, should we reject option like

ereport(ERROR,
 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
       errmsg("option \"%s\" requires a value",
        "publication_names")));


instead of a generic error message from defGetString?


[0] https://github.com/postgres/postgres/commit/290bb8afe6b


-- 
Best regards,
Kirill Reshke


Re: BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Hi,

On Wed, Oct 28, 2026 at 01:36 AM Kirill Reshke wrote:
> Looks like a real issue. I reviewed a patch, I think this is a natural
> follow-up for [0]. the CREATE_REPLICATION_SLOT/ALTER_REPLICATION_SLOT
> option parser only uses defGetString/defGetBoolean - pgoutput is the
> only crasher.
> 
> Also, should we reject option like
> 
> ereport(ERROR,
>   (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
>         errmsg("option \"%s\" requires a value",
>          "publication_names")));
> 
> 
> instead of a generic error message from defGetString?
I think a special message is unnecessary here, especially since we don't 
do that in other replication parsing code, so I've kept the current version.

-- 
Best Regards,
Zhijie Hou


Re: BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Вложения:
Hi,

On Mon, Oct 26, 2026 at 10:56 AM "Hayato Kuroda (Fujitsu)" wrote:
> 
>> Attached is a patch that uses defGetString() for "proto_version" and
>> "publication_names" and adds a simple test in 030_pg_recvlogical.pl
>> to confirm it works.
> 
> Thanks for the patch. One question:
> 
> ```
> 			if (parsed > PG_UINT32_MAX)
> 				ereport(ERROR,
> 						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
> 						 errmsg("proto_version \"%s\" out of range",
> 								strVal(defel->arg))));
> ```
> 
> Here strVal() is still used, but I feel it's better to refer `val` here. We can completely remove
> the function call.

Thanks for reviewing.

Here is the V2 patch.

-- 
Best Regards,
Zhijie Hou

Re: BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Sat, Oct 31, 2026 at 07:32:00PM +0800, Zhijie Hou wrote:
> Thanks for reviewing.
> 
> Here is the V2 patch.

Thanks for the patch, nice catch.  I'll handle that and backpatch.
I regret missing that during the lookup of this code for 290bb8afe6b4,
especially as the crash was possible before it for the two code paths
you are pointing at with the two direct accesses to defel->arg.
--
Michael

Re: BUG #19795: pgoutput: walsender crashes on START_REPLICATION option without a value

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Sun, Nov 01, 2026 at 05:00:31PM +0900, Michael Paquier wrote:
> Thanks for the patch, nice catch.  I'll handle that and backpatch.
> I regret missing that during the lookup of this code for 290bb8afe6b4,
> especially as the crash was possible before it for the two code paths
> you are pointing at with the two direct accesses to defel->arg.

Applied down to v14 as of 35848657c67d.  Thanks, all.
--
Michael
FAQ