(Mis?)Behavior of \copy with -f and \i

Lists: pgsql-hackers
From: Mark Feit <mfeit+postgresql(at)notonthe(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: (Mis?)Behavior of \copy with -f and \i
Date: 2003-12-23 16:12:26
Message-ID: 16360.26986.318568.851006@gargle.gargle.HOWL
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Greetings...

I've run into a problem with the way \copy behaves when psql is
reading its input from a file using either the -f command line option
or the \i command. (Not that it matters in this case, but this is
PostgreSQL 7.4 on i686-pc-linux-gnu, compiled with gcc.)

The following...

---8<--SNIP---
CREATE TABLE junk (
abbrev CHAR(1),
name VARCHAR(80)
);

\copy junk (abbrev, name) FROM STDIN WITH DELIMITER '|' NULL ''
F|Foo
B|Bar
Z|Baz
\.
---8<--SNIP---

...works just fine when keyed directly into psql or is redirected from
a file (i.e., psql < junk.sql). When using -f or \i, psql sits and
waits for data and an EOF on the standard input and then proceeds to
interpret the next four lines as commands.

This was discussed several years ago in this thread:

http://archives.postgresql.org/pgsql-hackers/2000-01/msg00361.php

Peter Eisentraut declared that from that point on, stdin would be
whatever stream the \copy command came from. I'd like to propose a
variant on the "FROM" clause which makes good on Peter's declaration
without breaking anything already using FROM STDIN and expecting it to
really read from stdin. (I think this is for the better because there
are lots of good uses for "psql -f foo.sql < foo.dat".)

I'd be more than happy to write and test a patch if folks think this
would be a good thing. I'm leaning toward "FROM -" as the syntax but
am open to other ideas (i.e., "FROM HERE" or "FROM INPUT").

- Mark

P.S.: I've been using Postgre(s(95)?|SQL) in its various forms for
close to a decade, and what was a stable platform for lightweight
storage has matured into something I'd pour a few million rows into
without thinking twice about it. Thanks to everyone for all the great
work!


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Mark Feit <mfeit+postgresql(at)notonthe(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: (Mis?)Behavior of \copy with -f and \i
Date: 2004-01-04 18:07:11
Message-ID: 200401041907.11326.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Mark Feit wrote:
> Peter Eisentraut declared that from that point on, stdin would be
> whatever stream the \copy command came from. I'd like to propose a
> variant on the "FROM" clause which makes good on Peter's declaration
> without breaking anything already using FROM STDIN and expecting it
> to really read from stdin. (I think this is for the better because
> there are lots of good uses for "psql -f foo.sql < foo.dat".)
>
> I'd be more than happy to write and test a patch if folks think this
> would be a good thing. I'm leaning toward "FROM -" as the syntax but
> am open to other ideas (i.e., "FROM HERE" or "FROM INPUT").

I'm not sure about the proposed syntax, but the feature sounds quite
reasonable.


From: Mark Feit <mfeit+postgresql(at)notonthe(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>
Subject: Re: (Mis?)Behavior of \copy with -f and \i
Date: 2004-01-05 02:22:26
Message-ID: 16376.51810.33992.248263@gargle.gargle.HOWL
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut writes:
>
> [Patch to make psql's \copy read from the current input, not just
> the standard input or a file.]
>
> I'm not sure about the proposed syntax, but the feature sounds quite
> reasonable.

I have a patch written for this that uses the "-" syntax:

\copy junk (abbrev, name) FROM - WITH DELIMITER '|' NULL ''

I considered a half-dozen or so different ways to go about it, and
this one fits in without breaking anything that already exists. The
hyphen seems like the best choice to go with STDIN and STDOUT since
there are already other programs where it implies the default input
stream and it's not something likely to be found alone as a filename.
If you find something you like better, changing it to something else
is a matter of tweaking a string in copy.c.

The patch will be posted to pgsql-patches as soon as I've had a chance
to torture it some more.

- Mark
--
"Debugging is twice as hard as writing the code in the first
place. Therefore, if you write the code as cleverly as possible, you
are, by definition, not smart enough to debug it." -- Brian Kernighan