diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index e103d5b084110582aa7ec9a91c7b519941dc7de0..97f9c2f1690ea67d2579e3b7944e1e60e7586a44 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -1265,7 +1265,7 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
 	/*
 	 * Deal with the pager here instead of in printTable(), because we could
 	 * get here via print_aligned_text() in expanded auto mode, and so we have
-	 * to recalcuate the pager requirement based on vertical output.
+	 * to recalculate the pager requirement based on vertical output.
 	 */
 	IsPagerNeeded(cont, 0, true, &fout, &is_pager);
 
@@ -1400,7 +1400,8 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
 		/* Determine width required for record header lines */
 		if (!opt_tuples_only)
 		{
-			rwidth = 1 + log10(cont->nrows);
+			if (cont->nrows > 0)
+				rwidth = 1 + (int) log10(cont->nrows);
 			if (opt_border == 0)
 				rwidth += 9;	/* "* RECORD " */
 			else if (opt_border == 1)
@@ -1412,33 +1413,46 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
 		/* We might need to do the rest of the calculation twice */
 		for (;;)
 		{
-			unsigned int width,
-						min_width;
+			unsigned int width;
 
 			/* Total width required to not wrap data */
 			width = hwidth + swidth + dwidth;
+			/* ... and not the header lines, either */
+			if (width < rwidth)
+				width = rwidth;
 
-			/* Minimum acceptable width: room for just 3 columns of data */
-			min_width = hwidth + swidth + 3;
-			/* ... but not less than what the record header lines need */
-			if (rwidth > min_width)
-				min_width = rwidth;
-
-			if (width < min_width ||
-				(output_columns > 0 && output_columns < min_width))
-			{
-				/* Set data width to match min_width */
-				newdwidth = min_width - hwidth - swidth;
-			}
-			else if (output_columns > 0)
+			if (output_columns > 0)
 			{
-				/* Set data width to match output_columns */
-				newdwidth = output_columns - hwidth - swidth;
+				unsigned int min_width;
+
+				/* Minimum acceptable width: room for just 3 columns of data */
+				min_width = hwidth + swidth + 3;
+				/* ... but not less than what the record header lines need */
+				if (min_width < rwidth)
+					min_width = rwidth;
+
+				if (output_columns >= width)
+				{
+					/* Plenty of room, use native data width */
+					/* (but at least enough for the record header lines) */
+					newdwidth = width - hwidth - swidth;
+				}
+				else if (output_columns < min_width)
+				{
+					/* Set data width to match min_width */
+					newdwidth = min_width - hwidth - swidth;
+				}
+				else
+				{
+					/* Set data width to match output_columns */
+					newdwidth = output_columns - hwidth - swidth;
+				}
 			}
 			else
 			{
-				/* Use native data width */
-				newdwidth = dwidth;
+				/* Don't know the wrap limit, so use native data width */
+				/* (but at least enough for the record header lines) */
+				newdwidth = width - hwidth - swidth;
 			}
 
 			/*
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index 371cef3cd7d39d6a352b4d862fb950a3fb5ba328..178a8093b7374ce4342a33708c0b0bec6a670653 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -2198,34 +2198,34 @@ execute q;
 
 \pset format wrapped
 execute q;
-* Record 1                             
+* Record 1                          
 0123456789abcdef xx
 0123456789       yyyyyyyyyyyyyyyyyy
-* Record 2                             
+* Record 2                          
 0123456789abcdef xxxx
 0123456789       yyyyyyyyyyyyyyyy
-* Record 3                             
+* Record 3                          
 0123456789abcdef xxxxxx
 0123456789       yyyyyyyyyyyyyy
-* Record 4                             
+* Record 4                          
 0123456789abcdef xxxxxxxx
 0123456789       yyyyyyyyyyyy
-* Record 5                             
+* Record 5                          
 0123456789abcdef xxxxxxxxxx
 0123456789       yyyyyyyyyy
-* Record 6                             
+* Record 6                          
 0123456789abcdef xxxxxxxxxxxx
 0123456789       yyyyyyyy
-* Record 7                             
+* Record 7                          
 0123456789abcdef xxxxxxxxxxxxxx
 0123456789       yyyyyy
-* Record 8                             
+* Record 8                          
 0123456789abcdef xxxxxxxxxxxxxxxx
 0123456789       yyyy
-* Record 9                             
+* Record 9                          
 0123456789abcdef xxxxxxxxxxxxxxxxxx
 0123456789       yy
-* Record 10                            
+* Record 10                         
 0123456789abcdef xxxxxxxxxxxxxxxxxxxx
 0123456789       
 
@@ -2296,34 +2296,34 @@ execute q;
 
 \pset format wrapped
 execute q;
--[ RECORD 1 ]----+----------------------
+-[ RECORD 1 ]----+---------------------
 0123456789abcdef | xx
 0123456789       | yyyyyyyyyyyyyyyyyy
--[ RECORD 2 ]----+----------------------
+-[ RECORD 2 ]----+---------------------
 0123456789abcdef | xxxx
 0123456789       | yyyyyyyyyyyyyyyy
--[ RECORD 3 ]----+----------------------
+-[ RECORD 3 ]----+---------------------
 0123456789abcdef | xxxxxx
 0123456789       | yyyyyyyyyyyyyy
--[ RECORD 4 ]----+----------------------
+-[ RECORD 4 ]----+---------------------
 0123456789abcdef | xxxxxxxx
 0123456789       | yyyyyyyyyyyy
--[ RECORD 5 ]----+----------------------
+-[ RECORD 5 ]----+---------------------
 0123456789abcdef | xxxxxxxxxx
 0123456789       | yyyyyyyyyy
--[ RECORD 6 ]----+----------------------
+-[ RECORD 6 ]----+---------------------
 0123456789abcdef | xxxxxxxxxxxx
 0123456789       | yyyyyyyy
--[ RECORD 7 ]----+----------------------
+-[ RECORD 7 ]----+---------------------
 0123456789abcdef | xxxxxxxxxxxxxx
 0123456789       | yyyyyy
--[ RECORD 8 ]----+----------------------
+-[ RECORD 8 ]----+---------------------
 0123456789abcdef | xxxxxxxxxxxxxxxx
 0123456789       | yyyy
--[ RECORD 9 ]----+----------------------
+-[ RECORD 9 ]----+---------------------
 0123456789abcdef | xxxxxxxxxxxxxxxxxx
 0123456789       | yy
--[ RECORD 10 ]---+----------------------
+-[ RECORD 10 ]---+---------------------
 0123456789abcdef | xxxxxxxxxxxxxxxxxxxx
 0123456789       |