Re: Listen and notify in psql process

From: Torsten Förtsch <tfoertsch123(at)gmail(dot)com>
To: Sakshi Jain <sakshijain10388(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Listen and notify in psql process
Date: 2021-06-17 13:34:05
Message-ID: CAKkG4_nrYBzd_upM9V5OUmZoA-ApxgLNpGeXq0JES56kwLs1-Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

On Thu, Jun 17, 2021 at 1:04 PM Sakshi Jain <sakshijain10388(at)gmail(dot)com>
wrote:

How to listen from within a psql process and get the payloads?
>
> Do Postgresql have any such a mechanism where in a session a process send
> a "listen <name>" sql command and then gets a message if someone in the
> other session issued a "notify <name>".
>
> Please provide an example of how to do this.
>
> I am looking for listen to return some thing when a notify has been issued.
>

Here is an example in perl. Basically you issue the LISTEN command. Then
you sit and watch the file descriptor to become ready. You do that outside
of a transaction. Once the descriptor is ready you call your driver's
version of notification read function until it comes back empty. That is
when you go back and wait for the next arrival.

$dbh->do("LISTEN channel");
my $sel = IO::Select->new;
$sel->add($dbh->{pg_socket});
while ($sel->can_read) {
while (my $notify = $dbh->pg_notifies) {
my ($name, $pid, $payload) = @$notify;
do_something($payload);
}
}

>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Celia McInnis 2021-06-17 14:13:05 Treating float arrays as vectors?
Previous Message Ravi Krishna 2021-06-17 11:45:27 Re: Listen and notify in psql process

Browse pgsql-sql by date

  From Date Subject
Next Message Erik Brandsberg 2021-06-17 14:22:34 Re: Listen and notify in psql process
Previous Message Ravi Krishna 2021-06-17 11:45:27 Re: Listen and notify in psql process