public inbox for [email protected]  
help / color / mirror / Atom feed
From: M.Atıf Ceylan <[email protected]>
To: [email protected]
Subject: [PATCH] psql: add size-based sorting options (O/o) for tables and indexes
Date: Wed, 26 Nov 2025 10:48:52 +0300
Message-ID: <CA+M9mDThhJmuzN8umoPx2JhhKWkXpKO_1E3+4MCSdqE9SnmCjw@mail.gmail.com> (raw)

Hello,
This patch adds two new meta-command modifiers for \dt(+) and \di(+):

  - O  : sort by total relation size descending
  - o  : sort by total relation size ascending

This makes it easier to identify the largest tables and indexes
without writing custom SQL queries.

Help message (\?) is updated to reflect the new options.

regards,
-- 
M.Atıf Ceylan


Attachments:

  [application/octet-stream] v1-0001-psql-add-size-based-sorting-for-tables-and-indexes.patch (2.7K, ../CA+M9mDThhJmuzN8umoPx2JhhKWkXpKO_1E3+4MCSdqE9SnmCjw@mail.gmail.com/2-v1-0001-psql-add-size-based-sorting-for-tables-and-indexes.patch)
  download | inline diff:
From 633b8ac3ce5d021fb31b16fa76744992bafcd2be Mon Sep 17 00:00:00 2001
From: "M.Atif Ceylan" <[email protected]>
Date: Wed, 26 Nov 2025 00:21:50 +0300
Subject: [PATCH] psql: add size-based sorting options (O/o) for tables and indexes

---
 src/bin/psql/describe.c | 24 +++++++++++++++++++++++-
 src/bin/psql/help.c     |  4 ++--
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 36f24502842..33eb5e799c7 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -4194,7 +4194,29 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
 		return false;
 	}
 
-	appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
+	/*
+	 * Append ORDER BY clause to the query buffer.
+	 *
+	 * If the 'o' or 'O' flags are present in tabtypes (e.g., \dtO+)
+	 * sort the results by relation size.
+	 *
+	 * - 'o': Sort by size ascending.
+	 * - 'O': Sort by size descending.
+	 * - Default: Sort by schema name (1) and relation name (2).
+	 */
+	if (showTables || showIndexes)
+	{
+		if (strchr(tabtypes, 'o') != NULL)
+			appendPQExpBufferStr(&buf, "ORDER BY pg_catalog.pg_table_size(c.oid), 1, 2;");
+		else if (strchr(tabtypes, 'O') != NULL)
+			appendPQExpBufferStr(&buf, "ORDER BY pg_catalog.pg_table_size(c.oid) DESC, 1, 2;");
+		else
+			appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
+	}
+	else
+	{
+		appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
+	}
 
 	res = PSQLexec(buf.data);
 	termPQExpBuffer(&buf);
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index ec0b49b957b..f58b66b2aa0 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -246,7 +246,7 @@ slashUsage(unsigned short int pager)
 	HELP0("  \\dFp[x+] [PATTERN]     list text search parsers\n");
 	HELP0("  \\dFt[x+] [PATTERN]     list text search templates\n");
 	HELP0("  \\dg[Sx+] [PATTERN]     list roles\n");
-	HELP0("  \\di[Sx+] [PATTERN]     list indexes\n");
+	HELP0("  \\di[OoSx+] [PATTERN]   list indexes\n");
 	HELP0("  \\dl[x+]                list large objects, same as \\lo_list\n");
 	HELP0("  \\dL[Sx+] [PATTERN]     list procedural languages\n");
 	HELP0("  \\dm[Sx+] [PATTERN]     list materialized views\n");
@@ -262,7 +262,7 @@ slashUsage(unsigned short int pager)
 	HELP0("  \\dRp[x+] [PATTERN]     list replication publications\n");
 	HELP0("  \\dRs[x+] [PATTERN]     list replication subscriptions\n");
 	HELP0("  \\ds[Sx+] [PATTERN]     list sequences\n");
-	HELP0("  \\dt[Sx+] [PATTERN]     list tables\n");
+	HELP0("  \\dt[OoSx+] [PATTERN]   list tables\n");
 	HELP0("  \\dT[Sx+] [PATTERN]     list data types\n");
 	HELP0("  \\du[Sx+] [PATTERN]     list roles\n");
 	HELP0("  \\dv[Sx+] [PATTERN]     list views\n");
-- 
2.39.5 (Apple Git-154)



view thread (7+ messages)  latest in thread

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: [email protected]
  Cc: [email protected]
  Subject: Re: [PATCH] psql: add size-based sorting options (O/o) for tables and indexes
  In-Reply-To: <CA+M9mDThhJmuzN8umoPx2JhhKWkXpKO_1E3+4MCSdqE9SnmCjw@mail.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 agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox