listen on NOTIFY zc
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 10 May 2022 07:11:49 +0000 (09:11 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 10 May 2022 07:11:49 +0000 (09:11 +0200)
pg-listen-zc.pl [new file with mode: 0755]

diff --git a/pg-listen-zc.pl b/pg-listen-zc.pl
new file mode 100755 (executable)
index 0000000..6f8563f
--- /dev/null
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use autodie;
+
+use DBI;
+use IO::Select;
+use Data::Dump qw(dump);
+
+my $dbh = DBI->connect("dbi:Pg:dbname=zc","dpavlin","", { RaiseError => 1, AutoCommit => 1 });
+
+$dbh->do("LISTEN zc");
+
+my $fd = $dbh->func("getfd");
+my $sel = IO::Select->new($fd);
+
+
+while (1) {
+       print "waiting...\n";
+       $sel->can_read;
+       my $notify = $dbh->func("pg_notifies");
+       if ($notify) {
+               warn "# notify = ",dump($notify);
+               my ($relname, $pid, $payload) = @$notify;
+               print "$relname from PID $pid\n";
+       }
+}
+