186 else if (
opts->binary)
210 bool hdr_delim =
false;
273 bool need_delim =
false;
406 for (
i = 0;
i < natts;
i++)
457 if (fwrite(fe_msgbuf->
data, fe_msgbuf->
len, 1,
482 errmsg(
"could not write to COPY program: %m")));
487 errmsg(
"could not write to COPY file: %m")));
577 errmsg(
"could not close pipe to external command: %m")));
578 else if (pclose_rc != 0)
581 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
582 errmsg(
"program \"%s\" failed",
603 errmsg(
"could not close file \"%s\": %m",
643 bool pipe = (
filename == NULL && data_dest_cb == NULL);
647 const int progress_cols[] = {
651 int64 progress_vals[] = {
657 if (rel != NULL && rel->
rd_rel->relkind != RELKIND_RELATION)
659 if (rel->
rd_rel->relkind == RELKIND_VIEW)
661 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
662 errmsg(
"cannot copy from view \"%s\"",
664 errhint(
"Try the COPY (SELECT ...) TO variant.")));
665 else if (rel->
rd_rel->relkind == RELKIND_MATVIEW)
669 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
670 errmsg(
"cannot copy from unpopulated materialized view \"%s\"",
672 errhint(
"Use the REFRESH MATERIALIZED VIEW command."));
674 else if (rel->
rd_rel->relkind == RELKIND_FOREIGN_TABLE)
676 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
677 errmsg(
"cannot copy from foreign table \"%s\"",
679 errhint(
"Try the COPY (SELECT ...) TO variant.")));
680 else if (rel->
rd_rel->relkind == RELKIND_SEQUENCE)
682 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
683 errmsg(
"cannot copy from sequence \"%s\"",
685 else if (rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
697 if (relkind == RELKIND_FOREIGN_TABLE)
702 errcode(ERRCODE_WRONG_OBJECT_TYPE),
703 errmsg(
"cannot copy from foreign table \"%s\"", relation_name),
704 errdetail(
"Partition \"%s\" is a foreign table in partitioned table \"%s\"",
706 errhint(
"Try the COPY (SELECT ...) TO variant."));
710 if (RELKIND_HAS_PARTITIONS(relkind))
716 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
717 errmsg(
"cannot copy from non-table relation \"%s\"",
770 if (rewritten ==
NIL)
773 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
774 errmsg(
"DO INSTEAD NOTHING rules are not supported for COPY")));
781 foreach(lc, rewritten)
787 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
788 errmsg(
"conditional DO INSTEAD rules are not supported for COPY")));
791 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
792 errmsg(
"DO ALSO rules are not supported for COPY")));
796 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
797 errmsg(
"multi-statement DO INSTEAD rules are not supported for COPY")));
806 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
807 errmsg(
"COPY (SELECT INTO) is not supported")));
812 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
813 errmsg(
"COPY query must not be a utility command")));
828 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
829 errmsg(
"COPY query must have a RETURNING clause")));
857 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
858 errmsg(
"relation referenced by COPY statement has changed")));
876 dest, NULL, NULL, 0);
891 num_phys_attrs = tupDesc->
natts;
906 foreach(
cur, attnums)
913 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
915 errmsg(
"%s column \"%s\" not referenced by COPY",
916 "FORCE_QUOTE",
NameStr(attr->attname))));
968 errmsg(
"could not execute command \"%s\": %m",
984 (
errcode(ERRCODE_INVALID_NAME),
985 errmsg(
"relative path not allowed for COPY to file")));
1000 int save_errno = errno;
1004 errmsg(
"could not open file \"%s\" for writing: %m",
1006 (save_errno == ENOENT || save_errno == EACCES) ?
1007 errhint(
"COPY TO instructs the PostgreSQL server process to write a file. "
1008 "You may want a client-side facility such as psql's \\copy.") : 0));
1014 errmsg(
"could not stat file \"%s\": %m",
1019 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1077 num_phys_attrs = tupDesc->
natts;
1112 if (cstate->
rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1167 if (root_rel != NULL)
1202 if (root_slot != NULL)
1233#define DUMPSOFAR() \
1236 CopySendData(cstate, start, ptr - start); \
1269 while ((
c = *ptr) !=
'\0')
1271 if ((
unsigned char)
c < (
unsigned char) 0x20)
1314 else if (
c ==
'\\' ||
c == delimc)
1329 while ((
c = *ptr) !=
'\0')
1331 if ((
unsigned char)
c < (
unsigned char) 0x20)
1374 else if (
c ==
'\\' ||
c == delimc)
1425 if (single_attr && strcmp(ptr,
"\\.") == 0)
1429 const char *tptr = ptr;
1431 while ((
c = *tptr) !=
'\0')
1433 if (
c == delimc ||
c == quotec ||
c ==
'\n' ||
c ==
'\r')
1454 while ((
c = *ptr) !=
'\0')
1456 if (
c == quotec ||
c == escapec)
void free_attrmap(AttrMap *map)
AttrMap * build_attrmap_by_name_if_req(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
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 CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel, uint64 *processed)
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 errdetail(const char *fmt,...)
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)
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)
void list_free(List *list)
bool list_member_int(const List *list, int datum)
bool list_member_oid(const List *list, Oid datum)
char * get_rel_name(Oid relid)
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
char get_rel_relkind(Oid relid)
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
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
#define lfirst_node(type, lc)
static int list_length(const List *l)
#define linitial_node(type, l)
#define foreach_delete_current(lst, var_or_cell)
#define foreach_oid(var, lst)
#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, ExplainState *es)
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, 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)
#define RelationIsPopulated(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)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
static void table_endscan(TableScanDesc scan)
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key)
TupleTableSlot * execute_attr_map_slot(AttrMap *attrMap, TupleTableSlot *in_slot, TupleTableSlot *out_slot)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
static void slot_getallattrs(TupleTableSlot *slot)
static Size VARSIZE(const void *PTR)
static char * VARDATA(const void *PTR)
char * wait_result_to_str(int exitstatus)
int pg_encoding_mblen(int encoding, const char *mbstr)