Re: Synchronous LISTEN/NOTIFY?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)my>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Synchronous LISTEN/NOTIFY?
Date: 2001-01-05 03:47:56
Message-ID: 29095.978666476@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ron Peterson 2001-01-05 03:53:12 Re: How passwords can be crypted in postgres?
Previous Message Lincoln Yeoh 2001-01-05 03:16:17 Synchronous LISTEN/NOTIFY?