Re: Additional message in pg_terminate_backend

Jim Jones <jim.jones@uni-muenster.de>

From: Jim Jones <jim.jones@uni-muenster.de>
To: Roman Khapov <rkhapov@yandex-team.ru>
Cc: Kirill Reshke <reshkekirill@gmail.com>, Daniel Gustafsson <daniel@yesql.se>, pgsql-hackers@lists.postgresql.org
Date: 2026-02-03T08:21:57Z
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