From 78c7ba67fb58ff44b492a207cf1833e296547175 Mon Sep 17 00:00:00 2001
From: Sutou Kouhei <kou@clear-code.com>
Date: Thu, 27 Mar 2025 11:56:45 +0900
Subject: [PATCH v39 4/5] Use copy handlers for built-in formats

This adds copy handlers for text, csv and binary. We can simplify
Copy{To,From}GetRoutine() by this. We'll be able to remove
CopyFormatOptions::{binary,csv_mode} when we add more callbacks to
Copy{To,From}Routine and move format specific routines to
Copy{To,From}Routine::*.
---
 src/backend/commands/copy.c                   | 101 ++++++++++++------
 src/backend/commands/copyfrom.c               |  42 ++++----
 src/backend/commands/copyto.c                 |  42 ++++----
 src/include/catalog/pg_proc.dat               |  11 ++
 src/include/commands/copy.h                   |   2 +-
 src/include/commands/copyfrom_internal.h      |   6 +-
 src/include/commands/copyto_internal.h        |   6 +-
 .../test_copy_format/expected/builtin.out     |  34 ++++++
 src/test/modules/test_copy_format/meson.build |   1 +
 .../modules/test_copy_format/sql/builtin.sql  |  30 ++++++
 .../test_copy_format--1.0.sql                 |  15 +++
 11 files changed, 209 insertions(+), 81 deletions(-)
 create mode 100644 src/test/modules/test_copy_format/expected/builtin.out
 create mode 100644 src/test/modules/test_copy_format/sql/builtin.sql

diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 2539aee3a53..2bd8989b1ae 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -22,7 +22,9 @@
 #include "access/table.h"
 #include "access/xact.h"
 #include "catalog/pg_authid.h"
-#include "commands/copy.h"
+#include "commands/copyapi.h"
+#include "commands/copyto_internal.h"
+#include "commands/copyfrom_internal.h"
 #include "commands/defrem.h"
 #include "executor/executor.h"
 #include "mb/pg_wchar.h"
@@ -521,43 +523,45 @@ ProcessCopyOptions(ParseState *pstate,
 
 		if (strcmp(defel->defname, "format") == 0)
 		{
-			char	   *fmt = defGetString(defel);
+			char	   *format = defGetString(defel);
+			List	   *qualified_format;
+			char	   *schema;
+			char	   *fmt;
+			Oid			arg_types[1];
+			Oid			handler = InvalidOid;
 
 			if (format_specified)
 				errorConflictingDefElem(defel, pstate);
 			format_specified = true;
-			if (strcmp(fmt, "text") == 0)
-				 /* default format */ ;
-			else if (strcmp(fmt, "csv") == 0)
-				opts_out->csv_mode = true;
-			else if (strcmp(fmt, "binary") == 0)
-				opts_out->binary = true;
-			else
+
+			qualified_format = stringToQualifiedNameList(format, NULL);
+			DeconstructQualifiedName(qualified_format, &schema, &fmt);
+			if (!schema || strcmp(schema, "pg_catalog") == 0)
 			{
-				List	   *qualified_format;
-				Oid			arg_types[1];
-				Oid			handler = InvalidOid;
-
-				qualified_format = stringToQualifiedNameList(fmt, NULL);
-				arg_types[0] = INTERNALOID;
-				handler = LookupFuncName(qualified_format, 1,
-										 arg_types, true);
-				if (!OidIsValid(handler))
-					ereport(ERROR,
-							(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-							 errmsg("COPY format \"%s\" not recognized", fmt),
-							 parser_errposition(pstate, defel->location)));
-
-				/* check that handler has correct return type */
-				if (get_func_rettype(handler) != COPY_HANDLEROID)
-					ereport(ERROR,
-							(errcode(ERRCODE_WRONG_OBJECT_TYPE),
-							 errmsg("function %s must return type %s",
-									fmt, "copy_handler"),
-							 parser_errposition(pstate, defel->location)));
-
-				opts_out->handler = handler;
+				if (strcmp(fmt, "csv") == 0)
+					opts_out->csv_mode = true;
+				else if (strcmp(fmt, "binary") == 0)
+					opts_out->binary = true;
 			}
+
+			arg_types[0] = INTERNALOID;
+			handler = LookupFuncName(qualified_format, 1,
+									 arg_types, true);
+			if (!OidIsValid(handler))
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+						 errmsg("COPY format \"%s\" not recognized", format),
+						 parser_errposition(pstate, defel->location)));
+
+			/* check that handler has correct return type */
+			if (get_func_rettype(handler) != COPY_HANDLEROID)
+				ereport(ERROR,
+						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+						 errmsg("function %s must return type %s",
+								format, "copy_handler"),
+						 parser_errposition(pstate, defel->location)));
+
+			opts_out->handler = handler;
 		}
 		else if (strcmp(defel->defname, "freeze") == 0)
 		{
@@ -1040,3 +1044,36 @@ CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
 
 	return attnums;
 }
+
+Datum
+copy_text_handler(PG_FUNCTION_ARGS)
+{
+	bool		is_from = PG_GETARG_BOOL(0);
+
+	if (is_from)
+		PG_RETURN_POINTER(&CopyFromRoutineText);
+	else
+		PG_RETURN_POINTER(&CopyToRoutineText);
+}
+
+Datum
+copy_csv_handler(PG_FUNCTION_ARGS)
+{
+	bool		is_from = PG_GETARG_BOOL(0);
+
+	if (is_from)
+		PG_RETURN_POINTER(&CopyFromRoutineCSV);
+	else
+		PG_RETURN_POINTER(&CopyToRoutineCSV);
+}
+
+Datum
+copy_binary_handler(PG_FUNCTION_ARGS)
+{
+	bool		is_from = PG_GETARG_BOOL(0);
+
+	if (is_from)
+		PG_RETURN_POINTER(&CopyFromRoutineBinary);
+	else
+		PG_RETURN_POINTER(&CopyToRoutineBinary);
+}
diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c
index 91f44193abf..7244eb6368a 100644
--- a/src/backend/commands/copyfrom.c
+++ b/src/backend/commands/copyfrom.c
@@ -45,6 +45,7 @@
 #include "rewrite/rewriteHandler.h"
 #include "storage/fd.h"
 #include "tcop/tcopprot.h"
+#include "utils/fmgroids.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/portal.h"
@@ -128,7 +129,7 @@ static void CopyFromBinaryEnd(CopyFromState cstate);
  */
 
 /* text format */
-static const CopyFromRoutine CopyFromRoutineText = {
+const CopyFromRoutine CopyFromRoutineText = {
 	.type = T_CopyFromRoutine,
 	.CopyFromInFunc = CopyFromTextLikeInFunc,
 	.CopyFromStart = CopyFromTextLikeStart,
@@ -137,7 +138,7 @@ static const CopyFromRoutine CopyFromRoutineText = {
 };
 
 /* CSV format */
-static const CopyFromRoutine CopyFromRoutineCSV = {
+const CopyFromRoutine CopyFromRoutineCSV = {
 	.type = T_CopyFromRoutine,
 	.CopyFromInFunc = CopyFromTextLikeInFunc,
 	.CopyFromStart = CopyFromTextLikeStart,
@@ -146,7 +147,7 @@ static const CopyFromRoutine CopyFromRoutineCSV = {
 };
 
 /* binary format */
-static const CopyFromRoutine CopyFromRoutineBinary = {
+const CopyFromRoutine CopyFromRoutineBinary = {
 	.type = T_CopyFromRoutine,
 	.CopyFromInFunc = CopyFromBinaryInFunc,
 	.CopyFromStart = CopyFromBinaryStart,
@@ -158,28 +159,23 @@ static const CopyFromRoutine CopyFromRoutineBinary = {
 static const CopyFromRoutine *
 CopyFromGetRoutine(const CopyFormatOptions *opts)
 {
-	if (OidIsValid(opts->handler))
-	{
-		Datum		datum;
-		Node	   *routine;
-
-		datum = OidFunctionCall1(opts->handler, BoolGetDatum(true));
-		routine = (Node *) DatumGetPointer(datum);
-		if (routine == NULL || !IsA(routine, CopyFromRoutine))
-			ereport(ERROR,
-					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("COPY handler function %s.%s did not return CopyFromRoutine struct",
-							get_namespace_name(get_func_namespace(opts->handler)),
-							get_func_name(opts->handler))));
-		return castNode(CopyFromRoutine, routine);
-	}
-	else if (opts->csv_mode)
-		return &CopyFromRoutineCSV;
-	else if (opts->binary)
-		return &CopyFromRoutineBinary;
+	Oid			handler = opts->handler;
+	Datum		datum;
+	Node	   *routine;
 
 	/* default is text */
-	return &CopyFromRoutineText;
+	if (!OidIsValid(handler))
+		handler = F_TEXT_INTERNAL;
+
+	datum = OidFunctionCall1(handler, BoolGetDatum(true));
+	routine = (Node *) DatumGetPointer(datum);
+	if (routine == NULL || !IsA(routine, CopyFromRoutine))
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("COPY handler function %s.%s did not return CopyFromRoutine struct",
+						get_namespace_name(get_func_namespace(handler)),
+						get_func_name(handler))));
+	return castNode(CopyFromRoutine, routine);
 }
 
 /* Implementation of the start callback for text and CSV formats */
diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c
index 23cbdad184c..b244167a568 100644
--- a/src/backend/commands/copyto.c
+++ b/src/backend/commands/copyto.c
@@ -32,6 +32,7 @@
 #include "pgstat.h"
 #include "storage/fd.h"
 #include "tcop/tcopprot.h"
+#include "utils/fmgroids.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
@@ -89,7 +90,7 @@ static void CopySendInt16(CopyToState cstate, int16 val);
  */
 
 /* text format */
-static const CopyToRoutine CopyToRoutineText = {
+const CopyToRoutine CopyToRoutineText = {
 	.type = T_CopyToRoutine,
 	.CopyToStart = CopyToTextLikeStart,
 	.CopyToOutFunc = CopyToTextLikeOutFunc,
@@ -98,7 +99,7 @@ static const CopyToRoutine CopyToRoutineText = {
 };
 
 /* CSV format */
-static const CopyToRoutine CopyToRoutineCSV = {
+const CopyToRoutine CopyToRoutineCSV = {
 	.type = T_CopyToRoutine,
 	.CopyToStart = CopyToTextLikeStart,
 	.CopyToOutFunc = CopyToTextLikeOutFunc,
@@ -107,7 +108,7 @@ static const CopyToRoutine CopyToRoutineCSV = {
 };
 
 /* binary format */
-static const CopyToRoutine CopyToRoutineBinary = {
+const CopyToRoutine CopyToRoutineBinary = {
 	.type = T_CopyToRoutine,
 	.CopyToStart = CopyToBinaryStart,
 	.CopyToOutFunc = CopyToBinaryOutFunc,
@@ -119,28 +120,23 @@ static const CopyToRoutine CopyToRoutineBinary = {
 static const CopyToRoutine *
 CopyToGetRoutine(const CopyFormatOptions *opts)
 {
-	if (OidIsValid(opts->handler))
-	{
-		Datum		datum;
-		Node	   *routine;
-
-		datum = OidFunctionCall1(opts->handler, BoolGetDatum(false));
-		routine = (Node *) DatumGetPointer(datum);
-		if (routine == NULL || !IsA(routine, CopyToRoutine))
-			ereport(ERROR,
-					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("COPY handler function %s.%s did not return CopyToRoutine struct",
-							get_namespace_name(get_func_namespace(opts->handler)),
-							get_func_name(opts->handler))));
-		return castNode(CopyToRoutine, routine);
-	}
-	else if (opts->csv_mode)
-		return &CopyToRoutineCSV;
-	else if (opts->binary)
-		return &CopyToRoutineBinary;
+	Oid			handler = opts->handler;
+	Datum		datum;
+	Node	   *routine;
 
 	/* default is text */
-	return &CopyToRoutineText;
+	if (!OidIsValid(handler))
+		handler = F_TEXT_INTERNAL;
+
+	datum = OidFunctionCall1(handler, BoolGetDatum(false));
+	routine = (Node *) DatumGetPointer(datum);
+	if (routine == NULL || !IsA(routine, CopyToRoutine))
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("COPY handler function %s.%s did not return CopyToRoutine struct",
+						get_namespace_name(get_func_namespace(handler)),
+						get_func_name(handler))));
+	return castNode(CopyToRoutine, routine);
 }
 
 /* Implementation of the start callback for text and CSV formats */
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 79b5a36088c..09bacb80084 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -12485,4 +12485,15 @@
   proargtypes => 'int4',
   prosrc => 'gist_stratnum_common' },
 
+# COPY handlers
+{ oid => '8100', descr => 'text COPY FORMAT handler',
+  proname => 'text', provolatile => 'i', prorettype => 'copy_handler',
+  proargtypes => 'internal', prosrc => 'copy_text_handler' },
+{ oid => '8101', descr => 'csv COPY FORMAT handler',
+  proname => 'csv', provolatile => 'i', prorettype => 'copy_handler',
+  proargtypes => 'internal', prosrc => 'copy_csv_handler' },
+{ oid => '8102', descr => 'binary COPY FORMAT handler',
+  proname => 'binary', provolatile => 'i', prorettype => 'copy_handler',
+  proargtypes => 'internal', prosrc => 'copy_binary_handler' },
+
 ]
diff --git a/src/include/commands/copy.h b/src/include/commands/copy.h
index 6df1f8a3b9b..4525261fcc4 100644
--- a/src/include/commands/copy.h
+++ b/src/include/commands/copy.h
@@ -87,7 +87,7 @@ typedef struct CopyFormatOptions
 	CopyLogVerbosityChoice log_verbosity;	/* verbosity of logged messages */
 	int64		reject_limit;	/* maximum tolerable number of errors */
 	List	   *convert_select; /* list of column names (can be NIL) */
-	Oid			handler;		/* handler function for custom format routine */
+	Oid			handler;		/* handler function */
 } CopyFormatOptions;
 
 /* These are private in commands/copy[from|to]_internal.h */
diff --git a/src/include/commands/copyfrom_internal.h b/src/include/commands/copyfrom_internal.h
index af425cf5fd9..abeccf85c1c 100644
--- a/src/include/commands/copyfrom_internal.h
+++ b/src/include/commands/copyfrom_internal.h
@@ -14,7 +14,7 @@
 #ifndef COPYFROM_INTERNAL_H
 #define COPYFROM_INTERNAL_H
 
-#include "commands/copy.h"
+#include "commands/copyapi.h"
 #include "commands/trigger.h"
 #include "nodes/miscnodes.h"
 
@@ -197,4 +197,8 @@ extern bool CopyFromCSVOneRow(CopyFromState cstate, ExprContext *econtext,
 extern bool CopyFromBinaryOneRow(CopyFromState cstate, ExprContext *econtext,
 								 Datum *values, bool *nulls);
 
+extern PGDLLIMPORT const CopyFromRoutine CopyFromRoutineText;
+extern PGDLLIMPORT const CopyFromRoutine CopyFromRoutineCSV;
+extern PGDLLIMPORT const CopyFromRoutine CopyFromRoutineBinary;
+
 #endif							/* COPYFROM_INTERNAL_H */
diff --git a/src/include/commands/copyto_internal.h b/src/include/commands/copyto_internal.h
index 14ee0f50588..1bd83fa7f14 100644
--- a/src/include/commands/copyto_internal.h
+++ b/src/include/commands/copyto_internal.h
@@ -14,7 +14,7 @@
 #ifndef COPYTO_INTERNAL_H
 #define COPYTO_INTERNAL_H
 
-#include "commands/copy.h"
+#include "commands/copyapi.h"
 #include "executor/execdesc.h"
 #include "executor/tuptable.h"
 #include "nodes/execnodes.h"
@@ -83,4 +83,8 @@ typedef struct CopyToStateData
 	void	   *opaque;			/* private space */
 } CopyToStateData;
 
+extern PGDLLIMPORT const CopyToRoutine CopyToRoutineText;
+extern PGDLLIMPORT const CopyToRoutine CopyToRoutineCSV;
+extern PGDLLIMPORT const CopyToRoutine CopyToRoutineBinary;
+
 #endif							/* COPYTO_INTERNAL_H */
diff --git a/src/test/modules/test_copy_format/expected/builtin.out b/src/test/modules/test_copy_format/expected/builtin.out
new file mode 100644
index 00000000000..11b1053c84e
--- /dev/null
+++ b/src/test/modules/test_copy_format/expected/builtin.out
@@ -0,0 +1,34 @@
+CREATE EXTENSION test_copy_format;
+CREATE TABLE public.test (a smallint, b integer, c bigint);
+INSERT INTO public.test VALUES (1, 2, 3), (12, 34, 56), (123, 456, 789);
+-- public.text must not be used
+COPY public.test FROM stdin WITH (FORMAT text);
+COPY public.test TO stdout WITH (FORMAT text);
+1	2	3
+12	34	56
+123	456	789
+COPY public.test FROM stdin WITH (FORMAT 'pg_catalog.text');
+COPY public.test TO stdout WITH (FORMAT 'pg_catalog.text');
+1	2	3
+12	34	56
+123	456	789
+-- public.csv must not be used
+COPY public.test FROM stdin WITH (FORMAT csv);
+COPY public.test TO stdout WITH (FORMAT csv);
+1,2,3
+12,34,56
+123,456,789
+COPY public.test FROM stdin WITH (FORMAT 'pg_catalog.csv');
+COPY public.test TO stdout WITH (FORMAT 'pg_catalog.csv');
+1,2,3
+12,34,56
+123,456,789
+-- public.binary must not be used
+\getenv abs_builddir PG_ABS_BUILDDIR
+\set filename :abs_builddir '/results/binary.data'
+COPY public.test TO :'filename' WITH (FORMAT binary);
+COPY public.test FROM :'filename' WITH (FORMAT binary);
+COPY public.test TO :'filename' WITH (FORMAT 'pg_catalog.binary');
+COPY public.test FROM :'filename' WITH (FORMAT 'pg_catalog.binary');
+DROP TABLE public.test;
+DROP EXTENSION test_copy_format;
diff --git a/src/test/modules/test_copy_format/meson.build b/src/test/modules/test_copy_format/meson.build
index 8010659585b..86ba610d4ff 100644
--- a/src/test/modules/test_copy_format/meson.build
+++ b/src/test/modules/test_copy_format/meson.build
@@ -27,6 +27,7 @@ tests += {
   'bd': meson.current_build_dir(),
   'regress': {
     'sql': [
+      'builtin',
       'invalid',
       'no_schema',
       'schema',
diff --git a/src/test/modules/test_copy_format/sql/builtin.sql b/src/test/modules/test_copy_format/sql/builtin.sql
new file mode 100644
index 00000000000..2d24069b538
--- /dev/null
+++ b/src/test/modules/test_copy_format/sql/builtin.sql
@@ -0,0 +1,30 @@
+CREATE EXTENSION test_copy_format;
+CREATE TABLE public.test (a smallint, b integer, c bigint);
+INSERT INTO public.test VALUES (1, 2, 3), (12, 34, 56), (123, 456, 789);
+
+-- public.text must not be used
+COPY public.test FROM stdin WITH (FORMAT text);
+\.
+COPY public.test TO stdout WITH (FORMAT text);
+COPY public.test FROM stdin WITH (FORMAT 'pg_catalog.text');
+\.
+COPY public.test TO stdout WITH (FORMAT 'pg_catalog.text');
+
+-- public.csv must not be used
+COPY public.test FROM stdin WITH (FORMAT csv);
+\.
+COPY public.test TO stdout WITH (FORMAT csv);
+COPY public.test FROM stdin WITH (FORMAT 'pg_catalog.csv');
+\.
+COPY public.test TO stdout WITH (FORMAT 'pg_catalog.csv');
+
+-- public.binary must not be used
+\getenv abs_builddir PG_ABS_BUILDDIR
+\set filename :abs_builddir '/results/binary.data'
+COPY public.test TO :'filename' WITH (FORMAT binary);
+COPY public.test FROM :'filename' WITH (FORMAT binary);
+COPY public.test TO :'filename' WITH (FORMAT 'pg_catalog.binary');
+COPY public.test FROM :'filename' WITH (FORMAT 'pg_catalog.binary');
+
+DROP TABLE public.test;
+DROP EXTENSION test_copy_format;
diff --git a/src/test/modules/test_copy_format/test_copy_format--1.0.sql b/src/test/modules/test_copy_format/test_copy_format--1.0.sql
index c1a137181f8..bfa1900e828 100644
--- a/src/test/modules/test_copy_format/test_copy_format--1.0.sql
+++ b/src/test/modules/test_copy_format/test_copy_format--1.0.sql
@@ -22,3 +22,18 @@ CREATE FUNCTION test_copy_format_wrong_return_value(internal)
 	RETURNS copy_handler
 	AS 'MODULE_PATHNAME', 'test_copy_format_wrong_return_value'
 	LANGUAGE C;
+
+CREATE FUNCTION text(internal)
+	RETURNS copy_handler
+	AS 'MODULE_PATHNAME', 'test_copy_format'
+	LANGUAGE C;
+
+CREATE FUNCTION csv(internal)
+	RETURNS copy_handler
+	AS 'MODULE_PATHNAME', 'test_copy_format'
+	LANGUAGE C;
+
+CREATE FUNCTION binary(internal)
+	RETURNS copy_handler
+	AS 'MODULE_PATHNAME', 'test_copy_format'
+	LANGUAGE C;
-- 
2.47.2

