diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 176f7e74c2..d13cfc3adf 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -4816,123 +4816,125 @@ _complete_from_query(const char *simple_query,
 			 */
 			if (schemaname == NULL || schema_query->namespace == NULL)
 			{
-			/* Get unqualified names matching the input-so-far */
-			appendPQExpBufferStr(&query_buffer, "SELECT ");
-			if (schema_query->use_distinct)
-				appendPQExpBufferStr(&query_buffer, "DISTINCT ");
-			appendPQExpBuffer(&query_buffer,
-							  "%s, NULL::pg_catalog.text FROM %s",
-							  schema_query->result,
-							  schema_query->catname);
-			if (schema_query->infonamespace && completion_info_schema)
-				appendPQExpBufferStr(&query_buffer,
-									 ", pg_catalog.pg_namespace ni");
-			appendPQExpBufferStr(&query_buffer, " WHERE ");
-			if (schema_query->selcondition)
-				appendPQExpBuffer(&query_buffer, "%s AND ",
-								  schema_query->selcondition);
-			appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s'",
-							  schema_query->result,
-							  object_length, e_objectname);
-			if (schema_query->viscondition)
-			appendPQExpBuffer(&query_buffer, " AND %s",
-							  schema_query->viscondition);
-			if (schema_query->infoname)
-			{
-				Assert(completion_info_object);
-				appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
-								  schema_query->infoname, e_info_object);
+				/* Get unqualified names matching the input-so-far */
+				appendPQExpBufferStr(&query_buffer, "SELECT ");
+				if (schema_query->use_distinct)
+					appendPQExpBufferStr(&query_buffer, "DISTINCT ");
+				appendPQExpBuffer(&query_buffer,
+								  "%s, NULL::pg_catalog.text FROM %s",
+								  schema_query->result,
+								  schema_query->catname);
 				if (schema_query->infonamespace && completion_info_schema)
-					appendPQExpBuffer(&query_buffer,
-									  " AND %s = ni.oid AND ni.nspname = '%s'",
-									  schema_query->infonamespace,
-									  e_info_schema);
-				else if (schema_query->infoviscondition)
-					appendPQExpBuffer(&query_buffer,
-									  " AND %s",
-									  schema_query->infoviscondition);
-			}
-
-			/*
-			 * When fetching relation names, suppress system catalogs unless
-			 * the input-so-far begins with "pg_".  This is a compromise
-			 * between not offering system catalogs for completion at all, and
-			 * having them swamp the result when the input is just "p".
-			 */
-			if (strcmp(schema_query->catname,
-					   "pg_catalog.pg_class c") == 0 &&
-				strncmp(objectname, "pg_", 3) != 0)
-			{
-				appendPQExpBufferStr(&query_buffer,
-									 " AND c.relnamespace <> (SELECT oid FROM"
-									 " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
-			}
-
-			/*
-			 * If the target object type can be schema-qualified, add in
-			 * schema names matching the input-so-far.
-			 */
-			if (schema_query->namespace)
-			{
-			appendPQExpBuffer(&query_buffer, "\nUNION ALL\n"
-							  "SELECT NULL::pg_catalog.text, n.nspname "
-							  "FROM pg_catalog.pg_namespace n "
-							  "WHERE substring(n.nspname,1,%d)='%s'",
-							  object_length, e_objectname);
+					appendPQExpBufferStr(&query_buffer,
+										 ", pg_catalog.pg_namespace ni");
+				appendPQExpBufferStr(&query_buffer, " WHERE ");
+				if (schema_query->selcondition)
+					appendPQExpBuffer(&query_buffer, "%s AND ",
+									  schema_query->selcondition);
+				appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s'",
+								  schema_query->result,
+								  object_length, e_objectname);
+				if (schema_query->viscondition)
+					appendPQExpBuffer(&query_buffer, " AND %s",
+									  schema_query->viscondition);
+				if (schema_query->infoname)
+				{
+					Assert(completion_info_object);
+					appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
+									  schema_query->infoname, e_info_object);
+					if (schema_query->infonamespace && completion_info_schema)
+						appendPQExpBuffer(&query_buffer,
+										  " AND %s = ni.oid AND ni.nspname = '%s'",
+										  schema_query->infonamespace,
+										  e_info_schema);
+					else if (schema_query->infoviscondition)
+						appendPQExpBuffer(&query_buffer,
+										  " AND %s",
+										  schema_query->infoviscondition);
+				}
 
-			/*
-			 * Likewise, suppress system schemas unless the input-so-far
-			 * begins with "pg_".
-			 */
-			if (strncmp(objectname, "pg_", 3) != 0)
-				appendPQExpBufferStr(&query_buffer,
-									 " AND n.nspname NOT LIKE 'pg\\_%'");
+				/*
+				 * When fetching relation names, suppress system catalogs
+				 * unless the input-so-far begins with "pg_".  This is a
+				 * compromise between not offering system catalogs for
+				 * completion at all, and having them swamp the result when
+				 * the input is just "p".
+				 */
+				if (strcmp(schema_query->catname,
+						   "pg_catalog.pg_class c") == 0 &&
+					strncmp(objectname, "pg_", 3) != 0)
+				{
+					appendPQExpBufferStr(&query_buffer,
+										 " AND c.relnamespace <> (SELECT oid FROM"
+										 " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
+				}
 
-			/*
-			 * Since we're matching these schema names to the object name,
-			 * handle their quoting using the object name's quoting state.
-			 */
-			schemaquoted = objectquoted;
-			}
+				/*
+				 * If the target object type can be schema-qualified, add in
+				 * schema names matching the input-so-far.
+				 */
+				if (schema_query->namespace)
+				{
+					appendPQExpBuffer(&query_buffer, "\nUNION ALL\n"
+									  "SELECT NULL::pg_catalog.text, n.nspname "
+									  "FROM pg_catalog.pg_namespace n "
+									  "WHERE substring(n.nspname,1,%d)='%s'",
+									  object_length, e_objectname);
+
+					/*
+					 * Likewise, suppress system schemas unless the
+					 * input-so-far begins with "pg_".
+					 */
+					if (strncmp(objectname, "pg_", 3) != 0)
+						appendPQExpBufferStr(&query_buffer,
+											 " AND n.nspname NOT LIKE 'pg\\_%'");
+
+					/*
+					 * Since we're matching these schema names to the object
+					 * name, handle their quoting using the object name's
+					 * quoting state.
+					 */
+					schemaquoted = objectquoted;
+				}
 			}
 			else
 			{
-			/* Input is qualified, so produce only qualified names */
-			appendPQExpBufferStr(&query_buffer, "SELECT ");
-			if (schema_query->use_distinct)
-				appendPQExpBufferStr(&query_buffer, "DISTINCT ");
-			appendPQExpBuffer(&query_buffer, "%s, n.nspname "
-							  "FROM %s, pg_catalog.pg_namespace n",
-							  schema_query->result,
-							  schema_query->catname);
-			if (schema_query->infonamespace && completion_info_schema)
-				appendPQExpBufferStr(&query_buffer,
-									 ", pg_catalog.pg_namespace ni");
-			appendPQExpBuffer(&query_buffer, " WHERE %s = n.oid AND ",
-							  schema_query->namespace);
-			if (schema_query->selcondition)
-				appendPQExpBuffer(&query_buffer, "%s AND ",
-								  schema_query->selcondition);
-			appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s' AND ",
-							  schema_query->result,
-							  object_length, e_objectname);
-			appendPQExpBuffer(&query_buffer, "n.nspname = '%s'",
-							  e_schemaname);
-			if (schema_query->infoname)
-			{
-				Assert(completion_info_object);
-				appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
-								  schema_query->infoname, e_info_object);
+				/* Input is qualified, so produce only qualified names */
+				appendPQExpBufferStr(&query_buffer, "SELECT ");
+				if (schema_query->use_distinct)
+					appendPQExpBufferStr(&query_buffer, "DISTINCT ");
+				appendPQExpBuffer(&query_buffer, "%s, n.nspname "
+								  "FROM %s, pg_catalog.pg_namespace n",
+								  schema_query->result,
+								  schema_query->catname);
 				if (schema_query->infonamespace && completion_info_schema)
-					appendPQExpBuffer(&query_buffer,
-									  " AND %s = ni.oid AND ni.nspname = '%s'",
-									  schema_query->infonamespace,
-									  e_info_schema);
-				else if (schema_query->infoviscondition)
-					appendPQExpBuffer(&query_buffer,
-									  " AND %s",
-									  schema_query->infoviscondition);
-			}
+					appendPQExpBufferStr(&query_buffer,
+										 ", pg_catalog.pg_namespace ni");
+				appendPQExpBuffer(&query_buffer, " WHERE %s = n.oid AND ",
+								  schema_query->namespace);
+				if (schema_query->selcondition)
+					appendPQExpBuffer(&query_buffer, "%s AND ",
+									  schema_query->selcondition);
+				appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s' AND ",
+								  schema_query->result,
+								  object_length, e_objectname);
+				appendPQExpBuffer(&query_buffer, "n.nspname = '%s'",
+								  e_schemaname);
+				if (schema_query->infoname)
+				{
+					Assert(completion_info_object);
+					appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
+									  schema_query->infoname, e_info_object);
+					if (schema_query->infonamespace && completion_info_schema)
+						appendPQExpBuffer(&query_buffer,
+										  " AND %s = ni.oid AND ni.nspname = '%s'",
+										  schema_query->infonamespace,
+										  e_info_schema);
+					else if (schema_query->infoviscondition)
+						appendPQExpBuffer(&query_buffer,
+										  " AND %s",
+										  schema_query->infoviscondition);
+				}
 			}
 		}
 		else
