Re: persistant psql feature suggestion

From: PeterKorman <calvin-pgsql-ml(at)eigenvision(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: persistant psql feature suggestion
Date: 2003-06-29 21:08:14
Message-ID: 20030629210814.GA15100@eigenvision.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Jun 29, 2003 at 02:15:26PM -0400, Andrew Dunstan wrote:
> We don't need to read and write on the same fd.
>
> The real right way is to detect when the psql client exits - possible when
> the perl program spawns it, like shown below.
>
> As always, TIMTOWTDI
>
> andrew
>
> --------------------------
>
> use strict;
> use IO::Handle;
> use POSIX ":sys_wait_h";
>
> my $curpos;
> my $fifofile = shift || usage();
> my $database = shift || usage();
>
> open(FILE,$fifofile) || die $!;
> my $psqlpid = open(OUTPIPE,"|-");
> unless (defined($psqlpid)) { die $!; }
> if ($psqlpid == 0)
> {
> exec("psql -a $database") || die $!;
> }
>
> # must be parent here
> sub REAPER
> {
> my $waitedpid;
> while (($waitedpid = waitpid(-1,WNOHANG)) > 0)
> {
> if ($waitedpid == $psqlpid) { exit 0; }
> }
> $SIG{CHLD} = \&REAPER; # loathe sysV
> }
>
> $SIG{CHLD} = \&REAPER;
>
> OUTPIPE->autoflush();
>
> for (;;)
> {
> for ($curpos = tell(FILE); $_ = <FILE>; $curpos = tell(FILE))
> {
> print OUTPIPE $_;
> }
> sleep(1);
> seek(FILE, $curpos, 0);
> }
>
>
> sub usage
> {
> print STDERR "usage: ",$0," fifofile database\n";
> exit 1;
> }

I dropped into autopilot without considering James Pye's
original phrase:

"The ability for psql to remain in the background reading
from stdin and writing to stdout until explicitly killed"

I went straight to thinking about handling the psql output
which matched /ERROR/ differently from other output.

I like your solution. But I think I'd code it:

my $psqlpid = open(OUTPIPE,"|-") || die qq(cant fork: $!);

Sometimes I'm too stupid to remember that that perl gives
you the fork (the knife and the kitchen sink) even if you
dont realize you asked for it;-)

Cheers,

JPK

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2003-06-29 21:18:10 Re: [HACKERS] PlPython
Previous Message Joe Conway 2003-06-29 20:04:42 Re: [HACKERS] Missing array support