PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
server.c File Reference
#include "postgres_fe.h"
#include "fe_utils/string_utils.h"
#include "pg_upgrade.h"
Include dependency graph for server.c:

Go to the source code of this file.

Functions

static PGconnget_db_conn (ClusterInfo *cluster, const char *db_name)
 
PGconnconnectToServer (ClusterInfo *cluster, const char *db_name)
 
char * cluster_conn_opts (ClusterInfo *cluster)
 
PGresultexecuteQueryOrDie (PGconn *conn, const char *fmt,...)
 
uint32 get_major_server_version (ClusterInfo *cluster)
 
static void stop_postmaster_atexit (void)
 
bool start_postmaster (ClusterInfo *cluster, bool throw_error)
 
void stop_postmaster (bool fast)
 
void check_pghost_envvar (void)
 

Function Documentation

void check_pghost_envvar ( void  )

Definition at line 344 of file server.c.

References _PQconninfoOption::envvar, _PQconninfoOption::keyword, pg_fatal(), PQconndefaults(), PQconninfoFree(), and value.

Referenced by setup().

345 {
347  PQconninfoOption *start;
348 
349  /* Get valid libpq env vars from the PQconndefaults function */
350 
351  start = PQconndefaults();
352 
353  if (!start)
354  pg_fatal("out of memory\n");
355 
356  for (option = start; option->keyword != NULL; option++)
357  {
358  if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
359  strcmp(option->envvar, "PGHOSTADDR") == 0))
360  {
361  const char *value = getenv(option->envvar);
362 
363  if (value && strlen(value) > 0 &&
364  /* check for 'local' host values */
365  (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
366  strcmp(value, "::1") != 0 && value[0] != '/'))
367  pg_fatal("libpq environment variable %s has a non-local server value: %s\n",
368  option->envvar, value);
369  }
370  }
371 
372  /* Free the memory that libpq allocated on our behalf */
373  PQconninfoFree(start);
374 }
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
static struct @121 value
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:5947
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1224
char* cluster_conn_opts ( ClusterInfo cluster)

Definition at line 88 of file server.c.

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendShellString(), buf, createPQExpBuffer(), PQExpBufferData::data, os_info, ClusterInfo::port, resetPQExpBuffer(), ClusterInfo::sockdir, and OSInfo::user.

Referenced by create_new_objects(), generate_old_dump(), prepare_new_cluster(), and prepare_new_databases().

89 {
90  static PQExpBuffer buf;
91 
92  if (buf == NULL)
93  buf = createPQExpBuffer();
94  else
95  resetPQExpBuffer(buf);
96 
97  if (cluster->sockdir)
98  {
99  appendPQExpBufferStr(buf, "--host ");
100  appendShellString(buf, cluster->sockdir);
101  appendPQExpBufferChar(buf, ' ');
102  }
103  appendPQExpBuffer(buf, "--port %d --username ", cluster->port);
105 
106  return buf->data;
107 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
unsigned short port
Definition: pg_upgrade.h:273
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:434
char * sockdir
Definition: pg_upgrade.h:272
OSInfo os_info
Definition: pg_upgrade.c:58
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
char * user
Definition: pg_upgrade.h:311
PGconn* connectToServer ( ClusterInfo cluster,
const char *  db_name 
)

Definition at line 27 of file server.c.

References _, conn, CONNECTION_OK, get_db_conn(), pg_log(), PG_REPORT, PQerrorMessage(), PQfinish(), and PQstatus().

Referenced by check_for_isn_and_int8_passing_mismatch(), check_for_jsonb_9_4_usage(), check_for_pg_role_prefix(), check_for_prepared_transactions(), check_for_reg_data_type_usage(), check_is_install_user(), check_loadable_libraries(), check_proper_datallowconn(), get_db_infos(), get_loadable_libraries(), get_rel_infos(), get_tablespace_paths(), new_9_0_populate_pg_largeobject_metadata(), old_9_3_check_for_line_data_type_usage(), old_9_6_check_for_unknown_data_type_usage(), old_9_6_invalidate_hash_indexes(), and set_frozenxids().

28 {
29  PGconn *conn = get_db_conn(cluster, db_name);
30 
31  if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
32  {
33  pg_log(PG_REPORT, "connection to database failed: %s",
34  PQerrorMessage(conn));
35 
36  if (conn)
37  PQfinish(conn);
38 
39  printf(_("Failure, exiting\n"));
40  exit(1);
41  }
42 
43  return conn;
44 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
PGconn * conn
Definition: streamutil.c:46
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
static PGconn * get_db_conn(ClusterInfo *cluster, const char *db_name)
Definition: server.c:53
#define _(x)
Definition: elog.c:84
PGresult* executeQueryOrDie ( PGconn conn,
const char *  fmt,
  ... 
)

Definition at line 118 of file server.c.

References _, generate_unaccent_rules::args, pg_log(), PG_REPORT, PG_VERBOSE, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), QUERY_ALLOC, status(), and vsnprintf().

119 {
120  static char query[QUERY_ALLOC];
121  va_list args;
122  PGresult *result;
124 
125  va_start(args, fmt);
126  vsnprintf(query, sizeof(query), fmt, args);
127  va_end(args);
128 
129  pg_log(PG_VERBOSE, "executing: %s\n", query);
130  result = PQexec(conn, query);
131  status = PQresultStatus(result);
132 
133  if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
134  {
135  pg_log(PG_REPORT, "SQL command failed\n%s\n%s", query,
136  PQerrorMessage(conn));
137  PQclear(result);
138  PQfinish(conn);
139  printf(_("Failure, exiting\n"));
140  exit(1);
141  }
142  else
143  return result;
144 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
ExecStatusType
Definition: libpq-fe.h:82
#define QUERY_ALLOC
Definition: logging.c:26
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
int int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define _(x)
Definition: elog.c:84
static PGconn * get_db_conn ( ClusterInfo cluster,
const char *  db_name 
)
static

Definition at line 53 of file server.c.

References appendConnStrVal(), appendPQExpBuffer(), appendPQExpBufferStr(), conn, PQExpBufferData::data, initPQExpBuffer(), os_info, ClusterInfo::port, PQconnectdb(), ClusterInfo::sockdir, termPQExpBuffer(), and OSInfo::user.

Referenced by connectToServer(), and start_postmaster().

54 {
55  PQExpBufferData conn_opts;
56  PGconn *conn;
57 
58  /* Build connection string with proper quoting */
59  initPQExpBuffer(&conn_opts);
60  appendPQExpBufferStr(&conn_opts, "dbname=");
61  appendConnStrVal(&conn_opts, db_name);
62  appendPQExpBufferStr(&conn_opts, " user=");
63  appendConnStrVal(&conn_opts, os_info.user);
64  appendPQExpBuffer(&conn_opts, " port=%d", cluster->port);
65  if (cluster->sockdir)
66  {
67  appendPQExpBufferStr(&conn_opts, " host=");
68  appendConnStrVal(&conn_opts, cluster->sockdir);
69  }
70 
71  conn = PQconnectdb(conn_opts.data);
72  termPQExpBuffer(&conn_opts);
73  return conn;
74 }
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:551
unsigned short port
Definition: pg_upgrade.h:273
PGconn * conn
Definition: streamutil.c:46
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
char * sockdir
Definition: pg_upgrade.h:272
OSInfo os_info
Definition: pg_upgrade.c:58
char * user
Definition: pg_upgrade.h:311
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:585
uint32 get_major_server_version ( ClusterInfo cluster)

Definition at line 155 of file server.c.

References ClusterInfo::major_version_str, MAXPGPATH, pg_fatal(), ClusterInfo::pgdata, and snprintf().

Referenced by check_cluster_versions(), and check_data_dir().

156 {
157  FILE *version_fd;
158  char ver_filename[MAXPGPATH];
159  int integer_version = 0;
160  int fractional_version = 0;
161 
162  snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION",
163  cluster->pgdata);
164  if ((version_fd = fopen(ver_filename, "r")) == NULL)
165  pg_fatal("could not open version file: %s\n", ver_filename);
166 
167  if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 ||
168  sscanf(cluster->major_version_str, "%d.%d", &integer_version,
169  &fractional_version) < 1)
170  pg_fatal("could not parse PG_VERSION file from %s\n", cluster->pgdata);
171 
172  fclose(version_fd);
173 
174  return (100 * integer_version + fractional_version) * 100;
175 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
char major_version_str[64]
Definition: pg_upgrade.h:275
#define MAXPGPATH
char * pgdata
Definition: pg_upgrade.h:266
bool start_postmaster ( ClusterInfo cluster,
bool  throw_error 
)

Definition at line 186 of file server.c.

References BINARY_UPGRADE_SERVER_FLAG_CAT_VER, ClusterInfo::bindir, ControlData::cat_ver, cluster(), conn, CONNECTION_OK, ClusterInfo::controldata, exec_prog(), get_db_conn(), GET_MAJOR_VERSION, ClusterInfo::major_version, MAXPGPATH, new_cluster, old_cluster, os_info, pg_fatal(), pg_log(), PG_REPORT, ClusterInfo::pgconfig, ClusterInfo::pgopts, ClusterInfo::port, PQerrorMessage(), PQfinish(), PQstatus(), OSInfo::running_cluster, SERVER_LOG_FILE, SERVER_START_LOG_FILE, snprintf(), ClusterInfo::sockdir, and stop_postmaster_atexit().

187 {
188  char cmd[MAXPGPATH * 4 + 1000];
189  PGconn *conn;
190  bool pg_ctl_return = false;
191  char socket_string[MAXPGPATH + 200];
192 
193  static bool exit_hook_registered = false;
194 
195  if (!exit_hook_registered)
196  {
197  atexit(stop_postmaster_atexit);
198  exit_hook_registered = true;
199  }
200 
201  socket_string[0] = '\0';
202 
203 #ifdef HAVE_UNIX_SOCKETS
204  /* prevent TCP/IP connections, restrict socket access */
205  strcat(socket_string,
206  " -c listen_addresses='' -c unix_socket_permissions=0700");
207 
208  /* Have a sockdir? Tell the postmaster. */
209  if (cluster->sockdir)
210  snprintf(socket_string + strlen(socket_string),
211  sizeof(socket_string) - strlen(socket_string),
212  " -c %s='%s'",
213  (GET_MAJOR_VERSION(cluster->major_version) < 903) ?
214  "unix_socket_directory" : "unix_socket_directories",
215  cluster->sockdir);
216 #endif
217 
218  /*
219  * Since PG 9.1, we have used -b to disable autovacuum. For earlier
220  * releases, setting autovacuum=off disables cleanup vacuum and analyze,
221  * but freeze vacuums can still happen, so we set
222  * autovacuum_freeze_max_age to its maximum.
223  * (autovacuum_multixact_freeze_max_age was introduced after 9.1, so there
224  * is no need to set that.) We assume all datfrozenxid and relfrozenxid
225  * values are less than a gap of 2000000000 from the current xid counter,
226  * so autovacuum will not touch them.
227  *
228  * Turn off durability requirements to improve object creation speed, and
229  * we only modify the new cluster, so only use it there. If there is a
230  * crash, the new cluster has to be recreated anyway. fsync=off is a big
231  * win on ext4.
232  */
233  snprintf(cmd, sizeof(cmd),
234  "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" -o \"-p %d%s%s %s%s\" start",
235  cluster->bindir, SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
236  (cluster->controldata.cat_ver >=
238  " -c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
239  (cluster == &new_cluster) ?
240  " -c synchronous_commit=off -c fsync=off -c full_page_writes=off" : "",
241  cluster->pgopts ? cluster->pgopts : "", socket_string);
242 
243  /*
244  * Don't throw an error right away, let connecting throw the error because
245  * it might supply a reason for the failure.
246  */
247  pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
248  /* pass both file names if they differ */
249  (strcmp(SERVER_LOG_FILE,
250  SERVER_START_LOG_FILE) != 0) ?
251  SERVER_LOG_FILE : NULL,
252  false,
253  "%s", cmd);
254 
255  /* Did it fail and we are just testing if the server could be started? */
256  if (!pg_ctl_return && !throw_error)
257  return false;
258 
259  /*
260  * We set this here to make sure atexit() shuts down the server, but only
261  * if we started the server successfully. We do it before checking for
262  * connectivity in case the server started but there is a connectivity
263  * failure. If pg_ctl did not return success, we will exit below.
264  *
265  * Pre-9.1 servers do not have PQping(), so we could be leaving the server
266  * running if authentication was misconfigured, so someday we might went
267  * to be more aggressive about doing server shutdowns even if pg_ctl
268  * fails, but now (2013-08-14) it seems prudent to be cautious. We don't
269  * want to shutdown a server that might have been accidentally started
270  * during the upgrade.
271  */
272  if (pg_ctl_return)
274 
275  /*
276  * pg_ctl -w might have failed because the server couldn't be started, or
277  * there might have been a connection problem in _checking_ if the server
278  * has started. Therefore, even if pg_ctl failed, we continue and test
279  * for connectivity in case we get a connection reason for the failure.
280  */
281  if ((conn = get_db_conn(cluster, "template1")) == NULL ||
282  PQstatus(conn) != CONNECTION_OK)
283  {
284  pg_log(PG_REPORT, "\nconnection to database failed: %s",
285  PQerrorMessage(conn));
286  if (conn)
287  PQfinish(conn);
288  if (cluster == &old_cluster)
289  pg_fatal("could not connect to source postmaster started with the command:\n"
290  "%s\n",
291  cmd);
292  else
293  pg_fatal("could not connect to target postmaster started with the command:\n"
294  "%s\n",
295  cmd);
296  }
297  PQfinish(conn);
298 
299  /*
300  * If pg_ctl failed, and the connection didn't fail, and throw_error is
301  * enabled, fail now. This could happen if the server was already
302  * running.
303  */
304  if (!pg_ctl_return)
305  {
306  if (cluster == &old_cluster)
307  pg_fatal("pg_ctl failed to start the source server, or connection failed\n");
308  else
309  pg_fatal("pg_ctl failed to start the target server, or connection failed\n");
310  }
311 
312  return true;
313 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
uint32 major_version
Definition: pg_upgrade.h:274
ControlData controldata
Definition: pg_upgrade.h:264
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
unsigned short port
Definition: pg_upgrade.h:273
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
char * pgopts
Definition: pg_upgrade.h:270
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
char * pgconfig
Definition: pg_upgrade.h:267
PGconn * conn
Definition: streamutil.c:46
#define MAXPGPATH
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
void cluster(ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:106
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
char * bindir
Definition: pg_upgrade.h:269
uint32 cat_ver
Definition: pg_upgrade.h:210
static void stop_postmaster_atexit(void)
Definition: server.c:179
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:36
ClusterInfo * running_cluster
Definition: pg_upgrade.h:317
#define SERVER_START_LOG_FILE
Definition: pg_upgrade.h:59
bool exec_prog(const char *log_file, const char *opt_log_file, bool throw_error, const char *fmt,...)
Definition: exec.c:77
char * sockdir
Definition: pg_upgrade.h:272
OSInfo os_info
Definition: pg_upgrade.c:58
#define BINARY_UPGRADE_SERVER_FLAG_CAT_VER
Definition: pg_upgrade.h:102
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
static PGconn * get_db_conn(ClusterInfo *cluster, const char *db_name)
Definition: server.c:53
void stop_postmaster ( bool  fast)

Definition at line 317 of file server.c.

References ClusterInfo::bindir, cluster(), exec_prog(), new_cluster, old_cluster, os_info, ClusterInfo::pgconfig, ClusterInfo::pgopts, OSInfo::running_cluster, and SERVER_STOP_LOG_FILE.

Referenced by check_and_dump_old_cluster(), issue_warnings_and_set_wal_level(), main(), report_clusters_compatible(), setup(), and stop_postmaster_atexit().

318 {
320 
322  cluster = &old_cluster;
323  else if (os_info.running_cluster == &new_cluster)
324  cluster = &new_cluster;
325  else
326  return; /* no cluster running */
327 
328  exec_prog(SERVER_STOP_LOG_FILE, NULL, !fast,
329  "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
330  cluster->bindir, cluster->pgconfig,
331  cluster->pgopts ? cluster->pgopts : "",
332  fast ? "-m fast" : "-m smart");
333 
334  os_info.running_cluster = NULL;
335 }
#define SERVER_STOP_LOG_FILE
Definition: pg_upgrade.h:60
char * pgopts
Definition: pg_upgrade.h:270
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
char * pgconfig
Definition: pg_upgrade.h:267
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
void cluster(ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:106
char * bindir
Definition: pg_upgrade.h:269
ClusterInfo * running_cluster
Definition: pg_upgrade.h:317
bool exec_prog(const char *log_file, const char *opt_log_file, bool throw_error, const char *fmt,...)
Definition: exec.c:77
OSInfo os_info
Definition: pg_upgrade.c:58
static void stop_postmaster_atexit ( void  )
static

Definition at line 179 of file server.c.

References stop_postmaster().

Referenced by start_postmaster().

180 {
181  stop_postmaster(true);
182 }
void stop_postmaster(bool fast)
Definition: server.c:317