Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 923413a

Browse files
committed
Define a new, more extensible syntax for COPY options.
This is intentionally similar to the recently revised syntax for EXPLAIN options, ie, (name value, ...). The old syntax is still supported for backwards compatibility, but we intend that any options added in future will be provided only in the new syntax. Robert Haas, Emmanuel Cecchet
1 parent 0f427df commit 923413a

File tree

6 files changed

+304
-132
lines changed

6 files changed

+304
-132
lines changed

doc/src/sgml/ref/copy.sgml

+139-84
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.91 2009/09/19 10:23:26 petere Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.92 2009/09/21 20:10:21 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -24,27 +24,23 @@ PostgreSQL documentation
2424
<synopsis>
2525
COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ]
2626
FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
27-
[ [ WITH ]
28-
[ BINARY ]
29-
[ OIDS ]
30-
[ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
31-
[ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
32-
[ CSV [ HEADER ]
33-
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
34-
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
35-
[ FORCE NOT NULL <replaceable class="parameter">column</replaceable> [, ...] ]
27+
[ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
3628

3729
COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
3830
TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
39-
[ [ WITH ]
40-
[ BINARY ]
41-
[ OIDS ]
42-
[ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
43-
[ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
44-
[ CSV [ HEADER ]
45-
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
46-
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
47-
[ FORCE QUOTE { <replaceable class="parameter">column</replaceable> [, ...] | * } ]
31+
[ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
32+
33+
<phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
34+
35+
FORMAT <replaceable class="parameter">format_name</replaceable>
36+
OIDS [ <replaceable class="parameter">boolean</replaceable> ]
37+
DELIMITER '<replaceable class="parameter">delimiter_character</replaceable>'
38+
NULL '<replaceable class="parameter">null_string</replaceable>'
39+
HEADER [ <replaceable class="parameter">boolean</replaceable> ]
40+
QUOTE '<replaceable class="parameter">quote_character</replaceable>'
41+
ESCAPE '<replaceable class="parameter">escape_character</replaceable>'
42+
FORCE_QUOTE { ( <replaceable class="parameter">column</replaceable> [, ...] ) | * }
43+
FORCE_NOT_NULL ( <replaceable class="parameter">column</replaceable> [, ...] )
4844
</synopsis>
4945
</refsynopsisdiv>
5046

@@ -120,8 +116,8 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
120116
<listitem>
121117
<para>
122118
The absolute path name of the input or output file. Windows users
123-
might need to use an <literal>E''</> string and double backslashes
124-
used as path separators.
119+
might need to use an <literal>E''</> string and double any backslashes
120+
used in the path name.
125121
</para>
126122
</listitem>
127123
</varlistentry>
@@ -145,12 +141,28 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
145141
</varlistentry>
146142

147143
<varlistentry>
148-
<term><literal>BINARY</literal></term>
144+
<term><replaceable class="parameter">boolean</replaceable></term>
149145
<listitem>
150146
<para>
151-
Causes all data to be stored or read in binary format rather
152-
than as text. You cannot specify the <option>DELIMITER</option>,
153-
<option>NULL</option>, or <option>CSV</> options in binary mode.
147+
Specifies whether the selected option should be turned on or off.
148+
You can write <literal>TRUE</literal>, <literal>ON</>, or
149+
<literal>1</literal> to enable the option, and <literal>FALSE</literal>,
150+
<literal>OFF</>, or <literal>0</literal> to disable it. The
151+
<replaceable class="parameter">boolean</replaceable> value can also
152+
be omitted, in which case <literal>TRUE</literal> is assumed.
153+
</para>
154+
</listitem>
155+
</varlistentry>
156+
157+
<varlistentry>
158+
<term><literal>FORMAT</literal></term>
159+
<listitem>
160+
<para>
161+
Selects the data format to be read or written:
162+
<literal>text</>,
163+
<literal>csv</> (Comma Separated Values),
164+
or <literal>binary</>.
165+
The default is <literal>text</>.
154166
</para>
155167
</listitem>
156168
</varlistentry>
@@ -168,25 +180,28 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
168180
</varlistentry>
169181

170182
<varlistentry>
171-
<term><replaceable class="parameter">delimiter</replaceable></term>
183+
<term><literal>DELIMITER</literal></term>
172184
<listitem>
173185
<para>
174-
The single ASCII character that separates columns within each row
175-
(line) of the file. The default is a tab character in text mode,
176-
a comma in <literal>CSV</> mode.
186+
Specifies the character that separates columns within each row
187+
(line) of the file. The default is a tab character in text format,
188+
a comma in <literal>CSV</> format.
189+
This must be a single one-byte character.
190+
This option is not allowed when using <literal>binary</> format.
177191
</para>
178192
</listitem>
179193
</varlistentry>
180194

181195
<varlistentry>
182-
<term><replaceable class="parameter">null string</replaceable></term>
196+
<term><literal>NULL</literal></term>
183197
<listitem>
184198
<para>
185-
The string that represents a null value. The default is
186-
<literal>\N</literal> (backslash-N) in text mode, and an unquoted empty
187-
string in <literal>CSV</> mode. You might prefer an
188-
empty string even in text mode for cases where you don't want to
199+
Specifies the string that represents a null value. The default is
200+
<literal>\N</literal> (backslash-N) in text format, and an unquoted empty
201+
string in <literal>CSV</> format. You might prefer an
202+
empty string even in text format for cases where you don't want to
189203
distinguish nulls from empty strings.
204+
This option is not allowed when using <literal>binary</> format.
190205
</para>
191206

192207
<note>
@@ -201,68 +216,68 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
201216
</listitem>
202217
</varlistentry>
203218

204-
<varlistentry>
205-
<term><literal>CSV</literal></term>
206-
<listitem>
207-
<para>
208-
Selects Comma Separated Value (<literal>CSV</>) mode.
209-
</para>
210-
</listitem>
211-
</varlistentry>
212-
213219
<varlistentry>
214220
<term><literal>HEADER</literal></term>
215221
<listitem>
216222
<para>
217223
Specifies that the file contains a header line with the names of each
218224
column in the file. On output, the first line contains the column
219225
names from the table, and on input, the first line is ignored.
226+
This option is allowed only when using <literal>CSV</> format.
220227
</para>
221228
</listitem>
222229
</varlistentry>
223230

224231
<varlistentry>
225-
<term><replaceable class="parameter">quote</replaceable></term>
232+
<term><literal>QUOTE</literal></term>
226233
<listitem>
227234
<para>
228-
Specifies the ASCII quotation character in <literal>CSV</> mode.
235+
Specifies the quoting character to be used when a data value is quoted.
229236
The default is double-quote.
237+
This must be a single one-byte character.
238+
This option is allowed only when using <literal>CSV</> format.
230239
</para>
231240
</listitem>
232241
</varlistentry>
233242

234243
<varlistentry>
235-
<term><replaceable class="parameter">escape</replaceable></term>
244+
<term><literal>ESCAPE</literal></term>
236245
<listitem>
237246
<para>
238-
Specifies the ASCII character that should appear before a
239-
<literal>QUOTE</> data character value in <literal>CSV</> mode.
240-
The default is the <literal>QUOTE</> value (usually double-quote).
247+
Specifies the character that should appear before a
248+
data character that matches the <literal>QUOTE</> value.
249+
The default is the same as the <literal>QUOTE</> value (so that
250+
the quoting character is doubled if it appears in the data).
251+
This must be a single one-byte character.
252+
This option is allowed only when using <literal>CSV</> format.
241253
</para>
242254
</listitem>
243255
</varlistentry>
244256

245257
<varlistentry>
246-
<term><literal>FORCE QUOTE</></term>
258+
<term><literal>FORCE_QUOTE</></term>
247259
<listitem>
248260
<para>
249-
In <literal>CSV</> <command>COPY TO</> mode, forces quoting to be
261+
Forces quoting to be
250262
used for all non-<literal>NULL</> values in each specified column.
251263
<literal>NULL</> output is never quoted. If <literal>*</> is specified,
252264
non-<literal>NULL</> values will be quoted in all columns.
265+
This option is allowed only in <command>COPY TO</>, and only when
266+
using <literal>CSV</> format.
253267
</para>
254268
</listitem>
255269
</varlistentry>
256270

257271
<varlistentry>
258-
<term><literal>FORCE NOT NULL</></term>
272+
<term><literal>FORCE_NOT_NULL</></term>
259273
<listitem>
260274
<para>
261-
In <literal>CSV</> <command>COPY FROM</> mode, process each
262-
specified column as though it were quoted and hence not a
263-
<literal>NULL</> value. For the default null string in
264-
<literal>CSV</> mode (<literal>''</>), this causes missing
265-
values to be input as zero-length strings.
275+
Do not match the specified columns' values against the null string.
276+
In the default case where the null string is empty, this means that
277+
empty values will be read as zero-length strings rather than nulls,
278+
even when they are not quoted.
279+
This option is allowed only in <command>COPY FROM</>, and only when
280+
using <literal>CSV</> format.
266281
</para>
267282
</listitem>
268283
</varlistentry>
@@ -293,18 +308,6 @@ COPY <replaceable class="parameter">count</replaceable>
293308
<replaceable class="parameter">viewname</replaceable>) TO ...</literal>.
294309
</para>
295310

296-
<para>
297-
The <literal>BINARY</literal> key word causes all data to be
298-
stored/read as binary format rather than as text. It is
299-
somewhat faster than the normal text mode, but a binary-format
300-
file is less portable across machine architectures and
301-
<productname>PostgreSQL</productname> versions.
302-
Also, the binary format is very data type specific; for example
303-
it will not work to output binary data from a <type>smallint</> column
304-
and read it into an <type>integer</> column, even though that would work
305-
fine in text format.
306-
</para>
307-
308311
<para>
309312
You must have select privilege on the table
310313
whose values are read by <command>COPY TO</command>, and
@@ -390,8 +393,7 @@ COPY <replaceable class="parameter">count</replaceable>
390393
<title>Text Format</title>
391394

392395
<para>
393-
When <command>COPY</command> is used without the <literal>BINARY</literal>
394-
or <literal>CSV</> options,
396+
When the <literal>text</> format is used,
395397
the data read or written is a text file with one line per table row.
396398
Columns in a row are separated by the delimiter character.
397399
The column values themselves are strings generated by the
@@ -527,10 +529,10 @@ COPY <replaceable class="parameter">count</replaceable>
527529
<title>CSV Format</title>
528530

529531
<para>
530-
This format is used for importing and exporting the Comma
532+
This format option is used for importing and exporting the Comma
531533
Separated Value (<literal>CSV</>) file format used by many other
532-
programs, such as spreadsheets. Instead of the escaping used by
533-
<productname>PostgreSQL</productname>'s standard text mode, it
534+
programs, such as spreadsheets. Instead of the escaping rules used by
535+
<productname>PostgreSQL</productname>'s standard text format, it
534536
produces and recognizes the common CSV escaping mechanism.
535537
</para>
536538

@@ -542,7 +544,7 @@ COPY <replaceable class="parameter">count</replaceable>
542544
suffixed by the <literal>QUOTE</> character, and any occurrence
543545
within the value of a <literal>QUOTE</> character or the
544546
<literal>ESCAPE</> character is preceded by the escape character.
545-
You can also use <literal>FORCE QUOTE</> to force quotes when outputting
547+
You can also use <literal>FORCE_QUOTE</> to force quotes when outputting
546548
non-<literal>NULL</> values in specific columns.
547549
</para>
548550

@@ -556,7 +558,7 @@ COPY <replaceable class="parameter">count</replaceable>
556558
default settings, a <literal>NULL</> is written as an unquoted empty
557559
string, while an empty string data value is written with double quotes
558560
(<literal>""</>). Reading values follows similar rules. You can
559-
use <literal>FORCE NOT NULL</> to prevent <literal>NULL</> input
561+
use <literal>FORCE_NOT_NULL</> to prevent <literal>NULL</> input
560562
comparisons for specific columns.
561563
</para>
562564

@@ -574,7 +576,7 @@ COPY <replaceable class="parameter">count</replaceable>
574576

575577
<note>
576578
<para>
577-
In <literal>CSV</> mode, all characters are significant. A quoted value
579+
In <literal>CSV</> format, all characters are significant. A quoted value
578580
surrounded by white space, or any characters other than
579581
<literal>DELIMITER</>, will include those characters. This can cause
580582
errors if you import data from a system that pads <literal>CSV</>
@@ -587,9 +589,9 @@ COPY <replaceable class="parameter">count</replaceable>
587589

588590
<note>
589591
<para>
590-
CSV mode will both recognize and produce CSV files with quoted
592+
CSV format will both recognize and produce CSV files with quoted
591593
values containing embedded carriage returns and line feeds. Thus
592-
the files are not strictly one line per table row like text-mode
594+
the files are not strictly one line per table row like text-format
593595
files.
594596
</para>
595597
</note>
@@ -610,12 +612,30 @@ COPY <replaceable class="parameter">count</replaceable>
610612
<title>Binary Format</title>
611613

612614
<para>
613-
The file format used for <command>COPY BINARY</command> changed in
614-
<productname>PostgreSQL</productname> 7.4. The new format consists
615+
The <literal>binary</literal> format option causes all data to be
616+
stored/read as binary format rather than as text. It is
617+
somewhat faster than the text and <literal>CSV</> formats,
618+
but a binary-format file is less portable across machine architectures and
619+
<productname>PostgreSQL</productname> versions.
620+
Also, the binary format is very data type specific; for example
621+
it will not work to output binary data from a <type>smallint</> column
622+
and read it into an <type>integer</> column, even though that would work
623+
fine in text format.
624+
</para>
625+
626+
<para>
627+
The <literal>binary</> file format consists
615628
of a file header, zero or more tuples containing the row data, and
616-
a file trailer. Headers and data are now in network byte order.
629+
a file trailer. Headers and data are in network byte order.
617630
</para>
618631

632+
<note>
633+
<para>
634+
<productname>PostgreSQL</productname> releases before 7.4 used a
635+
different binary file format.
636+
</para>
637+
</note>
638+
619639
<refsect3>
620640
<title>File Header</title>
621641

@@ -710,7 +730,7 @@ There is no alignment padding or any other extra data between fields.
710730
</para>
711731

712732
<para>
713-
Presently, all data values in a <command>COPY BINARY</command> file are
733+
Presently, all data values in a binary-format file are
714734
assumed to be in binary format (format code one). It is anticipated that a
715735
future extension might add a header field that allows per-column format codes
716736
to be specified.
@@ -758,7 +778,7 @@ OIDs to be shown as null if that ever proves desirable.
758778
The following example copies a table to the client
759779
using the vertical bar (<literal>|</literal>) as the field delimiter:
760780
<programlisting>
761-
COPY country TO STDOUT WITH DELIMITER '|';
781+
COPY country TO STDOUT (DELIMITER '|');
762782
</programlisting>
763783
</para>
764784

@@ -817,6 +837,41 @@ ZW ZIMBABWE
817837
There is no <command>COPY</command> statement in the SQL standard.
818838
</para>
819839

840+
<para>
841+
The following syntax was used before <productname>PostgreSQL</>
842+
version 8.5 and is still supported:
843+
844+
<synopsis>
845+
COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ]
846+
FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
847+
[ [ WITH ]
848+
[ BINARY ]
849+
[ OIDS ]
850+
[ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
851+
[ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
852+
[ CSV [ HEADER ]
853+
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
854+
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
855+
[ FORCE NOT NULL <replaceable class="parameter">column</replaceable> [, ...] ]
856+
857+
COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
858+
TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
859+
[ [ WITH ]
860+
[ BINARY ]
861+
[ OIDS ]
862+
[ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
863+
[ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
864+
[ CSV [ HEADER ]
865+
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
866+
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
867+
[ FORCE QUOTE { <replaceable class="parameter">column</replaceable> [, ...] | * } ]
868+
</synopsis>
869+
870+
Note that in this syntax, <literal>BINARY</> and <literal>CSV</> are
871+
treated as independent keywords, not as arguments of a <literal>FORMAT</>
872+
option.
873+
</para>
874+
820875
<para>
821876
The following syntax was used before <productname>PostgreSQL</>
822877
version 7.3 and is still supported:

0 commit comments

Comments
 (0)