1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.152 2010/02/26 02:01:33 momjian Exp $
12 *-------------------------------------------------------------------------
26 * postgres_ext.h defines the backend's externally visible types,
29 #include "postgres_ext.h"
32 * Option flags for PQcopyResult
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
39 /* Application-visible enum types */
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.
50 /* Non-blocking mode only below here */
53 * The existence of these should never be relied upon - they should only
54 * be used for user feedback or similar purposes.
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
60 CONNECTION_AUTH_OK, /* Received authentication; waiting for
62 CONNECTION_SETENV, /* Negotiating environment. */
63 CONNECTION_SSL_STARTUP, /* Negotiating SSL. */
64 CONNECTION_NEEDED /* Internal state: connect() needed */
69 PGRES_POLLING_FAILED = 0,
70 PGRES_POLLING_READING, /* These two indicate that one may */
71 PGRES_POLLING_WRITING, /* use select before polling again. */
73 PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
75 } PostgresPollingStatusType;
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
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
90 PGRES_NONFATAL_ERROR, /* notice or warning message */
91 PGRES_FATAL_ERROR /* query failed */
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;
105 PQERRORS_TERSE, /* single-line error messages */
106 PQERRORS_DEFAULT, /* recommended style */
107 PQERRORS_VERBOSE /* all the facts, ma'am */
110 /* PGconn encapsulates a connection to the backend.
111 * The contents of this struct are not supposed to be known to applications.
113 typedef struct pg_conn PGconn;
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.
120 typedef struct pg_result PGresult;
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.
126 typedef struct pg_cancel PGcancel;
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.
134 typedef struct pgNotify
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 */
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);
147 /* Print options for PQprint() */
150 typedef struct _PQprintOpt
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
166 * Structure for the conninfo parameter definitions returned by PQconndefaults
167 * or PQconninfoParse.
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.
174 typedef struct _PQconninfoOption
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
186 int dispsize; /* Field size in characters for dialog */
190 * PQArgBlock -- structure for PQfn() arguments
199 int *ptr; /* can't use void (dec compiler barfs) */
205 * PGresAttDesc -- Data about a single attribute (column) of a query result
208 typedef struct pgresAttDesc
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 */
220 * Exported functions of libpq
224 /* === in fe-connect.c === */
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);
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,
240 const char *login, const char *pwd);
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)
245 /* close the current connection and free the PGconn data structure */
246 extern void PQfinish(PGconn *conn);
248 /* get info about connection options known to PQconnectdb */
249 extern PQconninfoOption *PQconndefaults(void);
251 /* parse connection options in same way as PQconnectdb */
252 extern PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
254 /* free the data structure returned by PQconndefaults() or PQconninfoParse() */
255 extern void PQconninfoFree(PQconninfoOption *connOptions);
258 * close the current connection and restablish a new one with the same
261 /* Asynchronous (non-blocking) */
262 extern int PQresetStart(PGconn *conn);
263 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
265 /* Synchronous (blocking) */
266 extern void PQreset(PGconn *conn);
268 /* request a cancel structure */
269 extern PGcancel *PQgetCancel(PGconn *conn);
271 /* free a cancel structure */
272 extern void PQfreeCancel(PGcancel *cancel);
274 /* issue a cancel request */
275 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
277 /* backwards compatible version of PQcancel; not thread-safe */
278 extern int PQrequestCancel(PGconn *conn);
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);
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);
306 /* Tell libpq whether it needs to initialize OpenSSL */
307 extern void PQinitSSL(int do_init);
309 /* More detailed way to tell libpq whether it needs to initialize OpenSSL */
310 extern void PQinitOpenSSL(int do_ssl, int do_crypto);
312 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
313 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
315 /* Enable/disable tracing */
316 extern void PQtrace(PGconn *conn, FILE *debug_port);
317 extern void PQuntrace(PGconn *conn);
319 /* Override default notice handling routines */
320 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
321 PQnoticeReceiver proc,
323 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
324 PQnoticeProcessor proc,
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.
334 typedef void (*pgthreadlock_t) (int acquire);
336 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
338 /* === in fe-exec.c === */
340 /* Simple synchronous query */
341 extern PGresult *PQexec(PGconn *conn, const char *query);
342 extern PGresult *PQexecParams(PGconn *conn,
345 const Oid *paramTypes,
346 const char *const * paramValues,
347 const int *paramLengths,
348 const int *paramFormats,
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,
356 const char *const * paramValues,
357 const int *paramLengths,
358 const int *paramFormats,
361 /* Interface for multiple-result or asynchronous queries */
362 extern int PQsendQuery(PGconn *conn, const char *query);
363 extern int PQsendQueryParams(PGconn *conn,
366 const Oid *paramTypes,
367 const char *const * paramValues,
368 const int *paramLengths,
369 const int *paramFormats,
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,
377 const char *const * paramValues,
378 const int *paramLengths,
379 const int *paramFormats,
381 extern PGresult *PQgetResult(PGconn *conn);
383 /* Routines for managing an asynchronous query */
384 extern int PQisBusy(PGconn *conn);
385 extern int PQconsumeInput(PGconn *conn);
387 /* LISTEN/NOTIFY support */
388 extern PGnotify *PQnotifies(PGconn *conn);
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);
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);
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);
407 /* Force the write buffer to be written (or at least try) */
408 extern int PQflush(PGconn *conn);
411 * "Fast path" interface --- not really recommended for application
414 extern PGresult *PQfn(PGconn *conn,
419 const PQArgBlock *args,
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);
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);
454 /* Delete a PGresult */
455 extern void PQclear(PGresult *res);
457 /* For freeing other alloc'd results, such as PGnotify structs */
458 extern void PQfreemem(void *ptr);
460 /* Exists for backward compatibility. bjm 2003-03-24 */
461 #define PQfreeNotify(ptr) PQfreemem(ptr)
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"
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);
474 /* Quoting strings before inclusion in queries. */
475 extern size_t PQescapeStringConn(PGconn *conn,
476 char *to, const char *from, size_t length,
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,
483 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
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,
493 /* === in fe-print.c === */
496 PQprint(FILE *fout, /* output stream */
498 const PQprintOpt *ps); /* option structure */
501 * really old printing routines
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? */
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 */
519 /* === in fe-lobj.c === */
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);
536 /* === in fe-misc.c === */
538 /* Determine length of multibyte encoded char at *s */
539 extern int PQmblen(const char *s, int encoding);
541 /* Determine display length of multibyte encoded char at *s */
542 extern int PQdsplen(const char *s, int encoding);
544 /* Get encoding id from environment variable PGCLIENTENCODING */
545 extern int PQenv2encoding(void);
547 /* === in fe-auth.c === */
549 extern char *PQencryptPassword(const char *passwd, const char *user);
551 /* === in encnames.c === */
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);
561 #endif /* LIBPQ_FE_H */