Re: How about synchronous notifications?

From: Vick Khera <vivek(at)khera(dot)org>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How about synchronous notifications?
Date: 2010-09-22 11:55:27
Message-ID: AANLkTimCPN+UdS7tuZgsWD+N3dPHexBfH21Rz64_oUbg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Sep 21, 2010 at 12:23 PM, Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Vick Khera 2010-09-22 12:02:33 Re: versioned pl/pgsql functions
Previous Message Nagy Zoltan 2010-09-22 10:58:53 Re: versioned pl/pgsql functions