postgresql_16:5.1.10 Functions Associated with the COPY Command
PostgreSQL 16 > Client Interfaces > libpq — C Library > Functions Associated with the COPY Command
The COPY command in PostgreSQL has options to read from or write to the network connection used by libpq. The functions described in this section allow applications to take advantage of this capability by supplying or consuming copied data.
The overall process is that the application first issues the SQL COPY command via (see libpq-PQexec) or one of the equivalent functions. The response to this (if there is no error in the command) will be a PGresult object bearing a status code of PGRES_COPY_OUT or PGRES_COPY_IN (depending on the specified copy direction). The application should then use the functions of this section to receive or transmit data rows. When the data transfer is complete, another PGresult object is returned to indicate success or failure of the transfer. Its status will be PGRES_COMMAND_OK for success or PGRES_FATAL_ERROR if some problem was encountered. At this point further SQL commands can be issued via (see libpq-PQexec). (It is not possible to execute other SQL commands using the same connection while the COPY operation is in progress.)
If a COPY command is issued via (see libpq-PQexec) in a string that could contain additional commands, the application must continue fetching results via (see libpq-PQgetResult) after completing the COPY sequence. Only when (see libpq-PQgetResult) returns NULL is it certain that the (see libpq-PQexec) command string is done and it is safe to issue more commands.
The functions of this section should be executed only after obtaining a result status of PGRES_COPY_OUT or PGRES_COPY_IN from (see libpq-PQexec) or (see libpq-PQgetResult).
A PGresult object bearing one of these status values carries some additional data about the COPY operation that is starting. This additional data is available using functions that are also used in connection with query results: PQnfields Returns the number of columns (fields) to be copied. PQbinaryTuples 0 indicates the overall copy format is textual (rows separated by newlines, columns separated by separator characters, etc.). 1 indicates the overall copy format is binary. See (see postgresql_16:7.1.53) for more information. PQfformat Returns the format code (0 for text, 1 for binary) associated with each column of the copy operation. The per-column format codes will always be zero when the overall copy format is textual, but the binary format can support both text and binary columns. (However, as of the current implementation of COPY, only binary columns appear in a binary copy; so the per-column formats always match the overall format at present.)
Subsections
- postgresql_16:5.1.10.1 Functions for Sending
COPYData - postgresql_16:5.1.10.2 Functions for Receiving
COPYData - postgresql_16:5.1.10.3 Obsolete Functions for
COPY