PQconnectdb, and so the application can manage this operation in parallel with other activities.
The database connection is made using the parameters taken from the string
conninfo, passed to PQconnectStart. This string is in
the same format as described above for PQconnectdb.
Neither PQconnectStart nor PQconnectPoll will block, so long as a number of
restrictions are met:
The hostaddr and host parameters are used appropriately to ensure that
name and reverse name queries are not made. See the documentation of
these parameters under PQconnectdb above for details.
If you call PQtrace, ensure that the stream object
into which you trace will not block.
You ensure that the socket is in the appropriate state
before calling PQconnectPoll, as described below.
To begin a nonblocking connection request, call conn = PQconnectStart("connection_info_string").
If conn is null, then libpq has been unable to allocate a new PGconn
structure. Otherwise, a valid PGconn pointer is returned (though not yet
representing a valid connection to the database). On return from
PQconnectStart, call status = PQstatus(conn). If status equals
CONNECTION_BAD, PQconnectStart has failed.
If PQconnectStart succeeds, the next stage is to poll
libpq so that it can proceed with the connection sequence.
Use PQsocket(conn) to obtain the descriptor of the
socket underlying the database connection.
Loop thus: If PQconnectPoll(conn) last returned
PGRES_POLLING_READING, wait until the socket is ready to
read (as indicated by select(), poll(), or
similar system function).
Then call PQconnectPoll(conn) again.
Conversely, if PQconnectPoll(conn) last returned
PGRES_POLLING_WRITING, wait until the socket is ready
to write, then call PQconnectPoll(conn) again.
If you have yet to call
PQconnectPoll, i.e., just after the call to
PQconnectStart, behave as if it last returned
PGRES_POLLING_WRITING. Continue this loop until
PQconnectPoll(conn) returns
PGRES_POLLING_FAILED, indicating the connection procedure
has failed, or PGRES_POLLING_OK, indicating the connection
has been successfully made.
At any time during connection, the status of the connection can be
checked by calling PQstatus. If this gives CONNECTION_BAD, then the
connection procedure has failed; if it gives CONNECTION_OK, then the
connection is ready. Both of these states are equally detectable
from the return value of PQconnectPoll, described above. Other states might also occur
during (and only during) an asynchronous connection procedure. These
indicate the current stage of the connection procedure and might be useful
to provide feedback to the user for example. These statuses are:
Waiting for connection to be made.
Connection OK; waiting to send.
Waiting for a response from the server.
Received authentication; waiting for backend start-up to finish.
Negotiating SSL encryption.
Negotiating environment-driven parameter settings.
Note that, although these constants will remain (in order to maintain compatibility), an application should never rely upon these occurring in a particular order, or at all, or on the status always being one of these documented values. An application might do something like this:
switch(PQstatus(conn))
{
case CONNECTION_STARTED:
feedback = "Connecting...";
break;
case CONNECTION_MADE:
feedback = "Connected to server...";
break;
.
.
.
default:
feedback = "Connecting...";
}
The connect_timeout connection parameter is ignored
when using PQconnectPoll; it is the application's
responsibility to decide whether an excessive amount of time has elapsed.
Otherwise, PQconnectStart followed by a
PQconnectPoll loop is equivalent to
PQconnectdb.
Note that if PQconnectStart returns a non-null pointer, you must call
PQfinish when you are finished with it, in order to dispose of
the structure and any associated memory blocks. This must be done even if
the connection attempt fails or is abandoned.
PQconndefaultsReturns the default connection options.
PQconninfoOption *PQconndefaults(void);
typedef struct
{
char *keyword; /* The keyword of the option */
char *envvar; /* Fallback environment variable name */
char *compiled; /* Fallback compiled in default value */
char *val; /* Option's current value, or NULL */
char *label; /* Label for field in connect dialog */
char *dispchar; /* Indicates how to display this field
in a connect dialog. Values are:
"" Display entered value as is
"*" Password field - hide value
"D" Debug option - don't show by default */
int dispsize; /* Field size in characters for dialog */
} PQconninfoOption;
Returns a connection options array. This can be used to determine
all possible PQconnectdb options and their
current default values. The return value points to an array of
PQconninfoOption structures, which ends
with an entry having a null keyword pointer. The
null pointer is returned if memory could not be allocated. Note that
the current default values (val fields)
will depend on environment variables and other context. Callers
must treat the connection options data as read-only.
After processing the options array, free it by passing it to
PQconninfoFree. If this is not done, a small amount of memory
is leaked for each call to PQconndefaults.
PQconninfoParseReturns parsed connection options from the provided connection string.
PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
Parses a connection string and returns the resulting options as an
array; or returns NULL if there is a problem with the connection
string. This can be used to determine
the PQconnectdb options in the provided
connection string. The return value points to an array of
PQconninfoOption structures, which ends
with an entry having a null keyword pointer.
Note that only options explicitly specified in the string will have values set in the result array; no defaults are inserted.
If errmsg is not NULL, then *errmsg is set to NULL on success, else to a malloc'd error string explaining the problem. (It is also possible for *errmsg to be set to NULL even when NULL is returned; this indicates an out-of-memory situation.)
After processing the options array, free it by passing it to
PQconninfoFree. If this is not done, some memory
is leaked for each call to PQconninfoParse.
Conversely, if an error occurs and errmsg is not NULL,
be sure to free the error string using PQfreemem.
PQfinishCloses the connection to the server. Also frees memory used by the PGconn object.
void PQfinish(PGconn *conn);
Note that even if the server connection attempt fails (as
indicated by PQstatus), the application should call PQfinish
to free the memory used by the PGconn object.
The PGconn pointer must not be used again after
PQfinish has been called.
PQresetResets the communication channel to the server.
void PQreset(PGconn *conn);
This function will close the connection to the server and attempt to reestablish a new connection to the same server, using all the same parameters previously used. This might be useful for error recovery if a working connection is lost.
PQresetStartPQresetPollReset the communication channel to the server, in a nonblocking manner.
int PQresetStart(PGconn *conn);
PostgresPollingStatusType PQresetPoll(PGconn *conn);
These functions will close the connection to the server and attempt to
reestablish a new connection to the same server, using all the same
parameters previously used. This can be useful for error recovery if a
working connection is lost. They differ from PQreset (above) in that they
act in a nonblocking manner. These functions suffer from the same
restrictions as PQconnectStart and PQconnectPoll.
To initiate a connection reset, call
PQresetStart. If it returns 0, the reset has
failed. If it returns 1, poll the reset using
PQresetPoll in exactly the same way as you
would create the connection using PQconnectPoll.