PostgreSQL 9.2.4 문서 | ||||
---|---|---|---|---|
이전 | 위로 | 장 31. libpq - C Library | 다음 |
The
PQexec
function is adequate for submitting
commands in normal, synchronous applications. It has a few
deficiencies, however, that can be of importance to some users:
PQexec
waits for the command to be completed.
The application might have other work to do (such as maintaining a
user interface), in which case it won't want to block waiting for
the response.
Since the execution of the client application is suspended while it waits for the result, it is hard for the application to decide that it would like to try to cancel the ongoing command. (It can be done from a signal handler, but not otherwise.)
PQexec
can return only one
PGresult
structure. If the submitted command
string contains multiple
SQL
commands, all but
the last
PGresult
are discarded by
PQexec
.
PQexec
always collects the command's entire result,
buffering it in a single
PGresult
. While
this simplifies error-handling logic for the application, it can be
impractical for results containing many rows.
Applications that do not like these limitations can instead use the
underlying functions that
PQexec
is built from:
PQsendQuery
and
PQgetResult
.
There are also
PQsendQueryParams
,
PQsendPrepare
,
PQsendQueryPrepared
,
PQsendDescribePrepared
, and
PQsendDescribePortal
,
which can be used with
PQgetResult
to duplicate
the functionality of
PQexecParams
,
PQprepare
,
PQexecPrepared
,
PQdescribePrepared
, and
PQdescribePortal
respectively.
PQsendQuery
int PQsendQuery(PGconn *conn, const char *command);
PQsendQueryParams
Submits a command and separate parameters to the server without waiting for the result(s).
int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);
PQsendPrepare
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes);
PQsendQueryPrepared
int PQsendQueryPrepared(PGconn *conn, const char *stmtName, int nParams, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);
PQsendDescribePrepared
int PQsendDescribePrepared(PGconn *conn, const char *stmtName);
PQsendDescribePortal
Submits a request to obtain information about the specified portal, without waiting for completion.
int PQsendDescribePortal(PGconn *conn, const char *portalName);
PQgetResult
PGresult *PQgetResult(PGconn *conn);
Another frequently-desired feature that can be obtained with
PQsendQuery
andPQgetResult
is retrieving large query results a row at a time. This is discussed in 31.5절 .By itself, calling
PQgetResult
will still cause the client to block until the server completes the next SQL command. This can be avoided by proper use of two more functions:
PQconsumeInput
If input is available from the server, consume it.
int PQconsumeInput(PGconn *conn);PQisBusy
int PQisBusy(PGconn *conn);A typical application using these functions will have a main loop that uses
select()
orpoll()
to wait for all the conditions that it must respond to. One of the conditions will be input available from the server, which in terms ofselect()
means readable data on the file descriptor identified byPQsocket
. When the main loop detects input ready, it should callPQconsumeInput
to read the input. It can then callPQisBusy
, followed byPQgetResult
ifPQisBusy
returns false (0). It can also callPQnotifies
to detect NOTIFY messages (see 31.8절 ).A client that uses
PQsendQuery
/PQgetResult
can also attempt to cancel a command that is still being processed by the server; see 31.6절 . But regardless of the return value ofPQcancel
, the application must continue with the normal result-reading sequence usingPQgetResult
. A successful cancellation will simply cause the command to terminate sooner than it would have otherwise.By using the functions described above, it is possible to avoid blocking while waiting for input from the database server. However, it is still possible that the application will block waiting to send output to the server. This is relatively uncommon but can happen if very long SQL commands or data values are sent. (It is much more probable if the application sends data via COPY IN , however.) To prevent this possibility and achieve completely nonblocking database operation, the following additional functions can be used.
PQsetnonblocking
Sets the nonblocking status of the connection.
int PQsetnonblocking(PGconn *conn, int arg);PQisnonblocking
Returns the blocking status of the database connection.
int PQisnonblocking(const PGconn *conn);Returns 1 if the connection is set to nonblocking mode and 0 if blocking.
PQflush
int PQflush(PGconn *conn);