Subject: pgsql: libpq: Extend "read pending" check from SSL to GSS

From: Heikki Linnakangas <[email-masked]>
Date: Tue, 07 Jul 2026 15:56:46 +0000

libpq: Extend "read pending" check from SSL to GSS

An extra check for pending bytes in the SSL layer has been part of
pqReadReady() for a very long time (79ff2e96d). But when GSS transport
encryption was added, it didn't receive the same treatment. (As
79ff2e96d notes, "The bug that I fixed in this patch is exceptionally
hard to reproduce reliably.")

Without that check, it's possible to hit a hang in gssencmode, if the
server splits a large libpq message such that the final message in a
streamed response is part of the same wrapped token as the split
message:

    DataRowDataRowDataRowDataRowDataRowData
    -- token boundary --
    RowDataRowCommandCompleteReadyForQuery

If the split message takes up enough memory to nearly fill libpq's
receive buffer, libpq may return from pqReadData() before the later
messages are pulled out of the PqGSSRecvBuffer. Without additional
socket activity from the server, pqReadReady() (via pqSocketCheck())
will never again return true, hanging the connection.

Pull the pending-bytes check into the pqsecure API layer, where both
SSL and GSS now implement it.

Note that this does not fix the root problem! Third party clients of
libpq have no way to call pqsecure_read_is_pending() in their own
polling. This just brings the GSS implementation up to par with the
existing SSL workaround; a broader fix is left to a subsequent commit.

In preparation for the broader fix, this patch already changes the
*_read_pending() functions to return the number of bytes in the buffer
rather than just a boolean. The current callers don't need that, but
the subsequent fix will.

Author: Jacob Champion <[email-masked]>
Discussion: https://postgr.es/m/CAOYmi%2BmpymrgZ76Jre2dx_PwRniS9YZojwH0rZnTuiGHCsj0rA%40mail.gmail.com
Backpatch-through: 14

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/eeb2940ae83cb6a5e48e90465bea0cede341ea59

Modified Files
--------------
src/interfaces/libpq/fe-misc.c           |  6 ++----
src/interfaces/libpq/fe-secure-gssapi.c  |  7 +++++++
src/interfaces/libpq/fe-secure-openssl.c | 34 +++++++++++++++++++++++++++++---
src/interfaces/libpq/fe-secure.c         | 22 +++++++++++++++++++++
src/interfaces/libpq/libpq-int.h         |  6 ++++--
5 files changed, 66 insertions(+), 9 deletions(-)