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/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 "foreign/fdwapi.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "optimizer/clauses.h"
#include "optimizer/planner.h"
#include "nodes/makefuncs.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/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
 

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 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 MAX_BUFFERED_TUPLES   1000
 
#define DUMPSOFAR()
 

Typedefs

typedef enum CopyDest CopyDest
 
typedef enum EolType EolType
 
typedef struct CopyStateData CopyStateData
 

Enumerations

enum  CopyDest { COPY_FILE, COPY_OLD_FE, COPY_NEW_FE, COPY_CALLBACK }
 
enum  EolType { EOL_UNKNOWN, EOL_NL, EOL_CR, EOL_CRNL }
 

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, Oid tupleOid, Datum *values, bool *nulls)
 
static void CopyFromInsertBatch (CopyState cstate, EState *estate, CommandId mycid, int hi_options, ResultRelInfo *resultRelInfo, TupleTableSlot *myslot, BulkInsertState bistate, int nBufferedTuples, HeapTuple *bufferedTuples, int firstBufferedLineNo)
 
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)
 
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, Oid *tupleOid)
 
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 4479 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 252 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 240 of file copy.c.

Referenced by CopyReadLineText().

◆ ISOCTAL

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

Definition at line 56 of file copy.c.

Referenced by CopyReadAttributesText().

◆ MAX_BUFFERED_TUPLES

#define MAX_BUFFERED_TUPLES   1000

Referenced by CopyFrom().

◆ MAX_COPY_DATA_DISPLAY

#define MAX_COPY_DATA_DISPLAY   100

Referenced by limit_printout_length().

◆ 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 282 of file copy.c.

Referenced by CopyReadLineText().

◆ OCTVALUE

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

Definition at line 57 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 209 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 269 of file copy.c.

Referenced by CopyReadLineText().

Typedef Documentation

◆ CopyDest

◆ CopyStateData

◆ EolType

Enumeration Type Documentation

◆ CopyDest

enum CopyDest
Enumerator
COPY_FILE 
COPY_OLD_FE 
COPY_NEW_FE 
COPY_CALLBACK 

Definition at line 63 of file copy.c.

64 {
65  COPY_FILE, /* to/from file (or a piped program) */
66  COPY_OLD_FE, /* to/from frontend (2.0 protocol) */
67  COPY_NEW_FE, /* to/from frontend (3.0 protocol) */
68  COPY_CALLBACK /* to/from callback function */
69 } CopyDest;
Definition: copy.c:65
CopyDest
Definition: copy.c:63

◆ EolType

enum EolType
Enumerator
EOL_UNKNOWN 
EOL_NL 
EOL_CR 
EOL_CRNL 

Definition at line 74 of file copy.c.

75 {
77  EOL_NL,
78  EOL_CR,
79  EOL_CRNL
80 } EolType;
Definition: copy.c:77
EolType
Definition: copy.c:74
Definition: copy.c:78
Definition: copy.c:79

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 1389 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, tupleDesc::natts, CopyStateData::need_transcoding, NIL, CopyStateData::oids, 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, RelationData::rd_rel, CopyStateData::rel, RelationGetDescr, RelationGetRelationName, PlannedStmt::relationOids, Query::returningList, QueryDesc::tupDesc, TupleDescAttr, UpdateActiveSnapshotCommandId(), and Query::utilityStmt.

Referenced by BeginCopyFrom(), and BeginCopyTo().

1396 {
1397  CopyState cstate;
1398  TupleDesc tupDesc;
1399  int num_phys_attrs;
1400  MemoryContext oldcontext;
1401 
1402  /* Allocate workspace and zero all fields */
1403  cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
1404 
1405  /*
1406  * We allocate everything used by a cstate in a new memory context. This
1407  * avoids memory leaks during repeated use of COPY in a query.
1408  */
1410  "COPY",
1412 
1413  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1414 
1415  /* Extract options from the statement node tree */
1416  ProcessCopyOptions(pstate, cstate, is_from, options);
1417 
1418  /* Process the source/target relation or query */
1419  if (rel)
1420  {
1421  Assert(!raw_query);
1422 
1423  cstate->rel = rel;
1424 
1425  tupDesc = RelationGetDescr(cstate->rel);
1426 
1427  /* Don't allow COPY w/ OIDs to or from a table without them */
1428  if (cstate->oids && !cstate->rel->rd_rel->relhasoids)
1429  ereport(ERROR,
1430  (errcode(ERRCODE_UNDEFINED_COLUMN),
1431  errmsg("table \"%s\" does not have OIDs",
1432  RelationGetRelationName(cstate->rel))));
1433  }
1434  else
1435  {
1436  List *rewritten;
1437  Query *query;
1438  PlannedStmt *plan;
1439  DestReceiver *dest;
1440 
1441  Assert(!is_from);
1442  cstate->rel = NULL;
1443 
1444  /* Don't allow COPY w/ OIDs from a query */
1445  if (cstate->oids)
1446  ereport(ERROR,
1447  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1448  errmsg("COPY (query) WITH OIDS is not supported")));
1449 
1450  /*
1451  * Run parse analysis and rewrite. Note this also acquires sufficient
1452  * locks on the source table(s).
1453  *
1454  * Because the parser and planner tend to scribble on their input, we
1455  * make a preliminary copy of the source querytree. This prevents
1456  * problems in the case that the COPY is in a portal or plpgsql
1457  * function and is executed repeatedly. (See also the same hack in
1458  * DECLARE CURSOR and PREPARE.) XXX FIXME someday.
1459  */
1460  rewritten = pg_analyze_and_rewrite(copyObject(raw_query),
1461  pstate->p_sourcetext, NULL, 0,
1462  NULL);
1463 
1464  /* check that we got back something we can work with */
1465  if (rewritten == NIL)
1466  {
1467  ereport(ERROR,
1468  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1469  errmsg("DO INSTEAD NOTHING rules are not supported for COPY")));
1470  }
1471  else if (list_length(rewritten) > 1)
1472  {
1473  ListCell *lc;
1474 
1475  /* examine queries to determine which error message to issue */
1476  foreach(lc, rewritten)
1477  {
1478  Query *q = lfirst_node(Query, lc);
1479 
1481  ereport(ERROR,
1482  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1483  errmsg("conditional DO INSTEAD rules are not supported for COPY")));
1485  ereport(ERROR,
1486  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1487  errmsg("DO ALSO rules are not supported for the COPY")));
1488  }
1489 
1490  ereport(ERROR,
1491  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1492  errmsg("multi-statement DO INSTEAD rules are not supported for COPY")));
1493  }
1494 
1495  query = linitial_node(Query, rewritten);
1496 
1497  /* The grammar allows SELECT INTO, but we don't support that */
1498  if (query->utilityStmt != NULL &&
1500  ereport(ERROR,
1501  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1502  errmsg("COPY (SELECT INTO) is not supported")));
1503 
1504  Assert(query->utilityStmt == NULL);
1505 
1506  /*
1507  * Similarly the grammar doesn't enforce the presence of a RETURNING
1508  * clause, but this is required here.
1509  */
1510  if (query->commandType != CMD_SELECT &&
1511  query->returningList == NIL)
1512  {
1513  Assert(query->commandType == CMD_INSERT ||
1514  query->commandType == CMD_UPDATE ||
1515  query->commandType == CMD_DELETE);
1516 
1517  ereport(ERROR,
1518  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1519  errmsg("COPY query must have a RETURNING clause")));
1520  }
1521 
1522  /* plan the query */
1523  plan = pg_plan_query(query, CURSOR_OPT_PARALLEL_OK, NULL);
1524 
1525  /*
1526  * With row level security and a user using "COPY relation TO", we
1527  * have to convert the "COPY relation TO" to a query-based COPY (eg:
1528  * "COPY (SELECT * FROM relation) TO"), to allow the rewriter to add
1529  * in any RLS clauses.
1530  *
1531  * When this happens, we are passed in the relid of the originally
1532  * found relation (which we have locked). As the planner will look up
1533  * the relation again, we double-check here to make sure it found the
1534  * same one that we have locked.
1535  */
1536  if (queryRelId != InvalidOid)
1537  {
1538  /*
1539  * Note that with RLS involved there may be multiple relations,
1540  * and while the one we need is almost certainly first, we don't
1541  * make any guarantees of that in the planner, so check the whole
1542  * list and make sure we find the original relation.
1543  */
1544  if (!list_member_oid(plan->relationOids, queryRelId))
1545  ereport(ERROR,
1546  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1547  errmsg("relation referenced by COPY statement has changed")));
1548  }
1549 
1550  /*
1551  * Use a snapshot with an updated command ID to ensure this query sees
1552  * results of any previously executed queries.
1553  */
1556 
1557  /* Create dest receiver for COPY OUT */
1559  ((DR_copy *) dest)->cstate = cstate;
1560 
1561  /* Create a QueryDesc requesting no output */
1562  cstate->queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
1565  dest, NULL, NULL, 0);
1566 
1567  /*
1568  * Call ExecutorStart to prepare the plan for execution.
1569  *
1570  * ExecutorStart computes a result tupdesc for us
1571  */
1572  ExecutorStart(cstate->queryDesc, 0);
1573 
1574  tupDesc = cstate->queryDesc->tupDesc;
1575  }
1576 
1577  /* Generate or convert list of attributes to process */
1578  cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist);
1579 
1580  num_phys_attrs = tupDesc->natts;
1581 
1582  /* Convert FORCE_QUOTE name list to per-column flags, check validity */
1583  cstate->force_quote_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1584  if (cstate->force_quote_all)
1585  {
1586  int i;
1587 
1588  for (i = 0; i < num_phys_attrs; i++)
1589  cstate->force_quote_flags[i] = true;
1590  }
1591  else if (cstate->force_quote)
1592  {
1593  List *attnums;
1594  ListCell *cur;
1595 
1596  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_quote);
1597 
1598  foreach(cur, attnums)
1599  {
1600  int attnum = lfirst_int(cur);
1601  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1602 
1603  if (!list_member_int(cstate->attnumlist, attnum))
1604  ereport(ERROR,
1605  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1606  errmsg("FORCE_QUOTE column \"%s\" not referenced by COPY",
1607  NameStr(attr->attname))));
1608  cstate->force_quote_flags[attnum - 1] = true;
1609  }
1610  }
1611 
1612  /* Convert FORCE_NOT_NULL name list to per-column flags, check validity */
1613  cstate->force_notnull_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1614  if (cstate->force_notnull)
1615  {
1616  List *attnums;
1617  ListCell *cur;
1618 
1619  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_notnull);
1620 
1621  foreach(cur, attnums)
1622  {
1623  int attnum = lfirst_int(cur);
1624  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1625 
1626  if (!list_member_int(cstate->attnumlist, attnum))
1627  ereport(ERROR,
1628  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1629  errmsg("FORCE_NOT_NULL column \"%s\" not referenced by COPY",
1630  NameStr(attr->attname))));
1631  cstate->force_notnull_flags[attnum - 1] = true;
1632  }
1633  }
1634 
1635  /* Convert FORCE_NULL name list to per-column flags, check validity */
1636  cstate->force_null_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1637  if (cstate->force_null)
1638  {
1639  List *attnums;
1640  ListCell *cur;
1641 
1642  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->force_null);
1643 
1644  foreach(cur, attnums)
1645  {
1646  int attnum = lfirst_int(cur);
1647  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1648 
1649  if (!list_member_int(cstate->attnumlist, attnum))
1650  ereport(ERROR,
1651  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1652  errmsg("FORCE_NULL column \"%s\" not referenced by COPY",
1653  NameStr(attr->attname))));
1654  cstate->force_null_flags[attnum - 1] = true;
1655  }
1656  }
1657 
1658  /* Convert convert_selectively name list to per-column flags */
1659  if (cstate->convert_selectively)
1660  {
1661  List *attnums;
1662  ListCell *cur;
1663 
1664  cstate->convert_select_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1665 
1666  attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->convert_select);
1667 
1668  foreach(cur, attnums)
1669  {
1670  int attnum = lfirst_int(cur);
1671  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1672 
1673  if (!list_member_int(cstate->attnumlist, attnum))
1674  ereport(ERROR,
1675  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1676  errmsg_internal("selected column \"%s\" not referenced by COPY",
1677  NameStr(attr->attname))));
1678  cstate->convert_select_flags[attnum - 1] = true;
1679  }
1680  }
1681 
1682  /* Use client encoding when ENCODING option is not specified. */
1683  if (cstate->file_encoding < 0)
1685 
1686  /*
1687  * Set up encoding conversion info. Even if the file and server encodings
1688  * are the same, we must apply pg_any_to_server() to validate data in
1689  * multibyte encodings.
1690  */
1691  cstate->need_transcoding =
1692  (cstate->file_encoding != GetDatabaseEncoding() ||
1694  /* See Multibyte encoding comment above */
1696 
1697  cstate->copy_dest = COPY_FILE; /* default */
1698 
1699  MemoryContextSwitchTo(oldcontext);
1700 
1701  return cstate;
1702 }
#define NIL
Definition: pg_list.h:69
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:781
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
#define RelationGetDescr(relation)
Definition: rel.h:433
bool need_transcoding
Definition: copy.c:108
List * attnumlist
Definition: copy.c:114
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
List * relationOids
Definition: plannodes.h:90
Definition: copy.c:216
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:141
void ProcessCopyOptions(ParseState *pstate, CopyState cstate, bool is_from, List *options)
Definition: copy.c:1031
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
bool * force_quote_flags
Definition: copy.c:131
Form_pg_class rd_rel
Definition: rel.h:84
List * pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string, Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition: postgres.c:651
Node * utilityStmt
Definition: parsenodes.h:120
#define linitial_node(type, l)
Definition: pg_list.h:114
bool * force_null_flags
Definition: copy.c:135
int natts
Definition: tupdesc.h:82
bool * convert_select_flags
Definition: copy.c:138
CopyDest copy_dest
Definition: copy.c:101
Definition: copy.c:65
Relation rel
Definition: copy.c:112
MemoryContext copycontext
Definition: copy.c:149
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:769
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:109
QueryDesc * queryDesc
Definition: copy.c:113
bool list_member_int(const List *list, int datum)
Definition: list.c:485
bool encoding_embeds_ascii
Definition: copy.c:109
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:109
#define RelationGetRelationName(relation)
Definition: rel.h:441
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
const char * p_sourcetext
Definition: parse_node.h:173
List * returningList
Definition: parsenodes.h:146
#define ereport(elevel, rest)
Definition: elog.h:122
List * force_null
Definition: copy.c:134
int file_encoding
Definition: copy.c:107
static List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
Definition: copy.c:4730
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
TupleDesc tupDesc
Definition: execdesc.h:47
#define InvalidSnapshot
Definition: snapshot.h:25
void * palloc0(Size size)
Definition: mcxt.c:955
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
int pg_get_client_encoding(void)
Definition: mbutils.c:306
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:79
bool * force_notnull_flags
Definition: copy.c:133
CmdType commandType
Definition: parsenodes.h:112
List * force_notnull
Definition: copy.c:132
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
QuerySource querySource
Definition: parsenodes.h:114
#define Assert(condition)
Definition: c.h:699
List * convert_select
Definition: copy.c:137
static int list_length(const List *l)
Definition: pg_list.h:89
bool force_quote_all
Definition: copy.c:130
#define PG_ENCODING_IS_CLIENT_ONLY(_enc)
Definition: pg_wchar.h:298
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2653
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define NameStr(name)
Definition: c.h:576
#define copyObject(obj)
Definition: nodes.h:630
Definition: pg_list.h:45
List * force_quote
Definition: copy.c:129
bool convert_selectively
Definition: copy.c:136
bool oids
Definition: copy.c:119
PlannedStmt * pg_plan_query(Query *querytree, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:792

◆ 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 2986 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::fe_eof, CopyStateData::file_has_oids, 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(), tupleDesc::natts, CopyStateData::num_defaults, CopyStateData::oid_in_function, CopyStateData::oid_typioparam, CopyStateData::oids, 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, 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().

2993 {
2994  CopyState cstate;
2995  bool pipe = (filename == NULL);
2996  TupleDesc tupDesc;
2997  AttrNumber num_phys_attrs,
2998  num_defaults;
2999  FmgrInfo *in_functions;
3000  Oid *typioparams;
3001  int attnum;
3002  Oid in_func_oid;
3003  int *defmap;
3004  ExprState **defexprs;
3005  MemoryContext oldcontext;
3006  bool volatile_defexprs;
3007 
3008  cstate = BeginCopy(pstate, true, rel, NULL, InvalidOid, attnamelist, options);
3009  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
3010 
3011  /* Initialize state variables */
3012  cstate->fe_eof = false;
3013  cstate->eol_type = EOL_UNKNOWN;
3014  cstate->cur_relname = RelationGetRelationName(cstate->rel);
3015  cstate->cur_lineno = 0;
3016  cstate->cur_attname = NULL;
3017  cstate->cur_attval = NULL;
3018 
3019  /* Set up variables to avoid per-attribute overhead. */
3020  initStringInfo(&cstate->attribute_buf);
3021  initStringInfo(&cstate->line_buf);
3022  cstate->line_buf_converted = false;
3023  cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
3024  cstate->raw_buf_index = cstate->raw_buf_len = 0;
3025 
3026  /* Assign range table, we'll need it in CopyFrom. */
3027  if (pstate)
3028  cstate->range_table = pstate->p_rtable;
3029 
3030  tupDesc = RelationGetDescr(cstate->rel);
3031  num_phys_attrs = tupDesc->natts;
3032  num_defaults = 0;
3033  volatile_defexprs = false;
3034 
3035  /*
3036  * Pick up the required catalog information for each attribute in the
3037  * relation, including the input function, the element type (to pass to
3038  * the input function), and info about defaults and constraints. (Which
3039  * input function we use depends on text/binary format choice.)
3040  */
3041  in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
3042  typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
3043  defmap = (int *) palloc(num_phys_attrs * sizeof(int));
3044  defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
3045 
3046  for (attnum = 1; attnum <= num_phys_attrs; attnum++)
3047  {
3048  Form_pg_attribute att = TupleDescAttr(tupDesc, attnum - 1);
3049 
3050  /* We don't need info for dropped attributes */
3051  if (att->attisdropped)
3052  continue;
3053 
3054  /* Fetch the input function and typioparam info */
3055  if (cstate->binary)
3056  getTypeBinaryInputInfo(att->atttypid,
3057  &in_func_oid, &typioparams[attnum - 1]);
3058  else
3059  getTypeInputInfo(att->atttypid,
3060  &in_func_oid, &typioparams[attnum - 1]);
3061  fmgr_info(in_func_oid, &in_functions[attnum - 1]);
3062 
3063  /* Get default info if needed */
3064  if (!list_member_int(cstate->attnumlist, attnum))
3065  {
3066  /* attribute is NOT to be copied from input */
3067  /* use default value if one exists */
3068  Expr *defexpr = (Expr *) build_column_default(cstate->rel,
3069  attnum);
3070 
3071  if (defexpr != NULL)
3072  {
3073  /* Run the expression through planner */
3074  defexpr = expression_planner(defexpr);
3075 
3076  /* Initialize executable expression in copycontext */
3077  defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
3078  defmap[num_defaults] = attnum - 1;
3079  num_defaults++;
3080 
3081  /*
3082  * If a default expression looks at the table being loaded,
3083  * then it could give the wrong answer when using
3084  * multi-insert. Since database access can be dynamic this is
3085  * hard to test for exactly, so we use the much wider test of
3086  * whether the default expression is volatile. We allow for
3087  * the special case of when the default expression is the
3088  * nextval() of a sequence which in this specific case is
3089  * known to be safe for use with the multi-insert
3090  * optimization. Hence we use this special case function
3091  * checker rather than the standard check for
3092  * contain_volatile_functions().
3093  */
3094  if (!volatile_defexprs)
3095  volatile_defexprs = contain_volatile_functions_not_nextval((Node *) defexpr);
3096  }
3097  }
3098  }
3099 
3100  /* We keep those variables in cstate. */
3101  cstate->in_functions = in_functions;
3102  cstate->typioparams = typioparams;
3103  cstate->defmap = defmap;
3104  cstate->defexprs = defexprs;
3105  cstate->volatile_defexprs = volatile_defexprs;
3106  cstate->num_defaults = num_defaults;
3107  cstate->is_program = is_program;
3108 
3109  if (data_source_cb)
3110  {
3111  cstate->copy_dest = COPY_CALLBACK;
3112  cstate->data_source_cb = data_source_cb;
3113  }
3114  else if (pipe)
3115  {
3116  Assert(!is_program); /* the grammar does not allow this */
3118  ReceiveCopyBegin(cstate);
3119  else
3120  cstate->copy_file = stdin;
3121  }
3122  else
3123  {
3124  cstate->filename = pstrdup(filename);
3125 
3126  if (cstate->is_program)
3127  {
3128  cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_R);
3129  if (cstate->copy_file == NULL)
3130  ereport(ERROR,
3132  errmsg("could not execute command \"%s\": %m",
3133  cstate->filename)));
3134  }
3135  else
3136  {
3137  struct stat st;
3138 
3139  cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R);
3140  if (cstate->copy_file == NULL)
3141  {
3142  /* copy errno because ereport subfunctions might change it */
3143  int save_errno = errno;
3144 
3145  ereport(ERROR,
3147  errmsg("could not open file \"%s\" for reading: %m",
3148  cstate->filename),
3149  (save_errno == ENOENT || save_errno == EACCES) ?
3150  errhint("COPY FROM instructs the PostgreSQL server process to read a file. "
3151  "You may want a client-side facility such as psql's \\copy.") : 0));
3152  }
3153 
3154  if (fstat(fileno(cstate->copy_file), &st))
3155  ereport(ERROR,
3157  errmsg("could not stat file \"%s\": %m",
3158  cstate->filename)));
3159 
3160  if (S_ISDIR(st.st_mode))
3161  ereport(ERROR,
3162  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3163  errmsg("\"%s\" is a directory", cstate->filename)));
3164  }
3165  }
3166 
3167  if (!cstate->binary)
3168  {
3169  /* must rely on user to tell us... */
3170  cstate->file_has_oids = cstate->oids;
3171  }
3172  else
3173  {
3174  /* Read and verify binary header */
3175  char readSig[11];
3176  int32 tmp;
3177 
3178  /* Signature */
3179  if (CopyGetData(cstate, readSig, 11, 11) != 11 ||
3180  memcmp(readSig, BinarySignature, 11) != 0)
3181  ereport(ERROR,
3182  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3183  errmsg("COPY file signature not recognized")));
3184  /* Flags field */
3185  if (!CopyGetInt32(cstate, &tmp))
3186  ereport(ERROR,
3187  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3188  errmsg("invalid COPY file header (missing flags)")));
3189  cstate->file_has_oids = (tmp & (1 << 16)) != 0;
3190  tmp &= ~(1 << 16);
3191  if ((tmp >> 16) != 0)
3192  ereport(ERROR,
3193  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3194  errmsg("unrecognized critical flags in COPY file header")));
3195  /* Header extension length */
3196  if (!CopyGetInt32(cstate, &tmp) ||
3197  tmp < 0)
3198  ereport(ERROR,
3199  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3200  errmsg("invalid COPY file header (missing length)")));
3201  /* Skip extension header, if present */
3202  while (tmp-- > 0)
3203  {
3204  if (CopyGetData(cstate, readSig, 1, 1) != 1)
3205  ereport(ERROR,
3206  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3207  errmsg("invalid COPY file header (wrong length)")));
3208  }
3209  }
3210 
3211  if (cstate->file_has_oids && cstate->binary)
3212  {
3213  getTypeBinaryInputInfo(OIDOID,
3214  &in_func_oid, &cstate->oid_typioparam);
3215  fmgr_info(in_func_oid, &cstate->oid_in_function);
3216  }
3217 
3218  /* create workspace for CopyReadAttributes results */
3219  if (!cstate->binary)
3220  {
3221  AttrNumber attr_count = list_length(cstate->attnumlist);
3222  int nfields = cstate->file_has_oids ? (attr_count + 1) : attr_count;
3223 
3224  cstate->max_fields = nfields;
3225  cstate->raw_fields = (char **) palloc(nfields * sizeof(char *));
3226  }
3227 
3228  MemoryContextSwitchTo(oldcontext);
3229 
3230  return cstate;
3231 }
Definition: fmgr.h:56
List * range_table
Definition: copy.c:169
static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel, RawStmt *raw_query, Oid queryRelId, List *attnamelist, List *options)
Definition: copy.c:1389
bool contain_volatile_functions_not_nextval(Node *clause)
Definition: clauses.c:1008
static bool CopyGetInt32(CopyState cstate, int32 *val)
Definition: copy.c:683
int errhint(const char *fmt,...)
Definition: elog.c:987
char ** raw_fields
Definition: copy.c:189
bool binary
Definition: copy.c:118
#define RelationGetDescr(relation)
Definition: rel.h:433
AttrNumber num_defaults
Definition: copy.c:160
FmgrInfo * in_functions
Definition: copy.c:164
List * attnumlist
Definition: copy.c:114
char * filename
Definition: copy.c:115
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
bool file_has_oids
Definition: copy.c:161
char * pstrdup(const char *in)
Definition: mcxt.c:1161
static void ReceiveCopyBegin(CopyState cstate)
Definition: copy.c:381
Expr * expression_planner(Expr *expr)
Definition: planner.c:5888
StringInfoData line_buf
Definition: copy.c:198
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:517
int raw_buf_index
Definition: copy.c:211
int errcode(int sqlerrcode)
Definition: elog.c:575
bool fe_eof
Definition: copy.c:105
unsigned int Oid
Definition: postgres_ext.h:31
bool volatile_defexprs
Definition: copy.c:168
#define PG_BINARY_R
Definition: c.h:1082
int natts
Definition: tupdesc.h:82
bool line_buf_converted
Definition: copy.c:199
signed int int32
Definition: c.h:313
CopyDest copy_dest
Definition: copy.c:101
const char * cur_attname
Definition: copy.c:143
Relation rel
Definition: copy.c:112
MemoryContext copycontext
Definition: copy.c:149
copy_data_source_cb data_source_cb
Definition: copy.c:117
#define ERROR
Definition: elog.h:43
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:557
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
bool list_member_int(const List *list, int datum)
Definition: list.c:485
char * raw_buf
Definition: copy.c:210
ExprState ** defexprs
Definition: copy.c:167
const char * cur_relname
Definition: copy.c:141
int errcode_for_file_access(void)
Definition: elog.c:598
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2336
FmgrInfo oid_in_function
Definition: copy.c:162
#define RelationGetRelationName(relation)
Definition: rel.h:441
static const char BinarySignature[11]
Definition: copy.c:289
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
int raw_buf_len
Definition: copy.c:212
int max_fields
Definition: copy.c:188
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2435
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2683
#define ereport(elevel, rest)
Definition: elog.h:122
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2617
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define stat(a, b)
Definition: win32_port.h:266
Oid * typioparams
Definition: copy.c:165
bool is_program
Definition: copy.c:116
Node * build_column_default(Relation rel, int attrno)
#define RAW_BUF_SIZE
Definition: copy.c:209
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:79
EolType eol_type
Definition: copy.c:106
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
#define S_ISDIR(m)
Definition: win32_port.h:307
static char * filename
Definition: pg_dumpall.c:87
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
FILE * copy_file
Definition: copy.c:102
int cur_lineno
Definition: copy.c:142
StringInfoData attribute_buf
Definition: copy.c:184
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:119
const char * cur_attval
Definition: copy.c:144
CommandDest whereToSendOutput
Definition: postgres.c:90
Oid oid_typioparam
Definition: copy.c:163
int16 AttrNumber
Definition: attnum.h:21
int * defmap
Definition: copy.c:166
List * p_rtable
Definition: parse_node.h:174
bool oids
Definition: copy.c:119

◆ 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 1754 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_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pstrdup(), RelationData::rd_rel, RelationGetRelationName, S_ISDIR, S_IWGRP, S_IWOTH, stat, and whereToSendOutput.

Referenced by DoCopy().

1762 {
1763  CopyState cstate;
1764  bool pipe = (filename == NULL);
1765  MemoryContext oldcontext;
1766 
1767  if (rel != NULL && rel->rd_rel->relkind != RELKIND_RELATION)
1768  {
1769  if (rel->rd_rel->relkind == RELKIND_VIEW)
1770  ereport(ERROR,
1771  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1772  errmsg("cannot copy from view \"%s\"",
1774  errhint("Try the COPY (SELECT ...) TO variant.")));
1775  else if (rel->rd_rel->relkind == RELKIND_MATVIEW)
1776  ereport(ERROR,
1777  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1778  errmsg("cannot copy from materialized view \"%s\"",
1780  errhint("Try the COPY (SELECT ...) TO variant.")));
1781  else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1782  ereport(ERROR,
1783  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1784  errmsg("cannot copy from foreign table \"%s\"",
1786  errhint("Try the COPY (SELECT ...) TO variant.")));
1787  else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
1788  ereport(ERROR,
1789  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1790  errmsg("cannot copy from sequence \"%s\"",
1791  RelationGetRelationName(rel))));
1792  else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1793  ereport(ERROR,
1794  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1795  errmsg("cannot copy from partitioned table \"%s\"",
1797  errhint("Try the COPY (SELECT ...) TO variant.")));
1798  else
1799  ereport(ERROR,
1800  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1801  errmsg("cannot copy from non-table relation \"%s\"",
1802  RelationGetRelationName(rel))));
1803  }
1804 
1805  cstate = BeginCopy(pstate, false, rel, query, queryRelId, attnamelist,
1806  options);
1807  oldcontext = MemoryContextSwitchTo(cstate->copycontext);
1808 
1809  if (pipe)
1810  {
1811  Assert(!is_program); /* the grammar does not allow this */
1813  cstate->copy_file = stdout;
1814  }
1815  else
1816  {
1817  cstate->filename = pstrdup(filename);
1818  cstate->is_program = is_program;
1819 
1820  if (is_program)
1821  {
1822  cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_W);
1823  if (cstate->copy_file == NULL)
1824  ereport(ERROR,
1826  errmsg("could not execute command \"%s\": %m",
1827  cstate->filename)));
1828  }
1829  else
1830  {
1831  mode_t oumask; /* Pre-existing umask value */
1832  struct stat st;
1833 
1834  /*
1835  * Prevent write to relative path ... too easy to shoot oneself in
1836  * the foot by overwriting a database file ...
1837  */
1838  if (!is_absolute_path(filename))
1839  ereport(ERROR,
1840  (errcode(ERRCODE_INVALID_NAME),
1841  errmsg("relative path not allowed for COPY to file")));
1842 
1843  oumask = umask(S_IWGRP | S_IWOTH);
1844  PG_TRY();
1845  {
1846  cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1847  }
1848  PG_CATCH();
1849  {
1850  umask(oumask);
1851  PG_RE_THROW();
1852  }
1853  PG_END_TRY();
1854  umask(oumask);
1855  if (cstate->copy_file == NULL)
1856  {
1857  /* copy errno because ereport subfunctions might change it */
1858  int save_errno = errno;
1859 
1860  ereport(ERROR,
1862  errmsg("could not open file \"%s\" for writing: %m",
1863  cstate->filename),
1864  (save_errno == ENOENT || save_errno == EACCES) ?
1865  errhint("COPY TO instructs the PostgreSQL server process to write a file. "
1866  "You may want a client-side facility such as psql's \\copy.") : 0));
1867  }
1868 
1869  if (fstat(fileno(cstate->copy_file), &st))
1870  ereport(ERROR,
1872  errmsg("could not stat file \"%s\": %m",
1873  cstate->filename)));
1874 
1875  if (S_ISDIR(st.st_mode))
1876  ereport(ERROR,
1877  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1878  errmsg("\"%s\" is a directory", cstate->filename)));
1879  }
1880  }
1881 
1882  MemoryContextSwitchTo(oldcontext);
1883 
1884  return cstate;
1885 }
static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel, RawStmt *raw_query, Oid queryRelId, List *attnamelist, List *options)
Definition: copy.c:1389
int errhint(const char *fmt,...)
Definition: elog.c:987
char * filename
Definition: copy.c:115
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define S_IWOTH
Definition: win32_port.h:298
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_BINARY_W
Definition: c.h:1083
Form_pg_class rd_rel
Definition: rel.h:84
MemoryContext copycontext
Definition: copy.c:149
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:598
#define is_absolute_path(filename)
Definition: port.h:86
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2336
#define RelationGetRelationName(relation)
Definition: rel.h:441
#define S_IWGRP
Definition: win32_port.h:286
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2435
#define ereport(elevel, rest)
Definition: elog.h:122
#define stat(a, b)
Definition: win32_port.h:266
bool is_program
Definition: copy.c:116
#define PG_CATCH()
Definition: elog.h:293
#define Assert(condition)
Definition: c.h:699
#define PG_RE_THROW()
Definition: elog.h:314
#define S_ISDIR(m)
Definition: win32_port.h:307
static char * filename
Definition: pg_dumpall.c:87
int errmsg(const char *fmt,...)
Definition: elog.c:797
FILE * copy_file
Definition: copy.c:102
CommandDest whereToSendOutput
Definition: postgres.c:90
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300

◆ ClosePipeToProgram()

static void ClosePipeToProgram ( CopyState  cstate)
static

Definition at line 1708 of file copy.c.

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

Referenced by CopySendEndOfRow(), and EndCopy().

1709 {
1710  int pclose_rc;
1711 
1712  Assert(cstate->is_program);
1713 
1714  pclose_rc = ClosePipeStream(cstate->copy_file);
1715  if (pclose_rc == -1)
1716  ereport(ERROR,
1718  errmsg("could not close pipe to external command: %m")));
1719  else if (pclose_rc != 0)
1720  ereport(ERROR,
1721  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1722  errmsg("program \"%s\" failed",
1723  cstate->filename),
1724  errdetail_internal("%s", wait_result_to_str(pclose_rc))));
1725 }
char * filename
Definition: copy.c:115
int errcode(int sqlerrcode)
Definition: elog.c:575
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:32
int ClosePipeStream(FILE *file)
Definition: fd.c:2738
int errdetail_internal(const char *fmt,...)
Definition: elog.c:900
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
bool is_program
Definition: copy.c:116
#define Assert(condition)
Definition: c.h:699
int errmsg(const char *fmt,...)
Definition: elog.c:797
FILE * copy_file
Definition: copy.c:102

◆ copy_dest_destroy()

static void copy_dest_destroy ( DestReceiver self)
static

Definition at line 4840 of file copy.c.

References pfree().

Referenced by CreateCopyDestReceiver().

4841 {
4842  pfree(self);
4843 }
void pfree(void *pointer)
Definition: mcxt.c:1031

◆ copy_dest_receive()

static bool copy_dest_receive ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 4812 of file copy.c.

References CopyOneRowTo(), DR_copy::cstate, InvalidOid, DR_copy::processed, slot_getallattrs(), TupleTableSlot::tts_isnull, and TupleTableSlot::tts_values.

Referenced by CreateCopyDestReceiver().

4813 {
4814  DR_copy *myState = (DR_copy *) self;
4815  CopyState cstate = myState->cstate;
4816 
4817  /* Make sure the tuple is fully deconstructed */
4818  slot_getallattrs(slot);
4819 
4820  /* And send the data */
4821  CopyOneRowTo(cstate, InvalidOid, slot->tts_values, slot->tts_isnull);
4822  myState->processed++;
4823 
4824  return true;
4825 }
Definition: copy.c:216
CopyState cstate
Definition: copy.c:219
Datum * tts_values
Definition: tuptable.h:130
bool * tts_isnull
Definition: tuptable.h:132
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1612
#define InvalidOid
Definition: postgres_ext.h:36
static void CopyOneRowTo(CopyState cstate, Oid tupleOid, Datum *values, bool *nulls)
Definition: copy.c:2098
uint64 processed
Definition: copy.c:220

◆ copy_dest_shutdown()

static void copy_dest_shutdown ( DestReceiver self)
static

Definition at line 4831 of file copy.c.

Referenced by CreateCopyDestReceiver().

4832 {
4833  /* no-op */
4834 }

◆ copy_dest_startup()

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

Definition at line 4803 of file copy.c.

Referenced by CreateCopyDestReceiver().

4804 {
4805  /* no-op */
4806 }

◆ CopyAttributeOutCSV()

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

Definition at line 4639 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().

4641 {
4642  char *ptr;
4643  char *start;
4644  char c;
4645  char delimc = cstate->delim[0];
4646  char quotec = cstate->quote[0];
4647  char escapec = cstate->escape[0];
4648 
4649  /* force quoting if it matches null_print (before conversion!) */
4650  if (!use_quote && strcmp(string, cstate->null_print) == 0)
4651  use_quote = true;
4652 
4653  if (cstate->need_transcoding)
4654  ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
4655  else
4656  ptr = string;
4657 
4658  /*
4659  * Make a preliminary pass to discover if it needs quoting
4660  */
4661  if (!use_quote)
4662  {
4663  /*
4664  * Because '\.' can be a data value, quote it if it appears alone on a
4665  * line so it is not interpreted as the end-of-data marker.
4666  */
4667  if (single_attr && strcmp(ptr, "\\.") == 0)
4668  use_quote = true;
4669  else
4670  {
4671  char *tptr = ptr;
4672 
4673  while ((c = *tptr) != '\0')
4674  {
4675  if (c == delimc || c == quotec || c == '\n' || c == '\r')
4676  {
4677  use_quote = true;
4678  break;
4679  }
4680  if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
4681  tptr += pg_encoding_mblen(cstate->file_encoding, tptr);
4682  else
4683  tptr++;
4684  }
4685  }
4686  }
4687 
4688  if (use_quote)
4689  {
4690  CopySendChar(cstate, quotec);
4691 
4692  /*
4693  * We adopt the same optimization strategy as in CopyAttributeOutText
4694  */
4695  start = ptr;
4696  while ((c = *ptr) != '\0')
4697  {
4698  if (c == quotec || c == escapec)
4699  {
4700  DUMPSOFAR();
4701  CopySendChar(cstate, escapec);
4702  start = ptr; /* we include char in next run */
4703  }
4704  if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
4705  ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
4706  else
4707  ptr++;
4708  }
4709  DUMPSOFAR();
4710 
4711  CopySendChar(cstate, quotec);
4712  }
4713  else
4714  {
4715  /* If it doesn't need quoting, we can just dump it as-is */
4716  CopySendString(cstate, ptr);
4717  }
4718 }
bool need_transcoding
Definition: copy.c:108
char * delim
Definition: copy.c:126
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:634
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:458
char * null_print
Definition: copy.c:123
#define IS_HIGHBIT_SET(ch)
Definition: c.h:994
bool encoding_embeds_ascii
Definition: copy.c:109
char * c
char * quote
Definition: copy.c:127
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1785
char string[11]
Definition: preproc-type.c:46
char * escape
Definition: copy.c:128
int file_encoding
Definition: copy.c:107
static void CopySendString(CopyState cstate, const char *str)
Definition: copy.c:452
#define DUMPSOFAR()
Definition: copy.c:4479

◆ CopyAttributeOutText()

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

Definition at line 4486 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().

4487 {
4488  char *ptr;
4489  char *start;
4490  char c;
4491  char delimc = cstate->delim[0];
4492 
4493  if (cstate->need_transcoding)
4494  ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
4495  else
4496  ptr = string;
4497 
4498  /*
4499  * We have to grovel through the string searching for control characters
4500  * and instances of the delimiter character. In most cases, though, these
4501  * are infrequent. To avoid overhead from calling CopySendData once per
4502  * character, we dump out all characters between escaped characters in a
4503  * single call. The loop invariant is that the data from "start" to "ptr"
4504  * can be sent literally, but hasn't yet been.
4505  *
4506  * We can skip pg_encoding_mblen() overhead when encoding is safe, because
4507  * in valid backend encodings, extra bytes of a multibyte character never
4508  * look like ASCII. This loop is sufficiently performance-critical that
4509  * it's worth making two copies of it to get the IS_HIGHBIT_SET() test out
4510  * of the normal safe-encoding path.
4511  */
4512  if (cstate->encoding_embeds_ascii)
4513  {
4514  start = ptr;
4515  while ((c = *ptr) != '\0')
4516  {
4517  if ((unsigned char) c < (unsigned char) 0x20)
4518  {
4519  /*
4520  * \r and \n must be escaped, the others are traditional. We
4521  * prefer to dump these using the C-like notation, rather than
4522  * a backslash and the literal character, because it makes the
4523  * dump file a bit more proof against Microsoftish data
4524  * mangling.
4525  */
4526  switch (c)
4527  {
4528  case '\b':
4529  c = 'b';
4530  break;
4531  case '\f':
4532  c = 'f';
4533  break;
4534  case '\n':
4535  c = 'n';
4536  break;
4537  case '\r':
4538  c = 'r';
4539  break;
4540  case '\t':
4541  c = 't';
4542  break;
4543  case '\v':
4544  c = 'v';
4545  break;
4546  default:
4547  /* If it's the delimiter, must backslash it */
4548  if (c == delimc)
4549  break;
4550  /* All ASCII control chars are length 1 */
4551  ptr++;
4552  continue; /* fall to end of loop */
4553  }
4554  /* if we get here, we need to convert the control char */
4555  DUMPSOFAR();
4556  CopySendChar(cstate, '\\');
4557  CopySendChar(cstate, c);
4558  start = ++ptr; /* do not include char in next run */
4559  }
4560  else if (c == '\\' || c == delimc)
4561  {
4562  DUMPSOFAR();
4563  CopySendChar(cstate, '\\');
4564  start = ptr++; /* we include char in next run */
4565  }
4566  else if (IS_HIGHBIT_SET(c))
4567  ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
4568  else
4569  ptr++;
4570  }
4571  }
4572  else
4573  {
4574  start = ptr;
4575  while ((c = *ptr) != '\0')
4576  {
4577  if ((unsigned char) c < (unsigned char) 0x20)
4578  {
4579  /*
4580  * \r and \n must be escaped, the others are traditional. We
4581  * prefer to dump these using the C-like notation, rather than
4582  * a backslash and the literal character, because it makes the
4583  * dump file a bit more proof against Microsoftish data
4584  * mangling.
4585  */
4586  switch (c)
4587  {
4588  case '\b':
4589  c = 'b';
4590  break;
4591  case '\f':
4592  c = 'f';
4593  break;
4594  case '\n':
4595  c = 'n';
4596  break;
4597  case '\r':
4598  c = 'r';
4599  break;
4600  case '\t':
4601  c = 't';
4602  break;
4603  case '\v':
4604  c = 'v';
4605  break;
4606  default:
4607  /* If it's the delimiter, must backslash it */
4608  if (c == delimc)
4609  break;
4610  /* All ASCII control chars are length 1 */
4611  ptr++;
4612  continue; /* fall to end of loop */
4613  }
4614  /* if we get here, we need to convert the control char */
4615  DUMPSOFAR();
4616  CopySendChar(cstate, '\\');
4617  CopySendChar(cstate, c);
4618  start = ++ptr; /* do not include char in next run */
4619  }
4620  else if (c == '\\' || c == delimc)
4621  {
4622  DUMPSOFAR();
4623  CopySendChar(cstate, '\\');
4624  start = ptr++; /* we include char in next run */
4625  }
4626  else
4627  ptr++;
4628  }
4629  }
4630 
4631  DUMPSOFAR();
4632 }
bool need_transcoding
Definition: copy.c:108
char * delim
Definition: copy.c:126
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:634
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:458
#define IS_HIGHBIT_SET(ch)
Definition: c.h:994
bool encoding_embeds_ascii
Definition: copy.c:109
char * c
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1785
char string[11]
Definition: preproc-type.c:46
int file_encoding
Definition: copy.c:107
#define DUMPSOFAR()
Definition: copy.c:4479

◆ CopyFrom()

uint64 CopyFrom ( CopyState  cstate)

Definition at line 2297 of file copy.c.

References AfterTriggerBeginQuery(), AfterTriggerEndQuery(), ErrorContextCallback::arg, Assert, FdwRoutine::BeginForeignInsert, ErrorContextCallback::callback, CHECK_FOR_INTERRUPTS, CheckValidResultRel(), CMD_INSERT, tupleDesc::constr, ConvertPartitionTupleSlot(), CopyStateData::copy_dest, COPY_OLD_FE, CopyFromErrorCallback(), CopyFromInsertBatch(), CreateExecutorState(), CopyStateData::cur_lineno, CurrentMemoryContext, FdwRoutine::EndForeignInsert, ereport, errcode(), errhint(), errmsg(), ERROR, error_context_stack, EState::es_num_result_relations, EState::es_range_table, EState::es_result_relation_info, EState::es_result_relations, EState::es_trig_tuple_slot, EState::es_tupleTable, ExecARInsertTriggers(), ExecASInsertTriggers(), ExecBRInsertTriggers(), ExecBSInsertTriggers(), ExecCleanUpTriggerState(), ExecCleanupTupleRouting(), ExecCloseIndices(), ExecConstraints(), ExecFindPartition(), FdwRoutine::ExecForeignInsert, ExecInitExtraTupleSlot(), ExecInitPartitionInfo(), ExecInsertIndexTuples(), ExecIRInsertTriggers(), ExecMaterializeSlot(), ExecOpenIndices(), ExecResetTupleTable(), ExecSetupChildParentMapForLeaf(), ExecSetupPartitionTupleRouting(), ExecStoreTuple(), FreeBulkInsertState(), FreeExecutorState(), CopyStateData::freeze, GetBulkInsertState(), GetCurrentCommandId(), GetCurrentSubTransactionId(), GetPerTupleExprContext, GetPerTupleMemoryContext, heap_form_tuple(), heap_insert(), HEAP_INSERT_FROZEN, HEAP_INSERT_SKIP_FSM, HEAP_INSERT_SKIP_WAL, heap_sync(), HeapTupleSetOid, InitResultRelInfo(), InvalidateCatalogSnapshot(), InvalidBuffer, InvalidOid, InvalidSubTransactionId, list_free(), makeNode, MakeTransitionCaptureState(), MAX_BUFFERED_TUPLES, MemoryContextSwitchTo(), tupleDesc::natts, NextCopyFrom(), NIL, ModifyTableState::operation, palloc(), PartitionTupleRouting::parent_child_tupconv_maps, PartitionTupleRouting::partition_dispatch_info, CopyStateData::partition_tuple_routing, PartitionTupleRouting::partition_tuple_slot, PartitionTupleRouting::partitions, pfree(), PlanState::plan, pq_endmsgread(), ErrorContextCallback::previous, ModifyTableState::ps, CopyStateData::range_table, RelationData::rd_att, RelationData::rd_createSubid, RelationData::rd_newRelfilenodeSubid, RelationData::rd_rel, CopyStateData::rel, RelationGetDescr, RelationGetRelationName, RelationGetRelid, ReleaseBulkInsertStatePin(), ResetPerTupleExprContext, ModifyTableState::resultRelInfo, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_PartitionCheck, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, PlanState::state, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransitionCaptureState::tcs_map, TransitionCaptureState::tcs_original_insert_tuple, ThereAreNoPriorRegisteredSnapshots(), ThereAreNoReadyPortals(), CopyStateData::transition_capture, TriggerDesc::trig_insert_before_row, TriggerDesc::trig_insert_instead_row, RelationData::trigdesc, TupConvMapForLeaf(), values, CopyStateData::volatile_defexprs, and XLogIsNeeded.

Referenced by copy_table(), and DoCopy().

2298 {
2299  HeapTuple tuple;
2300  TupleDesc tupDesc;
2301  Datum *values;
2302  bool *nulls;
2303  ResultRelInfo *resultRelInfo;
2304  ResultRelInfo *saved_resultRelInfo = NULL;
2305  EState *estate = CreateExecutorState(); /* for ExecConstraints() */
2306  ModifyTableState *mtstate;
2307  ExprContext *econtext;
2308  TupleTableSlot *myslot;
2309  MemoryContext oldcontext = CurrentMemoryContext;
2310 
2311  ErrorContextCallback errcallback;
2312  CommandId mycid = GetCurrentCommandId(true);
2313  int hi_options = 0; /* start with default heap_insert options */
2314  BulkInsertState bistate;
2315  uint64 processed = 0;
2316  bool useHeapMultiInsert;
2317  int nBufferedTuples = 0;
2318  int prev_leaf_part_index = -1;
2319 
2320 #define MAX_BUFFERED_TUPLES 1000
2321  HeapTuple *bufferedTuples = NULL; /* initialize to silence warning */
2322  Size bufferedTuplesSize = 0;
2323  int firstBufferedLineNo = 0;
2324 
2325  Assert(cstate->rel);
2326 
2327  /*
2328  * The target must be a plain, foreign, or partitioned relation, or have
2329  * an INSTEAD OF INSERT row trigger. (Currently, such triggers are only
2330  * allowed on views, so we only hint about them in the view case.)
2331  */
2332  if (cstate->rel->rd_rel->relkind != RELKIND_RELATION &&
2333  cstate->rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
2334  cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
2335  !(cstate->rel->trigdesc &&
2337  {
2338  if (cstate->rel->rd_rel->relkind == RELKIND_VIEW)
2339  ereport(ERROR,
2340  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2341  errmsg("cannot copy to view \"%s\"",
2342  RelationGetRelationName(cstate->rel)),
2343  errhint("To enable copying to a view, provide an INSTEAD OF INSERT trigger.")));
2344  else if (cstate->rel->rd_rel->relkind == RELKIND_MATVIEW)
2345  ereport(ERROR,
2346  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2347  errmsg("cannot copy to materialized view \"%s\"",
2348  RelationGetRelationName(cstate->rel))));
2349  else if (cstate->rel->rd_rel->relkind == RELKIND_SEQUENCE)
2350  ereport(ERROR,
2351  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2352  errmsg("cannot copy to sequence \"%s\"",
2353  RelationGetRelationName(cstate->rel))));
2354  else
2355  ereport(ERROR,
2356  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2357  errmsg("cannot copy to non-table relation \"%s\"",
2358  RelationGetRelationName(cstate->rel))));
2359  }
2360 
2361  tupDesc = RelationGetDescr(cstate->rel);
2362 
2363  /*----------
2364  * Check to see if we can avoid writing WAL
2365  *
2366  * If archive logging/streaming is not enabled *and* either
2367  * - table was created in same transaction as this COPY
2368  * - data is being written to relfilenode created in this transaction
2369  * then we can skip writing WAL. It's safe because if the transaction
2370  * doesn't commit, we'll discard the table (or the new relfilenode file).
2371  * If it does commit, we'll have done the heap_sync at the bottom of this
2372  * routine first.
2373  *
2374  * As mentioned in comments in utils/rel.h, the in-same-transaction test
2375  * is not always set correctly, since in rare cases rd_newRelfilenodeSubid
2376  * can be cleared before the end of the transaction. The exact case is
2377  * when a relation sets a new relfilenode twice in same transaction, yet
2378  * the second one fails in an aborted subtransaction, e.g.
2379  *
2380  * BEGIN;
2381  * TRUNCATE t;
2382  * SAVEPOINT save;
2383  * TRUNCATE t;
2384  * ROLLBACK TO save;
2385  * COPY ...
2386  *
2387  * Also, if the target file is new-in-transaction, we assume that checking
2388  * FSM for free space is a waste of time, even if we must use WAL because
2389  * of archiving. This could possibly be wrong, but it's unlikely.
2390  *
2391  * The comments for heap_insert and RelationGetBufferForTuple specify that
2392  * skipping WAL logging is only safe if we ensure that our tuples do not
2393  * go into pages containing tuples from any other transactions --- but this
2394  * must be the case if we have a new table or new relfilenode, so we need
2395  * no additional work to enforce that.
2396  *----------
2397  */
2398  /* createSubid is creation check, newRelfilenodeSubid is truncation check */
2399  if (cstate->rel->rd_createSubid != InvalidSubTransactionId ||
2401  {
2402  hi_options |= HEAP_INSERT_SKIP_FSM;
2403  if (!XLogIsNeeded())
2404  hi_options |= HEAP_INSERT_SKIP_WAL;
2405  }
2406 
2407  /*
2408  * Optimize if new relfilenode was created in this subxact or one of its
2409  * committed children and we won't see those rows later as part of an
2410  * earlier scan or command. The subxact test ensures that if this subxact
2411  * aborts then the frozen rows won't be visible after xact cleanup. Note
2412  * that the stronger test of exactly which subtransaction created it is
2413  * crucial for correctness of this optimization. The test for an earlier
2414  * scan or command tolerates false negatives. FREEZE causes other sessions
2415  * to see rows they would not see under MVCC, and a false negative merely
2416  * spreads that anomaly to the current session.
2417  */
2418  if (cstate->freeze)
2419  {
2420  /*
2421  * Tolerate one registration for the benefit of FirstXactSnapshot.
2422  * Scan-bearing queries generally create at least two registrations,
2423  * though relying on that is fragile, as is ignoring ActiveSnapshot.
2424  * Clear CatalogSnapshot to avoid counting its registration. We'll
2425  * still detect ongoing catalog scans, each of which separately
2426  * registers the snapshot it uses.
2427  */
2430  ereport(ERROR,
2431  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
2432  errmsg("cannot perform FREEZE because of prior transaction activity")));
2433 
2434  if (cstate->rel->rd_createSubid != GetCurrentSubTransactionId() &&
2436  ereport(ERROR,
2437  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2438  errmsg("cannot perform FREEZE because the table was not created or truncated in the current subtransaction")));
2439 
2440  hi_options |= HEAP_INSERT_FROZEN;
2441  }
2442 
2443  /*
2444  * We need a ResultRelInfo so we can use the regular executor's
2445  * index-entry-making machinery. (There used to be a huge amount of code
2446  * here that basically duplicated execUtils.c ...)
2447  */
2448  resultRelInfo = makeNode(ResultRelInfo);
2449  InitResultRelInfo(resultRelInfo,
2450  cstate->rel,
2451  1, /* dummy rangetable index */
2452  NULL,
2453  0);
2454 
2455  /* Verify the named relation is a valid target for INSERT */
2456  CheckValidResultRel(resultRelInfo, CMD_INSERT);
2457 
2458  ExecOpenIndices(resultRelInfo, false);
2459 
2460  estate->es_result_relations = resultRelInfo;
2461  estate->es_num_result_relations = 1;
2462  estate->es_result_relation_info = resultRelInfo;
2463  estate->es_range_table = cstate->range_table;
2464 
2465  /* Set up a tuple slot too */
2466  myslot = ExecInitExtraTupleSlot(estate, tupDesc);
2467  /* Triggers might need a slot as well */
2468  estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate, NULL);
2469 
2470  /*
2471  * Set up a ModifyTableState so we can let FDW(s) init themselves for
2472  * foreign-table result relation(s).
2473  */
2474  mtstate = makeNode(ModifyTableState);
2475  mtstate->ps.plan = NULL;
2476  mtstate->ps.state = estate;
2477  mtstate->operation = CMD_INSERT;
2478  mtstate->resultRelInfo = estate->es_result_relations;
2479 
2480  if (resultRelInfo->ri_FdwRoutine != NULL &&
2481  resultRelInfo->ri_FdwRoutine->BeginForeignInsert != NULL)
2482  resultRelInfo->ri_FdwRoutine->BeginForeignInsert(mtstate,
2483  resultRelInfo);
2484 
2485  /* Prepare to catch AFTER triggers. */
2487 
2488  /*
2489  * If there are any triggers with transition tables on the named relation,
2490  * we need to be prepared to capture transition tuples.
2491  */
2492  cstate->transition_capture =
2494  RelationGetRelid(cstate->rel),
2495  CMD_INSERT);
2496 
2497  /*
2498  * If the named relation is a partitioned table, initialize state for
2499  * CopyFrom tuple routing.
2500  */
2501  if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2502  {
2503  PartitionTupleRouting *proute;
2504 
2505  proute = cstate->partition_tuple_routing =
2506  ExecSetupPartitionTupleRouting(NULL, cstate->rel);
2507 
2508  /*
2509  * If we are capturing transition tuples, they may need to be
2510  * converted from partition format back to partitioned table format
2511  * (this is only ever necessary if a BEFORE trigger modifies the
2512  * tuple).
2513  */
2514  if (cstate->transition_capture != NULL)
2516  }
2517 
2518  /*
2519  * It's more efficient to prepare a bunch of tuples for insertion, and
2520  * insert them in one heap_multi_insert() call, than call heap_insert()
2521  * separately for every tuple. However, we can't do that if there are
2522  * BEFORE/INSTEAD OF triggers, or we need to evaluate volatile default
2523  * expressions. Such triggers or expressions might query the table we're
2524  * inserting to, and act differently if the tuples that have already been
2525  * processed and prepared for insertion are not there. We also can't do
2526  * it if the table is foreign or partitioned.
2527  */
2528  if ((resultRelInfo->ri_TrigDesc != NULL &&
2529  (resultRelInfo->ri_TrigDesc->trig_insert_before_row ||
2530  resultRelInfo->ri_TrigDesc->trig_insert_instead_row)) ||
2531  resultRelInfo->ri_FdwRoutine != NULL ||
2532  cstate->partition_tuple_routing != NULL ||
2533  cstate->volatile_defexprs)
2534  {
2535  useHeapMultiInsert = false;
2536  }
2537  else
2538  {
2539  useHeapMultiInsert = true;
2540  bufferedTuples = palloc(MAX_BUFFERED_TUPLES * sizeof(HeapTuple));
2541  }
2542 
2543  /*
2544  * Check BEFORE STATEMENT insertion triggers. It's debatable whether we
2545  * should do this for COPY, since it's not really an "INSERT" statement as
2546  * such. However, executing these triggers maintains consistency with the
2547  * EACH ROW triggers that we already fire on COPY.
2548  */
2549  ExecBSInsertTriggers(estate, resultRelInfo);
2550 
2551  values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
2552  nulls = (bool *) palloc(tupDesc->natts * sizeof(bool));
2553 
2554  bistate = GetBulkInsertState();
2555  econtext = GetPerTupleExprContext(estate);
2556 
2557  /* Set up callback to identify error line number */
2558  errcallback.callback = CopyFromErrorCallback;
2559  errcallback.arg = (void *) cstate;
2560  errcallback.previous = error_context_stack;
2561  error_context_stack = &errcallback;
2562 
2563  for (;;)
2564  {
2565  TupleTableSlot *slot;
2566  bool skip_tuple;
2567  Oid loaded_oid = InvalidOid;
2568 
2570 
2571  if (nBufferedTuples == 0)
2572  {
2573  /*
2574  * Reset the per-tuple exprcontext. We can only do this if the
2575  * tuple buffer is empty. (Calling the context the per-tuple
2576  * memory context is a bit of a misnomer now.)
2577  */
2578  ResetPerTupleExprContext(estate);
2579  }
2580 
2581  /* Switch into its memory context */
2583 
2584  if (!NextCopyFrom(cstate, econtext, values, nulls, &loaded_oid))
2585  break;
2586 
2587  /* And now we can form the input tuple. */
2588  tuple = heap_form_tuple(tupDesc, values, nulls);
2589 
2590  if (loaded_oid != InvalidOid)
2591  HeapTupleSetOid(tuple, loaded_oid);
2592 
2593  /*
2594  * Constraints might reference the tableoid column, so initialize
2595  * t_tableOid before evaluating them.
2596  */
2597  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
2598 
2599  /* Triggers and stuff need to be invoked in query context. */
2600  MemoryContextSwitchTo(oldcontext);
2601 
2602  /* Place tuple in tuple slot --- but slot shouldn't free it */
2603  slot = myslot;
2604  ExecStoreTuple(tuple, slot, InvalidBuffer, false);
2605 
2606  /* Determine the partition to heap_insert the tuple into */
2607  if (cstate->partition_tuple_routing)
2608  {
2609  int leaf_part_index;
2611 
2612  /*
2613  * Away we go ... If we end up not finding a partition after all,
2614  * ExecFindPartition() does not return and errors out instead.
2615  * Otherwise, the returned value is to be used as an index into
2616  * arrays mt_partitions[] and mt_partition_tupconv_maps[] that
2617  * will get us the ResultRelInfo and TupleConversionMap for the
2618  * partition, respectively.
2619  */
2620  leaf_part_index = ExecFindPartition(resultRelInfo,
2621  proute->partition_dispatch_info,
2622  slot,
2623  estate);
2624  Assert(leaf_part_index >= 0 &&
2625  leaf_part_index < proute->num_partitions);
2626 
2627  /*
2628  * If this tuple is mapped to a partition that is not same as the
2629  * previous one, we'd better make the bulk insert mechanism gets a
2630  * new buffer.
2631  */
2632  if (prev_leaf_part_index != leaf_part_index)
2633  {
2634  ReleaseBulkInsertStatePin(bistate);
2635  prev_leaf_part_index = leaf_part_index;
2636  }
2637 
2638  /*
2639  * Save the old ResultRelInfo and switch to the one corresponding
2640  * to the selected partition.
2641  */
2642  saved_resultRelInfo = resultRelInfo;
2643  resultRelInfo = proute->partitions[leaf_part_index];
2644  if (resultRelInfo == NULL)
2645  {
2646  resultRelInfo = ExecInitPartitionInfo(mtstate,
2647  saved_resultRelInfo,
2648  proute, estate,
2649  leaf_part_index);
2650  Assert(resultRelInfo != NULL);
2651  }
2652 
2653  /*
2654  * For ExecInsertIndexTuples() to work on the partition's indexes
2655  */
2656  estate->es_result_relation_info = resultRelInfo;
2657 
2658  /*
2659  * If we're capturing transition tuples, we might need to convert
2660  * from the partition rowtype to parent rowtype.
2661  */
2662  if (cstate->transition_capture != NULL)
2663  {
2664  if (resultRelInfo->ri_TrigDesc &&
2665  resultRelInfo->ri_TrigDesc->trig_insert_before_row)
2666  {
2667  /*
2668  * If there are any BEFORE triggers on the partition,
2669  * we'll have to be ready to convert their result back to
2670  * tuplestore format.
2671  */
2673  cstate->transition_capture->tcs_map =
2674  TupConvMapForLeaf(proute, saved_resultRelInfo,
2675  leaf_part_index);
2676  }
2677  else
2678  {
2679  /*
2680  * Otherwise, just remember the original unconverted
2681  * tuple, to avoid a needless round trip conversion.
2682  */
2684  cstate->transition_capture->tcs_map = NULL;
2685  }
2686  }
2687 
2688  /*
2689  * We might need to convert from the parent rowtype to the
2690  * partition rowtype.
2691  */
2692  tuple = ConvertPartitionTupleSlot(proute->parent_child_tupconv_maps[leaf_part_index],
2693  tuple,
2694  proute->partition_tuple_slot,
2695  &slot);
2696 
2697  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
2698  }
2699 
2700  skip_tuple = false;
2701 
2702  /* BEFORE ROW INSERT Triggers */
2703  if (resultRelInfo->ri_TrigDesc &&
2704  resultRelInfo->ri_TrigDesc->trig_insert_before_row)
2705  {
2706  slot = ExecBRInsertTriggers(estate, resultRelInfo, slot);
2707 
2708  if (slot == NULL) /* "do nothing" */
2709  skip_tuple = true;
2710  else /* trigger might have changed tuple */
2711  tuple = ExecMaterializeSlot(slot);
2712  }
2713 
2714  if (!skip_tuple)
2715  {
2716  if (resultRelInfo->ri_TrigDesc &&
2717  resultRelInfo->ri_TrigDesc->trig_insert_instead_row)
2718  {
2719  /* Pass the data to the INSTEAD ROW INSERT trigger */
2720  ExecIRInsertTriggers(estate, resultRelInfo, slot);
2721  }
2722  else
2723  {
2724  /*
2725  * We always check the partition constraint, including when
2726  * the tuple got here via tuple-routing. However we don't
2727  * need to in the latter case if no BR trigger is defined on
2728  * the partition. Note that a BR trigger might modify the
2729  * tuple such that the partition constraint is no longer
2730  * satisfied, so we need to check in that case.
2731  */
2732  bool check_partition_constr =
2733  (resultRelInfo->ri_PartitionCheck != NIL);
2734 
2735  if (saved_resultRelInfo != NULL &&
2736  !(resultRelInfo->ri_TrigDesc &&
2737  resultRelInfo->ri_TrigDesc->trig_insert_before_row))
2738  check_partition_constr = false;
2739 
2740  /*
2741  * If the target is a plain table, check the constraints of
2742  * the tuple.
2743  */
2744  if (resultRelInfo->ri_FdwRoutine == NULL &&
2745  (resultRelInfo->ri_RelationDesc->rd_att->constr ||
2746  check_partition_constr))
2747  ExecConstraints(resultRelInfo, slot, estate, true);
2748 
2749  if (useHeapMultiInsert)
2750  {
2751  /* Add this tuple to the tuple buffer */
2752  if (nBufferedTuples == 0)
2753  firstBufferedLineNo = cstate->cur_lineno;
2754  bufferedTuples[nBufferedTuples++] = tuple;
2755  bufferedTuplesSize += tuple->t_len;
2756 
2757  /*
2758  * If the buffer filled up, flush it. Also flush if the
2759  * total size of all the tuples in the buffer becomes
2760  * large, to avoid using large amounts of memory for the
2761  * buffer when the tuples are exceptionally wide.
2762  */
2763  if (nBufferedTuples == MAX_BUFFERED_TUPLES ||
2764  bufferedTuplesSize > 65535)
2765  {
2766  CopyFromInsertBatch(cstate, estate, mycid, hi_options,
2767  resultRelInfo, myslot, bistate,
2768  nBufferedTuples, bufferedTuples,
2769  firstBufferedLineNo);
2770  nBufferedTuples = 0;
2771  bufferedTuplesSize = 0;
2772  }
2773  }
2774  else
2775  {
2776  List *recheckIndexes = NIL;
2777 
2778  /* OK, store the tuple */
2779  if (resultRelInfo->ri_FdwRoutine != NULL)
2780  {
2781  slot = resultRelInfo->ri_FdwRoutine->ExecForeignInsert(estate,
2782  resultRelInfo,
2783  slot,
2784  NULL);
2785 
2786  if (slot == NULL) /* "do nothing" */
2787  goto next_tuple;
2788 
2789  /* FDW might have changed tuple */
2790  tuple = ExecMaterializeSlot(slot);
2791 
2792  /*
2793  * AFTER ROW Triggers might reference the tableoid
2794  * column, so initialize t_tableOid before evaluating
2795  * them.
2796  */
2797  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
2798  }
2799  else
2800  heap_insert(resultRelInfo->ri_RelationDesc, tuple,
2801  mycid, hi_options, bistate);
2802 
2803  /* And create index entries for it */
2804  if (resultRelInfo->ri_NumIndices > 0)
2805  recheckIndexes = ExecInsertIndexTuples(slot,
2806  &(tuple->t_self),
2807  estate,
2808  false,
2809  NULL,
2810  NIL);
2811 
2812  /* AFTER ROW INSERT Triggers */
2813  ExecARInsertTriggers(estate, resultRelInfo, tuple,
2814  recheckIndexes, cstate->transition_capture);
2815 
2816  list_free(recheckIndexes);
2817  }
2818  }
2819 
2820  /*
2821  * We count only tuples not suppressed by a BEFORE INSERT trigger
2822  * or FDW; this is the same definition used by nodeModifyTable.c
2823  * for counting tuples inserted by an INSERT command.
2824  */
2825  processed++;
2826  }
2827 
2828 next_tuple:
2829  /* Restore the saved ResultRelInfo */
2830  if (saved_resultRelInfo)
2831  {
2832  resultRelInfo = saved_resultRelInfo;
2833  estate->es_result_relation_info = resultRelInfo;
2834  }
2835  }
2836 
2837  /* Flush any remaining buffered tuples */
2838  if (nBufferedTuples > 0)
2839  CopyFromInsertBatch(cstate, estate, mycid, hi_options,
2840  resultRelInfo, myslot, bistate,
2841  nBufferedTuples, bufferedTuples,
2842  firstBufferedLineNo);
2843 
2844  /* Done, clean up */
2845  error_context_stack = errcallback.previous;
2846 
2847  FreeBulkInsertState(bistate);
2848 
2849  MemoryContextSwitchTo(oldcontext);
2850 
2851  /*
2852  * In the old protocol, tell pqcomm that we can process normal protocol
2853  * messages again.
2854  */
2855  if (cstate->copy_dest == COPY_OLD_FE)
2856  pq_endmsgread();
2857 
2858  /* Execute AFTER STATEMENT insertion triggers */
2859  ExecASInsertTriggers(estate, resultRelInfo, cstate->transition_capture);
2860 
2861  /* Handle queued AFTER triggers */
2862  AfterTriggerEndQuery(estate);
2863 
2864  pfree(values);
2865  pfree(nulls);
2866 
2867  ExecResetTupleTable(estate->es_tupleTable, false);
2868 
2869  /* Allow the FDW to shut down */
2870  if (resultRelInfo->ri_FdwRoutine != NULL &&
2871  resultRelInfo->ri_FdwRoutine->EndForeignInsert != NULL)
2872  resultRelInfo->ri_FdwRoutine->EndForeignInsert(estate,
2873  resultRelInfo);
2874 
2875  ExecCloseIndices(resultRelInfo);
2876 
2877  /* Close all the partitioned tables, leaf partitions, and their indices */
2878  if (cstate->partition_tuple_routing)
2880 
2881  /* Close any trigger target relations */
2882  ExecCleanUpTriggerState(estate);
2883 
2884  FreeExecutorState(estate);
2885 
2886  /*
2887  * If we skipped writing WAL, then we need to sync the heap (but not
2888  * indexes since those use WAL anyway)
2889  */
2890  if (hi_options & HEAP_INSERT_SKIP_WAL)
2891  heap_sync(cstate->rel);
2892 
2893  return processed;
2894 }
int ri_NumIndices
Definition: execnodes.h:400
#define NIL
Definition: pg_list.h:69
uint32 CommandId
Definition: c.h:488
int ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd, TupleTableSlot *slot, EState *estate)
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:356
void InitResultRelInfo(ResultRelInfo *resultRelInfo, Relation resultRelationDesc, Index resultRelationIndex, Relation partition_root, int instrument_options)
Definition: execMain.c:1305
Relation ri_RelationDesc
Definition: execnodes.h:397
List * range_table
Definition: copy.c:169
int errhint(const char *fmt,...)
Definition: elog.c:987
void CopyFromErrorCallback(void *arg)
Definition: copy.c:2192
TupleConversionMap * TupConvMapForLeaf(PartitionTupleRouting *proute, ResultRelInfo *rootRelInfo, int leaf_index)
List * ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid, EState *estate, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:271
#define ResetPerTupleExprContext(estate)
Definition: executor.h:498
#define RelationGetDescr(relation)
Definition: rel.h:433
#define HEAP_INSERT_FROZEN
Definition: heapam.h:30
BeginForeignInsert_function BeginForeignInsert
Definition: fdwapi.h:214
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1043
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:210
#define XLogIsNeeded()
Definition: xlog.h:146
#define MAX_BUFFERED_TUPLES
TupleTableSlot * ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2596
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define InvalidBuffer
Definition: buf.h:25
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1655
int errcode(int sqlerrcode)
Definition: elog.c:575
CmdType operation
Definition: execnodes.h:1037
void ExecSetupChildParentMapForLeaf(PartitionTupleRouting *proute)
SubTransactionId rd_newRelfilenodeSubid
Definition: rel.h:81
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2581
void heap_sync(Relation rel)
Definition: heapam.c:9370
#define HEAP_INSERT_SKIP_WAL
Definition: heapam.h:28
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
EState * state
Definition: execnodes.h:914
TupleTableSlot * partition_tuple_slot
List * es_range_table
Definition: execnodes.h:480
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool check_partition_constraint)
Definition: execMain.c:1962
bool volatile_defexprs
Definition: copy.c:168
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
int natts
Definition: tupdesc.h:82
HeapTuple tcs_original_insert_tuple
Definition: trigger.h:82
ResultRelInfo ** partitions
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc)
Definition: execTuples.c:931
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:149
PartitionTupleRouting * ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, Relation rel)
Definition: execPartition.c:76
CopyDest copy_dest
Definition: copy.c:101
#define HeapTupleSetOid(tuple, oid)
Definition: htup_details.h:710
ErrorContextCallback * error_context_stack
Definition: elog.c:88
bool trig_insert_instead_row
Definition: reltrigger.h:57
void FreeExecutorState(EState *estate)
Definition: execUtils.c:188
Relation rel
Definition: copy.c:112
#define GetPerTupleExprContext(estate)
Definition: executor.h:489
BulkInsertState GetBulkInsertState(void)
Definition: heapam.c:2364
void pfree(void *pointer)
Definition: mcxt.c:1031
bool ThereAreNoReadyPortals(void)
Definition: portalmem.c:1198
#define ERROR
Definition: elog.h:43
PlanState ps
Definition: execnodes.h:1036
void ExecCleanupTupleRouting(ModifyTableState *mtstate, PartitionTupleRouting *proute)
static void CopyFromInsertBatch(CopyState cstate, EState *estate, CommandId mycid, int hi_options, ResultRelInfo *resultRelInfo, TupleTableSlot *myslot, BulkInsertState bistate, int nBufferedTuples, HeapTuple *bufferedTuples, int firstBufferedLineNo)
Definition: copy.c:2902
TupleConversionMap * tcs_map
Definition: trigger.h:73
ItemPointerData t_self
Definition: htup.h:65
TriggerDesc * trigdesc
Definition: rel.h:90
void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
Definition: execMain.c:1104
uint32 t_len
Definition: htup.h:64
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2447
ResultRelInfo * es_result_relations
Definition: execnodes.h:490
#define RelationGetRelationName(relation)
Definition: rel.h:441
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:421
Oid t_tableOid
Definition: htup.h:66
PartitionDispatch * partition_dispatch_info
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TupleTableSlot * es_trig_tuple_slot
Definition: execnodes.h:512
#define ereport(elevel, rest)
Definition: elog.h:122
Oid heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2441
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:510
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:409
EState * CreateExecutorState(void)
Definition: execUtils.c:80
SubTransactionId rd_createSubid
Definition: rel.h:80
bool trig_insert_before_row
Definition: reltrigger.h:55
List * es_tupleTable
Definition: execnodes.h:525
void ExecResetTupleTable(List *tupleTable, bool shouldFree)
Definition: execTuples.c:186
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2504
uintptr_t Datum
Definition: postgres.h:367
HeapTuple ConvertPartitionTupleSlot(TupleConversionMap *map, HeapTuple tuple, TupleTableSlot *new_slot, TupleTableSlot **p_my_slot)
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4644
int es_num_result_relations
Definition: execnodes.h:491
List * ri_PartitionCheck
Definition: execnodes.h:454
TupleDesc rd_att
Definition: rel.h:85
bool freeze
Definition: copy.c:120
Plan * plan
Definition: execnodes.h:912
void pq_endmsgread(void)
Definition: pqcomm.c:1234
#define InvalidOid
Definition: postgres_ext.h:36
PartitionTupleRouting * partition_tuple_routing
Definition: copy.c:172
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4766
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
void FreeBulkInsertState(BulkInsertState bistate)
Definition: heapam.c:2378
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:641
TupleConstr * constr
Definition: tupdesc.h:87
size_t Size
Definition: c.h:433
#define InvalidSubTransactionId
Definition: c.h:480
HeapTuple ExecMaterializeSlot(TupleTableSlot *slot)
Definition: execTuples.c:781
void ReleaseBulkInsertStatePin(BulkInsertState bistate)
Definition: heapam.c:2390
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:494
TupleConversionMap ** parent_child_tupconv_maps
void ExecCleanUpTriggerState(EState *estate)
Definition: execMain.c:1474
#define HEAP_INSERT_SKIP_FSM
Definition: heapam.h:29
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4786
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
void list_free(List *list)
Definition: list.c:1133
bool NextCopyFrom(CopyState cstate, ExprContext *econtext, Datum *values, bool *nulls, Oid *tupleOid)
Definition: copy.c:3297
int cur_lineno
Definition: copy.c:142
TransitionCaptureState * transition_capture
Definition: copy.c:174
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
TupleTableSlot * ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2515
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:679
ResultRelInfo * ExecInitPartitionInfo(ModifyTableState *mtstate, ResultRelInfo *resultRelInfo, PartitionTupleRouting *proute, EState *estate, int partidx)
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:407
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:224
EndForeignInsert_function EndForeignInsert
Definition: fdwapi.h:215
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:492

◆ CopyFromErrorCallback()

void CopyFromErrorCallback ( void *  arg)

Definition at line 2192 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, and pfree().

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

2193 {
2194  CopyState cstate = (CopyState) arg;
2195 
2196  if (cstate->binary)
2197  {
2198  /* can't usefully display the data */
2199  if (cstate->cur_attname)
2200  errcontext("COPY %s, line %d, column %s",
2201  cstate->cur_relname, cstate->cur_lineno,
2202  cstate->cur_attname);
2203  else
2204  errcontext("COPY %s, line %d",
2205  cstate->cur_relname, cstate->cur_lineno);
2206  }
2207  else
2208  {
2209  if (cstate->cur_attname && cstate->cur_attval)
2210  {
2211  /* error is relevant to a particular column */
2212  char *attval;
2213 
2214  attval = limit_printout_length(cstate->cur_attval);
2215  errcontext("COPY %s, line %d, column %s: \"%s\"",
2216  cstate->cur_relname, cstate->cur_lineno,
2217  cstate->cur_attname, attval);
2218  pfree(attval);
2219  }
2220  else if (cstate->cur_attname)
2221  {
2222  /* error is relevant to a particular column, value is NULL */
2223  errcontext("COPY %s, line %d, column %s: null input",
2224  cstate->cur_relname, cstate->cur_lineno,
2225  cstate->cur_attname);
2226  }
2227  else
2228  {
2229  /*
2230  * Error is relevant to a particular line.
2231  *
2232  * If line_buf still contains the correct line, and it's already
2233  * transcoded, print it. If it's still in a foreign encoding, it's
2234  * quite likely that the error is precisely a failure to do
2235  * encoding conversion (ie, bad data). We dare not try to convert
2236  * it, and at present there's no way to regurgitate it without
2237  * conversion. So we have to punt and just report the line number.
2238  */
2239  if (cstate->line_buf_valid &&
2240  (cstate->line_buf_converted || !cstate->need_transcoding))
2241  {
2242  char *lineval;
2243 
2244  lineval = limit_printout_length(cstate->line_buf.data);
2245  errcontext("COPY %s, line %d: \"%s\"",
2246  cstate->cur_relname, cstate->cur_lineno, lineval);
2247  pfree(lineval);
2248  }
2249  else
2250  {
2251  errcontext("COPY %s, line %d",
2252  cstate->cur_relname, cstate->cur_lineno);
2253  }
2254  }
2255  }
2256 }
bool binary
Definition: copy.c:118
bool need_transcoding
Definition: copy.c:108
StringInfoData line_buf
Definition: copy.c:198
bool line_buf_valid
Definition: copy.c:200
bool line_buf_converted
Definition: copy.c:199
const char * cur_attname
Definition: copy.c:143
void pfree(void *pointer)
Definition: mcxt.c:1031
static char * limit_printout_length(const char *str)
Definition: copy.c:2268
const char * cur_relname
Definition: copy.c:141
#define errcontext
Definition: elog.h:164
struct CopyStateData * CopyState
Definition: copy.h:23
void * arg
int cur_lineno
Definition: copy.c:142
const char * cur_attval
Definition: copy.c:144

◆ CopyFromInsertBatch()

static void CopyFromInsertBatch ( CopyState  cstate,
EState estate,
CommandId  mycid,
int  hi_options,
ResultRelInfo resultRelInfo,
TupleTableSlot myslot,
BulkInsertState  bistate,
int  nBufferedTuples,
HeapTuple bufferedTuples,
int  firstBufferedLineNo 
)
static

Definition at line 2902 of file copy.c.

References CopyStateData::cur_lineno, ExecARInsertTriggers(), ExecInsertIndexTuples(), ExecStoreTuple(), GetPerTupleMemoryContext, heap_multi_insert(), i, InvalidBuffer, CopyStateData::line_buf_valid, list_free(), MemoryContextSwitchTo(), NIL, CopyStateData::rel, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_TrigDesc, CopyStateData::transition_capture, TriggerDesc::trig_insert_after_row, and TriggerDesc::trig_insert_new_table.

Referenced by CopyFrom().

2907 {
2908  MemoryContext oldcontext;
2909  int i;
2910  int save_cur_lineno;
2911 
2912  /*
2913  * Print error context information correctly, if one of the operations
2914  * below fail.
2915  */
2916  cstate->line_buf_valid = false;
2917  save_cur_lineno = cstate->cur_lineno;
2918 
2919  /*
2920  * heap_multi_insert leaks memory, so switch to short-lived memory context
2921  * before calling it.
2922  */
2923  oldcontext = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2924  heap_multi_insert(cstate->rel,
2925  bufferedTuples,
2926  nBufferedTuples,
2927  mycid,
2928  hi_options,
2929  bistate);
2930  MemoryContextSwitchTo(oldcontext);
2931 
2932  /*
2933  * If there are any indexes, update them for all the inserted tuples, and
2934  * run AFTER ROW INSERT triggers.
2935  */
2936  if (resultRelInfo->ri_NumIndices > 0)
2937  {
2938  for (i = 0; i < nBufferedTuples; i++)
2939  {
2940  List *recheckIndexes;
2941 
2942  cstate->cur_lineno = firstBufferedLineNo + i;
2943  ExecStoreTuple(bufferedTuples[i], myslot, InvalidBuffer, false);
2944  recheckIndexes =
2945  ExecInsertIndexTuples(myslot, &(bufferedTuples[i]->t_self),
2946  estate, false, NULL, NIL);
2947  ExecARInsertTriggers(estate, resultRelInfo,
2948  bufferedTuples[i],
2949  recheckIndexes, cstate->transition_capture);
2950  list_free(recheckIndexes);
2951  }
2952  }
2953 
2954  /*
2955  * There's no indexes, but see if we need to run AFTER ROW INSERT triggers
2956  * anyway.
2957  */
2958  else if (resultRelInfo->ri_TrigDesc != NULL &&
2959  (resultRelInfo->ri_TrigDesc->trig_insert_after_row ||
2960  resultRelInfo->ri_TrigDesc->trig_insert_new_table))
2961  {
2962  for (i = 0; i < nBufferedTuples; i++)
2963  {
2964  cstate->cur_lineno = firstBufferedLineNo + i;
2965  ExecARInsertTriggers(estate, resultRelInfo,
2966  bufferedTuples[i],
2967  NIL, cstate->transition_capture);
2968  }
2969  }
2970 
2971  /* reset cur_lineno to where we were */
2972  cstate->cur_lineno = save_cur_lineno;
2973 }
int ri_NumIndices
Definition: execnodes.h:400
#define NIL
Definition: pg_list.h:69
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:356
List * ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid, EState *estate, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:271
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define InvalidBuffer
Definition: buf.h:25
bool line_buf_valid
Definition: copy.c:200
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2581
Relation rel
Definition: copy.c:112
bool trig_insert_new_table
Definition: reltrigger.h:74
bool trig_insert_after_row
Definition: reltrigger.h:56
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:409
void heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2705
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:494
void list_free(List *list)
Definition: list.c:1133
int i
int cur_lineno
Definition: copy.c:142
TransitionCaptureState * transition_capture
Definition: copy.c:174
Definition: pg_list.h:45

◆ CopyGetAttnums()

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

Definition at line 4730 of file copy.c.

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

Referenced by BeginCopy(), and DoCopy().

4731 {
4732  List *attnums = NIL;
4733 
4734  if (attnamelist == NIL)
4735  {
4736  /* Generate default column list */
4737  int attr_count = tupDesc->natts;
4738  int i;
4739 
4740  for (i = 0; i < attr_count; i++)
4741  {
4742  if (TupleDescAttr(tupDesc, i)->attisdropped)
4743  continue;
4744  attnums = lappend_int(attnums, i + 1);
4745  }
4746  }
4747  else
4748  {
4749  /* Validate the user-supplied list and extract attnums */
4750  ListCell *l;
4751 
4752  foreach(l, attnamelist)
4753  {
4754  char *name = strVal(lfirst(l));
4755  int attnum;
4756  int i;
4757 
4758  /* Lookup column name */
4759  attnum = InvalidAttrNumber;
4760  for (i = 0; i < tupDesc->natts; i++)
4761  {
4762  Form_pg_attribute att = TupleDescAttr(tupDesc, i);
4763 
4764  if (att->attisdropped)
4765  continue;
4766  if (namestrcmp(&(att->attname), name) == 0)
4767  {
4768  attnum = att->attnum;
4769  break;
4770  }
4771  }
4772  if (attnum == InvalidAttrNumber)
4773  {
4774  if (rel != NULL)
4775  ereport(ERROR,
4776  (errcode(ERRCODE_UNDEFINED_COLUMN),
4777  errmsg("column \"%s\" of relation \"%s\" does not exist",
4778  name, RelationGetRelationName(rel))));
4779  else
4780  ereport(ERROR,
4781  (errcode(ERRCODE_UNDEFINED_COLUMN),
4782  errmsg("column \"%s\" does not exist",
4783  name)));
4784  }
4785  /* Check for duplicates */
4786  if (list_member_int(attnums, attnum))
4787  ereport(ERROR,
4788  (errcode(ERRCODE_DUPLICATE_COLUMN),
4789  errmsg("column \"%s\" specified more than once",
4790  name)));
4791  attnums = lappend_int(attnums, attnum);
4792  }
4793  }
4794 
4795  return attnums;
4796 }
#define NIL
Definition: pg_list.h:69
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
int namestrcmp(Name name, const char *str)
Definition: name.c:247
int natts
Definition: tupdesc.h:82
#define ERROR
Definition: elog.h:43
bool list_member_int(const List *list, int datum)
Definition: list.c:485
#define RelationGetRelationName(relation)
Definition: rel.h:441
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend_int(List *list, int datum)
Definition: list.c:146
int16 attnum
Definition: pg_attribute.h:79
#define lfirst(lc)
Definition: pg_list.h:106
const char * name
Definition: encode.c:521
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: pg_list.h:45

◆ CopyGetData()

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

Definition at line 557 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_eof, CopyStateData::fe_msgbuf, HOLD_CANCEL_INTERRUPTS, StringInfoData::len, pq_copymsgbytes(), pq_getbyte(), pq_getbytes(), pq_getmessage(), pq_getmsgstring(), pq_startmsgread(), and RESUME_CANCEL_INTERRUPTS.

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

558 {
559  int bytesread = 0;
560 
561  switch (cstate->copy_dest)
562  {
563  case COPY_FILE:
564  bytesread = fread(databuf, 1, maxread, cstate->copy_file);
565  if (ferror(cstate->copy_file))
566  ereport(ERROR,
568  errmsg("could not read from COPY file: %m")));
569  break;
570  case COPY_OLD_FE:
571 
572  /*
573  * We cannot read more than minread bytes (which in practice is 1)
574  * because old protocol doesn't have any clear way of separating
575  * the COPY stream from following data. This is slow, but not any
576  * slower than the code path was originally, and we don't care
577  * much anymore about the performance of old protocol.
578  */
579  if (pq_getbytes((char *) databuf, minread))
580  {
581  /* Only a \. terminator is legal EOF in old protocol */
582  ereport(ERROR,
583  (errcode(ERRCODE_CONNECTION_FAILURE),
584  errmsg("unexpected EOF on client connection with an open transaction")));
585  }
586  bytesread = minread;
587  break;
588  case COPY_NEW_FE:
589  while (maxread > 0 && bytesread < minread && !cstate->fe_eof)
590  {
591  int avail;
592 
593  while (cstate->fe_msgbuf->cursor >= cstate->fe_msgbuf->len)
594  {
595  /* Try to receive another message */
596  int mtype;
597 
598  readmessage:
600  pq_startmsgread();
601  mtype = pq_getbyte();
602  if (mtype == EOF)
603  ereport(ERROR,
604  (errcode(ERRCODE_CONNECTION_FAILURE),
605  errmsg("unexpected EOF on client connection with an open transaction")));
606  if (pq_getmessage(cstate->fe_msgbuf, 0))
607  ereport(ERROR,
608  (errcode(ERRCODE_CONNECTION_FAILURE),
609  errmsg("unexpected EOF on client connection with an open transaction")));
611  switch (mtype)
612  {
613  case 'd': /* CopyData */
614  break;
615  case 'c': /* CopyDone */
616  /* COPY IN correctly terminated by frontend */
617  cstate->fe_eof = true;
618  return bytesread;
619  case 'f': /* CopyFail */
620  ereport(ERROR,
621  (errcode(ERRCODE_QUERY_CANCELED),
622  errmsg("COPY from stdin failed: %s",
623  pq_getmsgstring(cstate->fe_msgbuf))));
624  break;
625  case 'H': /* Flush */
626  case 'S': /* Sync */
627 
628  /*
629  * Ignore Flush/Sync for the convenience of client
630  * libraries (such as libpq) that may send those
631  * without noticing that the command they just
632  * sent was COPY.
633  */
634  goto readmessage;
635  default:
636  ereport(ERROR,
637  (errcode(ERRCODE_PROTOCOL_VIOLATION),
638  errmsg("unexpected message type 0x%02X during COPY from stdin",
639  mtype)));
640  break;
641  }
642  }
643  avail = cstate->fe_msgbuf->len - cstate->fe_msgbuf->cursor;
644  if (avail > maxread)
645  avail = maxread;
646  pq_copymsgbytes(cstate->fe_msgbuf, databuf, avail);
647  databuf = (void *) ((char *) databuf + avail);
648  maxread -= avail;
649  bytesread += avail;
650  }
651  break;
652  case COPY_CALLBACK:
653  bytesread = cstate->data_source_cb(databuf, minread, maxread);
654  break;
655  }
656 
657  return bytesread;
658 }
#define HOLD_CANCEL_INTERRUPTS()
Definition: miscadmin.h:123
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
int errcode(int sqlerrcode)
Definition: elog.c:575
bool fe_eof
Definition: copy.c:105
CopyDest copy_dest
Definition: copy.c:101
Definition: copy.c:65
copy_data_source_cb data_source_cb
Definition: copy.c:117
#define ERROR
Definition: elog.h:43
void pq_startmsgread(void)
Definition: pqcomm.c:1210
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1094
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
int pq_getmessage(StringInfo s, int maxlen)
Definition: pqcomm.c:1272
int pq_getbyte(void)
Definition: pqcomm.c:1000
StringInfo fe_msgbuf
Definition: copy.c:103
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
int errmsg(const char *fmt,...)
Definition: elog.c:797
FILE * copy_file
Definition: copy.c:102
#define RESUME_CANCEL_INTERRUPTS()
Definition: miscadmin.h:125

◆ CopyGetInt16()

static bool CopyGetInt16 ( CopyState  cstate,
int16 val 
)
static

Definition at line 712 of file copy.c.

References buf, CopyGetData(), and pg_ntoh16.

Referenced by NextCopyFrom().

713 {
714  uint16 buf;
715 
716  if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
717  {
718  *val = 0; /* suppress compiler warning */
719  return false;
720  }
721  *val = (int16) pg_ntoh16(buf);
722  return true;
723 }
signed short int16
Definition: c.h:312
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
unsigned short uint16
Definition: c.h:324
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:557
static char * buf
Definition: pg_test_fsync.c:67
long val
Definition: informix.c:689

◆ CopyGetInt32()

static bool CopyGetInt32 ( CopyState  cstate,
int32 val 
)
static

Definition at line 683 of file copy.c.

References buf, CopyGetData(), and pg_ntoh32.

Referenced by BeginCopyFrom(), and CopyReadBinaryAttribute().

684 {
685  uint32 buf;
686 
687  if (CopyGetData(cstate, &buf, sizeof(buf), sizeof(buf)) != sizeof(buf))
688  {
689  *val = 0; /* suppress compiler warning */
690  return false;
691  }
692  *val = (int32) pg_ntoh32(buf);
693  return true;
694 }
signed int int32
Definition: c.h:313
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:557
static char * buf
Definition: pg_test_fsync.c:67
unsigned int uint32
Definition: c.h:325
long val
Definition: informix.c:689

◆ CopyLoadRawBuf()

static bool CopyLoadRawBuf ( CopyState  cstate)
static

Definition at line 737 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().

738 {
739  int nbytes;
740  int inbytes;
741 
742  if (cstate->raw_buf_index < cstate->raw_buf_len)
743  {
744  /* Copy down the unprocessed data */
745  nbytes = cstate->raw_buf_len - cstate->raw_buf_index;
746  memmove(cstate->raw_buf, cstate->raw_buf + cstate->raw_buf_index,
747  nbytes);
748  }
749  else
750  nbytes = 0; /* no data need be saved */
751 
752  inbytes = CopyGetData(cstate, cstate->raw_buf + nbytes,
753  1, RAW_BUF_SIZE - nbytes);
754  nbytes += inbytes;
755  cstate->raw_buf[nbytes] = '\0';
756  cstate->raw_buf_index = 0;
757  cstate->raw_buf_len = nbytes;
758  return (inbytes > 0);
759 }
int raw_buf_index
Definition: copy.c:211
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:557
char * raw_buf
Definition: copy.c:210
#define memmove(d, s, c)
Definition: c.h:1135
int raw_buf_len
Definition: copy.c:212
#define RAW_BUF_SIZE
Definition: copy.c:209

◆ CopyOneRowTo()

static void CopyOneRowTo ( CopyState  cstate,
Oid  tupleOid,
Datum values,
bool nulls 
)
static

Definition at line 2098 of file copy.c.

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

Referenced by copy_dest_receive(), and CopyTo().

2099 {
2100  bool need_delim = false;
2101  FmgrInfo *out_functions = cstate->out_functions;
2102  MemoryContext oldcontext;
2103  ListCell *cur;
2104  char *string;
2105 
2106  MemoryContextReset(cstate->rowcontext);
2107  oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
2108 
2109  if (cstate->binary)
2110  {
2111  /* Binary per-tuple header */
2112  CopySendInt16(cstate, list_length(cstate->attnumlist));
2113  /* Send OID if wanted --- note attnumlist doesn't include it */
2114  if (cstate->oids)
2115  {
2116  /* Hack --- assume Oid is same size as int32 */
2117  CopySendInt32(cstate, sizeof(int32));
2118  CopySendInt32(cstate, tupleOid);
2119  }
2120  }
2121  else
2122  {
2123  /* Text format has no per-tuple header, but send OID if wanted */
2124  /* Assume digits don't need any quoting or encoding conversion */
2125  if (cstate->oids)
2126  {
2128  ObjectIdGetDatum(tupleOid)));
2129  CopySendString(cstate, string);
2130  need_delim = true;
2131  }
2132  }
2133 
2134  foreach(cur, cstate->attnumlist)
2135  {
2136  int attnum = lfirst_int(cur);
2137  Datum value = values[attnum - 1];
2138  bool isnull = nulls[attnum - 1];
2139 
2140  if (!cstate->binary)
2141  {
2142  if (need_delim)
2143  CopySendChar(cstate, cstate->delim[0]);
2144  need_delim = true;
2145  }
2146 
2147  if (isnull)
2148  {
2149  if (!cstate->binary)
2150  CopySendString(cstate, cstate->null_print_client);
2151  else
2152  CopySendInt32(cstate, -1);
2153  }
2154  else
2155  {
2156  if (!cstate->binary)
2157  {
2158  string = OutputFunctionCall(&out_functions[attnum - 1],
2159  value);
2160  if (cstate->csv_mode)
2161  CopyAttributeOutCSV(cstate, string,
2162  cstate->force_quote_flags[attnum - 1],
2163  list_length(cstate->attnumlist) == 1);
2164  else
2165  CopyAttributeOutText(cstate, string);
2166  }
2167  else
2168  {
2169  bytea *outputbytes;
2170 
2171  outputbytes = SendFunctionCall(&out_functions[attnum - 1],
2172  value);
2173  CopySendInt32(cstate, VARSIZE(outputbytes) - VARHDRSZ);
2174  CopySendData(cstate, VARDATA(outputbytes),
2175  VARSIZE(outputbytes) - VARHDRSZ);
2176  }
2177  }
2178  }
2179 
2180  CopySendEndOfRow(cstate);
2181 
2182  MemoryContextSwitchTo(oldcontext);
2183 }
Definition: fmgr.h:56
bool csv_mode
Definition: copy.c:121
#define VARDATA(PTR)
Definition: postgres.h:302
bool binary
Definition: copy.c:118
List * attnumlist
Definition: copy.c:114
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:522
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
struct cursor * cur
Definition: ecpg.c:28
static void CopyAttributeOutText(CopyState cstate, char *string)
Definition: copy.c:4486
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:590
bool * force_quote_flags
Definition: copy.c:131
char * delim
Definition: copy.c:126
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:127
MemoryContext rowcontext
Definition: copy.c:155
signed int int32
Definition: c.h:313
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:458
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1753
static void CopySendInt16(CopyState cstate, int16 val)
Definition: copy.c:700
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define DatumGetCString(X)
Definition: postgres.h:551
#define lfirst_int(lc)
Definition: pg_list.h:107
static void CopyAttributeOutCSV(CopyState cstate, char *string, bool use_quote, bool single_attr)
Definition: copy.c:4639
char string[11]
Definition: preproc-type.c:46
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1814
uintptr_t Datum
Definition: postgres.h:367
static struct @131 value
int16 attnum
Definition: pg_attribute.h:79
static void CopySendData(CopyState cstate, const void *databuf, int datasize)
Definition: copy.c:446
static void CopySendString(CopyState cstate, const char *str)
Definition: copy.c:452
static int list_length(const List *l)
Definition: pg_list.h:89
static void CopySendInt32(CopyState cstate, int32 val)
Definition: copy.c:669
static Datum values[MAXATTR]
Definition: bootstrap.c:164
FmgrInfo * out_functions
Definition: copy.c:154
static void CopySendEndOfRow(CopyState cstate)
Definition: copy.c:464
Definition: c.h:516
char * null_print_client
Definition: copy.c:125
bool oids
Definition: copy.c:119

◆ CopyReadAttributesCSV()

static int CopyReadAttributesCSV ( CopyState  cstate)
static

Definition at line 4258 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().

4259 {
4260  char delimc = cstate->delim[0];
4261  char quotec = cstate->quote[0];
4262  char escapec = cstate->escape[0];
4263  int fieldno;
4264  char *output_ptr;
4265  char *cur_ptr;
4266  char *line_end_ptr;
4267 
4268  /*
4269  * We need a special case for zero-column tables: check that the input
4270  * line is empty, and return.
4271  */
4272  if (cstate->max_fields <= 0)
4273  {
4274  if (cstate->line_buf.len != 0)
4275  ereport(ERROR,
4276  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4277  errmsg("extra data after last expected column")));
4278  return 0;
4279  }
4280 
4281  resetStringInfo(&cstate->attribute_buf);
4282 
4283  /*
4284  * The de-escaped attributes will certainly not be longer than the input
4285  * data line, so we can just force attribute_buf to be large enough and
4286  * then transfer data without any checks for enough space. We need to do
4287  * it this way because enlarging attribute_buf mid-stream would invalidate
4288  * pointers already stored into cstate->raw_fields[].
4289  */
4290  if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
4291  enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
4292  output_ptr = cstate->attribute_buf.data;
4293 
4294  /* set pointer variables for loop */
4295  cur_ptr = cstate->line_buf.data;
4296  line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
4297 
4298  /* Outer loop iterates over fields */
4299  fieldno = 0;
4300  for (;;)
4301  {
4302  bool found_delim = false;
4303  bool saw_quote = false;
4304  char *start_ptr;
4305  char *end_ptr;
4306  int input_len;
4307 
4308  /* Make sure there is enough space for the next value */
4309  if (fieldno >= cstate->max_fields)
4310  {
4311  cstate->max_fields *= 2;
4312  cstate->raw_fields =
4313  repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
4314  }
4315 
4316  /* Remember start of field on both input and output sides */
4317  start_ptr = cur_ptr;
4318  cstate->raw_fields[fieldno] = output_ptr;
4319 
4320  /*
4321  * Scan data for field,
4322  *
4323  * The loop starts in "not quote" mode and then toggles between that
4324  * and "in quote" mode. The loop exits normally if it is in "not
4325  * quote" mode and a delimiter or line end is seen.
4326  */
4327  for (;;)
4328  {
4329  char c;
4330 
4331  /* Not in quote */
4332  for (;;)
4333  {
4334  end_ptr = cur_ptr;
4335  if (cur_ptr >= line_end_ptr)
4336  goto endfield;
4337  c = *cur_ptr++;
4338  /* unquoted field delimiter */
4339  if (c == delimc)
4340  {
4341  found_delim = true;
4342  goto endfield;
4343  }
4344  /* start of quoted field (or part of field) */
4345  if (c == quotec)
4346  {
4347  saw_quote = true;
4348  break;
4349  }
4350  /* Add c to output string */
4351  *output_ptr++ = c;
4352  }
4353 
4354  /* In quote */
4355  for (;;)
4356  {
4357  end_ptr = cur_ptr;
4358  if (cur_ptr >= line_end_ptr)
4359  ereport(ERROR,
4360  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4361  errmsg("unterminated CSV quoted field")));
4362 
4363  c = *cur_ptr++;
4364 
4365  /* escape within a quoted field */
4366  if (c == escapec)
4367  {
4368  /*
4369  * peek at the next char if available, and escape it if it
4370  * is an escape char or a quote char
4371  */
4372  if (cur_ptr < line_end_ptr)
4373  {
4374  char nextc = *cur_ptr;
4375 
4376  if (nextc == escapec || nextc == quotec)
4377  {
4378  *output_ptr++ = nextc;
4379  cur_ptr++;
4380  continue;
4381  }
4382  }
4383  }
4384 
4385  /*
4386  * end of quoted field. Must do this test after testing for
4387  * escape in case quote char and escape char are the same
4388  * (which is the common case).
4389  */
4390  if (c == quotec)
4391  break;
4392 
4393  /* Add c to output string */
4394  *output_ptr++ = c;
4395  }
4396  }
4397 endfield:
4398 
4399  /* Terminate attribute value in output area */
4400  *output_ptr++ = '\0';
4401 
4402  /* Check whether raw input matched null marker */
4403  input_len = end_ptr - start_ptr;
4404  if (!saw_quote && input_len == cstate->null_print_len &&
4405  strncmp(start_ptr, cstate->null_print, input_len) == 0)
4406  cstate->raw_fields[fieldno] = NULL;
4407 
4408  fieldno++;
4409  /* Done if we hit EOL instead of a delim */
4410  if (!found_delim)
4411  break;
4412  }
4413 
4414  /* Clean up state of attribute_buf */
4415  output_ptr--;
4416  Assert(*output_ptr == '\0');
4417  cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
4418 
4419  return fieldno;
4420 }
char ** raw_fields
Definition: copy.c:189
StringInfoData line_buf
Definition: copy.c:198
int errcode(int sqlerrcode)
Definition: elog.c:575
char * delim
Definition: copy.c:126
char * null_print
Definition: copy.c:123
#define ERROR
Definition: elog.h:43
char * c
char * quote
Definition: copy.c:127
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:264
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
int max_fields
Definition: copy.c:188
char * escape
Definition: copy.c:128
#define ereport(elevel, rest)
Definition: elog.h:122
int null_print_len
Definition: copy.c:124
#define Assert(condition)
Definition: c.h:699
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
int errmsg(const char *fmt,...)
Definition: elog.c:797
StringInfoData attribute_buf
Definition: copy.c:184

◆ CopyReadAttributesText()

static int CopyReadAttributesText ( CopyState  cstate)
static

Definition at line 4030 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().

4031 {
4032  char delimc = cstate->delim[0];
4033  int fieldno;
4034  char *output_ptr;
4035  char *cur_ptr;
4036  char *line_end_ptr;
4037 
4038  /*
4039  * We need a special case for zero-column tables: check that the input
4040  * line is empty, and return.
4041  */
4042  if (cstate->max_fields <= 0)
4043  {
4044  if (cstate->line_buf.len != 0)
4045  ereport(ERROR,
4046  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4047  errmsg("extra data after last expected column")));
4048  return 0;
4049  }
4050 
4051  resetStringInfo(&cstate->attribute_buf);
4052 
4053  /*
4054  * The de-escaped attributes will certainly not be longer than the input
4055  * data line, so we can just force attribute_buf to be large enough and
4056  * then transfer data without any checks for enough space. We need to do
4057  * it this way because enlarging attribute_buf mid-stream would invalidate
4058  * pointers already stored into cstate->raw_fields[].
4059  */
4060  if (cstate->attribute_buf.maxlen <= cstate->line_buf.len)
4061  enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len);
4062  output_ptr = cstate->attribute_buf.data;
4063 
4064  /* set pointer variables for loop */
4065  cur_ptr = cstate->line_buf.data;
4066  line_end_ptr = cstate->line_buf.data + cstate->line_buf.len;
4067 
4068  /* Outer loop iterates over fields */
4069  fieldno = 0;
4070  for (;;)
4071  {
4072  bool found_delim = false;
4073  char *start_ptr;
4074  char *end_ptr;
4075  int input_len;
4076  bool saw_non_ascii = false;
4077 
4078  /* Make sure there is enough space for the next value */
4079  if (fieldno >= cstate->max_fields)
4080  {
4081  cstate->max_fields *= 2;
4082  cstate->raw_fields =
4083  repalloc(cstate->raw_fields, cstate->max_fields * sizeof(char *));
4084  }
4085 
4086  /* Remember start of field on both input and output sides */
4087  start_ptr = cur_ptr;
4088  cstate->raw_fields[fieldno] = output_ptr;
4089 
4090  /*
4091  * Scan data for field.
4092  *
4093  * Note that in this loop, we are scanning to locate the end of field
4094  * and also speculatively performing de-escaping. Once we find the
4095  * end-of-field, we can match the raw field contents against the null
4096  * marker string. Only after that comparison fails do we know that
4097  * de-escaping is actually the right thing to do; therefore we *must
4098  * not* throw any syntax errors before we've done the null-marker
4099  * check.
4100  */
4101  for (;;)
4102  {
4103  char c;
4104 
4105  end_ptr = cur_ptr;
4106  if (cur_ptr >= line_end_ptr)
4107  break;
4108  c = *cur_ptr++;
4109  if (c == delimc)
4110  {
4111  found_delim = true;
4112  break;
4113  }
4114  if (c == '\\')
4115  {
4116  if (cur_ptr >= line_end_ptr)
4117  break;
4118  c = *cur_ptr++;
4119  switch (c)
4120  {
4121  case '0':
4122  case '1':
4123  case '2':
4124  case '3':
4125  case '4':
4126  case '5':
4127  case '6':
4128  case '7':
4129  {
4130  /* handle \013 */
4131  int val;
4132 
4133  val = OCTVALUE(c);
4134  if (cur_ptr < line_end_ptr)
4135  {
4136  c = *cur_ptr;
4137  if (ISOCTAL(c))
4138  {
4139  cur_ptr++;
4140  val = (val << 3) + OCTVALUE(c);
4141  if (cur_ptr < line_end_ptr)
4142  {
4143  c = *cur_ptr;
4144  if (ISOCTAL(c))
4145  {
4146  cur_ptr++;
4147  val = (val << 3) + OCTVALUE(c);
4148  }
4149  }
4150  }
4151  }
4152  c = val & 0377;
4153  if (c == '\0' || IS_HIGHBIT_SET(c))
4154  saw_non_ascii = true;
4155  }
4156  break;
4157  case 'x':
4158  /* Handle \x3F */
4159  if (cur_ptr < line_end_ptr)
4160  {
4161  char hexchar = *cur_ptr;
4162 
4163  if (isxdigit((unsigned char) hexchar))
4164  {
4165  int val = GetDecimalFromHex(hexchar);
4166 
4167  cur_ptr++;
4168  if (cur_ptr < line_end_ptr)
4169  {
4170  hexchar = *cur_ptr;
4171  if (isxdigit((unsigned char) hexchar))
4172  {
4173  cur_ptr++;
4174  val = (val << 4) + GetDecimalFromHex(hexchar);
4175  }
4176  }
4177  c = val & 0xff;
4178  if (c == '\0' || IS_HIGHBIT_SET(c))
4179  saw_non_ascii = true;
4180  }
4181  }
4182  break;
4183  case 'b':
4184  c = '\b';
4185  break;
4186  case 'f':
4187  c = '\f';
4188  break;
4189  case 'n':
4190  c = '\n';
4191  break;
4192  case 'r':
4193  c = '\r';
4194  break;
4195  case 't':
4196  c = '\t';
4197  break;
4198  case 'v':
4199  c = '\v';
4200  break;
4201 
4202  /*
4203  * in all other cases, take the char after '\'
4204  * literally
4205  */
4206  }
4207  }
4208 
4209  /* Add c to output string */
4210  *output_ptr++ = c;
4211  }
4212 
4213  /* Check whether raw input matched null marker */
4214  input_len = end_ptr - start_ptr;
4215  if (input_len == cstate->null_print_len &&
4216  strncmp(start_ptr, cstate->null_print, input_len) == 0)
4217  cstate->raw_fields[fieldno] = NULL;
4218  else
4219  {
4220  /*
4221  * At this point we know the field is supposed to contain data.
4222  *
4223  * If we de-escaped any non-7-bit-ASCII chars, make sure the
4224  * resulting string is valid data for the db encoding.
4225  */
4226  if (saw_non_ascii)
4227  {
4228  char *fld = cstate->raw_fields[fieldno];
4229 
4230  pg_verifymbstr(fld, output_ptr - fld, false);
4231  }
4232  }
4233 
4234  /* Terminate attribute value in output area */
4235  *output_ptr++ = '\0';
4236 
4237  fieldno++;
4238  /* Done if we hit EOL instead of a delim */
4239  if (!found_delim)
4240  break;
4241  }
4242 
4243  /* Clean up state of attribute_buf */
4244  output_ptr--;
4245  Assert(*output_ptr == '\0');
4246  cstate->attribute_buf.len = (output_ptr - cstate->attribute_buf.data);
4247 
4248  return fieldno;
4249 }
static int GetDecimalFromHex(char hex)
Definition: copy.c:4002
char ** raw_fields
Definition: copy.c:189
#define ISOCTAL(c)
Definition: copy.c:56
#define OCTVALUE(c)
Definition: copy.c:57
StringInfoData line_buf
Definition: copy.c:198
int errcode(int sqlerrcode)
Definition: elog.c:575
char * delim
Definition: copy.c:126
char * null_print
Definition: copy.c:123
#define IS_HIGHBIT_SET(ch)
Definition: c.h:994
#define ERROR
Definition: elog.h:43
char * c
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:264
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
int max_fields
Definition: copy.c:188
#define ereport(elevel, rest)
Definition: elog.h:122
int null_print_len
Definition: copy.c:124
#define Assert(condition)
Definition: c.h:699
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1866
StringInfoData attribute_buf
Definition: copy.c:184
long val
Definition: informix.c:689

◆ CopyReadBinaryAttribute()

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

Definition at line 4427 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().

4431 {
4432  int32 fld_size;
4433  Datum result;
4434 
4435  if (!CopyGetInt32(cstate, &fld_size))
4436  ereport(ERROR,
4437  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4438  errmsg("unexpected EOF in COPY data")));
4439  if (fld_size == -1)
4440  {
4441  *isnull = true;
4442  return ReceiveFunctionCall(flinfo, NULL, typioparam, typmod);
4443  }
4444  if (fld_size < 0)
4445  ereport(ERROR,
4446  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4447  errmsg("invalid field size")));
4448 
4449  /* reset attribute_buf to empty, and load raw data in it */
4450  resetStringInfo(&cstate->attribute_buf);
4451 
4452  enlargeStringInfo(&cstate->attribute_buf, fld_size);
4453  if (CopyGetData(cstate, cstate->attribute_buf.data,
4454  fld_size, fld_size) != fld_size)
4455  ereport(ERROR,
4456  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
4457  errmsg("unexpected EOF in COPY data")));
4458 
4459  cstate->attribute_buf.len = fld_size;
4460  cstate->attribute_buf.data[fld_size] = '\0';
4461 
4462  /* Call the column type's binary input converter */
4463  result = ReceiveFunctionCall(flinfo, &cstate->attribute_buf,
4464  typioparam, typmod);
4465 
4466  /* Trouble if it didn't eat the whole buffer */
4467  if (cstate->attribute_buf.cursor != cstate->attribute_buf.len)
4468  ereport(ERROR,
4469  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
4470  errmsg("incorrect binary data format")));
4471 
4472  *isnull = false;
4473  return result;
4474 }
static bool CopyGetInt32(CopyState cstate, int32 *val)
Definition: copy.c:683
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
static int CopyGetData(CopyState cstate, void *databuf, int minread, int maxread)
Definition: copy.c:557
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1767
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:264
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:367
int errmsg(const char *fmt,...)
Definition: elog.c:797
StringInfoData attribute_buf
Definition: copy.c:184

◆ CopyReadLine()

static bool CopyReadLine ( CopyState  cstate)
static

Definition at line 3554 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().

3555 {
3556  bool result;
3557 
3558  resetStringInfo(&cstate->line_buf);
3559  cstate->line_buf_valid = true;
3560 
3561  /* Mark that encoding conversion hasn't occurred yet */
3562  cstate->line_buf_converted = false;
3563 
3564  /* Parse data and transfer into line_buf */
3565  result = CopyReadLineText(cstate);
3566 
3567  if (result)
3568  {
3569  /*
3570  * Reached EOF. In protocol version 3, we should ignore anything
3571  * after \. up to the protocol end of copy data. (XXX maybe better
3572  * not to treat \. as special?)
3573  */
3574  if (cstate->copy_dest == COPY_NEW_FE)
3575  {
3576  do
3577  {
3578  cstate->raw_buf_index = cstate->raw_buf_len;
3579  } while (CopyLoadRawBuf(cstate));
3580  }
3581  }
3582  else
3583  {
3584  /*
3585  * If we didn't hit EOF, then we must have transferred the EOL marker
3586  * to line_buf along with the data. Get rid of it.
3587  */
3588  switch (cstate->eol_type)
3589  {
3590  case EOL_NL:
3591  Assert(cstate->line_buf.len >= 1);
3592  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
3593  cstate->line_buf.len--;
3594  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3595  break;
3596  case EOL_CR:
3597  Assert(cstate->line_buf.len >= 1);
3598  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\r');
3599  cstate->line_buf.len--;
3600  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3601  break;
3602  case EOL_CRNL:
3603  Assert(cstate->line_buf.len >= 2);
3604  Assert(cstate->line_buf.data[cstate->line_buf.len - 2] == '\r');
3605  Assert(cstate->line_buf.data[cstate->line_buf.len - 1] == '\n');
3606  cstate->line_buf.len -= 2;
3607  cstate->line_buf.data[cstate->line_buf.len] = '\0';
3608  break;
3609  case EOL_UNKNOWN:
3610  /* shouldn't get here */
3611  Assert(false);
3612  break;
3613  }
3614  }
3615 
3616  /* Done reading the line. Convert it to server encoding. */
3617  if (cstate->need_transcoding)
3618  {
3619  char *cvt;
3620 
3621  cvt = pg_any_to_server(cstate->line_buf.data,
3622  cstate->line_buf.len,
3623  cstate->file_encoding);
3624  if (cvt != cstate->line_buf.data)
3625  {
3626  /* transfer converted data back to line_buf */
3627  resetStringInfo(&cstate->line_buf);
3628  appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt));
3629  pfree(cvt);
3630  }
3631  }
3632 
3633  /* Now it's safe to use the buffer in error messages */
3634  cstate->line_buf_converted = true;
3635 
3636  return result;
3637 }
static bool CopyReadLineText(CopyState cstate)
Definition: copy.c:3643
Definition: copy.c:77
bool need_transcoding
Definition: copy.c:108
StringInfoData line_buf
Definition: copy.c:198
int raw_buf_index
Definition: copy.c:211
bool line_buf_valid
Definition: copy.c:200
bool line_buf_converted
Definition: copy.c:199
CopyDest copy_dest
Definition: copy.c:101
void pfree(void *pointer)
Definition: mcxt.c:1031
static bool CopyLoadRawBuf(CopyState cstate)
Definition: copy.c:737
Definition: copy.c:78
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
int raw_buf_len
Definition: copy.c:212
Definition: copy.c:79
int file_encoding
Definition: copy.c:107
EolType eol_type
Definition: copy.c:106
#define Assert(condition)
Definition: c.h:699
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:561
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ CopyReadLineText()

static bool CopyReadLineText ( CopyState  cstate)
static

Definition at line 3643 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().

3644 {
3645  char *copy_raw_buf;
3646  int raw_buf_ptr;
3647  int copy_buf_len;
3648  bool need_data = false;
3649  bool hit_eof = false;
3650  bool result = false;
3651  char mblen_str[2];
3652 
3653  /* CSV variables */
3654  bool first_char_in_line = true;
3655  bool in_quote = false,
3656  last_was_esc = false;
3657  char quotec = '\0';
3658  char escapec = '\0';
3659 
3660  if (cstate->csv_mode)
3661  {
3662  quotec = cstate->quote[0];
3663  escapec = cstate->escape[0];
3664  /* ignore special escape processing if it's the same as quotec */
3665  if (quotec == escapec)
3666  escapec = '\0';
3667  }
3668 
3669  mblen_str[1] = '\0';
3670 
3671  /*
3672  * The objective of this loop is to transfer the entire next input line
3673  * into line_buf. Hence, we only care for detecting newlines (\r and/or
3674  * \n) and the end-of-copy marker (\.).
3675  *
3676  * In CSV mode, \r and \n inside a quoted field are just part of the data
3677  * value and are put in line_buf. We keep just enough state to know if we
3678  * are currently in a quoted field or not.
3679  *
3680  * These four characters, and the CSV escape and quote characters, are
3681  * assumed the same in frontend and backend encodings.
3682  *
3683  * For speed, we try to move data from raw_buf to line_buf in chunks
3684  * rather than one character at a time. raw_buf_ptr points to the next
3685  * character to examine; any characters from raw_buf_index to raw_buf_ptr
3686  * have been determined to be part of the line, but not yet transferred to
3687  * line_buf.
3688  *
3689  * For a little extra speed within the loop, we copy raw_buf and
3690  * raw_buf_len into local variables.
3691  */
3692  copy_raw_buf = cstate->raw_buf;
3693  raw_buf_ptr = cstate->raw_buf_index;
3694  copy_buf_len = cstate->raw_buf_len;
3695 
3696  for (;;)
3697  {
3698  int prev_raw_ptr;
3699  char c;
3700 
3701  /*
3702  * Load more data if needed. Ideally we would just force four bytes
3703  * of read-ahead and avoid the many calls to
3704  * IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(), but the COPY_OLD_FE protocol
3705  * does not allow us to read too far ahead or we might read into the
3706  * next data, so we read-ahead only as far we know we can. One
3707  * optimization would be to read-ahead four byte here if
3708  * cstate->copy_dest != COPY_OLD_FE, but it hardly seems worth it,
3709  * considering the size of the buffer.
3710  */
3711  if (raw_buf_ptr >= copy_buf_len || need_data)
3712  {
3714 
3715  /*
3716  * Try to read some more data. This will certainly reset
3717  * raw_buf_index to zero, and raw_buf_ptr must go with it.
3718  */
3719  if (!CopyLoadRawBuf(cstate))
3720  hit_eof = true;
3721  raw_buf_ptr = 0;
3722  copy_buf_len = cstate->raw_buf_len;
3723 
3724  /*
3725  * If we are completely out of data, break out of the loop,
3726  * reporting EOF.
3727  */
3728  if (copy_buf_len <= 0)
3729  {
3730  result = true;
3731  break;
3732  }
3733  need_data = false;
3734  }
3735 
3736  /* OK to fetch a character */
3737  prev_raw_ptr = raw_buf_ptr;
3738  c = copy_raw_buf[raw_buf_ptr++];
3739 
3740  if (cstate->csv_mode)
3741  {
3742  /*
3743  * If character is '\\' or '\r', we may need to look ahead below.
3744  * Force fetch of the next character if we don't already have it.
3745  * We need to do this before changing CSV state, in case one of
3746  * these characters is also the quote or escape character.
3747  *
3748  * Note: old-protocol does not like forced prefetch, but it's OK
3749  * here since we cannot validly be at EOF.
3750  */
3751  if (c == '\\' || c == '\r')
3752  {
3754  }
3755 
3756  /*
3757  * Dealing with quotes and escapes here is mildly tricky. If the
3758  * quote char is also the escape char, there's no problem - we
3759  * just use the char as a toggle. If they are different, we need
3760  * to ensure that we only take account of an escape inside a
3761  * quoted field and immediately preceding a quote char, and not
3762  * the second in an escape-escape sequence.
3763  */
3764  if (in_quote && c == escapec)
3765  last_was_esc = !last_was_esc;
3766  if (c == quotec && !last_was_esc)
3767  in_quote = !in_quote;
3768  if (c != escapec)
3769  last_was_esc = false;
3770 
3771  /*
3772  * Updating the line count for embedded CR and/or LF chars is
3773  * necessarily a little fragile - this test is probably about the
3774  * best we can do. (XXX it's arguable whether we should do this
3775  * at all --- is cur_lineno a physical or logical count?)
3776  */
3777  if (in_quote && c == (cstate->eol_type == EOL_NL ? '\n' : '\r'))
3778  cstate->cur_lineno++;
3779  }
3780 
3781  /* Process \r */
3782  if (c == '\r' && (!cstate->csv_mode || !in_quote))
3783  {
3784  /* Check for \r\n on first line, _and_ handle \r\n. */
3785  if (cstate->eol_type == EOL_UNKNOWN ||
3786  cstate->eol_type == EOL_CRNL)
3787  {
3788  /*
3789  * If need more data, go back to loop top to load it.
3790  *
3791  * Note that if we are at EOF, c will wind up as '\0' because
3792  * of the guaranteed pad of raw_buf.
3793  */
3795 
3796  /* get next char */
3797  c = copy_raw_buf[raw_buf_ptr];
3798 
3799  if (c == '\n')
3800  {
3801  raw_buf_ptr++; /* eat newline */
3802  cstate->eol_type = EOL_CRNL; /* in case not set yet */
3803  }
3804  else
3805  {
3806  /* found \r, but no \n */
3807  if (cstate->eol_type == EOL_CRNL)
3808  ereport(ERROR,
3809  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3810  !cstate->csv_mode ?
3811  errmsg("literal carriage return found in data") :
3812  errmsg("unquoted carriage return found in data"),
3813  !cstate->csv_mode ?
3814  errhint("Use \"\\r\" to represent carriage return.") :
3815  errhint("Use quoted CSV field to represent carriage return.")));
3816 
3817  /*
3818  * if we got here, it is the first line and we didn't find
3819  * \n, so don't consume the peeked character
3820  */
3821  cstate->eol_type = EOL_CR;
3822  }
3823  }
3824  else if (cstate->eol_type == EOL_NL)
3825  ereport(ERROR,
3826  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3827  !cstate->csv_mode ?
3828  errmsg("literal carriage return found in data") :
3829  errmsg("unquoted carriage return found in data"),
3830  !cstate->csv_mode ?
3831  errhint("Use \"\\r\" to represent carriage return.") :
3832  errhint("Use quoted CSV field to represent carriage return.")));
3833  /* If reach here, we have found the line terminator */
3834  break;
3835  }
3836 
3837  /* Process \n */
3838  if (c == '\n' && (!cstate->csv_mode || !in_quote))
3839  {
3840  if (cstate->eol_type == EOL_CR || cstate->eol_type == EOL_CRNL)
3841  ereport(ERROR,
3842  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3843  !cstate->csv_mode ?
3844  errmsg("literal newline found in data") :
3845  errmsg("unquoted newline found in data"),
3846  !cstate->csv_mode ?
3847  errhint("Use \"\\n\" to represent newline.") :
3848  errhint("Use quoted CSV field to represent newline.")));
3849  cstate->eol_type = EOL_NL; /* in case not set yet */
3850  /* If reach here, we have found the line terminator */
3851  break;
3852  }
3853 
3854  /*
3855  * In CSV mode, we only recognize \. alone on a line. This is because
3856  * \. is a valid CSV data value.
3857  */
3858  if (c == '\\' && (!cstate->csv_mode || first_char_in_line))
3859  {
3860  char c2;
3861 
3864 
3865  /* -----
3866  * get next character
3867  * Note: we do not change c so if it isn't \., we can fall
3868  * through and continue processing for file encoding.
3869  * -----
3870  */
3871  c2 = copy_raw_buf[raw_buf_ptr];
3872 
3873  if (c2 == '.')
3874  {
3875  raw_buf_ptr++; /* consume the '.' */
3876 
3877  /*
3878  * Note: if we loop back for more data here, it does not
3879  * matter that the CSV state change checks are re-executed; we
3880  * will come back here with no important state changed.
3881  */
3882  if (cstate->eol_type == EOL_CRNL)
3883  {
3884  /* Get the next character */
3886  /* if hit_eof, c2 will become '\0' */
3887  c2 = copy_raw_buf[raw_buf_ptr++];
3888 
3889  if (c2 == '\n')
3890  {
3891  if (!cstate->csv_mode)
3892  ereport(ERROR,
3893  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3894  errmsg("end-of-copy marker does not match previous newline style")));
3895  else
3897  }
3898  else if (c2 != '\r')
3899  {
3900  if (!cstate->csv_mode)
3901  ereport(ERROR,
3902  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3903  errmsg("end-of-copy marker corrupt")));
3904  else
3906  }
3907  }
3908 
3909  /* Get the next character */
3911  /* if hit_eof, c2 will become '\0' */
3912  c2 = copy_raw_buf[raw_buf_ptr++];
3913 
3914  if (c2 != '\r' && c2 != '\n')
3915  {
3916  if (!cstate->csv_mode)
3917  ereport(ERROR,
3918  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3919  errmsg("end-of-copy marker corrupt")));
3920  else
3922  }
3923 
3924  if ((cstate->eol_type == EOL_NL && c2 != '\n') ||
3925  (cstate->eol_type == EOL_CRNL && c2 != '\n') ||
3926  (cstate->eol_type == EOL_CR && c2 != '\r'))
3927  {
3928  ereport(ERROR,
3929  (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
3930  errmsg("end-of-copy marker does not match previous newline style")));
3931  }
3932 
3933  /*
3934  * Transfer only the data before the \. into line_buf, then
3935  * discard the data and the \. sequence.
3936  */
3937  if (prev_raw_ptr > cstate->raw_buf_index)
3939  cstate->raw_buf + cstate->raw_buf_index,
3940  prev_raw_ptr - cstate->raw_buf_index);
3941  cstate->raw_buf_index = raw_buf_ptr;
3942  result = true; /* report EOF */
3943  break;
3944  }
3945  else if (!cstate->csv_mode)
3946 
3947  /*
3948  * If we are here, it means we found a backslash followed by
3949  * something other than a period. In non-CSV mode, anything
3950  * after a backslash is special, so we skip over that second
3951  * character too. If we didn't do that \\. would be
3952  * considered an eof-of copy, while in non-CSV mode it is a
3953  * literal backslash followed by a period. In CSV mode,
3954  * backslashes are not special, so we want to process the
3955  * character after the backslash just like a normal character,
3956  * so we don't increment in those cases.
3957  */
3958  raw_buf_ptr++;
3959  }
3960 
3961  /*
3962  * This label is for CSV cases where \. appears at the start of a
3963  * line, but there is more text after it, meaning it was a data value.
3964  * We are more strict for \. in CSV mode because \. could be a data
3965  * value, while in non-CSV mode, \. cannot be a data value.
3966  */
3967 not_end_of_copy:
3968 
3969  /*
3970  * Process all bytes of a multi-byte character as a group.
3971  *
3972  * We only support multi-byte sequences where the first byte has the
3973  * high-bit set, so as an optimization we can avoid this block
3974  * entirely if it is not set.
3975  */
3976  if (cstate->encoding_embeds_ascii && IS_HIGHBIT_SET(c))
3977  {
3978  int mblen;
3979 
3980  mblen_str[0] = c;
3981  /* All our encodings only read the first byte to get the length */
3982  mblen = pg_encoding_mblen(cstate->file_encoding, mblen_str);
3984  IF_NEED_REFILL_AND_EOF_BREAK(mblen - 1);
3985  raw_buf_ptr += mblen - 1;
3986  }
3987  first_char_in_line = false;
3988  } /* end of outer loop */
3989 
3990  /*
3991  * Transfer any still-uncopied data to line_buf.
3992  */
3994 
3995  return result;
3996 }
bool csv_mode
Definition: copy.c:121
int errhint(const char *fmt,...)
Definition: elog.c:987
Definition: copy.c:77
StringInfoData line_buf
Definition: copy.c:198
#define IF_NEED_REFILL_AND_EOF_BREAK(extralen)
Definition: copy.c:252
int raw_buf_index
Definition: copy.c:211
int errcode(int sqlerrcode)
Definition: elog.c:575
#define REFILL_LINEBUF
Definition: copy.c:269
#define IS_HIGHBIT_SET(ch)
Definition: c.h:994
#define ERROR
Definition: elog.h:43
bool encoding_embeds_ascii
Definition: copy.c:109
char * c
char * raw_buf
Definition: copy.c:210
static bool CopyLoadRawBuf(CopyState cstate)
Definition: copy.c:737
char * quote
Definition: copy.c:127
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1785
Definition: copy.c:78
int raw_buf_len
Definition: copy.c:212
char * escape
Definition: copy.c:128
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: copy.c:79
int file_encoding
Definition: copy.c:107
#define IF_NEED_REFILL_AND_NOT_EOF_CONTINUE(extralen)
Definition: copy.c:240
EolType eol_type
Definition: copy.c:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NO_END_OF_COPY_GOTO
Definition: copy.c:282
int cur_lineno
Definition: copy.c:142
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ CopySendChar()

static void CopySendChar ( CopyState  cstate,
char  c 
)
static

Definition at line 458 of file copy.c.

References appendStringInfoCharMacro, and CopyStateData::fe_msgbuf.

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

459 {
461 }
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:127
char * c
StringInfo fe_msgbuf
Definition: copy.c:103

◆ CopySendData()

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

Definition at line 446 of file copy.c.

References appendBinaryStringInfo(), and CopyStateData::fe_msgbuf.

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

447 {
448  appendBinaryStringInfo(cstate->fe_msgbuf, databuf, datasize);
449 }
StringInfo fe_msgbuf
Definition: copy.c:103
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ CopySendEndOfRow()

static void CopySendEndOfRow ( CopyState  cstate)
static

Definition at line 464 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().

465 {
466  StringInfo fe_msgbuf = cstate->fe_msgbuf;
467 
468  switch (cstate->copy_dest)
469  {
470  case COPY_FILE:
471  if (!cstate->binary)
472  {
473  /* Default line termination depends on platform */
474 #ifndef WIN32
475  CopySendChar(cstate, '\n');
476 #else
477  CopySendString(cstate, "\r\n");
478 #endif
479  }
480 
481  if (fwrite(fe_msgbuf->data, fe_msgbuf->len, 1,
482  cstate->copy_file) != 1 ||
483  ferror(cstate->copy_file))
484  {
485  if (cstate->is_program)
486  {
487  if (errno == EPIPE)
488  {
489  /*
490  * The pipe will be closed automatically on error at
491  * the end of transaction, but we might get a better
492  * error message from the subprocess' exit code than
493  * just "Broken Pipe"
494  */
495  ClosePipeToProgram(cstate);
496 
497  /*
498  * If ClosePipeToProgram() didn't throw an error, the
499  * program terminated normally, but closed the pipe
500  * first. Restore errno, and throw an error.
501  */
502  errno = EPIPE;
503  }
504  ereport(ERROR,
506  errmsg("could not write to COPY program: %m")));
507  }
508  else
509  ereport(ERROR,
511  errmsg("could not write to COPY file: %m")));
512  }
513  break;
514  case COPY_OLD_FE:
515  /* The FE/BE protocol uses \n as newline for all platforms */
516  if (!cstate->binary)
517  CopySendChar(cstate, '\n');
518 
519  if (pq_putbytes(fe_msgbuf->data, fe_msgbuf->len))
520  {
521  /* no hope of recovering connection sync, so FATAL */
522  ereport(FATAL,
523  (errcode(ERRCODE_CONNECTION_FAILURE),
524  errmsg("connection lost during COPY to stdout")));
525  }
526  break;
527  case COPY_NEW_FE:
528  /* The FE/BE protocol uses \n as newline for all platforms */
529  if (!cstate->binary)
530  CopySendChar(cstate, '\n');
531 
532  /* Dump the accumulated row as one CopyData message */
533  (void) pq_putmessage('d', fe_msgbuf->data, fe_msgbuf->len);
534  break;
535  case COPY_CALLBACK:
536  Assert(false); /* Not yet supported. */
537  break;
538  }
539 
540  resetStringInfo(fe_msgbuf);
541 }
bool binary
Definition: copy.c:118
int errcode(int sqlerrcode)
Definition: elog.c:575
static void ClosePipeToProgram(CopyState cstate)
Definition: copy.c:1708
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:458
CopyDest copy_dest
Definition: copy.c:101
Definition: copy.c:65
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
int errcode_for_file_access(void)
Definition: elog.c:598
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define ereport(elevel, rest)
Definition: elog.h:122
bool is_program
Definition: copy.c:116
#define Assert(condition)
Definition: c.h:699
StringInfo fe_msgbuf
Definition: copy.c:103
static void CopySendString(CopyState cstate, const char *str)
Definition: copy.c:452
int errmsg(const char *fmt,...)
Definition: elog.c:797
FILE * copy_file
Definition: copy.c:102
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:42
int pq_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1353

◆ CopySendInt16()

static void CopySendInt16 ( CopyState  cstate,
int16  val 
)
static

Definition at line 700 of file copy.c.

References buf, CopySendData(), and pg_hton16.

Referenced by CopyOneRowTo(), and CopyTo().

701 {
702  uint16 buf;
703 
704  buf = pg_hton16((uint16) val);
705  CopySendData(cstate, &buf, sizeof(buf));
706 }
#define pg_hton16(x)
Definition: pg_bswap.h:120
unsigned short uint16
Definition: c.h:324
static char * buf
Definition: pg_test_fsync.c:67
static void CopySendData(CopyState cstate, const void *databuf, int datasize)
Definition: copy.c:446
long val
Definition: informix.c:689

◆ CopySendInt32()

static void CopySendInt32 ( CopyState  cstate,
int32  val 
)
static

Definition at line 669 of file copy.c.

References buf, CopySendData(), and pg_hton32.

Referenced by CopyOneRowTo(), and CopyTo().

670 {
671  uint32 buf;
672 
673  buf = pg_hton32((uint32) val);
674  CopySendData(cstate, &buf, sizeof(buf));
675 }
#define pg_hton32(x)
Definition: pg_bswap.h:121
static char * buf
Definition: pg_test_fsync.c:67
unsigned int uint32
Definition: c.h:325
static void CopySendData(CopyState cstate, const void *databuf, int datasize)
Definition: copy.c:446
long val
Definition: informix.c:689

◆ CopySendString()

static void CopySendString ( CopyState  cstate,
const char *  str 
)
static

Definition at line 452 of file copy.c.

References appendBinaryStringInfo(), and CopyStateData::fe_msgbuf.

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

453 {
454  appendBinaryStringInfo(cstate->fe_msgbuf, str, strlen(str));
455 }
StringInfo fe_msgbuf
Definition: copy.c:103
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ CopyTo()

static uint64 CopyTo ( CopyState  cstate)
static

Definition at line 1946 of file copy.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, attname, attnum, CopyStateData::attnumlist, CopyStateData::binary, BinarySignature, CHECK_FOR_INTERRUPTS, CopyAttributeOutCSV(), CopyOneRowTo(), CopySendChar(), CopySendData(), CopySendEndOfRow(), CopySendInt16(), CopySendInt32(), cur, CurrentMemoryContext, CopyStateData::delim, QueryDesc::dest, ExecutorRun(), CopyStateData::fe_msgbuf, CopyStateData::file_encoding, fmgr_info(), ForwardScanDirection, GetActiveSnapshot(), getTypeBinaryOutputInfo(), getTypeOutputInfo(), CopyStateData::header_line, heap_beginscan(), heap_deform_tuple(), heap_endscan(), heap_getnext(), HeapTupleGetOid, lfirst_int, list_length(), makeStringInfo(), MemoryContextDelete(), NameStr, tupleDesc::natts, CopyStateData::need_transcoding, CopyStateData::null_print, CopyStateData::null_print_client, CopyStateData::null_print_len, CopyStateData::oids, CopyStateData::out_functions, palloc(), pfree(), pg_server_to_any(), CopyStateData::queryDesc, CopyStateData::rel, RelationGetDescr, CopyStateData::rowcontext, QueryDesc::tupDesc, TupleDescAttr, and values.

Referenced by DoCopyTo().

1947 {
1948  TupleDesc tupDesc;
1949  int num_phys_attrs;
1950  ListCell *cur;
1951  uint64 processed;
1952 
1953  if (cstate->rel)
1954  tupDesc = RelationGetDescr(cstate->rel);
1955  else
1956  tupDesc = cstate->queryDesc->tupDesc;
1957  num_phys_attrs = tupDesc->natts;
1958  cstate->null_print_client = cstate->null_print; /* default */
1959 
1960  /* We use fe_msgbuf as a per-row buffer regardless of copy_dest */
1961  cstate->fe_msgbuf = makeStringInfo();
1962 
1963  /* Get info about the columns we need to process. */
1964  cstate->out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
1965  foreach(cur, cstate->attnumlist)
1966  {
1967  int attnum = lfirst_int(cur);
1968  Oid out_func_oid;
1969  bool isvarlena;
1970  Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1971 
1972  if (cstate->binary)
1973  getTypeBinaryOutputInfo(attr->atttypid,
1974  &out_func_oid,
1975  &isvarlena);
1976  else
1977  getTypeOutputInfo(attr->atttypid,
1978  &out_func_oid,
1979  &isvarlena);
1980  fmgr_info(out_func_oid, &cstate->out_functions[attnum - 1]);
1981  }
1982 
1983  /*
1984  * Create a temporary memory context that we can reset once per row to
1985  * recover palloc'd memory. This avoids any problems with leaks inside
1986  * datatype output routines, and should be faster than retail pfree's
1987  * anyway. (We don't need a whole econtext as CopyFrom does.)
1988  */
1990  "COPY TO",
1992 
1993  if (cstate->binary)
1994  {
1995  /* Generate header for a binary copy */
1996  int32 tmp;
1997 
1998  /* Signature */
1999  CopySendData(cstate, BinarySignature, 11);
2000  /* Flags field */
2001  tmp = 0;
2002  if (cstate->oids)
2003  tmp |= (1 << 16);
2004  CopySendInt32(cstate, tmp);
2005  /* No header extension */
2006  tmp = 0;
2007  CopySendInt32(cstate, tmp);
2008  }
2009  else
2010  {
2011  /*
2012  * For non-binary copy, we need to convert null_print to file
2013  * encoding, because it will be sent directly with CopySendString.
2014  */
2015  if (cstate->need_transcoding)
2016  cstate->null_print_client = pg_server_to_any(cstate->null_print,
2017  cstate->null_print_len,
2018  cstate->file_encoding);
2019 
2020  /* if a header has been requested send the line */
2021  if (cstate->header_line)
2022  {
2023  bool hdr_delim = false;
2024 
2025  foreach(cur, cstate->attnumlist)
2026  {
2027  int attnum = lfirst_int(cur);
2028  char *colname;
2029 
2030  if (hdr_delim)
2031  CopySendChar(cstate, cstate->delim[0]);
2032  hdr_delim = true;
2033 
2034  colname = NameStr(TupleDescAttr(tupDesc, attnum - 1)->attname);
2035 
2036  CopyAttributeOutCSV(cstate, colname, false,
2037  list_length(cstate->attnumlist) == 1);
2038  }
2039 
2040  CopySendEndOfRow(cstate);
2041  }
2042  }
2043 
2044  if (cstate->rel)
2045  {
2046  Datum *values;
2047  bool *nulls;
2048  HeapScanDesc scandesc;
2049  HeapTuple tuple;
2050 
2051  values = (Datum *) palloc(num_phys_attrs * sizeof(Datum));
2052  nulls = (bool *) palloc(num_phys_attrs * sizeof(bool));
2053 
2054  scandesc = heap_beginscan(cstate->rel, GetActiveSnapshot(), 0, NULL);
2055 
2056  processed = 0;
2057  while ((tuple = heap_getnext(scandesc, ForwardScanDirection)) != NULL)
2058  {
2060 
2061  /* Deconstruct the tuple ... faster than repeated heap_getattr */
2062  heap_deform_tuple(tuple, tupDesc, values, nulls);
2063 
2064  /* Format and send the data */
2065  CopyOneRowTo(cstate, HeapTupleGetOid(tuple), values, nulls);
2066  processed++;
2067  }
2068 
2069  heap_endscan(scandesc);
2070 
2071  pfree(values);
2072  pfree(nulls);
2073  }
2074  else
2075  {
2076  /* run the plan --- the dest receiver will send tuples */
2077  ExecutorRun(cstate->queryDesc, ForwardScanDirection, 0L, true);
2078  processed = ((DR_copy *) cstate->queryDesc->dest)->processed;
2079  }
2080 
2081  if (cstate->binary)
2082  {
2083  /* Generate trailer for a binary copy */
2084  CopySendInt16(cstate, -1);
2085  /* Need to flush out the trailer */
2086  CopySendEndOfRow(cstate);
2087  }
2088 
2090 
2091  return processed;
2092 }
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2650
bool binary
Definition: copy.c:118
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1572
#define RelationGetDescr(relation)
Definition: rel.h:433
bool need_transcoding
Definition: copy.c:108
List * attnumlist
Definition: copy.c:114
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
Definition: copy.c:216
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
struct cursor * cur
Definition: ecpg.c:28
unsigned int Oid
Definition: postgres_ext.h:31
char * delim
Definition: copy.c:126
MemoryContext rowcontext
Definition: copy.c:155
int natts
Definition: tupdesc.h:82
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:634
signed int int32
Definition: c.h:313
static void CopySendChar(CopyState cstate, char c)
Definition: copy.c:458
char * null_print
Definition: copy.c:123
Relation rel
Definition: copy.c:112
void pfree(void *pointer)
Definition: mcxt.c:1031
static void CopySendInt16(CopyState cstate, int16 val)
Definition: copy.c:700
NameData attname
Definition: pg_attribute.h:40
#define lfirst_int(lc)
Definition: pg_list.h:107
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:299
static void CopyAttributeOutCSV(CopyState cstate, char *string, bool use_quote, bool single_attr)
Definition: copy.c:4639
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
QueryDesc * queryDesc
Definition: copy.c:113
static const char BinarySignature[11]
Definition: copy.c:289
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
int null_print_len
Definition: copy.c:124
int file_encoding
Definition: copy.c:107
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
TupleDesc tupDesc
Definition: execdesc.h:47
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:2716
bool header_line
Definition: copy.c:122
uintptr_t Datum
Definition: postgres.h:367
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1835
int16 attnum
Definition: pg_attribute.h:79
static void CopySendData(CopyState cstate, const void *databuf, int datasize)
Definition: copy.c:446
StringInfo fe_msgbuf
Definition: copy.c:103
static int list_length(const List *l)
Definition: pg_list.h:89
static void CopySendInt32(CopyState cstate, int32 val)
Definition: copy.c:669
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1315
static Datum values[MAXATTR]
Definition: bootstrap.c:164
DestReceiver * dest
Definition: execdesc.h:41
FmgrInfo * out_functions
Definition: copy.c:154
void * palloc(Size size)
Definition: mcxt.c:924
static void CopySendEndOfRow(CopyState cstate)
Definition: copy.c:464
#define NameStr(name)
Definition: c.h:576
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
char * null_print_client
Definition: copy.c:125
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707
HeapScanDesc heap_beginscan(Relation relation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: heapam.c:1404
static void CopyOneRowTo(CopyState cstate, Oid tupleOid, Datum *values, bool *nulls)
Definition: copy.c:2098
bool oids
Definition: copy.c:119

◆ CreateCopyDestReceiver()

DestReceiver* CreateCopyDestReceiver ( void  )

Definition at line 4849 of file copy.c.

References copy_dest_destroy(), copy_dest_receive(), copy_dest_shutdown(), copy_dest_startup(), DestCopyOut, and palloc().

Referenced by CreateDestReceiver().

4850 {
4851  DR_copy *self = (DR_copy *) palloc(sizeof(DR_copy));
4852 
4853  self->pub.receiveSlot = copy_dest_receive;
4854  self->pub.rStartup = copy_dest_startup;
4855  self->pub.rShutdown = copy_dest_shutdown;
4856  self->pub.rDestroy = copy_dest_destroy;
4857  self->pub.mydest = DestCopyOut;
4858 
4859  self->cstate = NULL; /* will be set later */
4860  self->processed = 0;
4861 
4862  return (DestReceiver *) self;
4863 }
Definition: copy.c:216
static void copy_dest_destroy(DestReceiver *self)
Definition: copy.c:4840
static void copy_dest_shutdown(DestReceiver *self)
Definition: copy.c:4831
static void copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: copy.c:4803
static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: copy.c:4812
void * palloc(Size size)
Definition: mcxt.c:924

◆ DoCopy()

void DoCopy ( ParseState pstate,
const CopyStmt stmt,
int  stmt_location,
int  stmt_len,
uint64 *  processed 
)

Definition at line 782 of file copy.c.

References AccessShareLock, ACL_INSERT, ACL_SELECT, addRangeTableEntryForRelation(), Assert, CopyStmt::attlist, BeginCopyFrom(), BeginCopyTo(), bms_add_member(), check_enable_rls(), CopyFrom(), CopyGetAttnums(), cur, DoCopyTo(), EndCopyFrom(), EndCopyTo(), ereport, errcode(), errhint(), errmsg(), ERROR, ExecCheckRTPerms(), ColumnRef::fields, CopyStmt::filename, FirstLowInvalidHeapAttributeNumber, SelectStmt::fromClause, get_namespace_name(), GetUserId(), heap_close, heap_openrv(), ResTarget::indirection, RangeTblEntry::insertedCols, InvalidOid, CopyStmt::is_from, is_member_of_role(), CopyStmt::is_program, lappend(), lfirst, lfirst_int, list_make1, ColumnRef::location, ResTarget::location, makeNode, makeRangeVar(), ResTarget::name, NIL, NoLock, CopyStmt::options, ParseState::p_rtable, PreventCommandIfParallelMode(), PreventCommandIfReadOnly(), pstrdup(), CopyStmt::query, RelationData::rd_islocaltemp, CopyStateData::rel, CopyStmt::relation, RelationGetDescr, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::requiredPerms, RLS_ENABLED, RowExclusiveLock, select, RangeTblEntry::selectedCols, RawStmt::stmt, RawStmt::stmt_len, RawStmt::stmt_location, SelectStmt::targetList, ResTarget::val, and XactReadOnly.

Referenced by standard_ProcessUtility().

785 {
786  CopyState cstate;
787  bool is_from = stmt->is_from;
788  bool pipe = (stmt->filename == NULL);
789  Relation rel;
790  Oid relid;
791  RawStmt *query = NULL;
792 
793  /*
794  * Disallow COPY to/from file or program except to users with the
795  * appropriate role.
796  */
797  if (!pipe)
798  {
799  if (stmt->is_program)
800  {
801  if (!is_member_of_role(GetUserId(), DEFAULT_ROLE_EXECUTE_SERVER_PROGRAM))
802  ereport(ERROR,
803  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
804  errmsg("must be superuser or a member of the pg_execute_server_program role to COPY to or from an external program"),
805  errhint("Anyone can COPY to stdout or from stdin. "
806  "psql's \\copy command also works for anyone.")));
807  }
808  else
809  {
810  if (is_from && !is_member_of_role(GetUserId(), DEFAULT_ROLE_READ_SERVER_FILES))
811  ereport(ERROR,
812  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
813  errmsg("must be superuser or a member of the pg_read_server_files role to COPY from a file"),
814  errhint("Anyone can COPY to stdout or from stdin. "
815  "psql's \\copy command also works for anyone.")));
816 
817  if (!is_from && !is_member_of_role(GetUserId(), DEFAULT_ROLE_WRITE_SERVER_FILES))
818  ereport(ERROR,
819  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
820  errmsg("must be superuser or a member of the pg_write_server_files role to COPY to a file"),
821  errhint("Anyone can COPY to stdout or from stdin. "
822  "psql's \\copy command also works for anyone.")));
823  }
824  }
825 
826  if (stmt->relation)
827  {
828  TupleDesc tupDesc;
829  List *attnums;
830  ListCell *cur;
831  RangeTblEntry *rte;
832 
833  Assert(!stmt->query);
834 
835  /* Open and lock the relation, using the appropriate lock type. */
836  rel = heap_openrv(stmt->relation,
837  (is_from ? RowExclusiveLock : AccessShareLock));
838 
839  relid = RelationGetRelid(rel);
840 
841  rte = addRangeTableEntryForRelation(pstate, rel, NULL, false, false);
842  rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
843 
844  tupDesc = RelationGetDescr(rel);
845  attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
846  foreach(cur, attnums)
847  {
848  int attno = lfirst_int(cur) -
850 
851  if (is_from)
852  rte->insertedCols = bms_add_member(rte->insertedCols, attno);
853  else
854  rte->selectedCols = bms_add_member(rte->selectedCols, attno);
855  }
856  ExecCheckRTPerms(pstate->p_rtable, true);
857 
858  /*
859  * Permission check for row security policies.
860  *
861  * check_enable_rls will ereport(ERROR) if the user has requested
862  * something invalid and will otherwise indicate if we should enable
863  * RLS (returns RLS_ENABLED) or not for this COPY statement.
864  *
865  * If the relation has a row security policy and we are to apply it
866  * then perform a "query" copy and allow the normal query processing
867  * to handle the policies.
868  *
869  * If RLS is not enabled for this, then just fall through to the
870  * normal non-filtering relation handling.
871  */
872  if (check_enable_rls(rte->relid, InvalidOid, false) == RLS_ENABLED)
873  {
875  ColumnRef *cr;
876  ResTarget *target;
877  RangeVar *from;
878  List *targetList = NIL;
879 
880  if (is_from)
881  ereport(ERROR,
882  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
883  errmsg("COPY FROM not supported with row-level security"),
884  errhint("Use INSERT statements instead.")));
885 
886  /*
887  * Build target list
888  *
889  * If no columns are specified in the attribute list of the COPY
890  * command, then the target list is 'all' columns. Therefore, '*'
891  * should be used as the target list for the resulting SELECT
892  * statement.
893  *
894  * In the case that columns are specified in the attribute list,
895  * create a ColumnRef and ResTarget for each column and add them
896  * to the target list for the resulting SELECT statement.
897  */
898  if (!stmt->attlist)
899  {
900  cr = makeNode(ColumnRef);
902  cr->location = -1;
903 
904  target = makeNode(