1/*-------------------------------------------------------------------------
4 * Internal definitions for COPY FROM command.
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/commands/copyfrom_internal.h
12 *-------------------------------------------------------------------------
14#ifndef COPYFROM_INTERNAL_H
15#define COPYFROM_INTERNAL_H
22 * Represents the different source cases we need to worry about at
33 * Represents the end-of-line terminator type of the input
44 * Represents the insert method to be used during COPY FROM.
48 CIM_SINGLE,
/* use table_tuple_insert or ExecForeignInsert */
50 * ExecForeignBatchInsert */
52 * ExecForeignBatchInsert only if valid */
56 * This struct contains all the state variables used throughout a COPY FROM
64 /* low-level state data */
66 FILE *
copy_file;
/* used if copy_src == COPY_FILE */
74 /* parameters from the COPY command */
77 char *
filename;
/* filename, or NULL for STDIN */
85 /* these are just for error messages, see CopyFromErrorCallback */
89 const char *
cur_attval;
/* current att value for error messages */
105 int *
defmap;
/* array of default att numbers related to
110 * corresponding att */
119 * These variables are used to reduce overhead in COPY FROM.
121 * attribute_buf holds the separated, de-escaped text for each field of
122 * the current line. The CopyReadAttributes functions return arrays of
123 * pointers into this buffer. We avoid palloc/pfree overhead by re-using
124 * the buffer on each cycle.
126 * In binary COPY FROM, attribute_buf holds the binary data for the
127 * current field, but the usage is otherwise similar.
131 /* field raw data pointers found by COPY FROM */
137 * Similarly, line_buf holds the whole input line being processed. The
138 * input cycle is first to read the whole line into line_buf, and then
139 * extract the individual attribute fields into attribute_buf. line_buf
140 * is preserved unmodified so that we can display it in error messages if
141 * appropriate. (In binary mode, line_buf is not used.)
147 * input_buf holds input data, already converted to database encoding.
149 * In text mode, CopyReadLine parses this data sufficiently to locate line
150 * boundaries, then transfers the data to line_buf. We guarantee that
151 * there is a 0円 at input_buf[input_buf_len] at all times. (In binary
152 * mode, input_buf is not used.)
154 * If encoding conversion is not required, input_buf is not a separate
155 * buffer but points directly to raw_buf. In that case, input_buf_len
156 * tracks the number of bytes that have been verified as valid in the
157 * database encoding, and raw_buf_len is the total number of bytes stored
160 #define INPUT_BUF_SIZE 65536 /* we palloc INPUT_BUF_SIZE+1 bytes */
166 /* Shorthand for number of unconsumed bytes available in input_buf */
167 #define INPUT_BUF_BYTES(cstate) ((cstate)->input_buf_len - (cstate)->input_buf_index)
170 * raw_buf holds raw input data read from the data source (file or client
171 * connection), not yet converted to the database encoding. Like with
172 * 'input_buf', we guarantee that there is a 0円 at raw_buf[raw_buf_len].
174 #define RAW_BUF_SIZE 65536 /* we palloc RAW_BUF_SIZE+1 bytes */
180 /* Shorthand for number of unconsumed bytes available in raw_buf */
181 #define RAW_BUF_BYTES(cstate) ((cstate)->raw_buf_len - (cstate)->raw_buf_index)
189/* One-row callbacks for built-in formats defined in copyfromparse.c */
197#endif /* COPYFROM_INTERNAL_H */
static Datum values[MAXATTR]
struct CopyFromStateData CopyFromStateData
bool CopyFromTextOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
bool CopyFromCSVOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
void ReceiveCopyBinaryHeader(CopyFromState cstate)
void ReceiveCopyBegin(CopyFromState cstate)
bool CopyFromBinaryOneRow(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
int(* copy_data_source_cb)(void *outbuf, int minread, int maxread)
copy_data_source_cb data_source_cb
const struct CopyFromRoutine * routine
StringInfoData attribute_buf
bool * convert_select_flags
TransitionCaptureState * transition_capture
MemoryContext copycontext
ErrorSaveContext * escontext