OSDN Git Service

Prevents query conflict from suspending a failover.
[pg-rex/syncrep.git] / src / interfaces / libpq / libpq-fe.h
1 /*-------------------------------------------------------------------------
2  *
3  * libpq-fe.h
4  *        This file contains definitions for structures and
5  *        externs for functions used by frontend postgres applications.
6  *
7  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.152 2010/02/26 02:01:33 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #ifndef LIBPQ_FE_H
16 #define LIBPQ_FE_H
17
18 #ifdef __cplusplus
19 extern          "C"
20 {
21 #endif
22
23 #include <stdio.h>
24
25 /*
26  * postgres_ext.h defines the backend's externally visible types,
27  * such as Oid.
28  */
29 #include "postgres_ext.h"
30
31 /*
32  * Option flags for PQcopyResult
33  */
34 #define PG_COPYRES_ATTRS                  0x01
35 #define PG_COPYRES_TUPLES                 0x02  /* Implies PG_COPYRES_ATTRS */
36 #define PG_COPYRES_EVENTS                 0x04
37 #define PG_COPYRES_NOTICEHOOKS    0x08
38
39 /* Application-visible enum types */
40
41 typedef enum
42 {
43         /*
44          * Although it is okay to add to this list, values which become unused
45          * should never be removed, nor should constants be redefined - that would
46          * break compatibility with existing code.
47          */
48         CONNECTION_OK,
49         CONNECTION_BAD,
50         /* Non-blocking mode only below here */
51
52         /*
53          * The existence of these should never be relied upon - they should only
54          * be used for user feedback or similar purposes.
55          */
56         CONNECTION_STARTED,                     /* Waiting for connection to be made.  */
57         CONNECTION_MADE,                        /* Connection OK; waiting to send.         */
58         CONNECTION_AWAITING_RESPONSE,           /* Waiting for a response from the
59                                                                                  * postmaster.            */
60         CONNECTION_AUTH_OK,                     /* Received authentication; waiting for
61                                                                  * backend startup. */
62         CONNECTION_SETENV,                      /* Negotiating environment. */
63         CONNECTION_SSL_STARTUP,         /* Negotiating SSL. */
64         CONNECTION_NEEDED                       /* Internal state: connect() needed */
65 } ConnStatusType;
66
67 typedef enum
68 {
69         PGRES_POLLING_FAILED = 0,
70         PGRES_POLLING_READING,          /* These two indicate that one may        */
71         PGRES_POLLING_WRITING,          /* use select before polling again.   */
72         PGRES_POLLING_OK,
73         PGRES_POLLING_ACTIVE            /* unused; keep for awhile for backwards
74                                                                  * compatibility */
75 } PostgresPollingStatusType;
76
77 typedef enum
78 {
79         PGRES_EMPTY_QUERY = 0,          /* empty query string was executed */
80         PGRES_COMMAND_OK,                       /* a query command that doesn't return
81                                                                  * anything was executed properly by the
82                                                                  * backend */
83         PGRES_TUPLES_OK,                        /* a query command that returns tuples was
84                                                                  * executed properly by the backend, PGresult
85                                                                  * contains the result tuples */
86         PGRES_COPY_OUT,                         /* Copy Out data transfer in progress */
87         PGRES_COPY_IN,                          /* Copy In data transfer in progress */
88         PGRES_BAD_RESPONSE,                     /* an unexpected response was recv'd from the
89                                                                  * backend */
90         PGRES_NONFATAL_ERROR,           /* notice or warning message */
91         PGRES_FATAL_ERROR                       /* query failed */
92 } ExecStatusType;
93
94 typedef enum
95 {
96         PQTRANS_IDLE,                           /* connection idle */
97         PQTRANS_ACTIVE,                         /* command in progress */
98         PQTRANS_INTRANS,                        /* idle, within transaction block */
99         PQTRANS_INERROR,                        /* idle, within failed transaction */
100         PQTRANS_UNKNOWN                         /* cannot determine status */
101 } PGTransactionStatusType;
102
103 typedef enum
104 {
105         PQERRORS_TERSE,                         /* single-line error messages */
106         PQERRORS_DEFAULT,                       /* recommended style */
107         PQERRORS_VERBOSE                        /* all the facts, ma'am */
108 } PGVerbosity;
109
110 /* PGconn encapsulates a connection to the backend.
111  * The contents of this struct are not supposed to be known to applications.
112  */
113 typedef struct pg_conn PGconn;
114
115 /* PGresult encapsulates the result of a query (or more precisely, of a single
116  * SQL command --- a query string given to PQsendQuery can contain multiple
117  * commands and thus return multiple PGresult objects).
118  * The contents of this struct are not supposed to be known to applications.
119  */
120 typedef struct pg_result PGresult;
121
122 /* PGcancel encapsulates the information needed to cancel a running
123  * query on an existing connection.
124  * The contents of this struct are not supposed to be known to applications.
125  */
126 typedef struct pg_cancel PGcancel;
127
128 /* PGnotify represents the occurrence of a NOTIFY message.
129  * Ideally this would be an opaque typedef, but it's so simple that it's
130  * unlikely to change.
131  * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
132  * whereas in earlier versions it was always your own backend's PID.
133  */
134 typedef struct pgNotify
135 {
136         char       *relname;            /* notification condition name */
137         int                     be_pid;                 /* process ID of notifying server process */
138         char       *extra;                      /* notification parameter */
139         /* Fields below here are private to libpq; apps should not use 'em */
140         struct pgNotify *next;          /* list link */
141 } PGnotify;
142
143 /* Function types for notice-handling callbacks */
144 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
145 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
146
147 /* Print options for PQprint() */
148 typedef char pqbool;
149
150 typedef struct _PQprintOpt
151 {
152         pqbool          header;                 /* print output field headings and row count */
153         pqbool          align;                  /* fill align the fields */
154         pqbool          standard;               /* old brain dead format */
155         pqbool          html3;                  /* output html tables */
156         pqbool          expanded;               /* expand tables */
157         pqbool          pager;                  /* use pager for output if needed */
158         char       *fieldSep;           /* field separator */
159         char       *tableOpt;           /* insert to HTML <table ...> */
160         char       *caption;            /* HTML <caption> */
161         char      **fieldName;          /* null terminated array of replacement field
162                                                                  * names */
163 } PQprintOpt;
164
165 /* ----------------
166  * Structure for the conninfo parameter definitions returned by PQconndefaults
167  * or PQconninfoParse.
168  *
169  * All fields except "val" point at static strings which must not be altered.
170  * "val" is either NULL or a malloc'd current-value string.  PQconninfoFree()
171  * will release both the val strings and the PQconninfoOption array itself.
172  * ----------------
173  */
174 typedef struct _PQconninfoOption
175 {
176         char       *keyword;            /* The keyword of the option                    */
177         char       *envvar;                     /* Fallback environment variable name   */
178         char       *compiled;           /* Fallback compiled in default value   */
179         char       *val;                        /* Option's current value, or NULL               */
180         char       *label;                      /* Label for field in connect dialog    */
181         char       *dispchar;           /* Indicates how to display this field in a
182                                                                  * connect dialog. Values are: "" Display
183                                                                  * entered value as is "*" Password field -
184                                                                  * hide value "D"  Debug option - don't show
185                                                                  * by default */
186         int                     dispsize;               /* Field size in characters for dialog  */
187 } PQconninfoOption;
188
189 /* ----------------
190  * PQArgBlock -- structure for PQfn() arguments
191  * ----------------
192  */
193 typedef struct
194 {
195         int                     len;
196         int                     isint;
197         union
198         {
199                 int                *ptr;                /* can't use void (dec compiler barfs)   */
200                 int                     integer;
201         }                       u;
202 } PQArgBlock;
203
204 /* ----------------
205  * PGresAttDesc -- Data about a single attribute (column) of a query result
206  * ----------------
207  */
208 typedef struct pgresAttDesc
209 {
210         char       *name;                       /* column name */
211         Oid                     tableid;                /* source table, if known */
212         int                     columnid;               /* source column, if known */
213         int                     format;                 /* format code for value (text/binary) */
214         Oid                     typid;                  /* type id */
215         int                     typlen;                 /* type size */
216         int                     atttypmod;              /* type-specific modifier info */
217 } PGresAttDesc;
218
219 /* ----------------
220  * Exported functions of libpq
221  * ----------------
222  */
223
224 /* ===  in fe-connect.c === */
225
226 /* make a new client connection to the backend */
227 /* Asynchronous (non-blocking) */
228 extern PGconn *PQconnectStart(const char *conninfo);
229 extern PGconn *PQconnectStartParams(const char **keywords,
230                                          const char **values, int expand_dbname);
231 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
232
233 /* Synchronous (blocking) */
234 extern PGconn *PQconnectdb(const char *conninfo);
235 extern PGconn *PQconnectdbParams(const char **keywords,
236                                   const char **values, int expand_dbname);
237 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
238                          const char *pgoptions, const char *pgtty,
239                          const char *dbName,
240                          const char *login, const char *pwd);
241
242 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME)  \
243         PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
244
245 /* close the current connection and free the PGconn data structure */
246 extern void PQfinish(PGconn *conn);
247
248 /* get info about connection options known to PQconnectdb */
249 extern PQconninfoOption *PQconndefaults(void);
250
251 /* parse connection options in same way as PQconnectdb */
252 extern PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
253
254 /* free the data structure returned by PQconndefaults() or PQconninfoParse() */
255 extern void PQconninfoFree(PQconninfoOption *connOptions);
256
257 /*
258  * close the current connection and restablish a new one with the same
259  * parameters
260  */
261 /* Asynchronous (non-blocking) */
262 extern int      PQresetStart(PGconn *conn);
263 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
264
265 /* Synchronous (blocking) */
266 extern void PQreset(PGconn *conn);
267
268 /* request a cancel structure */
269 extern PGcancel *PQgetCancel(PGconn *conn);
270
271 /* free a cancel structure */
272 extern void PQfreeCancel(PGcancel *cancel);
273
274 /* issue a cancel request */
275 extern int      PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
276
277 /* backwards compatible version of PQcancel; not thread-safe */
278 extern int      PQrequestCancel(PGconn *conn);
279
280 /* Accessor functions for PGconn objects */
281 extern char *PQdb(const PGconn *conn);
282 extern char *PQuser(const PGconn *conn);
283 extern char *PQpass(const PGconn *conn);
284 extern char *PQhost(const PGconn *conn);
285 extern char *PQport(const PGconn *conn);
286 extern char *PQtty(const PGconn *conn);
287 extern char *PQoptions(const PGconn *conn);
288 extern ConnStatusType PQstatus(const PGconn *conn);
289 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
290 extern const char *PQparameterStatus(const PGconn *conn,
291                                   const char *paramName);
292 extern int      PQprotocolVersion(const PGconn *conn);
293 extern int      PQserverVersion(const PGconn *conn);
294 extern char *PQerrorMessage(const PGconn *conn);
295 extern int      PQsocket(const PGconn *conn);
296 extern int      PQbackendPID(const PGconn *conn);
297 extern int      PQconnectionNeedsPassword(const PGconn *conn);
298 extern int      PQconnectionUsedPassword(const PGconn *conn);
299 extern int      PQclientEncoding(const PGconn *conn);
300 extern int      PQsetClientEncoding(PGconn *conn, const char *encoding);
301
302 /* Get the OpenSSL structure associated with a connection. Returns NULL for
303  * unencrypted connections or if any other TLS library is in use. */
304 extern void *PQgetssl(PGconn *conn);
305
306 /* Tell libpq whether it needs to initialize OpenSSL */
307 extern void PQinitSSL(int do_init);
308
309 /* More detailed way to tell libpq whether it needs to initialize OpenSSL */
310 extern void PQinitOpenSSL(int do_ssl, int do_crypto);
311
312 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
313 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
314
315 /* Enable/disable tracing */
316 extern void PQtrace(PGconn *conn, FILE *debug_port);
317 extern void PQuntrace(PGconn *conn);
318
319 /* Override default notice handling routines */
320 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
321                                         PQnoticeReceiver proc,
322                                         void *arg);
323 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
324                                          PQnoticeProcessor proc,
325                                          void *arg);
326
327 /*
328  *         Used to set callback that prevents concurrent access to
329  *         non-thread safe functions that libpq needs.
330  *         The default implementation uses a libpq internal mutex.
331  *         Only required for multithreaded apps that use kerberos
332  *         both within their app and for postgresql connections.
333  */
334 typedef void (*pgthreadlock_t) (int acquire);
335
336 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
337
338 /* === in fe-exec.c === */
339
340 /* Simple synchronous query */
341 extern PGresult *PQexec(PGconn *conn, const char *query);
342 extern PGresult *PQexecParams(PGconn *conn,
343                          const char *command,
344                          int nParams,
345                          const Oid *paramTypes,
346                          const char *const * paramValues,
347                          const int *paramLengths,
348                          const int *paramFormats,
349                          int resultFormat);
350 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
351                   const char *query, int nParams,
352                   const Oid *paramTypes);
353 extern PGresult *PQexecPrepared(PGconn *conn,
354                            const char *stmtName,
355                            int nParams,
356                            const char *const * paramValues,
357                            const int *paramLengths,
358                            const int *paramFormats,
359                            int resultFormat);
360
361 /* Interface for multiple-result or asynchronous queries */
362 extern int      PQsendQuery(PGconn *conn, const char *query);
363 extern int PQsendQueryParams(PGconn *conn,
364                                   const char *command,
365                                   int nParams,
366                                   const Oid *paramTypes,
367                                   const char *const * paramValues,
368                                   const int *paramLengths,
369                                   const int *paramFormats,
370                                   int resultFormat);
371 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
372                           const char *query, int nParams,
373                           const Oid *paramTypes);
374 extern int PQsendQueryPrepared(PGconn *conn,
375                                         const char *stmtName,
376                                         int nParams,
377                                         const char *const * paramValues,
378                                         const int *paramLengths,
379                                         const int *paramFormats,
380                                         int resultFormat);
381 extern PGresult *PQgetResult(PGconn *conn);
382
383 /* Routines for managing an asynchronous query */
384 extern int      PQisBusy(PGconn *conn);
385 extern int      PQconsumeInput(PGconn *conn);
386
387 /* LISTEN/NOTIFY support */
388 extern PGnotify *PQnotifies(PGconn *conn);
389
390 /* Routines for copy in/out */
391 extern int      PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
392 extern int      PQputCopyEnd(PGconn *conn, const char *errormsg);
393 extern int      PQgetCopyData(PGconn *conn, char **buffer, int async);
394
395 /* Deprecated routines for copy in/out */
396 extern int      PQgetline(PGconn *conn, char *string, int length);
397 extern int      PQputline(PGconn *conn, const char *string);
398 extern int      PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
399 extern int      PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
400 extern int      PQendcopy(PGconn *conn);
401
402 /* Set blocking/nonblocking connection to the backend */
403 extern int      PQsetnonblocking(PGconn *conn, int arg);
404 extern int      PQisnonblocking(const PGconn *conn);
405 extern int      PQisthreadsafe(void);
406
407 /* Force the write buffer to be written (or at least try) */
408 extern int      PQflush(PGconn *conn);
409
410 /*
411  * "Fast path" interface --- not really recommended for application
412  * use
413  */
414 extern PGresult *PQfn(PGconn *conn,
415          int fnid,
416          int *result_buf,
417          int *result_len,
418          int result_is_int,
419          const PQArgBlock *args,
420          int nargs);
421
422 /* Accessor functions for PGresult objects */
423 extern ExecStatusType PQresultStatus(const PGresult *res);
424 extern char *PQresStatus(ExecStatusType status);
425 extern char *PQresultErrorMessage(const PGresult *res);
426 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
427 extern int      PQntuples(const PGresult *res);
428 extern int      PQnfields(const PGresult *res);
429 extern int      PQbinaryTuples(const PGresult *res);
430 extern char *PQfname(const PGresult *res, int field_num);
431 extern int      PQfnumber(const PGresult *res, const char *field_name);
432 extern Oid      PQftable(const PGresult *res, int field_num);
433 extern int      PQftablecol(const PGresult *res, int field_num);
434 extern int      PQfformat(const PGresult *res, int field_num);
435 extern Oid      PQftype(const PGresult *res, int field_num);
436 extern int      PQfsize(const PGresult *res, int field_num);
437 extern int      PQfmod(const PGresult *res, int field_num);
438 extern char *PQcmdStatus(PGresult *res);
439 extern char *PQoidStatus(const PGresult *res);  /* old and ugly */
440 extern Oid      PQoidValue(const PGresult *res);        /* new and improved */
441 extern char *PQcmdTuples(PGresult *res);
442 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
443 extern int      PQgetlength(const PGresult *res, int tup_num, int field_num);
444 extern int      PQgetisnull(const PGresult *res, int tup_num, int field_num);
445 extern int      PQnparams(const PGresult *res);
446 extern Oid      PQparamtype(const PGresult *res, int param_num);
447
448 /* Describe prepared statements and portals */
449 extern PGresult *PQdescribePrepared(PGconn *conn, const char *stmt);
450 extern PGresult *PQdescribePortal(PGconn *conn, const char *portal);
451 extern int      PQsendDescribePrepared(PGconn *conn, const char *stmt);
452 extern int      PQsendDescribePortal(PGconn *conn, const char *portal);
453
454 /* Delete a PGresult */
455 extern void PQclear(PGresult *res);
456
457 /* For freeing other alloc'd results, such as PGnotify structs */
458 extern void PQfreemem(void *ptr);
459
460 /* Exists for backward compatibility.  bjm 2003-03-24 */
461 #define PQfreeNotify(ptr) PQfreemem(ptr)
462
463 /* Error when no password was given. */
464 /* Note: depending on this is deprecated; use PQconnectionNeedsPassword(). */
465 #define PQnoPasswordSupplied    "fe_sendauth: no password supplied\n"
466
467 /* Create and manipulate PGresults */
468 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
469 extern PGresult *PQcopyResult(const PGresult *src, int flags);
470 extern int      PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
471 extern void *PQresultAlloc(PGresult *res, size_t nBytes);
472 extern int      PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
473
474 /* Quoting strings before inclusion in queries. */
475 extern size_t PQescapeStringConn(PGconn *conn,
476                                    char *to, const char *from, size_t length,
477                                    int *error);
478 extern char *PQescapeLiteral(PGconn *conn, const char *str, size_t len);
479 extern char *PQescapeIdentifier(PGconn *conn, const char *str, size_t len);
480 extern unsigned char *PQescapeByteaConn(PGconn *conn,
481                                   const unsigned char *from, size_t from_length,
482                                   size_t *to_length);
483 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
484                                 size_t *retbuflen);
485
486 /* These forms are deprecated! */
487 extern size_t PQescapeString(char *to, const char *from, size_t length);
488 extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
489                           size_t *to_length);
490
491
492
493 /* === in fe-print.c === */
494
495 extern void
496 PQprint(FILE *fout,                             /* output stream */
497                 const PGresult *res,
498                 const PQprintOpt *ps);  /* option structure */
499
500 /*
501  * really old printing routines
502  */
503 extern void
504 PQdisplayTuples(const PGresult *res,
505                                 FILE *fp,               /* where to send the output */
506                                 int fillAlign,  /* pad the fields with spaces */
507                                 const char *fieldSep,   /* field separator */
508                                 int printHeader,        /* display headers? */
509                                 int quiet);
510
511 extern void
512 PQprintTuples(const PGresult *res,
513                           FILE *fout,           /* output stream */
514                           int printAttName, /* print attribute names */
515                           int terseOutput,      /* delimiter bars */
516                           int width);           /* width of column, if 0, use variable width */
517
518
519 /* === in fe-lobj.c === */
520
521 /* Large-object access routines */
522 extern int      lo_open(PGconn *conn, Oid lobjId, int mode);
523 extern int      lo_close(PGconn *conn, int fd);
524 extern int      lo_read(PGconn *conn, int fd, char *buf, size_t len);
525 extern int      lo_write(PGconn *conn, int fd, const char *buf, size_t len);
526 extern int      lo_lseek(PGconn *conn, int fd, int offset, int whence);
527 extern Oid      lo_creat(PGconn *conn, int mode);
528 extern Oid      lo_create(PGconn *conn, Oid lobjId);
529 extern int      lo_tell(PGconn *conn, int fd);
530 extern int      lo_truncate(PGconn *conn, int fd, size_t len);
531 extern int      lo_unlink(PGconn *conn, Oid lobjId);
532 extern Oid      lo_import(PGconn *conn, const char *filename);
533 extern Oid      lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
534 extern int      lo_export(PGconn *conn, Oid lobjId, const char *filename);
535
536 /* === in fe-misc.c === */
537
538 /* Determine length of multibyte encoded char at *s */
539 extern int      PQmblen(const char *s, int encoding);
540
541 /* Determine display length of multibyte encoded char at *s */
542 extern int      PQdsplen(const char *s, int encoding);
543
544 /* Get encoding id from environment variable PGCLIENTENCODING */
545 extern int      PQenv2encoding(void);
546
547 /* === in fe-auth.c === */
548
549 extern char *PQencryptPassword(const char *passwd, const char *user);
550
551 /* === in encnames.c === */
552
553 extern int      pg_char_to_encoding(const char *name);
554 extern const char *pg_encoding_to_char(int encoding);
555 extern int      pg_valid_server_encoding_id(int encoding);
556
557 #ifdef __cplusplus
558 }
559 #endif
560
561 #endif   /* LIBPQ_FE_H */