Fix tracing of BackendKeyData and CancelRequest

BackendKeyData length was increased from 4 bytes to a variable-length
length (up to 256 bytes) in a460251f0a. However, pqTrace still traces
it as a 4 bytes key, leading to a "mismatched message length" warning
message. The same issue impacts the tracing of CancelRequest.

This patch fixes the issue by using pqTraceOutputNchar instead of
pqTraceOutputInt32 in both cases.

Author: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Discussion: https://www.postgresql.org/message-id/CAO6_Xqo6gTv9=76H=k2qDRFU+KHuBiY2S=bQynEr6J8gS7L6xA@mail.gmail.com
Backpatch-through: 18
This commit is contained in:
Heikki Linnakangas 2026-07-03 14:57:35 +03:00
parent 598af79b1b
commit dd5eca055d

View file

@ -452,11 +452,12 @@ pqTraceOutput_CopyOutResponse(FILE *f, const char *message, int *cursor)
}
static void
pqTraceOutput_BackendKeyData(FILE *f, const char *message, int *cursor, bool regress)
pqTraceOutput_BackendKeyData(FILE *f, const char *message, int *cursor, int length,
bool regress)
{
fprintf(f, "BackendKeyData\t");
pqTraceOutputInt32(f, message, cursor, regress);
pqTraceOutputInt32(f, message, cursor, regress);
pqTraceOutputNchar(f, length - *cursor + 1, message, cursor, regress);
}
static void
@ -762,7 +763,8 @@ pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer)
/* No message content */
break;
case PqMsg_BackendKeyData:
pqTraceOutput_BackendKeyData(conn->Pfdebug, message, &logCursor, regress);
pqTraceOutput_BackendKeyData(conn->Pfdebug, message, &logCursor,
length, regress);
break;
case PqMsg_NoData:
fprintf(conn->Pfdebug, "NoData");
@ -876,7 +878,8 @@ pqTraceOutputNoTypeByteMessage(PGconn *conn, const char *message)
pqTraceOutputInt16(conn->Pfdebug, message, &logCursor);
pqTraceOutputInt16(conn->Pfdebug, message, &logCursor);
pqTraceOutputInt32(conn->Pfdebug, message, &logCursor, regress);
pqTraceOutputInt32(conn->Pfdebug, message, &logCursor, regress);
pqTraceOutputNchar(conn->Pfdebug, length - logCursor, message,
&logCursor, regress);
}
else if (version == NEGOTIATE_SSL_CODE)
{