120 bool use_quote,
bool single_attr);
148 for (
i = 0;
i < natts;
i++)
209 if (fwrite(fe_msgbuf->
data, fe_msgbuf->
len, 1,
234 errmsg(
"could not write to COPY program: %m")));
239 errmsg(
"could not write to COPY file: %m")));
301 errmsg(
"could not close pipe to external command: %m")));
302 else if (pclose_rc != 0)
305 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
306 errmsg(
"program \"%s\" failed",
327 errmsg(
"could not close file \"%s\": %m",
355 const int progress_cols[] = {
359 int64 progress_vals[] = {
364 if (rel != NULL && rel->
rd_rel->relkind != RELKIND_RELATION)
366 if (rel->
rd_rel->relkind == RELKIND_VIEW)
368 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
369 errmsg(
"cannot copy from view \"%s\"",
371 errhint(
"Try the COPY (SELECT ...) TO variant.")));
372 else if (rel->
rd_rel->relkind == RELKIND_MATVIEW)
374 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
375 errmsg(
"cannot copy from materialized view \"%s\"",
377 errhint(
"Try the COPY (SELECT ...) TO variant.")));
378 else if (rel->
rd_rel->relkind == RELKIND_FOREIGN_TABLE)
380 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
381 errmsg(
"cannot copy from foreign table \"%s\"",
383 errhint(
"Try the COPY (SELECT ...) TO variant.")));
384 else if (rel->
rd_rel->relkind == RELKIND_SEQUENCE)
386 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
387 errmsg(
"cannot copy from sequence \"%s\"",
389 else if (rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
391 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
392 errmsg(
"cannot copy from partitioned table \"%s\"",
394 errhint(
"Try the COPY (SELECT ...) TO variant.")));
397 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
398 errmsg(
"cannot copy from non-table relation \"%s\"",
446 if (rewritten ==
NIL)
449 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
450 errmsg(
"DO INSTEAD NOTHING rules are not supported for COPY")));
457 foreach(lc, rewritten)
463 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
464 errmsg(
"conditional DO INSTEAD rules are not supported for COPY")));
467 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
468 errmsg(
"DO ALSO rules are not supported for the COPY")));
472 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
473 errmsg(
"multi-statement DO INSTEAD rules are not supported for COPY")));
482 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
483 errmsg(
"COPY (SELECT INTO) is not supported")));
499 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
500 errmsg(
"COPY query must have a RETURNING clause")));
528 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
529 errmsg(
"relation referenced by COPY statement has changed")));
547 dest, NULL, NULL, 0);
562 num_phys_attrs = tupDesc->
natts;
570 for (
i = 0;
i < num_phys_attrs;
i++)
580 foreach(
cur, attnums)
587 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
588 errmsg(
"FORCE_QUOTE column \"%s\" not referenced by COPY",
603 foreach(
cur, attnums)
610 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
611 errmsg(
"FORCE_NOT_NULL column \"%s\" not referenced by COPY",
626 foreach(
cur, attnums)
633 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
634 errmsg(
"FORCE_NULL column \"%s\" not referenced by COPY",
679 errmsg(
"could not execute command \"%s\": %m",
695 (
errcode(ERRCODE_INVALID_NAME),
696 errmsg(
"relative path not allowed for COPY to file")));
711 int save_errno = errno;
715 errmsg(
"could not open file \"%s\" for writing: %m",
717 (save_errno == ENOENT || save_errno == EACCES) ?
718 errhint(
"COPY TO instructs the PostgreSQL server process to write a file. "
719 "You may want a client-side facility such as psql's \\copy.") : 0));
725 errmsg(
"could not stat file \"%s\": %m",
730 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
772 bool pipe = (cstate->
filename == NULL);
786 num_phys_attrs = tupDesc->
natts;
850 bool hdr_delim =
false;
933 bool need_delim =
false;
1005 #define DUMPSOFAR() \
1008 CopySendData(cstate, start, ptr - start); \
1041 while ((
c = *ptr) !=
'\0')
1043 if ((
unsigned char)
c < (
unsigned char) 0x20)
1086 else if (
c ==
'\\' ||
c == delimc)
1101 while ((
c = *ptr) !=
'\0')
1103 if ((
unsigned char)
c < (
unsigned char) 0x20)
1146 else if (
c ==
'\\' ||
c == delimc)
1166 bool use_quote,
bool single_attr)
1193 if (single_attr && strcmp(ptr,
"\\.") == 0)
1197 const char *tptr = ptr;
1199 while ((
c = *tptr) !=
'\0')
1201 if (
c == delimc ||
c == quotec ||
c ==
'\n' ||
c ==
'\r')
1222 while ((
c = *ptr) !=
'\0')
1224 if (
c == quotec ||
c == escapec)
1306 self->cstate = NULL;
1307 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)
static void CopySendInt32(CopyToState cstate, int32 val)
static void ClosePipeToProgram(CopyToState cstate)
static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
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)
static void CopyAttributeOutCSV(CopyToState cstate, const char *string, bool use_quote, bool single_attr)
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)
CopyToState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *raw_query, Oid queryRelId, const char *filename, bool is_program, List *attnamelist, List *options)
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)
#define pq_putmessage(msgtype, s, len)
Assert(fmt[strlen(fmt) - 1] !='\n')
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_database_encoding_max_length(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_)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
#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 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_PIPE
#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 char *data, int datalen)
#define appendStringInfoCharMacro(str, ch)
MemoryContext copycontext
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)