On 2023-08-30 We 21:29, Thomas Munro wrote:
On Thu, Aug 31, 2023 at 10:32 AM Andrew Dunstan<and...@dunslane.net>  wrote:
#!/usr/bin/perl

use strict; use warnings;

use FFI::Platypus;

my $ffi = FFI::Platypus->new(api=>1);
$ffi->lib("inst/lib/libpq.so");


$ffi->type('opaque' => 'PGconn');
$ffi->attach(PQconnectdb => [ 'string' ] => 'PGconn');
$ffi->attach(PQfinish => [ 'PGconn' ] => 'void');

$ffi->type('opaque' => 'PGresult');
$ffi->attach(PQexec => [ 'PGconn', 'string' ] => 'PGresult');
$ffi->attach(PQgetvalue => [ 'PGresult', 'int', 'int' ] => 'string');

my $pgconn = PQconnectdb("dbname=postgres host=/tmp");
my $res = PQexec($pgconn, "select count(*) from pg_class");
my $count = PQgetvalue( $res, 0, 0);

print "count: $count\n";

PQfinish($pgconn);
It looks very promising so far.  How hard would it be for us to add
this dependency?  Mostly pinging build farm owners?

I'm still on the fence, but the more I know about IPC::Run, the better
the various let's-connect-directly-from-Perl options sound...


Here's some progress. I have put it all in a perl module, which I have tested on Windows (both mingw and MSVC) as well as Ubuntu. I think this is probably something worth having in itself. I wrapped a substantial portion of libpq, but left out things to do with large objects, async processing, pipelining, SSL and some other things. We can fill in the gaps in due course.

The test program now looks like this:

   use strict;
   use warnings;

   use lib ".";
   use PqFFI;

   PqFFI::setup("inst/lib");

   my $conn = PQconnectdb("dbname=postgres host=/tmp");
   my $res = PQexec($conn, 'select count(*) from pg_class');
   my $count = PQgetvalue($res,0,0);
   print "$count rows in pg_class\n";
   PQfinish($conn);

I guess the next thing would be to test it on a few more platforms and also to see if we need to expand the coverage of libpq for the intended uses.

I confess I'm a little reluctant to impose this burden on buildfarm owners. We should think about some sort of fallback in case this isn't supported on some platform, either due to technological barriers or buildfarm owner reluctance.


cheers


andrew


--
Andrew Dunstan
EDB:https://www.enterprisedb.com

Attachment: PqFFI.pm
Description: Perl program

Reply via email to