From: Michael Paquier Date: Mon, 8 Jun 2026 05:37:51 +0000 (+0900) Subject: psql: Fix expanded aligned output X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3d0d6741d810fbab894905b4945f4dc4e827c4ff;p=thirdparty%2Fpostgresql.git psql: Fix expanded aligned output When a table's columns are narrower than the record header line, the expanded aligned format produced misaligned output because the data column width was not adjusted to match the record header width, leading to output like: +-[ RECORD 1 ]-+ | a | 10 | | b | 20 | +---+----+ This commit adjusts the output so as the column width match with the header line, giving: +-[ RECORD 1 ]-+ | a | 10 | | b | 20 | +---+----------+ Author: Pavel Stehule Reviewed-by: Chao Li Discussion: https://postgr.es/m/CAFj8pRCzGpsr9zTHbtTd4mGh2YPJqOEgLgt8JLiopuYA9_1xGw@mail.gmail.com Backpatch-through: 14 --- diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c index f2dd52003c1..bfbaf094d7e 100644 --- a/src/fe_utils/print.c +++ b/src/fe_utils/print.c @@ -1443,9 +1443,10 @@ print_aligned_vertical(const printTableContent *cont, } /* - * Calculate available width for data in wrapped mode + * Determine data column width: fit output width in wrapped mode, or + * ensure alignment with the record header line in aligned mode. */ - if (cont->opt->format == PRINT_WRAPPED) + if (cont->opt->format == PRINT_WRAPPED || cont->opt->format == PRINT_ALIGNED) { unsigned int swidth, rwidth = 0, @@ -1517,7 +1518,7 @@ print_aligned_vertical(const printTableContent *cont, if (width < rwidth) width = rwidth; - if (output_columns > 0) + if (cont->opt->format == PRINT_WRAPPED && output_columns > 0) { unsigned int min_width; diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out index c8f3932edf0..42635a56a06 100644 --- a/src/test/regress/expected/psql.out +++ b/src/test/regress/expected/psql.out @@ -2854,6 +2854,32 @@ execute q; +------------------+-------------------+ deallocate q; +-- expanded output with short-width columns +\pset border 2 +\pset expanded on +create table psql_short_tab(a int, b int); +insert into psql_short_tab values(10,20),(30,40); +\pset format aligned +select * from psql_short_tab; ++-[ RECORD 1 ]-+ +| a | 10 | +| b | 20 | ++-[ RECORD 2 ]-+ +| a | 30 | +| b | 40 | ++---+----------+ + +\pset format wrapped +select * from psql_short_tab; ++-[ RECORD 1 ]-+ +| a | 10 | +| b | 20 | ++-[ RECORD 2 ]-+ +| a | 30 | +| b | 40 | ++---+----------+ + +drop table psql_short_tab; \pset linestyle ascii \pset border 1 -- support table for output-format tests (useful to create a footer) diff --git a/src/test/regress/sql/psql.sql b/src/test/regress/sql/psql.sql index dcdbd4fc020..e51767b3e2f 100644 --- a/src/test/regress/sql/psql.sql +++ b/src/test/regress/sql/psql.sql @@ -499,6 +499,17 @@ execute q; deallocate q; +-- expanded output with short-width columns +\pset border 2 +\pset expanded on +create table psql_short_tab(a int, b int); +insert into psql_short_tab values(10,20),(30,40); +\pset format aligned +select * from psql_short_tab; +\pset format wrapped +select * from psql_short_tab; +drop table psql_short_tab; + \pset linestyle ascii \pset border 1