Re: BackendKeyData is mandatory?
Heikki Linnakangas <hlinnaka@iki.fi>
From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Jelte Fennema-Nio <postgres@jeltef.nl>,
Jacob Champion <jacob.champion@enterprisedb.com>,
Tatsuo Ishii <ishii@postgresql.org>, peter@eisentraut.org,
pgsql-hackers@postgresql.org, Dave Cramer <davecramer@gmail.com>
Date: 2025-08-08T15:50:27Z
Lists: pgsql-hackers
On 08/08/2025 17:01, Tom Lane wrote: > Heikki Linnakangas <hlinnaka@iki.fi> writes: >> I noticed that there's a similar, existing case in getNotify(), where >> libpq just hangs if an allocation fails. To simulate that, apply this >> change and use LISTEN/NOTIFY: > >> --- a/src/interfaces/libpq/fe-protocol3.c >> +++ b/src/interfaces/libpq/fe-protocol3.c >> @@ -1587,7 +1587,7 @@ getNotify(PGconn *conn) >> if (pqGets(&conn->workBuffer, conn)) >> return EOF; >> /* must save name while getting extra string */ >> - svname = strdup(conn->workBuffer.data); >> + svname = NULL; >> if (!svname) >> return EOF; >> if (pqGets(&conn->workBuffer, conn)) > >> Returning EOF means "not enough data", which is wrong here just like in >> getBackendKeyData(). > > The implication of "return EOF" is "try again later", which seems like > about the best thing we can do. We could: a) report an error, b) silently discard the async notification and move one, or c) disconnect. There's another malloc() call in the same getNotify() function, and if that fails, we silently discard the notification and move on (i.e. option b). So it's inconsistent. The current behavior is "hang until more data is received from the server, then try again". I think any of the other options would be better. There's no guarantee that more data will ever arrive, the connection might be used just to wait for the notification. > If memory serves, other places that construct query results do > likewise. It's a mix. Most functions that are related to queries, e.g. getRowDescriptions(), construct an error result object. If we run out of space in allocating the input buffer in pqCheckInBufferSpace(), we disconnect. Another weird error handling: functions like pqGetInt() and pqGets() don't check that you don't read past the end of the message. They do check that they don't read past the input buffer, but the input buffer might contain another message after the current one, and the functions will merrily read past the message boundary. We do check for that later, report the "message contents do not agree with length ..." message, and resync. But it's weird that we don't check for that earlier already. - Heikki
Commits
-
Revert unnecessary check for NULL
- 5a26a3e4eeb2 18.0 landed
- 807ee417e562 19 (unreleased) landed
-
libpq: Be strict about cancel key lengths
- e7ff96853ea7 18.0 landed
- e411a8d25a4b 19 (unreleased) landed
-
libpq: Handle OOM by disconnecting instead of hanging or skipping msgs
- 8aa287c9061b 18.0 landed
- f6f0542266f0 19 (unreleased) landed
-
libpq: Complain about missing BackendKeyData later with PGgetCancel()
- 42b1480eb2c6 18.0 landed
- a4801eb691ed 19 (unreleased) landed