From b55e6afb9be89f68d5ea5dc67b96a1a7370f6fe4 Mon Sep 17 00:00:00 2001 From: Abed Alawiyeh Date: Sat, 5 Dec 2026 15:30:42 +0200 Subject: [PATCH v1] psql: Add tab completion for INSERT ... ON CONFLICT psql offered no completion after ON CONFLICT, and "DO UPDATE " fell through to the generic UPDATE rule and offered table names. Complete the conflict target forms ("(" and ON CONSTRAINT), DO, and the conflict actions. Without a conflict target only DO NOTHING is offered, because DO UPDATE and DO SELECT require one. For DO SELECT, added by commit 88327092, also complete the optional lock strength and the RETURNING clause it requires. Also stop offering configuration parameters after "DO UPDATE SET" and MERGE's "THEN UPDATE SET", where a column name is expected. --- src/bin/psql/t/010_tab_completion.pl | 43 ++++++++++++++++++++++ src/bin/psql/tab-complete.in.c | 55 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl index 1d2e5f5b..13dfc5d4 100644 --- a/src/bin/psql/t/010_tab_completion.pl +++ b/src/bin/psql/t/010_tab_completion.pl @@ -422,6 +422,49 @@ check_completion( clear_line(); +# check INSERT ... ON CONFLICT completion +check_completion( + "INSERT INTO tab1 VALUES (1) ON CONFLICT ON C\t", + qr/ON CONFLICT ON CONSTRAINT /, + "complete ON CONFLICT ON with CONSTRAINT"); + +clear_query(); + +check_completion( + "INSERT INTO tab1 VALUES (1) ON CONFLICT DO \t", + qr/ON CONFLICT DO NOTHING /, + "complete ON CONFLICT DO without a target with NOTHING"); + +clear_query(); + +check_completion( + "INSERT INTO tab1 VALUES (1) ON CONFLICT ON CONSTRAINT tab1_pkey DO S\t", + qr/tab1_pkey DO SELECT /, + "complete ON CONFLICT ON CONSTRAINT name DO with SELECT"); + +clear_query(); + +check_completion( + "INSERT INTO tab1 VALUES (1) ON CONFLICT (c1) DO U\t", + qr/\(c1\) DO UPDATE SET /, + "complete ON CONFLICT (target) DO with UPDATE SET"); + +clear_query(); + +check_completion( + "INSERT INTO tab1 VALUES (1) ON CONFLICT (c1) DO SELECT FOR N\t", + qr/FOR NO KEY UPDATE /, + "complete ON CONFLICT DO SELECT FOR with a lock strength"); + +clear_query(); + +check_completion( + "INSERT INTO tab1 VALUES (1) ON CONFLICT (c1) DO SELECT FOR SHARE R\t", + qr/FOR SHARE RETURNING /, + "complete RETURNING after the DO SELECT lock strength"); + +clear_query(); + # send psql an explicit \q to shut it down, else pty won't close properly $h->quit or die "psql returned $?"; diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c index 96de78cd..004663fb 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -4874,6 +4874,60 @@ match_previous_words(int pattern_id, else if (TailMatches("VALUES") && !TailMatches("DEFAULT", "VALUES")) COMPLETE_WITH("("); + /* Complete ON CONFLICT with a conflict target or DO */ + else if (TailMatches("ON", "CONFLICT")) + COMPLETE_WITH("(", "ON CONSTRAINT", "DO"); + else if (TailMatches("ON", "CONFLICT", "ON")) + COMPLETE_WITH("CONSTRAINT"); + else if (TailMatches("ON", "CONFLICT", "(*)")) + COMPLETE_WITH("WHERE", "DO"); + else if (TailMatches("ON", "CONFLICT", "ON", "CONSTRAINT", MatchAny)) + COMPLETE_WITH("DO"); + + /* + * Complete the conflict action. DO UPDATE and DO SELECT need a conflict + * target, so offer only DO NOTHING without one. + */ + else if (TailMatches("ON", "CONFLICT", "DO")) + COMPLETE_WITH("NOTHING"); + else if (TailMatches("ON", "CONFLICT", "(*)", "DO") || + TailMatches("ON", "CONFLICT", "ON", "CONSTRAINT", MatchAny, "DO")) + COMPLETE_WITH("NOTHING", "SELECT", "UPDATE SET"); + else if (TailMatches("ON", "CONFLICT", "DO", "NOTHING") || + TailMatches("ON", "CONFLICT", "(*)", "DO", "NOTHING") || + TailMatches("CONFLICT", "ON", "CONSTRAINT", MatchAny, "DO", "NOTHING")) + COMPLETE_WITH("RETURNING"); + + /* + * Complete ON CONFLICT DO UPDATE and DO SELECT. In CREATE RULE, DO is + * followed by the rule's action, which can be an UPDATE or SELECT + * command, so leave that alone. + */ + else if (TailMatches("DO", "UPDATE") && + !HeadMatches("CREATE", "RULE") && + !HeadMatches("CREATE", "OR", "REPLACE", "RULE")) + COMPLETE_WITH("SET"); + else if (TailMatches("DO", "SELECT") && + !HeadMatches("CREATE", "RULE") && + !HeadMatches("CREATE", "OR", "REPLACE", "RULE")) + COMPLETE_WITH("FOR", "RETURNING", "WHERE"); + else if (TailMatches("DO", "SELECT", "FOR") && + !HeadMatches("CREATE", "RULE") && + !HeadMatches("CREATE", "OR", "REPLACE", "RULE")) + COMPLETE_WITH("KEY SHARE", "NO KEY UPDATE", "SHARE", "UPDATE"); + else if (TailMatches("DO", "SELECT", "FOR", "UPDATE|SHARE") && + !HeadMatches("CREATE", "RULE") && + !HeadMatches("CREATE", "OR", "REPLACE", "RULE")) + COMPLETE_WITH("RETURNING", "WHERE"); + else if (TailMatches("DO", "SELECT", "FOR", "KEY", "SHARE") && + !HeadMatches("CREATE", "RULE") && + !HeadMatches("CREATE", "OR", "REPLACE", "RULE")) + COMPLETE_WITH("RETURNING", "WHERE"); + else if (TailMatches("DO", "SELECT", "FOR", "NO", "KEY", "UPDATE") && + !HeadMatches("CREATE", "RULE") && + !HeadMatches("CREATE", "OR", "REPLACE", "RULE")) + COMPLETE_WITH("RETURNING", "WHERE"); + /* LOCK */ /* Complete LOCK [TABLE] [ONLY] with a list of tables */ else if (Matches("LOCK")) @@ -5187,6 +5241,7 @@ match_previous_words(int pattern_id, /* Complete with a variable name */ else if (TailMatches("SET|RESET") && !TailMatches("UPDATE", MatchAny, "SET") && + !TailMatches("DO|THEN", "UPDATE", "SET") && !TailMatches("ALTER", "DATABASE|USER|ROLE", MatchAny, "RESET")) COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars, "CONSTRAINTS", -- 2.53.0.windows.2