@@ -57,14 +57,13 @@ header file <FileName>libpq-fe.h</FileName> and must link with the
5757<synopsis>
5858PGconn *PQconnectdb(const char *conninfo)
5959</synopsis>
60- This routine opens a new database connection using the parameters
61- taken from the string <literal>conninfo</literal>. Unlike PQsetdbLogin()
62- below, the parameter set
63- can be extended without changing the function signature, so use
64- of this routine is prefered for application programming. The passed
65- string can be empty to use all default
66- parameters, or it can contain one or more parameter settings
67- separated by whitespace.
60+ This routine opens a new database connection using the parameters taken
61+ from the string <literal>conninfo</literal>. Unlike PQsetdbLogin() below,
62+ the parameter set can be extended without changing the function signature,
63+ so use either of this routine or the non-blocking analogues PQconnectStart
64+ / PQconnectPoll is prefered for application programming. The passed string
65+ can be empty to use all default parameters, or it can contain one or more
66+ parameter settings separated by whitespace.
6867 </Para>
6968 <Para>
7069 Each parameter setting is in the form <literal>keyword = value</literal>.
@@ -80,8 +79,38 @@ PGconn *PQconnectdb(const char *conninfo)
8079 <term><literal>host</literal></term>
8180 <ListItem>
8281 <Para>
83- Host to connect to. If a non-zero-length string is specified, TCP/IP communication is used.
84- Without a host name, libpq will connect using a local Unix domain socket.
82+ Name of host to connect to. If a non-zero-length string is specified, TCP/IP
83+ communication is used. Using this parameter causes a hostname look-up.
84+ See hostaddr.
85+ </Para>
86+ </ListItem>
87+ </VarListEntry>
88+
89+ <VarListEntry>
90+ <term><literal>hostaddr</literal></term>
91+ <ListItem>
92+ <Para>
93+ IP address of host to connect to. This should be in standard
94+ numbers-and-dots form, as used by the BSD functions inet_aton et al. If
95+ a non-zero-length string is specified, TCP/IP communication is used.
96+ </Para>
97+ <Para>
98+ Using hostaddr instead of host allows the application to avoid a host
99+ name look-up, which may be important in applications with time
100+ constraints. However, Kerberos authentication requires the host
101+ name. The following therefore applies. If host is specified without
102+ hostaddr, a hostname look-up is forced. If hostaddr is specified without
103+ host, the value for hostaddr gives the remote address; if Kerberos is
104+ used, this causes a reverse name query. If both host and hostaddr are
105+ specified, the value for hostaddr gives the remote address; the value
106+ for host is ignored, unless Kerberos is used, in which case that value
107+ is used for Kerberos authentication. Note that authentication is likely
108+ to fail if libpq is passed a host name which is not the name of the
109+ machine at hostaddr.
110+ </Para>
111+ <Para>
112+ Without both a host name and host address, libpq will connect using a
113+ local Unix domain socket.
85114 </Para>
86115 </ListItem>
87116 </VarListEntry>
@@ -149,6 +178,9 @@ PGconn *PQconnectdb(const char *conninfo)
149178 The return value is a pointer to an abstract struct
150179 representing the connection to the backend.
151180 </Para>
181+ <Para>
182+ This function is not thread-safe.
183+ </Para>
152184 </ListItem>
153185
154186 <ListItem>
@@ -167,6 +199,9 @@ PGconn *PQsetdbLogin(const char *pghost,
167199 This is the predecessor of <function>PQconnectdb</function> with a fixed number
168200 of parameters but the same functionality.
169201 </Para>
202+ <Para>
203+ This function is not thread-safe.
204+ </Para>
170205 </ListItem>
171206
172207 <ListItem>
@@ -185,6 +220,173 @@ PGconn *PQsetdb(char *pghost,
185220 </Para>
186221 </ListItem>
187222
223+ <ListItem>
224+ <Para>
225+ <Function>PQconnectStart</Function>
226+ <Function>PQconnectPoll</Function>
227+ Make a connection to the database server in a non-blocking manner.
228+ <synopsis>
229+ PGconn *PQconnectStart(const char *conninfo)
230+ </synopsis>
231+ <synopsis>
232+ PostgresPollingStatusType *PQconnectPoll(PQconn *conn)
233+ </synopsis>
234+ These two routines are used to open a connection to a database server such
235+ that your application's thread of execution is not blocked on remote I/O
236+ whilst doing so.
237+ </Para>
238+ <Para>
239+ The database connection is made using the parameters taken from the string
240+ <literal>conninfo</literal>, passed to PQconnectStart. This string is in
241+ the same format as described above for PQconnectdb.
242+ </Para>
243+ <Para>
244+ Neither PQconnectStart nor PQconnectPoll will block, as long as a number of
245+ restrictions are met:
246+ <ItemizedList>
247+ <ListItem>
248+ <Para>
249+ The hostaddr and host parameters are used appropriately to ensure that
250+ name and reverse name queries are not made. See the documentation of
251+ these parameters under PQconnectdb above for details.
252+ </Para>
253+ </ListItem>
254+
255+ <ListItem>
256+ <Para>
257+ If you call PQtrace, ensure that the stream object into which you trace
258+ will not block.
259+ </Para>
260+ </ListItem>
261+
262+ <ListItem>
263+ <Para>
264+ You ensure for yourself that the socket is in the appropriate state
265+ before calling PQconnectPoll, as described below.
266+ </Para>
267+ </ListItem>
268+ </ItemizedList>
269+ </Para>
270+
271+ <Para>
272+ To begin, call conn=PQconnectStart("<connection_info_string>"). If
273+ conn is NULL, then libpq has been unable to allocate a new PGconn
274+ structure. Otherwise, a valid PGconn pointer is returned (though not yet
275+ representing a valid connection to the database). On return from
276+ PQconnectStart, call status=PQstatus(conn). If status equals
277+ CONNECTION_BAD, PQconnectStart has failed.
278+ </Para>
279+ <Para>
280+ If PQconnectStart succeeds, the next stage is to poll libpq so that it may
281+ proceed with the connection sequence. Loop thus: Consider a connection
282+ 'inactive' by default. If PQconnectPoll last returned PGRES_POLLING_ACTIVE,
283+ consider it 'active' instead. If PQconnectPoll(conn) last returned
284+ PGRES_POLLING_READING, perform a select for reading on PQsocket(conn). If
285+ it last returned PGRES_POLLING_WRITING, perform a select for writing on
286+ PQsocket(conn). If you have yet to call PQconnectPoll, i.e. after the call
287+ to PQconnectStart, behave as if it last returned PGRES_POLLING_WRITING. If
288+ the select shows that the socket is ready, consider it 'active'. If it has
289+ been decided that this connection is 'active', call PQconnectPoll(conn)
290+ again. If this call returns PGRES_POLLING_FAILED, the connection procedure
291+ has failed. If this call returns PGRES_POLLING_OK, the connection has been
292+ successfully made.
293+ </Para>
294+ <Para>
295+ Note that the use of select() to ensure that the socket is ready is merely
296+ a (likely) example; those with other facilities available, such as a
297+ poll() call, may of course use that instead.
298+ </Para>
299+ <Para>
300+ At any time during connection, the status of the connection may be
301+ checked, by calling PQstatus. If this is CONNECTION_BAD, then the
302+ connection procedure has failed; if this is CONNECTION_OK, then the
303+ connection is ready. Either of these states should be equally detectable
304+ from the return value of PQconnectPoll, as above. Other states may be
305+ shown during (and only during) an asynchronous connection procedure. These
306+ indicate the current stage of the connection procedure, and may be useful
307+ to provide feedback to the user for example. These statuses may include:
308+ <ItemizedList>
309+ <ListItem>
310+ <Para>
311+ CONNECTION_STARTED: Waiting for connection to be made.
312+ </Para>
313+ </ListItem>
314+ <ListItem>
315+ <Para>
316+ CONNECTION_MADE: Connection OK; waiting to send.
317+ </Para>
318+ </ListItem>
319+ <ListItem>
320+ <Para>
321+ CONNECTION_AWAITING_RESPONSE: Waiting for a response from the backend.
322+ </Para>
323+ </ListItem>
324+ <ListItem>
325+ <Para>
326+ CONNECTION_AUTH_RESPONSE: Got an authentication response; about to deal
327+ with it.
328+ </Para>
329+ </ListItem>
330+ <ListItem>
331+ <Para>
332+ CONNECTION_ERROR_RESPONSE: Got an error response; about to deal with it.
333+ </Para>
334+ </ListItem>
335+ <ListItem>
336+ <Para>
337+ CONNECTION_AUTH_OK: Received authentication; waiting for ReadyForQuery etc.
338+ </Para>
339+ </ListItem>
340+ <ListItem>
341+ <Para>
342+ CONNECTION_SETENV: Negotiating environment.
343+ </Para>
344+ </ListItem>
345+ </ItemizedList>
346+
347+ Note that, although these constants will remain (in order to maintain
348+ compatibility) an application should never rely upon these appearing in a
349+ particular order, or at all, or on the status always being one of these
350+ documented values. An application may do something like this:
351+ <ProgramListing>
352+ switch(PQstatus(conn))
353+ {
354+ case CONNECTION_STARTED:
355+ feedback = "Connecting...";
356+ break;
357+
358+ case CONNECTION_MADE:
359+ feedback = "Connected to server...";
360+ break;
361+ .
362+ .
363+ .
364+ default:
365+ feedback = "Connecting...";
366+ }
367+ </ProgramListing>
368+ </Para>
369+ <Para>
370+ Note that if PQconnectStart returns a non-NULL pointer, you must call
371+ PQfinish upon that, when you are finished with it, in order to dispose of
372+ the structure and any associated memory blocks. This must be done even if a
373+ call to PQconnectStart or PQconnectPoll failed.
374+ </Para>
375+ <Para>
376+ PQconnectPoll will currently block if libpq is compiled with USE_SSL
377+ defined. This restriction may be removed in the future.
378+ </Para>
379+ <Para>
380+ PQconnectPoll will currently block under Windows, unless libpq is compiled
381+ with WIN32_NON_BLOCKING_CONNECTIONS defined. This code has not yet been
382+ tested under Windows, and so it is currently off by default. This may be
383+ changed in the future.
384+ </Para>
385+ <Para>
386+ These functions are not thread-safe.
387+ </Para>
388+ </ListItem>
389+
188390 <ListItem>
189391 <Para>
190392 <Function>PQconndefaults</Function> Returns the default connection options.
@@ -215,6 +417,9 @@ struct PQconninfoOption
215417 will depend on environment variables and other context.
216418 Callers must treat the connection options data as read-only.
217419 </Para>
420+ <Para>
421+ This function is not thread-safe.
422+ </Para>
218423 </ListItem>
219424
220425 <ListItem>
@@ -247,6 +452,31 @@ void PQreset(PGconn *conn)
247452 </Para>
248453 </ListItem>
249454
455+ <ListItem>
456+ <Para>
457+ <Function>PQresetStart</Function>
458+ <Function>PQresetPoll</Function>
459+ Reset the communication port with the backend, in a non-blocking manner.
460+ <synopsis>
461+ int PQresetStart(PGconn *conn);
462+ </synopsis>
463+ <synopsis>
464+ PostgresPollingStatusType PQresetPoll(PGconn *conn);
465+ </synopsis>
466+ These functions will close the connection to the backend and attempt to
467+ reestablish a new connection to the same postmaster, using all the same
468+ parameters previously used. This may be useful for error recovery if a
469+ working connection is lost. They differ from PQreset (above) in that they
470+ act in a non-blocking manner. These functions suffer from the same
471+ restrictions as PQconnectStart and PQconnectPoll.
472+ </Para>
473+ <Para>
474+ Call PQresetStart. If it returns 0, the reset has failed. If it returns 1,
475+ poll the reset using PQresetPoll in exactly the same way as you would
476+ create the connection using PQconnectPoll.
477+ </Para>
478+ </ListItem>
479+
250480 </ItemizedList>
251481</Para>
252482
@@ -338,19 +568,25 @@ const char *PQoptions(const PGconn *conn)
338568<Para>
339569<Function>PQstatus</Function>
340570 Returns the status of the connection.
341- The status can be <literal>CONNECTION_OK</literal> or <literal>CONNECTION_BAD</literal>.
342571<synopsis>
343572ConnStatusType PQstatus(const PGconn *conn)
344573</synopsis>
345574</Para>
346575
347576<Para>
348- A failed connection attempt is signaled by status <literal>CONNECTION_BAD</literal>.
349- Ordinarily, an OK status will remain so until <function>PQfinish</function>, but a
577+ The status can be one of a number of values. However, only two of these are
578+ seen outside of an asynchronous connection procedure -
579+ <literal>CONNECTION_OK</literal> or <literal>CONNECTION_BAD</literal>. A good
580+ connection to the database has the status CONNECTION_OK. A failed connection
581+ attempt is signaled by status <literal>CONNECTION_BAD</literal>. Ordinarily,
582+ an OK status will remain so until <function>PQfinish</function>, but a
350583communications failure might result in the status changing to
351- <literal>CONNECTION_BAD</literal> prematurely. In that case the application could
352- try to recover by calling <function>PQreset</function>.
584+ <literal>CONNECTION_BAD</literal> prematurely. In that case the application
585+ could try to recover by calling <function>PQreset</function>.
353586</Para>
587+ <Para>
588+ See the entry for PQconnectStart and PQconnectPoll with regards to other status codes
589+ that might be seen.
354590</ListItem>
355591
356592<ListItem>
@@ -385,6 +621,60 @@ server host, not the local host!
385621</Para>
386622</ListItem>
387623
624+ <ListItem>
625+ <Para>
626+ <Function>PQsetenvStart</Function>
627+ <Function>PQsetenvPoll</Function>
628+ <Function>PQsetenvAbort</Function>
629+ Perform an environment negotiation.
630+ <synopsis>
631+ PGsetenvHandle *PQsetenvStart(PGconn *conn)
632+ </synopsis>
633+ <synopsis>
634+ PostgresPollingStatusType *PQsetenvPoll(PGsetenvHandle handle)
635+ </synopsis>
636+ <synopsis>
637+ void PQsetenvAbort(PGsetenvHandle handle)
638+ </synopsis>
639+ These two routines can be used to re-perform the environment negotiation
640+ that occurs during the opening of a connection to a database server. I have
641+ no idea why this might be useful (XXX anyone?) but it might prove useful
642+ for users to be able to reconfigure their character encodings on-the-fly,
643+ for example.
644+ </Para>
645+ <Para>
646+ These functions will not block, subject to the restrictions applied to
647+ PQconnectStart and PQconnectPoll.
648+ </Para>
649+ <Para>
650+ To begin, call handle=PQsetenvStart(conn), where conn is an open connection
651+ to the database server. If handle is NULL, then libpq has been unable to
652+ allocate a new PGsetenvHandle structure. Otherwise, a valid handle is
653+ returned. This handle is intended to be opaque - you may only use it to
654+ call other functions in libpq (PQsetenvPoll, for example).
655+ </Para>
656+ <Para>
657+ Poll the procedure using PQsetenvPoll, in exactly the same way as you would
658+ create a connection using PQconnectPoll.
659+ </Para>
660+ <Para>
661+ The procedure may be aborted at any time by calling PQsetenvAbort(handle).
662+ </Para>
663+ <Para>
664+ These functions are not thread-safe.
665+ </Para>
666+ </ListItem>
667+
668+ <ListItem>
669+ <Para>
670+ <Function>PQsetenv</Function>
671+ Perform an environment negotiation.
672+ <synopsis>
673+ int PQsetenv(PGconn *conn)
674+ </synopsis>
675+ This function performs the same duties as PQsetenvStart and PQsetenvPoll, but
676+ blocks to do so. It returns 1 on success and 0 on failure.
677+ </Para>
388678</ItemizedList>
389679</Para>
390680</Sect1>