1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2014, 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 */
114 * PGPing - The ordering of this enum should not be altered because the
115 * values are exposed externally via pg_isready.
120 PQPING_OK, /* server is accepting connections */
121 PQPING_REJECT, /* server is alive but rejecting connections */
122 PQPING_NO_RESPONSE, /* could not establish connection */
123 PQPING_NO_ATTEMPT /* connection not attempted (bad params) */
126 /* PGconn encapsulates a connection to the backend.
127 * The contents of this struct are not supposed to be known to applications.
129 typedef struct pg_conn PGconn;
131 /* PGresult encapsulates the result of a query (or more precisely, of a single
132 * SQL command --- a query string given to PQsendQuery can contain multiple
133 * commands and thus return multiple PGresult objects).
134 * The contents of this struct are not supposed to be known to applications.
136 typedef struct pg_result PGresult;
138 /* PGcancel encapsulates the information needed to cancel a running
139 * query on an existing connection.
140 * The contents of this struct are not supposed to be known to applications.
142 typedef struct pg_cancel PGcancel;
144 /* PGnotify represents the occurrence of a NOTIFY message.
145 * Ideally this would be an opaque typedef, but it's so simple that it's
146 * unlikely to change.
147 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
148 * whereas in earlier versions it was always your own backend's PID.
150 typedef struct pgNotify
152 char *relname; /* notification condition name */
153 int be_pid; /* process ID of notifying server process */
154 char *extra; /* notification parameter */
155 /* Fields below here are private to libpq; apps should not use 'em */
156 struct pgNotify *next; /* list link */
159 /* Function types for notice-handling callbacks */
160 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
161 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
163 /* Print options for PQprint() */
166 typedef struct _PQprintOpt
168 pqbool header; /* print output field headings and row count */
169 pqbool align; /* fill align the fields */
170 pqbool standard; /* old brain dead format */
171 pqbool html3; /* output html tables */
172 pqbool expanded; /* expand tables */
173 pqbool pager; /* use pager for output if needed */
174 char *fieldSep; /* field separator */
175 char *tableOpt; /* insert to HTML <table ...> */
176 char *caption; /* HTML <caption> */
177 char **fieldName; /* null terminated array of replacement field
182 * Structure for the conninfo parameter definitions returned by PQconndefaults
183 * or PQconninfoParse.
185 * All fields except "val" point at static strings which must not be altered.
186 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
187 * will release both the val strings and the PQconninfoOption array itself.
190 typedef struct _PQconninfoOption
192 char *keyword; /* The keyword of the option */
193 char *envvar; /* Fallback environment variable name */
194 char *compiled; /* Fallback compiled in default value */
195 char *val; /* Option's current value, or NULL */
196 char *label; /* Label for field in connect dialog */
197 char *dispchar; /* Indicates how to display this field in a
198 * connect dialog. Values are: "" Display
199 * entered value as is "*" Password field -
200 * hide value "D" Debug option - don't show
202 int dispsize; /* Field size in characters for dialog */
206 * PQArgBlock -- structure for PQfn() arguments
215 int *ptr; /* can't use void (dec compiler barfs) */
221 * PGresAttDesc -- Data about a single attribute (column) of a query result
224 typedef struct pgresAttDesc
226 char *name; /* column name */
227 Oid tableid; /* source table, if known */
228 int columnid; /* source column, if known */
229 int format; /* format code for value (text/binary) */
230 Oid typid; /* type id */
231 int typlen; /* type size */
232 int atttypmod; /* type-specific modifier info */
236 * Exported functions of libpq
240 /* === in fe-connect.c === */
242 /* make a new client connection to the backend */
243 /* Asynchronous (non-blocking) */
244 extern PGconn *PQconnectStart(const char *conninfo);
245 extern PGconn *PQconnectStartParams(const char *const * keywords,
246 const char *const * values, int expand_dbname);
247 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
249 /* Synchronous (blocking) */
250 extern PGconn *PQconnectdb(const char *conninfo);
251 extern PGconn *PQconnectdbParams(const char *const * keywords,
252 const char *const * values, int expand_dbname);
253 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
254 const char *pgoptions, const char *pgtty,
256 const char *login, const char *pwd);
258 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
259 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
261 /* close the current connection and free the PGconn data structure */
262 extern void PQfinish(PGconn *conn);
264 /* get info about connection options known to PQconnectdb */
265 extern PQconninfoOption *PQconndefaults(void);
267 /* parse connection options in same way as PQconnectdb */
268 extern PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
270 /* return the connection options used by a live connection */
271 extern PQconninfoOption *PQconninfo(PGconn *conn);
273 /* free the data structure returned by PQconndefaults() or PQconninfoParse() */
274 extern void PQconninfoFree(PQconninfoOption *connOptions);
277 * close the current connection and restablish a new one with the same
280 /* Asynchronous (non-blocking) */
281 extern int PQresetStart(PGconn *conn);
282 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
284 /* Synchronous (blocking) */
285 extern void PQreset(PGconn *conn);
287 /* request a cancel structure */
288 extern PGcancel *PQgetCancel(PGconn *conn);
290 /* free a cancel structure */
291 extern void PQfreeCancel(PGcancel *cancel);
293 /* issue a cancel request */
294 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
296 /* backwards compatible version of PQcancel; not thread-safe */
297 extern int PQrequestCancel(PGconn *conn);
299 /* Accessor functions for PGconn objects */
300 extern char *PQdb(const PGconn *conn);
301 extern char *PQuser(const PGconn *conn);
302 extern char *PQpass(const PGconn *conn);
303 extern char *PQhost(const PGconn *conn);
304 extern char *PQhostaddr(const PGconn *conn);
305 extern char *PQport(const PGconn *conn);
306 extern char *PQtty(const PGconn *conn);
307 extern char *PQoptions(const PGconn *conn);
308 extern ConnStatusType PQstatus(const PGconn *conn);
309 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
310 extern const char *PQparameterStatus(const PGconn *conn,
311 const char *paramName);
312 extern int PQprotocolVersion(const PGconn *conn);
313 extern int PQserverVersion(const PGconn *conn);
314 extern char *PQerrorMessage(const PGconn *conn);
315 extern int PQsocket(const PGconn *conn);
316 extern int PQbackendPID(const PGconn *conn);
317 extern int PQconnectionNeedsPassword(const PGconn *conn);
318 extern int PQconnectionUsedPassword(const PGconn *conn);
319 extern int PQclientEncoding(const PGconn *conn);
320 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
322 /* Get the OpenSSL structure associated with a connection. Returns NULL for
323 * unencrypted connections or if any other TLS library is in use. */
324 extern void *PQgetssl(PGconn *conn);
326 /* Tell libpq whether it needs to initialize OpenSSL */
327 extern void PQinitSSL(int do_init);
329 /* More detailed way to tell libpq whether it needs to initialize OpenSSL */
330 extern void PQinitOpenSSL(int do_ssl, int do_crypto);
332 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
333 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
335 /* Enable/disable tracing */
336 extern void PQtrace(PGconn *conn, FILE *debug_port);
337 extern void PQuntrace(PGconn *conn);
339 /* Override default notice handling routines */
340 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
341 PQnoticeReceiver proc,
343 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
344 PQnoticeProcessor proc,
348 * Used to set callback that prevents concurrent access to
349 * non-thread safe functions that libpq needs.
350 * The default implementation uses a libpq internal mutex.
351 * Only required for multithreaded apps that use kerberos
352 * both within their app and for postgresql connections.
354 typedef void (*pgthreadlock_t) (int acquire);
356 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
358 /* === in fe-exec.c === */
360 /* Simple synchronous query */
361 extern PGresult *PQexec(PGconn *conn, const char *query);
362 extern PGresult *PQexecParams(PGconn *conn,
365 const Oid *paramTypes,
366 const char *const * paramValues,
367 const int *paramLengths,
368 const int *paramFormats,
370 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
371 const char *query, int nParams,
372 const Oid *paramTypes);
373 extern PGresult *PQexecPrepared(PGconn *conn,
374 const char *stmtName,
376 const char *const * paramValues,
377 const int *paramLengths,
378 const int *paramFormats,
381 /* Interface for multiple-result or asynchronous queries */
382 extern int PQsendQuery(PGconn *conn, const char *query);
383 extern int PQsendQueryParams(PGconn *conn,
386 const Oid *paramTypes,
387 const char *const * paramValues,
388 const int *paramLengths,
389 const int *paramFormats,
391 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
392 const char *query, int nParams,
393 const Oid *paramTypes);
394 extern int PQsendQueryPrepared(PGconn *conn,
395 const char *stmtName,
397 const char *const * paramValues,
398 const int *paramLengths,
399 const int *paramFormats,
401 extern int PQsetSingleRowMode(PGconn *conn);
402 extern PGresult *PQgetResult(PGconn *conn);
404 /* Routines for managing an asynchronous query */
405 extern int PQisBusy(PGconn *conn);
406 extern int PQconsumeInput(PGconn *conn);
408 /* LISTEN/NOTIFY support */
409 extern PGnotify *PQnotifies(PGconn *conn);
411 /* Routines for copy in/out */
412 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
413 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
414 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
416 /* Deprecated routines for copy in/out */
417 extern int PQgetline(PGconn *conn, char *string, int length);
418 extern int PQputline(PGconn *conn, const char *string);
419 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
420 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
421 extern int PQendcopy(PGconn *conn);
423 /* Set blocking/nonblocking connection to the backend */
424 extern int PQsetnonblocking(PGconn *conn, int arg);
425 extern int PQisnonblocking(const PGconn *conn);
426 extern int PQisthreadsafe(void);
427 extern PGPing PQping(const char *conninfo);
428 extern PGPing PQpingParams(const char *const * keywords,
429 const char *const * values, int expand_dbname);
431 /* Force the write buffer to be written (or at least try) */
432 extern int PQflush(PGconn *conn);
435 * "Fast path" interface --- not really recommended for application
438 extern PGresult *PQfn(PGconn *conn,
443 const PQArgBlock *args,
446 /* Accessor functions for PGresult objects */
447 extern ExecStatusType PQresultStatus(const PGresult *res);
448 extern char *PQresStatus(ExecStatusType status);
449 extern char *PQresultErrorMessage(const PGresult *res);
450 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
451 extern int PQntuples(const PGresult *res);
452 extern int PQnfields(const PGresult *res);
453 extern int PQbinaryTuples(const PGresult *res);
454 extern char *PQfname(const PGresult *res, int field_num);
455 extern int PQfnumber(const PGresult *res, const char *field_name);
456 extern Oid PQftable(const PGresult *res, int field_num);
457 extern int PQftablecol(const PGresult *res, int field_num);
458 extern int PQfformat(const PGresult *res, int field_num);
459 extern Oid PQftype(const PGresult *res, int field_num);
460 extern int PQfsize(const PGresult *res, int field_num);
461 extern int PQfmod(const PGresult *res, int field_num);
462 extern char *PQcmdStatus(PGresult *res);
463 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
464 extern Oid PQoidValue(const PGresult *res); /* new and improved */
465 extern char *PQcmdTuples(PGresult *res);
466 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
467 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
468 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
469 extern int PQnparams(const PGresult *res);
470 extern Oid PQparamtype(const PGresult *res, int param_num);
472 /* Describe prepared statements and portals */
473 extern PGresult *PQdescribePrepared(PGconn *conn, const char *stmt);
474 extern PGresult *PQdescribePortal(PGconn *conn, const char *portal);
475 extern int PQsendDescribePrepared(PGconn *conn, const char *stmt);
476 extern int PQsendDescribePortal(PGconn *conn, const char *portal);
478 /* Delete a PGresult */
479 extern void PQclear(PGresult *res);
481 /* For freeing other alloc'd results, such as PGnotify structs */
482 extern void PQfreemem(void *ptr);
484 /* Exists for backward compatibility. bjm 2003-03-24 */
485 #define PQfreeNotify(ptr) PQfreemem(ptr)
487 /* Error when no password was given. */
488 /* Note: depending on this is deprecated; use PQconnectionNeedsPassword(). */
489 #define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
491 /* Create and manipulate PGresults */
492 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
493 extern PGresult *PQcopyResult(const PGresult *src, int flags);
494 extern int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
495 extern void *PQresultAlloc(PGresult *res, size_t nBytes);
496 extern int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
498 /* Quoting strings before inclusion in queries. */
499 extern size_t PQescapeStringConn(PGconn *conn,
500 char *to, const char *from, size_t length,
502 extern char *PQescapeLiteral(PGconn *conn, const char *str, size_t len);
503 extern char *PQescapeIdentifier(PGconn *conn, const char *str, size_t len);
504 extern unsigned char *PQescapeByteaConn(PGconn *conn,
505 const unsigned char *from, size_t from_length,
507 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
510 /* These forms are deprecated! */
511 extern size_t PQescapeString(char *to, const char *from, size_t length);
512 extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
517 /* === in fe-print.c === */
519 extern void PQprint(FILE *fout, /* output stream */
521 const PQprintOpt *ps); /* option structure */
524 * really old printing routines
526 extern void PQdisplayTuples(const PGresult *res,
527 FILE *fp, /* where to send the output */
528 int fillAlign, /* pad the fields with spaces */
529 const char *fieldSep, /* field separator */
530 int printHeader, /* display headers? */
533 extern void PQprintTuples(const PGresult *res,
534 FILE *fout, /* output stream */
535 int printAttName, /* print attribute names */
536 int terseOutput, /* delimiter bars */
537 int width); /* width of column, if 0, use variable width */
540 /* === in fe-lobj.c === */
542 /* Large-object access routines */
543 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
544 extern int lo_close(PGconn *conn, int fd);
545 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
546 extern int lo_write(PGconn *conn, int fd, const char *buf, size_t len);
547 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
548 extern pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence);
549 extern Oid lo_creat(PGconn *conn, int mode);
550 extern Oid lo_create(PGconn *conn, Oid lobjId);
551 extern int lo_tell(PGconn *conn, int fd);
552 extern pg_int64 lo_tell64(PGconn *conn, int fd);
553 extern int lo_truncate(PGconn *conn, int fd, size_t len);
554 extern int lo_truncate64(PGconn *conn, int fd, pg_int64 len);
555 extern int lo_unlink(PGconn *conn, Oid lobjId);
556 extern Oid lo_import(PGconn *conn, const char *filename);
557 extern Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
558 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
560 /* === in fe-misc.c === */
562 /* Get the version of the libpq library in use */
563 extern int PQlibVersion(void);
565 /* Determine length of multibyte encoded char at *s */
566 extern int PQmblen(const char *s, int encoding);
568 /* Determine display length of multibyte encoded char at *s */
569 extern int PQdsplen(const char *s, int encoding);
571 /* Get encoding id from environment variable PGCLIENTENCODING */
572 extern int PQenv2encoding(void);
574 /* === in fe-auth.c === */
576 extern char *PQencryptPassword(const char *passwd, const char *user);
578 /* === in encnames.c === */
580 extern int pg_char_to_encoding(const char *name);
581 extern const char *pg_encoding_to_char(int encoding);
582 extern int pg_valid_server_encoding_id(int encoding);
588 #endif /* LIBPQ_FE_H */