From 3e4360812a552274aaf4703bd373e766979be182 Mon Sep 17 00:00:00 2001 From: Ishjaap Singh Date: Thu, 19 Nov 2026 13:46:53 +0000 Subject: [PATCH v2] psql: Don't offer partitioned tables after CREATE INDEX CONCURRENTLY Tab completion after CREATE INDEX CONCURRENTLY [name] ON offered the same relations as plain CREATE INDEX, including partitioned tables. CREATE INDEX CONCURRENTLY is not supported on partitioned tables, so choosing one always fails: CREATE INDEX CONCURRENTLY ON parted (a); ERROR: cannot create index on partitioned table "parted" concurrently Use a separate list without partitioned tables after CONCURRENTLY. Plain tables, materialized views and leaf partitions are still offered. The rules for CONCURRENTLY [name] ON and CONCURRENTLY ON also matched only the end of the command, so that they would work inside CREATE SCHEMA. But CREATE INDEX CONCURRENTLY can't be used there, and matching the end made them fire after REPACK (CONCURRENTLY ON too, offering tables and then "(" and USING. Match the whole CREATE [UNIQUE] INDEX CONCURRENTLY command for the list of relations, and for the ON offered after an index name. The rules after the table don't need CONCURRENTLY at all: the INDEX ON
rules already match CREATE INDEX CONCURRENTLY ON
. Plain CREATE INDEX still matches the end, since it does work inside CREATE SCHEMA. --- src/bin/psql/t/010_tab_completion.pl | 84 ++++++++++++++++++++++++++++ src/bin/psql/tab-complete.in.c | 41 +++++++++++--- 2 files changed, 118 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl index 1d2e5f5b92a..045b68344b4 100644 --- a/src/bin/psql/t/010_tab_completion.pl +++ b/src/bin/psql/t/010_tab_completion.pl @@ -347,6 +347,90 @@ check_completion( clear_query(); +# CREATE INDEX CONCURRENTLY doesn't support partitioned tables, so they +# must not be offered after it. The two tables share a prefix: if only the +# plain one is offered, "cic_p" completes to it. +$node->safe_psql('postgres', + "CREATE TABLE cic_part (f1 int) PARTITION BY RANGE (f1);\n" + . "CREATE TABLE cic_plain (f1 int);\n"); + +# "index" is a valid index name, and must not make the command look like a +# plain CREATE INDEX. +foreach my $cmd ( + 'CREATE INDEX CONCURRENTLY ON', + 'CREATE INDEX CONCURRENTLY cic_idx ON', + 'CREATE INDEX CONCURRENTLY index ON', + 'CREATE UNIQUE INDEX CONCURRENTLY ON', + 'CREATE UNIQUE INDEX CONCURRENTLY index ON') +{ + check_completion("$cmd cic_p\t", qr/cic_plain /, + "$cmd does not offer partitioned tables"); + clear_query(); + + # A prefix that matches only the partitioned table must not complete. + # The X typed after the tab shows that the word was left alone. + check_completion("$cmd cic_pa\tX", + qr/cic_pa\a?X/, "$cmd does not complete a partitioned table"); + clear_query(); +} + +# CREATE INDEX CONCURRENTLY can't be used inside CREATE SCHEMA, and +# "CONCURRENTLY ON" in other commands has nothing to do with it, so no +# relations are offered there. +foreach my $cmd ( + 'CREATE SCHEMA cic_s CREATE INDEX CONCURRENTLY ON', + 'CREATE SCHEMA cic_s CREATE INDEX CONCURRENTLY cic_idx ON', + 'REPACK (CONCURRENTLY ON') +{ + check_completion("$cmd cic_pl\tX", + qr/cic_pl\a?X/, "$cmd does not offer relations"); + clear_query(); +} + +# Bare Tab there must not take ON as an index name and offer ON again. +check_completion( + "CREATE SCHEMA cic_s CREATE INDEX CONCURRENTLY ON \tX", + qr/CONCURRENTLY ON \a?X/, + "CREATE SCHEMA with CREATE INDEX CONCURRENTLY ON does not repeat ON"); +clear_query(); + +# An index name is still followed by ON at top level, and for plain +# CREATE INDEX inside CREATE SCHEMA. +foreach my $cmd ( + 'CREATE INDEX CONCURRENTLY cic_idx', + 'CREATE SCHEMA cic_s CREATE INDEX cic_idx') +{ + check_completion("$cmd \t", qr/cic_idx ON /, "$cmd offers ON"); + clear_query(); +} + +# After the table, CREATE INDEX CONCURRENTLY still completes like CREATE +# INDEX, and REPACK (CONCURRENTLY ON doesn't. +check_completion( + "CREATE INDEX CONCURRENTLY ON cic_plain U\t", + qr/USING /, + "CREATE INDEX CONCURRENTLY ON
offers USING"); +clear_query(); +check_completion( + "REPACK (CONCURRENTLY ON cic_plain U\tX", + qr/U\a?X/, + "REPACK (CONCURRENTLY ON does not offer USING"); +clear_query(); + +# Without CONCURRENTLY, partitioned tables are fine. +foreach my $cmd ( + 'CREATE INDEX ON', + 'CREATE INDEX cic_idx ON', + 'CREATE INDEX index ON', + 'CREATE UNIQUE INDEX ON') +{ + check_completion( + "$cmd cic_p\t\t", + qr/cic_part +cic_plain/, + "$cmd offers partitioned tables"); + 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..67bab88f446 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -930,6 +930,17 @@ static const SchemaQuery Query_for_list_of_indexables = { .result = "c.relname", }; +/* Relations supporting CREATE INDEX CONCURRENTLY */ +static const SchemaQuery Query_for_list_of_concurrent_indexables = { + .catname = "pg_catalog.pg_class c", + .selcondition = + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ")", + .viscondition = "pg_catalog.pg_table_is_visible(c.oid)", + .namespace = "c.relnamespace", + .result = "c.relname", +}; + /* * Relations supporting VACUUM are currently same as those supporting * indexing. @@ -3547,11 +3558,26 @@ match_previous_words(int pattern_id, "ON", "CONCURRENTLY"); /* - * Complete ... INDEX|CONCURRENTLY [] ON with a list of relations - * that indexes can be created on + * Complete CREATE [UNIQUE] INDEX CONCURRENTLY [] ON with a list of + * relations that support it, which leaves out partitioned tables. CREATE + * INDEX CONCURRENTLY can't be used inside CREATE SCHEMA, so these rules + * match the whole command. Matching only the end would also fire on + * other commands, such as REPACK (CONCURRENTLY ON. + */ + else if (Matches("CREATE", "INDEX", "CONCURRENTLY", "ON") || + Matches("CREATE", "INDEX", "CONCURRENTLY", MatchAny, "ON") || + Matches("CREATE", "UNIQUE", "INDEX", "CONCURRENTLY", "ON") || + Matches("CREATE", "UNIQUE", "INDEX", "CONCURRENTLY", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_concurrent_indexables); + + /* + * Complete CREATE|UNIQUE INDEX [] ON with a list of relations that + * indexes can be created on. This must not match any CONCURRENTLY form + * (not even one with an index named "index"), or it would offer + * partitioned tables there. */ - else if (TailMatches("INDEX|CONCURRENTLY", MatchAny, "ON") || - TailMatches("INDEX|CONCURRENTLY", "ON")) + else if (TailMatches("CREATE|UNIQUE", "INDEX", MatchAnyExcept("CONCURRENTLY"), "ON") || + TailMatches("CREATE|UNIQUE", "INDEX", "ON")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables); /* @@ -3563,7 +3589,8 @@ match_previous_words(int pattern_id, "ON"); /* Complete CREATE|UNIQUE INDEX [CONCURRENTLY] with "ON" */ else if (TailMatches("CREATE|UNIQUE", "INDEX", MatchAny) || - TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY", MatchAny)) + Matches("CREATE", "INDEX", "CONCURRENTLY", MatchAny) || + Matches("CREATE", "UNIQUE", "INDEX", "CONCURRENTLY", MatchAny)) COMPLETE_WITH("ON"); /* @@ -3571,10 +3598,10 @@ match_previous_words(int pattern_id, * should really be in parens) */ else if (TailMatches("INDEX", MatchAny, "ON", MatchAny) || - TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny)) + TailMatches("INDEX", "ON", MatchAny)) COMPLETE_WITH("(", "USING"); else if (TailMatches("INDEX", MatchAny, "ON", MatchAny, "(") || - TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny, "(")) + TailMatches("INDEX", "ON", MatchAny, "(")) COMPLETE_WITH_ATTR(prev2_wd); /* same if you put in USING */ else if (TailMatches("ON", MatchAny, "USING", MatchAny, "(")) -- 2.43.0