PostgreSQL Source Code git master
Loading...
Searching...
No Matches
copyfrom_internal.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * copyfrom_internal.h
4 * Internal definitions for COPY FROM command.
5 *
6 *
7 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/commands/copyfrom_internal.h
11 *
12 *-------------------------------------------------------------------------
13 */
14#ifndef COPYFROM_INTERNAL_H
15#define COPYFROM_INTERNAL_H
16
17#include "commands/copy.h"
18#include "commands/trigger.h"
19#include "nodes/miscnodes.h"
20
21/*
22 * Represents the different source cases we need to worry about at
23 * the bottom level
24 */
25typedef enum CopySource
26{
27 COPY_FILE, /* from file (or a piped program) */
28 COPY_FRONTEND, /* from frontend */
29 COPY_CALLBACK, /* from callback function */
31
32/*
33 * Represents the end-of-line terminator type of the input
34 */
42
43/*
44 * Represents the insert method to be used during COPY FROM.
45 */
46typedef enum CopyInsertMethod
47{
48 CIM_SINGLE, /* use table_tuple_insert or ExecForeignInsert */
49 CIM_MULTI, /* always use table_multi_insert or
50 * ExecForeignBatchInsert */
51 CIM_MULTI_CONDITIONAL, /* use table_multi_insert or
52 * ExecForeignBatchInsert only if valid */
54
55/*
56 * This struct contains all the state variables used throughout a COPY FROM
57 * operation.
58 */
59typedef struct CopyFromStateData
60{
61 /* format routine */
62 const struct CopyFromRoutine *routine;
63
64 /* low-level state data */
65 CopySource copy_src; /* type of copy source */
66 FILE *copy_file; /* used if copy_src == COPY_FILE */
67 StringInfo fe_msgbuf; /* used if copy_src == COPY_FRONTEND */
68
69 EolType eol_type; /* EOL type of input */
70 int file_encoding; /* file or remote side's character encoding */
71 bool need_transcoding; /* file encoding diff from server? */
72 Oid conversion_proc; /* encoding conversion function */
73
74 /* parameters from the COPY command */
75 Relation rel; /* relation to copy from */
76 List *attnumlist; /* integer list of attnums to copy */
77 char *filename; /* filename, or NULL for STDIN */
78 bool is_program; /* is 'filename' a program to popen? */
79 copy_data_source_cb data_source_cb; /* function for reading data */
80
82 bool *convert_select_flags; /* per-column CSV/TEXT CS flags */
83 Node *whereClause; /* WHERE condition (or NULL) */
84
85 /* these are just for error messages, see CopyFromErrorCallback */
86 const char *cur_relname; /* table name for error messages */
87 uint64 cur_lineno; /* line number for error messages */
88 const char *cur_attname; /* current att for error messages */
89 const char *cur_attval; /* current att value for error messages */
90 bool relname_only; /* don't output line number, att, etc. */
91
92 /*
93 * Working state
94 */
95 MemoryContext copycontext; /* per-copy execution context */
96
97 AttrNumber num_defaults; /* count of att that are missing and have
98 * default value */
99 FmgrInfo *in_functions; /* array of input functions for each attrs */
100 Oid *typioparams; /* array of element types for in_functions */
101 ErrorSaveContext *escontext; /* soft error trapped during in_functions
102 * execution */
103 uint64 num_errors; /* total number of rows which contained soft
104 * errors */
105 int *defmap; /* array of default att numbers related to
106 * missing att */
107 ExprState **defexprs; /* array of default att expressions for all
108 * att */
109 bool *defaults; /* if DEFAULT marker was found for
110 * corresponding att */
111
112 /*
113 * True if the corresponding attribute's is a constrained domain. This
114 * will be populated only when ON_ERROR is SET_NULL, otherwise NULL.
115 */
117
118 bool volatile_defexprs; /* is any of defexprs volatile? */
119 List *range_table; /* single element list of RangeTblEntry */
120 List *rteperminfos; /* single element list of RTEPermissionInfo */
122
124
125 /*
126 * These variables are used to reduce overhead in COPY FROM.
127 *
128 * attribute_buf holds the separated, de-escaped text for each field of
129 * the current line. The CopyReadAttributes functions return arrays of
130 * pointers into this buffer. We avoid palloc/pfree overhead by re-using
131 * the buffer on each cycle.
132 *
133 * In binary COPY FROM, attribute_buf holds the binary data for the
134 * current field, but the usage is otherwise similar.
135 */
137
138 /* field raw data pointers found by COPY FROM */
139
142
143 /*
144 * Similarly, line_buf holds the whole input line being processed. The
145 * input cycle is first to read the whole line into line_buf, and then
146 * extract the individual attribute fields into attribute_buf. line_buf
147 * is preserved unmodified so that we can display it in error messages if
148 * appropriate. (In binary mode, line_buf is not used.)
149 */
151 bool line_buf_valid; /* contains the row being processed? */
152
153 /*
154 * input_buf holds input data, already converted to database encoding.
155 *
156 * In text mode, CopyReadLine parses this data sufficiently to locate line
157 * boundaries, then transfers the data to line_buf. We guarantee that
158 * there is a \0 at input_buf[input_buf_len] at all times. (In binary
159 * mode, input_buf is not used.)
160 *
161 * If encoding conversion is not required, input_buf is not a separate
162 * buffer but points directly to raw_buf. In that case, input_buf_len
163 * tracks the number of bytes that have been verified as valid in the
164 * database encoding, and raw_buf_len is the total number of bytes stored
165 * in the buffer.
166 */
167#define INPUT_BUF_SIZE 65536 /* we palloc INPUT_BUF_SIZE+1 bytes */
169 int input_buf_index; /* next byte to process */
170 int input_buf_len; /* total # of bytes stored */
171 bool input_reached_eof; /* true if we reached EOF */
172 bool input_reached_error; /* true if a conversion error happened */
173 /* Shorthand for number of unconsumed bytes available in input_buf */
174#define INPUT_BUF_BYTES(cstate) ((cstate)->input_buf_len - (cstate)->input_buf_index)
175
176 /*
177 * raw_buf holds raw input data read from the data source (file or client
178 * connection), not yet converted to the database encoding. Like with
179 * 'input_buf', we guarantee that there is a \0 at raw_buf[raw_buf_len].
180 */
181#define RAW_BUF_SIZE 65536 /* we palloc RAW_BUF_SIZE+1 bytes */
182 char *raw_buf;
183 int raw_buf_index; /* next byte to process */
184 int raw_buf_len; /* total # of bytes stored */
185 bool raw_reached_eof; /* true if we reached EOF */
186
187 /* Shorthand for number of unconsumed bytes available in raw_buf */
188#define RAW_BUF_BYTES(cstate) ((cstate)->raw_buf_len - (cstate)->raw_buf_index)
189
190 uint64 bytes_processed; /* number of bytes processed so far */
192
193extern void ReceiveCopyBegin(CopyFromState cstate);
194extern void ReceiveCopyBinaryHeader(CopyFromState cstate);
195
196/* One-row callbacks for built-in formats defined in copyfromparse.c */
197extern bool CopyFromTextOneRow(CopyFromState cstate, ExprContext *econtext,
198 Datum *values, bool *nulls);
199extern bool CopyFromCSVOneRow(CopyFromState cstate, ExprContext *econtext,
200 Datum *values, bool *nulls);
201extern bool CopyFromBinaryOneRow(CopyFromState cstate, ExprContext *econtext,
202 Datum *values, bool *nulls);
203
204#endif /* COPYFROM_INTERNAL_H */
int16 AttrNumber
Definition attnum.h:21
static Datum values[MAXATTR]
Definition bootstrap.c:147
uint64_t uint64
Definition c.h:580
@ COPY_FILE
@ COPY_CALLBACK
@ COPY_FRONTEND
bool CopyFromTextOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
bool CopyFromCSVOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
CopyInsertMethod
@ CIM_SINGLE
@ CIM_MULTI_CONDITIONAL
@ CIM_MULTI
void ReceiveCopyBinaryHeader(CopyFromState cstate)
void ReceiveCopyBegin(CopyFromState cstate)
@ EOL_CR
@ EOL_CRNL
@ EOL_UNKNOWN
@ EOL_NL
bool CopyFromBinaryOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
int(* copy_data_source_cb)(void *outbuf, int minread, int maxread)
Definition copy.h:95
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
copy_data_source_cb data_source_cb
const struct CopyFromRoutine * routine
StringInfoData line_buf
CopyFormatOptions opts
StringInfoData attribute_buf
TransitionCaptureState * transition_capture
MemoryContext copycontext
const char * cur_attval
const char * cur_attname
const char * cur_relname
ErrorSaveContext * escontext
Definition pg_list.h:54
Definition nodes.h:135