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