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 1tlJ5h-00Asc8-9j for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Feb 2025 02:48:38 +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 1tlJ5f-000AOA-D8 for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Feb 2025 02:48:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tlJ5e-000AO2-7L for pgsql-hackers@lists.postgresql.org; Fri, 21 Feb 2025 02:48:35 +0000 Received: from mail.clear-code.com ([2401:2500:102:3037:153:126:203:179]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tlJ5V-0024SX-3B for pgsql-hackers@postgresql.org; Fri, 21 Feb 2025 02:48:32 +0000 Received: from localhost (unknown [IPv6:2404:7a80:9f01:f500:af1b:fa00:91d6:aaa9]) by mail.clear-code.com (Postfix) with ESMTPSA id 64054683C1A; Fri, 21 Feb 2025 11:48:15 +0900 (JST) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.clear-code.com 64054683C1A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clear-code.com; s=default; t=1740106095; bh=BraQVT1Y7Yh0EU9sANrFjTSZUDgbsfp4YUeanv9I8XU=; h=Date:To:Cc:Subject:From:In-Reply-To:References:From; b=GJIpnjC6aQISVjAgV92oKGdbmgWak8JRtNj53Seke+FDoYB85WStIcXtKMK2Zx6ec X+qtBdMVlQNv3DB4tD8VFfIWZc68HySUGcGew3YIKsxPsVTOKvQaUYZPH7YUQ9cHD4 AN2eY2G0azYTSKtFQgweSykxmAl6e+CeL1GR6W48= Date: Fri, 21 Feb 2025 11:48:12 +0900 (JST) Message-Id: <20250221.114812.86438027655536928.kou@clear-code.com> To: sawada.mshk@gmail.com Cc: michael@paquier.xyz, zhjwpku@gmail.com, pgsql-hackers@postgresql.org Subject: Re: Make COPY format extendable: Extract COPY TO format implementations From: Sutou Kouhei In-Reply-To: References: <20250207.220117.2285437283940199074.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-Rspamd-Server: mail.clear-code.com X-Rspamd-Queue-Id: 64054683C1A X-Rspamd-Action: no action X-Spamd-Result: default: False [2.90 / 999.00]; SUSPICIOUS_RECIPS(1.50)[]; MID_CONTAINS_FROM(1.00)[]; MV_CASE(0.50)[]; MIME_GOOD(-0.10)[text/plain]; TAGGED_RCPT(0.00)[]; ARC_NA(0.00)[]; ASN(0.00)[asn:2518, ipnet:2404:7a80::/29, country:JP]; RCVD_COUNT_ZERO(0.00)[0]; MIME_TRACE(0.00)[0:+]; FREEMAIL_TO(0.00)[gmail.com]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[paquier.xyz,gmail.com,postgresql.org]; FROM_EQ_ENVFROM(0.00)[]; TO_DN_NONE(0.00)[]; RCPT_COUNT_THREE(0.00)[4] 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 Thu, 20 Feb 2025 15:28:26 -0800, Masahiko Sawada wrote: > Looking at the 0001 patch again, I have a question: we have > CopyToTextLikeOneRow() for both CSV and text format: > > +/* Implementation of the per-row callback for text format */ > +static void > +CopyToTextOneRow(CopyToState cstate, TupleTableSlot *slot) > +{ > + CopyToTextLikeOneRow(cstate, slot, false); > +} > + > +/* Implementation of the per-row callback for CSV format */ > +static void > +CopyToCSVOneRow(CopyToState cstate, TupleTableSlot *slot) > +{ > + CopyToTextLikeOneRow(cstate, slot, true); > +} > > These two functions pass different is_csv value to that function, > which is used as follows: > > + if (is_csv) > + CopyAttributeOutCSV(cstate, string, > + > cstate->opts.force_quote_flags[attnum - 1]); > + else > + CopyAttributeOutText(cstate, string); > > However, we can know whether the format is CSV or text by checking > cstate->opts.csv_mode instead of passing is_csv. That way, we can > directly call CopyToTextLikeOneRow() but not via CopyToCSVOneRow() or > CopyToTextOneRow(). It would not help performance since we already > inline CopyToTextLikeOneRow(), but it looks simpler. This means the following, right? 1. We remove CopyToTextOneRow() and CopyToCSVOneRow() 2. We remove "bool is_csv" parameter from CopyToTextLikeOneRow() and use cstate->opts.csv_mode in CopyToTextLikeOneRow() instead of is_csv 3. We use CopyToTextLikeOneRow() for CopyToRoutineText::CopyToOneRow and CopyToRoutineCSV::CopyToOneRow If we use this approach, we can't remove the following branch in compile time: + if (is_csv) + CopyAttributeOutCSV(cstate, string, + cstate->opts.force_quote_flags[attnum - 1]); + else + CopyAttributeOutText(cstate, string); We can remove the branch in compile time with the current approach (constant argument + inline). It may have a negative performance impact because the "if" is used many times with large data. (That's why we choose the constant argument + inline approach in this thread.) Thanks, -- kou