RefLib

Reference library for AI

Back

postgresql_16:5.1.10.1 Functions for Sending COPY Data

PostgreSQL 16 > Client Interfaces > libpq — C Library > Functions Associated with the COPY Command > Functions for Sending COPY Data

These functions are used to send data during COPY FROM STDIN. They will fail if called when the connection is not in COPY_IN state.

PQputCopyData

Sends data to the server during COPY_IN state. int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);

Transmits the COPY data in the specified buffer, of length nbytes, to the server. The result is 1 if the data was queued, zero if it was not queued because of full buffers (this will only happen in nonblocking mode), or -1 if an error occurred. (Use (see libpq-PQerrorMessage) to retrieve details if the return value is -1. If the value is zero, wait for write-ready and try again.)

The application can divide the COPY data stream into buffer loads of any convenient size. Buffer-load boundaries have no semantic significance when sending. The contents of the data stream must match the data format expected by the COPY command; see (see postgresql_16:7.1.53) for details.

PQputCopyEnd

Sends end-of-data indication to the server during COPY_IN state. int PQputCopyEnd(PGconn *conn, const char *errormsg);

Ends the COPY_IN operation successfully if errormsg is NULL. If errormsg is not NULL then the COPY is forced to fail, with the string pointed to by errormsg used as the error message. (One should not assume that this exact error message will come back from the server, however, as the server might have already failed the COPY for its own reasons.)

The result is 1 if the termination message was sent; or in nonblocking mode, this may only indicate that the termination message was successfully queued. (In nonblocking mode, to be certain that the data has been sent, you should next wait for write-ready and call (see libpq-PQflush), repeating until it returns zero.) Zero indicates that the function could not queue the termination message because of full buffers; this will only happen in nonblocking mode. (In this case, wait for write-ready and try the (see libpq-PQputCopyEnd) call again.) If a hard error occurs, -1 is returned; you can use (see libpq-PQerrorMessage) to retrieve details.

After successfully calling (see libpq-PQputCopyEnd), call (see libpq-PQgetResult) to obtain the final result status of the COPY command. One can wait for this result to be available in the usual way. Then return to normal operation.

Back