OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / doc / postgresql / html / sql-copy.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >COPY</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="SQL Commands"
16 HREF="sql-commands.html"><LINK
17 REL="PREVIOUS"
18 TITLE="COMMIT"
19 HREF="sql-commit.html"><LINK
20 REL="NEXT"
21 TITLE="CREATE AGGREGATE"
22 HREF="sql-createaggregate.html"><LINK
23 REL="STYLESHEET"
24 TYPE="text/css"
25 HREF="stylesheet.css"><META
26 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="REFENTRY"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="sql-commit.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="sql-commit.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 ></TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="sql-createaggregate.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="sql-createaggregate.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><H1
91 ><A
92 NAME="SQL-COPY"
93 ></A
94 >COPY</H1
95 ><DIV
96 CLASS="REFNAMEDIV"
97 ><A
98 NAME="AEN36104"
99 ></A
100 ><H2
101 >Name</H2
102 >COPY&nbsp;--&nbsp;copy data between a file and a table</DIV
103 ><A
104 NAME="AEN36107"
105 ></A
106 ><DIV
107 CLASS="REFSYNOPSISDIV"
108 ><A
109 NAME="AEN36109"
110 ></A
111 ><H2
112 >Synopsis</H2
113 ><PRE
114 CLASS="SYNOPSIS"
115 >COPY <VAR
116 CLASS="REPLACEABLE"
117 >tablename</VAR
118 > [ ( <VAR
119 CLASS="REPLACEABLE"
120 >column</VAR
121 > [, ...] ) ]
122     FROM { '<VAR
123 CLASS="REPLACEABLE"
124 >filename</VAR
125 >' | STDIN }
126     [ [ WITH ] 
127           [ BINARY ] 
128           [ OIDS ]
129           [ DELIMITER [ AS ] '<VAR
130 CLASS="REPLACEABLE"
131 >delimiter</VAR
132 >' ]
133           [ NULL [ AS ] '<VAR
134 CLASS="REPLACEABLE"
135 >null string</VAR
136 >' ] ]
137
138 COPY <VAR
139 CLASS="REPLACEABLE"
140 >tablename</VAR
141 > [ ( <VAR
142 CLASS="REPLACEABLE"
143 >column</VAR
144 > [, ...] ) ]
145     TO { '<VAR
146 CLASS="REPLACEABLE"
147 >filename</VAR
148 >' | STDOUT }
149     [ [ WITH ] 
150           [ BINARY ]
151           [ OIDS ]
152           [ DELIMITER [ AS ] '<VAR
153 CLASS="REPLACEABLE"
154 >delimiter</VAR
155 >' ]
156           [ NULL [ AS ] '<VAR
157 CLASS="REPLACEABLE"
158 >null string</VAR
159 >' ] ]</PRE
160 ></DIV
161 ><DIV
162 CLASS="REFSECT1"
163 ><A
164 NAME="AEN36121"
165 ></A
166 ><H2
167 >Description</H2
168 ><P
169 >   <TT
170 CLASS="COMMAND"
171 >COPY</TT
172 > moves data between
173    <SPAN
174 CLASS="PRODUCTNAME"
175 >PostgreSQL</SPAN
176 > tables and standard file-system
177    files. <TT
178 CLASS="COMMAND"
179 >COPY TO</TT
180 > copies the contents of a table
181    <SPAN
182 CLASS="emphasis"
183 ><I
184 CLASS="EMPHASIS"
185 >to</I
186 ></SPAN
187 > a file, while <TT
188 CLASS="COMMAND"
189 >COPY FROM</TT
190 > copies
191    data <SPAN
192 CLASS="emphasis"
193 ><I
194 CLASS="EMPHASIS"
195 >from</I
196 ></SPAN
197 > a file to a table (appending the data to
198    whatever is in the table already).
199   </P
200 ><P
201 >   If a list of columns is specified, <TT
202 CLASS="COMMAND"
203 >COPY</TT
204 > will
205    only copy the data in the specified columns to or from the file.
206    If there are any columns in the table that are not in the column list,
207    <TT
208 CLASS="COMMAND"
209 >COPY FROM</TT
210 > will insert the default values for
211    those columns.
212   </P
213 ><P
214 >   <TT
215 CLASS="COMMAND"
216 >COPY</TT
217 > with a file name instructs the
218    <SPAN
219 CLASS="PRODUCTNAME"
220 >PostgreSQL</SPAN
221 > server to directly read from
222    or write to a file. The file must be accessible to the server and
223    the name must be specified from the viewpoint of the server. When
224    <TT
225 CLASS="LITERAL"
226 >STDIN</TT
227 > or <TT
228 CLASS="LITERAL"
229 >STDOUT</TT
230 > is
231    specified, data is transmitted via the connection between the
232    client and the server.
233   </P
234 ></DIV
235 ><DIV
236 CLASS="REFSECT1"
237 ><A
238 NAME="AEN36138"
239 ></A
240 ><H2
241 >Parameters</H2
242 ><P
243 ></P
244 ><DIV
245 CLASS="VARIABLELIST"
246 ><DL
247 ><DT
248 ><VAR
249 CLASS="REPLACEABLE"
250 >tablename</VAR
251 ></DT
252 ><DD
253 ><P
254 >      The name (optionally schema-qualified) of an existing table.
255      </P
256 ></DD
257 ><DT
258 ><VAR
259 CLASS="REPLACEABLE"
260 >column</VAR
261 ></DT
262 ><DD
263 ><P
264 >      An optional list of columns to be copied.  If no column list is
265       specified, all columns will be used.
266      </P
267 ></DD
268 ><DT
269 ><VAR
270 CLASS="REPLACEABLE"
271 >filename</VAR
272 ></DT
273 ><DD
274 ><P
275 >      The absolute path name of the input or output file.
276      </P
277 ></DD
278 ><DT
279 ><TT
280 CLASS="LITERAL"
281 >STDIN</TT
282 ></DT
283 ><DD
284 ><P
285 >      Specifies that input comes from the client application.
286      </P
287 ></DD
288 ><DT
289 ><TT
290 CLASS="LITERAL"
291 >STDOUT</TT
292 ></DT
293 ><DD
294 ><P
295 >      Specifies that output goes to the client application.
296      </P
297 ></DD
298 ><DT
299 ><TT
300 CLASS="LITERAL"
301 >BINARY</TT
302 ></DT
303 ><DD
304 ><P
305 >      Causes all data to be stored or read in binary format rather
306       than as text. You cannot specify the <VAR
307 CLASS="OPTION"
308 >DELIMITER</VAR
309 >
310       or <VAR
311 CLASS="OPTION"
312 >NULL</VAR
313 > options in binary mode.
314      </P
315 ></DD
316 ><DT
317 ><TT
318 CLASS="LITERAL"
319 >OIDS</TT
320 ></DT
321 ><DD
322 ><P
323 >      Specifies copying the OID for each row.  (An error is raised if
324       <TT
325 CLASS="LITERAL"
326 >OIDS</TT
327 > is specified for a table that does not
328       have OIDs.)
329      </P
330 ></DD
331 ><DT
332 ><VAR
333 CLASS="REPLACEABLE"
334 >delimiter</VAR
335 ></DT
336 ><DD
337 ><P
338 >      The single character that separates columns within each row
339       (line) of the file.  The default is a tab character.
340      </P
341 ></DD
342 ><DT
343 ><VAR
344 CLASS="REPLACEABLE"
345 >null string</VAR
346 ></DT
347 ><DD
348 ><P
349 >      The string that represents a null value. The default is
350       <TT
351 CLASS="LITERAL"
352 >\N</TT
353 > (backslash-N). You might prefer an empty
354       string, for example.
355      </P
356 ><DIV
357 CLASS="NOTE"
358 ><BLOCKQUOTE
359 CLASS="NOTE"
360 ><P
361 ><B
362 >Note: </B
363 >       On a <TT
364 CLASS="COMMAND"
365 >COPY FROM</TT
366 >, any data item that matches
367        this string will be stored as a null value, so you should make
368        sure that you use the same string as you used with
369        <TT
370 CLASS="COMMAND"
371 >COPY TO</TT
372 >.
373       </P
374 ></BLOCKQUOTE
375 ></DIV
376 ></DD
377 ></DL
378 ></DIV
379 ></DIV
380 ><DIV
381 CLASS="REFSECT1"
382 ><A
383 NAME="AEN36194"
384 ></A
385 ><H2
386 >Notes</H2
387 ><P
388 >    <TT
389 CLASS="COMMAND"
390 >COPY</TT
391 > can only be used with plain tables, not
392     with views.
393    </P
394 ><P
395 >    The <TT
396 CLASS="LITERAL"
397 >BINARY</TT
398 > key word causes all data to be
399     stored/read as binary format rather than as text.  It is
400     somewhat faster than the normal text mode, but a binary-format
401     file is less portable across machine architectures and
402     <SPAN
403 CLASS="PRODUCTNAME"
404 >PostgreSQL</SPAN
405 > versions.
406    </P
407 ><P
408 >    You must have select privilege on the table
409     whose values are read by <TT
410 CLASS="COMMAND"
411 >COPY TO</TT
412 >, and
413     insert privilege on the table into which values
414     are inserted by <TT
415 CLASS="COMMAND"
416 >COPY FROM</TT
417 >.
418    </P
419 ><P
420 >    Files named in a <TT
421 CLASS="COMMAND"
422 >COPY</TT
423 > command are read or written
424     directly by the server, not by the client application. Therefore,
425     they must reside on or be accessible to the database server machine,
426     not the client. They must be accessible to and readable or writable
427     by the <SPAN
428 CLASS="PRODUCTNAME"
429 >PostgreSQL</SPAN
430 > user (the user ID the
431     server runs as), not the client. <TT
432 CLASS="COMMAND"
433 >COPY</TT
434 > naming a
435     file is only allowed to database superusers, since it allows reading
436     or writing any file that the server has privileges to access.
437    </P
438 ><P
439 >    Do not confuse <TT
440 CLASS="COMMAND"
441 >COPY</TT
442 > with the
443     <SPAN
444 CLASS="APPLICATION"
445 >psql</SPAN
446 > instruction
447     <TT
448 CLASS="COMMAND"
449 >\copy</TT
450 >. <TT
451 CLASS="COMMAND"
452 >\copy</TT
453 > invokes
454     <TT
455 CLASS="COMMAND"
456 >COPY FROM STDIN</TT
457 > or <TT
458 CLASS="COMMAND"
459 >COPY TO
460     STDOUT</TT
461 >, and then fetches/stores the data in a file
462     accessible to the <SPAN
463 CLASS="APPLICATION"
464 >psql</SPAN
465 > client. Thus,
466     file accessibility and access rights depend on the client rather
467     than the server when <TT
468 CLASS="COMMAND"
469 >\copy</TT
470 > is used.
471    </P
472 ><P
473 >    It is recommended that the file name used in <TT
474 CLASS="COMMAND"
475 >COPY</TT
476 >
477     always be specified as an absolute path. This is enforced by the
478     server in the case of <TT
479 CLASS="COMMAND"
480 >COPY TO</TT
481 >, but for
482     <TT
483 CLASS="COMMAND"
484 >COPY FROM</TT
485 > you do have the option of reading from
486     a file specified by a relative path. The path will be interpreted
487     relative to the working directory of the server process (somewhere below
488     the data directory), not the client's working directory.
489    </P
490 ><P
491 >    <TT
492 CLASS="COMMAND"
493 >COPY FROM</TT
494 > will invoke any triggers and check
495     constraints on the destination table. However, it will not invoke rules.
496    </P
497 ><P
498 >    <TT
499 CLASS="COMMAND"
500 >COPY</TT
501 > stops operation at the first error. This
502     should not lead to problems in the event of a <TT
503 CLASS="COMMAND"
504 >COPY
505     TO</TT
506 >, but the target table will already have received
507     earlier rows in a <TT
508 CLASS="COMMAND"
509 >COPY FROM</TT
510 >. These rows will not
511     be visible or accessible, but they still occupy disk space. This may
512     amount to a considerable amount of wasted disk space if the failure
513     happened well into a large copy operation. You may wish to invoke
514     <TT
515 CLASS="COMMAND"
516 >VACUUM</TT
517 > to recover the wasted space.
518    </P
519 ></DIV
520 ><DIV
521 CLASS="REFSECT1"
522 ><A
523 NAME="AEN36228"
524 ></A
525 ><H2
526 >File Formats</H2
527 ><DIV
528 CLASS="REFSECT2"
529 ><A
530 NAME="AEN36230"
531 ></A
532 ><H3
533 >Text Format</H3
534 ><P
535 >    When <TT
536 CLASS="COMMAND"
537 >COPY</TT
538 > is used without the <TT
539 CLASS="LITERAL"
540 >BINARY</TT
541 > option,
542     the data read or written is a text file with one line per table row.
543     Columns in a row are separated by the delimiter character.
544     The column values themselves are strings generated by the
545     output function, or acceptable to the input function, of each
546     attribute's data type.  The specified null string is used in
547     place of columns that are null.
548     <TT
549 CLASS="COMMAND"
550 >COPY FROM</TT
551 > will raise an error if any line of the
552     input file contains more or fewer columns than are expected.
553     If <TT
554 CLASS="LITERAL"
555 >OIDS</TT
556 > is specified, the OID is read or written as the first column,
557     preceding the user data columns.
558    </P
559 ><P
560 >    End of data can be represented by a single line containing just
561     backslash-period (<TT
562 CLASS="LITERAL"
563 >\.</TT
564 >).  An end-of-data marker is
565     not necessary when reading from a file, since the end of file
566     serves perfectly well; it is needed only when copying data to or from
567     client applications using pre-3.0 client protocol.
568    </P
569 ><P
570 >    Backslash characters (<TT
571 CLASS="LITERAL"
572 >\</TT
573 >) may be used in the
574     <TT
575 CLASS="COMMAND"
576 >COPY</TT
577 > data to quote data characters that might
578     otherwise be taken as row or column delimiters. In particular, the
579     following characters <SPAN
580 CLASS="emphasis"
581 ><I
582 CLASS="EMPHASIS"
583 >must</I
584 ></SPAN
585 > be preceded by a backslash if
586     they appear as part of a column value: backslash itself,
587     newline, carriage return, and the current delimiter character.
588    </P
589 ><P
590 >    The specified null string is sent by <TT
591 CLASS="COMMAND"
592 >COPY TO</TT
593 > without
594     adding any backslashes; conversely, <TT
595 CLASS="COMMAND"
596 >COPY FROM</TT
597 > matches
598     the input against the null string before removing backslashes.  Therefore,
599     a null string such as <TT
600 CLASS="LITERAL"
601 >\N</TT
602 > cannot be confused with
603     the actual data value <TT
604 CLASS="LITERAL"
605 >\N</TT
606 > (which would be represented
607     as <TT
608 CLASS="LITERAL"
609 >\\N</TT
610 >).
611    </P
612 ><P
613 >    The following special backslash sequences are recognized by
614     <TT
615 CLASS="COMMAND"
616 >COPY FROM</TT
617 >:
618
619    <DIV
620 CLASS="INFORMALTABLE"
621 ><P
622 ></P
623 ><A
624 NAME="AEN36251"
625 ></A
626 ><TABLE
627 BORDER="1"
628 CLASS="CALSTABLE"
629 ><COL><COL><THEAD
630 ><TR
631 ><TH
632 >Sequence</TH
633 ><TH
634 >Represents</TH
635 ></TR
636 ></THEAD
637 ><TBODY
638 ><TR
639 ><TD
640 ><TT
641 CLASS="LITERAL"
642 >\b</TT
643 ></TD
644 ><TD
645 >Backspace (ASCII 8)</TD
646 ></TR
647 ><TR
648 ><TD
649 ><TT
650 CLASS="LITERAL"
651 >\f</TT
652 ></TD
653 ><TD
654 >Form feed (ASCII 12)</TD
655 ></TR
656 ><TR
657 ><TD
658 ><TT
659 CLASS="LITERAL"
660 >\n</TT
661 ></TD
662 ><TD
663 >Newline (ASCII 10)</TD
664 ></TR
665 ><TR
666 ><TD
667 ><TT
668 CLASS="LITERAL"
669 >\r</TT
670 ></TD
671 ><TD
672 >Carriage return (ASCII 13)</TD
673 ></TR
674 ><TR
675 ><TD
676 ><TT
677 CLASS="LITERAL"
678 >\t</TT
679 ></TD
680 ><TD
681 >Tab (ASCII 9)</TD
682 ></TR
683 ><TR
684 ><TD
685 ><TT
686 CLASS="LITERAL"
687 >\v</TT
688 ></TD
689 ><TD
690 >Vertical tab (ASCII 11)</TD
691 ></TR
692 ><TR
693 ><TD
694 ><TT
695 CLASS="LITERAL"
696 >\</TT
697 ><VAR
698 CLASS="REPLACEABLE"
699 >digits</VAR
700 ></TD
701 ><TD
702 >Backslash followed by one to three octal digits specifies
703        the character with that numeric code</TD
704 ></TR
705 ></TBODY
706 ></TABLE
707 ><P
708 ></P
709 ></DIV
710 >
711
712     Presently, <TT
713 CLASS="COMMAND"
714 >COPY TO</TT
715 > will never emit an octal-digits
716     backslash sequence, but it does use the other sequences listed above
717     for those control characters.
718    </P
719 ><P
720 >    Any other backslashed character that is not mentioned in the above table
721     will be taken to represent itself.  However, beware of adding backslashes
722     unnecessarily, since that might accidentally produce a string matching the
723     end-of-data marker (<TT
724 CLASS="LITERAL"
725 >\.</TT
726 >) or the null string (<TT
727 CLASS="LITERAL"
728 >\N</TT
729 > by
730     default).  These strings will be recognized before any other backslash
731     processing is done.
732    </P
733 ><P
734 >    It is strongly recommended that applications generating COPY data convert
735     data newlines and carriage returns to the <TT
736 CLASS="LITERAL"
737 >\n</TT
738 > and
739     <TT
740 CLASS="LITERAL"
741 >\r</TT
742 > sequences respectively.  At present it is
743     possible to represent a data carriage return by a backslash and carriage
744     return, and to represent a data newline by a backslash and newline.  
745     However, these representations might not be accepted in future releases.
746     They are also highly vulnerable to corruption if the COPY file is
747     transferred across different machines (for example, from Unix to Windows
748     or vice versa).
749    </P
750 ><P
751 >    <TT
752 CLASS="COMMAND"
753 >COPY TO</TT
754 > will terminate each row with a Unix-style 
755     newline (<SPAN
756 CLASS="QUOTE"
757 >"<TT
758 CLASS="LITERAL"
759 >\n</TT
760 >"</SPAN
761 >).  Servers running on MS Windows instead
762     output carriage return/newline (<SPAN
763 CLASS="QUOTE"
764 >"<TT
765 CLASS="LITERAL"
766 >\r\n</TT
767 >"</SPAN
768 >), but only for
769     <TT
770 CLASS="COMMAND"
771 >COPY</TT
772 > to a server file; for consistency across platforms,
773     <TT
774 CLASS="COMMAND"
775 >COPY TO STDOUT</TT
776 > always sends <SPAN
777 CLASS="QUOTE"
778 >"<TT
779 CLASS="LITERAL"
780 >\n</TT
781 >"</SPAN
782 >
783     regardless of server platform.
784     <TT
785 CLASS="COMMAND"
786 >COPY FROM</TT
787 > can handle lines ending with newlines,
788     carriage returns, or carriage return/newlines.  To reduce the risk of
789     error due to un-backslashed newlines or carriage returns that were
790     meant as data, <TT
791 CLASS="COMMAND"
792 >COPY FROM</TT
793 > will complain if the line
794     endings in the input are not all alike.
795    </P
796 ></DIV
797 ><DIV
798 CLASS="REFSECT2"
799 ><A
800 NAME="AEN36306"
801 ></A
802 ><H3
803 >Binary Format</H3
804 ><P
805 >    The file format used for <TT
806 CLASS="COMMAND"
807 >COPY BINARY</TT
808 > changed in
809     <SPAN
810 CLASS="PRODUCTNAME"
811 >PostgreSQL</SPAN
812 > 7.4. The new format consists
813     of a file header, zero or more tuples containing the row data, and
814     a file trailer. Headers and data are now in network byte order.
815    </P
816 ><DIV
817 CLASS="REFSECT3"
818 ><A
819 NAME="AEN36311"
820 ></A
821 ><H4
822 >File Header</H4
823 ><P
824 >     The file header consists of 15 bytes of fixed fields, followed
825      by a variable-length header extension area.  The fixed fields are:
826
827     <P
828 ></P
829 ></P><DIV
830 CLASS="VARIABLELIST"
831 ><DL
832 ><DT
833 >Signature</DT
834 ><DD
835 ><P
836 >11-byte sequence <TT
837 CLASS="LITERAL"
838 >PGCOPY\n\377\r\n\0</TT
839 > --- note that the zero byte
840 is a required part of the signature.  (The signature is designed to allow
841 easy identification of files that have been munged by a non-8-bit-clean
842 transfer.  This signature will be changed by end-of-line-translation
843 filters, dropped zero bytes, dropped high bits, or parity changes.)
844        </P
845 ></DD
846 ><DT
847 >Flags field</DT
848 ><DD
849 ><P
850 >32-bit integer bit mask to denote important aspects of the file format. Bits
851 are numbered from 0 (<ACRONYM
852 CLASS="ACRONYM"
853 >LSB</ACRONYM
854 >) to 31 (<ACRONYM
855 CLASS="ACRONYM"
856 >MSB</ACRONYM
857 >).  Note that
858 this field is stored in network byte order (most significant byte first),
859 as are all the integer fields used in the file format.  Bits
860 16-31 are reserved to denote critical file format issues; a reader
861 should abort if it finds an unexpected bit set in this range. Bits 0-15
862 are reserved to signal backwards-compatible format issues; a reader
863 should simply ignore any unexpected bits set in this range. Currently
864 only one flag bit is defined, and the rest must be zero:
865         <P
866 ></P
867 ></P><DIV
868 CLASS="VARIABLELIST"
869 ><DL
870 ><DT
871 >Bit 16</DT
872 ><DD
873 ><P
874 >            if 1, OIDs are included in the data; if 0, not
875            </P
876 ></DD
877 ></DL
878 ></DIV
879 ><P>
880        </P
881 ></DD
882 ><DT
883 >Header extension area length</DT
884 ><DD
885 ><P
886 >32-bit integer, length in bytes of remainder of header, not including self.
887 Currently, this is zero, and the first tuple follows
888 immediately.  Future changes to the format might allow additional data
889 to be present in the header.  A reader should silently skip over any header
890 extension data it does not know what to do with.
891        </P
892 ></DD
893 ></DL
894 ></DIV
895 ><P>
896     </P
897 ><P
898 >The header extension area is envisioned to contain a sequence of
899 self-identifying chunks.  The flags field is not intended to tell readers
900 what is in the extension area.  Specific design of header extension contents
901 is left for a later release.
902     </P
903 ><P
904 >     This design allows for both backwards-compatible header additions (add
905      header extension chunks, or set low-order flag bits) and
906      non-backwards-compatible changes (set high-order flag bits to signal such
907      changes, and add supporting data to the extension area if needed).
908     </P
909 ></DIV
910 ><DIV
911 CLASS="REFSECT3"
912 ><A
913 NAME="AEN36337"
914 ></A
915 ><H4
916 >Tuples</H4
917 ><P
918 >Each tuple begins with a 16-bit integer count of the number of fields in the
919 tuple.  (Presently, all tuples in a table will have the same count, but that
920 might not always be true.)  Then, repeated for each field in the tuple, there
921 is a 32-bit length word followed by that many bytes of field data.  (The
922 length word does not include itself, and can be zero.)  As a special case,
923 -1 indicates a NULL field value.  No value bytes follow in the NULL case.
924     </P
925 ><P
926 >There is no alignment padding or any other extra data between fields.
927     </P
928 ><P
929 >Presently, all data values in a <TT
930 CLASS="COMMAND"
931 >COPY BINARY</TT
932 > file are
933 assumed to be in binary format (format code one).  It is anticipated that a
934 future extension may add a header field that allows per-column format codes
935 to be specified.
936     </P
937 ><P
938 >To determine the appropriate binary format for the actual tuple data you
939 should consult the <SPAN
940 CLASS="PRODUCTNAME"
941 >PostgreSQL</SPAN
942 > source, in
943 particular the <CODE
944 CLASS="FUNCTION"
945 >*send</CODE
946 > and <CODE
947 CLASS="FUNCTION"
948 >*recv</CODE
949 > functions for
950 each column's data type (typically these functions are found in the
951 <TT
952 CLASS="FILENAME"
953 >src/backend/utils/adt/</TT
954 > directory of the source
955 distribution).
956     </P
957 ><P
958 >If OIDs are included in the file, the OID field immediately follows the
959 field-count word.  It is a normal field except that it's not included
960 in the field-count.  In particular it has a length word --- this will allow
961 handling of 4-byte vs. 8-byte OIDs without too much pain, and will allow
962 OIDs to be shown as null if that ever proves desirable.
963     </P
964 ></DIV
965 ><DIV
966 CLASS="REFSECT3"
967 ><A
968 NAME="AEN36349"
969 ></A
970 ><H4
971 >File Trailer</H4
972 ><P
973 >     The file trailer consists of a 16-bit integer word containing -1.  This
974      is easily distinguished from a tuple's field-count word.
975     </P
976 ><P
977 >     A reader should report an error if a field-count word is neither -1
978      nor the expected number of columns.  This provides an extra
979      check against somehow getting out of sync with the data.
980     </P
981 ></DIV
982 ></DIV
983 ></DIV
984 ><DIV
985 CLASS="REFSECT1"
986 ><A
987 NAME="AEN36353"
988 ></A
989 ><H2
990 >Examples</H2
991 ><P
992 >   The following example copies a table to the client
993    using the vertical bar (<TT
994 CLASS="LITERAL"
995 >|</TT
996 >) as the field delimiter:
997 </P><PRE
998 CLASS="PROGRAMLISTING"
999 >COPY country TO STDOUT WITH DELIMITER '|';</PRE
1000 ><P>
1001   </P
1002 ><P
1003 >   To copy data from a file into the <TT
1004 CLASS="LITERAL"
1005 >country</TT
1006 > table:
1007 </P><PRE
1008 CLASS="PROGRAMLISTING"
1009 >COPY country FROM '/usr1/proj/bray/sql/country_data';</PRE
1010 ><P>
1011   </P
1012 ><P
1013 >   Here is a sample of data suitable for copying into a table from
1014    <TT
1015 CLASS="LITERAL"
1016 >STDIN</TT
1017 >:
1018 </P><PRE
1019 CLASS="PROGRAMLISTING"
1020 >AF      AFGHANISTAN
1021 AL      ALBANIA
1022 DZ      ALGERIA
1023 ZM      ZAMBIA
1024 ZW      ZIMBABWE</PRE
1025 ><P>
1026    Note that the white space on each line is actually a tab character.
1027   </P
1028 ><P
1029 >   The following is the same data, output in binary format.
1030    The data is shown after filtering through the
1031    Unix utility <TT
1032 CLASS="COMMAND"
1033 >od -c</TT
1034 >. The table has three columns;
1035    the first has type <TT
1036 CLASS="TYPE"
1037 >char(2)</TT
1038 >, the second has type <TT
1039 CLASS="TYPE"
1040 >text</TT
1041 >,
1042    and the third has type <TT
1043 CLASS="TYPE"
1044 >integer</TT
1045 >. All the rows have a null value
1046    in the third column.
1047 </P><PRE
1048 CLASS="PROGRAMLISTING"
1049 >0000000   P   G   C   O   P   Y  \n 377  \r  \n  \0  \0  \0  \0  \0  \0
1050 0000020  \0  \0  \0  \0 003  \0  \0  \0 002   A   F  \0  \0  \0 013   A
1051 0000040   F   G   H   A   N   I   S   T   A   N 377 377 377 377  \0 003
1052 0000060  \0  \0  \0 002   A   L  \0  \0  \0 007   A   L   B   A   N   I
1053 0000100   A 377 377 377 377  \0 003  \0  \0  \0 002   D   Z  \0  \0  \0
1054 0000120 007   A   L   G   E   R   I   A 377 377 377 377  \0 003  \0  \0
1055 0000140  \0 002   Z   M  \0  \0  \0 006   Z   A   M   B   I   A 377 377
1056 0000160 377 377  \0 003  \0  \0  \0 002   Z   W  \0  \0  \0  \b   Z   I
1057 0000200   M   B   A   B   W   E 377 377 377 377 377 377</PRE
1058 ><P>
1059   </P
1060 ></DIV
1061 ><DIV
1062 CLASS="REFSECT1"
1063 ><A
1064 NAME="AEN36370"
1065 ></A
1066 ><H2
1067 >Compatibility</H2
1068 ><P
1069 >   There is no <TT
1070 CLASS="COMMAND"
1071 >COPY</TT
1072 > statement in the SQL standard.
1073   </P
1074 ><P
1075 >   The following syntax was used before PostgreSQL version 7.3 and is
1076    still supported:
1077
1078 </P><PRE
1079 CLASS="SYNOPSIS"
1080 >COPY [ BINARY ] <VAR
1081 CLASS="REPLACEABLE"
1082 >tablename</VAR
1083 > [ WITH OIDS ]
1084     FROM { '<VAR
1085 CLASS="REPLACEABLE"
1086 >filename</VAR
1087 >' | STDIN }
1088     [ [USING] DELIMITERS '<VAR
1089 CLASS="REPLACEABLE"
1090 >delimiter</VAR
1091 >' ]
1092     [ WITH NULL AS '<VAR
1093 CLASS="REPLACEABLE"
1094 >null string</VAR
1095 >' ]
1096
1097 COPY [ BINARY ] <VAR
1098 CLASS="REPLACEABLE"
1099 >tablename</VAR
1100 > [ WITH OIDS ]
1101     TO { '<VAR
1102 CLASS="REPLACEABLE"
1103 >filename</VAR
1104 >' | STDOUT }
1105     [ [USING] DELIMITERS '<VAR
1106 CLASS="REPLACEABLE"
1107 >delimiter</VAR
1108 >' ]
1109     [ WITH NULL AS '<VAR
1110 CLASS="REPLACEABLE"
1111 >null string</VAR
1112 >' ]</PRE
1113 ><P>
1114   </P
1115 ></DIV
1116 ><DIV
1117 CLASS="NAVFOOTER"
1118 ><HR
1119 ALIGN="LEFT"
1120 WIDTH="100%"><TABLE
1121 SUMMARY="Footer navigation table"
1122 WIDTH="100%"
1123 BORDER="0"
1124 CELLPADDING="0"
1125 CELLSPACING="0"
1126 ><TR
1127 ><TD
1128 WIDTH="33%"
1129 ALIGN="left"
1130 VALIGN="top"
1131 ><A
1132 HREF="sql-commit.html"
1133 ACCESSKEY="P"
1134 >Prev</A
1135 ></TD
1136 ><TD
1137 WIDTH="34%"
1138 ALIGN="center"
1139 VALIGN="top"
1140 ><A
1141 HREF="index.html"
1142 ACCESSKEY="H"
1143 >Home</A
1144 ></TD
1145 ><TD
1146 WIDTH="33%"
1147 ALIGN="right"
1148 VALIGN="top"
1149 ><A
1150 HREF="sql-createaggregate.html"
1151 ACCESSKEY="N"
1152 >Next</A
1153 ></TD
1154 ></TR
1155 ><TR
1156 ><TD
1157 WIDTH="33%"
1158 ALIGN="left"
1159 VALIGN="top"
1160 >COMMIT</TD
1161 ><TD
1162 WIDTH="34%"
1163 ALIGN="center"
1164 VALIGN="top"
1165 ><A
1166 HREF="sql-commands.html"
1167 ACCESSKEY="U"
1168 >Up</A
1169 ></TD
1170 ><TD
1171 WIDTH="33%"
1172 ALIGN="right"
1173 VALIGN="top"
1174 >CREATE AGGREGATE</TD
1175 ></TR
1176 ></TABLE
1177 ></DIV
1178 ></BODY
1179 ></HTML
1180 >