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 1totWz-00GysH-MU for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Mar 2025 00:19:39 +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 1totWw-009r3S-OP for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Mar 2025 00:19:34 +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 1totWv-009r2q-L9 for pgsql-hackers@lists.postgresql.org; Mon, 03 Mar 2025 00:19:34 +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 1totWp-000bmY-10 for pgsql-hackers@postgresql.org; Mon, 03 Mar 2025 00:19:32 +0000 Received: from localhost (unknown [IPv6:2404:7a80:9f01:f500:af1b:fa00:91d6:aaa9]) by mail.clear-code.com (Postfix) with ESMTPSA id 78B735660C8; Mon, 3 Mar 2025 09:19:17 +0900 (JST) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.clear-code.com 78B735660C8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clear-code.com; s=default; t=1740961157; bh=LREakiE9W/kFPLlaO9MFR7Qpm+wXRa7HDwT/wfZ2/tw=; h=Date:To:Cc:Subject:From:In-Reply-To:References:From; b=kZ2U5M5tVToqTu2dK2R5Qevoh7hFodt9PYS57ok2rb0xLwbqCTA6RHXXQeJmInalq 9Nj0lqzGHv71ij8VGir81B0l6+6uR52/PsAyTPEol9HaGYWfZdYTHRCCokZZ5omkuR wTKxuSl9am99tCz0SCA73D7FZ7L7EZYkR/b3KEFs= Date: Mon, 03 Mar 2025 09:19:12 +0900 (JST) Message-Id: <20250303.091912.305263537922114619.kou@clear-code.com> To: tgl@sss.pgh.pa.us Cc: zhjwpku@gmail.com, sawada.mshk@gmail.com, michael@paquier.xyz, pgsql-hackers@postgresql.org Subject: Re: Make COPY format extendable: Extract COPY TO format implementations From: Sutou Kouhei In-Reply-To: <3191030.1740932840@sss.pgh.pa.us> References: <20250301.115009.424844407736647598.kou@clear-code.com> <3191030.1740932840@sss.pgh.pa.us> X-Mailer: Mew version 6.8 on Emacs 30.1 Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Mon_Mar__3_09_19_12_2025_387)--" Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 78B735660C8 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]; RCVD_COUNT_ZERO(0.00)[0]; ASN(0.00)[asn:2518, ipnet:2404:7a80::/29, country:JP]; MIME_TRACE(0.00)[0:+,1:+,2:+]; TAGGED_RCPT(0.00)[]; ARC_NA(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[gmail.com,paquier.xyz,postgresql.org]; FROM_EQ_ENVFROM(0.00)[]; TO_DN_NONE(0.00)[]; RCPT_COUNT_FIVE(0.00)[5] X-Rspamd-Action: no action List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ----Next_Part(Mon_Mar__3_09_19_12_2025_387)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi, In <3191030.1740932840@sss.pgh.pa.us> "Re: Make COPY format extendable: Extract COPY TO format implementations" on Sun, 02 Mar 2025 11:27:20 -0500, Tom Lane wrote: >> While review another thread (Emitting JSON to file using COPY TO), >> I found the recently committed patches on this thread pass the >> CopyFormatOptions struct directly rather a pointer of the struct >> as a function parameter of CopyToGetRoutine and CopyFromGetRoutine. > > Coverity is unhappy about that too: > > /srv/coverity/git/pgsql-git/postgresql/src/backend/commands/copyto.c: 177 in CopyToGetRoutine() > 171 .CopyToOneRow = CopyToBinaryOneRow, > 172 .CopyToEnd = CopyToBinaryEnd, > 173 }; > 174 > 175 /* Return a COPY TO routine for the given options */ > 176 static const CopyToRoutine * >>>> CID 1643911: Performance inefficiencies (PASS_BY_VALUE) >>>> Passing parameter opts of type "CopyFormatOptions" (size 184 bytes) by value, which exceeds the low threshold of 128 bytes. > 177 CopyToGetRoutine(CopyFormatOptions opts) > 178 { > 179 if (opts.csv_mode) > 180 return &CopyToRoutineCSV; > > (and likewise for CopyFromGetRoutine). I realize that these > functions aren't called often enough for performance to be an > overriding concern, but it still seems like poor style. > >> Then I took a quick look at the newly rebased patch set and >> found Sutou has already fixed this issue. > > +1, except I'd suggest declaring the parameters as > "const CopyFormatOptions *opts". Thanks for pointing out this (and sorry for missing this in our reviews...)! How about the attached patch? I'll rebase the v35 patch set after this is fixed. Thanks, -- kou ----Next_Part(Mon_Mar__3_09_19_12_2025_387)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="0001-Use-const-pointer-for-CopyFormatOptions-for-Copy-To-.patch" From f21b48c7dd0b141c561e9c8a2c9f1d0e28aabfae Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Mon, 3 Mar 2025 09:13:37 +0900 Subject: [PATCH] Use const pointer for CopyFormatOptions for Copy{To,From}GetRoutine() We don't need to copy CopyFormatOptions here. Reported-by: Junwang Zhao Discussion: https://postgr.es/m/CAEG8a3L6YCpPksTQMzjD_CvwDEhW3D_t=5md9BvvdOs5k+TA=Q@mail.gmail.com --- src/backend/commands/copyfrom.c | 8 ++++---- src/backend/commands/copyto.c | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c index 198cee2bc48..bcf66f0adf8 100644 --- a/src/backend/commands/copyfrom.c +++ b/src/backend/commands/copyfrom.c @@ -153,11 +153,11 @@ static const CopyFromRoutine CopyFromRoutineBinary = { /* Return a COPY FROM routine for the given options */ static const CopyFromRoutine * -CopyFromGetRoutine(CopyFormatOptions opts) +CopyFromGetRoutine(const CopyFormatOptions *opts) { - if (opts.csv_mode) + if (opts->csv_mode) return &CopyFromRoutineCSV; - else if (opts.binary) + else if (opts->binary) return &CopyFromRoutineBinary; /* default is text */ @@ -1574,7 +1574,7 @@ BeginCopyFrom(ParseState *pstate, ProcessCopyOptions(pstate, &cstate->opts, true /* is_from */ , options); /* Set the format routine */ - cstate->routine = CopyFromGetRoutine(cstate->opts); + cstate->routine = CopyFromGetRoutine(&cstate->opts); /* Process the target relation */ cstate->rel = rel; diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c index 721d29f8e53..84a3f3879a8 100644 --- a/src/backend/commands/copyto.c +++ b/src/backend/commands/copyto.c @@ -174,11 +174,11 @@ static const CopyToRoutine CopyToRoutineBinary = { /* Return a COPY TO routine for the given options */ static const CopyToRoutine * -CopyToGetRoutine(CopyFormatOptions opts) +CopyToGetRoutine(const CopyFormatOptions *opts) { - if (opts.csv_mode) + if (opts->csv_mode) return &CopyToRoutineCSV; - else if (opts.binary) + else if (opts->binary) return &CopyToRoutineBinary; /* default is text */ @@ -700,7 +700,7 @@ BeginCopyTo(ParseState *pstate, ProcessCopyOptions(pstate, &cstate->opts, false /* is_from */ , options); /* Set format routine */ - cstate->routine = CopyToGetRoutine(cstate->opts); + cstate->routine = CopyToGetRoutine(&cstate->opts); /* Process the source/target relation or query */ if (rel) -- 2.47.2 ----Next_Part(Mon_Mar__3_09_19_12_2025_387)----