Re: Synchronous LISTEN/NOTIFY? - Mailing list pgsql-general

From Tom Lane
Subject Re: Synchronous LISTEN/NOTIFY?
Date
Msg-id 29095.978666476@sss.pgh.pa.us
Whole thread Raw
In response to Synchronous LISTEN/NOTIFY?  (Lincoln Yeoh <lyeoh@pop.jaring.my>)
Responses Re: Synchronous LISTEN/NOTIFY?
List pgsql-general
Lincoln Yeoh <lyeoh@pop.jaring.my> writes:
> Basically you now can have apps use the database for instant messaging :).

My former company was doing that since Postgres 6.4 or so.

> Right now it looks like B has to keep polling regularly.

No, it just has to use select() to wait on the postgres connection
(plus any other input files it wants to pay attention to).

> Basically LISTEN doesn't wait.

LISTEN has nothing to do with waiting.  It merely informs the backend
of your interest in subsequently receiving notices of a particular type.
Perhaps you should think of it as like signal(2).

See
http://www.postgresql.org/devel-corner/docs/postgres/libpq-notify.htm
but in brief the idea is:

1. The outer event loop of your application uses select() to wait on
the PQsocket() fd as well as any other interesting fds.

2. When you see input ready on the PQsocket() fd, call PQconsumeInput(),
then check PQnotifies().

3. Also check PQnotifies() after any PQexec(), to see if notify messages
came in during the query.

Keep in mind also that notifications are only delivered when you are
not within a transaction block (BEGIN/END).

            regards, tom lane

pgsql-general by date:

Previous
From: Lincoln Yeoh
Date:
Subject: Synchronous LISTEN/NOTIFY?
Next
From: Lamar Owen
Date:
Subject: Re: 7.0.3 rpm testing & other problems