Re: How about synchronous notifications? - Mailing list pgsql-general

From Vick Khera
Subject Re: How about synchronous notifications?
Date
Msg-id AANLkTimCPN+UdS7tuZgsWD+N3dPHexBfH21Rz64_oUbg@mail.gmail.com
Whole thread Raw
In response to How about synchronous notifications?  (Lincoln Yeoh <lyeoh@pop.jaring.my>)
List pgsql-general
On Tue, Sep 21, 2010 at 12:23 PM, Lincoln Yeoh <lyeoh@pop.jaring.my> wrote:
> To me what would also be useful would be synchronous notifications.
>
> This would allow many programs to wait for events to happen rather than all
> of them polling the database (and wasting CPU cycles, battery life, etc).
> You could still poll with a suitable timeout if you want.
>

Here's how you do it: first, make sure you are not within a
transaction or other Pg activity.  Get the socket's file handle from
the Pg connection handle.  When you're ready to wait for a notify
event, just do a select() system call on that file handle waiting
until there is data to read on that socket.

When you return from the select, just check for the notifications and
you're ready to go.  If you did not find a notification, return to the
select() call.  Of course, this assumes you've issued the necessary
LISTEN command.

This has worked for me (and is tested well) up thru Pg 8.3. I cannot
imagine it would stop working as the wire line protocol doesn't really
change.

pgsql-general by date:

Previous
From: Mike Christensen
Date:
Subject: Re: What's wrong with this query?
Next
From: Vick Khera
Date:
Subject: Re: versioned pl/pgsql functions