Re: psql NUL record and field separator - Mailing list pgsql-hackers

From Abhijit Menon-Sen
Subject Re: psql NUL record and field separator
Date
Msg-id 20120126133026.GA30769@toroid.org
Whole thread Raw
In response to psql NUL record and field separator  (Peter Eisentraut <peter_e@gmx.net>)
Responses Re: psql NUL record and field separator  (Peter Eisentraut <peter_e@gmx.net>)
List pgsql-hackers
At 2012-01-14 14:23:49 +0200, peter_e@gmx.net wrote:
>
> Inspired by this question http://stackoverflow.com/questions/6857265 I
> have implemented a way to set the psql record and field separators to
> a zero byte (ASCII NUL character).

Since this patch is in the commitfest, I had a look at it.

I agree that the feature is useful. The patch applies and builds cleanly
with HEAD@9f9135d1, but needs a further minor tweak to work (attached).
Without it, both zero separators get overwritten with the default value
after option parsing. The code looks good otherwise.

There's one problem:

> psql --record-separator-zero -At -c 'select something from somewhere' | xargs -0 dosomething

If you run find -print0 and it finds one file, it will still print
"filename\0", and xargs -0 will work fine.

But psql --record-separator-zero -At -c 'select 1' will print "1\n", not
"1\0" or even "1\0\n", so xargs -0 will use the value "1\n", not "1". If
you're doing this in a shell script, handing the last argument specially
would be painful.

At issue are (at least) these three lines from print_unaligned_text in
src/bin/psql/print.c:

 358         /* the last record needs to be concluded with a newline */
 359         if (need_recordsep)
 360             fputc('\n', fout);

Perhaps the right thing to do would be to change this to output \0 if
--record-separator-zero was used (but leave it at \n otherwise)? That
is what my second attached patch does:

$ bin/psql --record-separator-zero --field-separator-zero -At -c 'select 1,2 union select 3,4'|xargs -0 echo
1 2 3 4

Thoughts?

> I think the most common use of this would be to set the record
> separator from the command line, so we could use a short option
> such as -0 or -z for that.

I agree. The current option names are very unwieldy to type.

-- ams

Attachment

pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: Second thoughts on CheckIndexCompatible() vs. operator families
Next
From: Magnus Hagander
Date:
Subject: Re: PATCH: tracking temp files in pg_stat_database