Fix tracing of BackendKeyData and CancelRequest messages

From: Anthonin Bonnefoy <anthonin(dot)bonnefoy(at)datadoghq(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Fix tracing of BackendKeyData and CancelRequest messages
Date: 2026-07-03 09:42:42
Message-ID: CAO6_Xqo6gTv9=76H=k2qDRFU+KHuBiY2S=bQynEr6J8gS7L6xA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

BackendKeyData length was increased from 4 bytes to a variable sized
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 shows the lack of coverage of tracing startup packets:
libpq_pipeline only starts traces once the connection is established.
There's also no way to test this with psql.

The attached patchset fixes the traces BackendKeyData and
CancelRequest, add the possibility of tracing any connection created
by libpq, and add test coverage for the startup packets. It contains
the following files:

0001: Fix tracing of BackendKeyData and CancelRequest messages

0002: Add PGTRACE env var in libpq to enable protocol tracing to the
target file (or '-' for stdout). One limitation of this approach is
that you should only have one connection writing to a trace file. If
an application opens multiple connections to the same PGTRACE, each
connection will open its own fd and erase others traces. But given
this is to debug protocol messages, it sounds like an acceptable
limitation.

0003: Replace libpq_pipeline PQtrace calls by PGTRACE, adding tracing
of startup messages, with some additional regress masking. I've also
added a warning message to report mismatch length similar to what's
done in pqTraceOutputMessage.

0004: Add tracing of cancel requests in libpq_pipeline test. Calling
PQuntrace on the main and monitor connections was necessary to allow
the cancel connections to append the cancel messages without conflict.

Regards,
Anthonin Bonnefoy

Attachment Content-Type Size
v1-0002-Add-PGTRACE-env-to-enable-protocol-tracing-in-lib.patch application/octet-stream 7.6 KB
v1-0004-Test-tracing-of-cancel-requests.patch application/octet-stream 5.3 KB
v1-0001-Fix-tracing-of-BackendKeyData-and-CancelRequest.patch application/octet-stream 2.3 KB
v1-0003-Add-trace-of-startup-packets-in-libpq_pipeline-te.patch application/octet-stream 21.8 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Etsuro Fujita 2026-07-03 09:47:54 Re: use of SPI by postgresImportForeignStatistics
Previous Message Heikki Linnakangas 2026-07-03 09:25:10 Re: libpq: Process buffered SSL read bytes to support records >8kB on async API