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

Commit 588edd8

Browse files
committed
Widen COPY FROM's current-line-number counter from 32 to 64 bits.
Because the code for the HEADER option skips a line when this counter is zero, a very long COPY FROM WITH HEADER operation would drop a line every 2^32 lines. A lesser but still unfortunate problem is that errors would show a wrong input line number for errors occurring beyond the 2^31'st input line. While such large input streams seemed impractical when this code was first written, they're not any more. Widening the counter (and some associated variables) to uint64 should be enough to prevent problems for the foreseeable future. David Rowley Discussion: https://postgr.es/m/CAKJS1f88yh-6wwEfO6QLEEvH3BEugOq2QX1TOja0vCauoynmOQ@mail.gmail.com
1 parent 7a0aa8d commit 588edd8

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/backend/commands/copy.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ typedef struct CopyStateData
133133

134134
/* these are just for error messages, see CopyFromErrorCallback */
135135
const char *cur_relname; /* table name for error messages */
136-
int cur_lineno; /* line number for error messages */
136+
uint64 cur_lineno; /* line number for error messages */
137137
const char *cur_attname; /* current att for error messages */
138138
const char *cur_attval; /* current att value for error messages */
139139

@@ -298,7 +298,7 @@ static void CopyFromInsertBatch(CopyState cstate, EState *estate,
298298
ResultRelInfo *resultRelInfo, TupleTableSlot *myslot,
299299
BulkInsertState bistate,
300300
int nBufferedTuples, HeapTuple *bufferedTuples,
301-
int firstBufferedLineNo);
301+
uint64 firstBufferedLineNo);
302302
static bool CopyReadLine(CopyState cstate);
303303
static bool CopyReadLineText(CopyState cstate);
304304
static int CopyReadAttributesText(CopyState cstate);
@@ -2146,17 +2146,21 @@ void
21462146
CopyFromErrorCallback(void *arg)
21472147
{
21482148
CopyState cstate = (CopyState) arg;
2149+
char curlineno_str[32];
2150+
2151+
snprintf(curlineno_str, sizeof(curlineno_str), UINT64_FORMAT,
2152+
cstate->cur_lineno);
21492153

21502154
if (cstate->binary)
21512155
{
21522156
/* can't usefully display the data */
21532157
if (cstate->cur_attname)
2154-
errcontext("COPY %s, line %d, column %s",
2155-
cstate->cur_relname, cstate->cur_lineno,
2158+
errcontext("COPY %s, line %s, column %s",
2159+
cstate->cur_relname, curlineno_str,
21562160
cstate->cur_attname);
21572161
else
2158-
errcontext("COPY %s, line %d",
2159-
cstate->cur_relname, cstate->cur_lineno);
2162+
errcontext("COPY %s, line %s",
2163+
cstate->cur_relname, curlineno_str);
21602164
}
21612165
else
21622166
{
@@ -2166,16 +2170,16 @@ CopyFromErrorCallback(void *arg)
21662170
char *attval;
21672171

21682172
attval = limit_printout_length(cstate->cur_attval);
2169-
errcontext("COPY %s, line %d, column %s: \"%s\"",
2170-
cstate->cur_relname, cstate->cur_lineno,
2173+
errcontext("COPY %s, line %s, column %s: \"%s\"",
2174+
cstate->cur_relname, curlineno_str,
21712175
cstate->cur_attname, attval);
21722176
pfree(attval);
21732177
}
21742178
else if (cstate->cur_attname)
21752179
{
21762180
/* error is relevant to a particular column, value is NULL */
2177-
errcontext("COPY %s, line %d, column %s: null input",
2178-
cstate->cur_relname, cstate->cur_lineno,
2181+
errcontext("COPY %s, line %s, column %s: null input",
2182+
cstate->cur_relname, curlineno_str,
21792183
cstate->cur_attname);
21802184
}
21812185
else
@@ -2196,14 +2200,14 @@ CopyFromErrorCallback(void *arg)
21962200
char *lineval;
21972201

21982202
lineval = limit_printout_length(cstate->line_buf.data);
2199-
errcontext("COPY %s, line %d: \"%s\"",
2200-
cstate->cur_relname, cstate->cur_lineno, lineval);
2203+
errcontext("COPY %s, line %s: \"%s\"",
2204+
cstate->cur_relname, curlineno_str, lineval);
22012205
pfree(lineval);
22022206
}
22032207
else
22042208
{
2205-
errcontext("COPY %s, line %d",
2206-
cstate->cur_relname, cstate->cur_lineno);
2209+
errcontext("COPY %s, line %s",
2210+
cstate->cur_relname, curlineno_str);
22072211
}
22082212
}
22092213
}
@@ -2271,7 +2275,7 @@ CopyFrom(CopyState cstate)
22712275
#define MAX_BUFFERED_TUPLES 1000
22722276
HeapTuple *bufferedTuples = NULL; /* initialize to silence warning */
22732277
Size bufferedTuplesSize = 0;
2274-
int firstBufferedLineNo = 0;
2278+
uint64 firstBufferedLineNo = 0;
22752279

22762280
Assert(cstate->rel);
22772281

@@ -2627,11 +2631,11 @@ CopyFromInsertBatch(CopyState cstate, EState *estate, CommandId mycid,
26272631
int hi_options, ResultRelInfo *resultRelInfo,
26282632
TupleTableSlot *myslot, BulkInsertState bistate,
26292633
int nBufferedTuples, HeapTuple *bufferedTuples,
2630-
int firstBufferedLineNo)
2634+
uint64 firstBufferedLineNo)
26312635
{
26322636
MemoryContext oldcontext;
26332637
int i;
2634-
int save_cur_lineno;
2638+
uint64 save_cur_lineno;
26352639

26362640
/*
26372641
* Print error context information correctly, if one of the operations

0 commit comments

Comments
 (0)