I am trying to write a Perl script to bring up a Postgres client once and send several files through the client, captures the output separately for each file.
If I do something like:
system ("cat $query1.sql | psql -p 2070 super &> $HOME/Results1.txt");
system ("cat $query2.sql | psql -p 2070 super &> $HOME/Results2.txt");
then Perl will start up the client for each query. As I'll be running hundreds and maybe thousands of queries I want to skip the overhead for starting up all but the first client.
I think I should be able to bring up the Postgres client via Open2, but it hangs when I try. I'm doing this on SUSE Linux machine with Perl 5.10.0.
Here's my code:
use IPC::Open2;
use IO::Handle;
our $pid = open2(*CHILDOUT, *CHILDINT, '../installdir/bin/psql -p 2070 super');
print STDOUT $pid;
print CHILDINT "cat $dumpR5KScript";
print STDOUT 'Sent the commands';
$output = <CHILDOUT>;
close(CHILDIN);
close(CHILDOUT);
It appears to be hanging with "open2" because I never see the pid.
Can someone point out what I am doing wrong so my call to open2 doesn't hang?
And if anyone has advice on the larger issue of the best way of bringing up a Postgres client and running queries through it I would be grateful.