pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Platon Pronko <platon7pronko@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Subject: very long record lines in expanded psql output
Date: Thu, 5 Aug 2021 13:36:45 +0300
Message-ID: <f03d38a3-db96-a56e-d1bc-dbbc80bbde4d@gmail.com> (raw)

In expanded mode psql calculates the width of the longest field in all the output rows,
and prints the header line according to it. This often results in record header wrapping
over several lines (see example below).

This huge record header is printed the same way before each record, even if all the fields
in current record are small and fit into terminal width. This often leads to situations
when record header occupies the entirety of the screen, for all records, even though there are
only a few records with huge fields in a record set.

Maybe we can avoid making the header line longer than terminal width for \pset border 0
and \pset border 1? We already have terminal width calculated. Please see attached a patch
with the proposed implementation.

Example output before the modification, in a terminal with a 100-column width:

$ psql template1 -c "\x on" -c "\pset border 1;" -c "select n, repeat('x', n) as long_column_name from unnest(array[42,210]) as n"
Expanded display is on.
Border style is 1.
─[ RECORD 1 ]────┬──────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────
─────────────────────────────
n                │ 42
long_column_name │ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
─[ RECORD 2 ]────┼──────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────
─────────────────────────────
n                │ 210
long_column_name │ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

And here's the same command after the patch:

$ psql template1 -c "\x on" -c "\pset border 1;" -c "select n, repeat('x', n) as long_column_name from unnest(array[42,210]) as n"
Expanded display is on.
Border style is 1.
─[ RECORD 1 ]────┬──────────────────────────────────────────────────────────────────────────────────
n                │ 42
long_column_name │ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
─[ RECORD 2 ]────┼──────────────────────────────────────────────────────────────────────────────────
n                │ 210
long_column_name │ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Best regards,
Platon Pronko

Attachments:

fix-header-lines-in-expanded-output.patch (text/x-patch, 1.7K) download diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c index d48fcc4a03..9483d0d852 100644 --- a/src/fe_utils/print.c +++ b/src/fe_utils/print.c @@ -1157,6 +1157,7 @@ print_aligned_vertical_line(const printTextFormat *format, unsigned long record, unsigned int hwidth, unsigned int dwidth, + int output_columns, printTextRule pos, FILE *fout) { @@ -1200,6 +1201,14 @@ print_aligned_vertical_line(const printTextFormat *format, } if (reclen < 0) reclen = 0; + + if (output_columns > 0) { + if (opt_border == 0) + dwidth = Min(dwidth, output_columns - hwidth); + if (opt_border == 1) + dwidth = Min(dwidth, output_columns - hwidth - 3); + } + for (i = reclen; i < dwidth; i++) fputs(opt_border > 0 ? lformat->hrule : " ", fout); if (opt_border == 2) @@ -1501,10 +1510,11 @@ print_aligned_vertical(const printTableContent *cont, if (!opt_tuples_only) print_aligned_vertical_line(format, opt_border, record++, - lhwidth, dwidth, pos, fout); + lhwidth, dwidth, output_columns, + pos, fout); else if (i != 0 || !cont->opt->start_table || opt_border == 2) print_aligned_vertical_line(format, opt_border, 0, lhwidth, - dwidth, pos, fout); + dwidth, output_columns, pos, fout); } /* Format the header */ @@ -1691,7 +1701,7 @@ print_aligned_vertical(const printTableContent *cont, { if (opt_border == 2 && !cancel_pressed) print_aligned_vertical_line(format, opt_border, 0, hwidth, dwidth, - PRINT_RULE_BOTTOM, fout); + output_columns, PRINT_RULE_BOTTOM, fout); /* print footers */ if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)

view thread (39+ messages)  latest in thread

Message-ID: <f03d38a3-db96-a56e-d1bc-dbbc80bbde4d@gmail.com>
Permalink:  ../f03d38a3-db96-a56e-d1bc-dbbc80bbde4d@gmail.com/
Also on:    postgresql.org/message-id/f03d38a3-db96-a56e-d1bc-dbbc80bbde4d@gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: platon7pronko@gmail.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: very long record lines in expanded psql output
  In-Reply-To: <f03d38a3-db96-a56e-d1bc-dbbc80bbde4d@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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