diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index a9b1ed2699d..55aa5f2ac1d 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -482,6 +482,27 @@ PostgreSQL documentation
+
+
+
+
+
+ Set the field separator for unaligned output to a zero byte.
+
+
+
+
+
+
+
+
+
+ Set the record separator for unaligned output to a zero byte. This is
+ useful for interfacing, for example, with xargs -0.
+
+
+
+
@@ -1908,6 +1929,16 @@ lo_import 152801
+
+ fieldsep_zero
+
+
+ Sets the field separator to use in unaligned output format to a zero
+ byte.
+
+
+
+
footer
@@ -2077,6 +2108,16 @@ lo_import 152801
+
+ recordsep_zero
+
+
+ Sets the record separator to use in unaligned output format to a zero
+ byte.
+
+
+
+
tableattr (or T)
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index ab809ec3a02..8421ad00860 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2272,11 +2272,26 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
{
if (value)
{
- free(popt->topt.fieldSep);
- popt->topt.fieldSep = pg_strdup(value);
+ free(popt->topt.fieldSep.separator);
+ popt->topt.fieldSep.separator = pg_strdup(value);
+ popt->topt.fieldSep.separator_zero = false;
}
if (!quiet)
- printf(_("Field separator is \"%s\".\n"), popt->topt.fieldSep);
+ {
+ if (popt->topt.fieldSep.separator_zero)
+ printf(_("Field separator is zero byte.\n"));
+ else
+ printf(_("Field separator is \"%s\".\n"), popt->topt.fieldSep.separator);
+ }
+ }
+
+ else if (strcmp(param, "fieldsep_zero") == 0)
+ {
+ free(popt->topt.fieldSep.separator);
+ popt->topt.fieldSep.separator = NULL;
+ popt->topt.fieldSep.separator_zero = true;
+ if (!quiet)
+ printf(_("Field separator is zero byte.\n"));
}
/* record separator for unaligned text */
@@ -2284,18 +2299,30 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
{
if (value)
{
- free(popt->topt.recordSep);
- popt->topt.recordSep = pg_strdup(value);
+ free(popt->topt.recordSep.separator);
+ popt->topt.recordSep.separator = pg_strdup(value);
+ popt->topt.recordSep.separator_zero = false;
}
if (!quiet)
{
- if (strcmp(popt->topt.recordSep, "\n") == 0)
+ if (popt->topt.recordSep.separator_zero)
+ printf(_("Record separator is zero byte.\n"));
+ else if (strcmp(popt->topt.recordSep.separator, "\n") == 0)
printf(_("Record separator is ."));
else
- printf(_("Record separator is \"%s\".\n"), popt->topt.recordSep);
+ printf(_("Record separator is \"%s\".\n"), popt->topt.recordSep.separator);
}
}
+ else if (strcmp(param, "recordsep_zero") == 0)
+ {
+ free(popt->topt.recordSep.separator);
+ popt->topt.recordSep.separator = NULL;
+ popt->topt.recordSep.separator_zero = true;
+ if (!quiet)
+ printf(_("Record separator is zero byte.\n"));
+ }
+
/* toggle between full and tuples-only format */
else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
{
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 172fd0c733b..eff0ea53b69 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -123,6 +123,10 @@ usage(void)
printf(_(" -t, --tuples-only print rows only\n"));
printf(_(" -T, --table-attr=TEXT set HTML table tag attributes (e.g., width, border)\n"));
printf(_(" -x, --expanded turn on expanded table output\n"));
+ printf(_(" -z, --field-separator-zero\n"
+ " set field separator to zero byte\n"));
+ printf(_(" -0, --record-separator-zero\n"
+ " set record separator to zero byte\n"));
printf(_("\nConnection options:\n"));
/* Display default host */
@@ -237,8 +241,8 @@ slashUsage(unsigned short int pager)
fprintf(output, _(" \\H toggle HTML output mode (currently %s)\n"),
ON(pset.popt.topt.format == PRINT_HTML));
fprintf(output, _(" \\pset NAME [VALUE] set table output option\n"
- " (NAME := {format|border|expanded|fieldsep|footer|null|\n"
- " numericlocale|recordsep|tuples_only|title|tableattr|pager})\n"));
+ " (NAME := {format|border|expanded|fieldsep|fieldsep_zero|footer|null|\n"
+ " numericlocale|recordsep|recordsep_zero|tuples_only|title|tableattr|pager})\n"));
fprintf(output, _(" \\t [on|off] show only rows (currently %s)\n"),
ON(pset.popt.topt.tuples_only));
fprintf(output, _(" \\T [STRING] set HTML
tag attributes, or unset if none\n"));
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index e127edb1aed..dec440c264e 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -268,6 +268,16 @@ fputnbytes(FILE *f, const char *str, size_t n)
}
+static void
+print_separator(struct separator sep, FILE *fout)
+{
+ if (sep.separator_zero)
+ fputc('\000', fout);
+ else if (sep.separator)
+ fputs(sep.separator, fout);
+}
+
+
/*************************/
/* Unaligned text */
/*************************/
@@ -276,8 +286,6 @@ fputnbytes(FILE *f, const char *str, size_t n)
static void
print_unaligned_text(const printTableContent *cont, FILE *fout)
{
- const char *opt_fieldsep = cont->opt->fieldSep;
- const char *opt_recordsep = cont->opt->recordSep;
bool opt_tuples_only = cont->opt->tuples_only;
unsigned int i;
const char *const * ptr;
@@ -286,16 +294,14 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
if (cancel_pressed)
return;
- if (!opt_fieldsep)
- opt_fieldsep = "";
- if (!opt_recordsep)
- opt_recordsep = "";
-
if (cont->opt->start_table)
{
/* print title */
if (!opt_tuples_only && cont->title)
- fprintf(fout, "%s%s", cont->title, opt_recordsep);
+ {
+ fputs(cont->title, fout);
+ print_separator(cont->opt->recordSep, fout);
+ }
/* print headers */
if (!opt_tuples_only)
@@ -303,7 +309,7 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
for (ptr = cont->headers; *ptr; ptr++)
{
if (ptr != cont->headers)
- fputs(opt_fieldsep, fout);
+ print_separator(cont->opt->fieldSep, fout);
fputs(*ptr, fout);
}
need_recordsep = true;
@@ -318,7 +324,7 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
{
if (need_recordsep)
{
- fputs(opt_recordsep, fout);
+ print_separator(cont->opt->recordSep, fout);
need_recordsep = false;
if (cancel_pressed)
break;
@@ -326,7 +332,7 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
fputs(*ptr, fout);
if ((i + 1) % cont->ncolumns)
- fputs(opt_fieldsep, fout);
+ print_separator(cont->opt->fieldSep, fout);
else
need_recordsep = true;
}
@@ -342,16 +348,25 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
{
if (need_recordsep)
{
- fputs(opt_recordsep, fout);
+ print_separator(cont->opt->recordSep, fout);
need_recordsep = false;
}
fputs(f->data, fout);
need_recordsep = true;
}
}
- /* the last record needs to be concluded with a newline */
+ /*
+ * The last record is terminated by a newline, independent of the set
+ * record separator. But when the record separator is a zero byte, we
+ * use that (compatible with find -print0 and xargs).
+ */
if (need_recordsep)
- fputc('\n', fout);
+ {
+ if (cont->opt->recordSep.separator_zero)
+ print_separator(cont->opt->recordSep, fout);
+ else
+ fputc('\n', fout);
+ }
}
}
@@ -359,8 +374,6 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
static void
print_unaligned_vertical(const printTableContent *cont, FILE *fout)
{
- const char *opt_fieldsep = cont->opt->fieldSep;
- const char *opt_recordsep = cont->opt->recordSep;
bool opt_tuples_only = cont->opt->tuples_only;
unsigned int i;
const char *const * ptr;
@@ -369,11 +382,6 @@ print_unaligned_vertical(const printTableContent *cont, FILE *fout)
if (cancel_pressed)
return;
- if (!opt_fieldsep)
- opt_fieldsep = "";
- if (!opt_recordsep)
- opt_recordsep = "";
-
if (cont->opt->start_table)
{
/* print title */
@@ -393,19 +401,19 @@ print_unaligned_vertical(const printTableContent *cont, FILE *fout)
if (need_recordsep)
{
/* record separator is 2 occurrences of recordsep in this mode */
- fputs(opt_recordsep, fout);
- fputs(opt_recordsep, fout);
+ print_separator(cont->opt->recordSep, fout);
+ print_separator(cont->opt->recordSep, fout);
need_recordsep = false;
if (cancel_pressed)
break;
}
fputs(cont->headers[i % cont->ncolumns], fout);
- fputs(opt_fieldsep, fout);
+ print_separator(cont->opt->fieldSep, fout);
fputs(*ptr, fout);
if ((i + 1) % cont->ncolumns)
- fputs(opt_recordsep, fout);
+ print_separator(cont->opt->recordSep, fout);
else
need_recordsep = true;
}
@@ -417,15 +425,19 @@ print_unaligned_vertical(const printTableContent *cont, FILE *fout)
{
printTableFooter *f;
- fputs(opt_recordsep, fout);
+ print_separator(cont->opt->recordSep, fout);
for (f = cont->footers; f; f = f->next)
{
- fputs(opt_recordsep, fout);
+ print_separator(cont->opt->recordSep, fout);
fputs(f->data, fout);
}
}
- fputc('\n', fout);
+ /* see above in print_unaligned_text() */
+ if (cont->opt->recordSep.separator_zero)
+ print_separator(cont->opt->recordSep, fout);
+ else
+ fputc('\n', fout);
}
}
diff --git a/src/bin/psql/print.h b/src/bin/psql/print.h
index 86c6e754abd..931535e478f 100644
--- a/src/bin/psql/print.h
+++ b/src/bin/psql/print.h
@@ -67,6 +67,12 @@ typedef struct printTextFormat
* marks when border=0? */
} printTextFormat;
+struct separator
+{
+ char *separator;
+ bool separator_zero;
+};
+
typedef struct printTableOpt
{
enum printFormat format; /* see enum above */
@@ -81,8 +87,8 @@ typedef struct printTableOpt
bool stop_table; /* print stop decoration, eg
*/
unsigned long prior_records; /* start offset for record counters */
const printTextFormat *line_style; /* line style (NULL for default) */
- char *fieldSep; /* field separator for unaligned text mode */
- char *recordSep; /* record separator for unaligned text mode */
+ struct separator fieldSep; /* field separator for unaligned text mode */
+ struct separator recordSep; /* record separator for unaligned text mode */
bool numericLocale; /* locale-aware numeric units separator and
* decimal marker */
char *tableAttr; /* attributes for HTML