COPY 中文man頁面
NAME
COPY - 在表和文件之間拷貝數(shù)據(jù)
SYNOPSIS
COPY tablename [ ( column [, ...] ) ] FROM { 'filename' | STDIN } [ [ WITH ] [ BINARY ] [ OIDS ] [ DELIMITER [ AS ] 'delimiter' ] [ NULL [ AS ] 'null string' ] ] COPY tablename [ ( column [, ...] ) ] TO { 'filename' | STDOUT } [ [ WITH ] [ BINARY ] [ OIDS ] [ DELIMITER [ AS ] 'delimiter' ] [ NULL [ AS ] 'null string' ] ]
DESCRIPTION 描述
COPY 在 PostgreSQL表和標(biāo)準(zhǔn)文件系統(tǒng)文件之間交換數(shù)據(jù)。 COPY TO 把一個(gè)表的所有內(nèi)容都拷貝到一個(gè)文件, 而 COPY FROM 從一個(gè)文件里拷貝數(shù)據(jù)到一個(gè)表里(把數(shù)據(jù)附加到表中已經(jīng)存在的內(nèi)容里)。
如果聲明了一個(gè)字段列表,COPY 將只在文件和表之間拷貝聲明的字段的數(shù)據(jù)。 如果表中有任何不在字段列表里的字段,那么 COPY FROM 將為那些字段插入缺省值。
帶文件名的 COPY 指示 PostgreSQL 服務(wù)器直接從文件中讀寫數(shù)據(jù)。 如果聲明了文件名,那么該文件必須為服務(wù)器可見,而且文件名必須從服務(wù)器的角度聲明。如果聲明的是 STDIN 或 STDOUT,數(shù)據(jù)通過連接在客戶前端和服務(wù)器之間流動(dòng)。
PARAMETERS 參數(shù)
- tablename
現(xiàn)存表的名字(可以有模式修飾)。- column
可選的待拷貝字段列表。如果沒有聲明字段列表,那么將使用所有字段。- filename
輸入或輸出文件的絕對(duì)路徑名。- STDIN
聲明輸入是來自客戶端應(yīng)用。- STDOUT
聲明輸入前往客戶端應(yīng)用。- BINARY
使用二進(jìn)制格式存儲(chǔ)和讀取,而不是以文本的方式。 在二進(jìn)制模式下,不能聲明 DELIMITERS和NULL。- OIDS
聲明為每行拷貝內(nèi)部對(duì)象標(biāo)識(shí)(OID)。 (如果給那些沒有 OID 的表聲明了 OIDS 選項(xiàng),則拋出一個(gè)錯(cuò)誤。)- delimiter
用于在文件中每行中分隔各個(gè)字段的單個(gè)字符。 缺省是水平制表符。(tab)- null string
一個(gè)代表 NULL 值的字串。缺省是 \N (反斜杠-N)。 當(dāng)然,你可以自己挑一個(gè)空字串。- Note: 注意: 對(duì)于COPY FROM,任何匹配這個(gè)字串的字串將被存儲(chǔ)為 NULL 值, 所以你應(yīng)該確保你用的字串和COPY TO相同。
NOTES 注意
COPY 只能用于表,不能用于視圖。
BINARY 關(guān)鍵字將強(qiáng)制使用二進(jìn)制對(duì)象而不是文本存儲(chǔ)/讀取所有數(shù)據(jù)。 這樣做在一定程度上比傳統(tǒng)的拷貝命令快,但二進(jìn)制拷貝文件在不同機(jī)器體系間的植性不是很好。
你對(duì)任何要COPY TO 出來的數(shù)據(jù)必須有選取數(shù)據(jù)的權(quán)限,對(duì)任何要 COPY FROM 入數(shù)據(jù)的表必須有插入權(quán)限。
COPY 命令里面的文件必須是由服務(wù)器直接讀或?qū)懙奈募皇怯煽蛻舳藨?yīng)用讀寫。 因此,它們必須位于數(shù)據(jù)庫(kù)服務(wù)器上或者可以為數(shù)據(jù)庫(kù)服務(wù)器所訪問,而不是由客戶端做這些事情。 它們必須是PostgreSQL用戶(服務(wù)器運(yùn)行的用戶 ID)可以訪問到并且可讀或者可寫,而不是客戶端。 COPY 到一個(gè)命名文件是只允許數(shù)據(jù)庫(kù)超級(jí)用戶進(jìn)行的,因?yàn)樗试S讀寫任意服務(wù)器有權(quán)限訪問的文件。
不要混淆 COPY 和 psql 指令 \copy。 \copy 調(diào)用 COPY FROM STDIN 或者 COPY TO STDOUT, 然后把數(shù)據(jù)抓取/存儲(chǔ)到一個(gè) psql 客戶端可以訪問的文件中。 因此,使用 \copy 的時(shí)候,文件訪問權(quán)限是由客戶端而不是服務(wù)器端決定的。
我們建議在 COPY 里的文件名字總是使用絕對(duì)路徑。 在 COPY TO 的時(shí)候是由服務(wù)器強(qiáng)制進(jìn)行的, 但是對(duì)于 COPY FROM,你的確有從一個(gè)聲明為相對(duì)路徑的文件里讀取的選擇。 該路徑將解釋為相對(duì)于服務(wù)器的工作目錄(在數(shù)據(jù)目錄里的什么地方),而不是客戶端的工作目錄。
COPY FROM 會(huì)激活所有觸發(fā)器和檢查約束。不過,不會(huì)激活規(guī)則。
COPY 在第一個(gè)錯(cuò)誤處停下來。這些在 COPY TO中不應(yīng)該導(dǎo)致問題, 但在 COPY FROM 時(shí)目的表會(huì)已經(jīng)接收到早先的行, 這些行將不可見或不可訪問,但是仍然會(huì)占據(jù)磁盤空間。 如果你碰巧是拷貝很大一塊數(shù)據(jù)文件的話, 積累起來,這些東西可能會(huì)占據(jù)相當(dāng)大的一部分磁盤空間。你可以調(diào)用 VACUUM 來恢復(fù)那些磁盤空間。
FILE FORMATS 文件格式
TEXT FORMAT 文本格式
當(dāng)不帶 BINARY 選項(xiàng)使用 COPY 時(shí), 讀寫的文件是一個(gè)文本文件,每行代表表中一個(gè)行。 行中的列(字段)用分隔符分開。 字段值本身是由與每個(gè)字段類型相關(guān)的輸出函數(shù)生成的字符串, 或者是輸入函數(shù)可接受的字串。 數(shù)據(jù)中使用特定的空值字串表示那些為 NULL 的字段。 如果輸入文件的任意行包含比預(yù)期多或者少的字段,那么 COPY FROM 將拋出一個(gè)錯(cuò)誤。 如果聲明了 OIDS,那么 OID 將作為第一個(gè)字段讀寫, 放在所有用戶字段前面。
數(shù)據(jù)的結(jié)束可以用一個(gè)只包含反斜扛和句點(diǎn)(\.)的行表示。 如果從文件中讀取數(shù)據(jù),那么數(shù)據(jù)結(jié)束的標(biāo)記是不必要的, 因?yàn)槲募Y(jié)束起的作用就很好了;但是在 3.0 之前的客戶端協(xié)議里,如果在客戶端應(yīng)用之間拷貝數(shù)據(jù), 那么必須要有結(jié)束標(biāo)記。
反斜扛字符(\)可以用在 COPY 里給那些會(huì)有歧義的字符進(jìn)行逃逸(否則那些字符會(huì)被當(dāng)做行或者字段分隔符處理)。 特別是下面的字符如果是字段值的一部分時(shí),必須前綴一個(gè)反斜扛:反斜扛本身,換行符,回車,以及當(dāng)前分隔符。
聲明的空字串被 COPY TO 不加任何反斜杠發(fā)送;與之相對(duì),COPY FROM 在刪除反斜杠之前拿它的輸入與空字串比較。因此,像 \N 這樣的空字串不會(huì)和實(shí)際數(shù)據(jù)值 \N 之間混淆(因?yàn)楹笳邥?huì)表現(xiàn)成 \\N)。
COPY FROM 識(shí)別下列特殊反斜扛序列:
- \b 退格 (ASCII 8)
\f 進(jìn)紙 (ASCII 12)
\n 新行 (ASCII 10)
\r 回車 (ASCII 13)
\t 跳格 (ASCII 9)
\v 豎直跳格 (ASCII 11)
\digits (反斜杠,后面是三個(gè)八進(jìn)制數(shù)值,代表具有指定值的字符)
目前,COPY TO 將絕不會(huì)發(fā)出一個(gè)八進(jìn)制反斜扛序列, 但是它的確使用了上面列出的其它字符用于控制字符。
絕對(duì)不要把反斜扛放在一個(gè)數(shù)據(jù)字符N或者句點(diǎn)(.)前面。 這樣的組合將分別被誤認(rèn)為是空字串(\.) 或者數(shù)據(jù)結(jié)束標(biāo)記 (\N)。 另外一個(gè)沒有在上面的表中列出的反斜扛字符就是它自己。
我們強(qiáng)烈建議生成 COPY 數(shù)據(jù)的應(yīng)用八換行符和回車分別轉(zhuǎn)換成 \n 和 \r 序列。 目前我們可以用一個(gè)反斜杠和一個(gè)回車表示一個(gè)數(shù)據(jù)回車,以及用一個(gè)反斜扛和一個(gè)換行符表示一個(gè)數(shù)據(jù)換行符。 不過,這樣的表示在將來的版本中缺省時(shí)可能不會(huì)被接受。
They are also highly vulnerable to corruption if the COPY file is transferred across different machines (for example, from Unix to Windows or vice versa).
COPY TO 將再每行的結(jié)尾是用一個(gè) Unix 風(fēng)格的換行符("\n"), 或者是在 MS Windows 上運(yùn)行的服務(wù)器上用("\r\n")標(biāo)記一行終止,但只是用于COPY到服務(wù)器文件里; 為了在不同平臺(tái)之間一致,COPY TO STDOUT 總是發(fā)送 "\n",不管服務(wù)器平臺(tái)是什么。 COPY FROM 可以處理那些以回車符,或者換行符,或者回車換行符作為行結(jié)束的數(shù)據(jù)。 為了減少在數(shù)據(jù)中出現(xiàn)的未逃逸的新行或者回車導(dǎo)致的錯(cuò)誤,如果輸入的行結(jié)尾不像上面這些符號(hào), COPY FROM 會(huì)發(fā)出警告。
BINARY FORMAT 二進(jìn)制格式
在PostgreSQL 7.4 中的 COPY BINARY 的文件格式做了變化。新格式由一個(gè)文件頭,零或多條元組, 以及文件尾組成。文件頭和數(shù)據(jù)現(xiàn)在是網(wǎng)絡(luò)字節(jié)序。
FILE HEADER 文件頭
文件頭由 15 個(gè)字節(jié)的固定域組成,后面跟著一個(gè)變長(zhǎng)的頭擴(kuò)展區(qū)。 固定域是:
- Signature 簽名
- 11-字節(jié)的序列 PGCOPY\n\377\r\n\0 --- 請(qǐng)注意字節(jié)零是簽名是要求的一部分。 (使用這個(gè)簽名是為了讓我們能夠很容易看出文件是否已經(jīng)被一個(gè)非 8 位安全的轉(zhuǎn)換器給糟蹋了。 這個(gè)簽名會(huì)被行結(jié)尾轉(zhuǎn)換過濾器,刪除字節(jié)零,刪除高位,或者奇偶的改變而改變。)
- Flags field 標(biāo)志域
32 位整數(shù)掩碼表示該文件格式的重要方面。 位是從 0(LSB)到 31 (MSB)編碼的 --- 請(qǐng)注意這個(gè)域是以網(wǎng)絡(luò)字節(jié)序存儲(chǔ)的(高位在前), 后繼的整數(shù)都是如此。位 16 - 31 是保留用做關(guān)鍵文件格式信息的; 如果讀者發(fā)現(xiàn)一個(gè)不認(rèn)識(shí)的位出現(xiàn)在這個(gè)范圍內(nèi),那么它應(yīng)該退出。 位 0-15 都保留為標(biāo)志向后兼容的格式使用;讀者可以忽略這個(gè)范圍內(nèi)的不認(rèn)識(shí)的位。目前只定義了一個(gè)標(biāo)志位,而其它的必須是零:- Bit 16
如果為 1,那么在數(shù)據(jù)中包括了 OID;如果為 0,則沒有
- 頭擴(kuò)展范圍長(zhǎng)度
32 位整數(shù),以字節(jié)計(jì)的頭剩余長(zhǎng)度,不包括自身。目前,它是零, 后面緊跟第一條元組。對(duì)該格式的更多的修改都將允許額外的數(shù)據(jù)出現(xiàn)在頭中。 讀者應(yīng)該忽略任何它不知道該如何處理的頭擴(kuò)展數(shù)據(jù)。
頭擴(kuò)展數(shù)據(jù)是一個(gè)用來保留一個(gè)自定義的數(shù)據(jù)序列塊用的。這個(gè)標(biāo)志域無意告訴讀者擴(kuò)展區(qū)的內(nèi)容是什么。頭擴(kuò)展的具體設(shè)計(jì)內(nèi)容留給以后的版本用。
這樣設(shè)計(jì)就允許向下兼容頭附加(增加頭擴(kuò)展塊,或者設(shè)置低位序標(biāo)志位)以及非向下兼容修改(設(shè)置高位標(biāo)志位以標(biāo)識(shí)這樣的修改, 并且根據(jù)需要向擴(kuò)展區(qū)域增加支持?jǐn)?shù)據(jù))。
TUPLES 元組
每條元組都以一個(gè) 16 位整數(shù)計(jì)數(shù)開頭,該計(jì)數(shù)是元組中字段的數(shù)目。(目前,在一個(gè)表里的每條元組都有相同的計(jì)數(shù),但可能不會(huì)永遠(yuǎn)這樣。)然后后面不斷出現(xiàn)元組中的各個(gè)字段,字段先是一個(gè) 32 位的長(zhǎng)度字,后面跟著那么長(zhǎng)的字段數(shù)據(jù)。(長(zhǎng)度字并不包括自己,并且可以為零。)一個(gè)特例是:-1 表示一個(gè) NULL 字段值。在 NULL 情況下,后面不會(huì)跟著數(shù)值字節(jié)。
在數(shù)據(jù)域之間沒有對(duì)奇填充或者任何其它額外的數(shù)據(jù)。
目前,一個(gè) COPY BINARY 文件里的所有數(shù)據(jù)值都假設(shè)是二進(jìn)制格式的(格式代碼為一)。預(yù)計(jì)將來的擴(kuò)展可能增加一個(gè)頭域,允許為每個(gè)字段聲明格式代碼。
為了判斷實(shí)際元組數(shù)據(jù)的正確的二進(jìn)制格式,你應(yīng)該閱讀 PostgreSQL 源代碼,特別是該字段數(shù)據(jù)類型的 *send 和 *recv 函數(shù)(典型的函數(shù)可以在源代碼的 src/backend/utils/adt/ 目錄找到)。
如果在文件中包括了 OID,那么該 OID 域立即跟在字段計(jì)數(shù)字后面。它是一個(gè)普通的字段,只不過它沒有包括在字段計(jì)數(shù)。但它包括長(zhǎng)度字 --- 這樣就允許我們不用花太多的勁就可以處理 4 字節(jié)和 8 字節(jié)的 OID,并且如果某個(gè)家伙允許 OID 是可選的話,那么還可以把 OID 顯示成空。
FILE TRAILER 文件尾
文件尾包括保存著 -1 的一個(gè) 16 位整數(shù)字。這樣就很容易與一條元組的域計(jì)數(shù)字相區(qū)分。
如果一個(gè)域計(jì)數(shù)字既不是 -1 也不是預(yù)期的字段的數(shù)目,那么讀者應(yīng)該報(bào)錯(cuò)。 這樣就提供了對(duì)丟失與數(shù)據(jù)的同步的額外的檢查。
EXAMPLES 例子
下面的例子把一個(gè)表拷貝到客戶端, 使用豎直條(|)作為域分隔符:
COPY country TO STDOUT WITH DELIMITER '|';
從一個(gè) Unix 文件中拷貝數(shù)據(jù)到一個(gè)country表中:
COPY country FROM '/usr1/proj/bray/sql/country_data';
下面是一個(gè)可以從 STDIN 中拷貝數(shù)據(jù)到表中的例子:
AF AFGHANISTAN AL ALBANIA DZ ALGERIA ZM ZAMBIA ZW ZIMBABWE
請(qǐng)注意在這里每行里的空白實(shí)際上是一個(gè)水平制表符 tab。
下面的是同樣的數(shù)據(jù),在一臺(tái) Linux/i586 機(jī)器上以二進(jìn)制形式輸出。 這些數(shù)據(jù)是用 Unix 工具 od -c 過濾之后輸出的。 該表有三個(gè)字段;第一個(gè)是 char(2), 第二個(gè)是 text, 第三個(gè)是integer。所有的行在第三個(gè)域都是一個(gè) null 值。
0000000 P G C O P Y \n 377 \r \n \0 \0 \0 \0 \0 \0 0000020 \0 \0 \0 \0 003 \0 \0 \0 002 A F \0 \0 \0 013 A 0000040 F G H A N I S T A N 377 377 377 377 \0 003 0000060 \0 \0 \0 002 A L \0 \0 \0 007 A L B A N I 0000100 A 377 377 377 377 \0 003 \0 \0 \0 002 D Z \0 \0 \0 0000120 007 A L G E R I A 377 377 377 377 \0 003 \0 \0 0000140 \0 002 Z M \0 \0 \0 006 Z A M B I A 377 377 0000160 377 377 \0 003 \0 \0 \0 002 Z W \0 \0 \0 \b Z I 0000200 M B A B W E 377 377 377 377 377 377
#p#
NAME
COPY - copy data between a file and a table
SYNOPSIS
COPY tablename [ ( column [, ...] ) ] FROM { 'filename' | STDIN } [ [ WITH ] [ BINARY ] [ OIDS ] [ DELIMITER [ AS ] 'delimiter' ] [ NULL [ AS ] 'null string' ] ] COPY tablename [ ( column [, ...] ) ] TO { 'filename' | STDOUT } [ [ WITH ] [ BINARY ] [ OIDS ] [ DELIMITER [ AS ] 'delimiter' ] [ NULL [ AS ] 'null string' ] ]
DESCRIPTION
COPY moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already).
If a list of columns is specified, COPY will only copy the data in the specified columns to or from the file. If there are any columns in the table that are not in the column list, COPY FROM will insert the default values for those columns.
COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible to the server and the name must be specified from the viewpoint of the server. When STDIN or STDOUT is specified, data is transmitted via the connection between the client and the server.
PARAMETERS
- tablename
- The name (optionally schema-qualified) of an existing table.
- column
- An optional list of columns to be copied. If no column list is specified, all columns will be used.
- filename
- The absolute path name of the input or output file.
- STDIN
- Specifies that input comes from the client application.
- STDOUT
- Specifies that output goes to the client application.
- BINARY
- Causes all data to be stored or read in binary format rather than as text. You cannot specify the DELIMITER or NULL options in binary mode.
- OIDS
- Specifies copying the OID for each row. (An error is raised if OIDS is specified for a table that does not have OIDs.)
- delimiter
- The single character that separates columns within each row (line) of the file. The default is a tab character.
- null string
- The string that represents a null value. The default is \N (backslash-N). You might prefer an empty string, for example.
- Note: On a COPY FROM, any data item that matches this string will be stored as a null value, so you should make sure that you use the same string as you used with COPY TO.
NOTES
COPY can only be used with plain tables, not with views.
The BINARY key word causes all data to be stored/read as binary format rather than as text. It is somewhat faster than the normal text mode, but a binary-format file is less portable across machine architectures and PostgreSQL versions.
You must have select privilege on the table whose values are read by COPY TO, and insert privilege on the table into which values are inserted by COPY FROM.
Files named in a COPY command are read or written directly by the server, not by the client application. Therefore, they must reside on or be accessible to the database server machine, not the client. They must be accessible to and readable or writable by the PostgreSQL user (the user ID the server runs as), not the client. COPY naming a file is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access.
Do not confuse COPY with the psql instruction \copy. \copy invokes COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a file accessible to the psql client. Thus, file accessibility and access rights depend on the client rather than the server when \copy is used.
It is recommended that the file name used in COPY always be specified as an absolute path. This is enforced by the server in the case of COPY TO, but for COPY FROM you do have the option of reading from a file specified by a relative path. The path will be interpreted relative to the working directory of the server process (somewhere below the data directory), not the client's working directory.
COPY FROM will invoke any triggers and check constraints on the destination table. However, it will not invoke rules.
COPY stops operation at the first error. This should not lead to problems in the event of a COPY TO, but the target table will already have received earlier rows in a COPY FROM. These rows will not be visible or accessible, but they still occupy disk space. This may amount to a considerable amount of wasted disk space if the failure happened well into a large copy operation. You may wish to invoke VACUUM to recover the wasted space.
FILE FORMATS
TEXT FORMAT
When COPY is used without the BINARY option, the data read or written is a text file with one line per table row. Columns in a row are separated by the delimiter character. The column values themselves are strings generated by the output function, or acceptable to the input function, of each attribute's data type. The specified null string is used in place of columns that are null. COPY FROM will raise an error if any line of the input file contains more or fewer columns than are expected. If OIDS is specified, the OID is read or written as the first column, preceding the user data columns.
End of data can be represented by a single line containing just backslash-period (\.). An end-of-data marker is not necessary when reading from a file, since the end of file serves perfectly well; it is needed only when copying data to or from client applications using pre-3.0 client protocol.
Backslash characters (\) may be used in the COPY data to quote data characters that might otherwise be taken as row or column delimiters. In particular, the following characters must be preceded by a backslash if they appear as part of a column value: backslash itself, newline, carriage return, and the current delimiter character.
The specified null string is sent by COPY TO without adding any backslashes; conversely, COPY FROM matches the input against the null string before removing backslashes. Therefore, a null string such as \N cannot be confused with the actual data value \N (which would be represented as \\N).
The following special backslash sequences are recognized by COPY FROM: SequenceRepresents\bBackspace (ASCII 8)\fForm feed (ASCII 12)\nNewline (ASCII 10)\rCarriage return (ASCII 13)\tTab (ASCII 9)\vVertical tab (ASCII 11)\digitsBackslash followed by one to three octal digits specifies the character with that numeric code Presently, COPY TO will never emit an octal-digits backslash sequence, but it does use the other sequences listed above for those control characters.
Any other backslashed character that is not mentioned in the above table will be taken to represent itself. However, beware of adding backslashes unnecessarily, since that might accidentally produce a string matching the end-of-data marker (\.) or the null string (\N by default). These strings will be recognized before any other backslash processing is done.
It is strongly recommended that applications generating COPY data convert data newlines and carriage returns to the \n and \r sequences respectively. At present it is possible to represent a data carriage return by a backslash and carriage return, and to represent a data newline by a backslash and newline. However, these representations might not be accepted in future releases. They are also highly vulnerable to corruption if the COPY file is transferred across different machines (for example, from Unix to Windows or vice versa).
COPY TO will terminate each row with a Unix-style newline (``\n''). Servers running on MS Windows instead output carriage return/newline (``\r\n''), but only for COPY to a server file; for consistency across platforms, COPY TO STDOUT always sends ``\n'' regardless of server platform. COPY FROM can handle lines ending with newlines, carriage returns, or carriage return/newlines. To reduce the risk of error due to un-backslashed newlines or carriage returns that were meant as data, COPY FROM will complain if the line endings in the input are not all alike.
BINARY FORMAT
The file format used for COPY BINARY changed in PostgreSQL 7.4. The new format consists of a file header, zero or more tuples containing the row data, and a file trailer. Headers and data are now in network byte order.
FILE HEADER
The file header consists of 15 bytes of fixed fields, followed by a variable-length header extension area. The fixed fields are:
- Signature
- 11-byte sequence PGCOPY\n\377\r\n\0 --- note that the zero byte is a required part of the signature. (The signature is designed to allow easy identification of files that have been munged by a non-8-bit-clean transfer. This signature will be changed by end-of-line-translation filters, dropped zero bytes, dropped high bits, or parity changes.)
- Flags field
- 32-bit integer bit mask to denote important aspects of the file format. Bits are numbered from 0 (LSB) to 31 (MSB). Note that this field is stored in network byte order (most significant byte first), as are all the integer fields used in the file format. Bits 16-31 are reserved to denote critical file format issues; a reader should abort if it finds an unexpected bit set in this range. Bits 0-15 are reserved to signal backwards-compatible format issues; a reader should simply ignore any unexpected bits set in this range. Currently only one flag bit is defined, and the rest must be zero:
- Bit 16
- if 1, OIDs are included in the data; if 0, not
- Header extension area length
- 32-bit integer, length in bytes of remainder of header, not including self. Currently, this is zero, and the first tuple follows immediately. Future changes to the format might allow additional data to be present in the header. A reader should silently skip over any header extension data it does not know what to do with.
The header extension area is envisioned to contain a sequence of self-identifying chunks. The flags field is not intended to tell readers what is in the extension area. Specific design of header extension contents is left for a later release.
This design allows for both backwards-compatible header additions (add header extension chunks, or set low-order flag bits) and non-backwards-compatible changes (set high-order flag bits to signal such changes, and add supporting data to the extension area if needed).
TUPLES
Each tuple begins with a 16-bit integer count of the number of fields in the tuple. (Presently, all tuples in a table will have the same count, but that might not always be true.) Then, repeated for each field in the tuple, there is a 32-bit length word followed by that many bytes of field data. (The length word does not include itself, and can be zero.) As a special case, -1 indicates a NULL field value. No value bytes follow in the NULL case.
There is no alignment padding or any other extra data between fields.
Presently, all data values in a COPY BINARY file are assumed to be in binary format (format code one). It is anticipated that a future extension may add a header field that allows per-column format codes to be specified.
To determine the appropriate binary format for the actual tuple data you should consult the PostgreSQL source, in particular the *send and *recv functions for each column's data type (typically these functions are found in the src/backend/utils/adt/ directory of the source distribution).
If OIDs are included in the file, the OID field immediately follows the field-count word. It is a normal field except that it's not included in the field-count. In particular it has a length word --- this will allow handling of 4-byte vs. 8-byte OIDs without too much pain, and will allow OIDs to be shown as null if that ever proves desirable.
FILE TRAILER
The file trailer consists of a 16-bit integer word containing -1. This is easily distinguished from a tuple's field-count word.
A reader should report an error if a field-count word is neither -1 nor the expected number of columns. This provides an extra check against somehow getting out of sync with the data.
EXAMPLES
The following example copies a table to the client using the vertical bar (|) as the field delimiter:
COPY country TO STDOUT WITH DELIMITER '|';
To copy data from a file into the country table:
COPY country FROM '/usr1/proj/bray/sql/country_data';
Here is a sample of data suitable for copying into a table from STDIN:
AF AFGHANISTAN AL ALBANIA DZ ALGERIA ZM ZAMBIA ZW ZIMBABWE
Note that the white space on each line is actually a tab character.
The following is the same data, output in binary format. The data is shown after filtering through the Unix utility od -c. The table has three columns; the first has type char(2), the second has type text, and the third has type integer. All the rows have a null value in the third column.
0000000 P G C O P Y \n 377 \r \n \0 \0 \0 \0 \0 \0 0000020 \0 \0 \0 \0 003 \0 \0 \0 002 A F \0 \0 \0 013 A 0000040 F G H A N I S T A N 377 377 377 377 \0 003 0000060 \0 \0 \0 002 A L \0 \0 \0 007 A L B A N I 0000100 A 377 377 377 377 \0 003 \0 \0 \0 002 D Z \0 \0 \0 0000120 007 A L G E R I A 377 377 377 377 \0 003 \0 \0 0000140 \0 002 Z M \0 \0 \0 006 Z A M B I A 377 377 0000160 377 377 \0 003 \0 \0 \0 002 Z W \0 \0 \0 \b Z I 0000200 M B A B W E 377 377 377 377 377 377