Skip to content

Commit cd22d3c

Browse files
committed
Avoid useless buffer allocations during binary COPY FROM.
The raw_buf and line_buf buffers aren't used when reading binary format, so skip allocating them. raw_buf is 64K so that seems like a worthwhile savings. An unused line_buf only wastes 1K, but as long as we're checking it's free to avoid allocating that too. Bharath Rupireddy, tweaked a bit by me Discussion: https://postgr.es/m/CALj2ACXcCKaGPY0whowqrJ4OPJvDnTssgpGCzvuFQu5z0CXb-g@mail.gmail.com
1 parent ea91253 commit cd22d3c

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/backend/commands/copy.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ typedef struct CopyStateData
193193
* the current line. The CopyReadAttributes functions return arrays of
194194
* pointers into this buffer. We avoid palloc/pfree overhead by re-using
195195
* the buffer on each cycle.
196+
*
197+
* (In binary COPY FROM, attribute_buf holds the binary data for the
198+
* current field, while the other variables are not used.)
196199
*/
197200
StringInfoData attribute_buf;
198201

@@ -3359,12 +3362,19 @@ BeginCopyFrom(ParseState *pstate,
33593362
cstate->cur_attname = NULL;
33603363
cstate->cur_attval = NULL;
33613364

3362-
/* Set up variables to avoid per-attribute overhead. */
3365+
/*
3366+
* Set up variables to avoid per-attribute overhead. attribute_buf is
3367+
* used in both text and binary modes, but we use line_buf and raw_buf
3368+
* only in text mode.
3369+
*/
33633370
initStringInfo(&cstate->attribute_buf);
3364-
initStringInfo(&cstate->line_buf);
3365-
cstate->line_buf_converted = false;
3366-
cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
3367-
cstate->raw_buf_index = cstate->raw_buf_len = 0;
3371+
if (!cstate->binary)
3372+
{
3373+
initStringInfo(&cstate->line_buf);
3374+
cstate->line_buf_converted = false;
3375+
cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
3376+
cstate->raw_buf_index = cstate->raw_buf_len = 0;
3377+
}
33683378

33693379
/* Assign range table, we'll need it in CopyFrom. */
33703380
if (pstate)

0 commit comments

Comments
 (0)