|
Server : Apache/2.2.2 (Fedora) System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686 User : apache ( 48) PHP Version : 5.2.9 Disable Function : NONE Directory : /proc/self/root/usr/share/doc/postgresql-8.1.9/html/ |
Upload File : |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>libpq - C Library</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.1.9 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Client Interfaces"
HREF="client-interfaces.html"><LINK
REL="PREVIOUS"
TITLE="Client Interfaces"
HREF="client-interfaces.html"><LINK
REL="NEXT"
TITLE="Connection Status Functions"
HREF="libpq-status.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2007-04-20T04:40:08"></HEAD
><BODY
CLASS="CHAPTER"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.1.9 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="client-interfaces.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="client-interfaces.html#AEN24532"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="largeobjects.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="libpq-status.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="LIBPQ"
></A
>Chapter 28. <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> - C Library</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
>28.1. <A
HREF="libpq.html#LIBPQ-CONNECT"
>Database Connection Control Functions</A
></DT
><DT
>28.2. <A
HREF="libpq-status.html"
>Connection Status Functions</A
></DT
><DT
>28.3. <A
HREF="libpq-exec.html"
>Command Execution Functions</A
></DT
><DD
><DL
><DT
>28.3.1. <A
HREF="libpq-exec.html#LIBPQ-EXEC-MAIN"
>Main Functions</A
></DT
><DT
>28.3.2. <A
HREF="libpq-exec.html#LIBPQ-EXEC-SELECT-INFO"
>Retrieving Query Result Information</A
></DT
><DT
>28.3.3. <A
HREF="libpq-exec.html#LIBPQ-EXEC-NONSELECT"
>Retrieving Result Information for Other Commands</A
></DT
><DT
>28.3.4. <A
HREF="libpq-exec.html#LIBPQ-EXEC-ESCAPE-STRING"
>Escaping Strings for Inclusion in SQL Commands</A
></DT
><DT
>28.3.5. <A
HREF="libpq-exec.html#LIBPQ-EXEC-ESCAPE-BYTEA"
>Escaping Binary Strings for Inclusion in SQL Commands</A
></DT
></DL
></DD
><DT
>28.4. <A
HREF="libpq-async.html"
>Asynchronous Command Processing</A
></DT
><DT
>28.5. <A
HREF="libpq-cancel.html"
>Cancelling Queries in Progress</A
></DT
><DT
>28.6. <A
HREF="libpq-fastpath.html"
>The Fast-Path Interface</A
></DT
><DT
>28.7. <A
HREF="libpq-notify.html"
>Asynchronous Notification</A
></DT
><DT
>28.8. <A
HREF="libpq-copy.html"
>Functions Associated with the <TT
CLASS="COMMAND"
>COPY</TT
> Command</A
></DT
><DD
><DL
><DT
>28.8.1. <A
HREF="libpq-copy.html#LIBPQ-COPY-SEND"
>Functions for Sending <TT
CLASS="COMMAND"
>COPY</TT
> Data</A
></DT
><DT
>28.8.2. <A
HREF="libpq-copy.html#LIBPQ-COPY-RECEIVE"
>Functions for Receiving <TT
CLASS="COMMAND"
>COPY</TT
> Data</A
></DT
><DT
>28.8.3. <A
HREF="libpq-copy.html#LIBPQ-COPY-DEPRECATED"
>Obsolete Functions for <TT
CLASS="COMMAND"
>COPY</TT
></A
></DT
></DL
></DD
><DT
>28.9. <A
HREF="libpq-control.html"
>Control Functions</A
></DT
><DT
>28.10. <A
HREF="libpq-notice-processing.html"
>Notice Processing</A
></DT
><DT
>28.11. <A
HREF="libpq-envars.html"
>Environment Variables</A
></DT
><DT
>28.12. <A
HREF="libpq-pgpass.html"
>The Password File</A
></DT
><DT
>28.13. <A
HREF="libpq-pgservice.html"
>The Connection Service File</A
></DT
><DT
>28.14. <A
HREF="libpq-ssl.html"
>SSL Support</A
></DT
><DT
>28.15. <A
HREF="libpq-threading.html"
>Behavior in Threaded Programs</A
></DT
><DT
>28.16. <A
HREF="libpq-build.html"
>Building <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> Programs</A
></DT
><DT
>28.17. <A
HREF="libpq-example.html"
>Example Programs</A
></DT
></DL
></DIV
><A
NAME="AEN24541"
></A
><A
NAME="AEN24543"
></A
><P
> <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> is the <ACRONYM
CLASS="ACRONYM"
>C</ACRONYM
>
application programmer's interface to <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.
<SPAN
CLASS="APPLICATION"
>libpq</SPAN
> is a set of library functions that allow
client programs to pass queries to the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
backend server and to receive the results of these queries.
</P
><P
> <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> is also the underlying engine for several
other <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> application interfaces, including
those written for C++, Perl, Python, Tcl and <SPAN
CLASS="APPLICATION"
>ECPG</SPAN
>.
So some aspects of <SPAN
CLASS="APPLICATION"
>libpq</SPAN
>'s behavior will be
important to you if you use one of those packages. In particular,
<A
HREF="libpq-envars.html"
>Section 28.11</A
>,
<A
HREF="libpq-pgpass.html"
>Section 28.12</A
> and
<A
HREF="libpq-ssl.html"
>Section 28.14</A
>
describe behavior that is visible to the user of any application
that uses <SPAN
CLASS="APPLICATION"
>libpq</SPAN
>.
</P
><P
> Some short programs are included at the end of this chapter (<A
HREF="libpq-example.html"
>Section 28.17</A
>) to show how
to write programs that use <SPAN
CLASS="APPLICATION"
>libpq</SPAN
>. There are also several
complete examples of <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> applications in the
directory <TT
CLASS="FILENAME"
>src/test/examples</TT
> in the source code distribution.
</P
><P
> Client programs that use <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> must
include the header file
<TT
CLASS="FILENAME"
>libpq-fe.h</TT
><A
NAME="AEN24568"
></A
>
and must link with the <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> library.
</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="LIBPQ-CONNECT"
>28.1. Database Connection Control Functions</A
></H1
><P
> The following functions deal with making a connection to a
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> backend server. An
application program can have several backend connections open at
one time. (One reason to do that is to access more than one
database.) Each connection is represented by a
<TT
CLASS="STRUCTNAME"
>PGconn</TT
><A
NAME="AEN24576"
></A
> object, which
is obtained from the function <CODE
CLASS="FUNCTION"
>PQconnectdb</CODE
> or
<CODE
CLASS="FUNCTION"
>PQsetdbLogin</CODE
>. Note that these functions will always
return a non-null object pointer, unless perhaps there is too
little memory even to allocate the <TT
CLASS="STRUCTNAME"
>PGconn</TT
> object.
The <CODE
CLASS="FUNCTION"
>PQstatus</CODE
> function should be called to check
whether a connection was successfully made before queries are sent
via the connection object.
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><CODE
CLASS="FUNCTION"
>PQconnectdb</CODE
><A
NAME="AEN24586"
></A
></DT
><DD
><P
> Makes a new connection to the database server.
</P><PRE
CLASS="SYNOPSIS"
>PGconn *PQconnectdb(const char *conninfo);</PRE
><P></P
><P
> This function opens a new database connection using the parameters taken
from the string <TT
CLASS="LITERAL"
>conninfo</TT
>. Unlike <CODE
CLASS="FUNCTION"
>PQsetdbLogin</CODE
> below,
the parameter set can be extended without changing the function signature,
so use of this function (or its nonblocking analogues <CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
>
and <CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
>) is preferred for new application programming.
</P
><P
> The passed string
can be empty to use all default parameters, or it can contain one or more
parameter settings separated by whitespace.
Each parameter setting is in the form <TT
CLASS="LITERAL"
>keyword = value</TT
>.
Spaces around the equal sign are optional.
To write an empty value or a value containing
spaces, surround it with single quotes, e.g.,
<TT
CLASS="LITERAL"
>keyword = 'a value'</TT
>.
Single quotes and backslashes within the value must be escaped with a
backslash, i.e., <TT
CLASS="LITERAL"
>\'</TT
> and <TT
CLASS="LITERAL"
>\\</TT
>.
</P
><P
> The currently recognized parameter key words are:
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>host</TT
></DT
><DD
><P
> Name of host to connect to.<A
NAME="AEN24608"
></A
>
If this begins with a slash, it specifies Unix-domain
communication rather than TCP/IP communication; the value is the
name of the directory in which the socket file is stored. The
default behavior when <TT
CLASS="LITERAL"
>host</TT
> is not specified
is to connect to a Unix-domain
socket<A
NAME="AEN24611"
></A
> in
<TT
CLASS="FILENAME"
>/tmp</TT
> (or whatever socket directory was specified
when <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> was built). On machines without
Unix-domain sockets, the default is to connect to <TT
CLASS="LITERAL"
>localhost</TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>hostaddr</TT
></DT
><DD
><P
> Numeric IP address of host to connect to. This should be in the
standard IPv4 address format, e.g., <TT
CLASS="LITERAL"
>172.28.40.9</TT
>. If
your machine supports IPv6, you can also use those addresses.
TCP/IP communication is
always used when a nonempty string is specified for this parameter.
</P
><P
> Using <TT
CLASS="LITERAL"
>hostaddr</TT
> instead of <TT
CLASS="LITERAL"
>host</TT
> allows the
application to avoid a host name look-up, which may be important in
applications with time constraints. However, Kerberos authentication
requires the host name. The following therefore applies: If
<TT
CLASS="LITERAL"
>host</TT
> is specified without <TT
CLASS="LITERAL"
>hostaddr</TT
>, a host name
lookup occurs. If <TT
CLASS="LITERAL"
>hostaddr</TT
> is specified without
<TT
CLASS="LITERAL"
>host</TT
>, the value for <TT
CLASS="LITERAL"
>hostaddr</TT
> gives the remote
address. When Kerberos is used, a reverse name query occurs to obtain
the host name for Kerberos. If both
<TT
CLASS="LITERAL"
>host</TT
> and <TT
CLASS="LITERAL"
>hostaddr</TT
> are specified, the value for
<TT
CLASS="LITERAL"
>hostaddr</TT
> gives the remote address; the value for
<TT
CLASS="LITERAL"
>host</TT
> is ignored, unless Kerberos is used, in which case that
value is used for Kerberos authentication. (Note that authentication is
likely to fail if <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> is passed a host name
that is not the name of the machine at <TT
CLASS="LITERAL"
>hostaddr</TT
>.) Also,
<TT
CLASS="LITERAL"
>host</TT
> rather than <TT
CLASS="LITERAL"
>hostaddr</TT
> is used to identify
the connection in <TT
CLASS="FILENAME"
>~/.pgpass</TT
> (see
<A
HREF="libpq-pgpass.html"
>Section 28.12</A
>).
</P
><P
> Without either a host name or host address,
<SPAN
CLASS="APPLICATION"
>libpq</SPAN
> will connect using a
local Unix-domain socket; or on machines without Unix-domain
sockets, it will attempt to connect to <TT
CLASS="LITERAL"
>localhost</TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>port</TT
></DT
><DD
><P
> Port number to connect to at the server host, or socket file
name extension for Unix-domain
connections.<A
NAME="AEN24648"
></A
>
</P
></DD
><DT
><TT
CLASS="LITERAL"
>dbname</TT
></DT
><DD
><P
> The database name. Defaults to be the same as the user name.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>user</TT
></DT
><DD
><P
> <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> user name to connect as.
Defaults to be the same as the operating system name of the user
running the application.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>password</TT
></DT
><DD
><P
> Password to be used if the server demands password authentication.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>connect_timeout</TT
></DT
><DD
><P
> Maximum wait for connection, in seconds (write as a decimal integer
string). Zero or not specified means wait indefinitely. It is not
recommended to use a timeout of less than 2 seconds.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>options</TT
></DT
><DD
><P
> Command-line options to be sent to the server.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>tty</TT
></DT
><DD
><P
> Ignored (formerly, this specified where to send server debug output).
</P
></DD
><DT
><TT
CLASS="LITERAL"
>sslmode</TT
></DT
><DD
><P
> This option determines whether or with what priority an
<ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
> connection will be negotiated with the
server. There are four modes: <TT
CLASS="LITERAL"
>disable</TT
> will attempt
only an unencrypted <ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
> connection;
<TT
CLASS="LITERAL"
>allow</TT
> will negotiate, trying first a
non-<ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
> connection, then if that fails, trying an
<ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
> connection; <TT
CLASS="LITERAL"
>prefer</TT
> (the default)
will negotiate, trying first an <ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
> connection,
then if that fails, trying a regular non-<ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
>
connection; <TT
CLASS="LITERAL"
>require</TT
> will try only an
<ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
> connection.
</P
><P
> If <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> is compiled without SSL support,
using option <TT
CLASS="LITERAL"
>require</TT
> will cause an error, while
options <TT
CLASS="LITERAL"
>allow</TT
> and <TT
CLASS="LITERAL"
>prefer</TT
> will be
accepted but <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> will not in fact attempt
an <ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
>
connection.<A
NAME="AEN24704"
></A
>
</P
></DD
><DT
><TT
CLASS="LITERAL"
>requiressl</TT
></DT
><DD
><P
> This option is deprecated in favor of the <TT
CLASS="LITERAL"
>sslmode</TT
>
setting.
</P
><P
> If set to 1, an <ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
> connection to the server
is required (this is equivalent to <TT
CLASS="LITERAL"
>sslmode</TT
>
<TT
CLASS="LITERAL"
>require</TT
>). <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> will then refuse
to connect if the server does not accept an
<ACRONYM
CLASS="ACRONYM"
>SSL</ACRONYM
> connection. If set to 0 (default),
<SPAN
CLASS="APPLICATION"
>libpq</SPAN
> will negotiate the connection type with
the server (equivalent to <TT
CLASS="LITERAL"
>sslmode</TT
>
<TT
CLASS="LITERAL"
>prefer</TT
>). This option is only available if
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> is compiled with SSL support.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>krbsrvname</TT
></DT
><DD
><P
> Kerberos service name to use when authenticating with Kerberos 5.
This must match the service name specified in the server
configuration for Kerberos authentication to succeed. (See also
<A
HREF="auth-methods.html#KERBEROS-AUTH"
>Section 20.2.3</A
>.)
</P
></DD
><DT
><TT
CLASS="LITERAL"
>service</TT
></DT
><DD
><P
> Service name to use for additional parameters. It specifies a service
name in <TT
CLASS="FILENAME"
>pg_service.conf</TT
> that holds additional connection parameters.
This allows applications to specify only a service name so connection parameters
can be centrally maintained. See <A
HREF="libpq-pgservice.html"
>Section 28.13</A
>.
</P
></DD
></DL
></DIV
><P>
If any parameter is unspecified, then the corresponding
environment variable (see <A
HREF="libpq-envars.html"
>Section 28.11</A
>)
is checked. If the environment variable is not set either,
then the indicated built-in defaults are used.
</P
></DD
><DT
><CODE
CLASS="FUNCTION"
>PQsetdbLogin</CODE
><A
NAME="AEN24740"
></A
></DT
><DD
><P
> Makes a new connection to the database server.
</P><PRE
CLASS="SYNOPSIS"
>PGconn *PQsetdbLogin(const char *pghost,
const char *pgport,
const char *pgoptions,
const char *pgtty,
const char *dbName,
const char *login,
const char *pwd);</PRE
><P></P
><P
> This is the predecessor of <CODE
CLASS="FUNCTION"
>PQconnectdb</CODE
> with a fixed
set of parameters. It has the same functionality except that the
missing parameters will always take on default values. Write <TT
CLASS="SYMBOL"
>NULL</TT
> or an
empty string for any one of the fixed parameters that is to be defaulted.
</P
></DD
><DT
><CODE
CLASS="FUNCTION"
>PQsetdb</CODE
><A
NAME="AEN24751"
></A
></DT
><DD
><P
> Makes a new connection to the database server.
</P><PRE
CLASS="SYNOPSIS"
>PGconn *PQsetdb(char *pghost,
char *pgport,
char *pgoptions,
char *pgtty,
char *dbName);</PRE
><P></P
><P
> This is a macro that calls <CODE
CLASS="FUNCTION"
>PQsetdbLogin</CODE
> with null pointers
for the <TT
CLASS="PARAMETER"
>login</TT
> and <TT
CLASS="PARAMETER"
>pwd</TT
> parameters. It is provided
for backward compatibility with very old programs.
</P
></DD
><DT
><CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
><A
NAME="AEN24763"
></A
><BR><CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
><A
NAME="AEN24767"
></A
></DT
><DD
><P
> <A
NAME="AEN24771"
></A
>
Make a connection to the database server in a nonblocking manner.
</P><PRE
CLASS="SYNOPSIS"
>PGconn *PQconnectStart(const char *conninfo);</PRE
><P>
</P><PRE
CLASS="SYNOPSIS"
>PostgresPollingStatusType PQconnectPoll(PGconn *conn);</PRE
><P></P
><P
> These two functions are used to open a connection to a database server such
that your application's thread of execution is not blocked on remote I/O
whilst doing so.
The point of this approach is that the waits for I/O to complete can occur
in the application's main loop, rather than down inside
<CODE
CLASS="FUNCTION"
>PQconnectdb</CODE
>, and so the application can manage this
operation in parallel with other activities.
</P
><P
> The database connection is made using the parameters taken from the string
<TT
CLASS="LITERAL"
>conninfo</TT
>, passed to <CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
>. This string is in
the same format as described above for <CODE
CLASS="FUNCTION"
>PQconnectdb</CODE
>.
</P
><P
> Neither <CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
> nor <CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
> will block, so long as a number of
restrictions are met:
<P
></P
></P><UL
><LI
><P
> The <TT
CLASS="LITERAL"
>hostaddr</TT
> and <TT
CLASS="LITERAL"
>host</TT
> parameters are used appropriately to ensure that
name and reverse name queries are not made. See the documentation of
these parameters under <CODE
CLASS="FUNCTION"
>PQconnectdb</CODE
> above for details.
</P
></LI
><LI
><P
> If you call <CODE
CLASS="FUNCTION"
>PQtrace</CODE
>, ensure that the stream object
into which you trace will not block.
</P
></LI
><LI
><P
> You ensure that the socket is in the appropriate state
before calling <CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
>, as described below.
</P
></LI
></UL
><P>
</P
><P
> To begin a nonblocking connection request, call <TT
CLASS="LITERAL"
>conn = PQconnectStart("<TT
CLASS="REPLACEABLE"
><I
>connection_info_string</I
></TT
>")</TT
>.
If <TT
CLASS="VARNAME"
>conn</TT
> is null, then <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> has been unable to allocate a new <TT
CLASS="STRUCTNAME"
>PGconn</TT
>
structure. Otherwise, a valid <TT
CLASS="STRUCTNAME"
>PGconn</TT
> pointer is returned (though not yet
representing a valid connection to the database). On return from
<CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
>, call <TT
CLASS="LITERAL"
>status = PQstatus(conn)</TT
>. If <TT
CLASS="VARNAME"
>status</TT
> equals
<TT
CLASS="SYMBOL"
>CONNECTION_BAD</TT
>, <CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
> has failed.
</P
><P
> If <CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
> succeeds, the next stage is to poll
<SPAN
CLASS="APPLICATION"
>libpq</SPAN
> so that it may proceed with the connection sequence.
Use <CODE
CLASS="FUNCTION"
>PQsocket(conn)</CODE
> to obtain the descriptor of the
socket underlying the database connection.
Loop thus: If <CODE
CLASS="FUNCTION"
>PQconnectPoll(conn)</CODE
> last returned
<TT
CLASS="SYMBOL"
>PGRES_POLLING_READING</TT
>, wait until the socket is ready to
read (as indicated by <CODE
CLASS="FUNCTION"
>select()</CODE
>, <CODE
CLASS="FUNCTION"
>poll()</CODE
>, or
similar system function).
Then call <CODE
CLASS="FUNCTION"
>PQconnectPoll(conn)</CODE
> again.
Conversely, if <CODE
CLASS="FUNCTION"
>PQconnectPoll(conn)</CODE
> last returned
<TT
CLASS="SYMBOL"
>PGRES_POLLING_WRITING</TT
>, wait until the socket is ready
to write, then call <CODE
CLASS="FUNCTION"
>PQconnectPoll(conn)</CODE
> again.
If you have yet to call
<CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
>, i.e., just after the call to
<CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
>, behave as if it last returned
<TT
CLASS="SYMBOL"
>PGRES_POLLING_WRITING</TT
>. Continue this loop until
<CODE
CLASS="FUNCTION"
>PQconnectPoll(conn)</CODE
> returns
<TT
CLASS="SYMBOL"
>PGRES_POLLING_FAILED</TT
>, indicating the connection procedure
has failed, or <TT
CLASS="SYMBOL"
>PGRES_POLLING_OK</TT
>, indicating the connection
has been successfully made.
</P
><P
> At any time during connection, the status of the connection may be
checked by calling <CODE
CLASS="FUNCTION"
>PQstatus</CODE
>. If this gives <TT
CLASS="SYMBOL"
>CONNECTION_BAD</TT
>, then the
connection procedure has failed; if it gives <CODE
CLASS="FUNCTION"
>CONNECTION_OK</CODE
>, then the
connection is ready. Both of these states are equally detectable
from the return value of <CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
>, described above. Other states may also occur
during (and only during) an asynchronous connection procedure. These
indicate the current stage of the connection procedure and may be useful
to provide feedback to the user for example. These statuses are:
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="SYMBOL"
>CONNECTION_STARTED</TT
></DT
><DD
><P
> Waiting for connection to be made.
</P
></DD
><DT
><TT
CLASS="SYMBOL"
>CONNECTION_MADE</TT
></DT
><DD
><P
> Connection OK; waiting to send.
</P
></DD
><DT
><TT
CLASS="SYMBOL"
>CONNECTION_AWAITING_RESPONSE</TT
></DT
><DD
><P
> Waiting for a response from the server.
</P
></DD
><DT
><TT
CLASS="SYMBOL"
>CONNECTION_AUTH_OK</TT
></DT
><DD
><P
> Received authentication; waiting for backend start-up to finish.
</P
></DD
><DT
><TT
CLASS="SYMBOL"
>CONNECTION_SSL_STARTUP</TT
></DT
><DD
><P
> Negotiating SSL encryption.
</P
></DD
><DT
><TT
CLASS="SYMBOL"
>CONNECTION_SETENV</TT
></DT
><DD
><P
> Negotiating environment-driven parameter settings.
</P
></DD
></DL
></DIV
><P>
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:
</P><PRE
CLASS="PROGRAMLISTING"
>switch(PQstatus(conn))
{
case CONNECTION_STARTED:
feedback = "Connecting...";
break;
case CONNECTION_MADE:
feedback = "Connected to server...";
break;
.
.
.
default:
feedback = "Connecting...";
}</PRE
><P>
</P
><P
> The <TT
CLASS="LITERAL"
>connect_timeout</TT
> connection parameter is ignored
when using <CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
>; it is the application's
responsibility to decide whether an excessive amount of time has elapsed.
Otherwise, <CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
> followed by a
<CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
> loop is equivalent to
<CODE
CLASS="FUNCTION"
>PQconnectdb</CODE
>.
</P
><P
> Note that if <CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
> returns a non-null pointer, you must call
<CODE
CLASS="FUNCTION"
>PQfinish</CODE
> 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.
</P
></DD
><DT
><CODE
CLASS="FUNCTION"
>PQconndefaults</CODE
><A
NAME="AEN24875"
></A
></DT
><DD
><P
> Returns the default connection options.
</P><PRE
CLASS="SYNOPSIS"
>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; /* Character to display for 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;</PRE
><P></P
><P
> Returns a connection options array. This may be used to determine
all possible <CODE
CLASS="FUNCTION"
>PQconnectdb</CODE
> options and their
current default values. The return value points to an array of
<TT
CLASS="STRUCTNAME"
>PQconninfoOption</TT
> structures, which ends
with an entry having a null <TT
CLASS="STRUCTFIELD"
>keyword</TT
> pointer. The
null pointer is returned if memory could not be allocated. Note that
the current default values (<TT
CLASS="STRUCTFIELD"
>val</TT
> fields)
will depend on environment variables and other context. Callers
must treat the connection options data as read-only.
</P
><P
> After processing the options array, free it by passing it to
<CODE
CLASS="FUNCTION"
>PQconninfoFree</CODE
>. If this is not done, a small amount of memory
is leaked for each call to <CODE
CLASS="FUNCTION"
>PQconndefaults</CODE
>.
</P
></DD
><DT
><CODE
CLASS="FUNCTION"
>PQfinish</CODE
><A
NAME="AEN24891"
></A
></DT
><DD
><P
> Closes the connection to the server. Also frees
memory used by the <TT
CLASS="STRUCTNAME"
>PGconn</TT
> object.
</P><PRE
CLASS="SYNOPSIS"
>void PQfinish(PGconn *conn);</PRE
><P></P
><P
> Note that even if the server connection attempt fails (as
indicated by <CODE
CLASS="FUNCTION"
>PQstatus</CODE
>), the application should call <CODE
CLASS="FUNCTION"
>PQfinish</CODE
>
to free the memory used by the <TT
CLASS="STRUCTNAME"
>PGconn</TT
> object.
The <TT
CLASS="STRUCTNAME"
>PGconn</TT
> pointer must not be used again after
<CODE
CLASS="FUNCTION"
>PQfinish</CODE
> has been called.
</P
></DD
><DT
><CODE
CLASS="FUNCTION"
>PQreset</CODE
><A
NAME="AEN24906"
></A
></DT
><DD
><P
> Resets the communication channel to the server.
</P><PRE
CLASS="SYNOPSIS"
>void PQreset(PGconn *conn);</PRE
><P></P
><P
> 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 may be useful for
error recovery if a working connection is lost.
</P
></DD
><DT
><CODE
CLASS="FUNCTION"
>PQresetStart</CODE
><A
NAME="AEN24915"
></A
><BR><CODE
CLASS="FUNCTION"
>PQresetPoll</CODE
><A
NAME="AEN24919"
></A
></DT
><DD
><P
> Reset the communication channel to the server, in a nonblocking manner.
</P><PRE
CLASS="SYNOPSIS"
>int PQresetStart(PGconn *conn);</PRE
><P>
</P><PRE
CLASS="SYNOPSIS"
>PostgresPollingStatusType PQresetPoll(PGconn *conn);</PRE
><P></P
><P
> 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 may be useful for error recovery if a
working connection is lost. They differ from <CODE
CLASS="FUNCTION"
>PQreset</CODE
> (above) in that they
act in a nonblocking manner. These functions suffer from the same
restrictions as <CODE
CLASS="FUNCTION"
>PQconnectStart</CODE
> and <CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
>.
</P
><P
> To initiate a connection reset, call <CODE
CLASS="FUNCTION"
>PQresetStart</CODE
>. If it returns 0, the reset has failed. If it returns 1,
poll the reset using <CODE
CLASS="FUNCTION"
>PQresetPoll</CODE
> in exactly the same way as you would
create the connection using <CODE
CLASS="FUNCTION"
>PQconnectPoll</CODE
>.
</P
></DD
></DL
></DIV
><P></P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="client-interfaces.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="libpq-status.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Client Interfaces</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="client-interfaces.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Connection Status Functions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>