diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 00139b9..fab811a 100644 *** a/src/backend/commands/copy.c --- b/src/backend/commands/copy.c *************** NextCopyFrom(CopyState cstate, Datum *va *** 2415,2420 **** --- 2415,2448 ---- } /* + * Reset read pointer to the head of the file. + * This can't be used for COPY FROM STDIN. + */ + void + ResetCopyFrom(CopyState cstate) + { + /* stdin can't be rewinded */ + Assert(cstate->copy_dest == COPY_FILE); + + /* rewind seek pointer to the head */ + if (fseek(cstate->copy_file, 0, SEEK_SET)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not seek in file \"%s\": %m", + cstate->filename))); + cstate->fe_eof = false; + + /* reset error context */ + cstate->cur_lineno = 0; + cstate->cur_attname = NULL; + cstate->cur_attval = NULL; + + /* just clear the line buffer to avoid re-allocation */ + resetStringInfo(&cstate->line_buf); + } + + + /* * Clean up storage and release resources for COPY FROM. */ void diff --git a/src/include/commands/copy.h b/src/include/commands/copy.h index 8340e3d..424d170 100644 *** a/src/include/commands/copy.h --- b/src/include/commands/copy.h *************** extern CopyState BeginCopyFrom(Relation *** 28,33 **** --- 28,34 ---- extern void EndCopyFrom(CopyState cstate); extern bool NextCopyFrom(CopyState cstate, Datum *values, bool *nulls, Oid *tupleOid); + extern void ResetCopyFrom(CopyState cstate); extern void CopyFromErrorCallback(void *arg); extern DestReceiver *CreateCopyDestReceiver(void);