PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 };
@ TRI_YES
Definition: connect_utils.h:21
@ TRI_DEFAULT
Definition: connect_utils.h:19
@ TRI_NO
Definition: connect_utils.h:20

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.

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  pg_fatal("could not connect to database %s: out of memory",
92  cparams->dbname);
93 
94  /*
95  * No luck? Trying asking (again) for a password.
96  */
97  if (PQstatus(conn) == CONNECTION_BAD &&
99  cparams->prompt_password != TRI_NO)
100  {
101  PQfinish(conn);
102  free(password);
103  password = simple_prompt("Password: ", false);
104  new_pass = true;
105  }
106  } while (new_pass);
107 
108  /* check to see that the backend connection was successfully made */
109  if (PQstatus(conn) == CONNECTION_BAD)
110  {
111  if (fail_ok)
112  {
113  PQfinish(conn);
114  return NULL;
115  }
116  pg_fatal("%s", PQerrorMessage(conn));
117  }
118 
119  /* Start strict; callers may override this. */
121 
122  return conn;
123 }
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define Assert(condition)
Definition: c.h:863
#define lengthof(array)
Definition: c.h:793
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:690
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7251
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7200
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7137
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4879
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:72
@ CONNECTION_BAD
Definition: libpq-fe.h:82
const char * progname
Definition: main.c:43
#define pg_fatal(...)
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1883
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static char * password
Definition: streamutil.c:52
PGconn * conn
Definition: streamutil.c:53
const char * pguser
Definition: connect_utils.h:31
char * override_dbname
Definition: pg_backup.h:92
char * pgport
Definition: pg_backup.h:86
char * pghost
Definition: pg_backup.h:87
char * dbname
Definition: pg_backup.h:85
enum trivalue prompt_password
Definition: connect_utils.h:32
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_NO
Definition: vacuumlo.c:37

References ALWAYS_SECURE_SEARCH_PATH_SQL, Assert, conn, CONNECTION_BAD, _connParams::dbname, executeQuery(), free, i, lengthof, _connParams::override_dbname, password, pg_fatal, _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().

◆ connectMaintenanceDatabase()

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

Definition at line 134 of file connect_utils.c.

136 {
137  PGconn *conn;
138 
139  /* If a maintenance database name was specified, just connect to it. */
140  if (cparams->dbname)
141  return connectDatabase(cparams, progname, echo, false, false);
142 
143  /* Otherwise, try postgres first and then template1. */
144  cparams->dbname = "postgres";
145  conn = connectDatabase(cparams, progname, echo, true, false);
146  if (!conn)
147  {
148  cparams->dbname = "template1";
149  conn = connectDatabase(cparams, progname, echo, false, false);
150  }
151  return conn;
152 }
PGconn * connectDatabase(const ConnParams *cparams, const char *progname, bool echo, bool fail_ok, bool allow_password_reuse)
Definition: connect_utils.c:32

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

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

◆ disconnectDatabase()

void disconnectDatabase ( PGconn conn)

Definition at line 158 of file connect_utils.c.

159 {
160  Assert(conn != NULL);
161 
163  {
165 
168  }
169 
170  PQfinish(conn);
171 }
static PGcancel *volatile cancelConn
Definition: cancel.c:43
PGcancelConn * PQcancelCreate(PGconn *conn)
Definition: fe-cancel.c:65
int PQcancelBlocking(PGcancelConn *cancelConn)
Definition: fe-cancel.c:171
void PQcancelFinish(PGcancelConn *cancelConn)
Definition: fe-cancel.c:334
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7145
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:143

References Assert, cancelConn, conn, PQcancelBlocking(), PQcancelCreate(), PQcancelFinish(), PQfinish(), PQTRANS_ACTIVE, and PQtransactionStatus().

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