From 6312723c75c1459f24a5fcb04bfb2823621eefed Mon Sep 17 00:00:00 2001 From: Taiki Koshino Date: Mon, 30 Nov 2026 20:14:39 +0900 Subject: [PATCH v2] Improve tab completion after CREATE INDEX key list psql already completes index names, relations, access methods, and index columns for several CREATE INDEX forms, but offers no completions after the parenthesized list of index expressions has been closed. At that point, CREATE INDEX accepts INCLUDE, NULLS [NOT] DISTINCT, WITH, TABLESPACE, and WHERE clauses. Extend the existing completion paths to offer these clauses for named and unnamed indexes, including the forms using CONCURRENTLY or an explicit index access method. Limit this change to command forms already recognized by the existing CREATE INDEX completion rules. Supporting additional optional syntax such as IF NOT EXISTS and ONLY would also require extending the earlier completion stages and is outside the scope of this patch. Add tests covering the existing command forms and each of the offered clauses. Reviewd-by: Author: Taiki Koshino Discussion: https://www.postgresql.org/message-id/TY7P286MB974944DB10FB53DF9D4F936CED94422@TY7P286MB974944.JPNP286.PROD.OUTLOOK.COM --- src/bin/psql/t/010_tab_completion.pl | 15 +++++++++++++++ src/bin/psql/tab-complete.in.c | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl index 1d2e5f5b92a..e10483ac1bd 100644 --- a/src/bin/psql/t/010_tab_completion.pl +++ b/src/bin/psql/t/010_tab_completion.pl @@ -347,6 +347,21 @@ check_completion( clear_query(); +# check CREATE INDEX options following the index expression list. +check_completion( + "CREATE INDEX idx ON tab1 (c1) W\t\t", + qr/WHERE +WITH \(/, + "list options after a CREATE INDEX key list"); + +clear_query(); + +check_completion( + "CREATE INDEX idx ON tab1 (c1) NULLS \t\t", + qr/DISTINCT +NOT DISTINCT/, + "list CREATE INDEX NULLS options"); + +clear_query(); + # check VersionedQuery infrastructure check_completion( "DROP PUBLIC\t \t\t", diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c index 96de78cd087..18c5c1ed493 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -3589,6 +3589,25 @@ match_previous_words(int pattern_id, !TailMatches("FOR", MatchAny, MatchAny, MatchAny)) COMPLETE_WITH("("); + /* Complete options after the parenthesized list of index expressions. */ + else if (TailMatches("INDEX", "ON", MatchAny, "(*)") || + TailMatches("INDEX", MatchAny, "ON", MatchAny, "(*)") || + TailMatches("INDEX", "CONCURRENTLY", MatchAny, "ON", MatchAny, "(*)") || + TailMatches("INDEX", "ON", MatchAny, "USING", MatchAny, "(*)") || + TailMatches("INDEX", MatchAny, "ON", MatchAny, "USING", MatchAny, "(*)") || + TailMatches("INDEX", "CONCURRENTLY", MatchAny, "ON", MatchAny, "USING", MatchAny, "(*)")) + COMPLETE_WITH("INCLUDE (", "NULLS DISTINCT", "NULLS NOT DISTINCT", + "WITH (", "TABLESPACE", "WHERE"); + + /* Complete NULLS [NOT] DISTINCT. */ + else if (TailMatches("INDEX", "ON", MatchAny, "(*)", "NULLS") || + TailMatches("INDEX", MatchAny, "ON", MatchAny, "(*)", "NULLS") || + TailMatches("INDEX", "CONCURRENTLY", MatchAny, "ON", MatchAny, "(*)", "NULLS") || + TailMatches("INDEX", "ON", MatchAny, "USING", MatchAny, "(*)", "NULLS") || + TailMatches("INDEX", MatchAny, "ON", MatchAny, "USING", MatchAny, "(*)", "NULLS") || + TailMatches("INDEX", "CONCURRENTLY", MatchAny, "ON", MatchAny, "USING", MatchAny, "(*)", "NULLS")) + COMPLETE_WITH("DISTINCT", "NOT DISTINCT"); + /* CREATE OR REPLACE */ else if (Matches("CREATE", "OR")) COMPLETE_WITH("REPLACE"); -- 2.52.0