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 1umRaJ-00Flyw-U9 for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Aug 2025 06:37:13 +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 1umRaI-003LaA-CX for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Aug 2025 06:37:10 +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 1umRaH-003La1-0A for pgsql-hackers@lists.postgresql.org; Thu, 14 Aug 2025 06:37:10 +0000 Received: from mail.clear-code.com ([2401:2500:102:3037:153:126:203:179]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1umRaD-000Uaz-0C for pgsql-hackers@postgresql.org; Thu, 14 Aug 2025 06:37:08 +0000 Received: from localhost (unknown [IPv6:2404:7a80:9f01:f500:1788:74de:dc85:ea72]) by mail.clear-code.com (Postfix) with ESMTPSA id 431C664D710; Thu, 14 Aug 2025 15:36:57 +0900 (JST) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.clear-code.com 431C664D710 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clear-code.com; s=default; t=1755153417; bh=xA9wpzHF+oUbal3zZSyysX8KMo+hGhsEKsm28/Z1IPY=; h=Date:To:Cc:Subject:From:In-Reply-To:References:From; b=dcMdo2jr/VqVEKfxNbZ+s+qg4T8h8PDNTvucqqR/wLi/bm8Akdo6e+SJflKLrzPzR BhplYSMMxPs0DG032a9D6MEe4gAHmZ4RSzf3HAs1PalCfO53DvpqkDbb3SwotLt/+t mONELsEMEjrymxdOPy0C2rh/QcUKTqVY9kTh4fZo= Date: Thu, 14 Aug 2025 15:36:54 +0900 (JST) Message-Id: <20250814.153654.91221343186154858.kou@clear-code.com> To: sawada.mshk@gmail.com Cc: michael@paquier.xyz, david.g.johnston@gmail.com, tgl@sss.pgh.pa.us, 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: <20250718.190553.1172585000083080334.kou@clear-code.com> X-Mailer: Mew version 6.8 on Emacs 30.1 Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Thu_Aug_14_15_36_54_2025_549)--" Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 431C664D710 X-Rspamd-Server: mail.clear-code.com 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)[multipart/mixed,text/plain,text/x-patch]; 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:+,1:+,2:+]; 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,sss.pgh.pa.us,postgresql.org]; FROM_EQ_ENVFROM(0.00)[]; TO_DN_NONE(0.00)[]; RCPT_COUNT_FIVE(0.00)[6] X-Rspamd-Action: no action List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ----Next_Part(Thu_Aug_14_15_36_54_2025_549)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi, In "Re: Make COPY format extendable: Extract COPY TO format implementations" on Mon, 28 Jul 2025 22:19:36 -0700, Masahiko Sawada wrote: > The fields in 1 are mostly static fields, and the fields in 2 and 3 > are likely to be accessed in hot functions during COPY FROM. Would it > be a good idea to restructure these fields so that we can hide the > fields in 1 from callback functions and having the fields in 3 in a > separate format-specific struct that can be accessed via an opaque > pointer? But could the latter change potentially cause performance > overheads? Yes. It changes memory layout (1 continuous memory chunk -> 2 separated memory chunks) and introduces indirect member accesses (x->y -> x->z->y). They may not have performance impact but we need to measure it if we want to use this approach. BTW, how about the following approach? copyapi.h: typedef struct CopyToStateData { /* public members */ /* ... */ } CopyToStateData; copyto.c: typedef struct CopyToStateInternalData { CopyToStateData parent; /* private members */ /* ... */ } CopyToStateInternalData; We export CopyToStateData only with public members. We don't export CopyToStateInternalData that has members only for built-in formats. CopyToStateInternalData has the same memory layout as CopyToStateData. So we can use CopyToStateInternalData as CopyToStateData. We use CopyToStateData not CopyToStateInternalData in public API. We cast CopyToStateData to CopyToStateInternalData when we need to use private members: static void CopySendData(CopyToState cstate, const void *databuf, int datasize) { CopyToStateInternal cstate_internal = (CopyToStateInternal) cstate; appendBinaryStringInfo(cstate_internal->fe_msgbuf, databuf, datasize); } It's still direct member access. With this approach, we can keep the same memory layout (1 continuous memory chunk) and direct member access. I think that this approach doesn't have performance impact. See the attached patch for PoC of this approach. Drawback: This approach always allocates CopyToStateInternalData not CopyToStateData. So we need to allocate needless memories for extensions. But this will prevent performance regression of built-in formats. Is it acceptable? Thanks, -- kou ----Next_Part(Thu_Aug_14_15_36_54_2025_549)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="0001-Split-CopyToStateData-to-CopyToStateData-and-CopyToS.patch"