1/*-------------------------------------------------------------------------
2 *
3 * libpq-fe.h
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
6 *
7 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/interfaces/libpq/libpq-fe.h
11 *
12 *-------------------------------------------------------------------------
13 */
14
15#ifndef LIBPQ_FE_H
16#define LIBPQ_FE_H
17
18#ifdef __cplusplus
19extern "C"
20{
21#endif
22
23#include <stdio.h>
24
25/*
26 * postgres_ext.h defines the backend's externally visible types,
27 * such as Oid.
28 */
29#include "postgres_ext.h"
30
31/*
32 * Option flags for PQcopyResult
33 */
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
38
39/* Application-visible enum types */
40
41/*
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.
45 */
46
47typedef enum
48{
49 CONNECTION_OK,
50 CONNECTION_BAD,
51 /* Non-blocking mode only below here */
52
53 /*
54 * The existence of these should never be relied upon - they should only
55 * be used for user feedback or similar purposes.
56 */
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
60 * postmaster. */
61 CONNECTION_AUTH_OK, /* Received authentication; waiting for
62 * backend startup. */
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
67 * connection. */
68 CONNECTION_CONSUME /* Wait for any pending message and consume
69 * them. */
70} ConnStatusType;
71
72typedef enum
73{
74 PGRES_POLLING_FAILED = 0,
75 PGRES_POLLING_READING, /* These two indicate that one may */
76 PGRES_POLLING_WRITING, /* use select before polling again. */
77 PGRES_POLLING_OK,
78 PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
79 * compatibility */
80} PostgresPollingStatusType;
81
82typedef enum
83{
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
87 * backend */
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
94 * backend */
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 */
99} ExecStatusType;
100
101typedef enum
102{
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;
109
110typedef enum
111{
112 PQERRORS_TERSE, /* single-line error messages */
113 PQERRORS_DEFAULT, /* recommended style */
114 PQERRORS_VERBOSE /* all the facts, ma'am */
115} PGVerbosity;
116
117typedef enum
118{
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;
123
124/*
125 * PGPing - The ordering of this enum should not be altered because the
126 * values are exposed externally via pg_isready.
127 */
128
129typedef enum
130{
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) */
135} PGPing;
136
137/* PGconn encapsulates a connection to the backend.
138 * The contents of this struct are not supposed to be known to applications.
139 */
140typedef struct pg_conn PGconn;
141
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.
146 */
147typedef struct pg_result PGresult;
148
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.
152 */
153typedef struct pg_cancel PGcancel;
154
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.
160 */
161typedef struct pgNotify
162{
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 */
168} PGnotify;
169
170/* Function types for notice-handling callbacks */
171typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
172typedef void (*PQnoticeProcessor) (void *arg, const char *message);
173
174/* Print options for PQprint() */
175typedef char pqbool;
176
177typedef struct _PQprintOpt
178{
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
189 * names */
190} PQprintOpt;
191
192/* ----------------
193 * Structure for the conninfo parameter definitions returned by PQconndefaults
194 * or PQconninfoParse.
195 *
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.
199 * ----------------
200 */
201typedef struct _PQconninfoOption
202{
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
212 * by default */
213 int dispsize; /* Field size in characters for dialog */
214} PQconninfoOption;
215
216/* ----------------
217 * PQArgBlock -- structure for PQfn() arguments
218 * ----------------
219 */
220typedef struct
221{
222 int len;
223 int isint;
224 union
225 {
226 int *ptr; /* can't use void (dec compiler barfs) */
227 int integer;
228 } u;
229} PQArgBlock;
230
231/* ----------------
232 * PGresAttDesc -- Data about a single attribute (column) of a query result
233 * ----------------
234 */
235typedef struct pgresAttDesc
236{
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 */
244} PGresAttDesc;
245
246/* ----------------
247 * Exported functions of libpq
248 * ----------------
249 */
250
251/* === in fe-connect.c === */
252
253/* make a new client connection to the backend */
254/* Asynchronous (non-blocking) */
255extern PGconn *PQconnectStart(const char *conninfo);
256extern PGconn *PQconnectStartParams(const char *const *keywords,
257 const char *const *values, int expand_dbname);
258extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
259
260/* Synchronous (blocking) */
261extern PGconn *PQconnectdb(const char *conninfo);
262extern PGconn *PQconnectdbParams(const char *const *keywords,
263 const char *const *values, int expand_dbname);
264extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
265 const char *pgoptions, const char *pgtty,
266 const char *dbName,
267 const char *login, const char *pwd);
268
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)
271
272/* close the current connection and free the PGconn data structure */
273extern void PQfinish(PGconn *conn);
274
275/* get info about connection options known to PQconnectdb */
276extern PQconninfoOption *PQconndefaults(void);
277
278/* parse connection options in same way as PQconnectdb */
279extern PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
280
281/* return the connection options used by a live connection */
282extern PQconninfoOption *PQconninfo(PGconn *conn);
283
284/* free the data structure returned by PQconndefaults() or PQconninfoParse() */
285extern void PQconninfoFree(PQconninfoOption *connOptions);
286
287/*
288 * close the current connection and restablish a new one with the same
289 * parameters
290 */
291/* Asynchronous (non-blocking) */
292extern int PQresetStart(PGconn *conn);
293extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
294
295/* Synchronous (blocking) */
296extern void PQreset(PGconn *conn);
297
298/* request a cancel structure */
299extern PGcancel *PQgetCancel(PGconn *conn);
300
301/* free a cancel structure */
302extern void PQfreeCancel(PGcancel *cancel);
303
304/* issue a cancel request */
305extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
306
307/* backwards compatible version of PQcancel; not thread-safe */
308extern int PQrequestCancel(PGconn *conn);
309
310/* Accessor functions for PGconn objects */
311extern char *PQdb(const PGconn *conn);
312extern char *PQuser(const PGconn *conn);
313extern char *PQpass(const PGconn *conn);
314extern char *PQhost(const PGconn *conn);
315extern char *PQport(const PGconn *conn);
316extern char *PQtty(const PGconn *conn);
317extern char *PQoptions(const PGconn *conn);
318extern ConnStatusType PQstatus(const PGconn *conn);
319extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
320extern const char *PQparameterStatus(const PGconn *conn,
321 const char *paramName);
322extern int PQprotocolVersion(const PGconn *conn);
323extern int PQserverVersion(const PGconn *conn);
324extern char *PQerrorMessage(const PGconn *conn);
325extern int PQsocket(const PGconn *conn);
326extern int PQbackendPID(const PGconn *conn);
327extern int PQconnectionNeedsPassword(const PGconn *conn);
328extern int PQconnectionUsedPassword(const PGconn *conn);
329extern int PQclientEncoding(const PGconn *conn);
330extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
331
332/* SSL information functions */
333extern int PQsslInUse(PGconn *conn);
334extern void *PQsslStruct(PGconn *conn, const char *struct_name);
335extern const char *PQsslAttribute(PGconn *conn, const char *attribute_name);
336extern const char *const *PQsslAttributeNames(PGconn *conn);
337
338/* Get the OpenSSL structure associated with a connection. Returns NULL for
339 * unencrypted connections or if any other TLS library is in use. */
340extern void *PQgetssl(PGconn *conn);
341
342/* Tell libpq whether it needs to initialize OpenSSL */
343extern void PQinitSSL(int do_init);
344
345/* More detailed way to tell libpq whether it needs to initialize OpenSSL */
346extern void PQinitOpenSSL(int do_ssl, int do_crypto);
347
348/* Set verbosity for PQerrorMessage and PQresultErrorMessage */
349extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
350
351/* Set CONTEXT visibility for PQerrorMessage and PQresultErrorMessage */
352extern PGContextVisibility PQsetErrorContextVisibility(PGconn *conn,
353 PGContextVisibility show_context);
354
355/* Enable/disable tracing */
356extern void PQtrace(PGconn *conn, FILE *debug_port);
357extern void PQuntrace(PGconn *conn);
358
359/* Override default notice handling routines */
360extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
361 PQnoticeReceiver proc,
362 void *arg);
363extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
364 PQnoticeProcessor proc,
365 void *arg);
366
367/*
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.
373 */
374typedef void (*pgthreadlock_t) (int acquire);
375
376extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
377
378/* === in fe-exec.c === */
379
380/* Simple synchronous query */
381extern PGresult *PQexec(PGconn *conn, const char *query);
382extern PGresult *PQexecParams(PGconn *conn,
383 const char *command,
384 int nParams,
385 const Oid *paramTypes,
386 const char *const *paramValues,
387 const int *paramLengths,
388 const int *paramFormats,
389 int resultFormat);
390extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
391 const char *query, int nParams,
392 const Oid *paramTypes);
393extern PGresult *PQexecPrepared(PGconn *conn,
394 const char *stmtName,
395 int nParams,
396 const char *const *paramValues,
397 const int *paramLengths,
398 const int *paramFormats,
399 int resultFormat);
400
401/* Interface for multiple-result or asynchronous queries */
402extern int PQsendQuery(PGconn *conn, const char *query);
403extern int PQsendQueryParams(PGconn *conn,
404 const char *command,
405 int nParams,
406 const Oid *paramTypes,
407 const char *const *paramValues,
408 const int *paramLengths,
409 const int *paramFormats,
410 int resultFormat);
411extern int PQsendPrepare(PGconn *conn, const char *stmtName,
412 const char *query, int nParams,
413 const Oid *paramTypes);
414extern int PQsendQueryPrepared(PGconn *conn,
415 const char *stmtName,
416 int nParams,
417 const char *const *paramValues,
418 const int *paramLengths,
419 const int *paramFormats,
420 int resultFormat);
421extern int PQsetSingleRowMode(PGconn *conn);
422extern PGresult *PQgetResult(PGconn *conn);
423
424/* Routines for managing an asynchronous query */
425extern int PQisBusy(PGconn *conn);
426extern int PQconsumeInput(PGconn *conn);
427
428/* LISTEN/NOTIFY support */
429extern PGnotify *PQnotifies(PGconn *conn);
430
431/* Routines for copy in/out */
432extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
433extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
434extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
435
436/* Deprecated routines for copy in/out */
437extern int PQgetline(PGconn *conn, char *string, int length);
438extern int PQputline(PGconn *conn, const char *string);
439extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
440extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
441extern int PQendcopy(PGconn *conn);
442
443/* Set blocking/nonblocking connection to the backend */
444extern int PQsetnonblocking(PGconn *conn, int arg);
445extern int PQisnonblocking(const PGconn *conn);
446extern int PQisthreadsafe(void);
447extern PGPing PQping(const char *conninfo);
448extern PGPing PQpingParams(const char *const *keywords,
449 const char *const *values, int expand_dbname);
450
451/* Force the write buffer to be written (or at least try) */
452extern int PQflush(PGconn *conn);
453
454/*
455 * "Fast path" interface --- not really recommended for application
456 * use
457 */
458extern PGresult *PQfn(PGconn *conn,
459 int fnid,
460 int *result_buf,
461 int *result_len,
462 int result_is_int,
463 const PQArgBlock *args,
464 int nargs);
465
466/* Accessor functions for PGresult objects */
467extern ExecStatusType PQresultStatus(const PGresult *res);
468extern char *PQresStatus(ExecStatusType status);
469extern char *PQresultErrorMessage(const PGresult *res);
470extern char *PQresultVerboseErrorMessage(const PGresult *res,
471 PGVerbosity verbosity,
472 PGContextVisibility show_context);
473extern char *PQresultErrorField(const PGresult *res, int fieldcode);
474extern int PQntuples(const PGresult *res);
475extern int PQnfields(const PGresult *res);
476extern int PQbinaryTuples(const PGresult *res);
477extern char *PQfname(const PGresult *res, int field_num);
478extern int PQfnumber(const PGresult *res, const char *field_name);
479extern Oid PQftable(const PGresult *res, int field_num);
480extern int PQftablecol(const PGresult *res, int field_num);
481extern int PQfformat(const PGresult *res, int field_num);
482extern Oid PQftype(const PGresult *res, int field_num);
483extern int PQfsize(const PGresult *res, int field_num);
484extern int PQfmod(const PGresult *res, int field_num);
485extern char *PQcmdStatus(PGresult *res);
486extern char *PQoidStatus(const PGresult *res); /* old and ugly */
487extern Oid PQoidValue(const PGresult *res); /* new and improved */
488extern char *PQcmdTuples(PGresult *res);
489extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
490extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
491extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
492extern int PQnparams(const PGresult *res);
493extern Oid PQparamtype(const PGresult *res, int param_num);
494
495/* Describe prepared statements and portals */
496extern PGresult *PQdescribePrepared(PGconn *conn, const char *stmt);
497extern PGresult *PQdescribePortal(PGconn *conn, const char *portal);
498extern int PQsendDescribePrepared(PGconn *conn, const char *stmt);
499extern int PQsendDescribePortal(PGconn *conn, const char *portal);
500
501/* Delete a PGresult */
502extern void PQclear(PGresult *res);
503
504/* For freeing other alloc'd results, such as PGnotify structs */
505extern void PQfreemem(void *ptr);
506
507/* Exists for backward compatibility. bjm 2003-03-24 */
508#define PQfreeNotify(ptr) PQfreemem(ptr)
509
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"
513
514/* Create and manipulate PGresults */
515extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
516extern PGresult *PQcopyResult(const PGresult *src, int flags);
517extern int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
518extern void *PQresultAlloc(PGresult *res, size_t nBytes);
519extern int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
520
521/* Quoting strings before inclusion in queries. */
522extern size_t PQescapeStringConn(PGconn *conn,
523 char *to, const char *from, size_t length,
524 int *error);
525extern char *PQescapeLiteral(PGconn *conn, const char *str, size_t len);
526extern char *PQescapeIdentifier(PGconn *conn, const char *str, size_t len);
527extern unsigned char *PQescapeByteaConn(PGconn *conn,
528 const unsigned char *from, size_t from_length,
529 size_t *to_length);
530extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
531 size_t *retbuflen);
532
533/* These forms are deprecated! */
534extern size_t PQescapeString(char *to, const char *from, size_t length);
535extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
536 size_t *to_length);
537
538
539
540/* === in fe-print.c === */
541
542extern void PQprint(FILE *fout, /* output stream */
543 const PGresult *res,
544 const PQprintOpt *ps); /* option structure */
545
546/*
547 * really old printing routines
548 */
549extern 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? */
554 int quiet);
555
556extern 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 */
561
562
563/* === in fe-lobj.c === */
564
565/* Large-object access routines */
566extern int lo_open(PGconn *conn, Oid lobjId, int mode);
567extern int lo_close(PGconn *conn, int fd);
568extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
569extern int lo_write(PGconn *conn, int fd, const char *buf, size_t len);
570extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
571extern pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence);
572extern Oid lo_creat(PGconn *conn, int mode);
573extern Oid lo_create(PGconn *conn, Oid lobjId);
574extern int lo_tell(PGconn *conn, int fd);
575extern pg_int64 lo_tell64(PGconn *conn, int fd);
576extern int lo_truncate(PGconn *conn, int fd, size_t len);
577extern int lo_truncate64(PGconn *conn, int fd, pg_int64 len);
578extern int lo_unlink(PGconn *conn, Oid lobjId);
579extern Oid lo_import(PGconn *conn, const char *filename);
580extern Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
581extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
582
583/* === in fe-misc.c === */
584
585/* Get the version of the libpq library in use */
586extern int PQlibVersion(void);
587
588/* Determine length of multibyte encoded char at *s */
589extern int PQmblen(const char *s, int encoding);
590
591/* Determine display length of multibyte encoded char at *s */
592extern int PQdsplen(const char *s, int encoding);
593
594/* Get encoding id from environment variable PGCLIENTENCODING */
595extern int PQenv2encoding(void);
596
597/* === in fe-auth.c === */
598
599extern char *PQencryptPassword(const char *passwd, const char *user);
600extern char *PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm);
601
602/* === in encnames.c === */
603
604extern int pg_char_to_encoding(const char *name);
605extern const char *pg_encoding_to_char(int encoding);
606extern int pg_valid_server_encoding_id(int encoding);
607
608#ifdef __cplusplus
609}
610#endif
611
612#endif /* LIBPQ_FE_H */
613