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