|Prev||Chapter 40. libpq||Next|
The copy command in Postgres has options to read from or write to the network connection used by libpq. Therefore, functions are necessary to access this network connection directly so applications may take full advantage of this capability.
PQgetline Reads a newline-terminated line of characters (transmitted by the backend server) into a buffer string of size length. Like fgets(3), this routine copies up to length-1 characters into string. It is like gets(3), however, in that it converts the terminating newline into a null character. PQgetline returns EOF at EOF, 0 if the entire line has been read, and 1 if the buffer is full but the terminating newline has not yet been read. Notice that the application must check to see if a new line consists of the single character ".", which indicates that the backend server has finished sending the results of the copy command. Therefore, if the application ever expects to receive lines that are more than length-1 characters long, the application must be sure to check the return value of PQgetline very carefully. The code in ../src/bin/psql/psql.c contains routines that correctly handle the copy protocol.
int PQgetline(PGconn *conn, char *string, int length)
PQputline Sends a null-terminated string to the backend server. The application must explicitly send the single character "." to indicate to the backend that it has finished sending its data.
void PQputline(PGconn *conn, char *string);
PQendcopy Syncs with the backend. This function waits until the backend has finished the copy. It should either be issued when the last string has been sent to the backend using PQputline or when the last string has been received from the backend using PGgetline. It must be issued or the backend may get "out of sync" with the frontend. Upon return from this function, the backend is ready to receive the next query. The return value is 0 on successful completion, nonzero otherwise.
int PQendcopy(PGconn *conn);
PQexec(conn, "create table foo (a int4, b char16, d float8)"); PQexec(conn, "copy foo from stdin"); PQputline(conn, "3<TAB>hello world<TAB>4.5\n"); PQputline(conn,"4<TAB>goodbye world<TAB>7.11\n"); ... PQputline(conn,".\n"); PQendcopy(conn);
|Asynchronous Notification||Up||libpq Tracing Functions|
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.