Thread: [PATCH] notify support for DBD::Pg

[PATCH] notify support for DBD::Pg

From
Benjamin.Feinstein@guardent.com
Date:
Hey ya'll,

Attached is a patch to add support for asynchronous client notifications to
the DBD::Pg interface. The original patch was written by Alex Pilosov
<alex@pilosoft.com> and sent to this list on 10 Jun 2001. This is an updated
version that fixes some problems I encountered when running Alex's patch
against DBD-Pg-1.01. Alex's original patch was generated from a diff of
DBD-Pg-1.00.

The following description of the patch is from Alex's original email:

Added following functions (self-documented in Pg.pm):
 $ret = $dbh->func('notifies');

Returns either undef or a reference to two-element array
[ $table, $backend_pid ] of asynchronous notifications received.
 $fd = $dbh->func('getfd');

Returns fd of the actual connection to server. Can be used with
select() and func('notifies').

Also modified test.pl to test all of this functionality in the following
sequence (listen, select, notify, select).

Cheers,
Ben


diff -r DBD-Pg-1.01/Pg.pm DBD-Pg-1.01.ben/Pg.pm
631a632,640
>   $ret = $dbh->func('notifies');
> 
> Returns either undef or a reference to two-element array 
> [ $table, $backend_pid ] of asynchronous notifications received.
> 
>   $fd = $dbh->func('getfd');
> 
> Returns fd of the actual connection to server. Can be used with 
> select() and func('notifies').
diff -r DBD-Pg-1.01/Pg.xs DBD-Pg-1.01.ben/Pg.xs
85a86,103
> getfd(dbh)
>     SV *    dbh
>     CODE:
>     int ret;
>     D_imp_dbh(dbh);
> 
>     ret = dbd_db_getfd(dbh, imp_dbh);
>     ST(0) = sv_2mortal( newSViv( ret ) );
> 
> void
> notifies(dbh)
>     SV *    dbh
>     CODE:
>     D_imp_dbh(dbh);
> 
>     ST(0) = dbd_db_notifies(dbh, imp_dbh);
> 
> void
diff -r DBD-Pg-1.01/dbdimp.c DBD-Pg-1.01.ben/dbdimp.c
199a200,240
> int 
> dbd_db_getfd (dbh, imp_dbh)
>     SV *dbh;
>     imp_dbh_t *imp_dbh;
> {
>     char id;
>     SV* retsv;
> 
>     if (dbis->debug >= 1) { fprintf(DBILOGFP, "dbd_db_getfd\n"); }
> 
>     return PQsocket(imp_dbh->conn);
> }
> 
> SV * 
> dbd_db_notifies (dbh, imp_dbh)
>     SV *dbh;
>     imp_dbh_t *imp_dbh;
> {
>     char id;
>     PGnotify* notify;
>     AV* ret;
>     SV* retsv;
> 
>     if (dbis->debug >= 1) { fprintf(DBILOGFP, "dbd_db_notifies\n"); }
> 
>     PQconsumeInput(imp_dbh->conn);
> 
>     notify = PQnotifies(imp_dbh->conn);
> 
>     if (!notify) return &sv_undef; 
> 
>     ret=newAV();
> 
>     av_push(ret, newSVpv(notify->relname,0) );
>     av_push(ret, newSViv(notify->be_pid) );
>  
>     retsv = newRV(sv_2mortal((SV*)ret));
> 
>     return retsv;
> }
> 
diff -r DBD-Pg-1.01/dbdimp.h DBD-Pg-1.01.ben/dbdimp.h
72a73,74
> SV * dbd_db_notifies (SV *dbh, imp_dbh_t *imp_dbh);
> 
diff -r DBD-Pg-1.01/test.pl DBD-Pg-1.01.ben/test.pl
43c43
< my ($dbh0, $dbh, $sth);
---
> my ($dbh0, $dbh, $dbh1, $sth);
447a448,449
> # compare large objects
> 
452,453d453
< # compare large objects
< 
457a458,497
> my $fd;
> ( $fd=$dbh->func( 'getfd') )
>     and print "\$dbh->func(getfd) .......... ok\n"
>     or  print "\$dbh->func(getfd) .......... not ok\n";
> 
> ( $dbh->do( 'LISTEN test ') )
>     and print "\$dbh->do('LISTEN test') .... ok\n"
>     or  print "\$dbh->do('LISTEN test') .... not ok\n";
> 
> ( $dbh1 = DBI->connect("$dsn_test", '', '', { AutoCommit => 1 }) )
>     and print "DBI->connect (for notify)... ok\n"
>     or  die   "DBI->connect (for notify)... not ok: ", $DBI::errstr;
> 
> # there should be no data for read on $fd , until we send a notify
>    
>     my $rout;
>     my $rin = '';
>     vec($rin,$fd,1) = 1;
>     my $nfound = select( $rout=$rin, undef, undef, 0);
> 
> ( $nfound==0 ) 
>     and print "select(\$fd) returns no data. ok\n"
>     or  die   "select(\$fd) returns no data. not ok\n";
> 
> ( $dbh1->do( 'NOTIFY test ') )
>     and print "\$dbh1->do('NOTIFY test') ... ok\n"
>     or  print "\$dbh1->do('NOTIFY test') ... not ok\n";
> 
>     my $nfound2 = select( $rout=$rin, undef, undef, 1);
> 
> ( $nfound2==1 ) 
>     and print "select(\$fd) returns data.... ok\n"
>     or  die   "select(\$fd) returns data.... not ok\n";
> 
> my $notify_r;
> 
> ( $notify_r = $dbh->func('notifies') ) 
>     and print "\$dbh->func('notifies')...... ok\n"
>     or  die   "\$dbh->func('notifies')...... not ok\n";
> 
464a505,508
> 
> ( $dbh1->disconnect )
>     and print "\$dbh1->disconnect .......... ok\n"
>     or  die   "\$dbh1->disconnect .......... not ok: ", $DBI::errstr;


> Ben Feinstein
>   Software Development Engineer, R & D
>   W: 678.585.7865 x6726 F: 770.645.8311 M: 678.772.4126
>   8302 Dunwoody Pl., Suite 320, Atlanta, GA 30350 www.guardent.com
> _____________________________________________________
> G U A R D E N T
>   Enterprise Security and Privacy Programs
> 


Re: [PATCH] notify support for DBD::Pg

From
Bruce Momjian
Date:
Benjamin.Feinstein@guardent.com wrote:
> Hey ya'll,
> 
> Attached is a patch to add support for asynchronous client notifications to
> the DBD::Pg interface. The original patch was written by Alex Pilosov
> <alex@pilosoft.com> and sent to this list on 10 Jun 2001. This is an updated
> version that fixes some problems I encountered when running Alex's patch
> against DBD-Pg-1.01. Alex's original patch was generated from a diff of
> DBD-Pg-1.00.

We really need a context diff, diff -c.

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
853-3000+  If your life is a hard drive,     |  830 Blythe Avenue +  Christ can be your backup.        |  Drexel Hill,
Pennsylvania19026
 


Re: [PATCH] notify support for DBD::Pg

From
Tom Lane
Date:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> We really need a context diff, diff -c.

DBD::Pg is not ours to patch anyway.
        regards, tom lane


Re: [PATCH] notify support for DBD::Pg

From
Benjamin.Feinstein@guardent.com
Date:
Hey ya'll,

Sorry for any confusion.  Here is the diff in context format.

Cheers,
Ben


diff -c -r DBD-Pg-1.01/Pg.pm DBD-Pg-1.01.ben/Pg.pm
*** DBD-Pg-1.01/Pg.pm    Wed Jun 27 13:54:58 2001
--- DBD-Pg-1.01.ben/Pg.pm    Wed Jan  9 18:40:04 2002
***************
*** 629,634 ****
--- 629,643 ---- dump a complete table. See test.pl for an example on how to use  this function.  
+   $ret = $dbh->func('notifies');
+ 
+ Returns either undef or a reference to two-element array 
+ [ $table, $backend_pid ] of asynchronous notifications received.
+ 
+   $fd = $dbh->func('getfd');
+ 
+ Returns fd of the actual connection to server. Can be used with 
+ select() and func('notifies').  =back 
diff -c -r DBD-Pg-1.01/Pg.xs DBD-Pg-1.01.ben/Pg.xs
*** DBD-Pg-1.01/Pg.xs    Mon Jul 10 13:47:51 2000
--- DBD-Pg-1.01.ben/Pg.xs    Wed Jan  9 18:40:04 2002
***************
*** 83,88 ****
--- 83,106 ----     }  void
+ getfd(dbh)
+     SV *    dbh
+     CODE:
+     int ret;
+     D_imp_dbh(dbh);
+ 
+     ret = dbd_db_getfd(dbh, imp_dbh);
+     ST(0) = sv_2mortal( newSViv( ret ) );
+ 
+ void
+ notifies(dbh)
+     SV *    dbh
+     CODE:
+     D_imp_dbh(dbh);
+ 
+     ST(0) = dbd_db_notifies(dbh, imp_dbh);
+ 
+ void commit(dbh)     SV *    dbh     CODE:
diff -c -r DBD-Pg-1.01/dbdimp.c DBD-Pg-1.01.ben/dbdimp.c
*** DBD-Pg-1.01/dbdimp.c    Wed Jun 27 13:54:58 2001
--- DBD-Pg-1.01.ben/dbdimp.c    Wed Jan  9 18:40:04 2002
***************
*** 197,202 ****
--- 197,243 ---- }  
+ int 
+ dbd_db_getfd (dbh, imp_dbh)
+     SV *dbh;
+     imp_dbh_t *imp_dbh;
+ {
+     char id;
+     SV* retsv;
+ 
+     if (dbis->debug >= 1) { fprintf(DBILOGFP, "dbd_db_getfd\n"); }
+ 
+     return PQsocket(imp_dbh->conn);
+ }
+ 
+ SV * 
+ dbd_db_notifies (dbh, imp_dbh)
+     SV *dbh;
+     imp_dbh_t *imp_dbh;
+ {
+     char id;
+     PGnotify* notify;
+     AV* ret;
+     SV* retsv;
+ 
+     if (dbis->debug >= 1) { fprintf(DBILOGFP, "dbd_db_notifies\n"); }
+ 
+     PQconsumeInput(imp_dbh->conn);
+ 
+     notify = PQnotifies(imp_dbh->conn);
+ 
+     if (!notify) return &sv_undef; 
+ 
+     ret=newAV();
+ 
+     av_push(ret, newSVpv(notify->relname,0) );
+     av_push(ret, newSViv(notify->be_pid) );
+  
+     retsv = newRV(sv_2mortal((SV*)ret));
+ 
+     return retsv;
+ }
+  int dbd_db_ping (dbh)     SV *dbh;
diff -c -r DBD-Pg-1.01/dbdimp.h DBD-Pg-1.01.ben/dbdimp.h
*** DBD-Pg-1.01/dbdimp.h    Mon Apr  9 13:44:18 2001
--- DBD-Pg-1.01.ben/dbdimp.h    Wed Jan  9 18:40:04 2002
***************
*** 70,73 ****
--- 70,75 ---- };  
+ SV * dbd_db_notifies (SV *dbh, imp_dbh_t *imp_dbh);
+  /* end of dbdimp.h */
diff -c -r DBD-Pg-1.01/test.pl DBD-Pg-1.01.ben/test.pl
*** DBD-Pg-1.01/test.pl    Sun May 27 10:10:13 2001
--- DBD-Pg-1.01.ben/test.pl    Wed Jan  9 18:40:04 2002
***************
*** 40,46 **** my $dsn_main = "dbi:Pg:dbname=$dbmain"; my $dsn_test = "dbi:Pg:dbname=$dbtest"; 
! my ($dbh0, $dbh, $sth);  #DBI->trace(3); # make your choice 
--- 40,46 ---- my $dsn_main = "dbi:Pg:dbname=$dbmain"; my $dsn_test = "dbi:Pg:dbname=$dbtest"; 
! my ($dbh0, $dbh, $dbh1, $sth);  #DBI->trace(3); # make your choice 
***************
*** 445,460 **** # end transaction $dbh->{AutoCommit} = 1;  ( $dbh->func($lobjId, 'lo_unlink') )     and print
"\$dbh->func(lo_unlink)...... ok\n"     or  print "\$dbh->func(lo_unlink) ...... not ok\n"; 
 
- # compare large objects
-  ( $pgin cmp $buf and $pgin cmp $blob )     and print "compare blobs .............. not ok\n"     or  print "compare
blobs.............. ok\n";  ######################### disconnect and drop test database  # disconnect
 
--- 445,500 ---- # end transaction $dbh->{AutoCommit} = 1; 
+ # compare large objects
+  ( $dbh->func($lobjId, 'lo_unlink') )     and print "\$dbh->func(lo_unlink) ...... ok\n"     or  print
"\$dbh->func(lo_unlink)...... not ok\n";  ( $pgin cmp $buf and $pgin cmp $blob )     and print "compare blobs
..............not ok\n"     or  print "compare blobs .............. ok\n"; 
 
+ my $fd;
+ ( $fd=$dbh->func( 'getfd') )
+     and print "\$dbh->func(getfd) .......... ok\n"
+     or  print "\$dbh->func(getfd) .......... not ok\n";
+ 
+ ( $dbh->do( 'LISTEN test ') )
+     and print "\$dbh->do('LISTEN test') .... ok\n"
+     or  print "\$dbh->do('LISTEN test') .... not ok\n";
+ 
+ ( $dbh1 = DBI->connect("$dsn_test", '', '', { AutoCommit => 1 }) )
+     and print "DBI->connect (for notify)... ok\n"
+     or  die   "DBI->connect (for notify)... not ok: ", $DBI::errstr;
+ 
+ # there should be no data for read on $fd , until we send a notify
+    
+     my $rout;
+     my $rin = '';
+     vec($rin,$fd,1) = 1;
+     my $nfound = select( $rout=$rin, undef, undef, 0);
+ 
+ ( $nfound==0 ) 
+     and print "select(\$fd) returns no data. ok\n"
+     or  die   "select(\$fd) returns no data. not ok\n";
+ 
+ ( $dbh1->do( 'NOTIFY test ') )
+     and print "\$dbh1->do('NOTIFY test') ... ok\n"
+     or  print "\$dbh1->do('NOTIFY test') ... not ok\n";
+ 
+     my $nfound2 = select( $rout=$rin, undef, undef, 1);
+ 
+ ( $nfound2==1 ) 
+     and print "select(\$fd) returns data.... ok\n"
+     or  die   "select(\$fd) returns data.... not ok\n";
+ 
+ my $notify_r;
+ 
+ ( $notify_r = $dbh->func('notifies') ) 
+     and print "\$dbh->func('notifies')...... ok\n"
+     or  die   "\$dbh->func('notifies')...... not ok\n";
+  ######################### disconnect and drop test database  # disconnect
***************
*** 463,468 ****
--- 503,512 ----     and print "\$dbh->disconnect ........... ok\n"     or  die   "\$dbh->disconnect ........... not
ok:", $DBI::errstr; 
 
+ ( $dbh1->disconnect )
+     and print "\$dbh1->disconnect .......... ok\n"
+     or  die   "\$dbh1->disconnect .......... not ok: ", $DBI::errstr;
+  $dbh0->do("DROP DATABASE $dbtest"); $dbh0->disconnect;  


-----Original Message-----
From: Bruce Momjian [mailto:pgman@candle.pha.pa.us]
Sent: Wednesday, January 09, 2002 7:29 PM
To: Ben Feinstein
Cc: pgsql-interfaces@postgresql.org; alex@pilosoft.com; E.Mergl@bawue.de
Subject: Re: [INTERFACES] [PATCH] notify support for DBD::Pg


Benjamin.Feinstein@guardent.com wrote:
> Hey ya'll,
> 
> Attached is a patch to add support for asynchronous client notifications
to
> the DBD::Pg interface. The original patch was written by Alex Pilosov
> <alex@pilosoft.com> and sent to this list on 10 Jun 2001. This is an
updated
> version that fixes some problems I encountered when running Alex's patch
> against DBD-Pg-1.01. Alex's original patch was generated from a diff of
> DBD-Pg-1.00.

We really need a context diff, diff -c.

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
853-3000+  If your life is a hard drive,     |  830 Blythe Avenue +  Christ can be your backup.        |  Drexel Hill,
Pennsylvania19026