postgres.git / summary / log / commit / refs

commit    e3584e457db2dc9fa0310bfd8d9d07fe65a281d3
Author:   Masahiko Sawada <msawada@postgresql.org>
Date:     Wed Jul 09 12:45:23 2025 +0000
First released in: 14.19, 15.14, 16.10, 17.6, 18.0

    Fix tab-completion for COPY and \copy options.
    
    Commit c273d9d8ce4 reworked tab-completion of COPY and \copy in psql
    and added support for completing options within WITH clauses. However,
    the same COPY options were suggested for both COPY TO and COPY FROM
    commands, even though some options are only valid for one or the
    other.
    
    This commit separates the COPY options for COPY FROM and COPY TO
    commands to provide more accurate auto-completion suggestions.
    
    Back-patch to v14 where tab-completion for COPY and \copy options
    within WITH clauses was first supported.
    
    Author: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
    Reviewed-by: Yugo Nagata <nagata@sraoss.co.jp>
    Discussion: https://postgr.es/m/079e7a2c801f252ae8d522b772790ed7@oss.nttdata.com
    Backpatch-through: 14


src/bin/psql/tab-complete.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index f6e7011c21d..a7ac4336428 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1143,6 +1143,18 @@ static const SchemaQuery Query_for_trigger_of_table = { " FROM pg_catalog.pg_timezone_names() "\ " WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')" +/* COPY options shared between FROM and TO */ +#define Copy_common_options \ +"DELIMITER", "ENCODING", "ESCAPE", "FORMAT", "HEADER", "NULL", "QUOTE" + +/* COPY FROM options */ +#define Copy_from_options \ +Copy_common_options, "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE" + +/* COPY TO options */ +#define Copy_to_options \ +Copy_common_options, "FORCE_QUOTE" + /* * These object types were introduced later than our support cutoff of * server version 9.2. We use the VersionedQuery infrastructure so that @@ -2738,11 +2750,13 @@ psql_completion(const char *text, int start, int end) else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny)) COMPLETE_WITH("WITH (", "WHERE"); - /* Complete COPY <sth> FROM|TO filename WITH ( */ - else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(")) - COMPLETE_WITH("FORMAT", "FREEZE", "DELIMITER", "NULL", - "HEADER", "QUOTE", "ESCAPE", "FORCE_QUOTE", - "FORCE_NOT_NULL", "FORCE_NULL", "ENCODING"); + /* Complete COPY <sth> FROM filename WITH ( */ + else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(")) + COMPLETE_WITH(Copy_from_options); + + /* Complete COPY <sth> TO filename WITH ( */ + else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAny, "WITH", "(")) + COMPLETE_WITH(Copy_to_options); /* Complete COPY <sth> FROM|TO filename WITH (FORMAT */ else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "FORMAT")) [parent: c65c36ab5195]