Skip site navigation (1) Skip section navigation (2)

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
Message-ID: 20120126133026.GA30769@toroid.org (view raw or flat)
Thread:
Lists: pgsql-hackers
At 2012-01-14 14:23:49 +0200, peter_e(at)gmx(dot)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(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
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: petere-zero-lastrecord.diff
Description: text/x-diff (486 bytes)
Attachment: petere-zero-fix.diff
Description: text/x-diff (766 bytes)

In response to

Responses

pgsql-hackers by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group