Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Stabilize NOTIFY behavior by transmitting notifies before ReadyForQuery.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 24 Nov 2019 19:42:59 +0000 (14:42 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 24 Nov 2019 19:42:59 +0000 (14:42 -0500)
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 b10f40bf0.  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

src/backend/commands/async.c
src/backend/tcop/postgres.c

index a93c81bca28798e64ef84be847eecf12c6c6c740..9b5273a6de826e36d175dcb5152a05ca85fbeac1 100644 (file)
@@ -1721,11 +1721,13 @@ HandleNotifyInterrupt(void)
 /*
  * ProcessNotifyInterrupt
  *
- *     This is called just after waiting for a frontend command.  If a
- *     interrupt arrives (via HandleNotifyInterrupt()) while reading, the
- *     read will be interrupted via the process's latch, and this routine
- *     will get called.  If we are truly idle (ie, *not* inside a transaction
- *     block), process the incoming notifies.
+ *     This is called if we see notifyInterruptPending set, just before
+ *     transmitting ReadyForQuery at the end of a frontend command, and
+ *     also if a notify signal occurs while reading from the frontend.
+ *     HandleNotifyInterrupt() will cause the read to be interrupted
+ *     via the process's latch, and this routine will get called.
+ *     If we are truly idle (ie, *not* inside a transaction block),
+ *     process the incoming notifies.
  */
 void
 ProcessNotifyInterrupt(void)
index 3560f6488526662e2330ddc0c292213f35e79eea..b8ff74837aede543fdccedb7d3c5e984e056eb71 100644 (file)
@@ -4034,7 +4034,18 @@ PostgresMain(int argc, char *argv[],
            }
            else
            {
+               /* Send out notify signals and transmit self-notifies */
                ProcessCompletedNotifies();
+
+               /*
+                * Also process incoming notifies, if any.  This is mostly to
+                * ensure stable behavior in tests: if any notifies were
+                * received during the just-finished transaction, they'll be
+                * seen by the client before ReadyForQuery is.
+                */
+               if (notifyInterruptPending)
+                   ProcessNotifyInterrupt();
+
                pgstat_report_stat(false);
 
                set_ps_display("idle", false);