Skip to content

Commit 34aad21

Browse files
committed
Client-side fixes for delayed NOTIFY receipt.
PQnotifies() is defined to just process already-read data, not try to read any more from the socket. (This is a debatable decision, perhaps, but I'm hesitant to change longstanding library behavior.) The documentation has long recommended calling PQconsumeInput() before PQnotifies() to ensure that any already-arrived message would get absorbed and processed. However, psql did not get that memo, which explains why it's not very reliable about reporting notifications promptly. Also, most (not quite all) callers called PQconsumeInput() just once before a PQnotifies() loop. Taking this recommendation seriously implies that we should do PQconsumeInput() before each call. This is more important now that we have "payload" strings in notification messages than it was before; that increases the probability of having more than one packet's worth of notify messages. Hence, adjust code as well as documentation examples to do it like that. Back-patch to 9.5 to match related server fixes. In principle we could probably go back further with these changes, but given lack of field complaints I doubt it's worthwhile. Discussion: https://postgr.es/m/CAOYf6ec-TmRYjKBXLLaGaB-jrd=mjG1Hzn1a1wufUAR39PQYhw@mail.gmail.com
1 parent cbab940 commit 34aad21

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5000,7 +5000,7 @@ typedef struct pgNotify
50005000
<para>
50015001
<function>PQnotifies</function> does not actually read data from the
50025002
server; it just returns messages previously absorbed by another
5003-
<application>libpq</application> function. In prior releases of
5003+
<application>libpq</application> function. In ancient releases of
50045004
<application>libpq</application>, the only way to ensure timely receipt
50055005
of <command>NOTIFY</> messages was to constantly submit commands, even
50065006
empty ones, and then check <function>PQnotifies</function> after each
@@ -8313,6 +8313,7 @@ main(int argc, char **argv)
83138313
notify->relname, notify->be_pid);
83148314
PQfreemem(notify);
83158315
nnotifies++;
8316+
PQconsumeInput(conn);
83168317
}
83178318
}
83188319

src/bin/psql/common.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ PrintNotifications(void)
693693
{
694694
PGnotify *notify;
695695

696-
while ((notify = PQnotifies(pset.db)))
696+
PQconsumeInput(pset.db);
697+
while ((notify = PQnotifies(pset.db)) != NULL)
697698
{
698699
/* for backward compatibility, only show payload if nonempty */
699700
if (notify->extra[0])
@@ -704,6 +705,7 @@ PrintNotifications(void)
704705
notify->relname, notify->be_pid);
705706
fflush(pset.queryFout);
706707
PQfreemem(notify);
708+
PQconsumeInput(pset.db);
707709
}
708710
}
709711

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,12 +1730,13 @@ ecpg_process_output(struct statement * stmt, bool clear_result)
17301730
}
17311731

17321732
/* check for asynchronous returns */
1733-
notify = PQnotifies(stmt->connection->connection);
1734-
if (notify)
1733+
PQconsumeInput(stmt->connection->connection);
1734+
while ((notify = PQnotifies(stmt->connection->connection)) != NULL)
17351735
{
17361736
ecpg_log("ecpg_process_output on line %d: asynchronous notification of \"%s\" from backend PID %d received\n",
17371737
stmt->lineno, notify->relname, notify->be_pid);
17381738
PQfreemem(notify);
1739+
PQconsumeInput(stmt->connection->connection);
17391740
}
17401741

17411742
return status;

src/interfaces/libpq/fe-exec.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,9 @@ PQsendDescribe(PGconn *conn, char desc_type, const char *desc_target)
22392239
* no unhandled async notification from the backend
22402240
*
22412241
* the CALLER is responsible for FREE'ing the structure returned
2242+
*
2243+
* Note that this function does not read any new data from the socket;
2244+
* so usually, caller should call PQconsumeInput() first.
22422245
*/
22432246
PGnotify *
22442247
PQnotifies(PGconn *conn)

src/test/examples/testlibpq2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ main(int argc, char **argv)
140140
notify->relname, notify->be_pid);
141141
PQfreemem(notify);
142142
nnotifies++;
143+
PQconsumeInput(conn);
143144
}
144145
}
145146

0 commit comments

Comments
 (0)