1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/interfaces/libpq/libpq-fe.h
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 */
42 * Although it is okay to add to these lists, values which become unused
43 * should never be removed, nor should constants be redefined - that would
44 * break compatibility with existing code.
51 /* Non-blocking mode only below here */
54 * The existence of these should never be relied upon - they should only
55 * be used for user feedback or similar purposes.
57 CONNECTION_STARTED, /* Waiting for connection to be made. */
58 CONNECTION_MADE, /* Connection OK; waiting to send. */
59 CONNECTION_AWAITING_RESPONSE, /* Waiting for a response from the
61 CONNECTION_AUTH_OK, /* Received authentication; waiting for
63 CONNECTION_SETENV, /* Negotiating environment. */
64 CONNECTION_SSL_STARTUP, /* Negotiating SSL. */
65 CONNECTION_NEEDED /* Internal state: connect() needed */
70 PGRES_POLLING_FAILED = 0,
71 PGRES_POLLING_READING, /* These two indicate that one may */
72 PGRES_POLLING_WRITING, /* use select before polling again. */
74 PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
76 } PostgresPollingStatusType;
80 PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
81 PGRES_COMMAND_OK, /* a query command that doesn't return
82 * anything was executed properly by the
84 PGRES_TUPLES_OK, /* a query command that returns tuples was
85 * executed properly by the backend, PGresult
86 * contains the result tuples */
87 PGRES_COPY_OUT, /* Copy Out data transfer in progress */
88 PGRES_COPY_IN, /* Copy In data transfer in progress */
89 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the
91 PGRES_NONFATAL_ERROR, /* notice or warning message */
92 PGRES_FATAL_ERROR, /* query failed */
93 PGRES_COPY_BOTH, /* Copy In/Out data transfer in progress */
94 PGRES_SINGLE_TUPLE /* single tuple from larger resultset */
99 PQTRANS_IDLE, /* connection idle */
100 PQTRANS_ACTIVE, /* command in progress */
101 PQTRANS_INTRANS, /* idle, within transaction block */
102 PQTRANS_INERROR, /* idle, within failed transaction */
103 PQTRANS_UNKNOWN /* cannot determine status */
104 } PGTransactionStatusType;
108 PQERRORS_TERSE, /* single-line error messages */
109 PQERRORS_DEFAULT, /* recommended style */
110 PQERRORS_VERBOSE /* all the facts, ma'am */
115 PQPING_OK, /* server is accepting connections */
116 PQPING_REJECT, /* server is alive but rejecting connections */
117 PQPING_NO_RESPONSE, /* could not establish connection */
118 PQPING_NO_ATTEMPT /* connection not attempted (bad params) */
121 /* PGconn encapsulates a connection to the backend.
122 * The contents of this struct are not supposed to be known to applications.
124 typedef struct pg_conn PGconn;
126 /* PGresult encapsulates the result of a query (or more precisely, of a single
127 * SQL command --- a query string given to PQsendQuery can contain multiple
128 * commands and thus return multiple PGresult objects).
129 * The contents of this struct are not supposed to be known to applications.
131 typedef struct pg_result PGresult;
133 /* PGcancel encapsulates the information needed to cancel a running
134 * query on an existing connection.
135 * The contents of this struct are not supposed to be known to applications.
137 typedef struct pg_cancel PGcancel;
139 /* PGnotify represents the occurrence of a NOTIFY message.
140 * Ideally this would be an opaque typedef, but it's so simple that it's
141 * unlikely to change.
142 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
143 * whereas in earlier versions it was always your own backend's PID.
145 typedef struct pgNotify
147 char *relname; /* notification condition name */
148 int be_pid; /* process ID of notifying server process */
149 char *extra; /* notification parameter */
150 /* Fields below here are private to libpq; apps should not use 'em */
151 struct pgNotify *next; /* list link */
154 /* Function types for notice-handling callbacks */
155 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
156 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
158 /* Print options for PQprint() */
161 typedef struct _PQprintOpt
163 pqbool header; /* print output field headings and row count */
164 pqbool align; /* fill align the fields */
165 pqbool standard; /* old brain dead format */
166 pqbool html3; /* output html tables */
167 pqbool expanded; /* expand tables */
168 pqbool pager; /* use pager for output if needed */
169 char *fieldSep; /* field separator */
170 char *tableOpt; /* insert to HTML <table ...> */
171 char *caption; /* HTML <caption> */
172 char **fieldName; /* null terminated array of replacement field
177 * Structure for the conninfo parameter definitions returned by PQconndefaults
178 * or PQconninfoParse.
180 * All fields except "val" point at static strings which must not be altered.
181 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
182 * will release both the val strings and the PQconninfoOption array itself.
185 typedef struct _PQconninfoOption
187 char *keyword; /* The keyword of the option */
188 char *envvar; /* Fallback environment variable name */
189 char *compiled; /* Fallback compiled in default value */
190 char *val; /* Option's current value, or NULL */
191 char *label; /* Label for field in connect dialog */
192 char *dispchar; /* Indicates how to display this field in a
193 * connect dialog. Values are: "" Display
194 * entered value as is "*" Password field -
195 * hide value "D" Debug option - don't show
197 int dispsize; /* Field size in characters for dialog */
201 * PQArgBlock -- structure for PQfn() arguments
210 int *ptr; /* can't use void (dec compiler barfs) */
216 * PGresAttDesc -- Data about a single attribute (column) of a query result
219 typedef struct pgresAttDesc
221 char *name; /* column name */
222 Oid tableid; /* source table, if known */
223 int columnid; /* source column, if known */
224 int format; /* format code for value (text/binary) */
225 Oid typid; /* type id */
226 int typlen; /* type size */
227 int atttypmod; /* type-specific modifier info */
231 * Exported functions of libpq
235 /* === in fe-connect.c === */
237 /* make a new client connection to the backend */
238 /* Asynchronous (non-blocking) */
239 extern PGconn *PQconnectStart(const char *conninfo);
240 extern PGconn *PQconnectStartParams(const char *const * keywords,
241 const char *const * values, int expand_dbname);
242 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
244 /* Synchronous (blocking) */
245 extern PGconn *PQconnectdb(const char *conninfo);
246 extern PGconn *PQconnectdbParams(const char *const * keywords,
247 const char *const * values, int expand_dbname);
248 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
249 const char *pgoptions, const char *pgtty,
251 const char *login, const char *pwd);
253 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
254 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
256 /* close the current connection and free the PGconn data structure */
257 extern void PQfinish(PGconn *conn);
259 /* get info about connection options known to PQconnectdb */
260 extern PQconninfoOption *PQconndefaults(void);
262 /* parse connection options in same way as PQconnectdb */
263 extern PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
265 /* free the data structure returned by PQconndefaults() or PQconninfoParse() */
266 extern void PQconninfoFree(PQconninfoOption *connOptions);
269 * close the current connection and restablish a new one with the same
272 /* Asynchronous (non-blocking) */
273 extern int PQresetStart(PGconn *conn);
274 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
276 /* Synchronous (blocking) */
277 extern void PQreset(PGconn *conn);
279 /* request a cancel structure */
280 extern PGcancel *PQgetCancel(PGconn *conn);
282 /* free a cancel structure */
283 extern void PQfreeCancel(PGcancel *cancel);
285 /* issue a cancel request */
286 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
288 /* backwards compatible version of PQcancel; not thread-safe */
289 extern int PQrequestCancel(PGconn *conn);
291 /* Accessor functions for PGconn objects */
292 extern char *PQdb(const PGconn *conn);
293 extern char *PQuser(const PGconn *conn);
294 extern char *PQpass(const PGconn *conn);
295 extern char *PQhost(const PGconn *conn);
296 extern char *PQport(const PGconn *conn);
297 extern char *PQtty(const PGconn *conn);
298 extern char *PQoptions(const PGconn *conn);
299 extern ConnStatusType PQstatus(const PGconn *conn);
300 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
301 extern const char *PQparameterStatus(const PGconn *conn,
302 const char *paramName);
303 extern int PQprotocolVersion(const PGconn *conn);
304 extern int PQserverVersion(const PGconn *conn);
305 extern char *PQerrorMessage(const PGconn *conn);
306 extern int PQsocket(const PGconn *conn);
307 extern int PQbackendPID(const PGconn *conn);
308 extern int PQconnectionNeedsPassword(const PGconn *conn);
309 extern int PQconnectionUsedPassword(const PGconn *conn);
310 extern int PQclientEncoding(const PGconn *conn);
311 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
313 /* Get the OpenSSL structure associated with a connection. Returns NULL for
314 * unencrypted connections or if any other TLS library is in use. */
315 extern void *PQgetssl(PGconn *conn);
317 /* Tell libpq whether it needs to initialize OpenSSL */
318 extern void PQinitSSL(int do_init);
320 /* More detailed way to tell libpq whether it needs to initialize OpenSSL */
321 extern void PQinitOpenSSL(int do_ssl, int do_crypto);
323 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
324 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
326 /* Enable/disable tracing */
327 extern void PQtrace(PGconn *conn, FILE *debug_port);
328 extern void PQuntrace(PGconn *conn);
330 /* Override default notice handling routines */
331 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
332 PQnoticeReceiver proc,
334 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
335 PQnoticeProcessor proc,
339 * Used to set callback that prevents concurrent access to
340 * non-thread safe functions that libpq needs.
341 * The default implementation uses a libpq internal mutex.
342 * Only required for multithreaded apps that use kerberos
343 * both within their app and for postgresql connections.
345 typedef void (*pgthreadlock_t) (int acquire);
347 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
349 /* === in fe-exec.c === */
351 /* Simple synchronous query */
352 extern PGresult *PQexec(PGconn *conn, const char *query);
353 extern PGresult *PQexecParams(PGconn *conn,
356 const Oid *paramTypes,
357 const char *const * paramValues,
358 const int *paramLengths,
359 const int *paramFormats,
361 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
362 const char *query, int nParams,
363 const Oid *paramTypes);
364 extern PGresult *PQexecPrepared(PGconn *conn,
365 const char *stmtName,
367 const char *const * paramValues,
368 const int *paramLengths,
369 const int *paramFormats,
372 /* Interface for multiple-result or asynchronous queries */
373 extern int PQsendQuery(PGconn *conn, const char *query);
374 extern int PQsendQueryParams(PGconn *conn,
377 const Oid *paramTypes,
378 const char *const * paramValues,
379 const int *paramLengths,
380 const int *paramFormats,
382 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
383 const char *query, int nParams,
384 const Oid *paramTypes);
385 extern int PQsendQueryPrepared(PGconn *conn,
386 const char *stmtName,
388 const char *const * paramValues,
389 const int *paramLengths,
390 const int *paramFormats,
392 extern int PQsetSingleRowMode(PGconn *conn);
393 extern PGresult *PQgetResult(PGconn *conn);
395 /* Routines for managing an asynchronous query */
396 extern int PQisBusy(PGconn *conn);
397 extern int PQconsumeInput(PGconn *conn);
399 /* LISTEN/NOTIFY support */
400 extern PGnotify *PQnotifies(PGconn *conn);
402 /* Routines for copy in/out */
403 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
404 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
405 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
407 /* Deprecated routines for copy in/out */
408 extern int PQgetline(PGconn *conn, char *string, int length);
409 extern int PQputline(PGconn *conn, const char *string);
410 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
411 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
412 extern int PQendcopy(PGconn *conn);
414 /* Set blocking/nonblocking connection to the backend */
415 extern int PQsetnonblocking(PGconn *conn, int arg);
416 extern int PQisnonblocking(const PGconn *conn);
417 extern int PQisthreadsafe(void);
418 extern PGPing PQping(const char *conninfo);
419 extern PGPing PQpingParams(const char *const * keywords,
420 const char *const * values, int expand_dbname);
422 /* Force the write buffer to be written (or at least try) */
423 extern int PQflush(PGconn *conn);
426 * "Fast path" interface --- not really recommended for application
429 extern PGresult *PQfn(PGconn *conn,
434 const PQArgBlock *args,
437 /* Accessor functions for PGresult objects */
438 extern ExecStatusType PQresultStatus(const PGresult *res);
439 extern char *PQresStatus(ExecStatusType status);
440 extern char *PQresultErrorMessage(const PGresult *res);
441 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
442 extern int PQntuples(const PGresult *res);
443 extern int PQnfields(const PGresult *res);
444 extern int PQbinaryTuples(const PGresult *res);
445 extern char *PQfname(const PGresult *res, int field_num);
446 extern int PQfnumber(const PGresult *res, const char *field_name);
447 extern Oid PQftable(const PGresult *res, int field_num);
448 extern int PQftablecol(const PGresult *res, int field_num);
449 extern int PQfformat(const PGresult *res, int field_num);
450 extern Oid PQftype(const PGresult *res, int field_num);
451 extern int PQfsize(const PGresult *res, int field_num);
452 extern int PQfmod(const PGresult *res, int field_num);
453 extern char *PQcmdStatus(PGresult *res);
454 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
455 extern Oid PQoidValue(const PGresult *res); /* new and improved */
456 extern char *PQcmdTuples(PGresult *res);
457 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
458 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
459 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
460 extern int PQnparams(const PGresult *res);
461 extern Oid PQparamtype(const PGresult *res, int param_num);
463 /* Describe prepared statements and portals */
464 extern PGresult *PQdescribePrepared(PGconn *conn, const char *stmt);
465 extern PGresult *PQdescribePortal(PGconn *conn, const char *portal);
466 extern int PQsendDescribePrepared(PGconn *conn, const char *stmt);
467 extern int PQsendDescribePortal(PGconn *conn, const char *portal);
469 /* Delete a PGresult */
470 extern void PQclear(PGresult *res);
472 /* For freeing other alloc'd results, such as PGnotify structs */
473 extern void PQfreemem(void *ptr);
475 /* Exists for backward compatibility. bjm 2003-03-24 */
476 #define PQfreeNotify(ptr) PQfreemem(ptr)
478 /* Error when no password was given. */
479 /* Note: depending on this is deprecated; use PQconnectionNeedsPassword(). */
480 #define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
482 /* Create and manipulate PGresults */
483 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
484 extern PGresult *PQcopyResult(const PGresult *src, int flags);
485 extern int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
486 extern void *PQresultAlloc(PGresult *res, size_t nBytes);
487 extern int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
489 /* Quoting strings before inclusion in queries. */
490 extern size_t PQescapeStringConn(PGconn *conn,
491 char *to, const char *from, size_t length,
493 extern char *PQescapeLiteral(PGconn *conn, const char *str, size_t len);
494 extern char *PQescapeIdentifier(PGconn *conn, const char *str, size_t len);
495 extern unsigned char *PQescapeByteaConn(PGconn *conn,
496 const unsigned char *from, size_t from_length,
498 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
501 /* These forms are deprecated! */
502 extern size_t PQescapeString(char *to, const char *from, size_t length);
503 extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
508 /* === in fe-print.c === */
510 extern void PQprint(FILE *fout, /* output stream */
512 const PQprintOpt *ps); /* option structure */
515 * really old printing routines
517 extern void PQdisplayTuples(const PGresult *res,
518 FILE *fp, /* where to send the output */
519 int fillAlign, /* pad the fields with spaces */
520 const char *fieldSep, /* field separator */
521 int printHeader, /* display headers? */
524 extern void PQprintTuples(const PGresult *res,
525 FILE *fout, /* output stream */
526 int printAttName, /* print attribute names */
527 int terseOutput, /* delimiter bars */
528 int width); /* width of column, if 0, use variable width */
531 /* === in fe-lobj.c === */
533 /* Large-object access routines */
534 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
535 extern int lo_close(PGconn *conn, int fd);
536 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
537 extern int lo_write(PGconn *conn, int fd, const char *buf, size_t len);
538 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
539 extern pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence);
540 extern Oid lo_creat(PGconn *conn, int mode);
541 extern Oid lo_create(PGconn *conn, Oid lobjId);
542 extern int lo_tell(PGconn *conn, int fd);
543 extern pg_int64 lo_tell64(PGconn *conn, int fd);
544 extern int lo_truncate(PGconn *conn, int fd, size_t len);
545 extern int lo_truncate64(PGconn *conn, int fd, pg_int64 len);
546 extern int lo_unlink(PGconn *conn, Oid lobjId);
547 extern Oid lo_import(PGconn *conn, const char *filename);
548 extern Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
549 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
551 /* === in fe-misc.c === */
553 /* Get the version of the libpq library in use */
554 extern int PQlibVersion(void);
556 /* Determine length of multibyte encoded char at *s */
557 extern int PQmblen(const char *s, int encoding);
559 /* Determine display length of multibyte encoded char at *s */
560 extern int PQdsplen(const char *s, int encoding);
562 /* Get encoding id from environment variable PGCLIENTENCODING */
563 extern int PQenv2encoding(void);
565 /* === in fe-auth.c === */
567 extern char *PQencryptPassword(const char *passwd, const char *user);
569 /* === in encnames.c === */
571 extern int pg_char_to_encoding(const char *name);
572 extern const char *pg_encoding_to_char(int encoding);
573 extern int pg_valid_server_encoding_id(int encoding);
579 #endif /* LIBPQ_FE_H */