Improve tab completion for COPY

Started by torikoshia8 months ago18 messages
#1torikoshia
torikoshia@oss.nttdata.com
1 attachment(s)

Hi,

I noticed that REJECT_LIMIT, an option available for COPY FROM, is not
currently supported in psql's tab completion.

Additionally, some options are only valid for COPY FROM or COPY TO, i.e.
FREEZE, ON_ERROR, FORCE_QUOTE, but psql currently suggests them for both
COPY FROM and COPY TO.
As the number of COPY options continues to grow, I feel that having
irrelevant suggestions makes tab completion noisier.

Attached patch splits the tab completion rules between COPY FROM and
COPY TO, so that only the appropriate options are suggested for each.

What do you think?

Regards,

--
Atsushi Torikoshi
Seconded from NTT DATA GROUP CORPORATION to SRA OSS K.K.

Attachments:

v1-0001-Improve-tab-completion-for-COPY-WITH-options.patchtext/x-diff; name=v1-0001-Improve-tab-completion-for-COPY-WITH-options.patchDownload
From 9efecf30f4c25f7c0457b679338f9cf4bbf03ec5 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshi@sraoss.co.jp>
Date: Wed, 7 May 2025 15:14:15 +0900
Subject: [PATCH v1] Improve tab completion for COPY WITH options

Split tab completion rules for COPY FROM and COPY TO to provide
accurate suggestions.
Also, add completion for the REJECT_LIMIT option, which is valid
only for COPY FROM.
---
 src/bin/psql/tab-complete.in.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index c916b9299a..d812fcab12 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -3278,23 +3278,27 @@ match_previous_words(int pattern_id,
 	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",
+	/* Complete COPY <sth> FROM filename WITH ( */
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "("))
+		COMPLETE_WITH("FORMAT", "FREEZE", "DELIMITER", "NULL", "HEADER", "QUOTE", "ESCAPE",
 					  "FORCE_NOT_NULL", "FORCE_NULL", "ENCODING", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY");
+					  "ON_ERROR", "LOG_VERBOSITY", "REJECT_LIMIT");
+
+	/* Complete COPY <sth> TO filename WITH ( */
+	else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAny, "WITH", "("))
+		COMPLETE_WITH("FORMAT", "DELIMITER", "NULL", "HEADER", "QUOTE", "ESCAPE",
+					  "FORCE_QUOTE", "ENCODING");
 
 	/* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
 	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "FORMAT"))
 		COMPLETE_WITH("binary", "csv", "text");
 
 	/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
 		COMPLETE_WITH("stop", "ignore");
 
 	/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
 		COMPLETE_WITH("silent", "default", "verbose");
 
 	/* Complete COPY <sth> FROM <sth> WITH (<options>) */

base-commit: b560ce7884a32ba6409adab340b9732dcd210284
-- 
2.43.0

#2Yugo Nagata
nagata@sraoss.co.jp
In reply to: torikoshia (#1)
Re: Improve tab completion for COPY

On Wed, 7 May 2025 15:39:26 +0900
torikoshia <torikoshia@oss.nttdata.com> wrote:

Hi,

I noticed that REJECT_LIMIT, an option available for COPY FROM, is not
currently supported in psql's tab completion.

Additionally, some options are only valid for COPY FROM or COPY TO, i.e.
FREEZE, ON_ERROR, FORCE_QUOTE, but psql currently suggests them for both
COPY FROM and COPY TO.
As the number of COPY options continues to grow, I feel that having
irrelevant suggestions makes tab completion noisier.

Indeed eliminating irrelevant suggestions would improve user experience,
but I think there is a drawback that it increases code maintenance for
adding options used both in COPY FROM and TO. This might be trivial until
the number of common options are small as now, though.

Perhaps, the redundant code could be reduced by preparing a list (an array
of const char*) containing common options part, then appending options
specific to each mode using some function like kind of append_variable_names,
and passing these lists to COMPLETE_WITH_LIST.

Regards,
Yugo Nagata

Attached patch splits the tab completion rules between COPY FROM and
COPY TO, so that only the appropriate options are suggested for each.

What do you think?

Regards,

--
Atsushi Torikoshi
Seconded from NTT DATA GROUP CORPORATION to SRA OSS K.K.

--
Yugo Nagata <nagata@sraoss.co.jp>

#3Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Yugo Nagata (#2)
Re: Improve tab completion for COPY

On Wed, May 7, 2025 at 6:23 AM Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Wed, 7 May 2025 15:39:26 +0900
torikoshia <torikoshia@oss.nttdata.com> wrote:

Hi,

I noticed that REJECT_LIMIT, an option available for COPY FROM, is not
currently supported in psql's tab completion.

Additionally, some options are only valid for COPY FROM or COPY TO, i.e.
FREEZE, ON_ERROR, FORCE_QUOTE, but psql currently suggests them for both
COPY FROM and COPY TO.
As the number of COPY options continues to grow, I feel that having
irrelevant suggestions makes tab completion noisier.

Indeed eliminating irrelevant suggestions would improve user experience,

+1

but I think there is a drawback that it increases code maintenance for
adding options used both in COPY FROM and TO. This might be trivial until
the number of common options are small as now, though.

Perhaps, the redundant code could be reduced by preparing a list (an array
of const char*) containing common options part, then appending options
specific to each mode using some function like kind of append_variable_names,
and passing these lists to COMPLETE_WITH_LIST.

Or we can simply #define the common option list and #define two lists
for COPY TO and COPY FROM by concatenating the common option list,
like we do for ALTER PROCEDURE/ROUTINE/FUNCTION options.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#4Yugo Nagata
nagata@sraoss.co.jp
In reply to: Masahiko Sawada (#3)
Re: Improve tab completion for COPY

On Wed, 7 May 2025 14:36:35 -0700
Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Wed, May 7, 2025 at 6:23 AM Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Wed, 7 May 2025 15:39:26 +0900
torikoshia <torikoshia@oss.nttdata.com> wrote:

Hi,

I noticed that REJECT_LIMIT, an option available for COPY FROM, is not
currently supported in psql's tab completion.

Additionally, some options are only valid for COPY FROM or COPY TO, i.e.
FREEZE, ON_ERROR, FORCE_QUOTE, but psql currently suggests them for both
COPY FROM and COPY TO.
As the number of COPY options continues to grow, I feel that having
irrelevant suggestions makes tab completion noisier.

Indeed eliminating irrelevant suggestions would improve user experience,

+1

but I think there is a drawback that it increases code maintenance for
adding options used both in COPY FROM and TO. This might be trivial until
the number of common options are small as now, though.

Perhaps, the redundant code could be reduced by preparing a list (an array
of const char*) containing common options part, then appending options
specific to each mode using some function like kind of append_variable_names,
and passing these lists to COMPLETE_WITH_LIST.

Or we can simply #define the common option list and #define two lists
for COPY TO and COPY FROM by concatenating the common option list,
like we do for ALTER PROCEDURE/ROUTINE/FUNCTION options.

+1

I overlooked this although I looked for an existing solution in the tab
complement codes.

Regards,
Yugo Nagata

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

--
Yugo Nagata <nagata@sraoss.co.jp>

#5torikoshia
torikoshia@oss.nttdata.com
In reply to: Yugo Nagata (#4)
1 attachment(s)
Re: Improve tab completion for COPY

Thanks for your comments!

On 2025-05-08 08:53, Yugo Nagata wrote:

On Wed, 7 May 2025 14:36:35 -0700
Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Wed, May 7, 2025 at 6:23 AM Yugo Nagata <nagata@sraoss.co.jp>
wrote:

On Wed, 7 May 2025 15:39:26 +0900
torikoshia <torikoshia@oss.nttdata.com> wrote:

Hi,

I noticed that REJECT_LIMIT, an option available for COPY FROM, is not
currently supported in psql's tab completion.

Additionally, some options are only valid for COPY FROM or COPY TO, i.e.
FREEZE, ON_ERROR, FORCE_QUOTE, but psql currently suggests them for both
COPY FROM and COPY TO.
As the number of COPY options continues to grow, I feel that having
irrelevant suggestions makes tab completion noisier.

Indeed eliminating irrelevant suggestions would improve user experience,

+1

but I think there is a drawback that it increases code maintenance for
adding options used both in COPY FROM and TO. This might be trivial until
the number of common options are small as now, though.

Perhaps, the redundant code could be reduced by preparing a list (an array
of const char*) containing common options part, then appending options
specific to each mode using some function like kind of append_variable_names,
and passing these lists to COMPLETE_WITH_LIST.

Or we can simply #define the common option list and #define two lists
for COPY TO and COPY FROM by concatenating the common option list,
like we do for ALTER PROCEDURE/ROUTINE/FUNCTION options.

+1

Agreed.
I think attached patch implemented the suggested way.

--
Regards,

--
Atsushi Torikoshi
Seconded from NTT DATA GROUP CORPORATION to SRA OSS K.K.

Attachments:

v2-0001-Improve-tab-completion-for-COPY-WITH-options.patchtext/x-diff; name=v2-0001-Improve-tab-completion-for-COPY-WITH-options.patchDownload
From 4e1ed3b2a4c7283ed3b7803c7071768ed5c22fc4 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshi@sraoss.co.jp>
Date: Thu, 8 May 2025 21:11:20 +0900
Subject: [PATCH v2] Improve tab completion for COPY options

Currently, COPY FROM and TO share the same tab completion rules,
even though some options are only valid for one or the other.

This patch separates the tab completion rules for COPY FROM and
COPY TO to provide more accurate suggestions.
It also adds tab completion support for REJECT_LIMIT option,
which is valid only for COPY FROM.

---
 src/bin/psql/tab-complete.in.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index c916b9299a..2111f72c91 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -1190,6 +1190,19 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
+
+/* 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
@@ -3278,23 +3291,24 @@ match_previous_words(int pattern_id,
 	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", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY");
+	/* 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"))
 		COMPLETE_WITH("binary", "csv", "text");
 
 	/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
 		COMPLETE_WITH("stop", "ignore");
 
 	/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
 		COMPLETE_WITH("silent", "default", "verbose");
 
 	/* Complete COPY <sth> FROM <sth> WITH (<options>) */

base-commit: 8fcc6487809efa5508a4b70049402236a53be84d
-- 
2.43.0

#6Yugo Nagata
nagata@sraoss.co.jp
In reply to: torikoshia (#5)
Re: Improve tab completion for COPY

On Thu, 8 May 2025 21:39:10 +0900
torikoshia <torikoshia@oss.nttdata.com> wrote:

Thanks for your comments!

On 2025-05-08 08:53, Yugo Nagata wrote:

On Wed, 7 May 2025 14:36:35 -0700
Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Wed, May 7, 2025 at 6:23 AM Yugo Nagata <nagata@sraoss.co.jp>
wrote:

On Wed, 7 May 2025 15:39:26 +0900
torikoshia <torikoshia@oss.nttdata.com> wrote:

Hi,

I noticed that REJECT_LIMIT, an option available for COPY FROM, is not
currently supported in psql's tab completion.

Additionally, some options are only valid for COPY FROM or COPY TO, i.e.
FREEZE, ON_ERROR, FORCE_QUOTE, but psql currently suggests them for both
COPY FROM and COPY TO.
As the number of COPY options continues to grow, I feel that having
irrelevant suggestions makes tab completion noisier.

Indeed eliminating irrelevant suggestions would improve user experience,

+1

but I think there is a drawback that it increases code maintenance for
adding options used both in COPY FROM and TO. This might be trivial until
the number of common options are small as now, though.

Perhaps, the redundant code could be reduced by preparing a list (an array
of const char*) containing common options part, then appending options
specific to each mode using some function like kind of append_variable_names,
and passing these lists to COMPLETE_WITH_LIST.

Or we can simply #define the common option list and #define two lists
for COPY TO and COPY FROM by concatenating the common option list,
like we do for ALTER PROCEDURE/ROUTINE/FUNCTION options.

+1

Agreed.
I think attached patch implemented the suggested way.

Thank you for updating the patch.
It looks good and I confirmed that this works as expected.

Regards,
Yugo Nagata

--
Regards,

--
Atsushi Torikoshi
Seconded from NTT DATA GROUP CORPORATION to SRA OSS K.K.

--
Yugo Nagata <nagata@sraoss.co.jp>

#7torikoshia
torikoshia@oss.nttdata.com
In reply to: Yugo Nagata (#6)
2 attachment(s)
Re: Improve tab completion for COPY

On 2025-06-03 17:58, Yugo Nagata wrote:

Thank you for updating the patch.
It looks good and I confirmed that this works as expected.

Thanks for your review!

BTW this is a small patch, but it does two things:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

While maybe (2) should be postponed to v19 or later, I think it would be
better to include (1) in v18.
So I’ve split them into separate patches accordingly.

--
Atsushi Torikoshi
Seconded from NTT DATA GROUP CORPORATION to SRA OSS K.K.

Attachments:

v3-0002-Improve-tab-completion-for-COPY-options.patchtext/x-diff; name=v3-0002-Improve-tab-completion-for-COPY-options.patchDownload
From 77b96ce98ca58d016fa984f2dbb890cc86855ebe Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Tue, 10 Jun 2025 10:55:24 +0900
Subject: [PATCH v3 2/2] Improve tab completion for COPY options

Currently, COPY FROM and TO share the same tab completion rules,
even though some options are only valid for one or the other.

This patch separates the tab completion rules for COPY FROM and
COPY TO to provide more accurate suggestions.
---
 src/bin/psql/tab-complete.in.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index c0ba50019e..a1707100ac 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -1190,6 +1190,19 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
+
+/* 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
@@ -3284,23 +3297,24 @@ match_previous_words(int pattern_id,
 	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", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY", "REJECT_LIMIT");
+	/* 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"))
 		COMPLETE_WITH("binary", "csv", "text");
 
 	/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
 		COMPLETE_WITH("stop", "ignore");
 
 	/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
 		COMPLETE_WITH("silent", "default", "verbose");
 
 	/* Complete COPY <sth> FROM <sth> WITH (<options>) */
-- 
2.43.0

v3-0001-Add-tab-completion-for-COPY-REJECT_LIMIT-option.patchtext/x-diff; name=v3-0001-Add-tab-completion-for-COPY-REJECT_LIMIT-option.patchDownload
From b1f7076316e858a13f8bb1eabc7320938c613398 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Tue, 10 Jun 2025 10:45:28 +0900
Subject: [PATCH v3 1/2] Add tab completion for COPY REJECT_LIMIT option

REJECT_LIMIT is a valid COPY option but was missing from tab
completion.
---
 src/bin/psql/tab-complete.in.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index ec65ab79fe..c0ba50019e 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -3289,7 +3289,7 @@ match_previous_words(int pattern_id,
 		COMPLETE_WITH("FORMAT", "FREEZE", "DELIMITER", "NULL",
 					  "HEADER", "QUOTE", "ESCAPE", "FORCE_QUOTE",
 					  "FORCE_NOT_NULL", "FORCE_NULL", "ENCODING", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY");
+					  "ON_ERROR", "LOG_VERBOSITY", "REJECT_LIMIT");
 
 	/* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
 	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "FORMAT"))

base-commit: 166b4f4560d68e2bcf0f82eb898ac5ee15147b82
-- 
2.43.0

#8Yugo Nagata
nagata@sraoss.co.jp
In reply to: torikoshia (#7)
Re: Improve tab completion for COPY

On Tue, 10 Jun 2025 11:15:09 +0900
torikoshia <torikoshia@oss.nttdata.com> wrote:

On 2025-06-03 17:58, Yugo Nagata wrote:

Thank you for updating the patch.
It looks good and I confirmed that this works as expected.

Thanks for your review!

BTW this is a small patch, but it does two things:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

While maybe (2) should be postponed to v19 or later, I think it would be
better to include (1) in v18.
So I’ve split them into separate patches accordingly.

I am not convinced whether (1) should be regarded as a v18-related oversight
or a new feature, but if this is a oversight, this should be added to the open
items list?

https://wiki.postgresql.org/wiki/PostgreSQL_18_Open_Items

Regards,
Yugo Nagata

--
Yugo Nagata <nagata@sraoss.co.jp>

#9Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Yugo Nagata (#8)
Re: Improve tab completion for COPY

On Mon, Jun 9, 2025 at 7:59 PM Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Tue, 10 Jun 2025 11:15:09 +0900
torikoshia <torikoshia@oss.nttdata.com> wrote:

On 2025-06-03 17:58, Yugo Nagata wrote:

Thank you for updating the patch.
It looks good and I confirmed that this works as expected.

Thanks for your review!

BTW this is a small patch, but it does two things:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

While maybe (2) should be postponed to v19 or later, I think it would be
better to include (1) in v18.
So I’ve split them into separate patches accordingly.

I am not convinced whether (1) should be regarded as a v18-related oversight
or a new feature, but if this is a oversight, this should be added to the open
items list?

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#10Nathan Bossart
nathandbossart@gmail.com
In reply to: Masahiko Sawada (#9)
Re: Improve tab completion for COPY

On Tue, Jun 10, 2025 at 12:37:48PM -0700, Masahiko Sawada wrote:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

[...]

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

0001 sure seems like an oversight in commit 4ac2a9b to me. In any case, I
argued that we should fix tab completion for unlogged partitioned tables in
v18 [0]/messages/by-id/aEH4XYwrlyvjZUvE@nathan, and this seems pretty similar.

TBH I'd even say that 0002 is fixing a bug and could be back-patched. I've
added rmt@ here in case Tomas or Heikki feel differently.

[0]: /messages/by-id/aEH4XYwrlyvjZUvE@nathan

--
nathan

#11Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Nathan Bossart (#10)
Re: Improve tab completion for COPY

On 2025/06/11 5:33, Nathan Bossart wrote:

On Tue, Jun 10, 2025 at 12:37:48PM -0700, Masahiko Sawada wrote:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

[...]

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

0001 sure seems like an oversight in commit 4ac2a9b to me. In any case, I
argued that we should fix tab completion for unlogged partitioned tables in
v18 [0], and this seems pretty similar.

BTW, I also have a similar patch [1]/messages/by-id/CACJufxFxnSkikp+GormAGHcMTX1YH2HRXW1+3dJM9w7yY9hdsg@mail.gmail.com in my queue that I'm planning to commit.
Materialized views have been allowed in COPY TO since v18 (commit 534874fac0b),
and that patch improves tab completion to include them. For the same reason
discussed here, it might make sense to commit that patch in v18 as well.

Regards,

[1]: /messages/by-id/CACJufxFxnSkikp+GormAGHcMTX1YH2HRXW1+3dJM9w7yY9hdsg@mail.gmail.com

--
Fujii Masao
NTT DATA Japan Corporation

#12Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Nathan Bossart (#10)
Re: Improve tab completion for COPY

On Tue, Jun 10, 2025 at 1:33 PM Nathan Bossart <nathandbossart@gmail.com> wrote:

On Tue, Jun 10, 2025 at 12:37:48PM -0700, Masahiko Sawada wrote:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

[...]

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

0001 sure seems like an oversight in commit 4ac2a9b to me. In any case, I
argued that we should fix tab completion for unlogged partitioned tables in
v18 [0], and this seems pretty similar.

TBH I'd even say that 0002 is fixing a bug and could be back-patched. I've
added rmt@ here in case Tomas or Heikki feel differently.

Thank you for your confirmation. I've just pushed the change (1) and
am waiting for more comments on the change (2).

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#13Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Masahiko Sawada (#12)
Re: Improve tab completion for COPY

On Thu, Jun 12, 2025 at 3:47 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Tue, Jun 10, 2025 at 1:33 PM Nathan Bossart <nathandbossart@gmail.com> wrote:

On Tue, Jun 10, 2025 at 12:37:48PM -0700, Masahiko Sawada wrote:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

[...]

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

0001 sure seems like an oversight in commit 4ac2a9b to me. In any case, I
argued that we should fix tab completion for unlogged partitioned tables in
v18 [0], and this seems pretty similar.

TBH I'd even say that 0002 is fixing a bug and could be back-patched. I've
added rmt@ here in case Tomas or Heikki feel differently.

Thank you for your confirmation. I've just pushed the change (1) and
am waiting for more comments on the change (2).

Thinking of the 0002 patch, I'm also inclined to agree that this fixes
a bogus tab completion behavior for COPY command and can be
back-patched to v14. In v14, c273d9d8ce reworked the tab completion
for COPY command and supports the completion for the options of FORMAT
within a WITH clause so we cannot back-patch it to v13.

Torikoshi-san, could you please prepare the patch for other branches
too if you agree with this direction?

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#14torikoshia
torikoshia@oss.nttdata.com
In reply to: Masahiko Sawada (#13)
5 attachment(s)
Re: Improve tab completion for COPY

On 2025-07-01 14:20, Masahiko Sawada wrote:

On Thu, Jun 12, 2025 at 3:47 AM Masahiko Sawada <sawada.mshk@gmail.com>
wrote:

On Tue, Jun 10, 2025 at 1:33 PM Nathan Bossart
<nathandbossart@gmail.com> wrote:

On Tue, Jun 10, 2025 at 12:37:48PM -0700, Masahiko Sawada wrote:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

[...]

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

0001 sure seems like an oversight in commit 4ac2a9b to me. In any case, I
argued that we should fix tab completion for unlogged partitioned tables in
v18 [0], and this seems pretty similar.

TBH I'd even say that 0002 is fixing a bug and could be back-patched. I've
added rmt@ here in case Tomas or Heikki feel differently.

Thank you for your confirmation. I've just pushed the change (1) and
am waiting for more comments on the change (2).

Thinking of the 0002 patch, I'm also inclined to agree that this fixes
a bogus tab completion behavior for COPY command and can be
back-patched to v14. In v14, c273d9d8ce reworked the tab completion
for COPY command and supports the completion for the options of FORMAT
within a WITH clause so we cannot back-patch it to v13.

Agreed.

Torikoshi-san, could you please prepare the patch for other branches
too if you agree with this direction?

Sure! Attached patches.

--
Regards,

--
Atsushi Torikoshi
Seconded from NTT DATA Japan Corporation to SRA OSS K.K.

Attachments:

v3-0002-Improve-tab-completion-for-COPY-options_v16.patchtext/x-diff; name=v3-0002-Improve-tab-completion-for-COPY-options_v16.patchDownload
From e50f7f057bf41e5cd4df26a21abcb17c34601e1c Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia.tech@gmail.com>
Date: Wed, 2 Jul 2025 11:51:32 +0900
Subject: [PATCH v3 2/2] Improve tab completion for COPY options

Currently, COPY FROM and TO share the same tab completion rules,
even though some options are only valid for one or the other.

This patch separates the tab completion rules for COPY FROM and
COPY TO to provide more accurate suggestions.
---
 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 a1aa946b300..23d89d0720f 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1164,6 +1164,18 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "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
@@ -2854,11 +2866,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", "DEFAULT");
+	/* 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"))

base-commit: 779f24c0b7be5d66504a30c6d15ab0430bdf59e2
-- 
2.43.0

v3-0002-Improve-tab-completion-for-COPY-options_v17.patchtext/x-diff; name=v3-0002-Improve-tab-completion-for-COPY-options_v17.patchDownload
From 3e35713147b0fb09e262a514abc06482f71622c2 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 13:03:49 +0900
Subject: [PATCH v3 2/2] Improve tab completion for COPY options

Currently, COPY FROM and TO share the same tab completion rules,
even though some options are only valid for one or the other.

This patch separates the tab completion rules for COPY FROM and
COPY TO to provide more accurate suggestions.
---
 src/bin/psql/tab-complete.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 1bd01ff865f..c1faed53a01 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1169,6 +1169,19 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
+
+/* 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
@@ -2899,23 +2912,24 @@ 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", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY");
+	/* 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"))
 		COMPLETE_WITH("binary", "csv", "text");
 
 	/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
 		COMPLETE_WITH("stop", "ignore");
 
 	/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
 		COMPLETE_WITH("default", "verbose");
 
 	/* Complete COPY <sth> FROM <sth> WITH (<options>) */

base-commit: c6d0ef160e948b43222020e6199977c88969537b
-- 
2.43.0

v3-0002-Improve-tab-completion-for-COPY-options_v15.patchtext/x-diff; name=v3-0002-Improve-tab-completion-for-COPY-options_v15.patchDownload
From 565c1d0fa9bfb9590d4ba386690bda5a6eaae5e5 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 14:13:37 +0900
Subject: [PATCH v3 2/2] Improve tab completion for COPY options

Currently, COPY FROM and TO share the same tab completion rules,
even though some options are only valid for one or the other.

This patch separates the tab completion rules for COPY FROM and
COPY TO to provide more accurate suggestions.
---
 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"))

base-commit: 37b1fce858672bcd0350c24a9c5e4689de3a6cb7
-- 
2.43.0

v3-0002-Improve-tab-completion-for-COPY-options_v14.patchtext/x-diff; name=v3-0002-Improve-tab-completion-for-COPY-options_v14.patchDownload
From 438fe2650181ce34e66968b4667dff7ba90663cb Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 14:26:32 +0900
Subject: [PATCH v3 2/2] Improve tab completion for COPY options

Currently, COPY FROM and TO share the same tab completion rules,
even though some options are only valid for one or the other.

This patch separates the tab completion rules for COPY FROM and
COPY TO to provide more accurate suggestions.
---
 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 5b909d6f3a0..7e36cb11973 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1022,6 +1022,18 @@ static const SchemaQuery Query_for_list_of_collations = {
 "   FROM pg_catalog.pg_cursors "\
 "  WHERE substring(pg_catalog.quote_ident(name),1,%d)='%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 7.4.  We use the VersionedQuery infrastructure so that
@@ -2447,11 +2459,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"))

base-commit: e35a0174a9d29c4dac0f2f6f72ee804e83e31825
-- 
2.43.0

v3-0002-Improve-tab-completion-for-COPY-options_v18.patchtext/x-diff; name=v3-0002-Improve-tab-completion-for-COPY-options_v18.patchDownload
From 30501386cd660e92810a417009c879da3225d085 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 11:39:15 +0900
Subject: [PATCH v3 2/2] Improve tab completion for COPY options

Currently, COPY FROM and TO share the same tab completion rules,
even though some options are only valid for one or the other.

This patch separates the tab completion rules for COPY FROM and
COPY TO to provide more accurate suggestions.

---
 src/bin/psql/tab-complete.in.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 908eef97c6e..f2734f8f273 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -1190,6 +1190,19 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
+
+/* 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
@@ -3284,23 +3297,24 @@ match_previous_words(int pattern_id,
 	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", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY", "REJECT_LIMIT");
+	/* 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"))
 		COMPLETE_WITH("binary", "csv", "text");
 
 	/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
 		COMPLETE_WITH("stop", "ignore");
 
 	/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
 		COMPLETE_WITH("silent", "default", "verbose");
 
 	/* Complete COPY <sth> FROM <sth> WITH (<options>) */

base-commit: b897a58556d8c29366ae980d65bf5e90daf7098e
-- 
2.43.0

#15Masahiko Sawada
sawada.mshk@gmail.com
In reply to: torikoshia (#14)
6 attachment(s)
Re: Improve tab completion for COPY

On Wed, Jul 2, 2025 at 2:46 PM torikoshia <torikoshia@oss.nttdata.com> wrote:

On 2025-07-01 14:20, Masahiko Sawada wrote:

On Thu, Jun 12, 2025 at 3:47 AM Masahiko Sawada <sawada.mshk@gmail.com>
wrote:

On Tue, Jun 10, 2025 at 1:33 PM Nathan Bossart
<nathandbossart@gmail.com> wrote:

On Tue, Jun 10, 2025 at 12:37:48PM -0700, Masahiko Sawada wrote:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

[...]

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

0001 sure seems like an oversight in commit 4ac2a9b to me. In any case, I
argued that we should fix tab completion for unlogged partitioned tables in
v18 [0], and this seems pretty similar.

TBH I'd even say that 0002 is fixing a bug and could be back-patched. I've
added rmt@ here in case Tomas or Heikki feel differently.

Thank you for your confirmation. I've just pushed the change (1) and
am waiting for more comments on the change (2).

Thinking of the 0002 patch, I'm also inclined to agree that this fixes
a bogus tab completion behavior for COPY command and can be
back-patched to v14. In v14, c273d9d8ce reworked the tab completion
for COPY command and supports the completion for the options of FORMAT
within a WITH clause so we cannot back-patch it to v13.

Agreed.

Torikoshi-san, could you please prepare the patch for other branches
too if you agree with this direction?

Sure! Attached patches.

Thank you for updating the patches! The patches mostly look good to
me. As for v3-0002-Improve-tab-completion-for-COPY-options_v17.patch,
please note that we don't support REJECT_LIMIT in v17:

+/* COPY FROM options */
+#define Copy_from_options \
+Copy_common_options, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"

I've attached the updated patches that addressed the above issue and
updated the commit messages. Please review them.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

Attachments:

REL16_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchapplication/octet-stream; name=REL16_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchDownload
From 9a36c11f5a149d1f7ecbeb47b2049f535de87215 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia.tech@gmail.com>
Date: Wed, 2 Jul 2025 11:51:32 +0900
Subject: [PATCH v4] 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 a1aa946b300..23d89d0720f 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1164,6 +1164,18 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "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
@@ -2854,11 +2866,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", "DEFAULT");
+	/* 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"))
-- 
2.43.5

REL17_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchapplication/octet-stream; name=REL17_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchDownload
From de55236becad3dab38e36c854e5568f938894ee5 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 13:03:49 +0900
Subject: [PATCH v4] 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 | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 1bd01ff865f..6c62c07ce82 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1169,6 +1169,19 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR"
+
+/* 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
@@ -2899,23 +2912,24 @@ 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", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY");
+	/* 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"))
 		COMPLETE_WITH("binary", "csv", "text");
 
 	/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
 		COMPLETE_WITH("stop", "ignore");
 
 	/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
 		COMPLETE_WITH("default", "verbose");
 
 	/* Complete COPY <sth> FROM <sth> WITH (<options>) */
-- 
2.43.5

REL18_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchapplication/octet-stream; name=REL18_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchDownload
From 5243eb5e7db409bf86b5200a599816ffcda2b4b6 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 11:39:15 +0900
Subject: [PATCH v4] 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.in.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 908eef97c6e..f2734f8f273 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -1190,6 +1190,19 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
+
+/* 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
@@ -3284,23 +3297,24 @@ match_previous_words(int pattern_id,
 	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", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY", "REJECT_LIMIT");
+	/* 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"))
 		COMPLETE_WITH("binary", "csv", "text");
 
 	/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
 		COMPLETE_WITH("stop", "ignore");
 
 	/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
 		COMPLETE_WITH("silent", "default", "verbose");
 
 	/* Complete COPY <sth> FROM <sth> WITH (<options>) */
-- 
2.43.5

REL15_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchapplication/octet-stream; name=REL15_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchDownload
From 8f1a1a992f710f56c2255d8ab814ddf8153aebb8 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 14:13:37 +0900
Subject: [PATCH v4] 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"))
-- 
2.43.5

master_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchapplication/octet-stream; name=master_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchDownload
From efa563f033da0ca04411e99a2a250623465f737f Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 11:39:15 +0900
Subject: [PATCH v4] 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.in.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 8c2ea0b9587..83a9d49f37e 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -1198,6 +1198,19 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
 "STRICT", "SUPPORT"
 
+/* 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, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
+
+/* 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
@@ -3292,23 +3305,24 @@ match_previous_words(int pattern_id,
 	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", "DEFAULT",
-					  "ON_ERROR", "LOG_VERBOSITY", "REJECT_LIMIT");
+	/* 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"))
 		COMPLETE_WITH("binary", "csv", "text");
 
 	/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
 		COMPLETE_WITH("stop", "ignore");
 
 	/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
-	else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
+	else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
 		COMPLETE_WITH("silent", "default", "verbose");
 
 	/* Complete COPY <sth> FROM <sth> WITH (<options>) */
-- 
2.43.5

REL14_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchapplication/octet-stream; name=REL14_v4-0001-Fix-tab-completion-for-COPY-and-copy-options.patchDownload
From 4b0b26565813f13ab1b05b335d93ab7c67cf1376 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 2 Jul 2025 14:26:32 +0900
Subject: [PATCH v4] 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 5b909d6f3a0..7e36cb11973 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1022,6 +1022,18 @@ static const SchemaQuery Query_for_list_of_collations = {
 "   FROM pg_catalog.pg_cursors "\
 "  WHERE substring(pg_catalog.quote_ident(name),1,%d)='%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 7.4.  We use the VersionedQuery infrastructure so that
@@ -2447,11 +2459,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"))
-- 
2.43.5

#16torikoshia
torikoshia@oss.nttdata.com
In reply to: Masahiko Sawada (#15)
Re: Improve tab completion for COPY

On 2025-07-03 11:37, Masahiko Sawada wrote:

On Wed, Jul 2, 2025 at 2:46 PM torikoshia <torikoshia@oss.nttdata.com>
wrote:

On 2025-07-01 14:20, Masahiko Sawada wrote:

On Thu, Jun 12, 2025 at 3:47 AM Masahiko Sawada <sawada.mshk@gmail.com>
wrote:

On Tue, Jun 10, 2025 at 1:33 PM Nathan Bossart
<nathandbossart@gmail.com> wrote:

On Tue, Jun 10, 2025 at 12:37:48PM -0700, Masahiko Sawada wrote:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

[...]

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

0001 sure seems like an oversight in commit 4ac2a9b to me. In any case, I
argued that we should fix tab completion for unlogged partitioned tables in
v18 [0], and this seems pretty similar.

TBH I'd even say that 0002 is fixing a bug and could be back-patched. I've
added rmt@ here in case Tomas or Heikki feel differently.

Thank you for your confirmation. I've just pushed the change (1) and
am waiting for more comments on the change (2).

Thinking of the 0002 patch, I'm also inclined to agree that this fixes
a bogus tab completion behavior for COPY command and can be
back-patched to v14. In v14, c273d9d8ce reworked the tab completion
for COPY command and supports the completion for the options of FORMAT
within a WITH clause so we cannot back-patch it to v13.

Agreed.

Torikoshi-san, could you please prepare the patch for other branches
too if you agree with this direction?

Sure! Attached patches.

Thank you for updating the patches! The patches mostly look good to
me. As for v3-0002-Improve-tab-completion-for-COPY-options_v17.patch,
please note that we don't support REJECT_LIMIT in v17:

+/* COPY FROM options */
+#define Copy_from_options \
+Copy_common_options, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", 
"FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"

Thanks for your check and correction!

I've attached the updated patches that addressed the above issue and
updated the commit messages. Please review them.

Other than the fix you mentioned, the changes are the same as in the
v3-0002 patches.
I didn’t find any particular issues.

Regards,

--
Regards,

--
Atsushi Torikoshi
Seconded from NTT DATA Japan Corporation to SRA OSS K.K.

#17Masahiko Sawada
sawada.mshk@gmail.com
In reply to: torikoshia (#16)
Re: Improve tab completion for COPY

On Thu, Jul 3, 2025 at 10:30 PM torikoshia <torikoshia@oss.nttdata.com> wrote:

On 2025-07-03 11:37, Masahiko Sawada wrote:

On Wed, Jul 2, 2025 at 2:46 PM torikoshia <torikoshia@oss.nttdata.com>
wrote:

On 2025-07-01 14:20, Masahiko Sawada wrote:

On Thu, Jun 12, 2025 at 3:47 AM Masahiko Sawada <sawada.mshk@gmail.com>
wrote:

On Tue, Jun 10, 2025 at 1:33 PM Nathan Bossart
<nathandbossart@gmail.com> wrote:

On Tue, Jun 10, 2025 at 12:37:48PM -0700, Masahiko Sawada wrote:

(1) adds tab completion support for the REJECT_LIMIT option, which was
introduced in v18
(2) splits the tab completion logic between COPY FROM and COPY TO to
reflect their different options.

[...]

Given REJECT_LIMIT is a new v18 feature, it seems to me that (1) is an
oversight of this feature, but I also agree that it's not a bug and
doesn't block the release.

How does the RMT feel about the change (1)? Nathan, would you be OK with that?

0001 sure seems like an oversight in commit 4ac2a9b to me. In any case, I
argued that we should fix tab completion for unlogged partitioned tables in
v18 [0], and this seems pretty similar.

TBH I'd even say that 0002 is fixing a bug and could be back-patched. I've
added rmt@ here in case Tomas or Heikki feel differently.

Thank you for your confirmation. I've just pushed the change (1) and
am waiting for more comments on the change (2).

Thinking of the 0002 patch, I'm also inclined to agree that this fixes
a bogus tab completion behavior for COPY command and can be
back-patched to v14. In v14, c273d9d8ce reworked the tab completion
for COPY command and supports the completion for the options of FORMAT
within a WITH clause so we cannot back-patch it to v13.

Agreed.

Torikoshi-san, could you please prepare the patch for other branches
too if you agree with this direction?

Sure! Attached patches.

Thank you for updating the patches! The patches mostly look good to
me. As for v3-0002-Improve-tab-completion-for-COPY-options_v17.patch,
please note that we don't support REJECT_LIMIT in v17:

+/* COPY FROM options */
+#define Copy_from_options \
+Copy_common_options, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL",
"FREEZE", \
+"LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"

Thanks for your check and correction!

I've attached the updated patches that addressed the above issue and
updated the commit messages. Please review them.

Other than the fix you mentioned, the changes are the same as in the
v3-0002 patches.
I didn’t find any particular issues.

Thank you for reviewing the patches. I've just pushed the fix.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#18Atsushi Torikoshi
torikoshia.tech@gmail.com
In reply to: Masahiko Sawada (#17)
Re: Improve tab completion for COPY

On Wed, Jul 9, 2025 at 10:09 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

Thank you for reviewing the patches. I've just pushed the fix.

Thanks for pushing them!