Thread: Inter-process communication

Inter-process communication

From
Steven Bradley
Date:
I would like to use the LISTEN and NOTIFY features of Postgres to implement
communication between to proceses connected to Postgres, but I don't want
to "listening" process to use polling to detect notifications.  The
following polling example is given in the Postgres documentation:

  while (1)   {       /*        * wait a little bit between checks; waiting with select()        * would be more
efficient.       */       sleep(1);       /* collect any asynchronous backend messages */       PQconsumeInput(conn);
   /* check for asynchronous notify messages */       while ((notify = PQnotifies(conn)) != NULL)       {
fprintf(stderr,               "ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
notify->relname,notify->be_pid);           free(notify);       }   }
 


Instead, I would rather use the "more efficient select(2)" method alluded
to in the Postgres documentation (and code above).  Does anyone have
sample code that uses this method to wait for a backend notification to
arrive?


Thanks in advance...

Steven Bradley
Lawrence Livermore National Laboratory
sbradley@llnl.gov




Re: [INTERFACES] Inter-process communication

From
Tom Lane
Date:
Steven Bradley <sbradley@llnl.gov> writes:
> Instead, I would rather use the "more efficient select(2)" method alluded
> to in the Postgres documentation (and code above).  Does anyone have
> sample code that uses this method to wait for a backend notification to
> arrive?

I have tried to come up with a reasonable self-contained example of
waiting for Postgres input with select(), but have not had much luck.
The trouble is that in any realistic scenario, you are not only waiting
for Postgres but also for other events that might come along, so the
select() is not waiting on just the Postgres input socket but also
other ones (your X server input socket, perhaps).  So in reality the
select has to be embedded in your application's outer event loop.
It's difficult to make a plausible toy example, and even more difficult
to provide code that you might be able to just drop into whatever your
outer loop currently looks like.

If you really do want to just wait for Postgres input, you could
use the pqWait() routine in libpq's fe-misc.c.
        regards, tom lane