From 633b8ac3ce5d021fb31b16fa76744992bafcd2be Mon Sep 17 00:00:00 2001 From: "M.Atif Ceylan" 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)