PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
copyfrom_internal.h File Reference
#include "commands/copy.h"
#include "commands/trigger.h"
#include "nodes/miscnodes.h"
Include dependency graph for copyfrom_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  CopyFromStateData
 

Macros

#define INPUT_BUF_SIZE   65536 /* we palloc INPUT_BUF_SIZE+1 bytes */
 
#define INPUT_BUF_BYTES(cstate)   ((cstate)->input_buf_len - (cstate)->input_buf_index)
 
#define RAW_BUF_SIZE   65536 /* we palloc RAW_BUF_SIZE+1 bytes */
 
#define RAW_BUF_BYTES(cstate)   ((cstate)->raw_buf_len - (cstate)->raw_buf_index)
 

Typedefs

typedef enum CopySource CopySource
 
typedef enum EolType EolType
 
typedef enum CopyInsertMethod CopyInsertMethod
 
typedef struct CopyFromStateData CopyFromStateData
 

Enumerations

enum  CopySource { COPY_FILE , COPY_FRONTEND , COPY_CALLBACK }
 
enum  EolType { EOL_UNKNOWN , EOL_NL , EOL_CR , EOL_CRNL }
 
enum  CopyInsertMethod { CIM_SINGLE , CIM_MULTI , CIM_MULTI_CONDITIONAL }
 

Functions

void ReceiveCopyBegin (CopyFromState cstate)
 
void ReceiveCopyBinaryHeader (CopyFromState cstate)
 
bool CopyFromTextOneRow (CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
 
bool CopyFromCSVOneRow (CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
 
bool CopyFromBinaryOneRow (CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
 

Macro Definition Documentation

◆ INPUT_BUF_BYTES

#define INPUT_BUF_BYTES (   cstate)    ((cstate)->input_buf_len - (cstate)->input_buf_index)

Definition at line 167 of file copyfrom_internal.h.

◆ INPUT_BUF_SIZE

#define INPUT_BUF_SIZE   65536 /* we palloc INPUT_BUF_SIZE+1 bytes */

Definition at line 160 of file copyfrom_internal.h.

◆ RAW_BUF_BYTES

#define RAW_BUF_BYTES (   cstate)    ((cstate)->raw_buf_len - (cstate)->raw_buf_index)

Definition at line 181 of file copyfrom_internal.h.

◆ RAW_BUF_SIZE

#define RAW_BUF_SIZE   65536 /* we palloc RAW_BUF_SIZE+1 bytes */

Definition at line 174 of file copyfrom_internal.h.

Typedef Documentation

◆ CopyFromStateData

◆ CopyInsertMethod

◆ CopySource

typedef enum CopySource CopySource

◆ EolType

typedef enum EolType EolType

Enumeration Type Documentation

◆ CopyInsertMethod

Enumerator
CIM_SINGLE 
CIM_MULTI 
CIM_MULTI_CONDITIONAL 

Definition at line 46 of file copyfrom_internal.h.

47{
48 CIM_SINGLE, /* use table_tuple_insert or ExecForeignInsert */
49 CIM_MULTI, /* always use table_multi_insert or
50 * ExecForeignBatchInsert */
51 CIM_MULTI_CONDITIONAL, /* use table_multi_insert or
52 * ExecForeignBatchInsert only if valid */
CopyInsertMethod
@ CIM_SINGLE
@ CIM_MULTI_CONDITIONAL
@ CIM_MULTI

◆ CopySource

enum CopySource
Enumerator
COPY_FILE 
COPY_FRONTEND 
COPY_CALLBACK 

Definition at line 25 of file copyfrom_internal.h.

26{
27 COPY_FILE, /* from file (or a piped program) */
28 COPY_FRONTEND, /* from frontend */
29 COPY_CALLBACK, /* from callback function */
CopySource
@ COPY_FILE
@ COPY_CALLBACK
@ COPY_FRONTEND

◆ EolType

enum EolType
Enumerator
EOL_UNKNOWN 
EOL_NL 
EOL_CR 
EOL_CRNL 

Definition at line 35 of file copyfrom_internal.h.

36{
38 EOL_NL,
39 EOL_CR,
41} EolType;
@ EOL_CR
@ EOL_CRNL
@ EOL_UNKNOWN
@ EOL_NL

Function Documentation

◆ CopyFromBinaryOneRow()

bool CopyFromBinaryOneRow ( CopyFromState  cstate,
ExprContext econtext,
Datum values,
bool *  nulls 
)

Definition at line 1086 of file copyfromparse.c.

1088{
1089 TupleDesc tupDesc;
1090 AttrNumber attr_count;
1091 FmgrInfo *in_functions = cstate->in_functions;
1092 Oid *typioparams = cstate->typioparams;
1093 int16 fld_count;
1094 ListCell *cur;
1095
1096 tupDesc = RelationGetDescr(cstate->rel);
1097 attr_count = list_length(cstate->attnumlist);
1098
1099 cstate->cur_lineno++;
1100
1101 if (!CopyGetInt16(cstate, &fld_count))
1102 {
1103 /* EOF detected (end of file, or protocol-level EOF) */
1104 return false;
1105 }
1106
1107 if (fld_count == -1)
1108 {
1109 /*
1110 * Received EOF marker. Wait for the protocol-level EOF, and complain
1111 * if it doesn't come immediately. In COPY FROM STDIN, this ensures
1112 * that we correctly handle CopyFail, if client chooses to send that
1113 * now. When copying from file, we could ignore the rest of the file
1114 * like in text mode, but we choose to be consistent with the COPY
1115 * FROM STDIN case.
1116 */
1117 char dummy;
1118
1119 if (CopyReadBinaryData(cstate, &dummy, 1) > 0)
1120 ereport(ERROR,
1121 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1122 errmsg("received copy data after EOF marker")));
1123 return false;
1124 }
1125
1126 if (fld_count != attr_count)
1127 ereport(ERROR,
1128 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
1129 errmsg("row field count is %d, expected %d",
1130 (int) fld_count, attr_count)));
1131
1132 foreach(cur, cstate->attnumlist)
1133 {
1134 int attnum = lfirst_int(cur);
1135 int m = attnum - 1;
1136 Form_pg_attribute att = TupleDescAttr(tupDesc, m);
1137
1138 cstate->cur_attname = NameStr(att->attname);
1139 values[m] = CopyReadBinaryAttribute(cstate,
1140 &in_functions[m],
1141 typioparams[m],
1142 att->atttypmod,
1143 &nulls[m]);
1144 cstate->cur_attname = NULL;
1145 }
1146
1147 return true;
1148}
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define NameStr(name)
Definition: c.h:717
int16_t int16
Definition: c.h:497
static bool CopyGetInt16(CopyFromState cstate, int16 *val)
static Datum CopyReadBinaryAttribute(CopyFromState cstate, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
static int CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes)
struct cursor * cur
Definition: ecpg.c:29
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
static int list_length(const List *l)
Definition: pg_list.h:152
#define lfirst_int(lc)
Definition: pg_list.h:173
unsigned int Oid
Definition: postgres_ext.h:30
#define RelationGetDescr(relation)
Definition: rel.h:542
const char * cur_attname
Definition: fmgr.h:57
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

References attnum, CopyFromStateData::attnumlist, CopyGetInt16(), CopyReadBinaryAttribute(), CopyReadBinaryData(), cur, CopyFromStateData::cur_attname, CopyFromStateData::cur_lineno, ereport, errcode(), errmsg(), ERROR, CopyFromStateData::in_functions, lfirst_int, list_length(), NameStr, CopyFromStateData::rel, RelationGetDescr, TupleDescAttr(), CopyFromStateData::typioparams, and values.

◆ CopyFromCSVOneRow()

bool CopyFromCSVOneRow ( CopyFromState  cstate,
ExprContext econtext,
Datum values,
bool *  nulls 
)

Definition at line 924 of file copyfromparse.c.

926{
927 return CopyFromTextLikeOneRow(cstate, econtext, values, nulls, true);
928}
static pg_attribute_always_inline bool CopyFromTextLikeOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls, bool is_csv)

References CopyFromTextLikeOneRow(), and values.

◆ CopyFromTextOneRow()

bool CopyFromTextOneRow ( CopyFromState  cstate,
ExprContext econtext,
Datum values,
bool *  nulls 
)

Definition at line 916 of file copyfromparse.c.

918{
919 return CopyFromTextLikeOneRow(cstate, econtext, values, nulls, false);
920}

References CopyFromTextLikeOneRow(), and values.

◆ ReceiveCopyBegin()

void ReceiveCopyBegin ( CopyFromState  cstate)

Definition at line 170 of file copyfromparse.c.

171{
173 int natts = list_length(cstate->attnumlist);
174 int16 format = (cstate->opts.binary ? 1 : 0);
175 int i;
176
178 pq_sendbyte(&buf, format); /* overall format */
179 pq_sendint16(&buf, natts);
180 for (i = 0; i < natts; i++)
181 pq_sendint16(&buf, format); /* per-column formats */
183 cstate->copy_src = COPY_FRONTEND;
184 cstate->fe_msgbuf = makeStringInfo();
185 /* We *must* flush here to ensure FE knows it can send. */
186 pq_flush();
187}
@ COPY_FRONTEND
Definition: copyto.c:46
int i
Definition: isn.c:77
#define pq_flush()
Definition: libpq.h:46
static char format
static char * buf
Definition: pg_test_fsync.c:72
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:160
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:136
#define PqMsg_CopyInResponse
Definition: protocol.h:45
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72
bool binary
Definition: copy.h:64
CopyFormatOptions opts

References CopyFromStateData::attnumlist, CopyFormatOptions::binary, buf, COPY_FRONTEND, CopyFromStateData::copy_src, CopyFromStateData::fe_msgbuf, format, i, list_length(), makeStringInfo(), CopyFromStateData::opts, pq_beginmessage(), pq_endmessage(), pq_flush, pq_sendbyte(), pq_sendint16(), and PqMsg_CopyInResponse.

Referenced by BeginCopyFrom().

◆ ReceiveCopyBinaryHeader()

void ReceiveCopyBinaryHeader ( CopyFromState  cstate)

Definition at line 190 of file copyfromparse.c.

191{
192 char readSig[11];
193 int32 tmp;
194
195 /* Signature */
196 if (CopyReadBinaryData(cstate, readSig, 11) != 11 ||
197 memcmp(readSig, BinarySignature, 11) != 0)
199 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
200 errmsg("COPY file signature not recognized")));
201 /* Flags field */
202 if (!CopyGetInt32(cstate, &tmp))
204 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
205 errmsg("invalid COPY file header (missing flags)")));
206 if ((tmp & (1 << 16)) != 0)
208 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
209 errmsg("invalid COPY file header (WITH OIDS)")));
210 tmp &= ~(1 << 16);
211 if ((tmp >> 16) != 0)
213 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
214 errmsg("unrecognized critical flags in COPY file header")));
215 /* Header extension length */
216 if (!CopyGetInt32(cstate, &tmp) ||
217 tmp < 0)
219 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
220 errmsg("invalid COPY file header (missing length)")));
221 /* Skip extension header, if present */
222 while (tmp-- > 0)
223 {
224 if (CopyReadBinaryData(cstate, readSig, 1) != 1)
226 (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
227 errmsg("invalid COPY file header (wrong length)")));
228 }
229}
int32_t int32
Definition: c.h:498
static bool CopyGetInt32(CopyFromState cstate, int32 *val)
static const char BinarySignature[11]

References BinarySignature, CopyGetInt32(), CopyReadBinaryData(), ereport, errcode(), errmsg(), and ERROR.

Referenced by CopyFromBinaryStart().