Skip to content

Commit c47f498

Browse files
committed
Stabilize NOTIFY behavior by transmitting notifies before ReadyForQuery.
This patch ensures that, if any notify messages were received during a just-finished transaction, they get sent to the frontend just before not just after the ReadyForQuery message. With libpq and other client libraries that act similarly, this guarantees that the client will see the notify messages as available as soon as it thinks the transaction is done. This probably makes no difference in practice, since in realistic use-cases the application would have to cope with asynchronous arrival of notify events anyhow. However, it makes it a lot easier to build cross-session-notify test cases with stable behavior. I'm a bit surprised now that we've not seen any buildfarm instability with the test cases added by commit b10f40b. Tests that I intend to add in an upcoming bug fix are definitely unstable without this. Back-patch to 9.6, which is as far back as we can do NOTIFY testing with the isolationtester infrastructure. Discussion: https://postgr.es/m/13881.1574557302@sss.pgh.pa.us
1 parent 7d4c311 commit c47f498

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/backend/commands/async.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,11 +1720,13 @@ HandleNotifyInterrupt(void)
17201720
/*
17211721
* ProcessNotifyInterrupt
17221722
*
1723-
* This is called just after waiting for a frontend command. If a
1724-
* interrupt arrives (via HandleNotifyInterrupt()) while reading, the
1725-
* read will be interrupted via the process's latch, and this routine
1726-
* will get called. If we are truly idle (ie, *not* inside a transaction
1727-
* block), process the incoming notifies.
1723+
* This is called if we see notifyInterruptPending set, just before
1724+
* transmitting ReadyForQuery at the end of a frontend command, and
1725+
* also if a notify signal occurs while reading from the frontend.
1726+
* HandleNotifyInterrupt() will cause the read to be interrupted
1727+
* via the process's latch, and this routine will get called.
1728+
* If we are truly idle (ie, *not* inside a transaction block),
1729+
* process the incoming notifies.
17281730
*/
17291731
void
17301732
ProcessNotifyInterrupt(void)

src/backend/tcop/postgres.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4153,7 +4153,18 @@ PostgresMain(int argc, char *argv[],
41534153
}
41544154
else
41554155
{
4156+
/* Send out notify signals and transmit self-notifies */
41564157
ProcessCompletedNotifies();
4158+
4159+
/*
4160+
* Also process incoming notifies, if any. This is mostly to
4161+
* ensure stable behavior in tests: if any notifies were
4162+
* received during the just-finished transaction, they'll be
4163+
* seen by the client before ReadyForQuery is.
4164+
*/
4165+
if (notifyInterruptPending)
4166+
ProcessNotifyInterrupt();
4167+
41574168
pgstat_report_stat(false);
41584169

41594170
set_ps_display("idle", false);

0 commit comments

Comments
 (0)