From 625b14ee9b0b5611863aed4787a4dd6b66e53033 Mon Sep 17 00:00:00 2001
From: Baji Shaik <baji.pgdev@gmail.com>
Date: Mon, 19 Oct 2026 19:35:45 -0500
Subject: [PATCH] psql: Add tab completion for ALTER TABLE ALTER CONSTRAINT

After ALTER TABLE t ALTER CONSTRAINT c, psql now offers DEFERRABLE,
NOT DEFERRABLE, ENFORCED, NOT ENFORCED, INITIALLY DEFERRED, INITIALLY
IMMEDIATE, INHERIT, and NO INHERIT.  Previously, selecting the
constraint name was a dead end with no further suggestions.

Also add the follow-up completions for NOT, NO, and INITIALLY, so
that choosing one of those words and pressing tab again offers the
matching second word, instead of a second dead end.  (As pointed out
by Chao Li, matching the existing pattern used for e.g. ALTER
STATISTICS ... SET.)
---
 src/bin/psql/tab-complete.in.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index f86684edc08..d1061587cc6 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -2934,6 +2934,20 @@ match_previous_words(int pattern_id,
 		set_completion_reference(prev3_wd);
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table);
 	}
+	/* ALTER TABLE <sth> ALTER CONSTRAINT <constraint> options */
+	else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "CONSTRAINT", MatchAny))
+		COMPLETE_WITH("DEFERRABLE", "NOT DEFERRABLE", "ENFORCED", "NOT ENFORCED",
+					  "INITIALLY DEFERRED", "INITIALLY IMMEDIATE",
+					  "INHERIT", "NO INHERIT");
+	/* ALTER TABLE <sth> ALTER CONSTRAINT <constraint> NOT */
+	else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "CONSTRAINT", MatchAny, "NOT"))
+		COMPLETE_WITH("DEFERRABLE", "ENFORCED");
+	/* ALTER TABLE <sth> ALTER CONSTRAINT <constraint> NO */
+	else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "CONSTRAINT", MatchAny, "NO"))
+		COMPLETE_WITH("INHERIT");
+	/* ALTER TABLE <sth> ALTER CONSTRAINT <constraint> INITIALLY */
+	else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "CONSTRAINT", MatchAny, "INITIALLY"))
+		COMPLETE_WITH("DEFERRED", "IMMEDIATE");
 	/* ALTER TABLE <sth> VALIDATE CONSTRAINT <non-validated constraint> */
 	else if (Matches("ALTER", "TABLE", MatchAny, "VALIDATE", "CONSTRAINT"))
 	{
-- 
2.54.0 (Apple Git-157)

