From a5d7b2ede2b89667c56a516883621d6677f65eb0 Mon Sep 17 00:00:00 2001
From: Vignesh <vignesh21@gmail.com>
Date: Fri, 17 Jan 2025 11:11:02 +0530
Subject: [PATCH v53 1/5] Fix incorrect column index when describing
 publication in PG17

When using the psql client to describe a publication on a PG17 server,
an issue arose due to the use of an incorrect column index when fetching
results. This occurred because the newly added "Generated columns" was
placed before the existing "Via Root" column, instead of being added as
the last column. The logic for fetching results did not account for the
absence of the "Generated columns" column in PG17, leading to the "Via Root"
column referencing the wrong index.  The issue has been resolved by updating
the logic to correctly handle column indexing. Specifically, variables were
introduced to store and correctly reference the column index when fetching
results.
---
 src/bin/psql/describe.c | 67 +++++++++++++++++++++++++++++------------
 1 file changed, 47 insertions(+), 20 deletions(-)

diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 2ef99971ac..ab49aa7a61 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -6468,6 +6468,17 @@ describePublications(const char *pattern)
 	bool		has_pubtruncate;
 	bool		has_pubgencols;
 	bool		has_pubviaroot;
+	int			puboid_col = -1,	/* column indexes in "res" */
+				pubname_col = -1,
+				pubowner_col = -1,
+				puballtables_col = -1,
+				pubins_col = -1,
+				pubupd_col = -1,
+				pubdel_col = -1,
+				pubtrunc_col = -1,
+				pubgen_col = -1,
+				pubviaroot_col = -1;
+	int			ncols = 0;
 
 	PQExpBufferData title;
 	printTableContent cont;
@@ -6492,15 +6503,34 @@ describePublications(const char *pattern)
 					  "SELECT oid, pubname,\n"
 					  "  pg_catalog.pg_get_userbyid(pubowner) AS owner,\n"
 					  "  puballtables, pubinsert, pubupdate, pubdelete");
+	puboid_col = ncols++;
+	pubname_col = ncols++;
+	pubowner_col = ncols++;
+	puballtables_col = ncols++;
+	pubins_col = ncols++;
+	pubupd_col = ncols++;
+	pubdel_col = ncols++;
+
 	if (has_pubtruncate)
+	{
 		appendPQExpBufferStr(&buf,
 							 ", pubtruncate");
+		pubtrunc_col = ncols++;
+	}
+
 	if (has_pubgencols)
+	{
 		appendPQExpBufferStr(&buf,
 							 ", pubgencols");
+		pubgen_col = ncols++;
+	}
+
 	if (has_pubviaroot)
+	{
 		appendPQExpBufferStr(&buf,
 							 ", pubviaroot");
+		pubviaroot_col = ncols++;
+	}
 
 	appendPQExpBufferStr(&buf,
 						 "\nFROM pg_catalog.pg_publication\n");
@@ -6542,23 +6572,20 @@ describePublications(const char *pattern)
 	for (i = 0; i < PQntuples(res); i++)
 	{
 		const char	align = 'l';
-		int			ncols = 5;
 		int			nrows = 1;
-		char	   *pubid = PQgetvalue(res, i, 0);
-		char	   *pubname = PQgetvalue(res, i, 1);
-		bool		puballtables = strcmp(PQgetvalue(res, i, 3), "t") == 0;
+		char	   *pubid = PQgetvalue(res, i, puboid_col);
+		char	   *pubname = PQgetvalue(res, i, pubname_col);
+		bool		puballtables = strcmp(PQgetvalue(res, i, puballtables_col), "t") == 0;
 		printTableOpt myopt = pset.popt.topt;
 
-		if (has_pubtruncate)
-			ncols++;
-		if (has_pubgencols)
-			ncols++;
-		if (has_pubviaroot)
-			ncols++;
-
 		initPQExpBuffer(&title);
 		printfPQExpBuffer(&title, _("Publication %s"), pubname);
-		printTableInit(&cont, &myopt, title.data, ncols, nrows);
+
+		/*
+		 * The table will be initialized with (ncols - 2) columns excluding
+		 * 'pubid' and 'pubname'.
+		 */
+		printTableInit(&cont, &myopt, title.data, ncols - 2, nrows);
 
 		printTableAddHeader(&cont, gettext_noop("Owner"), true, align);
 		printTableAddHeader(&cont, gettext_noop("All tables"), true, align);
@@ -6572,17 +6599,17 @@ describePublications(const char *pattern)
 		if (has_pubviaroot)
 			printTableAddHeader(&cont, gettext_noop("Via root"), true, align);
 
-		printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
-		printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
-		printTableAddCell(&cont, PQgetvalue(res, i, 4), false, false);
-		printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false);
-		printTableAddCell(&cont, PQgetvalue(res, i, 6), false, false);
+		printTableAddCell(&cont, PQgetvalue(res, i, pubowner_col), false, false);
+		printTableAddCell(&cont, PQgetvalue(res, i, puballtables_col), false, false);
+		printTableAddCell(&cont, PQgetvalue(res, i, pubins_col), false, false);
+		printTableAddCell(&cont, PQgetvalue(res, i, pubupd_col), false, false);
+		printTableAddCell(&cont, PQgetvalue(res, i, pubdel_col), false, false);
 		if (has_pubtruncate)
-			printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
+			printTableAddCell(&cont, PQgetvalue(res, i, pubtrunc_col), false, false);
 		if (has_pubgencols)
-			printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
+			printTableAddCell(&cont, PQgetvalue(res, i, pubgen_col), false, false);
 		if (has_pubviaroot)
-			printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
+			printTableAddCell(&cont, PQgetvalue(res, i, pubviaroot_col), false, false);
 
 		if (!puballtables)
 		{
-- 
2.43.0

