Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b619852

Browse files
committed
Improve COPY TO performance when server and client encodings match
This commit fixes an oversight introduced in c61a2f5, where COPY TO would attempt to do encoding conversions even if the encodings of the client and the server matched for multi-byte encodings. All conversions go through pg_any_to_server() that makes the conversion a no-op when the encodings of the client and the server match, even for multi-byte encodings. The logic was fine, but setting CopyToStateData->need_transcoding would cause strlen() to be called for nothing for each attribute of all the rows copied, and that was showing high in some profiles (more attributes make that easier to reach). This change improves the runtime of some worst-case COPY TO queries by 15%~ (number present at least here). This is a performance improvement, so no backpatch is done out of caution as this is not a regression. Reported-by: Andres Freund Analyzed-by: Andres Freund Author: Michael Paquier Reviewed-by: Heikki Linnakangas Discussion: https://postgr.es/m/20240206020504.edijzczkgd25ek6z@awork3.anarazel.de
1 parent a89fa00 commit b619852

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/backend/commands/copyto.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,15 @@ BeginCopyTo(ParseState *pstate,
612612
cstate->file_encoding = cstate->opts.file_encoding;
613613

614614
/*
615-
* Set up encoding conversion info. Even if the file and server encodings
616-
* are the same, we must apply pg_any_to_server() to validate data in
617-
* multibyte encodings.
615+
* Set up encoding conversion info if the file and server encodings
616+
* differ (see also pg_server_to_any).
618617
*/
619-
cstate->need_transcoding =
620-
(cstate->file_encoding != GetDatabaseEncoding() ||
621-
pg_database_encoding_max_length() > 1);
618+
if (cstate->file_encoding == GetDatabaseEncoding() ||
619+
cstate->file_encoding == PG_SQL_ASCII)
620+
cstate->need_transcoding = false;
621+
else
622+
cstate->need_transcoding = true;
623+
622624
/* See Multibyte encoding comment above */
623625
cstate->encoding_embeds_ascii = PG_ENCODING_IS_CLIENT_ONLY(cstate->file_encoding);
624626

0 commit comments

Comments
 (0)