From 5a107c50672d9d98fe1ec6663ea8c96e529dca9d Mon Sep 17 00:00:00 2001 From: Taiki Koshino Date: Thu, 26 Nov 2026 15:15:55 +0900 Subject: [PATCH v1] 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. --- src/bin/psql/t/010_tab_completion.pl | 57 ++++++++++++++++++++++++++++ src/bin/psql/tab-complete.in.c | 19 ++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl index 1d2e5f5b92a..7e621dff2ac 100644 --- a/src/bin/psql/t/010_tab_completion.pl +++ b/src/bin/psql/t/010_tab_completion.pl @@ -347,6 +347,63 @@ check_completion( clear_query(); +# check CREATE INDEX options following the index expression list. +check_completion( + "CREATE INDEX CONCURRENTLY idx ON tab1 (c1) INC\t", + qr/INCLUDE \(/, + "complete a named concurrent index without USING"); + +clear_query(); + +check_completion( + "CREATE INDEX CONCURRENTLY ON tab1 (c1) WH\t", + qr/WHERE /, + "complete an unnamed concurrent index without USING"); + +clear_query(); + +check_completion( + "CREATE INDEX idx ON tab1 (c1) WI\t", + qr/WITH \(/, + "complete a named non-concurrent index without USING"); + +clear_query(); + +check_completion( + "CREATE INDEX ON tab1 (c1) TAB\t", + qr/TABLESPACE /, + "complete an unnamed non-concurrent index without USING"); + +clear_query(); + +check_completion( + "CREATE INDEX CONCURRENTLY idx ON tab1 USING btree (c1) WH\t", + qr/WHERE /, + "complete a named concurrent index with USING"); + +clear_query(); + +check_completion( + "CREATE INDEX CONCURRENTLY ON tab1 USING btree (c1) INC\t", + qr/INCLUDE \(/, + "complete an unnamed concurrent index with USING"); + +clear_query(); + +check_completion( + "CREATE UNIQUE INDEX idx ON tab1 USING btree (c1) NULLS \t\t", + qr/DISTINCT.*NOT DISTINCT/s, + "complete a named non-concurrent index with USING"); + +clear_query(); + +check_completion( + "CREATE INDEX ON tab1 USING btree (c1) WI\t", + qr/WITH \(/, + "complete an unnamed non-concurrent index with USING"); + +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