Обсуждение: psql and named pipes

Поиск
Список
Период
Сортировка

psql and named pipes

От
Alvaro Herrera
Дата:
I was under the impression that I could start a "psql -f pipe" and then
feed it commands through the pipe using echo, and expect it to hang from
one command to the next.  Of course, this doesn't work -- my guess is
that echo sends an EOF after the line I send, so psql sees the EOF in
the pipe and terminates.

Does anyone have an idea how to go about this?  I was expecting to be
able to drive two psql sessions in parallel in a shell script -- sort of
poor man's concurrent psql :-(

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


Re: psql and named pipes

От
Aidan Van Dyk
Дата:
* Alvaro Herrera <alvherre@commandprompt.com> [080327 12:58]:
> I was under the impression that I could start a "psql -f pipe" and then
> feed it commands through the pipe using echo, and expect it to hang from
> one command to the next.  Of course, this doesn't work -- my guess is
> that echo sends an EOF after the line I send, so psql sees the EOF in
> the pipe and terminates.
> 
> Does anyone have an idea how to go about this?  I was expecting to be
> able to drive two psql sessions in parallel in a shell script -- sort of
> poor man's concurrent psql :-(

I've had to use:while (true); do cat pipe; done | psql

The trick is that pipes "EOF"s everytime the cleint closes it.  (Not
strictly true, but it appears that way to basic read()ers).

You can see a more compilcated setup I use to echo commands to a pipe
going to a psql here:http://www.highrise.ca/aidan/postgresql/watchsql

-- 
Aidan Van Dyk                                             Create like a god,
aidan@highrise.ca                                       command like a king,
http://www.highrise.ca/                                   work like a slave.

Re: psql and named pipes

От
Alvaro Herrera
Дата:
Aidan Van Dyk wrote:

> I've had to use:
>     while (true); do cat pipe; done | psql
> 
> The trick is that pipes "EOF"s everytime the cleint closes it.  (Not
> strictly true, but it appears that way to basic read()ers).

Ah!  Yeah, I knew that and forgot :-)  It's easier than that actually --
you just need to keep the pipe open in another process.  So I can do
this: first open a terminal with

$ psql -f foo

And then, in another terminal,

$ cat > foo &
[1] 29155

[1]+  Stopped                 cat >foo
$ echo "begin;" > foo
$ echo "create table a (a int);" > foo
$ echo "insert into a values (1);" > foo
$ echo "insert into a values (2);" > foo
$ echo "insert into a values (3);" > foo
$ echo "commit;" > foo
$ echo "select * from a;" > foo
$ kill %1
-bash: echo: write error: Appel système interrompu
[1]+  Complété              cat >foo


And while this is going on, the other terminal shows the output being
produced by psql.

Thanks for the reminder :-)

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


Re: psql and named pipes

От
Aidan Van Dyk
Дата:
* Alvaro Herrera <alvherre@commandprompt.com> [080327 13:51]:
> Ah!  Yeah, I knew that and forgot :-)  It's easier than that actually --
> you just need to keep the pipe open in another process.  So I can do
> this: first open a terminal with
>
> $ psql -f foo
>
> And then, in another terminal,
>
> $ cat > foo &
> [1] 29155
>
> [1]+  Stopped                 cat >foo
> $ echo "begin;" > foo
> $ echo "create table a (a int);" > foo
> $ echo "insert into a values (1);" > foo
> $ echo "insert into a values (2);" > foo
> $ echo "insert into a values (3);" > foo
> $ echo "commit;" > foo
> $ echo "select * from a;" > foo
> $ kill %1
> -bash: echo: write error: Appel système interrompu
> [1]+  Complété              cat >foo
>
>
> And while this is going on, the other terminal shows the output being
> produced by psql.
>
> Thanks for the reminder :-)

And thanks for the "any open writer" trick.  Makes it even easier for me
to keep using named pipes with psql.

--
Aidan Van Dyk                                             Create like a god,
aidan@highrise.ca                                       command like a king,
http://www.highrise.ca/                                   work like a slave.

Re: psql and named pipes

От
Tom Lane
Дата:
Alvaro Herrera <alvherre@commandprompt.com> writes:
> I was under the impression that I could start a "psql -f pipe" and then
> feed it commands through the pipe using echo, and expect it to hang from
> one command to the next.  Of course, this doesn't work -- my guess is
> that echo sends an EOF after the line I send, so psql sees the EOF in
> the pipe and terminates.

Right.  You need some (other?) process holding the write end of the pipe
open continuously until you're done with the session.

There isn't really any such concept as "sending an EOF" here.  The read
side of the pipe reports EOF if there are no processes holding the write
side open.  More data could be sent by a new writer, but of course psql
has no idea about that.
        regards, tom lane