From bfd77e636675e5907be3da6994ac08cc57ddb8f4 Mon Sep 17 00:00:00 2001
From: Shubh Mehta <shubhmehta942@gmail.com>
Date: Sun, 29 Nov 2026 13:46:24 +0530
Subject: [PATCH v1] psql: Add tab completion for \dO, \dc and \det

These commands had no completion rule of their own, so they fell
through to the generic \d* rule and offered table names.  Complete
them with collation, conversion and foreign table names instead.
---
 src/bin/psql/t/010_tab_completion.pl | 23 ++++++++++++++++++++++-
 src/bin/psql/tab-complete.in.c       | 15 ++++++++++++++-
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl
index 1d2e5f5..bad7118 100644
--- a/src/bin/psql/t/010_tab_completion.pl
+++ b/src/bin/psql/t/010_tab_completion.pl
@@ -44,7 +44,12 @@ $node->safe_psql('postgres',
 	  . "CREATE TABLE mytab246 (f1 int, f2 text);\n"
 	  . "CREATE TABLE \"mixedName\" (f1 int, f2 text);\n"
 	  . "CREATE TYPE enum1 AS ENUM ('foo', 'bar', 'baz', 'BLACK');\n"
-	  . "CREATE PUBLICATION some_publication;\n");
+	  . "CREATE PUBLICATION some_publication;\n"
+	  . "CREATE COLLATION test_collation (provider = libc, locale = 'C');\n"
+	  . "CREATE CONVERSION test_conversion FOR 'UTF8' TO 'LATIN1' FROM utf8_to_iso8859_1;\n"
+	  . "CREATE FOREIGN DATA WRAPPER test_fdw;\n"
+	  . "CREATE SERVER test_server FOREIGN DATA WRAPPER test_fdw;\n"
+	  . "CREATE FOREIGN TABLE ftab1 (f1 int) SERVER test_server;\n");
 
 # In a VPATH build, we'll be started in the source directory, but we want
 # to run in the build directory so that we can use relative paths to
@@ -191,6 +196,22 @@ check_completion("\\DRD\t", qr/drds /, "complete \\DRD<tab> to \\drds");
 
 clear_query();
 
+# check completion of object names for some \d commands
+check_completion("\\dO test_coll\t", qr/test_collation /,
+	"complete \\dO with collation name");
+
+clear_query();
+
+check_completion("\\dc test_conv\t", qr/test_conversion /,
+	"complete \\dc with conversion name");
+
+clear_query();
+
+check_completion("\\det fta\t", qr/ftab1 /,
+	"complete \\det with foreign table name");
+
+clear_query();
+
 # check completion of a schema-qualified name
 check_completion("select * from pub\t",
 	qr/public\./, "complete schema when relevant");
diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 96de78c..0d4bc1f 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -970,6 +970,13 @@ static const SchemaQuery Query_for_list_of_collations = {
 	.result = "c.collname",
 };
 
+static const SchemaQuery Query_for_list_of_conversions = {
+	.catname = "pg_catalog.pg_conversion c",
+	.viscondition = "pg_catalog.pg_conversion_is_visible(c.oid)",
+	.namespace = "c.connamespace",
+	.result = "c.conname",
+};
+
 static const SchemaQuery Query_for_partition_of_table = {
 	.catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_inherits i",
 	.selcondition = "c1.oid=i.inhparent and i.inhrelid=c2.oid and c2.relispartition",
@@ -5524,7 +5531,7 @@ match_previous_words(int pattern_id,
 		COMPLETE_WITH_TIMEZONE_NAME();
 
 /* Backslash commands */
-/* TODO:  \dc \dd \dl */
+/* TODO:  \dd \dl */
 	else if (TailMatchesCS("\\?"))
 		COMPLETE_WITH_CS("commands", "options", "variables");
 	else if (TailMatchesCS("\\connect|\\c"))
@@ -5551,10 +5558,14 @@ match_previous_words(int pattern_id,
 		COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
 	else if (TailMatchesCS("\\dconfig*"))
 		COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_show_vars);
+	else if (TailMatchesCS("\\dc*"))
+		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_conversions);
 	else if (TailMatchesCS("\\dD*"))
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
 	else if (TailMatchesCS("\\des*"))
 		COMPLETE_WITH_QUERY(Query_for_list_of_servers);
+	else if (TailMatchesCS("\\det*"))
+		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
 	else if (TailMatchesCS("\\deu*"))
 		COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
 	else if (TailMatchesCS("\\dew*"))
@@ -5580,6 +5591,8 @@ match_previous_words(int pattern_id,
 		COMPLETE_WITH_QUERY(Query_for_list_of_languages);
 	else if (TailMatchesCS("\\dn*"))
 		COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
+	else if (TailMatchesCS("\\dO*"))
+		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
 	/* no support for completing operators, but we can complete types: */
 	else if (HeadMatchesCS("\\do*", MatchAny))
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
-- 
2.53.0

