Refactor Copy{From|To}GetRoutine() to use pass-by-reference argument.
authorMasahiko Sawada <[email protected]>
Tue, 4 Mar 2025 18:38:41 +0000 (10:38 -0800)
committerMasahiko Sawada <[email protected]>
Tue, 4 Mar 2025 18:38:41 +0000 (10:38 -0800)
The change improves efficiency by eliminating unnecessary copying of
CopyFormatOptions.

The coverity also complained about inefficiencies caused by
pass-by-value.

Oversight in 7717f6300 and 2e4127b6d.

Reported-by: Junwang Zhao <[email protected]>
Reported-by: Tom Lane <[email protected]> (per reports from coverity)
Author: Sutou Kouhei <[email protected]>
Reviewed-by: Junwang Zhao <[email protected]>
Reviewed-by: Masahiko Sawada <[email protected]>
Discussion: https://postgr.es/m/CAEG8a3L6YCpPksTQMzjD_CvwDEhW3D_t=5md9BvvdOs5k+TA=Q@mail.gmail.com

src/backend/commands/copyfrom.c
src/backend/commands/copyto.c

index 198cee2bc48b980742eb328b4ea6123a7bbd8959..bcf66f0adf89318c0ee88dc2ce4cabb313531449 100644 (file)
@@ -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;
index 721d29f8e53acbd4dab2542724dc2292467bfbfe..84a3f3879a87015ecaa9418d69a083d3ca3b9bb6 100644 (file)
@@ -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)