Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDxM9-004HFl-K8 for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Nov 2024 02:55:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tDxM8-00BW3T-8T for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Nov 2024 02:55:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDxM7-00BW3B-49 for pgsql-hackers@lists.postgresql.org; Thu, 21 Nov 2024 02:55:43 +0000 Received: from mail.clear-code.com ([153.126.203.179]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDxM2-002yfI-V8 for pgsql-hackers@postgresql.org; Thu, 21 Nov 2024 02:55:41 +0000 Received: from localhost (unknown [IPv6:2404:7a80:9f01:f500:acd0:2aa3:7b08:7206]) by mail.clear-code.com (Postfix) with ESMTPSA id 89475DD364; Thu, 21 Nov 2024 11:55:33 +0900 (JST) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.clear-code.com 89475DD364 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clear-code.com; s=default; t=1732157733; bh=ZPdC3M9v2vC6qWgO11a0bBgnjidZuvW1iCsNgSpHgNo=; h=Date:To:Cc:Subject:From:In-Reply-To:References:From; b=sEN0X7M9x+sexJbD0R8qOpo0JAjXUuHTnz738ncuStzFZp30j44g4LO1oxfxvvS3R pIeuekTCw43owL/5ETBrz+tb3n4Y+yogfGUxFS+P3HyrdVe08Nq71An/btpOs0bIbk luxY5WRGxp6K896UhMPS5qwa34pCZg9Ft4UXkQTw= Date: Thu, 21 Nov 2024 11:55:31 +0900 (JST) Message-Id: <20241121.115531.1600295431613712107.kou@clear-code.com> To: sawada.mshk@gmail.com Cc: michael@paquier.xyz, pgsql-hackers@postgresql.org Subject: Re: Make COPY format extendable: Extract COPY TO format implementations From: Sutou Kouhei In-Reply-To: References: <20241119.103115.418773618386970482.kou@clear-code.com> X-Mailer: Mew version 6.8 on Emacs 29.4 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spamd-Result: default: False [1.40 / 999.00]; MID_CONTAINS_FROM(1.00)[]; MV_CASE(0.50)[]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; RCVD_COUNT_ZERO(0.00)[0]; TAGGED_RCPT(0.00)[]; ASN(0.00)[asn:2518, ipnet:2404:7a80::/29, country:JP]; FREEMAIL_TO(0.00)[gmail.com]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; TO_DN_NONE(0.00)[]; RCPT_COUNT_THREE(0.00)[3] X-Rspamd-Server: mail.clear-code.com X-Rspamd-Action: no action X-Rspamd-Queue-Id: 89475DD364 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, In "Re: Make COPY format extendable: Extract COPY TO format implementations" on Wed, 20 Nov 2024 14:14:27 -0800, Masahiko Sawada wrote: > I've extracted the changes to refactor COPY TO/FROM to use the format > callback routines from v23 patch set, which seems to be a better patch > split to me. Also, I've reviewed these changes and made some changes > on top of them. The attached patches are: > > 0001: make COPY TO use CopyToRoutine. > 0002: minor changes to 0001 patch. will be fixed up. > 0003: make COPY FROM use CopyFromRoutine. > 0004: minor changes to 0003 patch. will be fixed up. > > I've confirmed that v24 has a similar performance improvement to v23. > Please check these extractions and minor change suggestions. Thanks. Here are my comments: 0002: +/* TEXT format */ "text" may be better than "TEXT". We use "text" not "TEXT" in other places. +static const CopyToRoutine CopyToRoutineText = { + .CopyToStart = CopyToTextLikeStart, + .CopyToOutFunc = CopyToTextLikeOutFunc, + .CopyToOneRow = CopyToTextOneRow, + .CopyToEnd = CopyToTextLikeEnd, +}; +/* BINARY format */ "binary" may be better than "BINARY". We use "binary" not "BINARY" in other places. +static const CopyToRoutine CopyToRoutineBinary = { + .CopyToStart = CopyToBinaryStart, + .CopyToOutFunc = CopyToBinaryOutFunc, + .CopyToOneRow = CopyToBinaryOneRow, + .CopyToEnd = CopyToBinaryEnd, +}; +/* Return COPY TO routines for the given option */ option -> options +static const CopyToRoutine * +CopyToGetRoutine(CopyFormatOptions opts) 0003: diff --git a/src/include/commands/copyapi.h b/src/include/commands/copyapi.h index 99981b1579..224fda172e 100644 --- a/src/include/commands/copyapi.h +++ b/src/include/commands/copyapi.h @@ -56,4 +56,46 @@ typedef struct CopyToRoutine void (*CopyToEnd) (CopyToState cstate); } CopyToRoutine; +/* + * API structure for a COPY FROM format implementation. Note this must be Should we remove a tab character here? 0004: diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c index e77986f9a9..7f1de8a42b 100644 --- a/src/backend/commands/copyfrom.c +++ b/src/backend/commands/copyfrom.c @@ -106,31 +106,65 @@ typedef struct CopyMultiInsertInfo /* non-export function prototypes */ static void ClosePipeFromProgram(CopyFromState cstate); - /* - * CopyFromRoutine implementations for text and CSV. + * built-in format-specific routines. One-row callbacks are defined in built-in -> Built-in /* - * CopyFromTextLikeInFunc - * - * Assign input function data for a relation's attribute in text/CSV format. + * COPY FROM routines for built-in formats. ++ "+" -> " *" +/* TEXT format */ TEXT -> text? +/* BINARY format */ BINARY -> binary? +/* Return COPY FROM routines for the given option */ option -> options +static const CopyFromRoutine * +CopyFromGetRoutine(CopyFormatOptions opts) diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c index 0447c4df7e..5416583e94 100644 --- a/src/backend/commands/copyfromparse.c +++ b/src/backend/commands/copyfromparse.c +static bool CopyFromTextLikeOneRow(CopyFromState cstate, ExprContext *econtext, + Datum *values, bool *nulls, bool is_csv); Oh, I didn't know that we don't need inline in a function declaration. @@ -1237,7 +1219,7 @@ CopyReadLine(CopyFromState cstate, bool is_csv) /* * CopyReadLineText - inner loop of CopyReadLine for text mode */ -static pg_attribute_always_inline bool +static bool CopyReadLineText(CopyFromState cstate, bool is_csv) Is this an intentional change? CopyReadLineText() has "bool in_csv". Thanks, -- kou