Re: Additional message in pg_terminate_backend

From: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
To: Roman Khapov <rkhapov(at)yandex-team(dot)ru>
Cc: Kirill Reshke <reshkekirill(at)gmail(dot)com>, Daniel Gustafsson <daniel(at)yesql(dot)se>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Additional message in pg_terminate_backend
Date: 2026-02-03 08:21:57
Message-ID: f1e805d4-eb48-4ebb-ad4f-269ebb8d139b@uni-muenster.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On 03/02/2026 08:52, Roman Khapov wrote:
> The message is truncated inside BackendMsgSet function, so I see a little
> point in truncating it at pg_terminate_backend..

Thanks for the update!

You might have a point there. One issue I see is with UTF8 character
boundaries. Calculating len like this can lead to invalid characters,
for instance:

postgres=# SELECT
pg_terminate_backend(pg_backend_pid(),0,repeat('🐘',1000));

NOTICE: message is too long, truncated to 127
FATAL: terminating connection due to administrator command:
🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘🐘�
server closed the connection unexpectedly
This probably means the server terminated abnormally

You might wanna take a look at other alternatives, such as pg_mbcliplen,
for instance (pseudocode):

len = strlen(msg);
if (len >= sizeof(slot->msg))
len = pg_mbcliplen(msg, len, sizeof(slot->msg) - 1);

memcpy(slot->msg, msg, len);
slot->msg[len] = '\0';

Best, Jim

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrey Borodin 2026-02-03 08:26:43 Re: Additional message in pg_terminate_backend
Previous Message Peter Eisentraut 2026-02-03 08:09:17 Re: rename and move AssertVariableIsOfType