The PQexec function is adequate for submitting queries in simple synchronous applications. However, it has a couple of major deficiencies:
PQexec waits for the query to be completed. The application may have other work to do (such as maintaining a user interface), in which case it will not want to block waiting for the response.
Since control is buried inside PQexec, it is hard for the frontend to decide if it would like to try to cancel the ongoing query. (It can be done from a signal handler.)
PQexec can return only one PGresult structure. If the submitted query string contains multiple SQL commands, all but the last PGresult are discarded by PQexec.
Applications that do not like these limitations can instead use the underlying functions that PQexec is built from: PQsendQuery and PQgetResult. |
Older programs that used this functionality as well as PQputline and PQputnbytes could block waiting to send data to the backend. To address that issue, the function PQsetnonblocking was added.
Old applications can neglect to use PQsetnonblocking and get the older potentially blocking behavior. Newer programs can use PQsetnonblocking to achieve a completely non-blocking connection to the backend.
PQsetnonblocking sets the nonblocking status of the connection.
int PQsetnonblocking(PGconn *conn, int arg) |
In the nonblocking state, calls to PQputline, PQputnbytes, PQsendQuery and PQendcopy will not block, but instead return an error if they need to be called again.
When a database connection has been set to non-blocking mode and PQexec is called, it will temporarily set the state of the connection to blocking until the PQexec completes.
More of libpq is expected to be made safe for PQsetnonblocking functionality in the near future.
PQisnonblocking returns the blocking status of the database connection.
int PQisnonblocking(const PGconn *conn) |
PQsendQuery submits a query to the backend server without waiting for the results. TRUE is returned if the query was successfully dispatched, FALSE if not (in which case, use PQerrorMessage to get more information about the failure).
int PQsendQuery(PGconn *conn, const char *query); |
PQgetResult waits for the next result from a prior PQsendQuery, and returns it. NULL is returned when the query is complete and there will be no more results.
PGresult *PQgetResult(PGconn *conn); |
Using PQsendQuery and PQgetResult solves one of PQexec's problems: if a query string contains multiple SQL commands, the results of those commands can be obtained individually. (This allows a simple form of overlapped processing as the frontend can be handling the results of one query while the backend is still working on later queries in the same query string.) However, calling PQgetResult will still cause the frontend to block until the backend completes the next SQL command. This can be avoided by proper use of the functions described below:
PQconsumeInput consumes any input available from the backend.
int PQconsumeInput(PGconn *conn); |
PQconsumeInput may be called even if the application is not prepared to deal with a result or notification just yet. The routine will read available data and save it in a buffer, thereby causing a select(2) read-ready indication to go away. The application can thus use PQconsumeInput to clear the select condition immediately, and then examine the results at leisure.
PQisBusy returns 1 if a query is busy; that is, PQgetResult would block waiting for input. A 0 return indicates that PQgetResult can be called with assurance of not blocking.
int PQisBusy(PGconn *conn); |
PQflush attempts to flush any data queued to the backend. Returns 0 if successful (or if the sent queue is empty) or EOF if it failed.
int PQflush(PGconn *conn); |
PQsocket obtains the file descriptor number for the backend connection socket. A valid descriptor will be >= 0; a result of -1 indicates that no backend connection is currently open.
int PQsocket(const PGconn *conn); |
Non-blocking connections (that have used PQsetnonblocking) should not use select until PQflush has returned 0 indicating that there is no buffered data waiting to be sent to the backend.
A typical frontend using these functions will have a main loop that uses select() to wait for all the conditions that it must respond to. One of the conditions will be input available from the backend, which in select's terms is readable data on the file descriptor identified by PQsocket. When the main loop detects input ready, it should call PQconsumeInput to read the input. It can then call PQisBusy, followed by PQgetResult if PQisBusy returns false (0). It can also call PQnotifies to detect NOTIFY messages (see the Section called Asynchronous Notification).
A frontend that uses PQsendQuery/PQgetResult can also attempt to cancel a query that is still being processed by the backend.
PQrequestCancel requests that PostgreSQL abandon processing of the current query.
int PQrequestCancel(PGconn *conn); |
Note that if the current query is part of a transaction, cancellation will abort the whole transaction.
PQrequestCancel can safely be invoked from a signal handler. So, it is also possible to use it in conjunction with plain PQexec, if the decision to cancel can be made in a signal handler. For example, psql invokes PQrequestCancel from a SIGINT signal handler, thus allowing interactive cancellation of queries that it issues through PQexec. Note that PQrequestCancel will have no effect if the connection is not currently open or the backend is not currently processing a query.