]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
psql: Fix expanded aligned output
authorMichael Paquier <michael@paquier.xyz>
Mon, 8 Jun 2026 05:38:01 +0000 (14:38 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 8 Jun 2026 05:38:01 +0000 (14:38 +0900)
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 <pavel.stehule@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/CAFj8pRCzGpsr9zTHbtTd4mGh2YPJqOEgLgt8JLiopuYA9_1xGw@mail.gmail.com
Backpatch-through: 14

src/fe_utils/print.c
src/test/regress/expected/psql.out
src/test/regress/sql/psql.sql

index 2d0f78b8a2d5982a97268638e197c0d7f1deefd7..15ea8fbc2d74cf4a82869784f2a88be924cafe59 100644 (file)
@@ -1346,9 +1346,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,
@@ -1420,7 +1421,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;
 
index 1cd4fea70c512d2896b4659a8d362c8ad4ef2348..d92b671b71afe18ca80b7a662fb4fae749e8c7ab 100644 (file)
@@ -2674,6 +2674,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)
index c24438e02dcdcc830d7d343ea2f3baa5acceb9b8..0cc96aacda041746a2e53dca2eddc590eae5ff9c 100644 (file)
@@ -416,6 +416,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