PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 175 of file copyfrom_internal.h.

◆ INPUT_BUF_SIZE

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

Definition at line 168 of file copyfrom_internal.h.

◆ RAW_BUF_BYTES

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

Definition at line 189 of file copyfrom_internal.h.

◆ RAW_BUF_SIZE

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

Definition at line 182 of file copyfrom_internal.h.

Typedef Documentation

◆ CopyFromStateData

◆ CopyInsertMethod

◆ CopySource

◆ 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

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 */
@ COPY_FILE
@ COPY_CALLBACK
@ COPY_FRONTEND

◆ 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 
)
extern

Definition at line 1164 of file copyfromparse.c.

1166{
1167 TupleDesc tupDesc;
1169 FmgrInfo *in_functions = cstate->in_functions;
1170 Oid *typioparams = cstate->typioparams;
1172 ListCell *cur;
1173
1174 tupDesc = RelationGetDescr(cstate->rel);
1176
1177 cstate->cur_lineno++;
1178
1179 if (!CopyGetInt16(cstate, &fld_count))
1180 {
1181 /* EOF detected (end of file, or protocol-level EOF) */
1182 return false;
1183 }
1184
1185 if (fld_count == -1)
1186 {
1187 /*
1188 * Received EOF marker. Wait for the protocol-level EOF, and complain
1189 * if it doesn't come immediately. In COPY FROM STDIN, this ensures
1190 * that we correctly handle CopyFail, if client chooses to send that
1191 * now. When copying from file, we could ignore the rest of the file
1192 * like in text mode, but we choose to be consistent with the COPY
1193 * FROM STDIN case.
1194 */
1195 char dummy;
1196
1197 if (CopyReadBinaryData(cstate, &dummy, 1) > 0)
1198 ereport(ERROR,
1200 errmsg("received copy data after EOF marker")));
1201 return false;
1202 }
1203
1204 if (fld_count != attr_count)
1205 ereport(ERROR,
1207 errmsg("row field count is %d, expected %d",
1209
1210 foreach(cur, cstate->attnumlist)
1211 {
1212 int attnum = lfirst_int(cur);
1213 int m = attnum - 1;
1214 Form_pg_attribute att = TupleDescAttr(tupDesc, m);
1215
1216 cstate->cur_attname = NameStr(att->attname);
1217 values[m] = CopyReadBinaryAttribute(cstate,
1218 &in_functions[m],
1219 typioparams[m],
1220 att->atttypmod,
1221 &nulls[m]);
1222 cstate->cur_attname = NULL;
1223 }
1224
1225 return true;
1226}
int16 AttrNumber
Definition attnum.h:21
static Datum values[MAXATTR]
Definition bootstrap.c:188
#define NameStr(name)
Definition c.h:837
int16_t int16
Definition c.h:613
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:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
static char * errmsg
int16 attnum
FormData_pg_attribute * Form_pg_attribute
static int list_length(const List *l)
Definition pg_list.h:152
#define lfirst_int(lc)
Definition pg_list.h:173
unsigned int Oid
static int fb(int x)
#define RelationGetDescr(relation)
Definition rel.h:540
const char * cur_attname
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

References attnum, CopyFromStateData::attnumlist, CopyGetInt16(), CopyReadBinaryAttribute(), CopyReadBinaryData(), cur, CopyFromStateData::cur_attname, CopyFromStateData::cur_lineno, ereport, errcode(), errmsg, ERROR, fb(), 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 
)
extern

Definition at line 940 of file copyfromparse.c.

942{
943 return CopyFromTextLikeOneRow(cstate, econtext, values, nulls, true);
944}
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 
)
extern

Definition at line 932 of file copyfromparse.c.

934{
935 return CopyFromTextLikeOneRow(cstate, econtext, values, nulls, false);
936}

References CopyFromTextLikeOneRow(), and values.

◆ ReceiveCopyBegin()

void ReceiveCopyBegin ( CopyFromState  cstate)
extern

Definition at line 174 of file copyfromparse.c.

175{
177 int natts = list_length(cstate->attnumlist);
178 int16 format = (cstate->opts.format == COPY_FORMAT_BINARY ? 1 : 0);
179 int i;
180
182 pq_sendbyte(&buf, format); /* overall format */
183 pq_sendint16(&buf, natts);
184 for (i = 0; i < natts; i++)
185 pq_sendint16(&buf, format); /* per-column formats */
187 cstate->copy_src = COPY_FRONTEND;
188 cstate->fe_msgbuf = makeStringInfo();
189 /* We *must* flush here to ensure FE knows it can send. */
190 pq_flush();
191}
@ COPY_FRONTEND
Definition copyto.c:52
@ COPY_FORMAT_BINARY
Definition copy.h:58
int i
Definition isn.c:77
#define pq_flush()
Definition libpq.h:49
static char format
static char buf[DEFAULT_XLOG_SEG_SIZE]
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
CopyFormat format
Definition copy.h:73
CopyFormatOptions opts

References CopyFromStateData::attnumlist, buf, COPY_FORMAT_BINARY, COPY_FRONTEND, CopyFromStateData::copy_src, CopyFromStateData::fe_msgbuf, format, CopyFormatOptions::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)
extern

Definition at line 194 of file copyfromparse.c.

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

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

Referenced by CopyFromBinaryStart().