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 1rB6Zd-009Rmz-ET for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Dec 2023 05:05:22 +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 1rB6Zc-00F3vw-3w for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Dec 2023 05:05:20 +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 1rB6Za-00F3vo-OE for pgsql-hackers@lists.postgresql.org; Thu, 07 Dec 2023 05:05:19 +0000 Received: from mail.clear-code.com ([153.126.206.245]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rB6ZQ-009Bz0-4T for pgsql-hackers@postgresql.org; Thu, 07 Dec 2023 05:05:15 +0000 Received: from localhost (unknown [IPv6:2404:7a80:89c1:1200:1653:202c:ea56:2daf]) by mail.clear-code.com (Postfix) with ESMTPSA id B61C861E82E; Thu, 7 Dec 2023 14:05:00 +0900 (JST) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.clear-code.com B61C861E82E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clear-code.com; s=default; t=1701925500; bh=Q7yqoR/CL7EovKa/UGblNuAjDtHcDH/D6pJxhwGgb9M=; h=Date:To:Cc:Subject:From:In-Reply-To:References:From; b=iywyZ82FYXQ26d6+jElbNd0fvhVO6/wVVgdX5ISiubO8AJgfqaLe3NoizPaXtHmoy oEY6PFA1YSLRiVlaipu8eA2ELrwtvoFtCDzjMTjdeiLdiFngh6opjmdAbtlVyGDE+A VNVW9csMA+gVxTwqDmmYUjaO0n2KjnJe8EtJLDlM= Date: Thu, 07 Dec 2023 14:04:58 +0900 (JST) Message-Id: <20231207.140458.425537343057608813.kou@clear-code.com> To: zhjwpku@gmail.com Cc: nathandbossart@gmail.com, pgsql-hackers@postgresql.org Subject: Re: Make COPY format extendable: Extract COPY TO format implementations From: Sutou Kouhei In-Reply-To: References: <20231206.162834.1390394858102165899.kou@clear-code.com> X-Mailer: Mew version 6.8 on Emacs 29.1 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: B61C861E82E X-Rspamd-Server: mail.clear-code.com 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)[]; FREEMAIL_TO(0.00)[gmail.com]; ASN(0.00)[asn:2518, ipnet:2404:7a80::/29, country:JP]; RCVD_COUNT_ZERO(0.00)[0]; MIME_TRACE(0.00)[0:+]; RCPT_COUNT_THREE(0.00)[3]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FREEMAIL_CC(0.00)[gmail.com,postgresql.org]; URIBL_BLOCKED(0.00)[localhost:helo,postgresql.org:url]; FROM_HAS_DN(0.00)[]; TO_DN_NONE(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; SURBL_MULTI_FAIL(0.00)[localhost:server fail,postgresql.org:server fail] X-Rspamd-Action: no action 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, 6 Dec 2023 22:07:51 +0800, Junwang Zhao wrote: > Should we extract both *copy to* and *copy from* for the first step, in that > case we can add the pg_copy_handler catalog smoothly later. I don't object it (mixing TO/FROM changes to one patch) but it may make review difficult. Is it acceptable? FYI: I planed that I implement TO part, and then FROM part, and then unify TO/FROM parts if needed. [1] > Attached V4 adds 'extract copy from' and it passed the cirrus ci, > please take a look. Thanks. Here are my comments: > + /* > + * Error is relevant to a particular line. > + * > + * If line_buf still contains the correct line, print it. > + */ > + if (cstate->line_buf_valid) We need to fix the indentation. > +CopyFromFormatBinaryStart(CopyFromState cstate, TupleDesc tupDesc) > +{ > + FmgrInfo *in_functions; > + Oid *typioparams; > + Oid in_func_oid; > + AttrNumber num_phys_attrs; > + > + /* > + * Pick up the required catalog information for each attribute in the > + * relation, including the input function, the element type (to pass to > + * the input function), and info about defaults and constraints. (Which > + * input function we use depends on text/binary format choice.) > + */ > + num_phys_attrs = tupDesc->natts; > + in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo)); > + typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid)); We need to update the comment because defaults and constraints aren't picked up here. > +CopyFromFormatTextStart(CopyFromState cstate, TupleDesc tupDesc) ... > + /* > + * Pick up the required catalog information for each attribute in the > + * relation, including the input function, the element type (to pass to > + * the input function), and info about defaults and constraints. (Which > + * input function we use depends on text/binary format choice.) > + */ > + in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo)); > + typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid)); ditto. > @@ -1716,15 +1776,6 @@ BeginCopyFrom(ParseState *pstate, > ReceiveCopyBinaryHeader(cstate); > } I think that this block should be moved to CopyFromFormatBinaryStart() too. But we need to run it after we setup inputs such as data_source_cb, pipe and filename... +/* Routines for a COPY HANDLER implementation. */ +typedef struct CopyHandlerOps +{ + /* Called when COPY TO is started. This will send a header. */ + void (*copy_to_start) (CopyToState cstate, TupleDesc tupDesc); + + /* Copy one row for COPY TO. */ + void (*copy_to_one_row) (CopyToState cstate, TupleTableSlot *slot); + + /* Called when COPY TO is ended. This will send a trailer. */ + void (*copy_to_end) (CopyToState cstate); + + void (*copy_from_start) (CopyFromState cstate, TupleDesc tupDesc); + bool (*copy_from_next) (CopyFromState cstate, ExprContext *econtext, + Datum *values, bool *nulls); + void (*copy_from_error_callback) (CopyFromState cstate); + void (*copy_from_end) (CopyFromState cstate); +} CopyHandlerOps; It seems that "copy_" prefix is redundant. Should we use "to_start" instead of "copy_to_start" and so on? BTW, it seems that "COPY FROM (FORMAT json)" may not be implemented. [2] We may need to care about NULL copy_from_* cases. > I added a hook *copy_from_end* but this might be removed later if not used. It may be useful to clean up resources for COPY FROM but the patch doesn't call the copy_from_end. How about removing it for now? We can add it and call it from EndCopyFrom() later? Because it's not needed for now. I think that we should focus on refactoring instead of adding a new feature in this patch. [1]: https://www.postgresql.org/message-id/20231204.153548.2126325458835528809.kou%40clear-code.com [2]: https://www.postgresql.org/message-id/flat/CALvfUkBxTYy5uWPFVwpk_7ii2zgT07t3d-yR_cy4sfrrLU%3Dkcg%40mail.gmail.com Thanks, -- kou