diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 73c5601..ed4ff09 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -153,6 +153,7 @@ do { \
 do { \
 	completion_squery = &(query); \
 	completion_charp = addon; \
+	completion_case_sensitive = false; \
 	matches = completion_matches(text, complete_from_schema_query); \
 } while (0)
 
@@ -3754,7 +3755,17 @@ _complete_from_query(int is_schema_query, const char *text, int state)
 		while (list_index < PQntuples(result) &&
 			   (item = PQgetvalue(result, list_index++, 0)))
 			if (pg_strncasecmp(text, item, byte_length) == 0)
-				return pg_strdup(item);
+			{
+				if (completion_case_sensitive)
+					return pg_strdup(item);
+				else
+
+					/*
+					 * If case insensitive matching was requested initially,
+					 * adjust the case according to setting.
+					 */
+					return pg_strdup_keyword_case(item, text);
+			}
 	}
 
 	/* If nothing matches, free the db structure and return null */
