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

Go to the source code of this file.

Typedefs

typedef void(* help_handler) (const char *progname)
 

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

void handle_help_version_opts (int argc, char *argv[], const char *fixed_progname, help_handler hlp)
 
PGconnconnectDatabase (const char *dbname, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool echo, bool fail_ok, bool allow_password_reuse)
 
PGconnconnectMaintenanceDatabase (const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool echo)
 
PGresultexecuteQuery (PGconn *conn, const char *query, const char *progname, bool echo)
 
void executeCommand (PGconn *conn, const char *query, const char *progname, bool echo)
 
bool executeMaintenanceCommand (PGconn *conn, const char *query, bool echo)
 
void appendQualifiedRelation (PQExpBuffer buf, const char *name, PGconn *conn, const char *progname, bool echo)
 
bool yesno_prompt (const char *question)
 
void setup_cancel_handler (void)
 
void SetCancelConn (PGconn *conn)
 
void ResetCancelConn (void)
 

Variables

bool CancelRequested
 

Typedef Documentation

◆ help_handler

typedef void(* help_handler) (const char *progname)

Definition at line 26 of file common.h.

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 common.h.

18 {
20  TRI_NO,
21  TRI_YES
22 };
Definition: common.h:20
Definition: common.h:21

Function Documentation

◆ appendQualifiedRelation()

void appendQualifiedRelation ( PQExpBuffer  buf,
const char *  name,
PGconn conn,
const char *  progname,
bool  echo 
)

Definition at line 305 of file common.c.

References ALWAYS_SECURE_SEARCH_PATH_SQL, appendPQExpBufferStr(), appendStringLiteralConn(), PQExpBufferData::data, executeCommand(), executeQuery(), fmtQualifiedId(), initPQExpBuffer(), ngettext, pg_free(), PQclear(), PQclientEncoding(), PQfinish(), PQgetvalue(), PQntuples(), PQserverVersion(), split_table_columns_spec(), and termPQExpBuffer().

Referenced by cluster_one_database(), prepare_vacuum_command(), and reindex_one_database().

307 {
308  char *table;
309  const char *columns;
310  PQExpBufferData sql;
311  PGresult *res;
312  int ntups;
313 
314  /* Before 7.3, the concept of qualifying a name did not exist. */
315  if (PQserverVersion(conn) < 70300)
316  {
317  appendPQExpBufferStr(&sql, spec);
318  return;
319  }
320 
321  split_table_columns_spec(spec, PQclientEncoding(conn), &table, &columns);
322 
323  /*
324  * Query must remain ABSOLUTELY devoid of unqualified names. This would
325  * be unnecessary given a regclassin() variant taking a search_path
326  * argument.
327  */
328  initPQExpBuffer(&sql);
330  "SELECT c.relname, ns.nspname\n"
331  " FROM pg_catalog.pg_class c,"
332  " pg_catalog.pg_namespace ns\n"
333  " WHERE c.relnamespace OPERATOR(pg_catalog.=) ns.oid\n"
334  " AND c.oid OPERATOR(pg_catalog.=) ");
335  appendStringLiteralConn(&sql, table, conn);
336  appendPQExpBufferStr(&sql, "::pg_catalog.regclass;");
337 
338  executeCommand(conn, "RESET search_path", progname, echo);
339 
340  /*
341  * One row is a typical result, as is a nonexistent relation ERROR.
342  * regclassin() unconditionally accepts all-digits input as an OID; if no
343  * relation has that OID; this query returns no rows. Catalog corruption
344  * might elicit other row counts.
345  */
346  res = executeQuery(conn, sql.data, progname, echo);
347  ntups = PQntuples(res);
348  if (ntups != 1)
349  {
350  fprintf(stderr,
351  ngettext("%s: query returned %d row instead of one: %s\n",
352  "%s: query returned %d rows instead of one: %s\n",
353  ntups),
354  progname, ntups, sql.data);
355  PQfinish(conn);
356  exit(1);
357  }
360  PQgetvalue(res, 0, 1),
361  PQgetvalue(res, 0, 0)));
362  appendPQExpBufferStr(buf, columns);
363  PQclear(res);
364  termPQExpBuffer(&sql);
365  pg_free(table);
366 
368  progname, echo));
369 }
void executeCommand(PGconn *conn, const char *query, const char *progname, bool echo)
Definition: common.c:212
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
static void split_table_columns_spec(const char *spec, int encoding, char **table, const char **columns)
Definition: common.c:269
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6106
const char * progname
Definition: pg_standby.c:37
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6176
PGresult * executeQuery(PGconn *conn, const char *query, const char *progname, bool echo)
Definition: common.c:185
#define ngettext(s, p, n)
Definition: c.h:1022
void PQclear(PGresult *res)
Definition: fe-exec.c:671
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
const char * fmtQualifiedId(int remoteVersion, const char *schema, const char *id)
Definition: string_utils.c:150
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89

◆ connectDatabase()

PGconn* connectDatabase ( const char *  dbname,
const char *  pghost,
const char *  pgport,
const char *  pguser,
enum trivalue  prompt_password,
const char *  progname,
bool  echo,
bool  fail_ok,
bool  allow_password_reuse 
)

Definition at line 68 of file common.c.

References _, ALWAYS_SECURE_SEARCH_PATH_SQL, conn, CONNECTION_BAD, dbname, executeQuery(), have_password, password, pghost, pgport, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQerrorMessage(), PQfinish(), PQserverVersion(), PQstatus(), progname, simple_prompt(), TRI_NO, TRI_YES, and values.

Referenced by connectMaintenanceDatabase().

72 {
73  PGconn *conn;
74  bool new_pass;
75  static bool have_password = false;
76  static char password[100];
77 
78  if (!allow_password_reuse)
79  have_password = false;
80 
81  if (!have_password && prompt_password == TRI_YES)
82  {
83  simple_prompt("Password: ", password, sizeof(password), false);
84  have_password = true;
85  }
86 
87  /*
88  * Start the connection. Loop until we have a password if requested by
89  * backend.
90  */
91  do
92  {
93  const char *keywords[7];
94  const char *values[7];
95 
96  keywords[0] = "host";
97  values[0] = pghost;
98  keywords[1] = "port";
99  values[1] = pgport;
100  keywords[2] = "user";
101  values[2] = pguser;
102  keywords[3] = "password";
103  values[3] = have_password ? password : NULL;
104  keywords[4] = "dbname";
105  values[4] = dbname;
106  keywords[5] = "fallback_application_name";
107  values[5] = progname;
108  keywords[6] = NULL;
109  values[6] = NULL;
110 
111  new_pass = false;
112  conn = PQconnectdbParams(keywords, values, true);
113 
114  if (!conn)
115  {
116  fprintf(stderr, _("%s: could not connect to database %s: out of memory\n"),
117  progname, dbname);
118  exit(1);
119  }
120 
121  /*
122  * No luck? Trying asking (again) for a password.
123  */
124  if (PQstatus(conn) == CONNECTION_BAD &&
126  prompt_password != TRI_NO)
127  {
128  PQfinish(conn);
129  simple_prompt("Password: ", password, sizeof(password), false);
130  have_password = true;
131  new_pass = true;
132  }
133  } while (new_pass);
134 
135  /* check to see that the backend connection was successfully made */
136  if (PQstatus(conn) == CONNECTION_BAD)
137  {
138  if (fail_ok)
139  {
140  PQfinish(conn);
141  return NULL;
142  }
143  fprintf(stderr, _("%s: could not connect to database %s: %s"),
144  progname, dbname, PQerrorMessage(conn));
145  exit(1);
146  }
147 
148  if (PQserverVersion(conn) >= 70300)
150  progname, echo));
151 
152  return conn;
153 }
static char password[100]
Definition: streamutil.c:54
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6106
const char * progname
Definition: pg_standby.c:37
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:536
PGresult * executeQuery(PGconn *conn, const char *query, const char *progname, bool echo)
Definition: common.c:185
PGconn * conn
Definition: streamutil.c:55
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
char * pghost
Definition: pgbench.c:192
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static bool have_password
Definition: streamutil.c:53
char * dbname
Definition: streamutil.c:51
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6150
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6063
#define _(x)
Definition: elog.c:84
char * pgport
Definition: pgbench.c:193

◆ connectMaintenanceDatabase()

PGconn* connectMaintenanceDatabase ( const char *  maintenance_db,
const char *  pghost,
const char *  pgport,
const char *  pguser,
enum trivalue  prompt_password,
const char *  progname,
bool  echo 
)

Definition at line 159 of file common.c.

References conn, and connectDatabase().

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

163 {
164  PGconn *conn;
165 
166  /* If a maintenance database name was specified, just connect to it. */
167  if (maintenance_db)
168  return connectDatabase(maintenance_db, pghost, pgport, pguser,
169  prompt_password, progname, echo, false, false);
170 
171  /* Otherwise, try postgres first and then template1. */
172  conn = connectDatabase("postgres", pghost, pgport, pguser, prompt_password,
173  progname, echo, true, false);
174  if (!conn)
175  conn = connectDatabase("template1", pghost, pgport, pguser,
176  prompt_password, progname, echo, false, false);
177 
178  return conn;
179 }
const char * progname
Definition: pg_standby.c:37
PGconn * conn
Definition: streamutil.c:55
char * pghost
Definition: pgbench.c:192
PGconn * connectDatabase(const char *dbname, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool echo, bool fail_ok, bool allow_password_reuse)
Definition: common.c:68
char * pgport
Definition: pgbench.c:193

◆ executeCommand()

void executeCommand ( PGconn conn,
const char *  query,
const char *  progname,
bool  echo 
)

Definition at line 212 of file common.c.

References _, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), and PQresultStatus().

Referenced by appendQualifiedRelation().

214 {
215  PGresult *res;
216 
217  if (echo)
218  printf("%s\n", query);
219 
220  res = PQexec(conn, query);
221  if (!res ||
223  {
224  fprintf(stderr, _("%s: query failed: %s"),
225  progname, PQerrorMessage(conn));
226  fprintf(stderr, _("%s: query was: %s\n"),
227  progname, query);
228  PQfinish(conn);
229  exit(1);
230  }
231 
232  PQclear(res);
233 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
const char * progname
Definition: pg_standby.c:37
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define _(x)
Definition: elog.c:84

◆ executeMaintenanceCommand()

bool executeMaintenanceCommand ( PGconn conn,
const char *  query,
bool  echo 
)

Definition at line 242 of file common.c.

References PGRES_COMMAND_OK, PQclear(), PQexec(), PQresultStatus(), ResetCancelConn(), and SetCancelConn().

Referenced by cluster_one_database(), reindex_one_database(), reindex_system_catalogs(), and run_vacuum_command().

243 {
244  PGresult *res;
245  bool r;
246 
247  if (echo)
248  printf("%s\n", query);
249 
250  SetCancelConn(conn);
251  res = PQexec(conn, query);
252  ResetCancelConn();
253 
254  r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
255 
256  if (res)
257  PQclear(res);
258 
259  return r;
260 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
void SetCancelConn(void)
Definition: common.c:446
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
void ResetCancelConn(void)
Definition: common.c:476

◆ executeQuery()

PGresult* executeQuery ( PGconn conn,
const char *  query,
const char *  progname,
bool  echo 
)

Definition at line 185 of file common.c.

References _, PGRES_TUPLES_OK, PQerrorMessage(), PQexec(), PQfinish(), and PQresultStatus().

Referenced by appendQualifiedRelation(), and connectDatabase().

186 {
187  PGresult *res;
188 
189  if (echo)
190  printf("%s\n", query);
191 
192  res = PQexec(conn, query);
193  if (!res ||
195  {
196  fprintf(stderr, _("%s: query failed: %s"),
197  progname, PQerrorMessage(conn));
198  fprintf(stderr, _("%s: query was: %s\n"),
199  progname, query);
200  PQfinish(conn);
201  exit(1);
202  }
203 
204  return res;
205 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
const char * progname
Definition: pg_standby.c:37
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define _(x)
Definition: elog.c:84

◆ handle_help_version_opts()

void handle_help_version_opts ( int  argc,
char *  argv[],
const char *  fixed_progname,
help_handler  hlp 
)

Definition at line 37 of file common.c.

References get_progname().

Referenced by main().

39 {
40  if (argc > 1)
41  {
42  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
43  {
44  hlp(get_progname(argv[0]));
45  exit(0);
46  }
47  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
48  {
49  printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname);
50  exit(0);
51  }
52  }
53 }
const char * get_progname(const char *argv0)
Definition: path.c:453

◆ ResetCancelConn()

void ResetCancelConn ( void  )

Definition at line 476 of file common.c.

References cancelConn, and PQfreeCancel().

Referenced by CheckConnection(), do_lo_export(), do_lo_import(), do_lo_unlink(), executeMaintenanceCommand(), GetIdleSlot(), GetQueryResult(), ProcessResult(), PSQLexec(), PSQLexecWatch(), and SendQuery().

477 {
478  PGcancel *oldCancelConn;
479 
480 #ifdef WIN32
481  EnterCriticalSection(&cancelConnLock);
482 #endif
483 
484  oldCancelConn = cancelConn;
485  /* be sure handle_sigint doesn't use pointer while freeing */
486  cancelConn = NULL;
487 
488  if (oldCancelConn != NULL)
489  PQfreeCancel(oldCancelConn);
490 
491 #ifdef WIN32
492  LeaveCriticalSection(&cancelConnLock);
493 #endif
494 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3782
static PGcancel *volatile cancelConn
Definition: common.c:278

◆ SetCancelConn()

void SetCancelConn ( PGconn conn)

Definition at line 414 of file common.c.

References cancelConn, PQfreeCancel(), and PQgetCancel().

415 {
416  PGcancel *oldCancelConn;
417 
418 #ifdef WIN32
419  EnterCriticalSection(&cancelConnLock);
420 #endif
421 
422  /* Free the old one if we have one */
423  oldCancelConn = cancelConn;
424 
425  /* be sure handle_sigint doesn't use pointer while freeing */
426  cancelConn = NULL;
427 
428  if (oldCancelConn != NULL)
429  PQfreeCancel(oldCancelConn);
430 
431  cancelConn = PQgetCancel(conn);
432 
433 #ifdef WIN32
434  LeaveCriticalSection(&cancelConnLock);
435 #endif
436 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3782
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:3759
static PGcancel *volatile cancelConn
Definition: common.c:25

◆ setup_cancel_handler()

void setup_cancel_handler ( void  )

Definition at line 332 of file common.c.

References _, cancel_pressed, CancelRequested, FALSE, handle_sigint(), PQcancel(), pqsignal(), setup_cancel_handler(), TRUE, and write_stderr.

Referenced by setup_cancel_handler().

333 {
334  pqsignal(SIGINT, handle_sigint);
335 }
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static void handle_sigint(SIGNAL_ARGS)
Definition: common.c:301

◆ yesno_prompt()

bool yesno_prompt ( const char *  question)

Definition at line 382 of file common.c.

References _, PG_NOLETTER, PG_YESLETTER, simple_prompt(), and snprintf().

Referenced by main().

383 {
384  char prompt[256];
385 
386  /*------
387  translator: This is a question followed by the translated options for
388  "yes" and "no". */
389  snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "),
390  _(question), _(PG_YESLETTER), _(PG_NOLETTER));
391 
392  for (;;)
393  {
394  char resp[10];
395 
396  simple_prompt(prompt, resp, sizeof(resp), true);
397 
398  if (strcmp(resp, _(PG_YESLETTER)) == 0)
399  return true;
400  if (strcmp(resp, _(PG_NOLETTER)) == 0)
401  return false;
402 
403  printf(_("Please answer \"%s\" or \"%s\".\n"),
405  }
406 }
#define PG_YESLETTER
Definition: common.c:377
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PG_NOLETTER
Definition: common.c:379
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
#define _(x)
Definition: elog.c:84

Variable Documentation

◆ CancelRequested

bool CancelRequested

Definition at line 26 of file common.c.

Referenced by handle_sigint(), select_loop(), setup_cancel_handler(), and vacuum_one_database().