143 for (
i = 0;
i < natts;
i++)
204 if (fwrite(fe_msgbuf->
data, fe_msgbuf->
len, 1,
229 errmsg(
"could not write to COPY program: %m")));
234 errmsg(
"could not write to COPY file: %m")));
299 errmsg(
"could not close pipe to external command: %m")));
300 else if (pclose_rc != 0)
303 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
304 errmsg(
"program \"%s\" failed",
325 errmsg(
"could not close file \"%s\": %m",
361 bool pipe = (
filename == NULL && data_dest_cb == NULL);
365 const int progress_cols[] = {
369 int64 progress_vals[] = {
374 if (rel != NULL && rel->
rd_rel->relkind != RELKIND_RELATION)
376 if (rel->
rd_rel->relkind == RELKIND_VIEW)
378 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
379 errmsg(
"cannot copy from view \"%s\"",
381 errhint(
"Try the COPY (SELECT ...) TO variant.")));
382 else if (rel->
rd_rel->relkind == RELKIND_MATVIEW)
384 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
385 errmsg(
"cannot copy from materialized view \"%s\"",
387 errhint(
"Try the COPY (SELECT ...) TO variant.")));
388 else if (rel->
rd_rel->relkind == RELKIND_FOREIGN_TABLE)
390 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
391 errmsg(
"cannot copy from foreign table \"%s\"",
393 errhint(
"Try the COPY (SELECT ...) TO variant.")));
394 else if (rel->
rd_rel->relkind == RELKIND_SEQUENCE)
396 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
397 errmsg(
"cannot copy from sequence \"%s\"",
399 else if (rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
401 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
402 errmsg(
"cannot copy from partitioned table \"%s\"",
404 errhint(
"Try the COPY (SELECT ...) TO variant.")));
407 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
408 errmsg(
"cannot copy from non-table relation \"%s\"",
456 if (rewritten ==
NIL)
459 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
460 errmsg(
"DO INSTEAD NOTHING rules are not supported for COPY")));
467 foreach(lc, rewritten)
473 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
474 errmsg(
"conditional DO INSTEAD rules are not supported for COPY")));
477 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
478 errmsg(
"DO ALSO rules are not supported for COPY")));
482 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
483 errmsg(
"multi-statement DO INSTEAD rules are not supported for COPY")));
492 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
493 errmsg(
"COPY (SELECT INTO) is not supported")));
498 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
499 errmsg(
"COPY query must not be a utility command")));
514 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
515 errmsg(
"COPY query must have a RETURNING clause")));
543 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
544 errmsg(
"relation referenced by COPY statement has changed")));
562 dest, NULL, NULL, 0);
577 num_phys_attrs = tupDesc->
natts;
592 foreach(
cur, attnums)
599 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
601 errmsg(
"%s column \"%s\" not referenced by COPY",
602 "FORCE_QUOTE",
NameStr(attr->attname))));
654 errmsg(
"could not execute command \"%s\": %m",
670 (
errcode(ERRCODE_INVALID_NAME),
671 errmsg(
"relative path not allowed for COPY to file")));
686 int save_errno = errno;
690 errmsg(
"could not open file \"%s\" for writing: %m",
692 (save_errno == ENOENT || save_errno == EACCES) ?
693 errhint(
"COPY TO instructs the PostgreSQL server process to write a file. "
694 "You may want a client-side facility such as psql's \\copy.") : 0));
700 errmsg(
"could not stat file \"%s\": %m",
705 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
763 num_phys_attrs = tupDesc->
natts;
827 bool hdr_delim =
false;
926 bool need_delim =
false;
981 #define DUMPSOFAR() \
984 CopySendData(cstate, start, ptr - start); \
1017 while ((
c = *ptr) !=
'\0')
1019 if ((
unsigned char)
c < (
unsigned char) 0x20)
1062 else if (
c ==
'\\' ||
c == delimc)
1077 while ((
c = *ptr) !=
'\0')
1079 if ((
unsigned char)
c < (
unsigned char) 0x20)
1122 else if (
c ==
'\\' ||
c == delimc)
1173 if (single_attr && strcmp(ptr,
"\\.") == 0)
1177 const char *tptr = ptr;
1179 while ((
c = *tptr) !=
'\0')
1181 if (
c == delimc ||
c == quotec ||
c ==
'\n' ||
c ==
'\r')
1202 while ((
c = *ptr) !=
'\0')
1204 if (
c == quotec ||
c == escapec)
1286 self->cstate = NULL;
1287 self->processed = 0;
List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *opts_out, bool is_from, List *options)
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
void pgstat_progress_end_command(void)
#define IS_HIGHBIT_SET(ch)
#define Assert(condition)
#define MemSet(start, val, len)
static void CopySendInt32(CopyToState cstate, int32 val)
static void ClosePipeToProgram(CopyToState cstate)
static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
static void CopyAttributeOutCSV(CopyToState cstate, const char *string, bool use_quote)
uint64 DoCopyTo(CopyToState cstate)
static void CopyAttributeOutText(CopyToState cstate, const char *string)
struct CopyToStateData CopyToStateData
static void CopySendInt16(CopyToState cstate, int16 val)
static void CopySendData(CopyToState cstate, const void *databuf, int datasize)
static void CopySendChar(CopyToState cstate, char c)
static void EndCopy(CopyToState cstate)
static void copy_dest_destroy(DestReceiver *self)
CopyToState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *raw_query, Oid queryRelId, const char *filename, bool is_program, copy_data_dest_cb data_dest_cb, List *attnamelist, List *options)
static void copy_dest_shutdown(DestReceiver *self)
static void copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
static void SendCopyBegin(CopyToState cstate)
static void SendCopyEnd(CopyToState cstate)
static void CopySendEndOfRow(CopyToState cstate)
static void CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
static void CopySendString(CopyToState cstate, const char *str)
static const char BinarySignature[11]
void EndCopyTo(CopyToState cstate)
DestReceiver * CreateCopyDestReceiver(void)
DestReceiver * CreateDestReceiver(CommandDest dest)
int errdetail_internal(const char *fmt,...)
int errcode_for_file_access(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void ExecutorEnd(QueryDesc *queryDesc)
void ExecutorFinish(QueryDesc *queryDesc)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
FILE * AllocateFile(const char *name, const char *mode)
int ClosePipeStream(FILE *file)
FILE * OpenPipeStream(const char *command, const char *mode)
void fmgr_info(Oid functionId, FmgrInfo *finfo)
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
void(* copy_data_dest_cb)(void *data, int len)
#define pq_putmessage(msgtype, s, len)
bool list_member_int(const List *list, int datum)
bool list_member_oid(const List *list, Oid datum)
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
int GetDatabaseEncoding(void)
int pg_get_client_encoding(void)
char * pg_server_to_any(const char *s, int len, int encoding)
void MemoryContextReset(MemoryContext context)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define CHECK_FOR_INTERRUPTS()
#define IsA(nodeptr, _type_)
#define CURSOR_OPT_PARALLEL_OK
FormData_pg_attribute * Form_pg_attribute
#define lfirst_node(type, lc)
static int list_length(const List *l)
#define linitial_node(type, l)
#define foreach_int(var, lst)
#define PG_ENCODING_IS_CLIENT_ONLY(_enc)
#define is_absolute_path(filename)
CommandDest whereToSendOutput
List * pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree, const char *query_string, const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
void FreeQueryDesc(QueryDesc *qdesc)
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
#define PROGRESS_COPY_COMMAND
#define PROGRESS_COPY_TYPE_FILE
#define PROGRESS_COPY_BYTES_PROCESSED
#define PROGRESS_COPY_COMMAND_TO
#define PROGRESS_COPY_TUPLES_PROCESSED
#define PROGRESS_COPY_TYPE
#define PROGRESS_COPY_TYPE_PROGRAM
#define PROGRESS_COPY_TYPE_CALLBACK
#define PROGRESS_COPY_TYPE_PIPE
#define PqMsg_CopyOutResponse
MemoryContextSwitchTo(old_ctx)
#define RelationGetRelid(relation)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
void UpdateActiveSnapshotCommandId(void)
void PopActiveSnapshot(void)
void PushCopiedSnapshot(Snapshot snapshot)
Snapshot GetActiveSnapshot(void)
StringInfo makeStringInfo(void)
void resetStringInfo(StringInfo str)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
#define appendStringInfoCharMacro(str, ch)
MemoryContext copycontext
copy_data_dest_cb data_dest_cb
bool encoding_embeds_ascii
const char * p_sourcetext
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key)
static void table_endscan(TableScanDesc scan)
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
#define TupleDescAttr(tupdesc, i)
static void slot_getallattrs(TupleTableSlot *slot)
char * wait_result_to_str(int exitstatus)
int pg_encoding_mblen(int encoding, const char *mbstr)