PostgreSQL Source Code git master
Loading...
Searching...
No Matches
copyto.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * copyto.c
4 * COPY <table> TO file/program/client
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/commands/copyto.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include <ctype.h>
18#include <unistd.h>
19#include <sys/stat.h>
20
21#include "access/table.h"
22#include "access/tableam.h"
23#include "access/tupconvert.h"
24#include "catalog/pg_inherits.h"
25#include "commands/copyapi.h"
26#include "commands/progress.h"
27#include "executor/execdesc.h"
28#include "executor/executor.h"
29#include "executor/tuptable.h"
30#include "funcapi.h"
31#include "libpq/libpq.h"
32#include "libpq/pqformat.h"
33#include "mb/pg_wchar.h"
34#include "miscadmin.h"
35#include "pgstat.h"
36#include "storage/fd.h"
37#include "tcop/tcopprot.h"
38#include "utils/json.h"
39#include "utils/lsyscache.h"
40#include "utils/memutils.h"
41#include "utils/rel.h"
42#include "utils/snapmgr.h"
43#include "utils/wait_event.h"
44
45/*
46 * Represents the different dest cases we need to worry about at
47 * the bottom level
48 */
49typedef enum CopyDest
50{
51 COPY_FILE, /* to file (or a piped program) */
52 COPY_FRONTEND, /* to frontend */
53 COPY_CALLBACK, /* to callback function */
55
56/*
57 * This struct contains all the state variables used throughout a COPY TO
58 * operation.
59 *
60 * Multi-byte encodings: all supported client-side encodings encode multi-byte
61 * characters by having the first byte's high bit set. Subsequent bytes of the
62 * character can have the high bit not set. When scanning data in such an
63 * encoding to look for a match to a single-byte (ie ASCII) character, we must
64 * use the full pg_encoding_mblen() machinery to skip over multibyte
65 * characters, else we might find a false match to a trailing byte. In
66 * supported server encodings, there is no possibility of a false match, and
67 * it's faster to make useless comparisons to trailing bytes than it is to
68 * invoke pg_encoding_mblen() to skip over them. encoding_embeds_ascii is true
69 * when we have to do it the hard way.
70 */
71typedef struct CopyToStateData
72{
73 /* format-specific routines */
75
76 /* low-level state data */
77 CopyDest copy_dest; /* type of copy source/destination */
78 FILE *copy_file; /* used if copy_dest == COPY_FILE */
79 StringInfo fe_msgbuf; /* used for all dests during COPY TO */
80
81 int file_encoding; /* file or remote side's character encoding */
82 bool need_transcoding; /* file encoding diff from server? */
83 bool encoding_embeds_ascii; /* ASCII can be non-first byte? */
84
85 /* parameters from the COPY command */
86 Relation rel; /* relation to copy to */
87 QueryDesc *queryDesc; /* executable query to copy from */
88 List *attnumlist; /* integer list of attnums to copy */
89 char *filename; /* filename, or NULL for STDOUT */
90 bool is_program; /* is 'filename' a program to popen? */
91 bool json_row_delim_needed; /* need delimiter before next row */
92 StringInfo json_buf; /* reusable buffer for JSON output,
93 * initialized in BeginCopyTo */
94 TupleDesc tupDesc; /* Descriptor for JSON output; for a column
95 * list this is a projected descriptor */
96 Datum *json_projvalues; /* pre-allocated projection values, or
97 * NULL */
98 bool *json_projnulls; /* pre-allocated projection nulls, or NULL */
99 copy_data_dest_cb data_dest_cb; /* function for writing data */
100
102 Node *whereClause; /* WHERE condition (or NULL) */
103 List *partitions; /* OID list of partitions to copy data from */
104
105 /*
106 * Working state
107 */
108 MemoryContext copycontext; /* per-copy execution context */
109
110 FmgrInfo *out_functions; /* lookup info for output functions */
111 MemoryContext rowcontext; /* per-row evaluation context */
112 uint64 bytes_processed; /* number of bytes processed so far */
114
115/* DestReceiver for COPY (query) TO */
116typedef struct
117{
118 DestReceiver pub; /* publicly-known function pointers */
119 CopyToState cstate; /* CopyToStateData for the command */
120 uint64 processed; /* # of tuples processed */
121} DR_copy;
122
123/* NOTE: there's a copy of this in copyfromparse.c */
124static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
125
126
127/* non-export function prototypes */
128static void EndCopy(CopyToState cstate);
129static void ClosePipeToProgram(CopyToState cstate);
130static void CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot);
131static void CopyAttributeOutText(CopyToState cstate, const char *string);
132static void CopyAttributeOutCSV(CopyToState cstate, const char *string,
133 bool use_quote);
134static void CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel,
135 uint64 *processed);
136
137/* built-in format-specific routines */
138static void CopyToTextLikeStart(CopyToState cstate, TupleDesc tupDesc);
139static void CopyToTextLikeOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo);
140static void CopyToTextOneRow(CopyToState cstate, TupleTableSlot *slot);
141static void CopyToCSVOneRow(CopyToState cstate, TupleTableSlot *slot);
142static void CopyToTextLikeOneRow(CopyToState cstate, TupleTableSlot *slot,
143 bool is_csv);
144static void CopyToTextLikeEnd(CopyToState cstate);
145static void CopyToJsonOneRow(CopyToState cstate, TupleTableSlot *slot);
146static void CopyToJsonEnd(CopyToState cstate);
147static void CopyToBinaryStart(CopyToState cstate, TupleDesc tupDesc);
148static void CopyToBinaryOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo);
149static void CopyToBinaryOneRow(CopyToState cstate, TupleTableSlot *slot);
150static void CopyToBinaryEnd(CopyToState cstate);
151
152/* Low-level communications functions */
153static void SendCopyBegin(CopyToState cstate);
154static void SendCopyEnd(CopyToState cstate);
155static void CopySendData(CopyToState cstate, const void *databuf, int datasize);
156static void CopySendString(CopyToState cstate, const char *str);
157static void CopySendChar(CopyToState cstate, char c);
158static void CopySendEndOfRow(CopyToState cstate);
159static void CopySendTextLikeEndOfRow(CopyToState cstate);
160static void CopySendInt32(CopyToState cstate, int32 val);
161static void CopySendInt16(CopyToState cstate, int16 val);
162
163/*
164 * COPY TO routines for built-in formats.
165 */
166
167/* text format */
170 .CopyToOutFunc = CopyToTextLikeOutFunc,
171 .CopyToOneRow = CopyToTextOneRow,
172 .CopyToEnd = CopyToTextLikeEnd,
173};
174
175/* CSV format */
178 .CopyToOutFunc = CopyToTextLikeOutFunc,
179 .CopyToOneRow = CopyToCSVOneRow,
180 .CopyToEnd = CopyToTextLikeEnd,
181};
182
183/* json format */
186 .CopyToOutFunc = CopyToTextLikeOutFunc,
187 .CopyToOneRow = CopyToJsonOneRow,
188 .CopyToEnd = CopyToJsonEnd,
189};
190
191/* binary format */
194 .CopyToOutFunc = CopyToBinaryOutFunc,
195 .CopyToOneRow = CopyToBinaryOneRow,
196 .CopyToEnd = CopyToBinaryEnd,
197};
198
199/* Return a COPY TO routine for the given options */
200static const CopyToRoutine *
202{
203 if (opts->format == COPY_FORMAT_CSV)
204 return &CopyToRoutineCSV;
205 else if (opts->format == COPY_FORMAT_BINARY)
206 return &CopyToRoutineBinary;
207 else if (opts->format == COPY_FORMAT_JSON)
208 return &CopyToRoutineJson;
209
210 /* default is text */
211 return &CopyToRoutineText;
212}
213
214/* Implementation of the start callback for text, CSV, and json formats */
215static void
217{
218 /*
219 * For non-binary copy, we need to convert null_print to file encoding,
220 * because it will be sent directly with CopySendString.
221 */
222 if (cstate->need_transcoding)
224 cstate->opts.null_print_len,
225 cstate->file_encoding);
226
227 /* if a header has been requested send the line */
228 if (cstate->opts.header_line == COPY_HEADER_TRUE)
229 {
230 ListCell *cur;
231 bool hdr_delim = false;
232
234
235 foreach(cur, cstate->attnumlist)
236 {
237 int attnum = lfirst_int(cur);
238 char *colname;
239
240 if (hdr_delim)
241 CopySendChar(cstate, cstate->opts.delim[0]);
242 hdr_delim = true;
243
244 colname = NameStr(TupleDescAttr(tupDesc, attnum - 1)->attname);
245
246 if (cstate->opts.format == COPY_FORMAT_CSV)
247 CopyAttributeOutCSV(cstate, colname, false);
248 else
249 CopyAttributeOutText(cstate, colname);
250 }
251
253 }
254
255 /*
256 * If FORCE_ARRAY has been specified, send the opening bracket.
257 */
258 if (cstate->opts.format == COPY_FORMAT_JSON && cstate->opts.force_array)
259 {
260 CopySendChar(cstate, '[');
262 }
263}
264
265/*
266 * Implementation of the outfunc callback for text, CSV, and json formats. Assign
267 * the output function data to the given *finfo.
268 */
269static void
271{
273 bool is_varlena;
274
275 /* Set output function for an attribute */
277 fmgr_info(func_oid, finfo);
278}
279
280/* Implementation of the per-row callback for text format */
281static void
283{
284 CopyToTextLikeOneRow(cstate, slot, false);
285}
286
287/* Implementation of the per-row callback for CSV format */
288static void
290{
291 CopyToTextLikeOneRow(cstate, slot, true);
292}
293
294/*
295 * Workhorse for CopyToTextOneRow() and CopyToCSVOneRow().
296 *
297 * We use pg_attribute_always_inline to reduce function call overhead
298 * and to help compilers to optimize away the 'is_csv' condition.
299 */
302 TupleTableSlot *slot,
303 bool is_csv)
304{
305 bool need_delim = false;
306 FmgrInfo *out_functions = cstate->out_functions;
307
309 {
310 Datum value = slot->tts_values[attnum - 1];
311 bool isnull = slot->tts_isnull[attnum - 1];
312
313 if (need_delim)
314 CopySendChar(cstate, cstate->opts.delim[0]);
315 need_delim = true;
316
317 if (isnull)
318 {
319 CopySendString(cstate, cstate->opts.null_print_client);
320 }
321 else
322 {
323 char *string;
324
325 string = OutputFunctionCall(&out_functions[attnum - 1],
326 value);
327
328 if (is_csv)
329 CopyAttributeOutCSV(cstate, string,
330 cstate->opts.force_quote_flags[attnum - 1]);
331 else
332 CopyAttributeOutText(cstate, string);
333 }
334 }
335
337}
338
339/* Implementation of the end callback for text and CSV formats */
340static void
342{
343 /* Nothing to do here */
344}
345
346/* Implementation of the end callback for json format */
347static void
349{
350 if (cstate->opts.force_array)
351 {
352 CopySendChar(cstate, ']');
354 }
355}
356
357/* Implementation of per-row callback for json format */
358static void
360{
362
363 resetStringInfo(cstate->json_buf);
364
365 if (cstate->json_projvalues != NULL)
366 {
367 /*
368 * Column list case: project selected column values into sequential
369 * positions matching the custom TupleDesc, then form a new tuple.
370 */
372 int i = 0;
373
375 {
376 cstate->json_projvalues[i] = slot->tts_values[attnum - 1];
377 cstate->json_projnulls[i] = slot->tts_isnull[attnum - 1];
378 i++;
379 }
380
381 tup = heap_form_tuple(cstate->tupDesc,
382 cstate->json_projvalues,
383 cstate->json_projnulls);
384
385 /*
386 * heap_form_tuple already stamps the datum-length, type-id, and
387 * type-mod fields on t_data, so we can use it directly as a composite
388 * Datum without the extra pallocmemcpy that heap_copy_tuple_as_datum
389 * would do. Any TOAST pointers in the projected values will be
390 * detoasted by the per-column output functions called from
391 * composite_to_json.
392 */
394 }
395 else
396 {
397 /*
398 * Full table or query without column list. For queries, the slot's
399 * TupleDesc may carry RECORDOID, which is not registered in the type
400 * cache and would cause composite_to_json's lookup_rowtype_tupdesc
401 * call to fail. Build a HeapTuple stamped with the blessed
402 * descriptor so the type can be looked up correctly.
403 */
404 if (!cstate->rel && slot->tts_tupleDescriptor->tdtypeid == RECORDOID)
405 {
407 slot->tts_values,
408 slot->tts_isnull);
409
411 }
412 else
414 }
415
416 composite_to_json(rowdata, cstate->json_buf, false);
417
418 if (cstate->opts.force_array)
419 {
420 if (cstate->json_row_delim_needed)
421 CopySendChar(cstate, ',');
422 else
423 {
424 /* first row needs no delimiter */
425 CopySendChar(cstate, ' ');
426 cstate->json_row_delim_needed = true;
427 }
428 }
429
430 CopySendData(cstate, cstate->json_buf->data, cstate->json_buf->len);
431
433}
434
435/*
436 * Implementation of the start callback for binary format. Send a header
437 * for a binary copy.
438 */
439static void
441{
442 int32 tmp;
443
444 /* Signature */
445 CopySendData(cstate, BinarySignature, 11);
446 /* Flags field */
447 tmp = 0;
448 CopySendInt32(cstate, tmp);
449 /* No header extension */
450 tmp = 0;
451 CopySendInt32(cstate, tmp);
452}
453
454/*
455 * Implementation of the outfunc callback for binary format. Assign
456 * the binary output function to the given *finfo.
457 */
458static void
460{
462 bool is_varlena;
463
464 /* Set output function for an attribute */
466 fmgr_info(func_oid, finfo);
467}
468
469/* Implementation of the per-row callback for binary format */
470static void
472{
473 FmgrInfo *out_functions = cstate->out_functions;
474
475 /* Binary per-tuple header */
476 CopySendInt16(cstate, list_length(cstate->attnumlist));
477
479 {
480 Datum value = slot->tts_values[attnum - 1];
481 bool isnull = slot->tts_isnull[attnum - 1];
482
483 if (isnull)
484 {
485 CopySendInt32(cstate, -1);
486 }
487 else
488 {
490
491 outputbytes = SendFunctionCall(&out_functions[attnum - 1],
492 value);
496 }
497 }
498
499 CopySendEndOfRow(cstate);
500}
501
502/* Implementation of the end callback for binary format */
503static void
505{
506 /* Generate trailer for a binary copy */
507 CopySendInt16(cstate, -1);
508 /* Need to flush out the trailer */
509 CopySendEndOfRow(cstate);
510}
511
512/*
513 * Send copy start/stop messages for frontend copies. These have changed
514 * in past protocol redesigns.
515 */
516static void
518{
520 int natts = list_length(cstate->attnumlist);
521 int16 format = (cstate->opts.format == COPY_FORMAT_BINARY ? 1 : 0);
522 int i;
523
525 pq_sendbyte(&buf, format); /* overall format */
526 if (cstate->opts.format != COPY_FORMAT_JSON)
527 {
528 pq_sendint16(&buf, natts);
529 for (i = 0; i < natts; i++)
530 pq_sendint16(&buf, format); /* per-column formats */
531 }
532 else
533 {
534 /*
535 * For JSON format, report one text-format column. Each CopyData
536 * message contains one complete JSON object, not individual column
537 * values, so the per-column count is always 1.
538 */
539 pq_sendint16(&buf, 1);
540 pq_sendint16(&buf, 0);
541 }
542
544 cstate->copy_dest = COPY_FRONTEND;
545}
546
547static void
549{
550 /* Shouldn't have any unsent data */
551 Assert(cstate->fe_msgbuf->len == 0);
552 /* Send Copy Done message */
554}
555
556/*----------
557 * CopySendData sends output data to the destination (file or frontend)
558 * CopySendString does the same for null-terminated strings
559 * CopySendChar does the same for single characters
560 * CopySendEndOfRow does the appropriate thing at end of each data row
561 * (data is not actually flushed except by CopySendEndOfRow)
562 *
563 * NB: no data conversion is applied by these functions
564 *----------
565 */
566static void
567CopySendData(CopyToState cstate, const void *databuf, int datasize)
568{
570}
571
572static void
573CopySendString(CopyToState cstate, const char *str)
574{
576}
577
578static void
580{
582}
583
584static void
586{
587 StringInfo fe_msgbuf = cstate->fe_msgbuf;
588
589 switch (cstate->copy_dest)
590 {
591 case COPY_FILE:
593 if (fwrite(fe_msgbuf->data, fe_msgbuf->len, 1,
594 cstate->copy_file) != 1 ||
595 ferror(cstate->copy_file))
596 {
597 if (cstate->is_program)
598 {
599 if (errno == EPIPE)
600 {
601 /*
602 * The pipe will be closed automatically on error at
603 * the end of transaction, but we might get a better
604 * error message from the subprocess' exit code than
605 * just "Broken Pipe"
606 */
607 ClosePipeToProgram(cstate);
608
609 /*
610 * If ClosePipeToProgram() didn't throw an error, the
611 * program terminated normally, but closed the pipe
612 * first. Restore errno, and throw an error.
613 */
614 errno = EPIPE;
615 }
618 errmsg("could not write to COPY program: %m")));
619 }
620 else
623 errmsg("could not write to COPY file: %m")));
624 }
626 break;
627 case COPY_FRONTEND:
628 /* Dump the accumulated row as one CopyData message */
629 (void) pq_putmessage(PqMsg_CopyData, fe_msgbuf->data, fe_msgbuf->len);
630 break;
631 case COPY_CALLBACK:
632 cstate->data_dest_cb(fe_msgbuf->data, fe_msgbuf->len);
633 break;
634 }
635
636 /* Update the progress */
637 cstate->bytes_processed += fe_msgbuf->len;
639
640 resetStringInfo(fe_msgbuf);
641}
642
643/*
644 * Wrapper function of CopySendEndOfRow for text, CSV, and json formats. Sends the
645 * line termination and do common appropriate things for the end of row.
646 */
647static inline void
649{
650 switch (cstate->copy_dest)
651 {
652 case COPY_FILE:
653 /* Default line termination depends on platform */
654#ifndef WIN32
655 CopySendChar(cstate, '\n');
656#else
657 CopySendString(cstate, "\r\n");
658#endif
659 break;
660 case COPY_FRONTEND:
661 /* The FE/BE protocol uses \n as newline for all platforms */
662 CopySendChar(cstate, '\n');
663 break;
664 default:
665 break;
666 }
667
668 /* Now take the actions related to the end of a row */
669 CopySendEndOfRow(cstate);
670}
671
672/*
673 * These functions do apply some data conversion
674 */
675
676/*
677 * CopySendInt32 sends an int32 in network byte order
678 */
679static inline void
681{
682 uint32 buf;
683
684 buf = pg_hton32((uint32) val);
685 CopySendData(cstate, &buf, sizeof(buf));
686}
687
688/*
689 * CopySendInt16 sends an int16 in network byte order
690 */
691static inline void
693{
694 uint16 buf;
695
696 buf = pg_hton16((uint16) val);
697 CopySendData(cstate, &buf, sizeof(buf));
698}
699
700/*
701 * Closes the pipe to an external program, checking the pclose() return code.
702 */
703static void
705{
706 int pclose_rc;
707
708 Assert(cstate->is_program);
709
711 if (pclose_rc == -1)
714 errmsg("could not close pipe to external command: %m")));
715 else if (pclose_rc != 0)
716 {
719 errmsg("program \"%s\" failed",
720 cstate->filename),
722 }
723}
724
725/*
726 * Release resources allocated in a cstate for COPY TO.
727 */
728static void
730{
731 if (cstate->is_program)
732 {
733 ClosePipeToProgram(cstate);
734 }
735 else
736 {
737 if (cstate->filename != NULL && FreeFile(cstate->copy_file))
740 errmsg("could not close file \"%s\": %m",
741 cstate->filename)));
742 }
743
745
747
748 if (cstate->partitions)
749 list_free(cstate->partitions);
750
751 pfree(cstate);
752}
753
754/*
755 * Setup CopyToState to read tuples from a table or a query for COPY TO.
756 *
757 * 'rel': Relation to be copied
758 * 'raw_query': Query whose results are to be copied
759 * 'queryRelId': OID of base relation to convert to a query (for RLS)
760 * 'filename': Name of server-local file to write, NULL for STDOUT
761 * 'is_program': true if 'filename' is program to execute
762 * 'data_dest_cb': Callback that processes the output data
763 * 'attnamelist': List of char *, columns to include. NIL selects all cols.
764 * 'options': List of DefElem. See copy_opt_item in gram.y for selections.
765 *
766 * Returns a CopyToState, to be passed to DoCopyTo() and related functions.
767 */
770 Relation rel,
773 const char *filename,
774 bool is_program,
775 copy_data_dest_cb data_dest_cb,
777 List *options)
778{
779 CopyToState cstate;
780 bool pipe = (filename == NULL && data_dest_cb == NULL);
781 TupleDesc tupDesc;
782 int num_phys_attrs;
783 MemoryContext oldcontext;
784 const int progress_cols[] = {
787 };
788 int64 progress_vals[] = {
790 0
791 };
792 List *children = NIL;
793
794 if (rel != NULL && rel->rd_rel->relkind != RELKIND_RELATION)
795 {
796 if (rel->rd_rel->relkind == RELKIND_VIEW)
799 errmsg("cannot copy from view \"%s\"",
801 errhint("Try the COPY (SELECT ...) TO variant.")));
802 else if (rel->rd_rel->relkind == RELKIND_MATVIEW)
803 {
804 if (!RelationIsPopulated(rel))
807 errmsg("cannot copy from unpopulated materialized view \"%s\"",
809 errhint("Use the REFRESH MATERIALIZED VIEW command."));
810 }
811 else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
814 errmsg("cannot copy from foreign table \"%s\"",
816 errhint("Try the COPY (SELECT ...) TO variant.")));
817 else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
820 errmsg("cannot copy from sequence \"%s\"",
822 else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
823 {
824 /*
825 * Collect OIDs of relation containing data, so that later
826 * DoCopyTo can copy the data from them.
827 */
829
830 foreach_oid(child, children)
831 {
832 char relkind = get_rel_relkind(child);
833
834 if (relkind == RELKIND_FOREIGN_TABLE)
835 {
836 char *relation_name = get_rel_name(child);
837
840 errmsg("cannot copy from foreign table \"%s\"", relation_name),
841 errdetail("Partition \"%s\" is a foreign table in partitioned table \"%s\"",
842 relation_name, RelationGetRelationName(rel)),
843 errhint("Try the COPY (SELECT ...) TO variant."));
844 }
845
846 /* Exclude tables with no data */
847 if (RELKIND_HAS_PARTITIONS(relkind))
848 children = foreach_delete_current(children, child);
849 }
850 }
851 else
854 errmsg("cannot copy from non-table relation \"%s\"",
856 }
857
858
859 /* Allocate workspace and zero all fields */
861
862 /*
863 * We allocate everything used by a cstate in a new memory context. This
864 * avoids memory leaks during repeated use of COPY in a query.
865 */
867 "COPY",
869
870 oldcontext = MemoryContextSwitchTo(cstate->copycontext);
871
872 /* Extract options from the statement node tree */
873 ProcessCopyOptions(pstate, &cstate->opts, false /* is_from */ , options);
874
875 /* Set format routine */
876 cstate->routine = CopyToGetRoutine(&cstate->opts);
877
878 /* Process the source/target relation or query */
879 if (rel)
880 {
882
883 cstate->rel = rel;
884
885 tupDesc = RelationGetDescr(cstate->rel);
886 cstate->partitions = children;
887 cstate->tupDesc = tupDesc;
888 }
889 else
890 {
892 Query *query;
894 DestReceiver *dest;
895
896 cstate->rel = NULL;
897 cstate->partitions = NIL;
898
899 /*
900 * Run parse analysis and rewrite. Note this also acquires sufficient
901 * locks on the source table(s).
902 */
904 pstate->p_sourcetext, NULL, 0,
905 NULL);
906
907 /* check that we got back something we can work with */
908 if (rewritten == NIL)
909 {
912 errmsg("DO INSTEAD NOTHING rules are not supported for COPY")));
913 }
914 else if (list_length(rewritten) > 1)
915 {
916 ListCell *lc;
917
918 /* examine queries to determine which error message to issue */
919 foreach(lc, rewritten)
920 {
921 Query *q = lfirst_node(Query, lc);
922
923 if (q->querySource == QSRC_QUAL_INSTEAD_RULE)
926 errmsg("conditional DO INSTEAD rules are not supported for COPY")));
927 if (q->querySource == QSRC_NON_INSTEAD_RULE)
930 errmsg("DO ALSO rules are not supported for COPY")));
931 }
932
935 errmsg("multi-statement DO INSTEAD rules are not supported for COPY")));
936 }
937
938 query = linitial_node(Query, rewritten);
939
940 /* The grammar allows SELECT INTO, but we don't support that */
941 if (query->utilityStmt != NULL &&
945 errmsg("COPY (SELECT INTO) is not supported")));
946
947 /* The only other utility command we could see is NOTIFY */
948 if (query->utilityStmt != NULL)
951 errmsg("COPY query must not be a utility command")));
952
953 /*
954 * Similarly the grammar doesn't enforce the presence of a RETURNING
955 * clause, but this is required here.
956 */
957 if (query->commandType != CMD_SELECT &&
958 query->returningList == NIL)
959 {
960 Assert(query->commandType == CMD_INSERT ||
961 query->commandType == CMD_UPDATE ||
962 query->commandType == CMD_DELETE ||
963 query->commandType == CMD_MERGE);
964
967 errmsg("COPY query must have a RETURNING clause")));
968 }
969
970 /* plan the query */
971 plan = pg_plan_query(query, pstate->p_sourcetext,
973
974 /*
975 * With row-level security and a user using "COPY relation TO", we
976 * have to convert the "COPY relation TO" to a query-based COPY (eg:
977 * "COPY (SELECT * FROM ONLY relation) TO"), to allow the rewriter to
978 * add in any RLS clauses.
979 *
980 * When this happens, we are passed in the relid of the originally
981 * found relation (which we have locked). As the planner will look up
982 * the relation again, we double-check here to make sure it found the
983 * same one that we have locked.
984 */
985 if (queryRelId != InvalidOid)
986 {
987 /*
988 * Note that with RLS involved there may be multiple relations,
989 * and while the one we need is almost certainly first, we don't
990 * make any guarantees of that in the planner, so check the whole
991 * list and make sure we find the original relation.
992 */
993 if (!list_member_oid(plan->relationOids, queryRelId))
996 errmsg("relation referenced by COPY statement has changed")));
997 }
998
999 /*
1000 * Use a snapshot with an updated command ID to ensure this query sees
1001 * results of any previously executed queries.
1002 */
1005
1006 /* Create dest receiver for COPY OUT */
1008 ((DR_copy *) dest)->cstate = cstate;
1009
1010 /* Create a QueryDesc requesting no output */
1011 cstate->queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
1014 dest, NULL, NULL, 0);
1015
1016 /*
1017 * Call ExecutorStart to prepare the plan for execution.
1018 *
1019 * ExecutorStart computes a result tupdesc for us
1020 */
1021 ExecutorStart(cstate->queryDesc, 0);
1022
1023 tupDesc = cstate->queryDesc->tupDesc;
1024 tupDesc = BlessTupleDesc(tupDesc);
1025 cstate->tupDesc = tupDesc;
1026 }
1027
1028 /* Generate or convert list of attributes to process */
1029 cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist);
1030
1031 /* Set up JSON-specific state */
1032 if (cstate->opts.format == COPY_FORMAT_JSON)
1033 {
1034 cstate->json_buf = makeStringInfo();
1035
1036 if (attnamelist != NIL && rel)
1037 {
1038 int natts = list_length(cstate->attnumlist);
1039 TupleDesc resultDesc;
1040
1041 /*
1042 * Build a TupleDesc describing only the selected columns so that
1043 * composite_to_json() emits the right column names and types.
1044 */
1045 resultDesc = CreateTemplateTupleDesc(natts);
1046
1047 foreach_int(attnum, cstate->attnumlist)
1048 {
1049 Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1050
1051 TupleDescInitEntry(resultDesc,
1053 NameStr(attr->attname),
1054 attr->atttypid,
1055 attr->atttypmod,
1056 attr->attndims);
1057 }
1058
1059 TupleDescFinalize(resultDesc);
1060 cstate->tupDesc = BlessTupleDesc(resultDesc);
1061
1062 /*
1063 * Pre-allocate arrays for projecting selected column values into
1064 * sequential positions matching the custom TupleDesc.
1065 */
1066 cstate->json_projvalues = palloc_array(Datum, natts);
1067 cstate->json_projnulls = palloc_array(bool, natts);
1068 }
1069 }
1070
1071 num_phys_attrs = tupDesc->natts;
1072
1073 /* Convert FORCE_QUOTE name list to per-column flags, check validity */
1074 cstate->opts.force_quote_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1075 if (cstate->opts.force_quote_all)
1076 {
1077 MemSet(cstate->opts.force_quote_flags, true, num_phys_attrs * sizeof(bool));
1078 }
1079 else if (cstate->opts.force_quote)
1080 {
1081 List *attnums;
1082 ListCell *cur;
1083
1084 attnums = CopyGetAttnums(tupDesc, cstate->rel, cstate->opts.force_quote);
1085
1086 foreach(cur, attnums)
1087 {
1088 int attnum = lfirst_int(cur);
1089 Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1090
1091 if (!list_member_int(cstate->attnumlist, attnum))
1092 ereport(ERROR,
1094 /*- translator: %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
1095 errmsg("%s column \"%s\" not referenced by COPY",
1096 "FORCE_QUOTE", NameStr(attr->attname))));
1097 cstate->opts.force_quote_flags[attnum - 1] = true;
1098 }
1099 }
1100
1101 /* Use client encoding when ENCODING option is not specified. */
1102 if (cstate->opts.file_encoding < 0)
1104 else
1105 cstate->file_encoding = cstate->opts.file_encoding;
1106
1107 /*
1108 * Set up encoding conversion info if the file and server encodings differ
1109 * (see also pg_server_to_any).
1110 */
1111 if (cstate->file_encoding == GetDatabaseEncoding() ||
1112 cstate->file_encoding == PG_SQL_ASCII)
1113 cstate->need_transcoding = false;
1114 else
1115 cstate->need_transcoding = true;
1116
1117 /* See Multibyte encoding comment above */
1119
1120 cstate->copy_dest = COPY_FILE; /* default */
1121
1122 if (data_dest_cb)
1123 {
1125 cstate->copy_dest = COPY_CALLBACK;
1126 cstate->data_dest_cb = data_dest_cb;
1127 }
1128 else if (pipe)
1129 {
1131
1132 Assert(!is_program); /* the grammar does not allow this */
1134 cstate->copy_file = stdout;
1135 }
1136 else
1137 {
1138 cstate->filename = pstrdup(filename);
1139 cstate->is_program = is_program;
1140
1141 if (is_program)
1142 {
1144 cstate->copy_file = OpenPipeStream(cstate->filename, PG_BINARY_W);
1145 if (cstate->copy_file == NULL)
1146 ereport(ERROR,
1148 errmsg("could not execute command \"%s\": %m",
1149 cstate->filename)));
1150 }
1151 else
1152 {
1153 mode_t oumask; /* Pre-existing umask value */
1154 struct stat st;
1155
1157
1158 /*
1159 * Prevent write to relative path ... too easy to shoot oneself in
1160 * the foot by overwriting a database file ...
1161 */
1163 ereport(ERROR,
1165 errmsg("relative path not allowed for COPY to file")));
1166
1168 PG_TRY();
1169 {
1170 cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1171 }
1172 PG_FINALLY();
1173 {
1174 umask(oumask);
1175 }
1176 PG_END_TRY();
1177 if (cstate->copy_file == NULL)
1178 {
1179 /* copy errno because ereport subfunctions might change it */
1180 int save_errno = errno;
1181
1182 ereport(ERROR,
1184 errmsg("could not open file \"%s\" for writing: %m",
1185 cstate->filename),
1186 (save_errno == ENOENT || save_errno == EACCES) ?
1187 errhint("COPY TO instructs the PostgreSQL server process to write a file. "
1188 "You may want a client-side facility such as psql's \\copy.") : 0));
1189 }
1190
1191 if (fstat(fileno(cstate->copy_file), &st))
1192 ereport(ERROR,
1194 errmsg("could not stat file \"%s\": %m",
1195 cstate->filename)));
1196
1197 if (S_ISDIR(st.st_mode))
1198 ereport(ERROR,
1200 errmsg("\"%s\" is a directory", cstate->filename)));
1201 }
1202 }
1203
1204 /* initialize progress */
1206 cstate->rel ? RelationGetRelid(cstate->rel) : InvalidOid);
1208
1209 cstate->bytes_processed = 0;
1210
1211 MemoryContextSwitchTo(oldcontext);
1212
1213 return cstate;
1214}
1215
1216/*
1217 * Clean up storage and release resources for COPY TO.
1218 */
1219void
1221{
1222 if (cstate->queryDesc != NULL)
1223 {
1224 /* Close down the query and free resources. */
1225 ExecutorFinish(cstate->queryDesc);
1226 ExecutorEnd(cstate->queryDesc);
1227 FreeQueryDesc(cstate->queryDesc);
1229 }
1230
1231 /* Clean up storage */
1232 EndCopy(cstate);
1233}
1234
1235/*
1236 * Copy from relation or query TO file.
1237 *
1238 * Returns the number of rows processed.
1239 */
1240uint64
1242{
1243 bool pipe = (cstate->filename == NULL && cstate->data_dest_cb == NULL);
1244 bool fe_copy = (pipe && whereToSendOutput == DestRemote);
1245 TupleDesc tupDesc;
1246 int num_phys_attrs;
1247 ListCell *cur;
1248 uint64 processed = 0;
1249
1250 if (fe_copy)
1251 SendCopyBegin(cstate);
1252
1253 if (cstate->rel)
1254 tupDesc = RelationGetDescr(cstate->rel);
1255 else
1256 tupDesc = cstate->queryDesc->tupDesc;
1257 num_phys_attrs = tupDesc->natts;
1258 cstate->opts.null_print_client = cstate->opts.null_print; /* default */
1259
1260 /* We use fe_msgbuf as a per-row buffer regardless of copy_dest */
1261 cstate->fe_msgbuf = makeStringInfo();
1262
1263 /* Get info about the columns we need to process. */
1264 cstate->out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
1265 foreach(cur, cstate->attnumlist)
1266 {
1267 int attnum = lfirst_int(cur);
1268 Form_pg_attribute attr = TupleDescAttr(tupDesc, attnum - 1);
1269
1270 cstate->routine->CopyToOutFunc(cstate, attr->atttypid,
1271 &cstate->out_functions[attnum - 1]);
1272 }
1273
1274 /*
1275 * Create a temporary memory context that we can reset once per row to
1276 * recover palloc'd memory. This avoids any problems with leaks inside
1277 * datatype output routines, and should be faster than retail pfree's
1278 * anyway. (We don't need a whole econtext as CopyFrom does.)
1279 */
1281 "COPY TO",
1283
1284 cstate->routine->CopyToStart(cstate, tupDesc);
1285
1286 if (cstate->rel)
1287 {
1288 /*
1289 * If COPY TO source table is a partitioned table, then open each
1290 * partition and process each individual partition.
1291 */
1292 if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1293 {
1294 foreach_oid(child, cstate->partitions)
1295 {
1297
1298 /* We already got the lock in BeginCopyTo */
1299 scan_rel = table_open(child, NoLock);
1300 CopyRelationTo(cstate, scan_rel, cstate->rel, &processed);
1302 }
1303 }
1304 else
1305 CopyRelationTo(cstate, cstate->rel, NULL, &processed);
1306 }
1307 else
1308 {
1309 /* run the plan --- the dest receiver will send tuples */
1311 processed = ((DR_copy *) cstate->queryDesc->dest)->processed;
1312 }
1313
1314 cstate->routine->CopyToEnd(cstate);
1315
1317
1318 if (fe_copy)
1319 SendCopyEnd(cstate);
1320
1321 return processed;
1322}
1323
1324/*
1325 * Scans a single table and exports its rows to the COPY destination.
1326 *
1327 * root_rel can be set to the root table of rel if rel is a partition
1328 * table so that we can send tuples in root_rel's rowtype, which might
1329 * differ from individual partitions.
1330*/
1331static void
1333{
1334 TupleTableSlot *slot;
1335 TableScanDesc scandesc;
1336 AttrMap *map = NULL;
1338
1339 scandesc = table_beginscan(rel, GetActiveSnapshot(), 0, NULL);
1340 slot = table_slot_create(rel, NULL);
1341
1342 /*
1343 * If we are exporting partition data here, we check if converting tuples
1344 * to the root table's rowtype, because a partition might have column
1345 * order different than its root table.
1346 */
1347 if (root_rel != NULL)
1348 {
1351 RelationGetDescr(rel),
1352 false);
1353 }
1354
1355 while (table_scan_getnextslot(scandesc, ForwardScanDirection, slot))
1356 {
1357 TupleTableSlot *copyslot;
1358
1360
1361 if (map != NULL)
1362 copyslot = execute_attr_map_slot(map, slot, root_slot);
1363 else
1364 {
1365 /* Deconstruct the tuple */
1366 slot_getallattrs(slot);
1367 copyslot = slot;
1368 }
1369
1370 /* Format and send the data */
1371 CopyOneRowTo(cstate, copyslot);
1372
1373 /*
1374 * Increment the number of processed tuples, and report the progress.
1375 */
1377 ++(*processed));
1378 }
1379
1381
1382 if (root_slot != NULL)
1384
1385 if (map != NULL)
1386 free_attrmap(map);
1387
1388 table_endscan(scandesc);
1389}
1390
1391/*
1392 * Emit one row during DoCopyTo().
1393 */
1394static inline void
1396{
1397 MemoryContext oldcontext;
1398
1400 oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
1401
1402 /* Make sure the tuple is fully deconstructed */
1403 slot_getallattrs(slot);
1404
1405 cstate->routine->CopyToOneRow(cstate, slot);
1406
1407 MemoryContextSwitchTo(oldcontext);
1408}
1409
1410/*
1411 * Send text representation of one attribute, with conversion and escaping
1412 */
1413#define DUMPSOFAR() \
1414 do { \
1415 if (ptr > start) \
1416 CopySendData(cstate, start, ptr - start); \
1417 } while (0)
1418
1419static void
1420CopyAttributeOutText(CopyToState cstate, const char *string)
1421{
1422 const char *ptr;
1423 const char *start;
1424 char c;
1425 char delimc = cstate->opts.delim[0];
1426
1427 if (cstate->need_transcoding)
1428 ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
1429 else
1430 ptr = string;
1431
1432 /*
1433 * We have to grovel through the string searching for control characters
1434 * and instances of the delimiter character. In most cases, though, these
1435 * are infrequent. To avoid overhead from calling CopySendData once per
1436 * character, we dump out all characters between escaped characters in a
1437 * single call. The loop invariant is that the data from "start" to "ptr"
1438 * can be sent literally, but hasn't yet been.
1439 *
1440 * We can skip pg_encoding_mblen() overhead when encoding is safe, because
1441 * in valid backend encodings, extra bytes of a multibyte character never
1442 * look like ASCII. This loop is sufficiently performance-critical that
1443 * it's worth making two copies of it to get the IS_HIGHBIT_SET() test out
1444 * of the normal safe-encoding path.
1445 */
1446 if (cstate->encoding_embeds_ascii)
1447 {
1448 start = ptr;
1449 while ((c = *ptr) != '\0')
1450 {
1451 if ((unsigned char) c < (unsigned char) 0x20)
1452 {
1453 /*
1454 * \r and \n must be escaped, the others are traditional. We
1455 * prefer to dump these using the C-like notation, rather than
1456 * a backslash and the literal character, because it makes the
1457 * dump file a bit more proof against Microsoftish data
1458 * mangling.
1459 */
1460 switch (c)
1461 {
1462 case '\b':
1463 c = 'b';
1464 break;
1465 case '\f':
1466 c = 'f';
1467 break;
1468 case '\n':
1469 c = 'n';
1470 break;
1471 case '\r':
1472 c = 'r';
1473 break;
1474 case '\t':
1475 c = 't';
1476 break;
1477 case '\v':
1478 c = 'v';
1479 break;
1480 default:
1481 /* If it's the delimiter, must backslash it */
1482 if (c == delimc)
1483 break;
1484 /* All ASCII control chars are length 1 */
1485 ptr++;
1486 continue; /* fall to end of loop */
1487 }
1488 /* if we get here, we need to convert the control char */
1489 DUMPSOFAR();
1490 CopySendChar(cstate, '\\');
1491 CopySendChar(cstate, c);
1492 start = ++ptr; /* do not include char in next run */
1493 }
1494 else if (c == '\\' || c == delimc)
1495 {
1496 DUMPSOFAR();
1497 CopySendChar(cstate, '\\');
1498 start = ptr++; /* we include char in next run */
1499 }
1500 else if (IS_HIGHBIT_SET(c))
1501 ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
1502 else
1503 ptr++;
1504 }
1505 }
1506 else
1507 {
1508 start = ptr;
1509 while ((c = *ptr) != '\0')
1510 {
1511 if ((unsigned char) c < (unsigned char) 0x20)
1512 {
1513 /*
1514 * \r and \n must be escaped, the others are traditional. We
1515 * prefer to dump these using the C-like notation, rather than
1516 * a backslash and the literal character, because it makes the
1517 * dump file a bit more proof against Microsoftish data
1518 * mangling.
1519 */
1520 switch (c)
1521 {
1522 case '\b':
1523 c = 'b';
1524 break;
1525 case '\f':
1526 c = 'f';
1527 break;
1528 case '\n':
1529 c = 'n';
1530 break;
1531 case '\r':
1532 c = 'r';
1533 break;
1534 case '\t':
1535 c = 't';
1536 break;
1537 case '\v':
1538 c = 'v';
1539 break;
1540 default:
1541 /* If it's the delimiter, must backslash it */
1542 if (c == delimc)
1543 break;
1544 /* All ASCII control chars are length 1 */
1545 ptr++;
1546 continue; /* fall to end of loop */
1547 }
1548 /* if we get here, we need to convert the control char */
1549 DUMPSOFAR();
1550 CopySendChar(cstate, '\\');
1551 CopySendChar(cstate, c);
1552 start = ++ptr; /* do not include char in next run */
1553 }
1554 else if (c == '\\' || c == delimc)
1555 {
1556 DUMPSOFAR();
1557 CopySendChar(cstate, '\\');
1558 start = ptr++; /* we include char in next run */
1559 }
1560 else
1561 ptr++;
1562 }
1563 }
1564
1565 DUMPSOFAR();
1566}
1567
1568/*
1569 * Send text representation of one attribute, with conversion and
1570 * CSV-style escaping
1571 */
1572static void
1573CopyAttributeOutCSV(CopyToState cstate, const char *string,
1574 bool use_quote)
1575{
1576 const char *ptr;
1577 const char *start;
1578 char c;
1579 char delimc = cstate->opts.delim[0];
1580 char quotec = cstate->opts.quote[0];
1581 char escapec = cstate->opts.escape[0];
1582 bool single_attr = (list_length(cstate->attnumlist) == 1);
1583
1584 /* force quoting if it matches null_print (before conversion!) */
1585 if (!use_quote && strcmp(string, cstate->opts.null_print) == 0)
1586 use_quote = true;
1587
1588 if (cstate->need_transcoding)
1589 ptr = pg_server_to_any(string, strlen(string), cstate->file_encoding);
1590 else
1591 ptr = string;
1592
1593 /*
1594 * Make a preliminary pass to discover if it needs quoting
1595 */
1596 if (!use_quote)
1597 {
1598 /*
1599 * Quote '\.' if it appears alone on a line, so that it will not be
1600 * interpreted as an end-of-data marker. (PG 18 and up will not
1601 * interpret '\.' in CSV that way, except in embedded-in-SQL data; but
1602 * we want the data to be loadable by older versions too. Also, this
1603 * avoids breaking clients that are still using PQgetline().)
1604 */
1605 if (single_attr && strcmp(ptr, "\\.") == 0)
1606 use_quote = true;
1607 else
1608 {
1609 const char *tptr = ptr;
1610
1611 while ((c = *tptr) != '\0')
1612 {
1613 if (c == delimc || c == quotec || c == '\n' || c == '\r')
1614 {
1615 use_quote = true;
1616 break;
1617 }
1618 if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
1620 else
1621 tptr++;
1622 }
1623 }
1624 }
1625
1626 if (use_quote)
1627 {
1628 CopySendChar(cstate, quotec);
1629
1630 /*
1631 * We adopt the same optimization strategy as in CopyAttributeOutText
1632 */
1633 start = ptr;
1634 while ((c = *ptr) != '\0')
1635 {
1636 if (c == quotec || c == escapec)
1637 {
1638 DUMPSOFAR();
1639 CopySendChar(cstate, escapec);
1640 start = ptr; /* we include char in next run */
1641 }
1642 if (IS_HIGHBIT_SET(c) && cstate->encoding_embeds_ascii)
1643 ptr += pg_encoding_mblen(cstate->file_encoding, ptr);
1644 else
1645 ptr++;
1646 }
1647 DUMPSOFAR();
1648
1649 CopySendChar(cstate, quotec);
1650 }
1651 else
1652 {
1653 /* If it doesn't need quoting, we can just dump it as-is */
1654 CopySendString(cstate, ptr);
1655 }
1656}
1657
1658/*
1659 * copy_dest_startup --- executor startup
1660 */
1661static void
1663{
1664 /* no-op */
1665}
1666
1667/*
1668 * copy_dest_receive --- receive one tuple
1669 */
1670static bool
1672{
1673 DR_copy *myState = (DR_copy *) self;
1674 CopyToState cstate = myState->cstate;
1675
1676 /* Send the data */
1677 CopyOneRowTo(cstate, slot);
1678
1679 /* Increment the number of processed tuples, and report the progress */
1681 ++myState->processed);
1682
1683 return true;
1684}
1685
1686/*
1687 * copy_dest_shutdown --- executor end
1688 */
1689static void
1691{
1692 /* no-op */
1693}
1694
1695/*
1696 * copy_dest_destroy --- release DestReceiver object
1697 */
1698static void
1700{
1701 pfree(self);
1702}
1703
1704/*
1705 * CreateCopyDestReceiver -- create a suitable DestReceiver object
1706 */
1709{
1710 DR_copy *self = palloc_object(DR_copy);
1711
1716 self->pub.mydest = DestCopyOut;
1717
1718 self->cstate = NULL; /* will be set later */
1719 self->processed = 0;
1720
1721 return (DestReceiver *) self;
1722}
void free_attrmap(AttrMap *map)
Definition attmap.c:56
AttrMap * build_attrmap_by_name_if_req(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
Definition attmap.c:261
List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
Definition copy.c:1048
void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *opts_out, bool is_from, List *options)
Definition copy.c:561
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
void pgstat_progress_end_command(void)
@ PROGRESS_COMMAND_COPY
#define NameStr(name)
Definition c.h:837
#define IS_HIGHBIT_SET(ch)
Definition c.h:1246
#define VARHDRSZ
Definition c.h:783
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
#define pg_attribute_always_inline
Definition c.h:299
int16_t int16
Definition c.h:613
int32_t int32
Definition c.h:614
uint64_t uint64
Definition c.h:619
uint16_t uint16
Definition c.h:617
uint32_t uint32
Definition c.h:618
#define PG_BINARY_W
Definition c.h:1379
#define MemSet(start, val, len)
Definition c.h:1109
static void CopyToBinaryOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo)
Definition copyto.c:459
static void CopyToBinaryOneRow(CopyToState cstate, TupleTableSlot *slot)
Definition copyto.c:471
static void CopySendInt32(CopyToState cstate, int32 val)
Definition copyto.c:680
static void ClosePipeToProgram(CopyToState cstate)
Definition copyto.c:704
static const CopyToRoutine CopyToRoutineCSV
Definition copyto.c:176
static bool copy_dest_receive(TupleTableSlot *slot, DestReceiver *self)
Definition copyto.c:1671
static void CopyAttributeOutCSV(CopyToState cstate, const char *string, bool use_quote)
Definition copyto.c:1573
uint64 DoCopyTo(CopyToState cstate)
Definition copyto.c:1241
static void CopyToTextLikeEnd(CopyToState cstate)
Definition copyto.c:341
static void CopyAttributeOutText(CopyToState cstate, const char *string)
Definition copyto.c:1420
#define DUMPSOFAR()
Definition copyto.c:1413
static const CopyToRoutine CopyToRoutineText
Definition copyto.c:168
static void CopySendInt16(CopyToState cstate, int16 val)
Definition copyto.c:692
static void CopySendData(CopyToState cstate, const void *databuf, int datasize)
Definition copyto.c:567
static void CopyToTextOneRow(CopyToState cstate, TupleTableSlot *slot)
Definition copyto.c:282
static void CopySendChar(CopyToState cstate, char c)
Definition copyto.c:579
DestReceiver * CreateCopyDestReceiver(void)
Definition copyto.c:1708
static const CopyToRoutine * CopyToGetRoutine(const CopyFormatOptions *opts)
Definition copyto.c:201
static void CopySendTextLikeEndOfRow(CopyToState cstate)
Definition copyto.c:648
static void CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel, uint64 *processed)
Definition copyto.c:1332
static void EndCopy(CopyToState cstate)
Definition copyto.c:729
static void copy_dest_destroy(DestReceiver *self)
Definition copyto.c:1699
CopyDest
Definition copyto.c:50
@ COPY_FILE
Definition copyto.c:51
@ COPY_CALLBACK
Definition copyto.c:53
@ COPY_FRONTEND
Definition copyto.c:52
static void CopyToTextLikeOutFunc(CopyToState cstate, Oid atttypid, FmgrInfo *finfo)
Definition copyto.c:270
CopyToState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *raw_query, Oid queryRelId, const char *filename, bool is_program, copy_data_dest_cb data_dest_cb, List *attnamelist, List *options)
Definition copyto.c:769
static void copy_dest_shutdown(DestReceiver *self)
Definition copyto.c:1690
static void copy_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition copyto.c:1662
static void CopyToTextLikeStart(CopyToState cstate, TupleDesc tupDesc)
Definition copyto.c:216
static void SendCopyBegin(CopyToState cstate)
Definition copyto.c:517
static void SendCopyEnd(CopyToState cstate)
Definition copyto.c:548
static void CopySendEndOfRow(CopyToState cstate)
Definition copyto.c:585
static void CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
Definition copyto.c:1395
static void CopyToTextLikeOneRow(CopyToState cstate, TupleTableSlot *slot, bool is_csv)
Definition copyto.c:301
static void CopyToJsonOneRow(CopyToState cstate, TupleTableSlot *slot)
Definition copyto.c:359
static void CopySendString(CopyToState cstate, const char *str)
Definition copyto.c:573
static void CopyToCSVOneRow(CopyToState cstate, TupleTableSlot *slot)
Definition copyto.c:289
static void CopyToJsonEnd(CopyToState cstate)
Definition copyto.c:348
static const char BinarySignature[11]
Definition copyto.c:124
void EndCopyTo(CopyToState cstate)
Definition copyto.c:1220
static void CopyToBinaryStart(CopyToState cstate, TupleDesc tupDesc)
Definition copyto.c:440
static const CopyToRoutine CopyToRoutineJson
Definition copyto.c:184
static const CopyToRoutine CopyToRoutineBinary
Definition copyto.c:192
static void CopyToBinaryEnd(CopyToState cstate)
Definition copyto.c:504
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition dest.c:113
@ DestRemote
Definition dest.h:89
@ DestCopyOut
Definition dest.h:95
struct cursor * cur
Definition ecpg.c:29
int errcode_for_file_access(void)
Definition elog.c:897
int errcode(int sqlerrcode)
Definition elog.c:874
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define ERROR
Definition elog.h:39
#define PG_FINALLY(...)
Definition elog.h:389
#define ereport(elevel,...)
Definition elog.h:150
void ExecutorEnd(QueryDesc *queryDesc)
Definition execMain.c:468
void ExecutorFinish(QueryDesc *queryDesc)
Definition execMain.c:408
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition execMain.c:124
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
Definition execMain.c:299
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Datum ExecFetchSlotHeapTupleDatum(TupleTableSlot *slot)
FILE * OpenPipeStream(const char *command, const char *mode)
Definition fd.c:2731
int ClosePipeStream(FILE *file)
Definition fd.c:3039
int FreeFile(FILE *file)
Definition fd.c:2827
FILE * AllocateFile(const char *name, const char *mode)
Definition fd.c:2628
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define palloc0_object(type)
Definition fe_memutils.h:75
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition fmgr.c:129
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition fmgr.c:1745
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition fmgr.c:1684
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition funcapi.h:230
return str start
const char * str
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1037
@ COPY_FORMAT_CSV
Definition copy.h:59
@ COPY_FORMAT_JSON
Definition copy.h:60
@ COPY_FORMAT_BINARY
Definition copy.h:58
void(* copy_data_dest_cb)(void *data, int len)
Definition copy.h:107
#define COPY_HEADER_TRUE
Definition copy.h:28
long val
Definition informix.c:689
static struct @174 value
int i
Definition isn.c:77
void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
Definition json.c:521
#define pq_putmessage(msgtype, s, len)
Definition libpq.h:52
void list_free(List *list)
Definition list.c:1546
bool list_member_int(const List *list, int datum)
Definition list.c:702
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
char * get_rel_name(Oid relid)
Definition lsyscache.c:2148
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition lsyscache.c:3195
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3129
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2223
int GetDatabaseEncoding(void)
Definition mbutils.c:1389
int pg_get_client_encoding(void)
Definition mbutils.c:345
char * pg_server_to_any(const char *s, int len, int encoding)
Definition mbutils.c:760
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:403
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc0(Size size)
Definition mcxt.c:1417
void * palloc(Size size)
Definition mcxt.c:1387
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
#define IsA(nodeptr, _type_)
Definition nodes.h:164
@ CMD_MERGE
Definition nodes.h:279
@ CMD_INSERT
Definition nodes.h:277
@ CMD_DELETE
Definition nodes.h:278
@ CMD_UPDATE
Definition nodes.h:276
@ CMD_SELECT
Definition nodes.h:275
static char * errmsg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
@ QSRC_NON_INSTEAD_RULE
Definition parsenodes.h:40
@ QSRC_QUAL_INSTEAD_RULE
Definition parsenodes.h:39
#define CURSOR_OPT_PARALLEL_OK
static AmcheckOptions opts
Definition pg_amcheck.c:112
NameData attname
int16 attnum
FormData_pg_attribute * Form_pg_attribute
static char format
#define pg_hton32(x)
Definition pg_bswap.h:121
#define pg_hton16(x)
Definition pg_bswap.h:120
static char * filename
Definition pg_dumpall.c:133
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial_node(type, l)
Definition pg_list.h:181
#define NIL
Definition pg_list.h:68
#define foreach_current_index(var_or_cell)
Definition pg_list.h:403
#define lfirst_int(lc)
Definition pg_list.h:173
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:391
#define foreach_oid(var, lst)
Definition pg_list.h:471
#define foreach_int(var, lst)
Definition pg_list.h:470
#define plan(x)
Definition pg_regress.c:161
static char buf[DEFAULT_XLOG_SEG_SIZE]
@ PG_SQL_ASCII
Definition pg_wchar.h:226
#define PG_ENCODING_IS_CLIENT_ONLY(_enc)
Definition pg_wchar.h:284
#define is_absolute_path(filename)
Definition port.h:104
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams, ExplainState *es)
Definition postgres.c:887
CommandDest whereToSendOutput
Definition postgres.c:94
List * pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree, const char *query_string, const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition postgres.c:670
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
void pq_putemptymessage(char msgtype)
Definition pqformat.c:387
void pq_endmessage(StringInfo buf)
Definition pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition pqformat.c:88
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition pqformat.h:160
static void pq_sendint16(StringInfo buf, uint16 i)
Definition pqformat.h:136
void FreeQueryDesc(QueryDesc *qdesc)
Definition pquery.c:106
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition pquery.c:68
char * c
static int fb(int x)
char string[11]
#define PROGRESS_COPY_COMMAND
Definition progress.h:174
#define PROGRESS_COPY_TYPE_FILE
Definition progress.h:183
#define PROGRESS_COPY_BYTES_PROCESSED
Definition progress.h:170
#define PROGRESS_COPY_COMMAND_TO
Definition progress.h:180
#define PROGRESS_COPY_TUPLES_PROCESSED
Definition progress.h:172
#define PROGRESS_COPY_TYPE
Definition progress.h:175
#define PROGRESS_COPY_TYPE_PROGRAM
Definition progress.h:184
#define PROGRESS_COPY_TYPE_CALLBACK
Definition progress.h:186
#define PROGRESS_COPY_TYPE_PIPE
Definition progress.h:185
#define PqMsg_CopyDone
Definition protocol.h:64
#define PqMsg_CopyData
Definition protocol.h:65
#define PqMsg_CopyOutResponse
Definition protocol.h:46
#define RelationGetRelid(relation)
Definition rel.h:514
#define RelationGetDescr(relation)
Definition rel.h:540
#define RelationGetRelationName(relation)
Definition rel.h:548
#define RelationIsPopulated(relation)
Definition rel.h:686
@ ForwardScanDirection
Definition sdir.h:28
void UpdateActiveSnapshotCommandId(void)
Definition snapmgr.c:744
void PopActiveSnapshot(void)
Definition snapmgr.c:775
void PushCopiedSnapshot(Snapshot snapshot)
Definition snapmgr.c:732
Snapshot GetActiveSnapshot(void)
Definition snapmgr.c:800
#define InvalidSnapshot
Definition snapshot.h:119
StringInfo makeStringInfo(void)
Definition stringinfo.c:72
void resetStringInfo(StringInfo str)
Definition stringinfo.c:126
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition stringinfo.c:281
#define appendStringInfoCharMacro(str, ch)
Definition stringinfo.h:231
int header_line
Definition copy.h:75
CopyFormat format
Definition copy.h:73
bool force_quote_all
Definition copy.h:86
int null_print_len
Definition copy.h:78
char * quote
Definition copy.h:83
bool force_array
Definition copy.h:91
List * force_quote
Definition copy.h:85
char * escape
Definition copy.h:84
char * null_print
Definition copy.h:77
char * delim
Definition copy.h:82
bool * force_quote_flags
Definition copy.h:87
char * null_print_client
Definition copy.h:79
int file_encoding
Definition copy.h:71
void(* CopyToOutFunc)(CopyToState cstate, Oid atttypid, FmgrInfo *finfo)
Definition copyapi.h:34
void(* CopyToOneRow)(CopyToState cstate, TupleTableSlot *slot)
Definition copyapi.h:49
void(* CopyToEnd)(CopyToState cstate)
Definition copyapi.h:54
void(* CopyToStart)(CopyToState cstate, TupleDesc tupDesc)
Definition copyapi.h:44
FmgrInfo * out_functions
Definition copyto.c:110
MemoryContext copycontext
Definition copyto.c:108
Node * whereClause
Definition copyto.c:102
Relation rel
Definition copyto.c:86
Datum * json_projvalues
Definition copyto.c:96
const CopyToRoutine * routine
Definition copyto.c:74
copy_data_dest_cb data_dest_cb
Definition copyto.c:99
bool encoding_embeds_ascii
Definition copyto.c:83
CopyDest copy_dest
Definition copyto.c:77
bool need_transcoding
Definition copyto.c:82
bool is_program
Definition copyto.c:90
FILE * copy_file
Definition copyto.c:78
bool * json_projnulls
Definition copyto.c:98
int file_encoding
Definition copyto.c:81
MemoryContext rowcontext
Definition copyto.c:111
CopyFormatOptions opts
Definition copyto.c:101
uint64 bytes_processed
Definition copyto.c:112
bool json_row_delim_needed
Definition copyto.c:91
StringInfo fe_msgbuf
Definition copyto.c:79
char * filename
Definition copyto.c:89
List * attnumlist
Definition copyto.c:88
QueryDesc * queryDesc
Definition copyto.c:87
TupleDesc tupDesc
Definition copyto.c:94
List * partitions
Definition copyto.c:103
StringInfo json_buf
Definition copyto.c:92
CopyToState cstate
Definition copyto.c:119
DestReceiver pub
Definition copyto.c:118
uint64 processed
Definition copyto.c:120
Definition pg_list.h:54
Definition nodes.h:135
const char * p_sourcetext
Definition parse_node.h:210
DestReceiver * dest
Definition execdesc.h:41
TupleDesc tupDesc
Definition execdesc.h:47
List * returningList
Definition parsenodes.h:214
CmdType commandType
Definition parsenodes.h:121
Node * utilityStmt
Definition parsenodes.h:141
Form_pg_class rd_rel
Definition rel.h:111
TupleDesc tts_tupleDescriptor
Definition tuptable.h:129
bool * tts_isnull
Definition tuptable.h:133
Datum * tts_values
Definition tuptable.h:131
void(* rStartup)(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition dest.h:121
void(* rShutdown)(DestReceiver *self)
Definition dest.h:124
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition dest.h:118
void(* rDestroy)(DestReceiver *self)
Definition dest.h:126
CommandDest mydest
Definition dest.h:128
unsigned short st_mode
Definition win32_port.h:258
Definition c.h:778
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition tableam.c:92
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1004
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition tableam.h:1039
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key)
Definition tableam.h:896
TupleTableSlot * execute_attr_map_slot(AttrMap *attrMap, TupleTableSlot *in_slot, TupleTableSlot *out_slot)
Definition tupconvert.c:193
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:511
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:900
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
static void slot_getallattrs(TupleTableSlot *slot)
Definition tuptable.h:390
static Size VARSIZE(const void *PTR)
Definition varatt.h:298
static char * VARDATA(const void *PTR)
Definition varatt.h:305
char * wait_result_to_str(int exitstatus)
Definition wait_error.c:33
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:69
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition wchar.c:2157
#define S_IWOTH
Definition win32_port.h:306
#define S_ISDIR(m)
Definition win32_port.h:315
#define fstat
Definition win32_port.h:73
#define S_IWGRP
Definition win32_port.h:294