PostgreSQL Source Code  git master
connect_utils.h File Reference
#include "libpq-fe.h"
Include dependency graph for connect_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _connParams
 

Typedefs

typedef struct _connParams ConnParams
 

Enumerations

enum  trivalue {
  TRI_DEFAULT, TRI_NO, TRI_YES, TRI_DEFAULT,
  TRI_NO, TRI_YES, TRI_DEFAULT, TRI_NO,
  TRI_YES, TRI_DEFAULT, TRI_NO, TRI_YES
}
 

Functions

PGconnconnectDatabase (const ConnParams *cparams, const char *progname, bool echo, bool fail_ok, bool allow_password_reuse)
 
PGconnconnectMaintenanceDatabase (ConnParams *cparams, const char *progname, bool echo)
 
void disconnectDatabase (PGconn *conn)
 

Typedef Documentation

◆ ConnParams

typedef struct _connParams ConnParams

Enumeration Type Documentation

◆ trivalue

enum trivalue
Enumerator
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 

Definition at line 17 of file connect_utils.h.

18 {
20  TRI_NO,
21  TRI_YES
22 };

Function Documentation

◆ connectDatabase()

PGconn* connectDatabase ( const ConnParams cparams,
const char *  progname,
bool  echo,
bool  fail_ok,
bool  allow_password_reuse 
)

Definition at line 32 of file connect_utils.c.

References ALWAYS_SECURE_SEARCH_PATH_SQL, Assert, conn, CONNECTION_BAD, _connParams::dbname, executeQuery(), free, i, lengthof, _connParams::override_dbname, password, pg_log_error, _connParams::pghost, _connParams::pgport, _connParams::pguser, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQerrorMessage(), PQfinish(), PQstatus(), progname, _connParams::prompt_password, simple_prompt(), TRI_NO, TRI_YES, and values.

Referenced by connectMaintenanceDatabase().

34 {
35  PGconn *conn;
36  bool new_pass;
37  static char *password = NULL;
38 
39  /* Callers must supply at least dbname; other params can be NULL */
40  Assert(cparams->dbname);
41 
42  if (!allow_password_reuse && password)
43  {
44  free(password);
45  password = NULL;
46  }
47 
48  if (cparams->prompt_password == TRI_YES && password == NULL)
49  password = simple_prompt("Password: ", false);
50 
51  /*
52  * Start the connection. Loop until we have a password if requested by
53  * backend.
54  */
55  do
56  {
57  const char *keywords[8];
58  const char *values[8];
59  int i = 0;
60 
61  /*
62  * If dbname is a connstring, its entries can override the other
63  * values obtained from cparams; but in turn, override_dbname can
64  * override the dbname component of it.
65  */
66  keywords[i] = "host";
67  values[i++] = cparams->pghost;
68  keywords[i] = "port";
69  values[i++] = cparams->pgport;
70  keywords[i] = "user";
71  values[i++] = cparams->pguser;
72  keywords[i] = "password";
73  values[i++] = password;
74  keywords[i] = "dbname";
75  values[i++] = cparams->dbname;
76  if (cparams->override_dbname)
77  {
78  keywords[i] = "dbname";
79  values[i++] = cparams->override_dbname;
80  }
81  keywords[i] = "fallback_application_name";
82  values[i++] = progname;
83  keywords[i] = NULL;
84  values[i++] = NULL;
85  Assert(i <= lengthof(keywords));
86 
87  new_pass = false;
88  conn = PQconnectdbParams(keywords, values, true);
89 
90  if (!conn)
91  {
92  pg_log_error("could not connect to database %s: out of memory",
93  cparams->dbname);
94  exit(1);
95  }
96 
97  /*
98  * No luck? Trying asking (again) for a password.
99  */
100  if (PQstatus(conn) == CONNECTION_BAD &&
102  cparams->prompt_password != TRI_NO)
103  {
104  PQfinish(conn);
105  if (password)
106  free(password);
107  password = simple_prompt("Password: ", false);
108  new_pass = true;
109  }
110  } while (new_pass);
111 
112  /* check to see that the backend connection was successfully made */
113  if (PQstatus(conn) == CONNECTION_BAD)
114  {
115  if (fail_ok)
116  {
117  PQfinish(conn);
118  return NULL;
119  }
120  pg_log_error("%s", PQerrorMessage(conn));
121  exit(1);
122  }
123 
124  /* Start strict; callers may override this. */
126 
127  return conn;
128 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1874
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6735
const char * progname
Definition: main.c:46
char * pgport
Definition: pg_backup.h:66
#define pg_log_error(...)
Definition: logging.h:80
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4229
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
#define lengthof(array)
Definition: c.h:734
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:662
char * dbname
Definition: pg_backup.h:65
PGconn * conn
Definition: streamutil.c:54
static char * password
Definition: streamutil.c:53
char * override_dbname
Definition: pg_backup.h:72
char * pghost
Definition: pg_backup.h:67
enum trivalue prompt_password
Definition: connect_utils.h:32
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:804
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6778
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6682
const char * pguser
Definition: connect_utils.h:31

◆ connectMaintenanceDatabase()

PGconn* connectMaintenanceDatabase ( ConnParams cparams,
const char *  progname,
bool  echo 
)

Definition at line 139 of file connect_utils.c.

References conn, connectDatabase(), and _connParams::dbname.

Referenced by cluster_all_databases(), main(), reindex_all_databases(), and vacuum_all_databases().

141 {
142  PGconn *conn;
143 
144  /* If a maintenance database name was specified, just connect to it. */
145  if (cparams->dbname)
146  return connectDatabase(cparams, progname, echo, false, false);
147 
148  /* Otherwise, try postgres first and then template1. */
149  cparams->dbname = "postgres";
150  conn = connectDatabase(cparams, progname, echo, true, false);
151  if (!conn)
152  {
153  cparams->dbname = "template1";
154  conn = connectDatabase(cparams, progname, echo, false, false);
155  }
156  return conn;
157 }
const char * progname
Definition: main.c:46
PGconn * connectDatabase(const ConnParams *cparams, const char *progname, bool echo, bool fail_ok, bool allow_password_reuse)
Definition: connect_utils.c:32
char * dbname
Definition: pg_backup.h:65
PGconn * conn
Definition: streamutil.c:54

◆ disconnectDatabase()

void disconnectDatabase ( PGconn conn)

Definition at line 163 of file connect_utils.c.

References Assert, PQcancel(), PQfinish(), PQfreeCancel(), PQgetCancel(), PQTRANS_ACTIVE, and PQtransactionStatus().

Referenced by compile_database_list(), compile_relation_list_one_db(), main(), ParallelSlotsAdoptConn(), ParallelSlotsGetIdle(), and ParallelSlotsTerminate().

164 {
165  char errbuf[256];
166 
167  Assert(conn != NULL);
168 
169  if (PQtransactionStatus(conn) == PQTRANS_ACTIVE)
170  {
171  PGcancel *cancel;
172 
173  if ((cancel = PQgetCancel(conn)))
174  {
175  (void) PQcancel(cancel, errbuf, sizeof(errbuf));
176  PQfreeCancel(cancel);
177  }
178  }
179 
180  PQfinish(conn);
181 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:4373
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4229
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:4350
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6690
#define Assert(condition)
Definition: c.h:804
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:4505