copyfrom_internal.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*-------------------------------------------------------------------------
  2. *
  3. * copyfrom_internal.h
  4. * Internal definitions for COPY FROM command.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, 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. #include "commands/copy.h"
  17. #include "commands/trigger.h"
  18. /*
  19. * Represents the different source cases we need to worry about at
  20. * the bottom level
  21. */
  22. typedef enum CopySource
  23. {
  24. COPY_FILE, /* from file (or a piped program) */
  25. COPY_FRONTEND, /* from frontend */
  26. COPY_CALLBACK /* from callback function */
  27. } CopySource;
  28. /*
  29. * Represents the end-of-line terminator type of the input
  30. */
  31. typedef enum EolType
  32. {
  33. EOL_UNKNOWN,
  34. EOL_NL,
  35. EOL_CR,
  36. EOL_CRNL
  37. } EolType;
  38. /*
  39. * Represents the heap insert method to be used during COPY FROM.
  40. */
  41. typedef enum CopyInsertMethod
  42. {
  43. CIM_SINGLE, /* use table_tuple_insert or fdw routine */
  44. CIM_MULTI, /* always use table_multi_insert */
  45. CIM_MULTI_CONDITIONAL /* use table_multi_insert only if valid */
  46. } CopyInsertMethod;
  47. /*
  48. * This struct contains all the state variables used throughout a COPY FROM
  49. * operation.
  50. */
  51. typedef struct CopyFromStateData
  52. {
  53. /* low-level state data */
  54. CopySource copy_src; /* type of copy source */
  55. FILE *copy_file; /* used if copy_src == COPY_FILE */
  56. StringInfo fe_msgbuf; /* used if copy_src == COPY_NEW_FE */
  57. EolType eol_type; /* EOL type of input */
  58. int file_encoding; /* file or remote side's character encoding */
  59. bool need_transcoding; /* file encoding diff from server? */
  60. Oid conversion_proc; /* encoding conversion function */
  61. /* parameters from the COPY command */
  62. Relation rel; /* relation to copy from */
  63. List *attnumlist; /* integer list of attnums to copy */
  64. char *filename; /* filename, or NULL for STDIN */
  65. bool is_program; /* is 'filename' a program to popen? */
  66. copy_data_source_cb data_source_cb; /* function for reading data */
  67. CopyFormatOptions opts;
  68. bool *convert_select_flags; /* per-column CSV/TEXT CS flags */
  69. Node *whereClause; /* WHERE condition (or NULL) */
  70. /* these are just for error messages, see CopyFromErrorCallback */
  71. const char *cur_relname; /* table name for error messages */
  72. uint64 cur_lineno; /* line number for error messages */
  73. const char *cur_attname; /* current att for error messages */
  74. const char *cur_attval; /* current att value for error messages */
  75. /*
  76. * Working state
  77. */
  78. MemoryContext copycontext; /* per-copy execution context */
  79. AttrNumber num_defaults;
  80. FmgrInfo *in_functions; /* array of input functions for each attrs */
  81. Oid *typioparams; /* array of element types for in_functions */
  82. int *defmap; /* array of default att numbers */
  83. ExprState **defexprs; /* array of default att expressions */
  84. bool volatile_defexprs; /* is any of defexprs volatile? */
  85. List *range_table;
  86. ExprState *qualexpr;
  87. TransitionCaptureState *transition_capture;
  88. /*
  89. * These variables are used to reduce overhead in COPY FROM.
  90. *
  91. * attribute_buf holds the separated, de-escaped text for each field of
  92. * the current line. The CopyReadAttributes functions return arrays of
  93. * pointers into this buffer. We avoid palloc/pfree overhead by re-using
  94. * the buffer on each cycle.
  95. *
  96. * In binary COPY FROM, attribute_buf holds the binary data for the
  97. * current field, but the usage is otherwise similar.
  98. */
  99. StringInfoData attribute_buf;
  100. /* field raw data pointers found by COPY FROM */
  101. int max_fields;
  102. char **raw_fields;
  103. /*
  104. * Similarly, line_buf holds the whole input line being processed. The
  105. * input cycle is first to read the whole line into line_buf, and then
  106. * extract the individual attribute fields into attribute_buf. line_buf
  107. * is preserved unmodified so that we can display it in error messages if
  108. * appropriate. (In binary mode, line_buf is not used.)
  109. */
  110. StringInfoData line_buf;
  111. bool line_buf_valid; /* contains the row being processed? */
  112. /*
  113. * input_buf holds input data, already converted to database encoding.
  114. *
  115. * In text mode, CopyReadLine parses this data sufficiently to locate line
  116. * boundaries, then transfers the data to line_buf. We guarantee that
  117. * there is a \0 at input_buf[input_buf_len] at all times. (In binary
  118. * mode, input_buf is not used.)
  119. *
  120. * If encoding conversion is not required, input_buf is not a separate
  121. * buffer but points directly to raw_buf. In that case, input_buf_len
  122. * tracks the number of bytes that have been verified as valid in the
  123. * database encoding, and raw_buf_len is the total number of bytes stored
  124. * in the buffer.
  125. */
  126. #define INPUT_BUF_SIZE 65536 /* we palloc INPUT_BUF_SIZE+1 bytes */
  127. char *input_buf;
  128. int input_buf_index; /* next byte to process */
  129. int input_buf_len; /* total # of bytes stored */
  130. bool input_reached_eof; /* true if we reached EOF */
  131. bool input_reached_error; /* true if a conversion error happened */
  132. /* Shorthand for number of unconsumed bytes available in input_buf */
  133. #define INPUT_BUF_BYTES(cstate) ((cstate)->input_buf_len - (cstate)->input_buf_index)
  134. /*
  135. * raw_buf holds raw input data read from the data source (file or client
  136. * connection), not yet converted to the database encoding. Like with
  137. * 'input_buf', we guarantee that there is a \0 at raw_buf[raw_buf_len].
  138. */
  139. #define RAW_BUF_SIZE 65536 /* we palloc RAW_BUF_SIZE+1 bytes */
  140. char *raw_buf;
  141. int raw_buf_index; /* next byte to process */
  142. int raw_buf_len; /* total # of bytes stored */
  143. bool raw_reached_eof; /* true if we reached EOF */
  144. /* Shorthand for number of unconsumed bytes available in raw_buf */
  145. #define RAW_BUF_BYTES(cstate) ((cstate)->raw_buf_len - (cstate)->raw_buf_index)
  146. uint64 bytes_processed; /* number of bytes processed so far */
  147. } CopyFromStateData;
  148. extern void ReceiveCopyBegin(CopyFromState cstate);
  149. extern void ReceiveCopyBinaryHeader(CopyFromState cstate);
  150. #endif /* COPYFROM_INTERNAL_H */