Re: psql NUL record and field separator

From: Abhijit Menon-Sen <ams(at)toroid(dot)org>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: psql NUL record and field separator
Date: 2012-01-26 13:30:26
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

At 2012-01-14 14:23:49 +0200, peter_e(at)gmx(dot)net wrote:
> Inspired by this question 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(at)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

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


> 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 Content-Type Size
petere-zero-fix.diff text/x-diff 766 bytes
petere-zero-lastrecord.diff text/x-diff 486 bytes

In response to


Browse pgsql-hackers by date

  From Date Subject
Next Message Magnus Hagander 2012-01-26 13:44:17 Re: PATCH: tracking temp files in pg_stat_database
Previous Message Robert Haas 2012-01-26 13:23:34 Re: Second thoughts on CheckIndexCompatible() vs. operator families