* Receive CopyData message available from XLOG stream, blocking for * maximum of 'timeout' ms. * * If data was received, returns the length of the data. *buffer is set to * point to a buffer holding the received message. The buffer is only valid * until the next CopyStreamReceive call. * * Returns 0 if no data was available within timeout, or if wait was * interrupted by signal or stop_soc
| 933 | * -1 on error. -2 if the server ended the COPY. |
| 934 | */ |
| 935 | static int |
| 936 | CopyStreamReceive(PGconn *conn, long timeout, pgsocket stop_socket, |
| 937 | char **buffer) |
| 938 | { |
| 939 | char *copybuf = NULL; |
| 940 | int rawlen; |
| 941 | |
| 942 | if (*buffer != NULL) |
| 943 | PQfreemem(*buffer); |
| 944 | *buffer = NULL; |
| 945 | |
| 946 | /* Try to receive a CopyData message */ |
| 947 | rawlen = PQgetCopyData(conn, ©buf, 1); |
| 948 | if (rawlen == 0) |
| 949 | { |
| 950 | int ret; |
| 951 | |
| 952 | /* |
| 953 | * No data available. Wait for some to appear, but not longer than |
| 954 | * the specified timeout, so that we can ping the server. Also stop |
| 955 | * waiting if input appears on stop_socket. |
| 956 | */ |
| 957 | ret = CopyStreamPoll(conn, timeout, stop_socket); |
| 958 | if (ret <= 0) |
| 959 | return ret; |
| 960 | |
| 961 | /* Now there is actually data on the socket */ |
| 962 | if (PQconsumeInput(conn) == 0) |
| 963 | { |
| 964 | pg_log_error("could not receive data from WAL stream: %s", |
| 965 | PQerrorMessage(conn)); |
| 966 | return -1; |
| 967 | } |
| 968 | |
| 969 | /* Now that we've consumed some input, try again */ |
| 970 | rawlen = PQgetCopyData(conn, ©buf, 1); |
| 971 | if (rawlen == 0) |
| 972 | return 0; |
| 973 | } |
| 974 | if (rawlen == -1) /* end-of-streaming or error */ |
| 975 | return -2; |
| 976 | if (rawlen == -2) |
| 977 | { |
| 978 | pg_log_error("could not read COPY data: %s", PQerrorMessage(conn)); |
| 979 | return -1; |
| 980 | } |
| 981 | |
| 982 | /* Return received messages to caller */ |
| 983 | *buffer = copybuf; |
| 984 | return rawlen; |
| 985 | } |
| 986 | |
| 987 | /* |
| 988 | * Process the keepalive message. |
no test coverage detected