From 7c6e19c14d20c02b33f22531947cf9ca1cb00a21 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignesh21@gmail.com>
Date: Wed, 15 Apr 2026 18:30:03 +0530
Subject: [PATCH v2] Fix tab completion after EXCEPT() in IMPORT FOREIGN SCHEMA

Tab completion for IMPORT FOREIGN SCHEMA incorrectly suggested FROM
SERVER after EXCEPT (...), because EXCEPT (...) is now also valid in
CREATE and ALTER PUBLICATION.

Previously, the generic TailMatches("EXCEPT", "(*)") check was safe
because no other command used 'EXCEPT (...)'. After adding support for
'EXCEPT (...)' in publication commands, the same completion rule started
matching unrelated statements and incorrectly offered 'FROM SERVER'
there as well.

Fix this by restricting the 'EXCEPT (...)' completion path to
'IMPORT FOREIGN SCHEMA' using Matches().

This ensures 'FROM SERVER' is suggested only for valid
'IMPORT FOREIGN SCHEMA' syntax.
---
 src/bin/psql/tab-complete.in.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 9990f818942..db65d130fcb 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -4900,7 +4900,7 @@ match_previous_words(int pattern_id,
 	else if (Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny))
 		COMPLETE_WITH("EXCEPT (", "FROM SERVER", "LIMIT TO (");
 	else if (TailMatches("LIMIT", "TO", "(*)") ||
-			 TailMatches("EXCEPT", "(*)"))
+			 Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny, "EXCEPT", "(*)"))
 		COMPLETE_WITH("FROM SERVER");
 	else if (TailMatches("FROM", "SERVER", MatchAny))
 		COMPLETE_WITH("INTO");
-- 
2.43.0

