181 else if (
opts->binary)
205 bool hdr_delim =
false;
268 bool need_delim =
false;
401 for (
i = 0;
i < natts;
i++)
452 if (fwrite(fe_msgbuf->
data, fe_msgbuf->
len, 1,
477 errmsg(
"could not write to COPY program: %m")));
482 errmsg(
"could not write to COPY file: %m")));
572 errmsg(
"could not close pipe to external command: %m")));
573 else if (pclose_rc != 0)
576 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
577 errmsg(
"program \"%s\" failed",
598 errmsg(
"could not close file \"%s\": %m",
634 bool pipe = (
filename == NULL && data_dest_cb == NULL);
638 const int progress_cols[] = {
642 int64 progress_vals[] = {
647 if (rel != NULL && rel->
rd_rel->relkind != RELKIND_RELATION)
649 if (rel->
rd_rel->relkind == RELKIND_VIEW)
651 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
652 errmsg(
"cannot copy from view \"%s\"",
654 errhint(
"Try the COPY (SELECT ...) TO variant.")));
655 else if (rel->
rd_rel->relkind == RELKIND_MATVIEW)
657 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
658 errmsg(
"cannot copy from materialized view \"%s\"",
660 errhint(
"Try the COPY (SELECT ...) TO variant.")));
661 else if (rel->
rd_rel->relkind == RELKIND_FOREIGN_TABLE)
663 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
664 errmsg(
"cannot copy from foreign table \"%s\"",
666 errhint(
"Try the COPY (SELECT ...) TO variant.")));
667 else if (rel->
rd_rel->relkind == RELKIND_SEQUENCE)
669 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
670 errmsg(
"cannot copy from sequence \"%s\"",
672 else if (rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
674 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
675 errmsg(
"cannot copy from partitioned table \"%s\"",
677 errhint(
"Try the COPY (SELECT ...) TO variant.")));
680 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
681 errmsg(
"cannot copy from non-table relation \"%s\"",
732 if (rewritten ==
NIL)
735 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
736 errmsg(
"DO INSTEAD NOTHING rules are not supported for COPY")));
743 foreach(lc, rewritten)
749 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
750 errmsg(
"conditional DO INSTEAD rules are not supported for COPY")));
753 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
754 errmsg(
"DO ALSO rules are not supported for COPY")));
758 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
759 errmsg(
"multi-statement DO INSTEAD rules are not supported for COPY")));
768 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
769 errmsg(
"COPY (SELECT INTO) is not supported")));
774 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
775 errmsg(
"COPY query must not be a utility command")));
790 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
791 errmsg(
"COPY query must have a RETURNING clause")));
819 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
820 errmsg(
"relation referenced by COPY statement has changed")));
838 dest, NULL, NULL, 0);
846 elog(
ERROR,
"ExecutorStart() failed unexpectedly");
854 num_phys_attrs = tupDesc->
natts;
869 foreach(
cur, attnums)
876 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
878 errmsg(
"%s column \"%s\" not referenced by COPY",
879 "FORCE_QUOTE",
NameStr(attr->attname))));
931 errmsg(
"could not execute command \"%s\": %m",
947 (
errcode(ERRCODE_INVALID_NAME),
948 errmsg(
"relative path not allowed for COPY to file")));
963 int save_errno = errno;
967 errmsg(
"could not open file \"%s\" for writing: %m",
969 (save_errno == ENOENT || save_errno == EACCES) ?
970 errhint(
"COPY TO instructs the PostgreSQL server process to write a file. "
971 "You may want a client-side facility such as psql's \\copy.") : 0));
977 errmsg(
"could not stat file \"%s\": %m",
982 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1040 num_phys_attrs = tupDesc->
natts;
1138#define DUMPSOFAR() \
1141 CopySendData(cstate, start, ptr - start); \
1174 while ((
c = *ptr) !=
'\0')
1176 if ((
unsigned char)
c < (
unsigned char) 0x20)
1219 else if (
c ==
'\\' ||
c == delimc)
1234 while ((
c = *ptr) !=
'\0')
1236 if ((
unsigned char)
c < (
unsigned char) 0x20)
1279 else if (
c ==
'\\' ||
c == delimc)
1330 if (single_attr && strcmp(ptr,
"\\.") == 0)
1334 const char *tptr = ptr;
1336 while ((
c = *tptr) !=
'\0')
1338 if (
c == delimc ||
c == quotec ||
c ==
'\n' ||
c ==
'\r')
1359 while ((
c = *ptr) !=
'\0')
1361 if (
c == quotec ||
c == escapec)
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 pg_attribute_always_inline
#define MemSet(start, val, len)
static void CopyToBinaryOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo)
static void CopyToBinaryOneRow(CopyToState cstate, TupleTableSlot *slot)
static void CopySendInt32(CopyToState cstate, int32 val)
static void ClosePipeToProgram(CopyToState cstate)
static const CopyToRoutine CopyToRoutineCSV
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 CopyToTextLikeEnd(CopyToState cstate)
static void CopyAttributeOutText(CopyToState cstate, const char *string)
struct CopyToStateData CopyToStateData
static const CopyToRoutine CopyToRoutineText
static void CopySendInt16(CopyToState cstate, int16 val)
static void CopySendData(CopyToState cstate, const void *databuf, int datasize)
static void CopyToTextOneRow(CopyToState cstate, TupleTableSlot *slot)
static void CopySendChar(CopyToState cstate, char c)
DestReceiver * CreateCopyDestReceiver(void)
static const CopyToRoutine * CopyToGetRoutine(const CopyFormatOptions *opts)
static void CopySendTextLikeEndOfRow(CopyToState cstate)
static void EndCopy(CopyToState cstate)
static void copy_dest_destroy(DestReceiver *self)
static void CopyToTextLikeOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo)
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 CopyToTextLikeStart(CopyToState cstate, TupleDesc tupDesc)
static void SendCopyBegin(CopyToState cstate)
static void SendCopyEnd(CopyToState cstate)
static void CopySendEndOfRow(CopyToState cstate)
static void CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
static void CopyToTextLikeOneRow(CopyToState cstate, TupleTableSlot *slot, bool is_csv)
static void CopySendString(CopyToState cstate, const char *str)
static void CopyToCSVOneRow(CopyToState cstate, TupleTableSlot *slot)
static const char BinarySignature[11]
void EndCopyTo(CopyToState cstate)
static void CopyToBinaryStart(CopyToState cstate, TupleDesc tupDesc)
static const CopyToRoutine CopyToRoutineBinary
static void CopyToBinaryEnd(CopyToState cstate)
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,...)
bool ExecutorStart(QueryDesc *queryDesc, int eflags)
void ExecutorEnd(QueryDesc *queryDesc)
void ExecutorFinish(QueryDesc *queryDesc)
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
FILE * OpenPipeStream(const char *command, const char *mode)
int ClosePipeStream(FILE *file)
FILE * AllocateFile(const char *name, const char *mode)
void fmgr_info(Oid functionId, FmgrInfo *finfo)
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Assert(PointerIsAligned(start, uint64))
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_)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
#define CURSOR_OPT_PARALLEL_OK
static AmcheckOptions opts
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)
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
CommandDest whereToSendOutput
List * pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree, const char *query_string, const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
void FreeQueryDesc(QueryDesc *qdesc)
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, CachedPlan *cplan, 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
#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)
void(* CopyToOutFunc)(CopyToState cstate, Oid atttypid, FmgrInfo *finfo)
void(* CopyToOneRow)(CopyToState cstate, TupleTableSlot *slot)
void(* CopyToEnd)(CopyToState cstate)
void(* CopyToStart)(CopyToState cstate, TupleDesc tupDesc)
MemoryContext copycontext
const CopyToRoutine * routine
copy_data_dest_cb data_dest_cb
bool encoding_embeds_ascii
const char * p_sourcetext
void(* rStartup)(DestReceiver *self, int operation, TupleDesc typeinfo)
void(* rShutdown)(DestReceiver *self)
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
void(* rDestroy)(DestReceiver *self)
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)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
static void slot_getallattrs(TupleTableSlot *slot)
char * wait_result_to_str(int exitstatus)
int pg_encoding_mblen(int encoding, const char *mbstr)