pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Is it possible to set end-of-data marker for COPY statement.
8+ messages / 5 participants
[nested] [flat]

* Is it possible to set end-of-data marker for COPY statement.
@ 2020-09-01 06:14  Junfeng Yang <yjerome@vmware.com>
  0 siblings, 3 replies; 8+ messages in thread

From: Junfeng Yang @ 2020-09-01 06:14 UTC (permalink / raw)
  To: pgsql-hackers@postgresql.org <pgsql-hackers@lists.postgresql.org>

Hi hackers,

As described in the doc https://www.postgresql.org/docs/current/sql-copy.html, the TEXT format recognizes
backslash-period (\.) as end-of-data marker.

The example below will raise an error for the line contains `\.`.

CREATE TABLE test (
id int,
name text,
dep text
)

Data in file "/tmp/data".

122,as\.d,adad
133,sa dad,adadad

Then execute

copy test from '/tmp/data' DELIMITER ',';

An end-of-copy marker corrupt error will be raised.

This requires users to escape the end-of-data marker manually in their data.
Why we don't have a mechanism to define other characters as end-of-data marker?
Or there are other ways to avoid escape the end-of-data in data?

Regards,
Junfeng




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Is it possible to set end-of-data marker for COPY statement.
@ 2020-09-01 09:06  Laurenz Albe <laurenz.albe@cybertec.at>
  parent: Junfeng Yang <yjerome@vmware.com>
  2 siblings, 1 reply; 8+ messages in thread

From: Laurenz Albe @ 2020-09-01 09:06 UTC (permalink / raw)
  To: Junfeng Yang <yjerome@vmware.com>; pgsql-general

[redirected from -hackers]
On Tue, 2020-09-01 at 06:14 +0000, Junfeng Yang wrote:
> As described in the doc https://www.postgresql.org/docs/current/sql-copy.html, the TEXT format recognizes
> backslash-period (\.) as end-of-data marker.
> 
> The example below will raise an error for the line contains `\.`.
> CREATE TABLE test (
> id int,
> name text,
> dep text
> )
>
> Data in file "/tmp/data".
>
> 122,as\.d,adad
> 133,sa dad,adadad
> Then execute
> copy test from '/tmp/data' DELIMITER ',';
> An end-of-copy marker corrupt error will be raised.
> 
> This requires users to escape the end-of-data marker manually in their data.
> Why we don't have a mechanism to define other characters as end-of-data marker?
> Or there are other ways to avoid escape the end-of-data in data?

Your problem is that the file contains bad data.

You are using the default TEXT format of copy, and backslashes must
be escaped there.

Everything will work as you want if you write the first line correctly like

122,as\\.d,adad

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: 回复: Is it possible to set end-of-data marker for COPY statement.
@ 2020-09-01 09:37  Laurenz Albe <laurenz.albe@cybertec.at>
  parent: Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 0 replies; 8+ messages in thread

From: Laurenz Albe @ 2020-09-01 09:37 UTC (permalink / raw)
  To: Junfeng Yang <yjerome@vmware.com>; pgsql-general

Please, don't top-post on these lists.

On Tue, 2020-09-01 at 09:20 +0000, Junfeng Yang wrote:
> 
> 发件人: Laurenz Albe <laurenz.albe@cybertec.at>
> > On Tue, 2020-09-01 at 06:14 +0000, Junfeng Yang wrote:
> > > As described in the doc , the TEXT format recognizes
> > > backslash-period (\.) as end-of-data marker.
> > >
> > > The example below will raise an error for the line contains `\.`.
> > >
> > > CREATE TABLE test (
> > > id int,
> > > name text,
> > > dep text
> > > )
> > >
> > > Data in file "/tmp/data".
> > >
> > > 122,as\.d,adad
> > > 133,sa dad,adadad
> > > Then execute
> > > copy test from '/tmp/data' DELIMITER ',';
> > > An end-of-copy marker corrupt error will be raised.
> > >
> > > This requires users to escape the end-of-data marker manually in their data.
> > > Why we don't have a mechanism to define other characters as end-of-data marker?
> > > Or there are other ways to avoid escape the end-of-data in data?
> >
> > Your problem is that the file contains bad data.
> >
> > You are using the default TEXT format of copy, and backslashes must
> > be escaped there.
> >
> > Everything will work as you want if you write the first line correctly like
> >
> > 122,as\\.d,adad
>
> I understand `\\.` should work. But this requires users to modify huge data.
> I'm wondering is it possible to change the default end-of-data marker or
> could we implement a method to let users choose their own marker?

I don't see the problem.

If you use the CSV format, your data will work fine the way they are:

  COPY test FROM '/tmp/data' (FORMAT 'csv');

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Is it possible to set end-of-data marker for COPY statement.
@ 2020-09-01 16:05  Bruce Momjian <bruce@momjian.us>
  parent: Junfeng Yang <yjerome@vmware.com>
  2 siblings, 2 replies; 8+ messages in thread

From: Bruce Momjian @ 2020-09-01 16:05 UTC (permalink / raw)
  To: Junfeng Yang <yjerome@vmware.com>; +Cc: pgsql-hackers@postgresql.org <pgsql-hackers@lists.postgresql.org>

On Tue, Sep  1, 2020 at 06:14:45AM +0000, Junfeng Yang wrote:
> Hi hackers,
> 
> As described in the doc https://www.postgresql.org/docs/current/sql-copy.html,
> the TEXT format recognizes
> backslash-period (\.) as end-of-data marker.
> 
> The example below will raise an error for the line contains `\.`.
> 
> CREATE TABLE test (
> id int,
> name text,
> dep text
> )
> 
> Data in file "/tmp/data".
> 
> 122,as\.d,adad
> 133,sa dad,adadad
> 
> Then execute
> 
> copy test from '/tmp/data' DELIMITER ',';
> 
> An end-of-copy marker corrupt error will be raised.
> 
> This requires users to escape the end-of-data marker manually in their data.
> Why we don't have a mechanism to define other characters as end-of-data marker?
> Or there are other ways to avoid escape the end-of-data in data?

This is the first I am hearing of this.  The problem is that the system
can't decide if \. is escaping a delimiter, or the end-of-copy marker. 
I think we need to just disable period as a delimiter.  I don't think
there is enough demand to allow the end-of-data marker to be
configurable.

Interestingly, you can use period as s delimiter if you are copying from
a file that doesn't need an end-of-data marker and you never need to
escape the delimiter, but that seems like too rare a use case to allow
period to be supported as a delimiter.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Is it possible to set end-of-data marker for COPY statement.
@ 2020-09-01 16:30  David G. Johnston <david.g.johnston@gmail.com>
  parent: Bruce Momjian <bruce@momjian.us>
  1 sibling, 0 replies; 8+ messages in thread

From: David G. Johnston @ 2020-09-01 16:30 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; +Cc: Junfeng Yang <yjerome@vmware.com>; pgsql-hackers@postgresql.org <pgsql-hackers@lists.postgresql.org>

On Tue, Sep 1, 2020 at 9:05 AM Bruce Momjian <bruce@momjian.us> wrote:

> On Tue, Sep  1, 2020 at 06:14:45AM +0000, Junfeng Yang wrote:
> > Hi hackers,
> >
>
> > Data in file "/tmp/data".
> >
> > 122,as\.d,adad
> > 133,sa dad,adadad
> >
> > Then execute
> >
> > copy test from '/tmp/data' DELIMITER ',';
> >
> > An end-of-copy marker corrupt error will be raised.
>
> This is the first I am hearing of this.  The problem is that the system
> can't decide if \. is escaping a delimiter, or the end-of-copy marker.
> I think we need to just disable period as a delimiter.  I don't think
> there is enough demand to allow the end-of-data marker to be
> configurable.
>
> Interestingly, you can use period as s delimiter if you are copying from
> a file that doesn't need an end-of-data marker and you never need to
> escape the delimiter, but that seems like too rare a use case to allow
> period to be supported as a delimiter.
>

Something isn't right here because the rules for end-of-copy are explicit
that the \. must appear on a line all by itself.  That isn't the case with
the shown test data.

The system should do one of two things with that input (it seems option 2
is the one we've chosen):

One, see that the character following the backslash is not an action
character and just treat the backslash as data.
Two, complain that the character following the backslash is not a valid
action character.

The system is reporting an error, it's just trying to be helpful and seeing
the period it incorrectly reports that the error has something to do with
the end-of-copy marker when in reality all that can be said is that "a
period in this location is not valid" (unless the command uses DELIMITER
'<period>' at least, in which case the period is now valid and \. means a
literal period since its not alone on a line.)

The only limitation our definition of end-of-copy imposes is that a single
column input file cannot contain a record that is only a period.  It does
not impose a limitation on which delimiters are valid.

David J.

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Is it possible to set end-of-data marker for COPY statement.
@ 2020-09-01 17:23  Bruce Momjian <bruce@momjian.us>
  parent: Bruce Momjian <bruce@momjian.us>
  1 sibling, 0 replies; 8+ messages in thread

From: Bruce Momjian @ 2020-09-01 17:23 UTC (permalink / raw)
  To: Junfeng Yang <yjerome@vmware.com>; +Cc: pgsql-hackers@postgresql.org <pgsql-hackers@lists.postgresql.org>

On Tue, Sep  1, 2020 at 12:05:02PM -0400, Bruce Momjian wrote:
> > copy test from '/tmp/data' DELIMITER ',';
> > 
> > An end-of-copy marker corrupt error will be raised.
> > 
> > This requires users to escape the end-of-data marker manually in their data.
> > Why we don't have a mechanism to define other characters as end-of-data marker?
> > Or there are other ways to avoid escape the end-of-data in data?
> 
> This is the first I am hearing of this.  The problem is that the system
> can't decide if \. is escaping a delimiter, or the end-of-copy marker. 
> I think we need to just disable period as a delimiter.  I don't think
> there is enough demand to allow the end-of-data marker to be
> configurable.
> 
> Interestingly, you can use period as s delimiter if you are copying from
> a file that doesn't need an end-of-data marker and you never need to
> escape the delimiter, but that seems like too rare a use case to allow
> period to be supported as a delimiter.

I am sorry I mis-read this email.  The example uses _comma_ for the
delimiter, rather than period.  Let me reply again.  We already disallow
period for delimiters:

        COPY test TO '/u/postgres/tmp/x' WITH (DELIMITER '.');
        ERROR:  COPY delimiter cannot be "."

        COPY test TO STDOUT WITH (DELIMITER '.');
        ERROR:  COPY delimiter cannot be "."

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Is it possible to set end-of-data marker for COPY statement.
@ 2020-09-01 17:25  Bruce Momjian <bruce@momjian.us>
  parent: Junfeng Yang <yjerome@vmware.com>
  2 siblings, 1 reply; 8+ messages in thread

From: Bruce Momjian @ 2020-09-01 17:25 UTC (permalink / raw)
  To: Junfeng Yang <yjerome@vmware.com>; +Cc: pgsql-hackers@postgresql.org <pgsql-hackers@lists.postgresql.org>

On Tue, Sep  1, 2020 at 06:14:45AM +0000, Junfeng Yang wrote:
> Hi hackers,
> 
> As described in the doc https://www.postgresql.org/docs/current/sql-copy.html,
> the TEXT format recognizes
> backslash-period (\.) as end-of-data marker.
> 
> The example below will raise an error for the line contains `\.`.
> 
> CREATE TABLE test (
> id int,
> name text,
> dep text
> )
> 
> Data in file "/tmp/data".
> 
> 122,as\.d,adad
> 133,sa dad,adadad
> 
> Then execute
> 
> copy test from '/tmp/data' DELIMITER ',';
> 
> An end-of-copy marker corrupt error will be raised.
> 
> This requires users to escape the end-of-data marker manually in their data.
> Why we don't have a mechanism to define other characters as end-of-data marker?
> Or there are other ways to avoid escape the end-of-data in data?

So, you are using comma as the delimiter, but have \. (backslash-period)
as a data value.  You need to double-up backslashes in your input data,
no matter what is after the backslash.  You just happen to hit backslash
period, but other things like \N could cause problems --- literal
backslashes have to be doubled.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Is it possible to set end-of-data marker for COPY statement.
@ 2020-09-01 18:01  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Bruce Momjian <bruce@momjian.us>
  0 siblings, 0 replies; 8+ messages in thread

From: Tom Lane @ 2020-09-01 18:01 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; +Cc: Junfeng Yang <yjerome@vmware.com>; pgsql-hackers@postgresql.org <pgsql-hackers@lists.postgresql.org>

Bruce Momjian <bruce@momjian.us> writes:
> On Tue, Sep  1, 2020 at 06:14:45AM +0000, Junfeng Yang wrote:
>> Data in file "/tmp/data".
>> 
>> 122,as\.d,adad
>> 133,sa dad,adadad

> So, you are using comma as the delimiter, but have \. (backslash-period)
> as a data value.  You need to double-up backslashes in your input data,
> no matter what is after the backslash.  You just happen to hit backslash
> period, but other things like \N could cause problems --- literal
> backslashes have to be doubled.

As mentioned upthread, using CSV format might work better.  In CSV
you aren't necessarily stuck with backslash being treated as an
escape character.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2020-09-01 18:01 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-09-01 06:14 Is it possible to set end-of-data marker for COPY statement. Junfeng Yang <yjerome@vmware.com>
2020-09-01 09:06 ` Laurenz Albe <laurenz.albe@cybertec.at>
2020-09-01 09:37   ` Re: 回复: Is it possible to set end-of-data marker for COPY statement. Laurenz Albe <laurenz.albe@cybertec.at>
2020-09-01 16:05 ` Bruce Momjian <bruce@momjian.us>
2020-09-01 16:30   ` David G. Johnston <david.g.johnston@gmail.com>
2020-09-01 17:23   ` Bruce Momjian <bruce@momjian.us>
2020-09-01 17:25 ` Bruce Momjian <bruce@momjian.us>
2020-09-01 18:01   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox