1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2017, 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 */
66 CONNECTION_CHECK_WRITABLE, /* Check if we could make a writable
68 CONNECTION_CONSUME /* Wait for any pending message and consume
74 PGRES_POLLING_FAILED = 0,
75 PGRES_POLLING_READING, /* These two indicate that one may */
76 PGRES_POLLING_WRITING, /* use select before polling again. */
78 PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
80 } PostgresPollingStatusType;
84 PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
85 PGRES_COMMAND_OK, /* a query command that doesn't return
86 * anything was executed properly by the
88 PGRES_TUPLES_OK, /* a query command that returns tuples was
89 * executed properly by the backend, PGresult
90 * contains the result tuples */
91 PGRES_COPY_OUT, /* Copy Out data transfer in progress */
92 PGRES_COPY_IN, /* Copy In data transfer in progress */
93 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the
95 PGRES_NONFATAL_ERROR, /* notice or warning message */
96 PGRES_FATAL_ERROR, /* query failed */
97 PGRES_COPY_BOTH, /* Copy In/Out data transfer in progress */
98 PGRES_SINGLE_TUPLE /* single tuple from larger resultset */
103 PQTRANS_IDLE, /* connection idle */
104 PQTRANS_ACTIVE, /* command in progress */
105 PQTRANS_INTRANS, /* idle, within transaction block */
106 PQTRANS_INERROR, /* idle, within failed transaction */
107 PQTRANS_UNKNOWN /* cannot determine status */
108 } PGTransactionStatusType;
112 PQERRORS_TERSE, /* single-line error messages */
113 PQERRORS_DEFAULT, /* recommended style */
114 PQERRORS_VERBOSE /* all the facts, ma'am */
119 PQSHOW_CONTEXT_NEVER, /* never show CONTEXT field */
120 PQSHOW_CONTEXT_ERRORS, /* show CONTEXT for errors only (default) */
121 PQSHOW_CONTEXT_ALWAYS /* always show CONTEXT field */
122 } PGContextVisibility;
125 * PGPing - The ordering of this enum should not be altered because the
126 * values are exposed externally via pg_isready.
131 PQPING_OK, /* server is accepting connections */
132 PQPING_REJECT, /* server is alive but rejecting connections */
133 PQPING_NO_RESPONSE, /* could not establish connection */
134 PQPING_NO_ATTEMPT /* connection not attempted (bad params) */
137 /* PGconn encapsulates a connection to the backend.
138 * The contents of this struct are not supposed to be known to applications.
140 typedef struct pg_conn PGconn;
142 /* PGresult encapsulates the result of a query (or more precisely, of a single
143 * SQL command --- a query string given to PQsendQuery can contain multiple
144 * commands and thus return multiple PGresult objects).
145 * The contents of this struct are not supposed to be known to applications.
147 typedef struct pg_result PGresult;
149 /* PGcancel encapsulates the information needed to cancel a running
150 * query on an existing connection.
151 * The contents of this struct are not supposed to be known to applications.
153 typedef struct pg_cancel PGcancel;
155 /* PGnotify represents the occurrence of a NOTIFY message.
156 * Ideally this would be an opaque typedef, but it's so simple that it's
157 * unlikely to change.
158 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
159 * whereas in earlier versions it was always your own backend's PID.
161 typedef struct pgNotify
163 char *relname; /* notification condition name */
164 int be_pid; /* process ID of notifying server process */
165 char *extra; /* notification parameter */
166 /* Fields below here are private to libpq; apps should not use 'em */
167 struct pgNotify *next; /* list link */
170 /* Function types for notice-handling callbacks */
171 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
172 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
174 /* Print options for PQprint() */
177 typedef struct _PQprintOpt
179 pqbool header; /* print output field headings and row count */
180 pqbool align; /* fill align the fields */
181 pqbool standard; /* old brain dead format */
182 pqbool html3; /* output html tables */
183 pqbool expanded; /* expand tables */
184 pqbool pager; /* use pager for output if needed */
185 char *fieldSep; /* field separator */
186 char *tableOpt; /* insert to HTML <table ...> */
187 char *caption; /* HTML <caption> */
188 char **fieldName; /* null terminated array of replacement field
193 * Structure for the conninfo parameter definitions returned by PQconndefaults
194 * or PQconninfoParse.
196 * All fields except "val" point at static strings which must not be altered.
197 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
198 * will release both the val strings and the PQconninfoOption array itself.
201 typedef struct _PQconninfoOption
203 char *keyword; /* The keyword of the option */
204 char *envvar; /* Fallback environment variable name */
205 char *compiled; /* Fallback compiled in default value */
206 char *val; /* Option's current value, or NULL */
207 char *label; /* Label for field in connect dialog */
208 char *dispchar; /* Indicates how to display this field in a
209 * connect dialog. Values are: "" Display
210 * entered value as is "*" Password field -
211 * hide value "D" Debug option - don't show
213 int dispsize; /* Field size in characters for dialog */
217 * PQArgBlock -- structure for PQfn() arguments
226 int *ptr; /* can't use void (dec compiler barfs) */
232 * PGresAttDesc -- Data about a single attribute (column) of a query result
235 typedef struct pgresAttDesc
237 char *name; /* column name */
238 Oid tableid; /* source table, if known */
239 int columnid; /* source column, if known */
240 int format; /* format code for value (text/binary) */
241 Oid typid; /* type id */
242 int typlen; /* type size */
243 int atttypmod; /* type-specific modifier info */
247 * Exported functions of libpq
251 /* === in fe-connect.c === */
253 /* make a new client connection to the backend */
254 /* Asynchronous (non-blocking) */
255 extern PGconn *PQconnectStart(const char *conninfo);
256 extern PGconn *PQconnectStartParams(const char *const *keywords,
257 const char *const *values, int expand_dbname);
258 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
260 /* Synchronous (blocking) */
261 extern PGconn *PQconnectdb(const char *conninfo);
262 extern PGconn *PQconnectdbParams(const char *const *keywords,
263 const char *const *values, int expand_dbname);
264 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
265 const char *pgoptions, const char *pgtty,
267 const char *login, const char *pwd);
269 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
270 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
272 /* close the current connection and free the PGconn data structure */
273 extern void PQfinish(PGconn *conn);
275 /* get info about connection options known to PQconnectdb */
276 extern PQconninfoOption *PQconndefaults(void);
278 /* parse connection options in same way as PQconnectdb */
279 extern PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
281 /* return the connection options used by a live connection */
282 extern PQconninfoOption *PQconninfo(PGconn *conn);
284 /* free the data structure returned by PQconndefaults() or PQconninfoParse() */
285 extern void PQconninfoFree(PQconninfoOption *connOptions);
288 * close the current connection and restablish a new one with the same
291 /* Asynchronous (non-blocking) */
292 extern int PQresetStart(PGconn *conn);
293 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
295 /* Synchronous (blocking) */
296 extern void PQreset(PGconn *conn);
298 /* request a cancel structure */
299 extern PGcancel *PQgetCancel(PGconn *conn);
301 /* free a cancel structure */
302 extern void PQfreeCancel(PGcancel *cancel);
304 /* issue a cancel request */
305 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
307 /* backwards compatible version of PQcancel; not thread-safe */
308 extern int PQrequestCancel(PGconn *conn);
310 /* Accessor functions for PGconn objects */
311 extern char *PQdb(const PGconn *conn);
312 extern char *PQuser(const PGconn *conn);
313 extern char *PQpass(const PGconn *conn);
314 extern char *PQhost(const PGconn *conn);
315 extern char *PQport(const PGconn *conn);
316 extern char *PQtty(const PGconn *conn);
317 extern char *PQoptions(const PGconn *conn);
318 extern ConnStatusType PQstatus(const PGconn *conn);
319 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
320 extern const char *PQparameterStatus(const PGconn *conn,
321 const char *paramName);
322 extern int PQprotocolVersion(const PGconn *conn);
323 extern int PQserverVersion(const PGconn *conn);
324 extern char *PQerrorMessage(const PGconn *conn);
325 extern int PQsocket(const PGconn *conn);
326 extern int PQbackendPID(const PGconn *conn);
327 extern int PQconnectionNeedsPassword(const PGconn *conn);
328 extern int PQconnectionUsedPassword(const PGconn *conn);
329 extern int PQclientEncoding(const PGconn *conn);
330 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
332 /* SSL information functions */
333 extern int PQsslInUse(PGconn *conn);
334 extern void *PQsslStruct(PGconn *conn, const char *struct_name);
335 extern const char *PQsslAttribute(PGconn *conn, const char *attribute_name);
336 extern const char *const *PQsslAttributeNames(PGconn *conn);
338 /* Get the OpenSSL structure associated with a connection. Returns NULL for
339 * unencrypted connections or if any other TLS library is in use. */
340 extern void *PQgetssl(PGconn *conn);
342 /* Tell libpq whether it needs to initialize OpenSSL */
343 extern void PQinitSSL(int do_init);
345 /* More detailed way to tell libpq whether it needs to initialize OpenSSL */
346 extern void PQinitOpenSSL(int do_ssl, int do_crypto);
348 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
349 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
351 /* Set CONTEXT visibility for PQerrorMessage and PQresultErrorMessage */
352 extern PGContextVisibility PQsetErrorContextVisibility(PGconn *conn,
353 PGContextVisibility show_context);
355 /* Enable/disable tracing */
356 extern void PQtrace(PGconn *conn, FILE *debug_port);
357 extern void PQuntrace(PGconn *conn);
359 /* Override default notice handling routines */
360 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
361 PQnoticeReceiver proc,
363 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
364 PQnoticeProcessor proc,
368 * Used to set callback that prevents concurrent access to
369 * non-thread safe functions that libpq needs.
370 * The default implementation uses a libpq internal mutex.
371 * Only required for multithreaded apps that use kerberos
372 * both within their app and for postgresql connections.
374 typedef void (*pgthreadlock_t) (int acquire);
376 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
378 /* === in fe-exec.c === */
380 /* Simple synchronous query */
381 extern PGresult *PQexec(PGconn *conn, const char *query);
382 extern PGresult *PQexecParams(PGconn *conn,
385 const Oid *paramTypes,
386 const char *const *paramValues,
387 const int *paramLengths,
388 const int *paramFormats,
390 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
391 const char *query, int nParams,
392 const Oid *paramTypes);
393 extern PGresult *PQexecPrepared(PGconn *conn,
394 const char *stmtName,
396 const char *const *paramValues,
397 const int *paramLengths,
398 const int *paramFormats,
401 /* Interface for multiple-result or asynchronous queries */
402 extern int PQsendQuery(PGconn *conn, const char *query);
403 extern int PQsendQueryParams(PGconn *conn,
406 const Oid *paramTypes,
407 const char *const *paramValues,
408 const int *paramLengths,
409 const int *paramFormats,
411 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
412 const char *query, int nParams,
413 const Oid *paramTypes);
414 extern int PQsendQueryPrepared(PGconn *conn,
415 const char *stmtName,
417 const char *const *paramValues,
418 const int *paramLengths,
419 const int *paramFormats,
421 extern int PQsetSingleRowMode(PGconn *conn);
422 extern PGresult *PQgetResult(PGconn *conn);
424 /* Routines for managing an asynchronous query */
425 extern int PQisBusy(PGconn *conn);
426 extern int PQconsumeInput(PGconn *conn);
428 /* LISTEN/NOTIFY support */
429 extern PGnotify *PQnotifies(PGconn *conn);
431 /* Routines for copy in/out */
432 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
433 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
434 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
436 /* Deprecated routines for copy in/out */
437 extern int PQgetline(PGconn *conn, char *string, int length);
438 extern int PQputline(PGconn *conn, const char *string);
439 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
440 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
441 extern int PQendcopy(PGconn *conn);
443 /* Set blocking/nonblocking connection to the backend */
444 extern int PQsetnonblocking(PGconn *conn, int arg);
445 extern int PQisnonblocking(const PGconn *conn);
446 extern int PQisthreadsafe(void);
447 extern PGPing PQping(const char *conninfo);
448 extern PGPing PQpingParams(const char *const *keywords,
449 const char *const *values, int expand_dbname);
451 /* Force the write buffer to be written (or at least try) */
452 extern int PQflush(PGconn *conn);
455 * "Fast path" interface --- not really recommended for application
458 extern PGresult *PQfn(PGconn *conn,
463 const PQArgBlock *args,
466 /* Accessor functions for PGresult objects */
467 extern ExecStatusType PQresultStatus(const PGresult *res);
468 extern char *PQresStatus(ExecStatusType status);
469 extern char *PQresultErrorMessage(const PGresult *res);
470 extern char *PQresultVerboseErrorMessage(const PGresult *res,
471 PGVerbosity verbosity,
472 PGContextVisibility show_context);
473 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
474 extern int PQntuples(const PGresult *res);
475 extern int PQnfields(const PGresult *res);
476 extern int PQbinaryTuples(const PGresult *res);
477 extern char *PQfname(const PGresult *res, int field_num);
478 extern int PQfnumber(const PGresult *res, const char *field_name);
479 extern Oid PQftable(const PGresult *res, int field_num);
480 extern int PQftablecol(const PGresult *res, int field_num);
481 extern int PQfformat(const PGresult *res, int field_num);
482 extern Oid PQftype(const PGresult *res, int field_num);
483 extern int PQfsize(const PGresult *res, int field_num);
484 extern int PQfmod(const PGresult *res, int field_num);
485 extern char *PQcmdStatus(PGresult *res);
486 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
487 extern Oid PQoidValue(const PGresult *res); /* new and improved */
488 extern char *PQcmdTuples(PGresult *res);
489 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
490 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
491 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
492 extern int PQnparams(const PGresult *res);
493 extern Oid PQparamtype(const PGresult *res, int param_num);
495 /* Describe prepared statements and portals */
496 extern PGresult *PQdescribePrepared(PGconn *conn, const char *stmt);
497 extern PGresult *PQdescribePortal(PGconn *conn, const char *portal);
498 extern int PQsendDescribePrepared(PGconn *conn, const char *stmt);
499 extern int PQsendDescribePortal(PGconn *conn, const char *portal);
501 /* Delete a PGresult */
502 extern void PQclear(PGresult *res);
504 /* For freeing other alloc'd results, such as PGnotify structs */
505 extern void PQfreemem(void *ptr);
507 /* Exists for backward compatibility. bjm 2003-03-24 */
508 #define PQfreeNotify(ptr) PQfreemem(ptr)
510 /* Error when no password was given. */
511 /* Note: depending on this is deprecated; use PQconnectionNeedsPassword(). */
512 #define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
514 /* Create and manipulate PGresults */
515 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
516 extern PGresult *PQcopyResult(const PGresult *src, int flags);
517 extern int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
518 extern void *PQresultAlloc(PGresult *res, size_t nBytes);
519 extern int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
521 /* Quoting strings before inclusion in queries. */
522 extern size_t PQescapeStringConn(PGconn *conn,
523 char *to, const char *from, size_t length,
525 extern char *PQescapeLiteral(PGconn *conn, const char *str, size_t len);
526 extern char *PQescapeIdentifier(PGconn *conn, const char *str, size_t len);
527 extern unsigned char *PQescapeByteaConn(PGconn *conn,
528 const unsigned char *from, size_t from_length,
530 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
533 /* These forms are deprecated! */
534 extern size_t PQescapeString(char *to, const char *from, size_t length);
535 extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
540 /* === in fe-print.c === */
542 extern void PQprint(FILE *fout, /* output stream */
544 const PQprintOpt *ps); /* option structure */
547 * really old printing routines
549 extern void PQdisplayTuples(const PGresult *res,
550 FILE *fp, /* where to send the output */
551 int fillAlign, /* pad the fields with spaces */
552 const char *fieldSep, /* field separator */
553 int printHeader, /* display headers? */
556 extern void PQprintTuples(const PGresult *res,
557 FILE *fout, /* output stream */
558 int printAttName, /* print attribute names */
559 int terseOutput, /* delimiter bars */
560 int width); /* width of column, if 0, use variable width */
563 /* === in fe-lobj.c === */
565 /* Large-object access routines */
566 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
567 extern int lo_close(PGconn *conn, int fd);
568 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
569 extern int lo_write(PGconn *conn, int fd, const char *buf, size_t len);
570 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
571 extern pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence);
572 extern Oid lo_creat(PGconn *conn, int mode);
573 extern Oid lo_create(PGconn *conn, Oid lobjId);
574 extern int lo_tell(PGconn *conn, int fd);
575 extern pg_int64 lo_tell64(PGconn *conn, int fd);
576 extern int lo_truncate(PGconn *conn, int fd, size_t len);
577 extern int lo_truncate64(PGconn *conn, int fd, pg_int64 len);
578 extern int lo_unlink(PGconn *conn, Oid lobjId);
579 extern Oid lo_import(PGconn *conn, const char *filename);
580 extern Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
581 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
583 /* === in fe-misc.c === */
585 /* Get the version of the libpq library in use */
586 extern int PQlibVersion(void);
588 /* Determine length of multibyte encoded char at *s */
589 extern int PQmblen(const char *s, int encoding);
591 /* Determine display length of multibyte encoded char at *s */
592 extern int PQdsplen(const char *s, int encoding);
594 /* Get encoding id from environment variable PGCLIENTENCODING */
595 extern int PQenv2encoding(void);
597 /* === in fe-auth.c === */
599 extern char *PQencryptPassword(const char *passwd, const char *user);
600 extern char *PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm);
602 /* === in encnames.c === */
604 extern int pg_char_to_encoding(const char *name);
605 extern const char *pg_encoding_to_char(int encoding);
606 extern int pg_valid_server_encoding_id(int encoding);
612 #endif /* LIBPQ_FE_H */