Skip to content

Commit 3943da4

Browse files
committed
Refactor CopyOneRowTo
The handling of binary and text formats are quite different here, so it's more clear to check for the format first and have two separate loops. Author: jian he <jian.universality@gmail.com> Reviewed-by: Ilia Evdokimov, Junwang Zhao Discussion: https://www.postgresql.org/message-id/CACJufxFzHCeFBQF0M%2BSgk_NwknWuQ4oU7tS1isVeBrbhcKOHkg@mail.gmail.com
1 parent 1153422 commit 3943da4

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/backend/commands/copyto.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -902,11 +902,8 @@ DoCopyTo(CopyToState cstate)
902902
static void
903903
CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
904904
{
905-
bool need_delim = false;
906905
FmgrInfo *out_functions = cstate->out_functions;
907906
MemoryContext oldcontext;
908-
ListCell *cur;
909-
char *string;
910907

911908
MemoryContextReset(cstate->rowcontext);
912909
oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
@@ -920,29 +917,23 @@ CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
920917
/* Make sure the tuple is fully deconstructed */
921918
slot_getallattrs(slot);
922919

923-
foreach(cur, cstate->attnumlist)
920+
if (!cstate->opts.binary)
924921
{
925-
int attnum = lfirst_int(cur);
926-
Datum value = slot->tts_values[attnum - 1];
927-
bool isnull = slot->tts_isnull[attnum - 1];
922+
bool need_delim = false;
928923

929-
if (!cstate->opts.binary)
924+
foreach_int(attnum, cstate->attnumlist)
930925
{
926+
Datum value = slot->tts_values[attnum - 1];
927+
bool isnull = slot->tts_isnull[attnum - 1];
928+
char *string;
929+
931930
if (need_delim)
932931
CopySendChar(cstate, cstate->opts.delim[0]);
933932
need_delim = true;
934-
}
935933

936-
if (isnull)
937-
{
938-
if (!cstate->opts.binary)
934+
if (isnull)
939935
CopySendString(cstate, cstate->opts.null_print_client);
940936
else
941-
CopySendInt32(cstate, -1);
942-
}
943-
else
944-
{
945-
if (!cstate->opts.binary)
946937
{
947938
string = OutputFunctionCall(&out_functions[attnum - 1],
948939
value);
@@ -952,10 +943,20 @@ CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
952943
else
953944
CopyAttributeOutText(cstate, string);
954945
}
946+
}
947+
}
948+
else
949+
{
950+
foreach_int(attnum, cstate->attnumlist)
951+
{
952+
Datum value = slot->tts_values[attnum - 1];
953+
bool isnull = slot->tts_isnull[attnum - 1];
954+
bytea *outputbytes;
955+
956+
if (isnull)
957+
CopySendInt32(cstate, -1);
955958
else
956959
{
957-
bytea *outputbytes;
958-
959960
outputbytes = SendFunctionCall(&out_functions[attnum - 1],
960961
value);
961962
CopySendInt32(cstate, VARSIZE(outputbytes) - VARHDRSZ);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy