Fix tracing of BackendKeyData and CancelRequest messages
Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
From: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2026-07-03T09:42:42Z
Lists: pgsql-hackers
Attachments
- v1-0002-Add-PGTRACE-env-to-enable-protocol-tracing-in-lib.patch (application/octet-stream) patch v1-0002
- v1-0004-Test-tracing-of-cancel-requests.patch (application/octet-stream) patch v1-0004
- v1-0001-Fix-tracing-of-BackendKeyData-and-CancelRequest.patch (application/octet-stream) patch v1-0001
- v1-0003-Add-trace-of-startup-packets-in-libpq_pipeline-te.patch (application/octet-stream) patch v1-0003
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
Commits
-
Fix tracing of BackendKeyData and CancelRequest
- dd5eca055d48 18 (unreleased) landed
- 0766bc57e9f9 19 (unreleased) landed
- b22b619056e8 master landed