PostgreSQL Source Code  git master
copyfrom_internal.h File Reference
#include "commands/copy.h"
#include "commands/trigger.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 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_OLD_FE, COPY_NEW_FE, 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)
 

Macro Definition Documentation

◆ RAW_BUF_BYTES

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

Definition at line 159 of file copyfrom_internal.h.

Referenced by CopyLoadRawBuf(), and CopyReadBinaryData().

◆ RAW_BUF_SIZE

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

Definition at line 153 of file copyfrom_internal.h.

Referenced by BeginCopyFrom(), and CopyLoadRawBuf().

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 fdw routine */
49  CIM_MULTI, /* always use table_multi_insert */
50  CIM_MULTI_CONDITIONAL /* use table_multi_insert only if valid */
CopyInsertMethod

◆ CopySource

enum CopySource
Enumerator
COPY_FILE 
COPY_OLD_FE 
COPY_NEW_FE 
COPY_CALLBACK 

Definition at line 24 of file copyfrom_internal.h.

25 {
26  COPY_FILE, /* from file (or a piped program) */
27  COPY_OLD_FE, /* from frontend (2.0 protocol) */
28  COPY_NEW_FE, /* from frontend (3.0 protocol) */
29  COPY_CALLBACK /* from callback function */
30 } CopySource;
CopySource

◆ EolType

enum EolType
Enumerator
EOL_UNKNOWN 
EOL_NL 
EOL_CR 
EOL_CRNL 

Definition at line 35 of file copyfrom_internal.h.

Function Documentation

◆ ReceiveCopyBegin()

void ReceiveCopyBegin ( CopyFromState  cstate)

Definition at line 125 of file copyfromparse.c.

References CopyFromStateData::attnumlist, CopyFormatOptions::binary, buf, COPY_NEW_FE, COPY_OLD_FE, CopyFromStateData::copy_src, ereport, errcode(), errmsg(), ERROR, CopyFromStateData::fe_msgbuf, format, FrontendProtocol, i, list_length(), makeStringInfo(), CopyFromStateData::opts, PG_PROTOCOL_MAJOR, pq_beginmessage(), pq_endmessage(), pq_flush, pq_putemptymessage(), pq_sendbyte(), pq_sendint16(), and pq_startmsgread().

Referenced by BeginCopyFrom().

126 {
128  {
129  /* new way */
131  int natts = list_length(cstate->attnumlist);
132  int16 format = (cstate->opts.binary ? 1 : 0);
133  int i;
134 
135  pq_beginmessage(&buf, 'G');
136  pq_sendbyte(&buf, format); /* overall format */
137  pq_sendint16(&buf, natts);
138  for (i = 0; i < natts; i++)
139  pq_sendint16(&buf, format); /* per-column formats */
140  pq_endmessage(&buf);
141  cstate->copy_src = COPY_NEW_FE;
142  cstate->fe_msgbuf = makeStringInfo();
143  }
144  else
145  {
146  /* old way */
147  if (cstate->opts.binary)
148  ereport(ERROR,
149  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
150  errmsg("COPY BINARY is not supported to stdout or from stdin")));
151  pq_putemptymessage('G');
152  /* any error in old protocol will make us lose sync */
153  pq_startmsgread();
154  cstate->copy_src = COPY_OLD_FE;
155  }
156  /* We *must* flush here to ensure FE knows it can send. */
157  pq_flush();
158 }
signed short int16
Definition: c.h:428
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
#define pq_flush()
Definition: libpq.h:39
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
int errcode(int sqlerrcode)
Definition: elog.c:704
void pq_putemptymessage(char msgtype)
Definition: pqformat.c:390
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:113
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
#define ERROR
Definition: elog.h:45
void pq_startmsgread(void)
Definition: pqcomm.c:1221
bool binary
Definition: copy.h:32
static char * buf
Definition: pg_test_fsync.c:68
#define ereport(elevel,...)
Definition: elog.h:155
static int list_length(const List *l)
Definition: pg_list.h:149
CopyFormatOptions opts
int errmsg(const char *fmt,...)
Definition: elog.c:915
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
int i
static char format
ProtocolVersion FrontendProtocol
Definition: globals.c:28

◆ ReceiveCopyBinaryHeader()

void ReceiveCopyBinaryHeader ( CopyFromState  cstate)

Definition at line 161 of file copyfromparse.c.

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

Referenced by BeginCopyFrom().

162 {
163  char readSig[11];
164  int32 tmp;
165 
166  /* Signature */
167  if (CopyReadBinaryData(cstate, readSig, 11) != 11 ||
168  memcmp(readSig, BinarySignature, 11) != 0)
169  ereport(ERROR,
170  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
171  errmsg("COPY file signature not recognized")));
172  /* Flags field */
173  if (!CopyGetInt32(cstate, &tmp))
174  ereport(ERROR,
175  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
176  errmsg("invalid COPY file header (missing flags)")));
177  if ((tmp & (1 << 16)) != 0)
178  ereport(ERROR,
179  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
180  errmsg("invalid COPY file header (WITH OIDS)")));
181  tmp &= ~(1 << 16);
182  if ((tmp >> 16) != 0)
183  ereport(ERROR,
184  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
185  errmsg("unrecognized critical flags in COPY file header")));
186  /* Header extension length */
187  if (!CopyGetInt32(cstate, &tmp) ||
188  tmp < 0)
189  ereport(ERROR,
190  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
191  errmsg("invalid COPY file header (missing length)")));
192  /* Skip extension header, if present */
193  while (tmp-- > 0)
194  {
195  if (CopyReadBinaryData(cstate, readSig, 1) != 1)
196  ereport(ERROR,
197  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
198  errmsg("invalid COPY file header (wrong length)")));
199  }
200 }
static const char BinarySignature[11]
int errcode(int sqlerrcode)
Definition: elog.c:704
static bool CopyGetInt32(CopyFromState cstate, int32 *val)
signed int int32
Definition: c.h:429
static int CopyReadBinaryData(CopyFromState cstate, char *dest, int nbytes)
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915