PostgreSQL Source Code  git master
copy.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/dependency.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_type.h"
#include "commands/copy.h"
#include "commands/defrem.h"
#include "commands/trigger.h"
#include "executor/execPartition.h"
#include "executor/executor.h"
#include "executor/nodeModifyTable.h"
#include "executor/tuptable.h"
#include "foreign/fdwapi.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "optimizer/optimizer.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_expr.h"
#include "parser/parse_relation.h"
#include "port/pg_bswap.h"
#include "rewrite/rewriteHandler.h"
#include "storage/fd.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/partcache.h"
#include "utils/portal.h"
#include "utils/rel.h"
#include "utils/rls.h"
#include "utils/snapmgr.h"
Include dependency graph for copy.c:

Go to the source code of this file.

Data Structures

struct  CopyStateData
 
struct  DR_copy
 
struct  CopyMultiInsertBuffer
 
struct  CopyMultiInsertInfo
 

Macros

#define ISOCTAL(c)   (((c) >= '0') && ((c) <= '7'))
 
#define OCTVALUE(c)   ((c) - '0')
 
#define RAW_BUF_SIZE   65536 /* we palloc RAW_BUF_SIZE+1 bytes */
 
#define MAX_BUFFERED_TUPLES   1000
 
#define MAX_BUFFERED_BYTES   65535
 
#define MAX_PARTITION_BUFFERS   32
 
#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen)
 
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
 
#define REFILL_LINEBUF
 
#define NO_END_OF_COPY_GOTO
 
#define MAX_COPY_DATA_DISPLAY   100
 
#define DUMPSOFAR()
 

Typedefs

typedef enum CopyDest CopyDest
 
typedef enum EolType EolType
 
typedef enum CopyInsertMethod CopyInsertMethod
 
typedef struct CopyStateData CopyStateData
 
typedef struct CopyMultiInsertBuffer CopyMultiInsertBuffer
 
typedef struct CopyMultiInsertInfo CopyMultiInsertInfo
 

Enumerations

enum  CopyDest { 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

static CopyState BeginCopy (ParseState *pstate, bool is_from, Relation rel, RawStmt *raw_query, Oid queryRelId, List *attnamelist, List *options)
 
static void EndCopy (CopyState cstate)
 
static void ClosePipeToProgram (CopyState cstate)
 
static CopyState BeginCopyTo (ParseState *pstate, Relation rel, RawStmt *query, Oid queryRelId, const char *filename, bool is_program, List *attnamelist, List *options)
 
static void EndCopyTo (CopyState cstate)
 
static uint64 DoCopyTo (CopyState cstate)
 
static uint64 CopyTo (CopyState cstate)
 
static void CopyOneRowTo (CopyState cstate, TupleTableSlot *slot)
 
static bool CopyReadLine (CopyState cstate)
 
static bool CopyReadLineText (CopyState cstate)
 
static int CopyReadAttributesText (CopyState cstate)
 
static int CopyReadAttributesCSV (CopyState cstate)
 
static Datum CopyReadBinaryAttribute (CopyState cstate, int column_no, FmgrInfo *flinfo, Oid typioparam, int32 typmod, bool *isnull)
 
static void CopyAttributeOutText (CopyState cstate, char *string)
 
static void CopyAttributeOutCSV (CopyState cstate, char *string, bool use_quote, bool single_attr)
 
static ListCopyGetAttnums (TupleDesc tupDesc, Relation rel, List *attnamelist)
 
static char * limit_printout_length (const char *str)
 
static void SendCopyBegin (CopyState cstate)
 
static void ReceiveCopyBegin (CopyState cstate)
 
static void SendCopyEnd (CopyState cstate)
 
static void CopySendData (CopyState cstate, const void *databuf, int datasize)
 
static void CopySendString (CopyState cstate, const char *str)
 
static void CopySendChar (CopyState cstate, char c)
 
static void CopySendEndOfRow (CopyState cstate)
 
static int CopyGetData (CopyState cstate, void *databuf, int minread, int maxread)
 
static void CopySendInt32 (CopyState cstate, int32 val)
 
static bool CopyGetInt32 (CopyState cstate, int32 *val)
 
static void CopySendInt16 (CopyState cstate, int16 val)
 
static bool CopyGetInt16 (CopyState cstate, int16 *val)
 
static bool CopyLoadRawBuf (CopyState cstate)
 
void DoCopy (ParseState *pstate, const CopyStmt *stmt, int stmt_location, int stmt_len, uint64 *processed)
 
void ProcessCopyOptions (ParseState *pstate, CopyState cstate, bool is_from, List *options)
 
void CopyFromErrorCallback (void *arg)
 
static CopyMultiInsertBufferCopyMultiInsertBufferInit (ResultRelInfo *rri)
 
static void CopyMultiInsertInfoSetupBuffer (CopyMultiInsertInfo *miinfo, ResultRelInfo *rri)
 
static void CopyMultiInsertInfoInit (CopyMultiInsertInfo *miinfo, ResultRelInfo *rri, CopyState cstate, EState *estate, CommandId mycid, int ti_options)
 
static bool CopyMultiInsertInfoIsFull (CopyMultiInsertInfo *miinfo)
 
static bool CopyMultiInsertInfoIsEmpty (CopyMultiInsertInfo *miinfo)
 
static void CopyMultiInsertBufferFlush (CopyMultiInsertInfo *miinfo, CopyMultiInsertBuffer *buffer)
 
static void CopyMultiInsertBufferCleanup (CopyMultiInsertInfo *miinfo, CopyMultiInsertBuffer *buffer)
 
static void CopyMultiInsertInfoFlush (CopyMultiInsertInfo *miinfo, ResultRelInfo *curr_rri)
 
static void CopyMultiInsertInfoCleanup (CopyMultiInsertInfo *miinfo)
 
static TupleTableSlotCopyMultiInsertInfoNextFreeSlot (CopyMultiInsertInfo *miinfo, ResultRelInfo *rri)
 
static void CopyMultiInsertInfoStore (CopyMultiInsertInfo *miinfo, ResultRelInfo *rri, TupleTableSlot *slot, int tuplen, uint64 lineno)
 
uint64 CopyFrom (CopyState cstate)
 
CopyState BeginCopyFrom (ParseState *pstate, Relation rel, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options)
 
bool NextCopyFromRawFields (CopyState cstate, char ***fields, int *nfields)
 
bool NextCopyFrom (CopyState cstate, ExprContext *econtext, Datum *values, bool *nulls)
 
void EndCopyFrom (CopyState cstate)
 
static int GetDecimalFromHex (char hex)
 
static void copy_dest_startup (DestReceiver *self, int operation, TupleDesc typeinfo)
 
static bool copy_dest_receive (TupleTableSlot *slot, DestReceiver *self)
 
static void copy_dest_shutdown (DestReceiver *self)
 
static void copy_dest_destroy (DestReceiver *self)
 
DestReceiverCreateCopyDestReceiver (void)
 

Variables

static const char BinarySignature [11] = "PGCOPY\n\377\r\n\0"
 

Macro Definition Documentation

◆ DUMPSOFAR

#define DUMPSOFAR ( )
Value:
do { \
if (ptr > start) \
CopySendData(cstate, start, ptr - start); \
} while (0)

Definition at line 4820 of file copy.c.

Referenced by CopyAttributeOutCSV(), and CopyAttributeOutText().

◆ IF_NEED_REFILL_AND_EOF_BREAK

#define IF_NEED_REFILL_AND_EOF_BREAK (   extralen)
Value:
if (1) \
{ \
if (raw_buf_ptr + (extralen) >= copy_buf_len && hit_eof) \
{ \
if (extralen) \
raw_buf_ptr = copy_buf_len; /* consume the partial character */ \
/* backslash just before EOF, treat as data char */ \
result = true; \
break; \
} \
} else ((void) 0)

Definition at line 312 of file copy.c.

Referenced by CopyReadLineText().

◆ IF_NEED_REFILL_AND_NOT_EOF_CONTINUE

#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE (   extralen)
Value:
if (1) \
{ \
if (raw_buf_ptr + (extralen) >= copy_buf_len && !hit_eof) \
{ \
raw_buf_ptr = prev_raw_ptr; /* undo fetch */ \
need_data = true; \
continue; \
} \
} else ((void) 0)

Definition at line 300 of file copy.c.

Referenced by CopyReadLineText().

◆ ISOCTAL

#define ISOCTAL (   c)    (((c) >= '0') && ((c) <= '7'))

Definition at line 61 of file copy.c.

Referenced by CopyReadAttributesText().

◆ MAX_BUFFERED_BYTES

#define MAX_BUFFERED_BYTES   65535

Definition at line 251 of file copy.c.

Referenced by CopyMultiInsertInfoIsFull().

◆ MAX_BUFFERED_TUPLES

#define MAX_BUFFERED_TUPLES   1000

◆ MAX_COPY_DATA_DISPLAY

#define MAX_COPY_DATA_DISPLAY   100

Referenced by limit_printout_length().

◆ MAX_PARTITION_BUFFERS

#define MAX_PARTITION_BUFFERS   32

Definition at line 254 of file copy.c.

Referenced by CopyMultiInsertInfoFlush().

◆ NO_END_OF_COPY_GOTO

#define NO_END_OF_COPY_GOTO
Value:
if (1) \
{ \
raw_buf_ptr = prev_raw_ptr + 1; \
goto not_end_of_copy; \
} else ((void) 0)

Definition at line 342 of file copy.c.

Referenced by CopyReadLineText().

◆ OCTVALUE

#define OCTVALUE (   c)    ((c) - '0')

Definition at line 62 of file copy.c.

Referenced by CopyReadAttributesText().

◆ RAW_BUF_SIZE

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

Definition at line 221 of file copy.c.

Referenced by BeginCopyFrom(), and CopyLoadRawBuf().

◆ REFILL_LINEBUF

#define REFILL_LINEBUF
Value:
if (1) \
{ \
if (raw_buf_ptr > cstate->raw_buf_index) \
{ \
appendBinaryStringInfo(&cstate->line_buf, \
cstate->raw_buf + cstate->raw_buf_index, \
raw_buf_ptr - cstate->raw_buf_index); \
cstate->raw_buf_index = raw_buf_ptr; \
} \
} else ((void) 0)

Definition at line 329 of file copy.c.

Referenced by CopyReadLineText().

Typedef Documentation

◆ CopyDest

typedef enum CopyDest CopyDest

◆ CopyInsertMethod

◆ CopyMultiInsertBuffer

◆ CopyMultiInsertInfo

◆ CopyStateData

typedef struct CopyStateData CopyStateData

◆ EolType

typedef enum EolType EolType

Enumeration Type Documentation

◆ CopyDest

enum CopyDest
Enumerator
COPY_FILE 
COPY_OLD_FE 
COPY_NEW_FE 
COPY_CALLBACK 

Definition at line 68 of file copy.c.

69 {
70  COPY_FILE, /* to/from file (or a piped program) */
71  COPY_OLD_FE, /* to/from frontend (2.0 protocol) */
72  COPY_NEW_FE, /* to/from frontend (3.0 protocol) */
73  COPY_CALLBACK /* to/from callback function */
74 } CopyDest;
Definition: copy.c:70
CopyDest
Definition: copy.c:68

◆ CopyInsertMethod

Enumerator
CIM_SINGLE 
CIM_MULTI 
CIM_MULTI_CONDITIONAL 

Definition at line 90 of file copy.c.

91 {
92  CIM_SINGLE, /* use table_tuple_insert or fdw routine */
93  CIM_MULTI, /* always use table_multi_insert */
94  CIM_MULTI_CONDITIONAL /* use table_multi_insert only if valid */
Definition: copy.c:93
CopyInsertMethod
Definition: copy.c:90

◆ EolType

enum EolType
Enumerator
EOL_UNKNOWN 
EOL_NL 
EOL_CR 
EOL_CRNL 

Definition at line 79 of file copy.c.

80 {
82  EOL_NL,
83  EOL_CR,
84  EOL_CRNL
85 } EolType;
Definition: copy.c:82
EolType
Definition: copy.c:79
Definition: copy.c:83
Definition: copy.c:84

Function Documentation

◆ BeginCopy()

static CopyState BeginCopy ( ParseState pstate,
bool  is_from,
Relation  rel,
RawStmt raw_query,
Oid  queryRelId,
List attnamelist,
List options 
)
static

Definition at line 1461 of file copy.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, attnum, CopyStateData::attnumlist, CMD_DELETE, CMD_INSERT, CMD_SELECT, CMD_UPDATE, Query::commandType, CopyStateData::convert_select, CopyStateData::convert_select_flags, CopyStateData::convert_selectively, CopyStateData::copy_dest, COPY_FILE, CopyStateData::copycontext, CopyGetAttnums(), copyObject, CreateDestReceiver(), CreateQueryDesc(), cur, CurrentMemoryContext, CURSOR_OPT_PARALLEL_OK, generate_unaccent_rules::dest, DestCopyOut, CopyStateData::encoding_embeds_ascii, ereport, errcode(), errmsg(), errmsg_internal(), ERROR, ExecutorStart(), CopyStateData::file_encoding, CopyStateData::force_notnull, CopyStateData::force_notnull_flags, CopyStateData::force_null, CopyStateData::force_null_flags, CopyStateData::force_quote, CopyStateData::force_quote_all, CopyStateData::force_quote_flags, GetActiveSnapshot(), GetDatabaseEncoding(), i, InvalidOid, InvalidSnapshot, IsA, lfirst_int, lfirst_node, linitial_node, list_length(), list_member_int(), list_member_oid(), MemoryContextSwitchTo(), NameStr, TupleDescData::natts, CopyStateData::need_transcoding, NIL, ParseState::p_sourcetext, palloc0(), pg_analyze_and_rewrite(), pg_database_encoding_max_length(), PG_ENCODING_IS_CLIENT_ONLY, pg_get_client_encoding(), pg_plan_query(), ProcessCopyOptions(), PushCopiedSnapshot(), QSRC_NON_INSTEAD_RULE, QSRC_QUAL_INSTEAD_RULE, CopyStateData::queryDesc, Query::querySource, CopyStateData::rel, RelationGetDescr, PlannedStmt::relationOids, Query::returningList, QueryDesc::tupDesc, TupleDescAttr, UpdateActiveSnapshotCommandId(), and Query::utilityStmt.

Referenced by BeginCopyFrom(), and BeginCopyTo().

1468 {
1469  CopyState cstate;
1470  TupleDesc tupDesc;
1471  int num_phys_attrs;
1472  MemoryContext oldcontext;
1473 
1474  /* Allocate workspace and zero all fields */
1475  cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
1476 
1477  /*
1478  * We allocate everything used by a cstate in a new memory context. This
1479  * avoids memory leaks during repeated use of COPY in a query.
1480  */
1482  "COPY",
1484 
1485  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1486 
1487  /* Extract options from the statement node tree */
1488  ProcessCopyOptions(pstate, cstate, is_from, options);
1489 
1490  /* Process the source/target relation or query */
1491  if (rel)
1492  {
1493  Assert(!raw_query);
1494 
1495  cstate->rel = rel;
1496 
1497  tupDesc = RelationGetDescr(cstate->rel);
1498  }
1499  else
1500  {
1501  List *rewritten;
1502  Query *query;
1503  PlannedStmt *plan;
1504  DestReceiver *dest;
1505 
1506  Assert(!is_from);
1507  cstate->rel = NULL;
1508 
1509  /*
1510  * Run parse analysis and rewrite. Note this also acquires sufficient
1511  * locks on the source table(s).
1512  *
1513  * Because the parser and planner tend to scribble on their input, we
1514  * make a preliminary copy of the source querytree. This prevents
1515  * problems in the case that the COPY is in a portal or plpgsql
1516  * function and is executed repeatedly. (See also the same hack in
1517  * DECLARE CURSOR and PREPARE.) XXX FIXME someday.
1518  */
1519  rewritten = pg_analyze_and_rewrite(copyObject(raw_query),
1520  pstate->p_sourcetext, NULL, 0,
1521  NULL);
1522 
1523  /* check that we got back something we can work with */
1524  if (rewritten == NIL)
1525  {
1526  ereport(ERROR,
1527  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1528  errmsg("DO INSTEAD NOTHING rules are not supported for COPY")));
1529  }
1530  else if (list_length(rewritten) > 1)
1531  {
1532  ListCell *lc;
1533 
1534  /* examine queries to determine which error message to issue */
1535  foreach(lc, rewritten)
1536  {
1537  Query *q = lfirst_node(Query, lc);
1538 
1540  ereport(ERROR,
1541  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1542  errmsg("conditional DO INSTEAD rules are not supported for COPY")));
1544  ereport(ERROR,
1545  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1546  errmsg("DO ALSO rules are not supported for the COPY")));
1547  }
1548 
1549  ereport(ERROR,
1550  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1551  errmsg("multi-statement DO INSTEAD rules are not supported for COPY")));
1552  }
1553 
1554  query = linitial_node(Query, rewritten);
1555 
1556  /* The grammar allows SELECT INTO, but we don't support that */
1557  if (query->utilityStmt != NULL &&
1559  ereport(ERROR,
1560  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1561  errmsg("COPY (SELECT INTO) is not supported")));
1562 
1563  Assert(query->utilityStmt == NULL);
1564 
1565  /*
1566  * Similarly the grammar doesn't enforce the presence of a RETURNING
1567  * clause, but this is required here.
1568  */
1569  if (query->commandType != CMD_SELECT &&
1570  query->returningList == NIL)
1571  {
1572  Assert(query->commandType == CMD_INSERT ||
1573  query->commandType == CMD_UPDATE ||
1574  query->commandType == CMD_DELETE);
1575 
1576  ereport(ERROR,
1577  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1578  errmsg("COPY query must have a RETURNING clause")));
1579  }
1580 
1581  /* plan the query */
1582  plan = pg_plan_query(query, CURSOR_OPT_PARALLEL_OK, NULL);
1583 
1584  /*
1585  * With row level security and a user using "COPY relation TO", we
1586  * have to convert the "COPY relation TO" to a query-based COPY (eg:
1587  * "COPY (SELECT * FROM relation) TO"), to allow the rewriter to add
1588  * in any RLS clauses.
1589  *
1590  * When this happens, we are passed in the relid of the originally
1591  * found relation (which we have locked). As the planner will look up
1592  * the relation again, we double-check here to make sure it found the
1593  * same one that we have locked.
1594  */
1595  if (queryRelId != InvalidOid)
1596  {
1597  /*
1598  * Note that with RLS involved there may be multiple relations,
1599  * and while the one we need is almost certainly first, we don't
1600  * make any guarantees of that in the planner, so check the whole
1601  * list and make sure we find the original relation.
1602  */
1603  if (!list_member_oid(plan->relationOids, queryRelId))
1604  ereport(ERROR,
1605  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1606  errmsg("relation referenced by COPY statement has changed")));
1607  }
1608 
1609  /*
1610  * Use a snapshot with an updated command ID to ensure this query sees
1611  * results of any previously executed queries.
1612  */
1615 
1616  /* Create dest receiver for COPY OUT */
1618  ((DR_copy *) dest)->cstate = cstate;
1619 
1620  /* Create a QueryDesc requesting no output */
1621  cstate->queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
1624  dest, NULL, NULL, 0);
1625 
1626  /*
1627  * Call ExecutorStart to prepare the plan for execution.
1628  *
1629  * ExecutorStart computes a result tupdesc for us
1630  */
1631  ExecutorStart(cstate->queryDesc, 0);
1632 
1633  tupDesc = cstate->queryDesc->tupDesc;
1634  }
1635 
1636  /* Generate or convert list of attributes to process */
1637  cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist);
1638 
1639  num_phys_attrs = tupDesc->natts;
1640 
1641  /* Convert FORCE_QUOTE name list to per-column flags, check validity */
1642  cstate->force_quote_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1643  if (cstate->force_quote_all)
1644  {
1645  int i;
1646 
1647  for (i = 0; i < num_phys_attrs; i++)
1648  cstate->force_quote_flags[i] = true;
1649  }
1650  else if (cstate->force_quote)
1651  {
1652  List *attnums;
1653  ListCell *cur;
1654 
1655  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_quote);
1656 
1657  foreach(cur, attnums)
1658  {
1659  int attnum = lfirst_int(cur);
1660  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1661 
1662  if (!list_member_int(cstate->attnumlist, attnum))
1663  ereport(ERROR,
1664  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1665  errmsg("FORCE_QUOTE column \"%s\" not referenced by COPY",
1666  NameStr(attr->attname))));
1667  cstate->force_quote_flags[attnum - 1] = true;
1668  }
1669  }
1670 
1671  /* Convert FORCE_NOT_NULL name list to per-column flags, check validity */
1672  cstate->force_notnull_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1673  if (cstate->force_notnull)
1674  {
1675  List *attnums;
1676  ListCell *cur;
1677 
1678  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_notnull);
1679 
1680  foreach(cur, attnums)
1681  {
1682  int attnum = lfirst_int(cur);
1683  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1684 
1685  if (!list_member_int(cstate->attnumlist, attnum))
1686  ereport(ERROR,
1687  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1688  errmsg("FORCE_NOT_NULL column \"%s\" not referenced by COPY",
1689  NameStr(attr->attname))));
1690  cstate->force_notnull_flags[attnum - 1] = true;
1691  }
1692  }
1693 
1694  /* Convert FORCE_NULL name list to per-column flags, check validity */
1695  cstate->force_null_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1696  if (cstate->force_null)
1697  {
1698  List *attnums;
1699  ListCell *cur;
1700 
1701  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_null);
1702 
1703  foreach(cur, attnums)
1704  {
1705  int attnum = lfirst_int(cur);
1706  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1707 
1708  if (!list_member_int(cstate->attnumlist, attnum))
1709  ereport(ERROR,
1710  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1711  errmsg("FORCE_NULL column \"%s\" not referenced by COPY",
1712  NameStr(attr->attname))));
1713  cstate->force_null_flags[attnum - 1] = true;
1714  }
1715  }
1716 
1717  /* Convert convert_selectively name list to per-column flags */
1718  if (cstate->convert_selectively)
1719  {
1720  List *attnums;
1721  ListCell *cur;
1722 
1723  cstate->convert_select_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1724 
1725  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->convert_select);
1726 
1727  foreach(cur, attnums)
1728  {
1729  int attnum = lfirst_int(cur);
1730  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1731 
1732  if (!list_member_int(cstate->attnumlist, attnum))
1733  ereport(ERROR,
1734  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1735  errmsg_internal("selected column \"%s\" not referenced by COPY",
1736  NameStr(attr->attname))));
1737  cstate->convert_select_flags[attnum - 1] = true;
1738  }
1739  }
1740 
1741  /* Use client encoding when ENCODING option is not specified. */
1742  if (cstate->file_encoding < 0)
1744 
1745  /*
1746  * Set up encoding conversion info. Even if the file and server encodings
1747  * are the same, we must apply pg_any_to_server() to validate data in
1748  * multibyte encodings.
1749  */
1750  cstate->need_transcoding =
1751  (cstate->file_encoding != GetDatabaseEncoding() ||
1753  /* See Multibyte encoding comment above */
1755 
1756  cstate->copy_dest = COPY_FILE; /* default */
1757 
1758  MemoryContextSwitchTo(oldcontext);
1759 
1760  return cstate;
1761 }
#define NIL
Definition: pg_list.h:65
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:783
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define AllocSetContextCreate
Definition: memutils.h:170
#define RelationGetDescr(relation)
Definition: rel.h:454
bool need_transcoding
Definition: copy.c:125
List * attnumlist
Definition: copy.c:131
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * relationOids
Definition: plannodes.h:86
Definition: copy.c:228
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:143
void ProcessCopyOptions(ParseState *pstate, CopyState cstate, bool is_from, List *options)
Definition: copy.c:1110
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:841
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:608
bool * force_quote_flags
Definition: copy.c:147
List * pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string, Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition: postgres.c:676
Node * utilityStmt
Definition: parsenodes.h:120
#define linitial_node(type, l)
Definition: pg_list.h:198
bool * force_null_flags
Definition: copy.c:151
bool * convert_select_flags
Definition: copy.c:154
CopyDest copy_dest
Definition: copy.c:116
Definition: copy.c:70
Relation rel
Definition: copy.c:129
MemoryContext copycontext
Definition: copy.c:166
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:191
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:771
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:67
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define lfirst_node(type, lc)
Definition: pg_list.h:193
QueryDesc * queryDesc
Definition: copy.c:130
bool list_member_int(const List *list, int datum)
Definition: list.c:655
bool encoding_embeds_ascii
Definition: copy.c:126
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:113
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
const char * p_sourcetext
Definition: parse_node.h:179
List * returningList
Definition: parsenodes.h:146
#define ereport(elevel, rest)
Definition: elog.h:141
List * force_null
Definition: copy.c:150
int file_encoding
Definition: copy.c:124
static List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
Definition: copy.c:5076
TupleDesc tupDesc
Definition: execdesc.h:47
#define InvalidSnapshot
Definition: snapshot.h:123
void * palloc0(Size size)
Definition: mcxt.c:980
int GetDatabaseEncoding(void)
Definition: mbutils.c:1046
int pg_get_client_encoding(void)
Definition: mbutils.c:298
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:79
bool * force_notnull_flags
Definition: copy.c:149
CmdType commandType
Definition: parsenodes.h:112
List * force_notnull
Definition: copy.c:148
int errmsg_internal(const char *fmt,...)
Definition: elog.c:909
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675
QuerySource querySource
Definition: parsenodes.h:114
#define Assert(condition)
Definition: c.h:739
List * convert_select
Definition: copy.c:153
static int list_length(const List *l)
Definition: pg_list.h:169
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1331
bool force_quote_all
Definition: copy.c:146
#define PG_ENCODING_IS_CLIENT_ONLY(_enc)
Definition: pg_wchar.h:298
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2712
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
#define NameStr(name)
Definition: c.h:616
#define copyObject(obj)
Definition: nodes.h:641
Definition: pg_list.h:50
List * force_quote
Definition: copy.c:145
bool convert_selectively
Definition: copy.c:152
PlannedStmt * pg_plan_query(Query *querytree, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:857

◆ BeginCopyFrom()

CopyState BeginCopyFrom ( ParseState pstate,
Relation  rel,
const char *  filename,
bool  is_program,
copy_data_source_cb  data_source_cb,
List attnamelist,
List options 
)

Definition at line 3385 of file copy.c.

References AllocateFile(), Assert, attnum, CopyStateData::attnumlist, CopyStateData::attribute_buf, BeginCopy(), CopyStateData::binary, BinarySignature, build_column_default(), contain_volatile_functions_not_nextval(), COPY_CALLBACK, CopyStateData::copy_dest, CopyStateData::copy_file, CopyStateData::copycontext, CopyGetData(), CopyGetInt32(), CopyStateData::cur_attname, CopyStateData::cur_attval, CopyStateData::cur_lineno, CopyStateData::cur_relname, CopyStateData::data_source_cb, CopyStateData::defexprs, CopyStateData::defmap, DestRemote, CopyStateData::eol_type, EOL_UNKNOWN, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, ExecInitExpr(), expression_planner(), CopyStateData::filename, fmgr_info(), getTypeBinaryInputInfo(), getTypeInputInfo(), CopyStateData::in_functions, initStringInfo(), InvalidOid, CopyStateData::is_program, CopyStateData::line_buf, CopyStateData::line_buf_converted, list_length(), list_member_int(), CopyStateData::max_fields, MemoryContextSwitchTo(), TupleDescData::natts, CopyStateData::num_defaults, OpenPipeStream(), ParseState::p_rtable, palloc(), PG_BINARY_R, pstrdup(), CopyStateData::range_table, CopyStateData::raw_buf, CopyStateData::raw_buf_index, CopyStateData::raw_buf_len, RAW_BUF_SIZE, CopyStateData::raw_fields, CopyStateData::reached_eof, ReceiveCopyBegin(), CopyStateData::rel, RelationGetDescr, RelationGetRelationName, S_ISDIR, stat, TupleDescAttr, CopyStateData::typioparams, CopyStateData::volatile_defexprs, and whereToSendOutput.

Referenced by copy_table(), DoCopy(), file_acquire_sample_rows(), fileBeginForeignScan(), and fileReScanForeignScan().

3392 {
3393  CopyState cstate;
3394  bool pipe = (filename == NULL);
3395  TupleDesc tupDesc;
3396  AttrNumber num_phys_attrs,
3397  num_defaults;
3398  FmgrInfo *in_functions;
3399  Oid *typioparams;
3400  int attnum;
3401  Oid in_func_oid;
3402  int *defmap;
3403  ExprState **defexprs;
3404  MemoryContext oldcontext;
3405  bool volatile_defexprs;
3406 
3407  cstate = BeginCopy(pstate, true, rel, NULL, InvalidOid, attnamelist, options);
3408  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
3409 
3410  /* Initialize state variables */
3411  cstate->reached_eof = false;
3412  cstate->eol_type = EOL_UNKNOWN;
3413  cstate->cur_relname = RelationGetRelationName(cstate->rel);
3414  cstate->cur_lineno = 0;
3415  cstate->cur_attname = NULL;
3416  cstate->cur_attval = NULL;
3417 
3418  /* Set up variables to avoid per-attribute overhead. */
3419  initStringInfo(&cstate->attribute_buf);
3420  initStringInfo(&cstate->line_buf);
3421  cstate->line_buf_converted = false;
3422  cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
3423  cstate->raw_buf_index = cstate->raw_buf_len = 0;
3424 
3425  /* Assign range table, we'll need it in CopyFrom. */
3426  if (pstate)
3427  cstate->range_table = pstate->p_rtable;
3428 
3429  tupDesc = RelationGetDescr(cstate->rel);
3430  num_phys_attrs = tupDesc->natts;
3431  num_defaults = 0;
3432  volatile_defexprs = false;
3433 
3434  /*
3435  * Pick up the required catalog information for each attribute in the
3436  * relation, including the input function, the element type (to pass to
3437  * the input function), and info about defaults and constraints. (Which
3438  * input function we use depends on text/binary format choice.)
3439  */
3440  in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
3441  typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
3442  defmap = (int *) palloc(num_phys_attrs * sizeof(int));
3443  defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
3444 
3445  for (attnum = 1; attnum <= num_phys_attrs; attnum++)
3446  {
3447  Form_pg_attribute att = TupleDescAttr(tupDesc, attnum - 1);
3448 
3449  /* We don't need info for dropped attributes */
3450  if (att->attisdropped)
3451  continue;
3452 
3453  /* Fetch the input function and typioparam info */
3454  if (cstate->binary)
3455  getTypeBinaryInputInfo(att->atttypid,
3456  &in_func_oid, &typioparams[attnum - 1]);
3457  else
3458  getTypeInputInfo(att->atttypid,
3459  &in_func_oid, &typioparams[attnum - 1]);
3460  fmgr_info(in_func_oid, &in_functions[attnum - 1]);
3461 
3462  /* Get default info if needed */
3463  if (!list_member_int(cstate->attnumlist, attnum) && !att->attgenerated)
3464  {
3465  /* attribute is NOT to be copied from input */
3466  /* use default value if one exists */
3467  Expr *defexpr = (Expr *) build_column_default(cstate->rel,
3468  attnum);
3469 
3470  if (defexpr != NULL)
3471  {
3472  /* Run the expression through planner */
3473  defexpr = expression_planner(defexpr);
3474 
3475  /* Initialize executable expression in copycontext */
3476  defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
3477  defmap[num_defaults] = attnum - 1;
3478  num_defaults++;
3479 
3480  /*
3481  * If a default expression looks at the table being loaded,
3482  * then it could give the wrong answer when using
3483  * multi-insert. Since database access can be dynamic this is
3484  * hard to test for exactly, so we use the much wider test of
3485  * whether the default expression is volatile. We allow for
3486  * the special case of when the default expression is the
3487  * nextval() of a sequence which in this specific case is
3488  * known to be safe for use with the multi-insert
3489  * optimization. Hence we use this special case function
3490  * checker rather than the standard check for
3491  * contain_volatile_functions().
3492  */
3493  if (!volatile_defexprs)
3494  volatile_defexprs = contain_volatile_functions_not_nextval((Node *) defexpr);
3495  }
3496  }
3497  }
3498 
3499  /* We keep those variables in cstate. */
3500  cstate->in_functions = in_functions;
3501  cstate->typioparams = typioparams;
3502  cstate->defmap = defmap;
3503  cstate->defexprs = defexprs;
3504  cstate->volatile_defexprs = volatile_defexprs;
3505  cstate->num_defaults = num_defaults;
3506  cstate->is_program = is_program;
3507 
3508  if (data_source_cb)
3509  {
3510  cstate->copy_dest = COPY_CALLBACK;
3511  cstate->data_source_cb = data_source_cb;
3512  }
3513  else if (pipe)
3514  {
3515  Assert(!is_program); /* the grammar does not allow this */
3517  ReceiveCopyBegin(cstate);
3518  else
3519  cstate->copy_file = stdin;
3520  }
3521  else
3522  {
3523  cstate->filename = pstrdup(filename);
3524 
3525  if (cstate->is_program)
3526  {
3527  cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_R);
3528  if (cstate->copy_file == NULL)
3529  ereport(ERROR,
3531  errmsg("could not execute command \"%s\": %m",
3532  cstate->filename)));
3533  }
3534  else
3535  {
3536  struct stat st;
3537 
3538  cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R);
3539  if (cstate->copy_file == NULL)
3540  {
3541  /* copy errno because ereport subfunctions might change it */
3542  int save_errno = errno;
3543 
3544  ereport(ERROR,
3546  errmsg("could not open file \"%s\" for reading: %m",
3547  cstate->filename),
3548  (save_errno == ENOENT || save_errno == EACCES) ?
3549  errhint("COPY FROM instructs the PostgreSQL server process to read a file. "
3550  "You may want a client-side facility such as psql's \\copy.") : 0));
3551  }
3552 
3553  if (fstat(fileno(cstate->copy_file), &st))
3554  ereport(ERROR,
3556  errmsg("could not stat file \"%s\": %m",
3557  cstate->filename)));
3558 
3559  if (S_ISDIR(st.st_mode))
3560  ereport(ERROR,
3561  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3562  errmsg("\"%s\" is a directory", cstate->filename)));
3563  }
3564  }
3565 
3566  if (cstate->binary)
3567  {
3568  /* Read and verify binary header */
3569  char readSig[11];
3570  int32 tmp;
3571 
3572  /* Signature */
3573  if (CopyGetData(cstate, readSig, 11, 11) != 11 ||
3574  memcmp(readSig, BinarySignature, 11) != 0)
3575  ereport(ERROR,
3576  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3577  errmsg("COPY file signature not recognized")));
3578  /* Flags field */
3579  if (!CopyGetInt32(cstate, &tmp))
3580  ereport(ERROR,
3581  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3582  errmsg("invalid COPY file header (missing flags)")));
3583  if ((tmp & (1 << 16)) != 0)
3584  ereport(ERROR,
3585  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3586  errmsg("invalid COPY file header (WITH OIDS)")));
3587  tmp &= ~(1 << 16);
3588  if ((tmp >> 16) != 0)
3589  ereport(ERROR,
3590  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3591  errmsg("unrecognized critical flags in COPY file header")));
3592  /* Header extension length */
3593  if (!CopyGetInt32(cstate, &tmp) ||
3594  tmp < 0)
3595  ereport(ERROR,
3596  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3597  errmsg("invalid COPY file header (missing length)")));
3598  /* Skip extension header, if present */
3599  while (tmp-- > 0)
3600  {
3601  if (CopyGetData(cstate, readSig, 1, 1) != 1)
3602  ereport(ERROR,
3603  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3604  errmsg("invalid COPY file header (wrong length)")));
3605  }
3606  }
3607 
3608  /* create workspace for CopyReadAttributes results */
3609  if (!cstate->binary)
3610  {
3611  AttrNumber attr_count = list_length(cstate->attnumlist);
3612 
3613  cstate->max_fields = attr_count;
3614  cstate->raw_fields = (char **) palloc(attr_count * sizeof(char *));
3615  }
3616 
3617  MemoryContextSwitchTo(oldcontext);
3618 
3619  return cstate;
3620 }
Definition: fmgr.h:56
List * range_table
Definition: copy.c:183
static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel, RawStmt *raw_query, Oid queryRelId, List *attnamelist, List *options)
Definition: copy.c:1461
bool contain_volatile_functions_not_nextval(Node *clause)
Definition: clauses.c:773
static bool CopyGetInt32(CopyState cstate, int32 *val)
Definition: copy.c:738
int errhint(const char *fmt,...)
Definition: elog.c:1069
char ** raw_fields
Definition: copy.c:201
bool binary
Definition: copy.c:135
#define RelationGetDescr(relation)
Definition: rel.h:454
AttrNumber num_defaults
Definition: copy.c:177
FmgrInfo * in_functions
Definition: copy.c:178
List * attnumlist
Definition: copy.c:131
char * filename
Definition: copy.c:132
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1186
static void ReceiveCopyBegin(CopyState cstate)
Definition: copy.c:434
Expr * expression_planner(Expr *expr)
Definition: planner.c:6051
StringInfoData line_buf
Definition: copy.c:210
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:525
int raw_buf_index
Definition: copy.c:223
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
bool volatile_defexprs
Definition: copy.c:182
#define PG_BINARY_R
Definition: c.h:1223
bool line_buf_converted
Definition: copy.c:211
signed int int32
Definition: c.h:347
CopyDest copy_dest
Definition: copy.c:116
uint64 cur_lineno
Definition: copy.c:159
const char * cur_attname
Definition: copy.c:160
Relation rel
Definition: copy.c:129
MemoryContext copycontext
Definition: copy.c:166
copy_data_source_cb data_source_cb
Definition: copy.c:134
#define ERROR
Definition: elog.h:43
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:610
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
bool list_member_int(const List *list, int datum)
Definition: list.c:655
char * raw_buf
Definition: copy.c:222
ExprState ** defexprs
Definition: copy.c:181
const char * cur_relname
Definition: copy.c:158
int errcode_for_file_access(void)
Definition: elog.c:631
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2242
#define RelationGetRelationName(relation)
Definition: rel.h:462
static const char BinarySignature[11]
Definition: copy.c:349
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
int raw_buf_len
Definition: copy.c:224
int max_fields
Definition: copy.c:200
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2345
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2707
#define ereport(elevel, rest)
Definition: elog.h:141
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2641
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define stat(a, b)
Definition: win32_port.h:255
Oid * typioparams
Definition: copy.c:179
bool is_program
Definition: copy.c:133
Node * build_column_default(Relation rel, int attrno)
#define RAW_BUF_SIZE
Definition: copy.c:221
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:79
EolType eol_type
Definition: copy.c:123
#define Assert(condition)
Definition: c.h:739
bool reached_eof
Definition: copy.c:121
static int list_length(const List *l)
Definition: pg_list.h:169
#define S_ISDIR(m)
Definition: win32_port.h:296
static char * filename
Definition: pg_dumpall.c:90
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
FILE * copy_file
Definition: copy.c:117
StringInfoData attribute_buf
Definition: copy.c:196
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:121
const char * cur_attval
Definition: copy.c:161
CommandDest whereToSendOutput
Definition: postgres.c:91
int16 AttrNumber
Definition: attnum.h:21
int * defmap
Definition: copy.c:180
List * p_rtable
Definition: parse_node.h:180

◆ BeginCopyTo()

static CopyState BeginCopyTo ( ParseState pstate,
Relation  rel,
RawStmt query,
Oid  queryRelId,
const char *  filename,
bool  is_program,
List attnamelist,
List options 
)
static

Definition at line 1825 of file copy.c.

References AllocateFile(), Assert, BeginCopy(), CopyStateData::copy_file, CopyStateData::copycontext, DestRemote, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, CopyStateData::filename, is_absolute_path, CopyStateData::is_program, MemoryContextSwitchTo(), OpenPipeStream(), PG_BINARY_W, PG_END_TRY, PG_FINALLY, PG_TRY, pstrdup(), RelationData::rd_rel, RelationGetRelationName, S_ISDIR, S_IWGRP, S_IWOTH, stat, generate_unaccent_rules::stdout, and whereToSendOutput.

Referenced by DoCopy().

1833 {
1834  CopyState cstate;
1835  bool pipe = (filename == NULL);
1836  MemoryContext oldcontext;
1837 
1838  if (rel != NULL && rel->rd_rel->relkind != RELKIND_RELATION)
1839  {
1840  if (rel->rd_rel->relkind == RELKIND_VIEW)
1841  ereport(ERROR,
1842  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1843  errmsg("cannot copy from view \"%s\"",
1845  errhint("Try the COPY (SELECT ...) TO variant.")));
1846  else if (rel->rd_rel->relkind == RELKIND_MATVIEW)
1847  ereport(ERROR,
1848  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1849  errmsg("cannot copy from materialized view \"%s\"",
1851  errhint("Try the COPY (SELECT ...) TO variant.")));
1852  else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1853  ereport(ERROR,
1854  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1855  errmsg("cannot copy from foreign table \"%s\"",
1857  errhint("Try the COPY (SELECT ...) TO variant.")));
1858  else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
1859  ereport(ERROR,
1860  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1861  errmsg("cannot copy from sequence \"%s\"",
1862  RelationGetRelationName(rel))));
1863  else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1864  ereport(ERROR,
1865  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1866  errmsg("cannot copy from partitioned table \"%s\"",
1868  errhint("Try the COPY (SELECT ...) TO variant.")));
1869  else
1870  ereport(ERROR,
1871  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1872  errmsg("cannot copy from non-table relation \"%s\"",
1873  RelationGetRelationName(rel))));
1874  }
1875 
1876  cstate = BeginCopy(pstate, false, rel, query, queryRelId, attnamelist,
1877  options);
1878  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1879 
1880  if (pipe)
1881  {
1882  Assert(!is_program); /* the grammar does not allow this */
1884  cstate->copy_file = stdout;
1885  }
1886  else
1887  {
1888  cstate->filename = pstrdup(filename);
1889  cstate->is_program = is_program;
1890 
1891  if (is_program)
1892  {
1893  cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_W);
1894  if (cstate->copy_file == NULL)
1895  ereport(ERROR,
1897  errmsg("could not execute command \"%s\": %m",
1898  cstate->filename)));
1899  }
1900  else
1901  {
1902  mode_t oumask; /* Pre-existing umask value */
1903  struct stat st;
1904 
1905  /*
1906  * Prevent write to relative path ... too easy to shoot oneself in
1907  * the foot by overwriting a database file ...
1908  */
1909  if (!is_absolute_path(filename))
1910  ereport(ERROR,
1911  (errcode(ERRCODE_INVALID_NAME),
1912  errmsg("relative path not allowed for COPY to file")));
1913 
1914  oumask = umask(S_IWGRP | S_IWOTH);
1915  PG_TRY();
1916  {
1917  cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1918  }
1919  PG_FINALLY();
1920  {
1921  umask(oumask);
1922  }
1923  PG_END_TRY();
1924  if (cstate->copy_file == NULL)
1925  {
1926  /* copy errno because ereport subfunctions might change it */
1927  int save_errno = errno;
1928 
1929  ereport(ERROR,
1931  errmsg("could not open file \"%s\" for writing: %m",
1932  cstate->filename),
1933  (save_errno == ENOENT || save_errno == EACCES) ?
1934  errhint("COPY TO instructs the PostgreSQL server process to write a file. "
1935  "You may want a client-side facility such as psql's \\copy.") : 0));
1936  }
1937 
1938  if (fstat(fileno(cstate->copy_file), &st))
1939  ereport(ERROR,
1941  errmsg("could not stat file \"%s\": %m",
1942  cstate->filename)));
1943 
1944  if (S_ISDIR(st.st_mode))
1945  ereport(ERROR,
1946  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1947  errmsg("\"%s\" is a directory", cstate->filename)));
1948  }
1949  }
1950 
1951  MemoryContextSwitchTo(oldcontext);
1952 
1953  return cstate;
1954 }
static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel, RawStmt *raw_query, Oid queryRelId, List *attnamelist, List *options)
Definition: copy.c:1461
int errhint(const char *fmt,...)
Definition: elog.c:1069
char * filename
Definition: copy.c:132
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define S_IWOTH
Definition: win32_port.h:287
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_BINARY_W
Definition: c.h:1224
Form_pg_class rd_rel
Definition: rel.h:84
MemoryContext copycontext
Definition: copy.c:166
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
#define is_absolute_path(filename)
Definition: port.h:86
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2242
#define RelationGetRelationName(relation)
Definition: rel.h:462
#define S_IWGRP
Definition: win32_port.h:275
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2345
#define ereport(elevel, rest)
Definition: elog.h:141
#define stat(a, b)
Definition: win32_port.h:255
bool is_program
Definition: copy.c:133
#define PG_FINALLY()
Definition: elog.h:339
#define Assert(condition)
Definition: c.h:739
#define S_ISDIR(m)
Definition: win32_port.h:296
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:822
FILE * copy_file
Definition: copy.c:117
CommandDest whereToSendOutput
Definition: postgres.c:91
#define PG_TRY()
Definition: elog.h:322
#define PG_END_TRY()
Definition: elog.h:347

◆ ClosePipeToProgram()

static void ClosePipeToProgram ( CopyState  cstate)
static

Definition at line 1767 of file copy.c.

References Assert, ClosePipeStream(), CopyStateData::copy_file, ereport, errcode(), errcode_for_file_access(), errdetail_internal(), errmsg(), ERROR, CopyStateData::filename, CopyStateData::is_copy_from, CopyStateData::is_program, CopyStateData::reached_eof, SIGPIPE, wait_result_is_signal(), and wait_result_to_str().

Referenced by CopySendEndOfRow(), and EndCopy().

1768 {
1769  int pclose_rc;
1770 
1771  Assert(cstate->is_program);
1772 
1773  pclose_rc = ClosePipeStream(cstate->copy_file);
1774  if (pclose_rc == -1)
1775  ereport(ERROR,
1777  errmsg("could not close pipe to external command: %m")));
1778  else if (pclose_rc != 0)
1779  {
1780  /*
1781  * If we ended a COPY FROM PROGRAM before reaching EOF, then it's
1782  * expectable for the called program to fail with SIGPIPE, and we
1783  * should not report that as an error. Otherwise, SIGPIPE indicates a
1784  * problem.
1785  */
1786  if (cstate->is_copy_from && !cstate->reached_eof &&
1787  wait_result_is_signal(pclose_rc, SIGPIPE))
1788  return;
1789 
1790  ereport(ERROR,
1791  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1792  errmsg("program \"%s\" failed",
1793  cstate->filename),
1794  errdetail_internal("%s", wait_result_to_str(pclose_rc))));
1795  }
1796 }
char * filename
Definition: copy.c:132
int errcode(int sqlerrcode)
Definition: elog.c:608
#define SIGPIPE
Definition: win32_port.h:159
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:32
int ClosePipeStream(FILE *file)
Definition: fd.c:2651
int errdetail_internal(const char *fmt,...)
Definition: elog.c:982
bool wait_result_is_signal(int exit_status, int signum)
Definition: wait_error.c:92
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
bool is_copy_from
Definition: copy.c:120
#define ereport(elevel, rest)
Definition: elog.h:141
bool is_program
Definition: copy.c:133
#define Assert(condition)
Definition: c.h:739
bool reached_eof
Definition: copy.c:121
int errmsg(const char *fmt,...)
Definition: elog.c:822
FILE * copy_file
Definition: copy.c:117

◆ copy_dest_destroy()

static void copy_dest_destroy ( DestReceiver self)
static

Definition at line 5191 of file copy.c.

References pfree().

Referenced by CreateCopyDestReceiver().

5192 {
5193  pfree(self);
5194 }
void pfree(void *pointer)
Definition: mcxt.c:1056

◆ copy_dest_receive()

static bool copy_dest_receive ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 5166 of file copy.c.

References CopyOneRowTo(), DR_copy::cstate, and DR_copy::processed.

Referenced by CreateCopyDestReceiver().

5167 {
5168  DR_copy *myState = (DR_copy *) self;
5169  CopyState cstate = myState->cstate;
5170 
5171  /* Send the data */
5172  CopyOneRowTo(cstate, slot);
5173  myState->processed++;
5174 
5175  return true;
5176 }
Definition: copy.c:228
CopyState cstate
Definition: copy.c:231
static void CopyOneRowTo(CopyState cstate, TupleTableSlot *slot)
Definition: copy.c:2159
uint64 processed
Definition: copy.c:232

◆ copy_dest_shutdown()

static void copy_dest_shutdown ( DestReceiver self)
static

Definition at line 5182 of file copy.c.

Referenced by CreateCopyDestReceiver().

5183 {
5184  /* no-op */
5185 }

◆ copy_dest_startup()

static void copy_dest_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)
static

Definition at line 5157 of file copy.c.

Referenced by CreateCopyDestReceiver().

5158 {
5159  /* no-op */
5160 }

◆ CopyAttributeOutCSV()

static void CopyAttributeOutCSV ( CopyState  cstate,
char *  string,
bool  use_quote,
bool  single_attr 
)
static

Definition at line 4980 of file copy.c.

References CopySendChar(), CopySendString(), CopyStateData::delim, DUMPSOFAR, CopyStateData::encoding_embeds_ascii, CopyStateData::escape, CopyStateData::file_encoding, IS_HIGHBIT_SET, CopyStateData::need_transcoding, CopyStateData::null_print, pg_encoding_mblen(), pg_server_to_any(), and CopyStateData::quote.

Referenced by CopyOneRowTo(), and CopyTo().

4982 {
4983  char *ptr;
4984  char *start;
4985  char c;
4986  char delimc = cstate->delim[0];
4987  char quotec = cstate->quote[0];
4988  char escapec = cstate->escape[0];
4989 
4990  /* force quoting if it matches null_print (before conversion!) */
4991  if (!use_quote && strcmp(string, cstate->null_print) == 0)
4992  use_quote = true;
4993 
4994  if (cstate->need_transcoding)
4995  ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
4996  else
4997  ptr = string;
4998 
4999  /*
5000  * Make a preliminary pass to discover if it needs quoting
5001  */
5002  if (!use_quote)
5003  {
5004  /*
5005  * Because '\.' can be a data value, quote it if it appears alone on a
5006  * line so it is not interpreted as the end-of-data marker.
5007  */
5008  if (single_attr && strcmp(ptr, "\\.") == 0)
5009  use_quote = true;
5010  else
5011  {
5012  char *tptr = ptr;
5013 
5014  while ((c = *tptr) != '\0')
5015  {
5016  if (c == delimc || c == quotec || c == '\n' || c == '\r')
5017  {
5018  use_quote = true;
5019  break;
5020  }
5021  if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
5022  tptr += pg_encoding_mblen(cstate->file_encoding, tptr);
5023  else
5024  tptr++;
5025  }
5026  }
5027  }
5028 
5029  if (use_quote)
5030  {
5031  CopySendChar(cstate, quotec);
5032 
5033  /*
5034  * We adopt the same optimization strategy as in CopyAttributeOutText
5035  */
5036  start = ptr;
5037  while ((c = *ptr) != '\0')
5038  {
5039  if (c == quotec || c == escapec)
5040  {
5041  DUMPSOFAR();
5042  CopySendChar(cstate, escapec);
5043  start = ptr; /* we include char in next run */
5044  }
5045  if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
5046  ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
5047  else
5048  ptr++;
5049  }
5050  DUMPSOFAR();
5051 
5052  CopySendChar(cstate, quotec);
5053  }
5054  else
5055  {
5056  /* If it doesn't need quoting, we can just dump it as-is */
5057  CopySendString(cstate, ptr);
5058  }
5059 }
bool need_transcoding
Definition: copy.c:125
char * delim
Definition: copy.c:142
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:654
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:511
char * null_print
Definition: copy.c:139
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1106
bool encoding_embeds_ascii
Definition: copy.c:126
char * c
char * quote
Definition: copy.c:143
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1554
char string[11]
Definition: preproc-type.c:46
char * escape
Definition: copy.c:144
int file_encoding
Definition: copy.c:124
static void CopySendString(CopyState cstate, const char *str)
Definition: copy.c:505
#define DUMPSOFAR()
Definition: copy.c:4820

◆ CopyAttributeOutText()

static void CopyAttributeOutText ( CopyState  cstate,
char *  string 
)
static

Definition at line 4827 of file copy.c.

References CopySendChar(), CopyStateData::delim, DUMPSOFAR, CopyStateData::encoding_embeds_ascii, CopyStateData::file_encoding, IS_HIGHBIT_SET, CopyStateData::need_transcoding, pg_encoding_mblen(), and pg_server_to_any().

Referenced by CopyOneRowTo().

4828 {
4829  char *ptr;
4830  char *start;
4831  char c;
4832  char delimc = cstate->delim[0];
4833 
4834  if (cstate->need_transcoding)
4835  ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
4836  else
4837  ptr = string;
4838 
4839  /*
4840  * We have to grovel through the string searching for control characters
4841  * and instances of the delimiter character. In most cases, though, these
4842  * are infrequent. To avoid overhead from calling CopySendData once per
4843  * character, we dump out all characters between escaped characters in a
4844  * single call. The loop invariant is that the data from "start" to "ptr"
4845  * can be sent literally, but hasn't yet been.
4846  *
4847  * We can skip pg_encoding_mblen() overhead when encoding is safe, because
4848  * in valid backend encodings, extra bytes of a multibyte character never
4849  * look like ASCII. This loop is sufficiently performance-critical that
4850  * it's worth making two copies of it to get the IS_HIGHBIT_SET() test out
4851  * of the normal safe-encoding path.
4852  */
4853  if (cstate->encoding_embeds_ascii)
4854  {
4855  start = ptr;
4856  while ((c = *ptr) != '\0')
4857  {
4858  if ((unsigned char) c < (unsigned char) 0x20)
4859  {
4860  /*
4861  * \r and \n must be escaped, the others are traditional. We
4862  * prefer to dump these using the C-like notation, rather than
4863  * a backslash and the literal character, because it makes the
4864  * dump file a bit more proof against Microsoftish data
4865  * mangling.
4866  */
4867  switch (c)
4868  {
4869  case '\b':
4870  c = 'b';
4871  break;
4872  case '\f':
4873  c = 'f';
4874  break;
4875  case '\n':
4876  c = 'n';
4877  break;
4878  case '\r':
4879  c = 'r';
4880  break;
4881  case '\t':
4882  c = 't';
4883  break;
4884  case '\v':
4885  c = 'v';
4886  break;
4887  default:
4888  /* If it's the delimiter, must backslash it */
4889  if (c == delimc)
4890  break;
4891  /* All ASCII control chars are length 1 */
4892  ptr++;
4893  continue; /* fall to end of loop */
4894  }
4895  /* if we get here, we need to convert the control char */
4896  DUMPSOFAR();
4897  CopySendChar(cstate, '\\');
4898  CopySendChar(cstate, c);
4899  start = ++ptr; /* do not include char in next run */
4900  }
4901  else if (c == '\\' || c == delimc)
4902  {
4903  DUMPSOFAR();
4904  CopySendChar(cstate, '\\');
4905  start = ptr++; /* we include char in next run */
4906  }
4907  else if (IS_HIGHBIT_SET(c))
4908  ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
4909  else
4910  ptr++;
4911  }
4912  }
4913  else
4914  {
4915  start = ptr;
4916  while ((c = *ptr) != '\0')
4917  {
4918  if ((unsigned char) c < (unsigned char) 0x20)
4919  {
4920  /*
4921  * \r and \n must be escaped, the others are traditional. We
4922  * prefer to dump these using the C-like notation, rather than
4923  * a backslash and the literal character, because it makes the
4924  * dump file a bit more proof against Microsoftish data
4925  * mangling.
4926  */
4927  switch (c)
4928  {
4929  case '\b':
4930  c = 'b';
4931  break;
4932  case '\f':
4933  c = 'f';
4934  break;
4935  case '\n':
4936  c = 'n';
4937  break;
4938  case '\r':
4939  c = 'r';
4940  break;
4941  case '\t':
4942  c = 't';
4943  break;
4944  case '\v':
4945  c = 'v';
4946  break;
4947  default:
4948  /* If it's the delimiter, must backslash it */
4949  if (c == delimc)
4950  break;
4951  /* All ASCII control chars are length 1 */
4952  ptr++;
4953  continue; /* fall to end of loop */
4954  }
4955  /* if we get here, we need to convert the control char */
4956  DUMPSOFAR();
4957  CopySendChar(cstate, '\\');
4958  CopySendChar(cstate, c);
4959  start = ++ptr; /* do not include char in next run */
4960  }
4961  else if (c == '\\' || c == delimc)
4962  {
4963  DUMPSOFAR();
4964  CopySendChar(cstate, '\\');
4965  start = ptr++; /* we include char in next run */
4966  }
4967  else
4968  ptr++;
4969  }
4970  }
4971 
4972  DUMPSOFAR();
4973 }
bool need_transcoding
Definition: copy.c:125
char * delim
Definition: copy.c:142
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:654
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:511
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1106
bool encoding_embeds_ascii
Definition: copy.c:126
char * c
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1554
char string[11]
Definition: preproc-type.c:46
int file_encoding
Definition: copy.c:124
#define DUMPSOFAR()
Definition: copy.c:4820

◆ CopyFrom()

uint64 CopyFrom ( CopyState  cstate)

Definition at line 2656 of file copy.c.

References AfterTriggerBeginQuery(), AfterTriggerEndQuery(), ErrorContextCallback::arg, Assert, TupleConversionMap::attrMap, FdwRoutine::BeginForeignInsert, ErrorContextCallback::callback, castNode, CHECK_FOR_INTERRUPTS, CheckValidResultRel(), CIM_MULTI, CIM_MULTI_CONDITIONAL, CIM_SINGLE, CMD_INSERT, TupleDescData::constr, contain_volatile_functions(), CopyStateData::copy_dest, COPY_OLD_FE, CopyFromErrorCallback(), CopyMultiInsertInfoCleanup(), CopyMultiInsertInfoFlush(), CopyMultiInsertInfoInit(), CopyMultiInsertInfoIsEmpty(), CopyMultiInsertInfoIsFull(), CopyMultiInsertInfoNextFreeSlot(), CopyMultiInsertInfoSetupBuffer(), CopyMultiInsertInfoStore(), CreateExecutorState(), CopyStateData::cur_lineno, CurrentMemoryContext, ExprContext::ecxt_scantuple, FdwRoutine::EndForeignInsert, ereport, errcode(), errhint(), errmsg(), ERROR, error_context_stack, EState::es_num_result_relations, EState::es_result_relation_info, EState::es_result_relations, EState::es_tupleTable, ExecARInsertTriggers(), ExecASInsertTriggers(), ExecBRInsertTriggers(), ExecBSInsertTriggers(), ExecCleanUpTriggerState(), ExecCleanupTupleRouting(), ExecClearTuple(), ExecCloseIndices(), ExecComputeStoredGenerated(), ExecConstraints(), ExecCopySlot(), ExecFindPartition(), FdwRoutine::ExecForeignInsert, ExecInitQual(), ExecInitRangeTable(), ExecInsertIndexTuples(), ExecIRInsertTriggers(), ExecMaterializeSlot(), ExecOpenIndices(), ExecPartitionCheck(), ExecQual(), ExecResetTupleTable(), ExecSetupPartitionTupleRouting(), ExecStoreVirtualTuple(), execute_attr_map_slot(), FreeBulkInsertState(), FreeExecutorState(), CopyStateData::freeze, GetBulkInsertState(), GetCurrentCommandId(), GetCurrentSubTransactionId(), GetPerTupleExprContext, GetPerTupleMemoryContext, TupleConstr::has_generated_stored, InitResultRelInfo(), InvalidateCatalogSnapshot(), InvalidSubTransactionId, StringInfoData::len, CopyStateData::line_buf, list_free(), makeNode, MakeTransitionCaptureState(), MemoryContextSwitchTo(), ModifyTableState::mt_transition_capture, NextCopyFrom(), NIL, ModifyTableState::operation, PartitionRoutingInfo::pi_PartitionToRootMap, PartitionRoutingInfo::pi_PartitionTupleSlot, PartitionRoutingInfo::pi_RootToPartitionMap, PlanState::plan, pq_endmsgread(), ErrorContextCallback::previous, ModifyTableState::ps, CopyStateData::qualexpr, CopyStateData::range_table, RelationData::rd_att, RelationData::rd_createSubid, RelationData::rd_newRelfilenodeSubid, RelationData::rd_rel, CopyStateData::rel, RelationGetRelationName, RelationGetRelid, ReleaseBulkInsertStatePin(), ResetPerTupleExprContext, ModifyTableState::resultRelInfo, ResultRelInfo::ri_CopyMultiInsertBuffer, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_PartitionCheck, ResultRelInfo::ri_PartitionInfo, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, PlanState::state, TABLE_INSERT_FROZEN, TABLE_INSERT_SKIP_FSM, TABLE_INSERT_SKIP_WAL, table_slot_create(), table_tuple_insert(), TransitionCaptureState::tcs_map, TransitionCaptureState::tcs_original_insert_tuple, ThereAreNoPriorRegisteredSnapshots(), ThereAreNoReadyPortals(), CopyStateData::transition_capture, TriggerDesc::trig_insert_before_row, TriggerDesc::trig_insert_instead_row, TriggerDesc::trig_insert_new_table, RelationData::trigdesc, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tableOid, TupleTableSlot::tts_values, CopyStateData::volatile_defexprs, CopyStateData::whereClause, and XLogIsNeeded.

Referenced by copy_table(), and DoCopy().

2657 {
2658  ResultRelInfo *resultRelInfo;
2659  ResultRelInfo *target_resultRelInfo;
2660  ResultRelInfo *prevResultRelInfo = NULL;
2661  EState *estate = CreateExecutorState(); /* for ExecConstraints() */
2662  ModifyTableState *mtstate;
2663  ExprContext *econtext;
2664  TupleTableSlot *singleslot = NULL;
2665  MemoryContext oldcontext = CurrentMemoryContext;
2666 
2667  PartitionTupleRouting *proute = NULL;
2668  ErrorContextCallback errcallback;
2669  CommandId mycid = GetCurrentCommandId(true);
2670  int ti_options = 0; /* start with default options for insert */
2671  BulkInsertState bistate = NULL;
2672  CopyInsertMethod insertMethod;
2673  CopyMultiInsertInfo multiInsertInfo = {0}; /* pacify compiler */
2674  uint64 processed = 0;
2675  bool has_before_insert_row_trig;
2676  bool has_instead_insert_row_trig;
2677  bool leafpart_use_multi_insert = false;
2678 
2679  Assert(cstate->rel);
2680 
2681  /*
2682  * The target must be a plain, foreign, or partitioned relation, or have
2683  * an INSTEAD OF INSERT row trigger. (Currently, such triggers are only
2684  * allowed on views, so we only hint about them in the view case.)
2685  */
2686  if (cstate->rel->rd_rel->relkind != RELKIND_RELATION &&
2687  cstate->rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
2688  cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
2689  !(cstate->rel->trigdesc &&
2691  {
2692  if (cstate->rel->rd_rel->relkind == RELKIND_VIEW)
2693  ereport(ERROR,
2694  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2695  errmsg("cannot copy to view \"%s\"",
2696  RelationGetRelationName(cstate->rel)),
2697  errhint("To enable copying to a view, provide an INSTEAD OF INSERT trigger.")));
2698  else if (cstate->rel->rd_rel->relkind == RELKIND_MATVIEW)
2699  ereport(ERROR,
2700  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2701  errmsg("cannot copy to materialized view \"%s\"",
2702  RelationGetRelationName(cstate->rel))));
2703  else if (cstate->rel->rd_rel->relkind == RELKIND_SEQUENCE)
2704  ereport(ERROR,
2705  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2706  errmsg("cannot copy to sequence \"%s\"",
2707  RelationGetRelationName(cstate->rel))));
2708  else
2709  ereport(ERROR,
2710  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2711  errmsg("cannot copy to non-table relation \"%s\"",
2712  RelationGetRelationName(cstate->rel))));
2713  }
2714 
2715  /*----------
2716  * Check to see if we can avoid writing WAL
2717  *
2718  * If archive logging/streaming is not enabled *and* either
2719  * - table was created in same transaction as this COPY
2720  * - data is being written to relfilenode created in this transaction
2721  * then we can skip writing WAL. It's safe because if the transaction
2722  * doesn't commit, we'll discard the table (or the new relfilenode file).
2723  * If it does commit, we'll have done the table_finish_bulk_insert() at
2724  * the bottom of this routine first.
2725  *
2726  * As mentioned in comments in utils/rel.h, the in-same-transaction test
2727  * is not always set correctly, since in rare cases rd_newRelfilenodeSubid
2728  * can be cleared before the end of the transaction. The exact case is
2729  * when a relation sets a new relfilenode twice in same transaction, yet
2730  * the second one fails in an aborted subtransaction, e.g.
2731  *
2732  * BEGIN;
2733  * TRUNCATE t;
2734  * SAVEPOINT save;
2735  * TRUNCATE t;
2736  * ROLLBACK TO save;
2737  * COPY ...
2738  *
2739  * Also, if the target file is new-in-transaction, we assume that checking
2740  * FSM for free space is a waste of time, even if we must use WAL because
2741  * of archiving. This could possibly be wrong, but it's unlikely.
2742  *
2743  * The comments for table_tuple_insert and RelationGetBufferForTuple
2744  * specify that skipping WAL logging is only safe if we ensure that our
2745  * tuples do not go into pages containing tuples from any other
2746  * transactions --- but this must be the case if we have a new table or
2747  * new relfilenode, so we need no additional work to enforce that.
2748  *
2749  * We currently don't support this optimization if the COPY target is a
2750  * partitioned table as we currently only lazily initialize partition
2751  * information when routing the first tuple to the partition. We cannot
2752  * know at this stage if we can perform this optimization. It should be
2753  * possible to improve on this, but it does mean maintaining heap insert
2754  * option flags per partition and setting them when we first open the
2755  * partition.
2756  *
2757  * This optimization is not supported for relation types which do not
2758  * have any physical storage, with foreign tables and views using
2759  * INSTEAD OF triggers entering in this category. Partitioned tables
2760  * are not supported as per the description above.
2761  *----------
2762  */
2763  /* createSubid is creation check, newRelfilenodeSubid is truncation check */
2764  if (RELKIND_HAS_STORAGE(cstate->rel->rd_rel->relkind) &&
2767  {
2768  ti_options |= TABLE_INSERT_SKIP_FSM;
2769  if (!XLogIsNeeded())
2770  ti_options |= TABLE_INSERT_SKIP_WAL;
2771  }
2772 
2773  /*
2774  * Optimize if new relfilenode was created in this subxact or one of its
2775  * committed children and we won't see those rows later as part of an
2776  * earlier scan or command. The subxact test ensures that if this subxact
2777  * aborts then the frozen rows won't be visible after xact cleanup. Note
2778  * that the stronger test of exactly which subtransaction created it is
2779  * crucial for correctness of this optimization. The test for an earlier
2780  * scan or command tolerates false negatives. FREEZE causes other sessions
2781  * to see rows they would not see under MVCC, and a false negative merely
2782  * spreads that anomaly to the current session.
2783  */
2784  if (cstate->freeze)
2785  {
2786  /*
2787  * We currently disallow COPY FREEZE on partitioned tables. The
2788  * reason for this is that we've simply not yet opened the partitions
2789  * to determine if the optimization can be applied to them. We could
2790  * go and open them all here, but doing so may be quite a costly
2791  * overhead for small copies. In any case, we may just end up routing
2792  * tuples to a small number of partitions. It seems better just to
2793  * raise an ERROR for partitioned tables.
2794  */
2795  if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2796  {
2797  ereport(ERROR,
2798  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2799  errmsg("cannot perform COPY FREEZE on a partitioned table")));
2800  }
2801 
2802  /*
2803  * Tolerate one registration for the benefit of FirstXactSnapshot.
2804  * Scan-bearing queries generally create at least two registrations,
2805  * though relying on that is fragile, as is ignoring ActiveSnapshot.
2806  * Clear CatalogSnapshot to avoid counting its registration. We'll
2807  * still detect ongoing catalog scans, each of which separately
2808  * registers the snapshot it uses.
2809  */
2812  ereport(ERROR,
2813  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
2814  errmsg("cannot perform COPY FREEZE because of prior transaction activity")));
2815 
2816  if (cstate->rel->rd_createSubid != GetCurrentSubTransactionId() &&
2818  ereport(ERROR,
2819  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2820  errmsg("cannot perform COPY FREEZE because the table was not created or truncated in the current subtransaction")));
2821 
2822  ti_options |= TABLE_INSERT_FROZEN;
2823  }
2824 
2825  /*
2826  * We need a ResultRelInfo so we can use the regular executor's
2827  * index-entry-making machinery. (There used to be a huge amount of code
2828  * here that basically duplicated execUtils.c ...)
2829  */
2830  resultRelInfo = makeNode(ResultRelInfo);
2831  InitResultRelInfo(resultRelInfo,
2832  cstate->rel,
2833  1, /* must match rel's position in range_table */
2834  NULL,
2835  0);
2836  target_resultRelInfo = resultRelInfo;
2837 
2838  /* Verify the named relation is a valid target for INSERT */
2839  CheckValidResultRel(resultRelInfo, CMD_INSERT);
2840 
2841  ExecOpenIndices(resultRelInfo, false);
2842 
2843  estate->es_result_relations = resultRelInfo;
2844  estate->es_num_result_relations = 1;
2845  estate->es_result_relation_info = resultRelInfo;
2846 
2847  ExecInitRangeTable(estate, cstate->range_table);
2848 
2849  /*
2850  * Set up a ModifyTableState so we can let FDW(s) init themselves for
2851  * foreign-table result relation(s).
2852  */
2853  mtstate = makeNode(ModifyTableState);
2854  mtstate->ps.plan = NULL;
2855  mtstate->ps.state = estate;
2856  mtstate->operation = CMD_INSERT;
2857  mtstate->resultRelInfo = estate->es_result_relations;
2858 
2859  if (resultRelInfo->ri_FdwRoutine != NULL &&
2860  resultRelInfo->ri_FdwRoutine->BeginForeignInsert != NULL)
2861  resultRelInfo->ri_FdwRoutine->BeginForeignInsert(mtstate,
2862  resultRelInfo);
2863 
2864  /* Prepare to catch AFTER triggers. */
2866 
2867  /*
2868  * If there are any triggers with transition tables on the named relation,
2869  * we need to be prepared to capture transition tuples.
2870  *
2871  * Because partition tuple routing would like to know about whether
2872  * transition capture is active, we also set it in mtstate, which is
2873  * passed to ExecFindPartition() below.
2874  */
2875  cstate->transition_capture = mtstate->mt_transition_capture =
2877  RelationGetRelid(cstate->rel),
2878  CMD_INSERT);
2879 
2880  /*
2881  * If the named relation is a partitioned table, initialize state for
2882  * CopyFrom tuple routing.
2883  */
2884  if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2885  proute = ExecSetupPartitionTupleRouting(estate, NULL, cstate->rel);
2886 
2887  if (cstate->whereClause)
2888  cstate->qualexpr = ExecInitQual(castNode(List, cstate->whereClause),
2889  &mtstate->ps);
2890 
2891  /*
2892  * It's generally more efficient to prepare a bunch of tuples for
2893  * insertion, and insert them in one table_multi_insert() call, than call
2894  * table_tuple_insert() separately for every tuple. However, there are a
2895  * number of reasons why we might not be able to do this. These are
2896  * explained below.
2897  */
2898  if (resultRelInfo->ri_TrigDesc != NULL &&
2899  (resultRelInfo->ri_TrigDesc->trig_insert_before_row ||
2900  resultRelInfo->ri_TrigDesc->trig_insert_instead_row))
2901  {
2902  /*
2903  * Can't support multi-inserts when there are any BEFORE/INSTEAD OF
2904  * triggers on the table. Such triggers might query the table we're
2905  * inserting into and act differently if the tuples that have already
2906  * been processed and prepared for insertion are not there.
2907  */
2908  insertMethod = CIM_SINGLE;
2909  }
2910  else if (proute != NULL && resultRelInfo->ri_TrigDesc != NULL &&
2911  resultRelInfo->ri_TrigDesc->trig_insert_new_table)
2912  {
2913  /*
2914  * For partitioned tables we can't support multi-inserts when there
2915  * are any statement level insert triggers. It might be possible to
2916  * allow partitioned tables with such triggers in the future, but for
2917  * now, CopyMultiInsertInfoFlush expects that any before row insert
2918  * and statement level insert triggers are on the same relation.
2919  */
2920  insertMethod = CIM_SINGLE;
2921  }
2922  else if (resultRelInfo->ri_FdwRoutine != NULL ||
2923  cstate->volatile_defexprs)
2924  {
2925  /*
2926  * Can't support multi-inserts to foreign tables or if there are any
2927  * volatile default expressions in the table. Similarly to the
2928  * trigger case above, such expressions may query the table we're
2929  * inserting into.
2930  *
2931  * Note: It does not matter if any partitions have any volatile
2932  * default expressions as we use the defaults from the target of the
2933  * COPY command.
2934  */
2935  insertMethod = CIM_SINGLE;
2936  }
2937  else if (contain_volatile_functions(cstate->whereClause))
2938  {
2939  /*
2940  * Can't support multi-inserts if there are any volatile function
2941  * expressions in WHERE clause. Similarly to the trigger case above,
2942  * such expressions may query the table we're inserting into.
2943  */
2944  insertMethod = CIM_SINGLE;
2945  }
2946  else
2947  {
2948  /*
2949  * For partitioned tables, we may still be able to perform bulk
2950  * inserts. However, the possibility of this depends on which types
2951  * of triggers exist on the partition. We must disable bulk inserts
2952  * if the partition is a foreign table or it has any before row insert
2953  * or insert instead triggers (same as we checked above for the parent
2954  * table). Since the partition's resultRelInfos are initialized only
2955  * when we actually need to insert the first tuple into them, we must
2956  * have the intermediate insert method of CIM_MULTI_CONDITIONAL to
2957  * flag that we must later determine if we can use bulk-inserts for
2958  * the partition being inserted into.
2959  */
2960  if (proute)
2961  insertMethod = CIM_MULTI_CONDITIONAL;
2962  else
2963  insertMethod = CIM_MULTI;
2964 
2965  CopyMultiInsertInfoInit(&multiInsertInfo, resultRelInfo, cstate,
2966  estate, mycid, ti_options);
2967  }
2968 
2969  /*
2970  * If not using batch mode (which allocates slots as needed) set up a
2971  * tuple slot too. When inserting into a partitioned table, we also need
2972  * one, even if we might batch insert, to read the tuple in the root
2973  * partition's form.
2974  */
2975  if (insertMethod == CIM_SINGLE || insertMethod == CIM_MULTI_CONDITIONAL)
2976  {
2977  singleslot = table_slot_create(resultRelInfo->ri_RelationDesc,
2978  &estate->es_tupleTable);
2979  bistate = GetBulkInsertState();
2980  }
2981 
2982  has_before_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
2983  resultRelInfo->ri_TrigDesc->trig_insert_before_row);
2984 
2985  has_instead_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
2986  resultRelInfo->ri_TrigDesc->trig_insert_instead_row);
2987 
2988  /*
2989  * Check BEFORE STATEMENT insertion triggers. It's debatable whether we
2990  * should do this for COPY, since it's not really an "INSERT" statement as
2991  * such. However, executing these triggers maintains consistency with the
2992  * EACH ROW triggers that we already fire on COPY.
2993  */
2994  ExecBSInsertTriggers(estate, resultRelInfo);
2995 
2996  econtext = GetPerTupleExprContext(estate);
2997 
2998  /* Set up callback to identify error line number */
2999  errcallback.callback = CopyFromErrorCallback;
3000  errcallback.arg = (void *) cstate;
3001  errcallback.previous = error_context_stack;
3002  error_context_stack = &errcallback;
3003 
3004  for (;;)
3005  {
3006  TupleTableSlot *myslot;
3007  bool skip_tuple;
3008 
3010 
3011  /*
3012  * Reset the per-tuple exprcontext. We do this after every tuple, to
3013  * clean-up after expression evaluations etc.
3014  */
3015  ResetPerTupleExprContext(estate);
3016 
3017  /* select slot to (initially) load row into */
3018  if (insertMethod == CIM_SINGLE || proute)
3019  {
3020  myslot = singleslot;
3021  Assert(myslot != NULL);
3022  }
3023  else
3024  {
3025  Assert(resultRelInfo == target_resultRelInfo);
3026  Assert(insertMethod == CIM_MULTI);
3027 
3028  myslot = CopyMultiInsertInfoNextFreeSlot(&multiInsertInfo,
3029  resultRelInfo);
3030  }
3031 
3032  /*
3033  * Switch to per-tuple context before calling NextCopyFrom, which does
3034  * evaluate default expressions etc. and requires per-tuple context.
3035  */
3037 
3038  ExecClearTuple(myslot);
3039 
3040  /* Directly store the values/nulls array in the slot */
3041  if (!NextCopyFrom(cstate, econtext, myslot->tts_values, myslot->tts_isnull))
3042  break;
3043 
3044  ExecStoreVirtualTuple(myslot);
3045 
3046  /*
3047  * Constraints and where clause might reference the tableoid column,
3048  * so (re-)initialize tts_tableOid before evaluating them.
3049  */
3050  myslot->tts_tableOid = RelationGetRelid(target_resultRelInfo->ri_RelationDesc);
3051 
3052  /* Triggers and stuff need to be invoked in query context. */
3053  MemoryContextSwitchTo(oldcontext);
3054 
3055  if (cstate->whereClause)
3056  {
3057  econtext->ecxt_scantuple = myslot;
3058  /* Skip items that don't match COPY's WHERE clause */
3059  if (!ExecQual(cstate->qualexpr, econtext))
3060  continue;
3061  }
3062 
3063  /* Determine the partition to insert the tuple into */
3064  if (proute)
3065  {
3066  TupleConversionMap *map;
3067 
3068  /*
3069  * Attempt to find a partition suitable for this tuple.
3070  * ExecFindPartition() will raise an error if none can be found or
3071  * if the found partition is not suitable for INSERTs.
3072  */
3073  resultRelInfo = ExecFindPartition(mtstate, target_resultRelInfo,
3074  proute, myslot, estate);
3075 
3076  if (prevResultRelInfo != resultRelInfo)
3077  {
3078  /* Determine which triggers exist on this partition */
3079  has_before_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
3080  resultRelInfo->ri_TrigDesc->trig_insert_before_row);
3081 
3082  has_instead_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
3083  resultRelInfo->ri_TrigDesc->trig_insert_instead_row);
3084 
3085  /*
3086  * Disable multi-inserts when the partition has BEFORE/INSTEAD
3087  * OF triggers, or if the partition is a foreign partition.
3088  */
3089  leafpart_use_multi_insert = insertMethod == CIM_MULTI_CONDITIONAL &&
3090  !has_before_insert_row_trig &&
3091  !has_instead_insert_row_trig &&
3092  resultRelInfo->ri_FdwRoutine == NULL;
3093 
3094  /* Set the multi-insert buffer to use for this partition. */
3095  if (leafpart_use_multi_insert)
3096  {
3097  if (resultRelInfo->ri_CopyMultiInsertBuffer == NULL)
3098  CopyMultiInsertInfoSetupBuffer(&multiInsertInfo,
3099  resultRelInfo);
3100  }
3101  else if (insertMethod == CIM_MULTI_CONDITIONAL &&
3102  !CopyMultiInsertInfoIsEmpty(&multiInsertInfo))
3103  {
3104  /*
3105  * Flush pending inserts if this partition can't use
3106  * batching, so rows are visible to triggers etc.
3107  */
3108  CopyMultiInsertInfoFlush(&multiInsertInfo, resultRelInfo);
3109  }
3110 
3111  if (bistate != NULL)
3112  ReleaseBulkInsertStatePin(bistate);
3113  prevResultRelInfo = resultRelInfo;
3114  }
3115 
3116  /*
3117  * For ExecInsertIndexTuples() to work on the partition's indexes
3118  */
3119  estate->es_result_relation_info = resultRelInfo;
3120 
3121  /*
3122  * If we're capturing transition tuples, we might need to convert
3123  * from the partition rowtype to root rowtype.
3124  */
3125  if (cstate->transition_capture != NULL)
3126  {
3127  if (has_before_insert_row_trig)
3128  {
3129  /*
3130  * If there are any BEFORE triggers on the partition,
3131  * we'll have to be ready to convert their result back to
3132  * tuplestore format.
3133  */
3135  cstate->transition_capture->tcs_map =
3136  resultRelInfo->ri_PartitionInfo->pi_PartitionToRootMap;
3137  }
3138  else
3139  {
3140  /*
3141  * Otherwise, just remember the original unconverted
3142  * tuple, to avoid a needless round trip conversion.
3143  */
3145  cstate->transition_capture->tcs_map = NULL;
3146  }
3147  }
3148 
3149  /*
3150  * We might need to convert from the root rowtype to the partition
3151  * rowtype.
3152  */
3153  map = resultRelInfo->ri_PartitionInfo->pi_RootToPartitionMap;
3154  if (insertMethod == CIM_SINGLE || !leafpart_use_multi_insert)
3155  {
3156  /* non batch insert */
3157  if (map != NULL)
3158  {
3159  TupleTableSlot *new_slot;
3160 
3161  new_slot = resultRelInfo->ri_PartitionInfo->pi_PartitionTupleSlot;
3162  myslot = execute_attr_map_slot(map->attrMap, myslot, new_slot);
3163  }
3164  }
3165  else
3166  {
3167  /*
3168  * Prepare to queue up tuple for later batch insert into
3169  * current partition.
3170  */
3171  TupleTableSlot *batchslot;
3172 
3173  /* no other path available for partitioned table */
3174  Assert(insertMethod == CIM_MULTI_CONDITIONAL);
3175 
3176  batchslot = CopyMultiInsertInfoNextFreeSlot(&multiInsertInfo,
3177  resultRelInfo);
3178 
3179  if (map != NULL)
3180  myslot = execute_attr_map_slot(map->attrMap, myslot,
3181  batchslot);
3182  else
3183  {
3184  /*
3185  * This looks more expensive than it is (Believe me, I
3186  * optimized it away. Twice.). The input is in virtual
3187  * form, and we'll materialize the slot below - for most
3188  * slot types the copy performs the work materialization
3189  * would later require anyway.
3190  */
3191  ExecCopySlot(batchslot, myslot);
3192  myslot = batchslot;
3193  }
3194  }
3195 
3196  /* ensure that triggers etc see the right relation */
3197  myslot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
3198  }
3199 
3200  skip_tuple = false;
3201 
3202  /* BEFORE ROW INSERT Triggers */
3203  if (has_before_insert_row_trig)
3204  {
3205  if (!ExecBRInsertTriggers(estate, resultRelInfo, myslot))
3206  skip_tuple = true; /* "do nothing" */
3207  }
3208 
3209  if (!skip_tuple)
3210  {
3211  /*
3212  * If there is an INSTEAD OF INSERT ROW trigger, let it handle the
3213  * tuple. Otherwise, proceed with inserting the tuple into the
3214  * table or foreign table.
3215  */
3216  if (has_instead_insert_row_trig)
3217  {
3218  ExecIRInsertTriggers(estate, resultRelInfo, myslot);
3219  }
3220  else
3221  {
3222  /* Compute stored generated columns */
3223  if (resultRelInfo->ri_RelationDesc->rd_att->constr &&
3225  ExecComputeStoredGenerated(estate, myslot);
3226 
3227  /*
3228  * If the target is a plain table, check the constraints of
3229  * the tuple.
3230  */
3231  if (resultRelInfo->ri_FdwRoutine == NULL &&
3232  resultRelInfo->ri_RelationDesc->rd_att->constr)
3233  ExecConstraints(resultRelInfo, myslot, estate);
3234 
3235  /*
3236  * Also check the tuple against the partition constraint, if
3237  * there is one; except that if we got here via tuple-routing,
3238  * we don't need to if there's no BR trigger defined on the
3239  * partition.
3240  */
3241  if (resultRelInfo->ri_PartitionCheck &&
3242  (proute == NULL || has_before_insert_row_trig))
3243  ExecPartitionCheck(resultRelInfo, myslot, estate, true);
3244 
3245  /* Store the slot in the multi-insert buffer, when enabled. */
3246  if (insertMethod == CIM_MULTI || leafpart_use_multi_insert)
3247  {
3248  /*
3249  * The slot previously might point into the per-tuple
3250  * context. For batching it needs to be longer lived.
3251  */
3252  ExecMaterializeSlot(myslot);
3253 
3254  /* Add this tuple to the tuple buffer */
3255  CopyMultiInsertInfoStore(&multiInsertInfo,
3256  resultRelInfo, myslot,
3257  cstate->line_buf.len,
3258  cstate->cur_lineno);
3259 
3260  /*
3261  * If enough inserts have queued up, then flush all
3262  * buffers out to their tables.
3263  */
3264  if (CopyMultiInsertInfoIsFull(&multiInsertInfo))
3265  CopyMultiInsertInfoFlush(&multiInsertInfo, resultRelInfo);
3266  }
3267  else
3268  {
3269  List *recheckIndexes = NIL;
3270 
3271  /* OK, store the tuple */
3272  if (resultRelInfo->ri_FdwRoutine != NULL)
3273  {
3274  myslot = resultRelInfo->ri_FdwRoutine->ExecForeignInsert(estate,
3275  resultRelInfo,
3276  myslot,
3277  NULL);
3278 
3279  if (myslot == NULL) /* "do nothing" */
3280  continue; /* next tuple please */
3281 
3282  /*
3283  * AFTER ROW Triggers might reference the tableoid
3284  * column, so (re-)initialize tts_tableOid before
3285  * evaluating them.
3286  */
3287  myslot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
3288  }
3289  else
3290  {
3291  /* OK, store the tuple and create index entries for it */
3292  table_tuple_insert(resultRelInfo->ri_RelationDesc,
3293  myslot, mycid, ti_options, bistate);
3294 
3295  if (resultRelInfo->ri_NumIndices > 0)
3296  recheckIndexes = ExecInsertIndexTuples(myslot,
3297  estate,
3298  false,
3299  NULL,
3300  NIL);
3301  }
3302 
3303  /* AFTER ROW INSERT Triggers */
3304  ExecARInsertTriggers(estate, resultRelInfo, myslot,
3305  recheckIndexes, cstate->transition_capture);
3306 
3307  list_free(recheckIndexes);
3308  }
3309  }
3310 
3311  /*
3312  * We count only tuples not suppressed by a BEFORE INSERT trigger
3313  * or FDW; this is the same definition used by nodeModifyTable.c
3314  * for counting tuples inserted by an INSERT command.
3315  */
3316  processed++;
3317  }
3318  }
3319 
3320  /* Flush any remaining buffered tuples */
3321  if (insertMethod != CIM_SINGLE)
3322  {
3323  if (!CopyMultiInsertInfoIsEmpty(&multiInsertInfo))
3324  CopyMultiInsertInfoFlush(&multiInsertInfo, NULL);
3325  }
3326 
3327  /* Done, clean up */
3328  error_context_stack = errcallback.previous;
3329 
3330  if (bistate != NULL)
3331  FreeBulkInsertState(bistate);
3332 
3333  MemoryContextSwitchTo(oldcontext);
3334 
3335  /*
3336  * In the old protocol, tell pqcomm that we can process normal protocol
3337  * messages again.
3338  */
3339  if (cstate->copy_dest == COPY_OLD_FE)
3340  pq_endmsgread();
3341 
3342  /* Execute AFTER STATEMENT insertion triggers */
3343  ExecASInsertTriggers(estate, target_resultRelInfo, cstate->transition_capture);
3344 
3345  /* Handle queued AFTER triggers */
3346  AfterTriggerEndQuery(estate);
3347 
3348  ExecResetTupleTable(estate->es_tupleTable, false);
3349 
3350  /* Allow the FDW to shut down */
3351  if (target_resultRelInfo->ri_FdwRoutine != NULL &&
3352  target_resultRelInfo->ri_FdwRoutine->EndForeignInsert != NULL)
3353  target_resultRelInfo->ri_FdwRoutine->EndForeignInsert(estate,
3354  target_resultRelInfo);
3355 
3356  /* Tear down the multi-insert buffer data */
3357  if (insertMethod != CIM_SINGLE)
3358  CopyMultiInsertInfoCleanup(&multiInsertInfo);
3359 
3360  ExecCloseIndices(target_resultRelInfo);
3361 
3362  /* Close all the partitioned tables, leaf partitions, and their indices */
3363  if (proute)
3364  ExecCleanupTupleRouting(mtstate, proute);
3365 
3366  /* Close any trigger target relations */
3367  ExecCleanUpTriggerState(estate);
3368 
3369  FreeExecutorState(estate);
3370 
3371  return processed;
3372 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:77
int ri_NumIndices
Definition: execnodes.h:413
Node * whereClause
Definition: copy.c:155
static TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: tuptable.h:475
#define NIL
Definition: pg_list.h:65
Oid tts_tableOid
Definition: tuptable.h:131
uint32 CommandId
Definition: c.h:528
void ExecInitRangeTable(EState *estate, List *rangeTable)
Definition: execUtils.c:724
void InitResultRelInfo(ResultRelInfo *resultRelInfo, Relation resultRelationDesc, Index resultRelationIndex, Relation partition_root, int instrument_options)
Definition: execMain.c:1277
Relation ri_RelationDesc
Definition: execnodes.h:410
List * range_table
Definition: copy.c:183
static void CopyMultiInsertInfoStore(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri, TupleTableSlot *slot, int tuplen, uint64 lineno)
Definition: copy.c:2633
int errhint(const char *fmt,...)
Definition: elog.c:1069
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2592
struct CopyMultiInsertBuffer * ri_CopyMultiInsertBuffer
Definition: execnodes.h:487
void CopyFromErrorCallback(void *arg)
Definition: copy.c:2237
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define ResetPerTupleExprContext(estate)
Definition: executor.h:510
#define TABLE_INSERT_FROZEN
Definition: tableam.h:132
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
BeginForeignInsert_function BeginForeignInsert
Definition: fdwapi.h:215
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1168
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:211
struct PartitionRoutingInfo * ri_PartitionInfo
Definition: execnodes.h:484
#define XLogIsNeeded()
Definition: xlog.h:181
static void CopyMultiInsertInfoSetupBuffer(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri)
Definition: copy.c:2364
void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:1896
StringInfoData line_buf
Definition: copy.c:210
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1689
int errcode(int sqlerrcode)
Definition: elog.c:608
CmdType operation
Definition: execnodes.h:1160
SubTransactionId rd_newRelfilenodeSubid
Definition: rel.h:81
Datum * tts_values
Definition: tuptable.h:126
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:723
EState * state
Definition: execnodes.h:941
Form_pg_class rd_rel
Definition: rel.h:84
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:365
bool volatile_defexprs
Definition: copy.c:182
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
static void CopyMultiInsertInfoInit(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri, CopyState cstate, EState *estate, CommandId mycid, int ti_options)
Definition: copy.c:2384
static void table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid, int options, struct BulkInsertStateData *bistate)
Definition: tableam.h:1141
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:207
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:151
void ExecComputeStoredGenerated(EState *estate, TupleTableSlot *slot)
CopyDest copy_dest
Definition: copy.c:116
ErrorContextCallback * error_context_stack
Definition: elog.c:91
uint64 cur_lineno
Definition: copy.c:159
bool trig_insert_instead_row
Definition: reltrigger.h:57
void FreeExecutorState(EState *estate)
Definition: execUtils.c:190
Relation rel
Definition: copy.c:129
#define GetPerTupleExprContext(estate)
Definition: executor.h:501
BulkInsertState GetBulkInsertState(void)
Definition: heapam.c:1815
bool trig_insert_new_table
Definition: reltrigger.h:74
bool has_generated_stored
Definition: tupdesc.h:45
TupleConversionMap * pi_RootToPartitionMap
Definition: execPartition.h:37
bool ThereAreNoReadyPortals(void)
Definition: portalmem.c:1207
#define ERROR
Definition: elog.h:43
PlanState ps
Definition: execnodes.h:1159
void ExecCleanupTupleRouting(ModifyTableState *mtstate, PartitionTupleRouting *proute)
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2525
static bool CopyMultiInsertInfoIsFull(CopyMultiInsertInfo *miinfo)
Definition: copy.c:2409
TupleConversionMap * tcs_map
Definition: trigger.h:73
static bool CopyMultiInsertInfoIsEmpty(CopyMultiInsertInfo *miinfo)
Definition: copy.c:2421
List * ExecInsertIndexTuples(TupleTableSlot *slot, EState *estate, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:273
TriggerDesc * trigdesc
Definition: rel.h:90
void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
Definition: execMain.c:1076
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2457
struct TransitionCaptureState * mt_transition_capture
Definition: execnodes.h:1185
bool * tts_isnull
Definition: tuptable.h:128
static void CopyMultiInsertInfoFlush(CopyMultiInsertInfo *miinfo, ResultRelInfo *curr_rri)
Definition: copy.c:2552
TupleConstr * constr
Definition: tupdesc.h:85
ResultRelInfo * es_result_relations
Definition: execnodes.h:519
#define RelationGetRelationName(relation)
Definition: rel.h:462
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:439
#define TABLE_INSERT_SKIP_WAL
Definition: tableam.h:130
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
PartitionTupleRouting * ExecSetupPartitionTupleRouting(EState *estate, ModifyTableState *mtstate, Relation rel)
#define ereport(elevel, rest)
Definition: elog.h:141
AttrMap * attrMap
Definition: tupconvert.h:27
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:512
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:422
EState * CreateExecutorState(void)
Definition: execUtils.c:88
SubTransactionId rd_createSubid
Definition: rel.h:80
Definition: copy.c:93
bool trig_insert_before_row
Definition: reltrigger.h:55
List * es_tupleTable
Definition: execnodes.h:551
void ExecResetTupleTable(List *tupleTable, bool shouldFree)
Definition: execTuples.c:1161
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2514
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4663
int es_num_result_relations
Definition: execnodes.h:520
#define TABLE_INSERT_SKIP_FSM
Definition: tableam.h:131
TupleConversionMap * pi_PartitionToRootMap
Definition: execPartition.h:43
List * ri_PartitionCheck
Definition: execnodes.h:475
TupleDesc rd_att
Definition: rel.h:85
static void ExecMaterializeSlot(TupleTableSlot *slot)
Definition: tuptable.h:443
bool freeze
Definition: copy.c:136
Plan * plan
Definition: execnodes.h:939
void pq_endmsgread(void)
Definition: pqcomm.c:1235
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4785
#define makeNode(_type_)
Definition: nodes.h:573
TupleTableSlot * execute_attr_map_slot(AttrMap *attrMap, TupleTableSlot *in_slot, TupleTableSlot *out_slot)
Definition: tupconvert.c:177
#define Assert(condition)
Definition: c.h:739
void FreeBulkInsertState(BulkInsertState bistate)
Definition: heapam.c:1829
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:707
bool ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2607
CopyInsertMethod
Definition: copy.c:90
bool NextCopyFrom(CopyState cstate, ExprContext *econtext, Datum *values, bool *nulls)
Definition: copy.c:3686
static TupleTableSlot * CopyMultiInsertInfoNextFreeSlot(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri)
Definition: copy.c:2614
static void CopyMultiInsertInfoCleanup(CopyMultiInsertInfo *miinfo)
Definition: copy.c:2598
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:223
#define InvalidSubTransactionId
Definition: c.h:520
void ReleaseBulkInsertStatePin(BulkInsertState bistate)
Definition: heapam.c:1841
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:506
void ExecCleanUpTriggerState(EState *estate)
Definition: execMain.c:1454
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4805
int errmsg(const char *fmt,...)
Definition: elog.c:822
void list_free(List *list)
Definition: list.c:1377
bool ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool emitError)
Definition: execMain.c:1783
TransitionCaptureState * transition_capture
Definition: copy.c:186
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:745
ResultRelInfo * ExecFindPartition(ModifyTableState *mtstate, ResultRelInfo *rootResultRelInfo, PartitionTupleRouting *proute, TupleTableSlot *slot, EState *estate)
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:428
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:226
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522
EndForeignInsert_function EndForeignInsert
Definition: fdwapi.h:216
TupleTableSlot * pi_PartitionTupleSlot
Definition: execPartition.h:49
TupleTableSlot * tcs_original_insert_tuple
Definition: trigger.h:82
ExprState * qualexpr
Definition: copy.c:184
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:521

◆ CopyFromErrorCallback()

void CopyFromErrorCallback ( void *  arg)

Definition at line 2237 of file copy.c.

References CopyStateData::binary, CopyStateData::cur_attname, CopyStateData::cur_attval, CopyStateData::cur_lineno, CopyStateData::cur_relname, StringInfoData::data, errcontext, limit_printout_length(), CopyStateData::line_buf, CopyStateData::line_buf_converted, CopyStateData::line_buf_valid, CopyStateData::need_transcoding, pfree(), snprintf, and UINT64_FORMAT.

Referenced by CopyFrom(), file_acquire_sample_rows(), and fileIterateForeignScan().

2238 {
2239  CopyState cstate = (CopyState) arg;
2240  char curlineno_str[32];
2241 
2242  snprintf(curlineno_str, sizeof(curlineno_str), UINT64_FORMAT,
2243  cstate->cur_lineno);
2244 
2245  if (cstate->binary)
2246  {
2247  /* can't usefully display the data */
2248  if (cstate->cur_attname)
2249  errcontext("COPY %s, line %s, column %s",
2250  cstate->cur_relname, curlineno_str,
2251  cstate->cur_attname);
2252  else
2253  errcontext("COPY %s, line %s",
2254  cstate->cur_relname, curlineno_str);
2255  }
2256  else
2257  {
2258  if (cstate->cur_attname && cstate->cur_attval)
2259  {
2260  /* error is relevant to a particular column */
2261  char *attval;
2262 
2263  attval = limit_printout_length(cstate->cur_attval);
2264  errcontext("COPY %s, line %s, column %s: \"%s\"",
2265  cstate->cur_relname, curlineno_str,
2266  cstate->cur_attname, attval);
2267  pfree(attval);
2268  }
2269  else if (cstate->cur_attname)
2270  {
2271  /* error is relevant to a particular column, value is NULL */
2272  errcontext("COPY %s, line %s, column %s: null input",
2273  cstate->cur_relname, curlineno_str,
2274  cstate->cur_attname);
2275  }
2276  else
2277  {
2278  /*
2279  * Error is relevant to a particular line.
2280  *
2281  * If line_buf still contains the correct line, and it's already
2282  * transcoded, print it. If it's still in a foreign encoding, it's
2283  * quite likely that the error is precisely a failure to do
2284  * encoding conversion (ie, bad data). We dare not try to convert
2285  * it, and at present there's no way to regurgitate it without
2286  * conversion. So we have to punt and just report the line number.
2287  */
2288  if (cstate->line_buf_valid &&
2289  (cstate->line_buf_converted || !cstate->need_transcoding))
2290  {
2291  char *lineval;
2292 
2293  lineval = limit_printout_length(cstate->line_buf.data);
2294  errcontext("COPY %s, line %s: \"%s\"",
2295  cstate->cur_relname, curlineno_str, lineval);
2296  pfree(lineval);
2297  }
2298  else
2299  {
2300  errcontext("COPY %s, line %s",
2301  cstate->cur_relname, curlineno_str);
2302  }
2303  }
2304  }
2305 }
bool binary
Definition: copy.c:135
bool need_transcoding
Definition: copy.c:125
StringInfoData line_buf
Definition: copy.c:210
bool line_buf_valid
Definition: copy.c:212
bool line_buf_converted
Definition: copy.c:211
uint64 cur_lineno
Definition: copy.c:159
const char * cur_attname
Definition: copy.c:160
void pfree(void *pointer)
Definition: mcxt.c:1056
static char * limit_printout_length(const char *str)
Definition: copy.c:2317
const char * cur_relname
Definition: copy.c:158
#define errcontext
Definition: elog.h:183
struct CopyStateData * CopyState
Definition: copy.h:23
void * arg
const char * cur_attval
Definition: copy.c:161
#define snprintf
Definition: port.h:192
#define UINT64_FORMAT
Definition: c.h:402

◆ CopyGetAttnums()

static List * CopyGetAttnums ( TupleDesc  tupDesc,
Relation  rel,
List attnamelist 
)
static

Definition at line 5076 of file copy.c.

References attnum, ereport, errcode(), errdetail(), errmsg(), ERROR, i, InvalidAttrNumber, lappend_int(), lfirst, list_member_int(), name, namestrcmp(), TupleDescData::natts, NIL, RelationGetRelationName, strVal, and TupleDescAttr.

Referenced by BeginCopy(), and DoCopy().

5077 {
5078  List *attnums = NIL;
5079 
5080  if (attnamelist == NIL)
5081  {
5082  /* Generate default column list */
5083  int attr_count = tupDesc->natts;
5084  int i;
5085 
5086  for (i = 0; i < attr_count; i++)
5087  {
5088  if (TupleDescAttr(tupDesc, i)->attisdropped)
5089  continue;
5090  if (TupleDescAttr(tupDesc, i)->attgenerated)
5091  continue;
5092  attnums = lappend_int(attnums, i + 1);
5093  }
5094  }
5095  else
5096  {
5097  /* Validate the user-supplied list and extract attnums */
5098  ListCell *l;
5099 
5100  foreach(l, attnamelist)
5101  {
5102  char *name = strVal(lfirst(l));
5103  int attnum;
5104  int i;
5105 
5106  /* Lookup column name */
5107  attnum = InvalidAttrNumber;
5108  for (i = 0; i < tupDesc->natts; i++)
5109  {
5110  Form_pg_attribute att = TupleDescAttr(tupDesc, i);
5111 
5112  if (att->attisdropped)
5113  continue;
5114  if (namestrcmp(&(att->attname), name) == 0)
5115  {
5116  if (att->attgenerated)
5117  ereport(ERROR,
5118  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
5119  errmsg("column \"%s\" is a generated column",
5120  name),
5121  errdetail("Generated columns cannot be used in COPY.")));
5122  attnum = att->attnum;
5123  break;
5124  }
5125  }
5126  if (attnum == InvalidAttrNumber)
5127  {
5128  if (rel != NULL)
5129  ereport(ERROR,
5130  (errcode(ERRCODE_UNDEFINED_COLUMN),
5131  errmsg("column \"%s\" of relation \"%s\" does not exist",
5132  name, RelationGetRelationName(rel))));
5133  else
5134  ereport(ERROR,
5135  (errcode(ERRCODE_UNDEFINED_COLUMN),
5136  errmsg("column \"%s\" does not exist",
5137  name)));
5138  }
5139  /* Check for duplicates */
5140  if (list_member_int(attnums, attnum))
5141  ereport(ERROR,
5142  (errcode(ERRCODE_DUPLICATE_COLUMN),
5143  errmsg("column \"%s\" specified more than once",
5144  name)));
5145  attnums = lappend_int(attnums, attnum);
5146  }
5147  }
5148 
5149  return attnums;
5150 }
#define NIL
Definition: pg_list.h:65
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:608
int namestrcmp(Name name, const char *str)
Definition: name.c:287
#define ERROR
Definition: elog.h:43
bool list_member_int(const List *list, int datum)
Definition: list.c:655
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define RelationGetRelationName(relation)
Definition: rel.h:462
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend_int(List *list, int datum)
Definition: list.c:340
int16 attnum
Definition: pg_attribute.h:79
#define lfirst(lc)
Definition: pg_list.h:190
const char * name
Definition: encode.c:521
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
Definition: pg_list.h:50

◆ CopyGetData()

static int CopyGetData ( CopyState  cstate,
void *  databuf,
int  minread,
int  maxread 
)
static

Definition at line 610 of file copy.c.

References COPY_CALLBACK, CopyStateData::copy_dest, COPY_FILE, CopyStateData::copy_file, COPY_NEW_FE, COPY_OLD_FE, StringInfoData::cursor, CopyStateData::data_source_cb, ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, CopyStateData::fe_msgbuf, HOLD_CANCEL_INTERRUPTS, StringInfoData::len, pq_copymsgbytes(), pq_getbyte(), pq_getbytes(), pq_getmessage(), pq_getmsgstring(), pq_startmsgread(), CopyStateData::reached_eof, and RESUME_CANCEL_INTERRUPTS.

Referenced by BeginCopyFrom(), CopyGetInt16(), CopyGetInt32(), CopyLoadRawBuf(), CopyReadBinaryAttribute(), and NextCopyFrom().

611 {
612  int bytesread = 0;
613 
614  switch (cstate->copy_dest)
615  {
616  case COPY_FILE:
617  bytesread = fread(databuf, 1, maxread, cstate->copy_file);
618  if (ferror(cstate->copy_file))
619  ereport(ERROR,
621  errmsg("could not read from COPY file: %m")));
622  if (bytesread == 0)
623  cstate->reached_eof = true;
624  break;
625  case COPY_OLD_FE:
626 
627  /*
628  * We cannot read more than minread bytes (which in practice is 1)
629  * because old protocol doesn't have any clear way of separating
630  * the COPY stream from following data. This is slow, but not any
631  * slower than the code path was originally, and we don't care
632  * much anymore about the performance of old protocol.
633  */
634  if (pq_getbytes((char *) databuf, minread))
635  {
636  /* Only a \. terminator is legal EOF in old protocol */
637  ereport(ERROR,
638  (errcode(ERRCODE_CONNECTION_FAILURE),
639  errmsg("unexpected EOF on client connection with an open transaction")));
640  }
641  bytesread = minread;
642  break;
643  case COPY_NEW_FE:
644  while (maxread > 0 && bytesread < minread && !cstate->reached_eof)
645  {
646  int avail;
647 
648  while (cstate->fe_msgbuf->cursor >= cstate->fe_msgbuf->len)
649  {
650  /* Try to receive another message */
651  int mtype;
652 
653  readmessage:
655  pq_startmsgread();
656  mtype = pq_getbyte();
657  if (mtype == EOF)
658  ereport(ERROR,
659  (errcode(ERRCODE_CONNECTION_FAILURE),
660  errmsg("unexpected EOF on client connection with an open transaction")));
661  if (pq_getmessage(cstate->fe_msgbuf, 0))
662  ereport(ERROR,
663  (errcode(ERRCODE_CONNECTION_FAILURE),
664  errmsg("unexpected EOF on client connection with an open transaction")));
666  switch (mtype)
667  {
668  case 'd': /* CopyData */
669  break;
670  case 'c': /* CopyDone */
671  /* COPY IN correctly terminated by frontend */
672  cstate->reached_eof = true;
673  return bytesread;
674  case 'f': /* CopyFail */
675  ereport(ERROR,
676  (errcode(ERRCODE_QUERY_CANCELED),
677  errmsg("COPY from stdin failed: %s",
678  pq_getmsgstring(cstate->fe_msgbuf))));
679  break;
680  case 'H': /* Flush */
681  case 'S': /* Sync */
682 
683  /*
684  * Ignore Flush/Sync for the convenience of client
685  * libraries (such as libpq) that may send those
686  * without noticing that the command they just
687  * sent was COPY.
688  */
689  goto readmessage;
690  default:
691  ereport(ERROR,
692  (errcode(ERRCODE_PROTOCOL_VIOLATION),
693  errmsg("unexpected message type 0x%02X during COPY from stdin",
694  mtype)));
695  break;
696  }
697  }
698  avail = cstate->fe_msgbuf->len - cstate->fe_msgbuf->cursor;
699  if (avail > maxread)
700  avail = maxread;
701  pq_copymsgbytes(cstate->fe_msgbuf, databuf, avail);
702  databuf = (void *) ((char *) databuf + avail);
703  maxread -= avail;
704  bytesread += avail;
705  }
706  break;
707  case COPY_CALLBACK:
708  bytesread = cstate->data_source_cb(databuf, minread, maxread);
709  break;
710  }
711 
712  return bytesread;
713 }
#define HOLD_CANCEL_INTERRUPTS()
Definition: miscadmin.h:124
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
int errcode(int sqlerrcode)
Definition: elog.c:608
CopyDest copy_dest
Definition: copy.c:116
Definition: copy.c:70
copy_data_source_cb data_source_cb
Definition: copy.c:134
#define ERROR
Definition: elog.h:43
void pq_startmsgread(void)
Definition: pqcomm.c:1211
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1095
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
int pq_getmessage(StringInfo s, int maxlen)
Definition: pqcomm.c:1273
int pq_getbyte(void)
Definition: pqcomm.c:1001
StringInfo fe_msgbuf
Definition: copy.c:118
bool reached_eof
Definition: copy.c:121
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
int errmsg(const char *fmt,...)
Definition: elog.c:822
FILE * copy_file
Definition: copy.c:117
#define RESUME_CANCEL_INTERRUPTS()
Definition: miscadmin.h:126

◆ CopyGetInt16()

static bool CopyGetInt16 ( CopyState  cstate,
int16 val 
)
static

Definition at line 767 of file copy.c.

References buf, CopyGetData(), and pg_ntoh16.

Referenced by NextCopyFrom().

768 {
769  uint16 buf;
770 
771  if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
772  {
773  *val = 0; /* suppress compiler warning */
774  return false;
775  }
776  *val = (int16) pg_ntoh16(buf);
777  return true;
778 }
signed short int16
Definition: c.h:346
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
unsigned short uint16
Definition: c.h:358
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:610
static char * buf
Definition: pg_test_fsync.c:67
long val
Definition: informix.c:664

◆ CopyGetInt32()

static bool CopyGetInt32 ( CopyState  cstate,
int32 val 
)
static

Definition at line 738 of file copy.c.

References buf, CopyGetData(), and pg_ntoh32.

Referenced by BeginCopyFrom(), and CopyReadBinaryAttribute().

739 {
740  uint32 buf;
741 
742  if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
743  {
744  *val = 0; /* suppress compiler warning */
745  return false;
746  }
747  *val = (int32) pg_ntoh32(buf);
748  return true;
749 }
signed int int32
Definition: c.h:347
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:610
static char * buf
Definition: pg_test_fsync.c:67
unsigned int uint32
Definition: c.h:359
long val
Definition: informix.c:664

◆ CopyLoadRawBuf()

static bool CopyLoadRawBuf ( CopyState  cstate)
static

Definition at line 792 of file copy.c.

References CopyGetData(), memmove, CopyStateData::raw_buf, CopyStateData::raw_buf_index, CopyStateData::raw_buf_len, and RAW_BUF_SIZE.

Referenced by CopyReadLine(), and CopyReadLineText().

793 {
794  int nbytes;
795  int inbytes;
796 
797  if (cstate->raw_buf_index < cstate->raw_buf_len)
798  {
799  /* Copy down the unprocessed data */
800  nbytes = cstate->raw_buf_len - cstate->raw_buf_index;
801  memmove(cstate->raw_buf, cstate->raw_buf + cstate->raw_buf_index,
802  nbytes);
803  }
804  else
805  nbytes = 0; /* no data need be saved */
806 
807  inbytes = CopyGetData(cstate, cstate->raw_buf + nbytes,
808  1, RAW_BUF_SIZE - nbytes);
809  nbytes += inbytes;
810  cstate->raw_buf[nbytes] = '\0';
811  cstate->raw_buf_index = 0;
812  cstate->raw_buf_len = nbytes;
813  return (inbytes > 0);
814 }
int raw_buf_index
Definition: copy.c:223
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:610
char * raw_buf
Definition: copy.c:222
#define memmove(d, s, c)
Definition: c.h:1266
int raw_buf_len
Definition: copy.c:224
#define RAW_BUF_SIZE
Definition: copy.c:221

◆ CopyMultiInsertBufferCleanup()

static void CopyMultiInsertBufferCleanup ( CopyMultiInsertInfo miinfo,
CopyMultiInsertBuffer buffer 
)
inlinestatic

Definition at line 2518 of file copy.c.

References Assert, CopyMultiInsertBuffer::bistate, ExecDropSingleTupleTableSlot(), FreeBulkInsertState(), i, MAX_BUFFERED_TUPLES, CopyMultiInsertBuffer::nused, pfree(), CopyMultiInsertBuffer::resultRelInfo, ResultRelInfo::ri_CopyMultiInsertBuffer, ResultRelInfo::ri_RelationDesc, CopyMultiInsertBuffer::slots, table_finish_bulk_insert(), and CopyMultiInsertInfo::ti_options.

Referenced by CopyMultiInsertInfoCleanup(), and CopyMultiInsertInfoFlush().

2520 {
2521  int i;
2522 
2523  /* Ensure buffer was flushed */
2524  Assert(buffer->nused == 0);
2525 
2526  /* Remove back-link to ourself */
2527  buffer->resultRelInfo->ri_CopyMultiInsertBuffer = NULL;
2528 
2529  FreeBulkInsertState(buffer->bistate);
2530 
2531  /* Since we only create slots on demand, just drop the non-null ones. */
2532  for (i = 0; i < MAX_BUFFERED_TUPLES && buffer->slots[i] != NULL; i++)
2533  ExecDropSingleTupleTableSlot(buffer->slots[i]);
2534 
2536  miinfo->ti_options);
2537 
2538  pfree(buffer);
2539 }
Relation ri_RelationDesc
Definition: execnodes.h:410
struct CopyMultiInsertBuffer * ri_CopyMultiInsertBuffer
Definition: execnodes.h:487
#define MAX_BUFFERED_TUPLES
Definition: copy.c:245
static void table_finish_bulk_insert(Relation rel, int options)
Definition: tableam.h:1336
ResultRelInfo * resultRelInfo
Definition: copy.c:260
void pfree(void *pointer)
Definition: mcxt.c:1056
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
BulkInsertState bistate
Definition: copy.c:261
#define Assert(condition)
Definition: c.h:739
void FreeBulkInsertState(BulkInsertState bistate)
Definition: heapam.c:1829
TupleTableSlot * slots[MAX_BUFFERED_TUPLES]
Definition: copy.c:259
int i

◆ CopyMultiInsertBufferFlush()

static void CopyMultiInsertBufferFlush ( CopyMultiInsertInfo miinfo,
CopyMultiInsertBuffer buffer 
)
inlinestatic

Definition at line 2430 of file copy.c.

References CopyMultiInsertBuffer::bistate, CopyMultiInsertInfo::cstate, CopyStateData::cur_lineno, EState::es_result_relation_info, CopyMultiInsertInfo::estate, ExecARInsertTriggers(), ExecClearTuple(), ExecInsertIndexTuples(), GetPerTupleMemoryContext, i, CopyStateData::line_buf_valid, CopyMultiInsertBuffer::linenos, list_free(), MemoryContextSwitchTo(), CopyMultiInsertInfo::mycid, NIL, CopyMultiInsertBuffer::nused, CopyMultiInsertBuffer::resultRelInfo, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, CopyMultiInsertBuffer::slots, table_multi_insert(), CopyMultiInsertInfo::ti_options, CopyStateData::transition_capture, TriggerDesc::trig_insert_after_row, and TriggerDesc::trig_insert_new_table.

Referenced by CopyMultiInsertInfoFlush().

2432 {
2433  MemoryContext oldcontext;
2434  int i;
2435  uint64 save_cur_lineno;
2436  CopyState cstate = miinfo->cstate;
2437  EState *estate = miinfo->estate;
2438  CommandId mycid = miinfo->mycid;
2439  int ti_options = miinfo->ti_options;
2440  bool line_buf_valid = cstate->line_buf_valid;
2441  int nused = buffer->nused;
2442  ResultRelInfo *resultRelInfo = buffer->resultRelInfo;
2443  TupleTableSlot **slots = buffer->slots;
2444 
2445  /* Set es_result_relation_info to the ResultRelInfo we're flushing. */
2446  estate->es_result_relation_info = resultRelInfo;
2447 
2448  /*
2449  * Print error context information correctly, if one of the operations
2450  * below fail.
2451  */
2452  cstate->line_buf_valid = false;
2453  save_cur_lineno = cstate->cur_lineno;
2454 
2455  /*
2456  * table_multi_insert may leak memory, so switch to short-lived memory
2457  * context before calling it.
2458  */
2459  oldcontext = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2460  table_multi_insert(resultRelInfo->ri_RelationDesc,
2461  slots,
2462  nused,
2463  mycid,
2464  ti_options,
2465  buffer->bistate);
2466  MemoryContextSwitchTo(oldcontext);
2467 
2468  for (i = 0; i < nused; i++)
2469  {
2470  /*
2471  * If there are any indexes, update them for all the inserted tuples,
2472  * and run AFTER ROW INSERT triggers.
2473  */
2474  if (resultRelInfo->ri_NumIndices > 0)
2475  {
2476  List *recheckIndexes;
2477 
2478  cstate->cur_lineno = buffer->linenos[i];
2479  recheckIndexes =
2480  ExecInsertIndexTuples(buffer->slots[i], estate, false, NULL,
2481  NIL);
2482  ExecARInsertTriggers(estate, resultRelInfo,
2483  slots[i], recheckIndexes,
2484  cstate->transition_capture);
2485  list_free(recheckIndexes);
2486  }
2487 
2488  /*
2489  * There's no indexes, but see if we need to run AFTER ROW INSERT
2490  * triggers anyway.
2491  */
2492  else if (resultRelInfo->ri_TrigDesc != NULL &&
2493  (resultRelInfo->ri_TrigDesc->trig_insert_after_row ||
2494  resultRelInfo->ri_TrigDesc->trig_insert_new_table))
2495  {
2496  cstate->cur_lineno = buffer->linenos[i];
2497  ExecARInsertTriggers(estate, resultRelInfo,
2498  slots[i], NIL, cstate->transition_capture);
2499  }
2500 
2501  ExecClearTuple(slots[i]);
2502  }
2503 
2504  /* Mark that all slots are free */
2505  buffer->nused = 0;
2506 
2507  /* reset cur_lineno and line_buf_valid to what they were */
2508  cstate->line_buf_valid = line_buf_valid;
2509  cstate->cur_lineno = save_cur_lineno;
2510 }
int ri_NumIndices
Definition: execnodes.h:413
#define NIL
Definition: pg_list.h:65
uint32 CommandId
Definition: c.h:528
Relation ri_RelationDesc
Definition: execnodes.h:410
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2592
EState * estate
Definition: copy.c:278
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
CommandId mycid
Definition: copy.c:279
uint64 linenos[MAX_BUFFERED_TUPLES]
Definition: copy.c:263
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool line_buf_valid
Definition: copy.c:212
ResultRelInfo * resultRelInfo
Definition: copy.c:260
uint64 cur_lineno
Definition: copy.c:159
bool trig_insert_new_table
Definition: reltrigger.h:74
List * ExecInsertIndexTuples(TupleTableSlot *slot, EState *estate, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:273
bool trig_insert_after_row
Definition: reltrigger.h:56
static void table_multi_insert(Relation rel, TupleTableSlot **slots, int nslots, CommandId cid, int options, struct BulkInsertStateData *bistate)
Definition: tableam.h:1196
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:422
BulkInsertState bistate
Definition: copy.c:261
TupleTableSlot * slots[MAX_BUFFERED_TUPLES]
Definition: copy.c:259
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:506
CopyState cstate
Definition: copy.c:277
void list_free(List *list)
Definition: list.c:1377
int i
TransitionCaptureState * transition_capture
Definition: copy.c:186
Definition: pg_list.h:50
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:521

◆ CopyMultiInsertBufferInit()

static CopyMultiInsertBuffer* CopyMultiInsertBufferInit ( ResultRelInfo rri)
static

Definition at line 2347 of file copy.c.

References CopyMultiInsertBuffer::bistate, GetBulkInsertState(), MAX_BUFFERED_TUPLES, CopyMultiInsertBuffer::nused, palloc(), CopyMultiInsertBuffer::resultRelInfo, and CopyMultiInsertBuffer::slots.

Referenced by CopyMultiInsertInfoSetupBuffer().

2348 {
2349  CopyMultiInsertBuffer *buffer;
2350 
2351  buffer = (CopyMultiInsertBuffer *) palloc(sizeof(CopyMultiInsertBuffer));
2352  memset(buffer->slots, 0, sizeof(TupleTableSlot *) * MAX_BUFFERED_TUPLES);
2353  buffer->resultRelInfo = rri;
2354  buffer->bistate = GetBulkInsertState();
2355  buffer->nused = 0;
2356 
2357  return buffer;
2358 }
#define MAX_BUFFERED_TUPLES
Definition: copy.c:245
ResultRelInfo * resultRelInfo
Definition: copy.c:260
BulkInsertState GetBulkInsertState(void)
Definition: heapam.c:1815
BulkInsertState bistate
Definition: copy.c:261
TupleTableSlot * slots[MAX_BUFFERED_TUPLES]
Definition: copy.c:259
void * palloc(Size size)
Definition: mcxt.c:949

◆ CopyMultiInsertInfoCleanup()

static void CopyMultiInsertInfoCleanup ( CopyMultiInsertInfo miinfo)
inlinestatic

Definition at line 2598 of file copy.c.

References CopyMultiInsertBufferCleanup(), lfirst, list_free(), and CopyMultiInsertInfo::multiInsertBuffers.

Referenced by CopyFrom().

2599 {
2600  ListCell *lc;
2601 
2602  foreach(lc, miinfo->multiInsertBuffers)
2603  CopyMultiInsertBufferCleanup(miinfo, lfirst(lc));
2604 
2605  list_free(miinfo->multiInsertBuffers);
2606 }
#define lfirst(lc)
Definition: pg_list.h:190
static void CopyMultiInsertBufferCleanup(CopyMultiInsertInfo *miinfo, CopyMultiInsertBuffer *buffer)
Definition: copy.c:2518
void list_free(List *list)
Definition: list.c:1377
List * multiInsertBuffers
Definition: copy.c:274

◆ CopyMultiInsertInfoFlush()

static void CopyMultiInsertInfoFlush ( CopyMultiInsertInfo miinfo,
ResultRelInfo curr_rri 
)
inlinestatic

Definition at line 2552 of file copy.c.

References CopyMultiInsertInfo::bufferedBytes, CopyMultiInsertInfo::bufferedTuples, CopyMultiInsertBufferCleanup(), CopyMultiInsertBufferFlush(), lappend(), lfirst, linitial, list_delete_first(), list_length(), MAX_PARTITION_BUFFERS, CopyMultiInsertInfo::multiInsertBuffers, and CopyMultiInsertBuffer::resultRelInfo.

Referenced by CopyFrom().

2553 {
2554  ListCell *lc;
2555 
2556  foreach(lc, miinfo->multiInsertBuffers)
2557  {
2559 
2560  CopyMultiInsertBufferFlush(miinfo, buffer);
2561  }
2562 
2563  miinfo->bufferedTuples = 0;
2564  miinfo->bufferedBytes = 0;
2565 
2566  /*
2567  * Trim the list of tracked buffers down if it exceeds the limit. Here we
2568  * remove buffers starting with the ones we created first. It seems more
2569  * likely that these older ones are less likely to be needed than ones
2570  * that were just created.
2571  */
2573  {
2574  CopyMultiInsertBuffer *buffer;
2575 
2576  buffer = (CopyMultiInsertBuffer *) linitial(miinfo->multiInsertBuffers);
2577 
2578  /*
2579  * We never want to remove the buffer that's currently being used, so
2580  * if we happen to find that then move it to the end of the list.
2581  */
2582  if (buffer->resultRelInfo == curr_rri)
2583  {
2585  miinfo->multiInsertBuffers = lappend(miinfo->multiInsertBuffers, buffer);
2586  buffer = (CopyMultiInsertBuffer *) linitial(miinfo->multiInsertBuffers);
2587  }
2588 
2589  CopyMultiInsertBufferCleanup(miinfo, buffer);
2591  }
2592 }
ResultRelInfo * resultRelInfo
Definition: copy.c:260
#define linitial(l)
Definition: pg_list.h:195
static void CopyMultiInsertBufferFlush(CopyMultiInsertInfo *miinfo, CopyMultiInsertBuffer *buffer)
Definition: copy.c:2430
List * lappend(List *list, void *datum)
Definition: list.c:322
int bufferedTuples
Definition: copy.c:275
#define MAX_PARTITION_BUFFERS
Definition: copy.c:254
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
static void CopyMultiInsertBufferCleanup(CopyMultiInsertInfo *miinfo, CopyMultiInsertBuffer *buffer)
Definition: copy.c:2518
List * multiInsertBuffers
Definition: copy.c:274
List * list_delete_first(List *list)
Definition: list.c:861

◆ CopyMultiInsertInfoInit()

static void CopyMultiInsertInfoInit ( CopyMultiInsertInfo miinfo,
ResultRelInfo rri,
CopyState  cstate,
EState estate,
CommandId  mycid,
int  ti_options 
)
static

Definition at line 2384 of file copy.c.

References CopyMultiInsertInfo::bufferedBytes, CopyMultiInsertInfo::bufferedTuples, CopyMultiInsertInfoSetupBuffer(), CopyMultiInsertInfo::cstate, CopyMultiInsertInfo::estate, CopyMultiInsertInfo::multiInsertBuffers, CopyMultiInsertInfo::mycid, NIL, RelationData::rd_rel, ResultRelInfo::ri_RelationDesc, and CopyMultiInsertInfo::ti_options.

Referenced by CopyFrom().

2387 {
2388  miinfo->multiInsertBuffers = NIL;
2389  miinfo->bufferedTuples = 0;
2390  miinfo->bufferedBytes = 0;
2391  miinfo->cstate = cstate;
2392  miinfo->estate = estate;
2393  miinfo->mycid = mycid;
2394  miinfo->ti_options = ti_options;
2395 
2396  /*
2397  * Only setup the buffer when not dealing with a partitioned table.
2398  * Buffers for partitioned tables will just be setup when we need to send
2399  * tuples their way for the first time.
2400  */
2401  if (rri->ri_RelationDesc->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2402  CopyMultiInsertInfoSetupBuffer(miinfo, rri);
2403 }
#define NIL
Definition: pg_list.h:65
Relation ri_RelationDesc
Definition: execnodes.h:410
EState * estate
Definition: copy.c:278
static void CopyMultiInsertInfoSetupBuffer(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri)
Definition: copy.c:2364
CommandId mycid
Definition: copy.c:279
Form_pg_class rd_rel
Definition: rel.h:84
int bufferedTuples
Definition: copy.c:275
CopyState cstate
Definition: copy.c:277
List * multiInsertBuffers
Definition: copy.c:274

◆ CopyMultiInsertInfoIsEmpty()

static bool CopyMultiInsertInfoIsEmpty ( CopyMultiInsertInfo miinfo)
inlinestatic

Definition at line 2421 of file copy.c.

References CopyMultiInsertInfo::bufferedTuples.

Referenced by CopyFrom().

2422 {
2423  return miinfo->bufferedTuples == 0;
2424 }
int bufferedTuples
Definition: copy.c:275

◆ CopyMultiInsertInfoIsFull()

static bool CopyMultiInsertInfoIsFull ( CopyMultiInsertInfo miinfo)
inlinestatic

Definition at line 2409 of file copy.c.

References CopyMultiInsertInfo::bufferedBytes, CopyMultiInsertInfo::bufferedTuples, MAX_BUFFERED_BYTES, and MAX_BUFFERED_TUPLES.

Referenced by CopyFrom().

2410 {
2411  if (miinfo->bufferedTuples >= MAX_BUFFERED_TUPLES ||
2412  miinfo->bufferedBytes >= MAX_BUFFERED_BYTES)
2413  return true;
2414  return false;
2415 }
#define MAX_BUFFERED_TUPLES
Definition: copy.c:245
#define MAX_BUFFERED_BYTES
Definition: copy.c:251
int bufferedTuples
Definition: copy.c:275

◆ CopyMultiInsertInfoNextFreeSlot()

static TupleTableSlot* CopyMultiInsertInfoNextFreeSlot ( CopyMultiInsertInfo miinfo,
ResultRelInfo rri 
)
inlinestatic

Definition at line 2614 of file copy.c.

References Assert, MAX_BUFFERED_TUPLES, CopyMultiInsertBuffer::nused, ResultRelInfo::ri_CopyMultiInsertBuffer, ResultRelInfo::ri_RelationDesc, CopyMultiInsertBuffer::slots, and table_slot_create().

Referenced by CopyFrom().

2616 {
2618  int nused = buffer->nused;
2619 
2620  Assert(buffer != NULL);
2621  Assert(nused < MAX_BUFFERED_TUPLES);
2622 
2623  if (buffer->slots[nused] == NULL)
2624  buffer->slots[nused] = table_slot_create(rri->ri_RelationDesc, NULL);
2625  return buffer->slots[nused];
2626 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:77
Relation ri_RelationDesc
Definition: execnodes.h:410
struct CopyMultiInsertBuffer * ri_CopyMultiInsertBuffer
Definition: execnodes.h:487
#define MAX_BUFFERED_TUPLES
Definition: copy.c:245
#define Assert(condition)
Definition: c.h:739
TupleTableSlot * slots[MAX_BUFFERED_TUPLES]
Definition: copy.c:259

◆ CopyMultiInsertInfoSetupBuffer()

static void CopyMultiInsertInfoSetupBuffer ( CopyMultiInsertInfo miinfo,
ResultRelInfo rri 
)
inlinestatic

Definition at line 2364 of file copy.c.

References CopyMultiInsertBufferInit(), lappend(), CopyMultiInsertInfo::multiInsertBuffers, and ResultRelInfo::ri_CopyMultiInsertBuffer.

Referenced by CopyFrom(), and CopyMultiInsertInfoInit().

2366 {
2367  CopyMultiInsertBuffer *buffer;
2368 
2369  buffer = CopyMultiInsertBufferInit(rri);
2370 
2371  /* Setup back-link so we can easily find this buffer again */
2372  rri->ri_CopyMultiInsertBuffer = buffer;
2373  /* Record that we're tracking this buffer */
2374  miinfo->multiInsertBuffers = lappend(miinfo->multiInsertBuffers, buffer);
2375 }
struct CopyMultiInsertBuffer * ri_CopyMultiInsertBuffer
Definition: execnodes.h:487
List * lappend(List *list, void *datum)
Definition: list.c:322
static CopyMultiInsertBuffer * CopyMultiInsertBufferInit(ResultRelInfo *rri)
Definition: copy.c:2347
List * multiInsertBuffers
Definition: copy.c:274

◆ CopyMultiInsertInfoStore()

static void CopyMultiInsertInfoStore ( CopyMultiInsertInfo miinfo,
ResultRelInfo rri,
TupleTableSlot slot,
int  tuplen,
uint64  lineno 
)
inlinestatic

Definition at line 2633 of file copy.c.

References Assert, CopyMultiInsertInfo::bufferedBytes, CopyMultiInsertInfo::bufferedTuples, CopyMultiInsertBuffer::linenos, CopyMultiInsertBuffer::nused, ResultRelInfo::ri_CopyMultiInsertBuffer, and CopyMultiInsertBuffer::slots.

Referenced by CopyFrom().

2635 {
2637 
2638  Assert(buffer != NULL);
2639  Assert(slot == buffer->slots[buffer->nused]);
2640 
2641  /* Store the line number so we can properly report any errors later */
2642  buffer->linenos[buffer->nused] = lineno;
2643 
2644  /* Record this slot as being used */
2645  buffer->nused++;
2646 
2647  /* Update how many tuples are stored and their size */
2648  miinfo->bufferedTuples++;
2649  miinfo->bufferedBytes += tuplen;
2650 }
struct CopyMultiInsertBuffer * ri_CopyMultiInsertBuffer
Definition: execnodes.h:487
uint64 linenos[MAX_BUFFERED_TUPLES]
Definition: copy.c:263
int bufferedTuples
Definition: copy.c:275
#define Assert(condition)
Definition: c.h:739
TupleTableSlot * slots[MAX_BUFFERED_TUPLES]
Definition: copy.c:259

◆ CopyOneRowTo()

static void CopyOneRowTo ( CopyState  cstate,
TupleTableSlot slot 
)
static

Definition at line 2159 of file copy.c.

References attnum, CopyStateData::attnumlist, CopyStateData::binary, CopyAttributeOutCSV(), CopyAttributeOutText(), CopySendChar(), CopySendData(), CopySendEndOfRow(), CopySendInt16(), CopySendInt32(), CopySendString(), CopyStateData::csv_mode, cur, CopyStateData::delim, CopyStateData::force_quote_flags, lfirst_int, list_length(), MemoryContextReset(), MemoryContextSwitchTo(), CopyStateData::null_print_client, CopyStateData::out_functions, OutputFunctionCall(), CopyStateData::rowcontext, SendFunctionCall(), slot_getallattrs(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, value, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by copy_dest_receive(), and CopyTo().

2160 {
2161  bool need_delim = false;
2162  FmgrInfo *out_functions = cstate->out_functions;
2163  MemoryContext oldcontext;
2164  ListCell *cur;
2165  char *string;
2166 
2167  MemoryContextReset(cstate->rowcontext);
2168  oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
2169 
2170  if (cstate->binary)
2171  {
2172  /* Binary per-tuple header */
2173  CopySendInt16(cstate, list_length(cstate->attnumlist));
2174  }
2175 
2176  /* Make sure the tuple is fully deconstructed */
2177  slot_getallattrs(slot);
2178 
2179  foreach(cur, cstate->attnumlist)
2180  {
2181  int attnum = lfirst_int(cur);
2182  Datum value = slot->tts_values[attnum - 1];
2183  bool isnull = slot->tts_isnull[attnum - 1];
2184 
2185  if (!cstate->binary)
2186  {
2187  if (need_delim)
2188  CopySendChar(cstate, cstate->delim[0]);
2189  need_delim = true;
2190  }
2191 
2192  if (isnull)
2193  {
2194  if (!cstate->binary)
2195  CopySendString(cstate, cstate->null_print_client);
2196  else
2197  CopySendInt32(cstate, -1);
2198  }
2199  else
2200  {
2201  if (!cstate->binary)
2202  {
2203  string = OutputFunctionCall(&out_functions[attnum - 1],
2204  value);
2205  if (cstate->csv_mode)
2206  CopyAttributeOutCSV(cstate, string,
2207  cstate->force_quote_flags[attnum - 1],
2208  list_length(cstate->attnumlist) == 1);
2209  else
2210  CopyAttributeOutText(cstate, string);
2211  }
2212  else
2213  {
2214  bytea *outputbytes;
2215 
2216  outputbytes = SendFunctionCall(&out_functions[attnum - 1],
2217  value);
2218  CopySendInt32(cstate, VARSIZE(outputbytes) - VARHDRSZ);
2219  CopySendData(cstate, VARDATA(outputbytes),
2220  VARSIZE(outputbytes) - VARHDRSZ);
2221  }
2222  }
2223  }
2224 
2225  CopySendEndOfRow(cstate);
2226 
2227  MemoryContextSwitchTo(oldcontext);
2228 }
Definition: fmgr.h:56
bool csv_mode
Definition: copy.c:137
#define VARDATA(PTR)
Definition: postgres.h:302
bool binary
Definition: copy.c:135
List * attnumlist
Definition: copy.c:131
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:562
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static struct @145 value
struct cursor * cur
Definition: ecpg.c:28
static void CopyAttributeOutText(CopyState cstate, char *string)
Definition: copy.c:4827
Datum * tts_values
Definition: tuptable.h:126
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
bool * force_quote_flags
Definition: copy.c:147
char * delim
Definition: copy.c:142
MemoryContext rowcontext
Definition: copy.c:172
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:511
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1575
static void CopySendInt16(CopyState cstate, int16 val)
Definition: copy.c:755
#define lfirst_int(lc)
Definition: pg_list.h:191
static void CopyAttributeOutCSV(CopyState cstate, char *string, bool use_quote, bool single_attr)
Definition: copy.c:4980
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
bool * tts_isnull
Definition: tuptable.h:128
char string[11]
Definition: preproc-type.c:46
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1636
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
static void CopySendData(CopyState cstate, const void *databuf, int datasize)
Definition: copy.c:499
static void CopySendString(CopyState cstate, const char *str)
Definition: copy.c:505
static int list_length(const List *l)
Definition: pg_list.h:169
static void CopySendInt32(CopyState cstate, int32 val)
Definition: copy.c:724
FmgrInfo * out_functions
Definition: copy.c:171
static void CopySendEndOfRow(CopyState cstate)
Definition: copy.c:517
Definition: c.h:556
char * null_print_client
Definition: copy.c:141

◆ CopyReadAttributesCSV()

static int CopyReadAttributesCSV ( CopyState  cstate)
static

Definition at line 4599 of file copy.c.

References Assert, CopyStateData::attribute_buf, StringInfoData::data, CopyStateData::delim, enlargeStringInfo(), ereport, errcode(), errmsg(), ERROR, CopyStateData::escape, StringInfoData::len, CopyStateData::line_buf, CopyStateData::max_fields, StringInfoData::maxlen, CopyStateData::null_print, CopyStateData::null_print_len, CopyStateData::quote, CopyStateData::raw_fields, repalloc(), and resetStringInfo().

Referenced by NextCopyFromRawFields().

4600 {
4601  char delimc = cstate->delim[0];
4602  char quotec = cstate->quote[0];
4603  char escapec = cstate->escape[0];
4604  int fieldno;
4605  char *output_ptr;
4606  char *cur_ptr;
4607  char *line_end_ptr;
4608 
4609  /*
4610  * We need a special case for zero-column tables: check that the input
4611  * line is empty, and return.
4612  */
4613  if (cstate->max_fields <= 0)
4614  {
4615  if (cstate->line_buf.len != 0)
4616  ereport(ERROR,
4617  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4618  errmsg("extra data after last expected column")));
4619  return 0;
4620  }
4621 
4622  resetStringInfo(&cstate->attribute_buf);
4623 
4624  /*
4625  * The de-escaped attributes will certainly not be longer than the input
4626  * data line, so we can just force attribute_buf to be large enough and
4627  * then transfer data without any checks for enough space. We need to do
4628  * it this way because enlarging attribute_buf mid-stream would invalidate
4629  * pointers already stored into cstate->raw_fields[].
4630  */
4631  if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
4632  enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
4633  output_ptr = cstate->attribute_buf.data;
4634 
4635  /* set pointer variables for loop */
4636  cur_ptr = cstate->line_buf.data;
4637  line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
4638 
4639  /* Outer loop iterates over fields */
4640  fieldno = 0;
4641  for (;;)
4642  {
4643  bool found_delim = false;
4644  bool saw_quote = false;
4645  char *start_ptr;
4646  char *end_ptr;
4647  int input_len;
4648 
4649  /* Make sure there is enough space for the next value */
4650  if (fieldno >= cstate->max_fields)
4651  {
4652  cstate->max_fields *= 2;
4653  cstate->raw_fields =
4654  repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
4655  }
4656 
4657  /* Remember start of field on both input and output sides */
4658  start_ptr = cur_ptr;
4659  cstate->raw_fields[fieldno] = output_ptr;
4660 
4661  /*
4662  * Scan data for field,
4663  *
4664  * The loop starts in "not quote" mode and then toggles between that
4665  * and "in quote" mode. The loop exits normally if it is in "not
4666  * quote" mode and a delimiter or line end is seen.
4667  */
4668  for (;;)
4669  {
4670  char c;
4671 
4672  /* Not in quote */
4673  for (;;)
4674  {
4675  end_ptr = cur_ptr;
4676  if (cur_ptr >= line_end_ptr)
4677  goto endfield;
4678  c = *cur_ptr++;
4679  /* unquoted field delimiter */
4680  if (c == delimc)
4681  {
4682  found_delim = true;
4683  goto endfield;
4684  }
4685  /* start of quoted field (or part of field) */
4686  if (c == quotec)
4687  {
4688  saw_quote = true;
4689  break;
4690  }
4691  /* Add c to output string */
4692  *output_ptr++ = c;
4693  }
4694 
4695  /* In quote */
4696  for (;;)
4697  {
4698  end_ptr = cur_ptr;
4699  if (cur_ptr >= line_end_ptr)
4700  ereport(ERROR,
4701  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4702  errmsg("unterminated CSV quoted field")));
4703 
4704  c = *cur_ptr++;
4705 
4706  /* escape within a quoted field */
4707  if (c == escapec)
4708  {
4709  /*
4710  * peek at the next char if available, and escape it if it
4711  * is an escape char or a quote char
4712  */
4713  if (cur_ptr < line_end_ptr)
4714  {
4715  char nextc = *cur_ptr;
4716 
4717  if (nextc == escapec || nextc == quotec)
4718  {
4719  *output_ptr++ = nextc;
4720  cur_ptr++;
4721  continue;
4722  }
4723  }
4724  }
4725 
4726  /*
4727  * end of quoted field. Must do this test after testing for
4728  * escape in case quote char and escape char are the same
4729  * (which is the common case).
4730  */
4731  if (c == quotec)
4732  break;
4733 
4734  /* Add c to output string */
4735  *output_ptr++ = c;
4736  }
4737  }
4738 endfield:
4739 
4740  /* Terminate attribute value in output area */
4741  *output_ptr++ = '\0';
4742 
4743  /* Check whether raw input matched null marker */
4744  input_len = end_ptr - start_ptr;
4745  if (!saw_quote && input_len == cstate->null_print_len &&
4746  strncmp(start_ptr, cstate->null_print, input_len) == 0)
4747  cstate->raw_fields[fieldno] = NULL;
4748 
4749  fieldno++;
4750  /* Done if we hit EOL instead of a delim */
4751  if (!found_delim)
4752  break;
4753  }
4754 
4755  /* Clean up state of attribute_buf */
4756  output_ptr--;
4757  Assert(*output_ptr == '\0');
4758  cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
4759 
4760  return fieldno;
4761 }
char ** raw_fields
Definition: copy.c:201
StringInfoData line_buf
Definition: copy.c:210
int errcode(int sqlerrcode)
Definition: elog.c:608
char * delim
Definition: copy.c:142
char * null_print
Definition: copy.c:139
#define ERROR
Definition: elog.h:43
char * c
char * quote
Definition: copy.c:143
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
int max_fields
Definition: copy.c:200
char * escape
Definition: copy.c:144
#define ereport(elevel, rest)
Definition: elog.h:141
int null_print_len
Definition: copy.c:140
#define Assert(condition)
Definition: c.h:739
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
int errmsg(const char *fmt,...)
Definition: elog.c:822
StringInfoData attribute_buf
Definition: copy.c:196

◆ CopyReadAttributesText()

static int CopyReadAttributesText ( CopyState  cstate)
static

Definition at line 4371 of file copy.c.

References Assert, CopyStateData::attribute_buf, StringInfoData::data, CopyStateData::delim, enlargeStringInfo(), ereport, errcode(), errmsg(), ERROR, GetDecimalFromHex(), IS_HIGHBIT_SET, ISOCTAL, StringInfoData::len, CopyStateData::line_buf, CopyStateData::max_fields, StringInfoData::maxlen, CopyStateData::null_print, CopyStateData::null_print_len, OCTVALUE, pg_verifymbstr(), CopyStateData::raw_fields, repalloc(), resetStringInfo(), and val.

Referenced by NextCopyFromRawFields().

4372 {
4373  char delimc = cstate->delim[0];
4374  int fieldno;
4375  char *output_ptr;
4376  char *cur_ptr;
4377  char *line_end_ptr;
4378 
4379  /*
4380  * We need a special case for zero-column tables: check that the input
4381  * line is empty, and return.
4382  */
4383  if (cstate->max_fields <= 0)
4384  {
4385  if (cstate->line_buf.len != 0)
4386  ereport(ERROR,
4387  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4388  errmsg("extra data after last expected column")));
4389  return 0;
4390  }
4391 
4392  resetStringInfo(&cstate->attribute_buf);
4393 
4394  /*
4395  * The de-escaped attributes will certainly not be longer than the input
4396  * data line, so we can just force attribute_buf to be large enough and
4397  * then transfer data without any checks for enough space. We need to do
4398  * it this way because enlarging attribute_buf mid-stream would invalidate
4399  * pointers already stored into cstate->raw_fields[].
4400  */
4401  if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
4402  enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
4403  output_ptr = cstate->attribute_buf.data;
4404 
4405  /* set pointer variables for loop */
4406  cur_ptr = cstate->line_buf.data;
4407  line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
4408 
4409  /* Outer loop iterates over fields */
4410  fieldno = 0;
4411  for (;;)
4412  {
4413  bool found_delim = false;
4414  char *start_ptr;
4415  char *end_ptr;
4416  int input_len;
4417  bool saw_non_ascii = false;
4418 
4419  /* Make sure there is enough space for the next value */
4420  if (fieldno >= cstate->max_fields)
4421  {
4422  cstate->max_fields *= 2;
4423  cstate->raw_fields =
4424  repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
4425  }
4426 
4427  /* Remember start of field on both input and output sides */
4428  start_ptr = cur_ptr;
4429  cstate->raw_fields[fieldno] = output_ptr;
4430 
4431  /*
4432  * Scan data for field.
4433  *
4434  * Note that in this loop, we are scanning to locate the end of field
4435  * and also speculatively performing de-escaping. Once we find the
4436  * end-of-field, we can match the raw field contents against the null
4437  * marker string. Only after that comparison fails do we know that
4438  * de-escaping is actually the right thing to do; therefore we *must
4439  * not* throw any syntax errors before we've done the null-marker
4440  * check.
4441  */
4442  for (;;)
4443  {
4444  char c;
4445 
4446  end_ptr = cur_ptr;
4447  if (cur_ptr >= line_end_ptr)
4448  break;
4449  c = *cur_ptr++;
4450  if (c == delimc)
4451  {
4452  found_delim = true;
4453  break;
4454  }
4455  if (c == '\\')
4456  {
4457  if (cur_ptr >= line_end_ptr)
4458  break;
4459  c = *cur_ptr++;
4460  switch (c)
4461  {
4462  case '0':
4463  case '1':
4464  case '2':
4465  case '3':
4466  case '4':
4467  case '5':
4468  case '6':
4469  case '7':
4470  {
4471  /* handle \013 */
4472  int val;
4473 
4474  val = OCTVALUE(c);
4475  if (cur_ptr < line_end_ptr)
4476  {
4477  c = *cur_ptr;
4478  if (ISOCTAL(c))
4479  {
4480  cur_ptr++;
4481  val = (val << 3) + OCTVALUE(c);
4482  if (cur_ptr < line_end_ptr)
4483  {
4484  c = *cur_ptr;
4485  if (ISOCTAL(c))
4486  {
4487  cur_ptr++;
4488  val = (val << 3) + OCTVALUE(c);
4489  }
4490  }
4491  }
4492  }
4493  c = val & 0377;
4494  if (c == '\0' || IS_HIGHBIT_SET(c))
4495  saw_non_ascii = true;
4496  }
4497  break;
4498  case 'x':
4499  /* Handle \x3F */
4500  if (cur_ptr < line_end_ptr)
4501  {
4502  char hexchar = *cur_ptr;
4503 
4504  if (isxdigit((unsigned char) hexchar))
4505  {
4506  int val = GetDecimalFromHex(hexchar);
4507 
4508  cur_ptr++;
4509  if (cur_ptr < line_end_ptr)
4510  {
4511  hexchar = *cur_ptr;
4512  if (isxdigit((unsigned char) hexchar))
4513  {
4514  cur_ptr++;
4515  val = (val << 4) + GetDecimalFromHex(hexchar);
4516  }
4517  }
4518  c = val & 0xff;
4519  if (c == '\0' || IS_HIGHBIT_SET(c))
4520  saw_non_ascii = true;
4521  }
4522  }
4523  break;
4524  case 'b':
4525  c = '\b';
4526  break;
4527  case 'f':
4528  c = '\f';
4529  break;
4530  case 'n':
4531  c = '\n';
4532  break;
4533  case 'r':
4534  c = '\r';
4535  break;
4536  case 't':
4537  c = '\t';
4538  break;
4539  case 'v':
4540  c = '\v';
4541  break;
4542 
4543  /*
4544  * in all other cases, take the char after '\'
4545  * literally
4546  */
4547  }
4548  }
4549 
4550  /* Add c to output string */
4551  *output_ptr++ = c;
4552  }
4553 
4554  /* Check whether raw input matched null marker */
4555  input_len = end_ptr - start_ptr;
4556  if (input_len == cstate->null_print_len &&
4557  strncmp(start_ptr, cstate->null_print, input_len) == 0)
4558  cstate->raw_fields[fieldno] = NULL;
4559  else
4560  {
4561  /*
4562  * At this point we know the field is supposed to contain data.
4563  *
4564  * If we de-escaped any non-7-bit-ASCII chars, make sure the
4565  * resulting string is valid data for the db encoding.
4566  */
4567  if (saw_non_ascii)
4568  {
4569  char *fld = cstate->raw_fields[fieldno];
4570 
4571  pg_verifymbstr(fld, output_ptr - fld, false);
4572  }
4573  }
4574 
4575  /* Terminate attribute value in output area */
4576  *output_ptr++ = '\0';
4577 
4578  fieldno++;
4579  /* Done if we hit EOL instead of a delim */
4580  if (!found_delim)
4581  break;
4582  }
4583 
4584  /* Clean up state of attribute_buf */
4585  output_ptr--;
4586  Assert(*output_ptr == '\0');
4587  cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
4588 
4589  return fieldno;
4590 }
static int GetDecimalFromHex(char hex)
Definition: copy.c:4343
char ** raw_fields
Definition: copy.c:201
#define ISOCTAL(c)
Definition: copy.c:61
#define OCTVALUE(c)
Definition: copy.c:62
StringInfoData line_buf
Definition: copy.c:210
int errcode(int sqlerrcode)
Definition: elog.c:608
char * delim
Definition: copy.c:142
char * null_print
Definition: copy.c:139
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1106
#define ERROR
Definition: elog.h:43
char * c
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
int max_fields
Definition: copy.c:200
#define ereport(elevel, rest)
Definition: elog.h:141
int null_print_len
Definition: copy.c:140
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1341
#define Assert(condition)
Definition: c.h:739
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
int errmsg(const char *fmt,...)
Definition: elog.c:822
StringInfoData attribute_buf
Definition: copy.c:196
long val
Definition: informix.c:664

◆ CopyReadBinaryAttribute()

static Datum CopyReadBinaryAttribute ( CopyState  cstate,
int  column_no,
FmgrInfo flinfo,
Oid  typioparam,
int32  typmod,
bool isnull 
)
static

Definition at line 4768 of file copy.c.

References CopyStateData::attribute_buf, CopyGetData(), CopyGetInt32(), StringInfoData::cursor, StringInfoData::data, enlargeStringInfo(), ereport, errcode(), errmsg(), ERROR, StringInfoData::len, ReceiveFunctionCall(), and resetStringInfo().

Referenced by NextCopyFrom().

4772 {
4773  int32 fld_size;
4774  Datum result;
4775 
4776  if (!CopyGetInt32(cstate, &fld_size))
4777  ereport(ERROR,
4778  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4779  errmsg("unexpected EOF in COPY data")));
4780  if (fld_size == -1)
4781  {
4782  *isnull = true;
4783  return ReceiveFunctionCall(flinfo, NULL, typioparam, typmod);
4784  }
4785  if (fld_size < 0)
4786  ereport(ERROR,
4787  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4788  errmsg("invalid field size")));
4789 
4790  /* reset attribute_buf to empty, and load raw data in it */
4791  resetStringInfo(&cstate->attribute_buf);
4792 
4793  enlargeStringInfo(&cstate->attribute_buf, fld_size);
4794  if (CopyGetData(cstate, cstate->attribute_buf.data,
4795  fld_size, fld_size) != fld_size)
4796  ereport(ERROR,
4797  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4798  errmsg("unexpected EOF in COPY data")));
4799 
4800  cstate->attribute_buf.len = fld_size;
4801  cstate->attribute_buf.data[fld_size] = '\0';
4802 
4803  /* Call the column type's binary input converter */
4804  result = ReceiveFunctionCall(flinfo, &cstate->attribute_buf,
4805  typioparam, typmod);
4806 
4807  /* Trouble if it didn't eat the whole buffer */
4808  if (cstate->attribute_buf.cursor != cstate->attribute_buf.len)
4809  ereport(ERROR,
4810  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
4811  errmsg("incorrect binary data format")));
4812 
4813  *isnull = false;
4814  return result;
4815 }
static bool CopyGetInt32(CopyState cstate, int32 *val)
Definition: copy.c:738
int errcode(int sqlerrcode)
Definition: elog.c:608
signed int int32
Definition: c.h:347
#define ERROR
Definition: elog.h:43
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:610
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1589
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
#define ereport(elevel, rest)
Definition: elog.h:141
uintptr_t Datum
Definition: postgres.h:367
int errmsg(const char *fmt,...)
Definition: elog.c:822
StringInfoData attribute_buf
Definition: copy.c:196

◆ CopyReadLine()

static bool CopyReadLine ( CopyState  cstate)
static

Definition at line 3890 of file copy.c.

References appendBinaryStringInfo(), Assert, CopyStateData::copy_dest, COPY_NEW_FE, CopyLoadRawBuf(), CopyReadLineText(), StringInfoData::data, EOL_CR, EOL_CRNL, EOL_NL, CopyStateData::eol_type, EOL_UNKNOWN, CopyStateData::file_encoding, StringInfoData::len, CopyStateData::line_buf, CopyStateData::line_buf_converted, CopyStateData::line_buf_valid, CopyStateData::need_transcoding, pfree(), pg_any_to_server(), CopyStateData::raw_buf_index, CopyStateData::raw_buf_len, and resetStringInfo().

Referenced by NextCopyFromRawFields().

3891 {
3892  bool result;
3893 
3894  resetStringInfo(&cstate->line_buf);
3895  cstate->line_buf_valid = true;
3896 
3897  /* Mark that encoding conversion hasn't occurred yet */
3898  cstate->line_buf_converted = false;
3899 
3900  /* Parse data and transfer into line_buf */
3901  result = CopyReadLineText(cstate);
3902 
3903  if (result)
3904  {
3905  /*
3906  * Reached EOF. In protocol version 3, we should ignore anything
3907  * after \. up to the protocol end of copy data. (XXX maybe better
3908  * not to treat \. as special?)
3909  */
3910  if (cstate->copy_dest == COPY_NEW_FE)
3911  {
3912  do
3913  {
3914  cstate->raw_buf_index = cstate->raw_buf_len;
3915  } while (CopyLoadRawBuf(cstate));
3916  }
3917  }
3918  else
3919  {
3920  /*
3921  * If we didn't hit EOF, then we must have transferred the EOL marker
3922  * to line_buf along with the data. Get rid of it.
3923  */
3924  switch (cstate->eol_type)
3925  {
3926  case EOL_NL:
3927  Assert(cstate->line_buf.len >= 1);
3928  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
3929  cstate->line_buf.len--;
3930  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3931  break;
3932  case EOL_CR:
3933  Assert(cstate->line_buf.len >= 1);
3934  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\r');
3935  cstate->line_buf.len--;
3936  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3937  break;
3938  case EOL_CRNL:
3939  Assert(cstate->line_buf.len >= 2);
3940  Assert(cstate->line_buf.data[cstate->line_buf.len - 2] == '\r');
3941  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
3942  cstate->line_buf.len -= 2;
3943  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3944  break;
3945  case EOL_UNKNOWN:
3946  /* shouldn't get here */
3947  Assert(false);
3948  break;
3949  }
3950  }
3951 
3952  /* Done reading the line. Convert it to server encoding. */
3953  if (cstate->need_transcoding)
3954  {
3955  char *cvt;
3956 
3957  cvt = pg_any_to_server(cstate->line_buf.data,
3958  cstate->line_buf.len,
3959  cstate->file_encoding);
3960  if (cvt != cstate->line_buf.data)
3961  {
3962  /* transfer converted data back to line_buf */
3963  resetStringInfo(&cstate->line_buf);
3964  appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt));
3965  pfree(cvt);
3966  }
3967  }
3968 
3969  /* Now it's safe to use the buffer in error messages */
3970  cstate->line_buf_converted = true;
3971 
3972  return result;
3973 }
static bool CopyReadLineText(CopyState cstate)
Definition: copy.c:3979
Definition: copy.c:82
bool need_transcoding
Definition: copy.c:125
StringInfoData line_buf
Definition: copy.c:210
int raw_buf_index
Definition: copy.c:223
bool line_buf_valid
Definition: copy.c:212
bool line_buf_converted
Definition: copy.c:211
CopyDest copy_dest
Definition: copy.c:116
void pfree(void *pointer)
Definition: mcxt.c:1056
static bool CopyLoadRawBuf(CopyState cstate)
Definition: copy.c:792
Definition: copy.c:83
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
int raw_buf_len
Definition: copy.c:224
Definition: copy.c:84
int file_encoding
Definition: copy.c:124
EolType eol_type
Definition: copy.c:123
#define Assert(condition)
Definition: c.h:739
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:581
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ CopyReadLineText()

static bool CopyReadLineText ( CopyState  cstate)
static

Definition at line 3979 of file copy.c.

References appendBinaryStringInfo(), CopyLoadRawBuf(), CopyStateData::csv_mode, CopyStateData::cur_lineno, CopyStateData::encoding_embeds_ascii, EOL_CR, EOL_CRNL, EOL_NL, CopyStateData::eol_type, EOL_UNKNOWN, ereport, errcode(), errhint(), errmsg(), ERROR, CopyStateData::escape, CopyStateData::file_encoding, IF_NEED_REFILL_AND_EOF_BREAK, IF_NEED_REFILL_AND_NOT_EOF_CONTINUE, IS_HIGHBIT_SET, CopyStateData::line_buf, NO_END_OF_COPY_GOTO, pg_encoding_mblen(), CopyStateData::quote, CopyStateData::raw_buf, CopyStateData::raw_buf_index, CopyStateData::raw_buf_len, and REFILL_LINEBUF.

Referenced by CopyReadLine().

3980 {
3981  char *copy_raw_buf;
3982  int raw_buf_ptr;
3983  int copy_buf_len;
3984  bool need_data = false;
3985  bool hit_eof = false;
3986  bool result = false;
3987  char mblen_str[2];
3988 
3989  /* CSV variables */
3990  bool first_char_in_line = true;
3991  bool in_quote = false,
3992  last_was_esc = false;
3993  char quotec = '\0';
3994  char escapec = '\0';
3995 
3996  if (cstate->csv_mode)
3997  {
3998  quotec = cstate->quote[0];
3999  escapec = cstate->escape[0];
4000  /* ignore special escape processing if it's the same as quotec */
4001  if (quotec == escapec)
4002  escapec = '\0';
4003  }
4004 
4005  mblen_str[1] = '\0';
4006 
4007  /*
4008  * The objective of this loop is to transfer the entire next input line
4009  * into line_buf. Hence, we only care for detecting newlines (\r and/or
4010  * \n) and the end-of-copy marker (\.).
4011  *
4012  * In CSV mode, \r and \n inside a quoted field are just part of the data
4013  * value and are put in line_buf. We keep just enough state to know if we
4014  * are currently in a quoted field or not.
4015  *
4016  * These four characters, and the CSV escape and quote characters, are
4017  * assumed the same in frontend and backend encodings.
4018  *
4019  * For speed, we try to move data from raw_buf to line_buf in chunks
4020  * rather than one character at a time. raw_buf_ptr points to the next
4021  * character to examine; any characters from raw_buf_index to raw_buf_ptr
4022  * have been determined to be part of the line, but not yet transferred to
4023  * line_buf.
4024  *
4025  * For a little extra speed within the loop, we copy raw_buf and
4026  * raw_buf_len into local variables.
4027  */
4028  copy_raw_buf = cstate->raw_buf;
4029  raw_buf_ptr = cstate->raw_buf_index;
4030  copy_buf_len = cstate->raw_buf_len;
4031 
4032  for (;;)
4033  {
4034  int prev_raw_ptr;
4035  char c;
4036 
4037  /*
4038  * Load more data if needed. Ideally we would just force four bytes
4039  * of read-ahead and avoid the many calls to
4040  * IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(), but the COPY_OLD_FE protocol
4041  * does not allow us to read too far ahead or we might read into the
4042  * next data, so we read-ahead only as far we know we can. One
4043  * optimization would be to read-ahead four byte here if
4044  * cstate->copy_dest != COPY_OLD_FE, but it hardly seems worth it,
4045  * considering the size of the buffer.
4046  */
4047  if (raw_buf_ptr >= copy_buf_len || need_data)
4048  {
4050 
4051  /*
4052  * Try to read some more data. This will certainly reset
4053  * raw_buf_index to zero, and raw_buf_ptr must go with it.
4054  */
4055  if (!CopyLoadRawBuf(cstate))
4056  hit_eof = true;
4057  raw_buf_ptr = 0;
4058  copy_buf_len = cstate->raw_buf_len;
4059 
4060  /*
4061  * If we are completely out of data, break out of the loop,
4062  * reporting EOF.
4063  */
4064  if (copy_buf_len <= 0)
4065  {
4066  result = true;
4067  break;
4068  }
4069  need_data = false;
4070  }
4071 
4072  /* OK to fetch a character */
4073  prev_raw_ptr = raw_buf_ptr;
4074  c = copy_raw_buf[raw_buf_ptr++];
4075 
4076  if (cstate->csv_mode)
4077  {
4078  /*
4079  * If character is '\\' or '\r', we may need to look ahead below.
4080  * Force fetch of the next character if we don't already have it.
4081  * We need to do this before changing CSV state, in case one of
4082  * these characters is also the quote or escape character.
4083  *
4084  * Note: old-protocol does not like forced prefetch, but it's OK
4085  * here since we cannot validly be at EOF.
4086  */
4087  if (c == '\\' || c == '\r')
4088  {
4090  }
4091 
4092  /*
4093  * Dealing with quotes and escapes here is mildly tricky. If the
4094  * quote char is also the escape char, there's no problem - we
4095  * just use the char as a toggle. If they are different, we need
4096  * to ensure that we only take account of an escape inside a
4097  * quoted field and immediately preceding a quote char, and not
4098  * the second in an escape-escape sequence.
4099  */
4100  if (in_quote && c == escapec)
4101  last_was_esc = !last_was_esc;
4102  if (c == quotec && !last_was_esc)
4103  in_quote = !in_quote;
4104  if (c != escapec)
4105  last_was_esc = false;
4106 
4107  /*
4108  * Updating the line count for embedded CR and/or LF chars is
4109  * necessarily a little fragile - this test is probably about the
4110  * best we can do. (XXX it's arguable whether we should do this
4111  * at all --- is cur_lineno a physical or logical count?)
4112  */
4113  if (in_quote && c == (cstate->eol_type == EOL_NL ? '\n' : '\r'))
4114  cstate->cur_lineno++;
4115  }
4116 
4117  /* Process \r */
4118  if (c == '\r' && (!cstate->csv_mode || !in_quote))
4119  {
4120  /* Check for \r\n on first line, _and_ handle \r\n. */
4121  if (cstate->eol_type == EOL_UNKNOWN ||
4122  cstate->eol_type == EOL_CRNL)
4123  {
4124  /*
4125  * If need more data, go back to loop top to load it.
4126  *
4127  * Note that if we are at EOF, c will wind up as '\0' because
4128  * of the guaranteed pad of raw_buf.
4129  */
4131 
4132  /* get next char */
4133  c = copy_raw_buf[raw_buf_ptr];
4134 
4135  if (c == '\n')
4136  {
4137  raw_buf_ptr++; /* eat newline */
4138  cstate->eol_type = EOL_CRNL; /* in case not set yet */
4139  }
4140  else
4141  {
4142  /* found \r, but no \n */
4143  if (cstate->eol_type == EOL_CRNL)
4144  ereport(ERROR,
4145  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4146  !cstate->csv_mode ?
4147  errmsg("literal carriage return found in data") :
4148  errmsg("unquoted carriage return found in data"),
4149  !cstate->csv_mode ?
4150  errhint("Use \"\\r\" to represent carriage return.") :
4151  errhint("Use quoted CSV field to represent carriage return.")));
4152 
4153  /*
4154  * if we got here, it is the first line and we didn't find
4155  * \n, so don't consume the peeked character
4156  */
4157  cstate->eol_type = EOL_CR;
4158  }
4159  }
4160  else if (cstate->eol_type == EOL_NL)
4161  ereport(ERROR,
4162  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4163  !cstate->csv_mode ?
4164  errmsg("literal carriage return found in data") :
4165  errmsg("unquoted carriage return found in data"),
4166  !cstate->csv_mode ?
4167  errhint("Use \"\\r\" to represent carriage return.") :
4168  errhint("Use quoted CSV field to represent carriage return.")));
4169  /* If reach here, we have found the line terminator */
4170  break;
4171  }
4172 
4173  /* Process \n */
4174  if (c == '\n' && (!cstate->csv_mode || !in_quote))
4175  {
4176  if (cstate->eol_type == EOL_CR || cstate->eol_type == EOL_CRNL)
4177  ereport(ERROR,
4178  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4179  !cstate->csv_mode ?
4180  errmsg("literal newline found in data") :
4181  errmsg("unquoted newline found in data"),
4182  !cstate->csv_mode ?
4183  errhint("Use \"\\n\" to represent newline.") :
4184  errhint("Use quoted CSV field to represent newline.")));
4185  cstate->eol_type = EOL_NL; /* in case not set yet */
4186  /* If reach here, we have found the line terminator */
4187  break;
4188  }
4189 
4190  /*
4191  * In CSV mode, we only recognize \. alone on a line. This is because
4192  * \. is a valid CSV data value.
4193  */
4194  if (c == '\\' && (!cstate->csv_mode || first_char_in_line))
4195  {
4196  char c2;
4197 
4200 
4201  /* -----
4202  * get next character
4203  * Note: we do not change c so if it isn't \., we can fall
4204  * through and continue processing for file encoding.
4205  * -----
4206  */
4207  c2 = copy_raw_buf[raw_buf_ptr];
4208 
4209  if (c2 == '.')
4210  {
4211  raw_buf_ptr++; /* consume the '.' */
4212 
4213  /*
4214  * Note: if we loop back for more data here, it does not
4215  * matter that the CSV state change checks are re-executed; we
4216  * will come back here with no important state changed.
4217  */
4218  if (cstate->eol_type == EOL_CRNL)
4219  {
4220  /* Get the next character */
4222  /* if hit_eof, c2 will become '\0' */
4223  c2 = copy_raw_buf[raw_buf_ptr++];
4224 
4225  if (c2 == '\n')
4226  {
4227  if (!cstate->csv_mode)
4228  ereport(ERROR,
4229  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4230  errmsg("end-of-copy marker does not match previous newline style")));
4231  else
4233  }
4234  else if (c2 != '\r')
4235  {
4236  if (!cstate->csv_mode)
4237  ereport(ERROR,
4238  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4239  errmsg("end-of-copy marker corrupt")));
4240  else
4242  }
4243  }
4244 
4245  /* Get the next character */
4247  /* if hit_eof, c2 will become '\0' */
4248  c2 = copy_raw_buf[raw_buf_ptr++];
4249 
4250  if (c2 != '\r' && c2 != '\n')
4251  {
4252  if (!cstate->csv_mode)
4253  ereport(ERROR,
4254  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4255  errmsg("end-of-copy marker corrupt")));
4256  else
4258  }
4259 
4260  if ((cstate->eol_type == EOL_NL && c2 != '\n') ||
4261  (cstate->eol_type == EOL_CRNL && c2 != '\n') ||
4262  (cstate->eol_type == EOL_CR && c2 != '\r'))
4263  {
4264  ereport(ERROR,
4265  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4266  errmsg("end-of-copy marker does not match previous newline style")));
4267  }
4268 
4269  /*
4270  * Transfer only the data before the \. into line_buf, then
4271  * discard the data and the \. sequence.
4272  */
4273  if (prev_raw_ptr > cstate->raw_buf_index)
4275  cstate->raw_buf + cstate->raw_buf_index,
4276  prev_raw_ptr - cstate->raw_buf_index);
4277  cstate->raw_buf_index = raw_buf_ptr;
4278  result = true; /* report EOF */
4279  break;
4280  }
4281  else if (!cstate->csv_mode)
4282 
4283  /*
4284  * If we are here, it means we found a backslash followed by
4285  * something other than a period. In non-CSV mode, anything
4286  * after a backslash is special, so we skip over that second
4287  * character too. If we didn't do that \\. would be
4288  * considered an eof-of copy, while in non-CSV mode it is a
4289  * literal backslash followed by a period. In CSV mode,
4290  * backslashes are not special, so we want to process the
4291  * character after the backslash just like a normal character,
4292  * so we don't increment in those cases.
4293  */
4294  raw_buf_ptr++;
4295  }
4296 
4297  /*
4298  * This label is for CSV cases where \. appears at the start of a
4299  * line, but there is more text after it, meaning it was a data value.
4300  * We are more strict for \. in CSV mode because \. could be a data
4301  * value, while in non-CSV mode, \. cannot be a data value.
4302  */
4303 not_end_of_copy:
4304 
4305  /*
4306  * Process all bytes of a multi-byte character as a group.
4307  *
4308  * We only support multi-byte sequences where the first byte has the
4309  * high-bit set, so as an optimization we can avoid this block
4310  * entirely if it is not set.
4311  */
4312  if (cstate->encoding_embeds_ascii && IS_HIGHBIT_SET(c))
4313  {
4314  int mblen;
4315 
4316  /*
4317  * It is enough to look at the first byte in all our encodings, to
4318  * get the length. (GB18030 is a bit special, but still works for
4319  * our purposes; see comment in pg_gb18030_mblen())
4320  */
4321  mblen_str[0] = c;
4322  mblen = pg_encoding_mblen(cstate->file_encoding, mblen_str);
4323 
4325  IF_NEED_REFILL_AND_EOF_BREAK(mblen - 1);
4326  raw_buf_ptr += mblen - 1;
4327  }
4328  first_char_in_line = false;
4329  } /* end of outer loop */
4330 
4331  /*
4332  * Transfer any still-uncopied data to line_buf.
4333  */
4335 
4336  return result;
4337 }
bool csv_mode
Definition: copy.c:137
int errhint(const char *fmt,...)
Definition: elog.c:1069
Definition: copy.c:82
StringInfoData line_buf
Definition: copy.c:210
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
Definition: copy.c:312
int raw_buf_index
Definition: copy.c:223
int errcode(int sqlerrcode)
Definition: elog.c:608
#define REFILL_LINEBUF
Definition: copy.c:329
uint64 cur_lineno
Definition: copy.c:159
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1106
#define ERROR
Definition: elog.h:43
bool encoding_embeds_ascii
Definition: copy.c:126
char * c
char * raw_buf
Definition: copy.c:222
static bool CopyLoadRawBuf(CopyState cstate)
Definition: copy.c:792
char * quote
Definition: copy.c:143
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1554
Definition: copy.c:83
int raw_buf_len
Definition: copy.c:224
char * escape
Definition: copy.c:144
#define ereport(elevel, rest)
Definition: elog.h:141
Definition: copy.c:84
int file_encoding
Definition: copy.c:124
#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen)
Definition: copy.c:300
EolType eol_type
Definition: copy.c:123
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define NO_END_OF_COPY_GOTO
Definition: copy.c:342
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ CopySendChar()

static void CopySendChar ( CopyState  cstate,
char  c 
)
static

Definition at line 511 of file copy.c.

References appendStringInfoCharMacro, and CopyStateData::fe_msgbuf.

Referenced by CopyAttributeOutCSV(), CopyAttributeOutText(), CopyOneRowTo(), CopySendEndOfRow(), and CopyTo().

512 {
514 }
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128
char * c
StringInfo fe_msgbuf
Definition: copy.c:118

◆ CopySendData()

static void CopySendData ( CopyState  cstate,
const void *  databuf,
int  datasize 
)
static

Definition at line 499 of file copy.c.

References appendBinaryStringInfo(), and CopyStateData::fe_msgbuf.

Referenced by CopyOneRowTo(), CopySendInt16(), CopySendInt32(), CopyTo(), and SendCopyEnd().

500 {
501  appendBinaryStringInfo(cstate->fe_msgbuf, databuf, datasize);
502 }
StringInfo fe_msgbuf
Definition: copy.c:118
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ CopySendEndOfRow()

static void CopySendEndOfRow ( CopyState  cstate)
static

Definition at line 517 of file copy.c.

References Assert, CopyStateData::binary, ClosePipeToProgram(), COPY_CALLBACK, CopyStateData::copy_dest, COPY_FILE, CopyStateData::copy_file, COPY_NEW_FE, COPY_OLD_FE, CopySendChar(), CopySendString(), StringInfoData::data, ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, FATAL, CopyStateData::fe_msgbuf, CopyStateData::is_program, StringInfoData::len, pq_putbytes(), pq_putmessage, and resetStringInfo().

Referenced by CopyOneRowTo(), CopyTo(), and SendCopyEnd().

518 {
519  StringInfo fe_msgbuf = cstate->fe_msgbuf;
520 
521  switch (cstate->copy_dest)
522  {
523  case COPY_FILE:
524  if (!cstate->binary)
525  {
526  /* Default line termination depends on platform */
527 #ifndef WIN32
528  CopySendChar(cstate, '\n');
529 #else
530  CopySendString(cstate, "\r\n");
531 #endif
532  }
533 
534  if (fwrite(fe_msgbuf->data, fe_msgbuf->len, 1,
535  cstate->copy_file) != 1 ||
536  ferror(cstate->copy_file))
537  {
538  if (cstate->