Hi

2017-08-15 4:37 GMT+02:00 Peter Eisentraut <peter.eisentr...@2ndquadrant.com
>:

> On 3/11/17 07:06, Pavel Stehule wrote:
> > I am sending a updated version with separated sort direction in special
> > variable
>
> This patch also needs a rebase.
>

I am sending rebased patch

Regards

Pavel


>
> --
> Peter Eisentraut              http://www.2ndQuadrant.com/
> PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
>
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index c592edac60..9bc391cb39 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -3719,6 +3719,27 @@ bar
       </varlistentry>
 
       <varlistentry>
+        <term><varname>VERBOSE_SORT_COLUMNS</varname></term>
+        <listitem>
+        <para>
+        This variable can be set to the values <literal>schema_name</>,
+        <literal>name_schema</> or <literal>size</> to control the 
+        order of content of decrible command.
+        </para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><varname>VERBOSE_SORT_DIRECTION</varname></term>
+        <listitem>
+        <para>
+        This variable can be set to the values <literal>asc</>,
+        or <literal>desc</> to control the order of content of decrible command.
+        </para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><varname>VERBOSITY</varname></term>
         <listitem>
         <para>
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index f6049cc9e5..1b2346d38b 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -200,6 +200,8 @@ describeAccessMethods(const char *pattern, bool verbose)
 	return true;
 }
 
+#define SORT_DIRECTION_STR(v)		((v) == PSQL_SORT_ASC ? "ASC" : "DESC")
+
 /*
  * \db
  * Takes an optional regexp to select particular tablespaces
@@ -268,7 +270,18 @@ describeTablespaces(const char *pattern, bool verbose)
 						  NULL, "spcname", NULL,
 						  NULL);
 
-	appendPQExpBufferStr(&buf, "ORDER BY 1;");
+
+	if (verbose && pset.sversion >= 90200)
+	{
+		if (pset.verbose_sort_columns == PSQL_SORT_SIZE)
+			appendPQExpBuffer(&buf,
+							  "ORDER BY pg_catalog.pg_tablespace_size(oid) %s, 1;",
+							  SORT_DIRECTION_STR(pset.verbose_sort_direction));
+		else
+			appendPQExpBufferStr(&buf, "ORDER BY 1;");
+	}
+	else
+		appendPQExpBufferStr(&buf, "ORDER BY 1;");
 
 	res = PSQLexec(buf.data);
 	termPQExpBuffer(&buf);
@@ -830,7 +843,19 @@ listAllDbs(const char *pattern, bool verbose)
 		processSQLNamePattern(pset.db, &buf, pattern, false, false,
 							  NULL, "d.datname", NULL, NULL);
 
-	appendPQExpBufferStr(&buf, "ORDER BY 1;");
+	if (verbose && pset.sversion >= 80200)
+	{
+		if (pset.verbose_sort_columns == PSQL_SORT_SIZE)
+			appendPQExpBuffer(&buf,
+						  "ORDER BY CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
+						  "              THEN pg_catalog.pg_database_size(d.datname) END %s, 1;\n",
+						  SORT_DIRECTION_STR(pset.verbose_sort_direction));
+		else
+			appendPQExpBufferStr(&buf, "ORDER BY 1");
+	}
+	else
+		appendPQExpBufferStr(&buf, "ORDER BY 1");
+
 	res = PSQLexec(buf.data);
 	termPQExpBuffer(&buf);
 	if (!res)
@@ -3422,7 +3447,26 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
 						  "n.nspname", "c.relname", NULL,
 						  "pg_catalog.pg_table_is_visible(c.oid)");
 
-	appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
+	if (verbose && pset.sversion >= 80100)
+	{
+		if (pset.verbose_sort_columns == PSQL_SORT_SCHEMA_NAME)
+			appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
+		else if (pset.verbose_sort_columns == PSQL_SORT_NAME_SCHEMA)
+			appendPQExpBufferStr(&buf, "ORDER BY 2,1;");
+		else
+		{
+			if (pset.sversion >= 90000)
+				appendPQExpBuffer(&buf,
+					"ORDER BY pg_catalog.pg_table_size(c.oid) %s, 1,2",
+					SORT_DIRECTION_STR(pset.verbose_sort_direction));
+			else
+				appendPQExpBuffer(&buf,
+					"ORDER BY pg_catalog.pg_relation_size(c.oid) %s, 1,2",
+					SORT_DIRECTION_STR(pset.verbose_sort_direction));
+		}
+	}
+	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 b3dbb5946e..abdc6555b6 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -336,7 +336,7 @@ helpVariables(unsigned short int pager)
 	 * Windows builds currently print one more line than non-Windows builds.
 	 * Using the larger number is fine.
 	 */
-	output = PageOutput(88, pager ? &(pset.popt.topt) : NULL);
+	output = PageOutput(92, pager ? &(pset.popt.topt) : NULL);
 
 	fprintf(output, _("List of specially treated variables\n\n"));
 
@@ -373,6 +373,10 @@ helpVariables(unsigned short int pager)
 	fprintf(output, _("  SINGLESTEP         single-step mode (same as -s option)\n"));
 	fprintf(output, _("  USER               the currently connected database user\n"));
 	fprintf(output, _("  VERBOSITY          controls verbosity of error reports [default, verbose, terse]\n"));
+	fprintf(output, _("  VERBOSE_SORT_COLUMNS\n"
+					  "                     sort columns for verbose mode [schema_name, name_schema, size]\n"));
+	fprintf(output, _("  VERBOSE_SORT_DIRECTION\n"
+					  "                     direction of sort of verbose mode [asc, desc]\n"));
 
 	fprintf(output, _("\nDisplay settings:\n"));
 	fprintf(output, _("Usage:\n"));
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index b78f151acd..32249df15c 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -77,6 +77,19 @@ enum trivalue
 	TRI_YES
 };
 
+typedef enum
+{
+	PSQL_SORT_SCHEMA_NAME,
+	PSQL_SORT_NAME_SCHEMA,
+	PSQL_SORT_SIZE,
+} PSQL_SORT_COLUMNS;
+
+typedef enum
+{
+	PSQL_SORT_ASC,
+	PSQL_SORT_DESC
+} PSQL_SORT_DIRECTION;
+
 typedef struct _psqlSettings
 {
 	PGconn	   *db;				/* connection to backend */
@@ -138,6 +151,8 @@ typedef struct _psqlSettings
 	const char *prompt3;
 	PGVerbosity verbosity;		/* current error verbosity level */
 	PGContextVisibility show_context;	/* current context display level */
+	PSQL_SORT_COLUMNS	verbose_sort_columns;	/* sort columns for describe verbose command */
+	PSQL_SORT_DIRECTION verbose_sort_direction;	/* sort direction for describe verbose command */
 } PsqlSettings;
 
 extern PsqlSettings pset;
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 7f767976a5..8e3fb1bd78 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -168,6 +168,9 @@ main(int argc, char *argv[])
 	SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
 	SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
 
+	SetVariable(pset.vars, "VERBOSE_SORT_COLUMNS", "schema_name");
+	SetVariable(pset.vars, "VERBOSE_SORT_DIRECTION", "asc");
+
 	parse_psql_options(argc, argv, &options);
 
 	/*
@@ -1075,6 +1078,60 @@ verbosity_hook(const char *newval)
 }
 
 static char *
+verbose_sort_columns_substitute_hook(char *newval)
+{
+	if (newval == NULL)
+		newval = pg_strdup("schema_name");
+	return newval;
+}
+
+static bool
+verbose_sort_columns_hook(const char *newval)
+{
+	Assert(newval != NULL);		/* else substitute hook messed up */
+	if (pg_strcasecmp(newval, "schema_name") == 0)
+		pset.verbose_sort_columns = PSQL_SORT_SCHEMA_NAME;
+	else if (pg_strcasecmp(newval, "name_schema") == 0)
+		pset.verbose_sort_columns = PSQL_SORT_NAME_SCHEMA;
+	else if (pg_strcasecmp(newval, "size") == 0)
+		pset.verbose_sort_columns = PSQL_SORT_SIZE;
+	else
+	{
+		PsqlVarEnumError("VERBOSE_SORT_COLUMNS", newval,
+						 "schema_name, name_schema, size");
+		return false;
+	}
+
+	return true;
+}
+
+static char *
+verbose_sort_direction_substitute_hook(char *newval)
+{
+	if (newval == NULL)
+		newval = pg_strdup("asc");
+	return newval;
+}
+
+static bool
+verbose_sort_direction_hook(const char *newval)
+{
+	Assert(newval != NULL);		/* else substitute hook messed up */
+	if (pg_strcasecmp(newval, "asc") == 0)
+		pset.verbose_sort_direction = PSQL_SORT_ASC;
+	else if (pg_strcasecmp(newval, "desc") == 0)
+		pset.verbose_sort_direction = PSQL_SORT_DESC;
+	else
+	{
+		PsqlVarEnumError("VERBOSE_SORT_DIRECTION", newval, "asc, desc");
+		return false;
+	}
+
+	return true;
+}
+
+
+static char *
 show_context_substitute_hook(char *newval)
 {
 	if (newval == NULL)
@@ -1163,6 +1220,12 @@ EstablishVariableSpace(void)
 	SetVariableHooks(pset.vars, "VERBOSITY",
 					 verbosity_substitute_hook,
 					 verbosity_hook);
+	SetVariableHooks(pset.vars, "VERBOSE_SORT_COLUMNS",
+					 verbose_sort_columns_substitute_hook,
+					 verbose_sort_columns_hook);
+	SetVariableHooks(pset.vars, "VERBOSE_SORT_DIRECTION",
+					 verbose_sort_direction_substitute_hook,
+					 verbose_sort_direction_hook);
 	SetVariableHooks(pset.vars, "SHOW_CONTEXT",
 					 show_context_substitute_hook,
 					 show_context_hook);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 1583cfa998..963f537a24 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3564,6 +3564,10 @@ psql_completion(const char *text, int start, int end)
 			COMPLETE_WITH_LIST_CS3("never", "errors", "always");
 		else if (TailMatchesCS1("VERBOSITY"))
 			COMPLETE_WITH_LIST_CS3("default", "verbose", "terse");
+		else if (TailMatchesCS1("VERBOSE_SORT_COLUMNS"))
+			COMPLETE_WITH_LIST_CS3("schema_name", "name_schema","size");
+		else if (TailMatchesCS1("VERBOSE_SORT_DIRECTION"))
+			COMPLETE_WITH_LIST_CS2("asc", "desc");
 	}
 	else if (TailMatchesCS1("\\sf*"))
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to