PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
server.c
Go to the documentation of this file.
1 /*
2  * server.c
3  *
4  * database server functions
5  *
6  * Copyright (c) 2010-2017, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/server.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include "fe_utils/string_utils.h"
13 #include "pg_upgrade.h"
14 
15 
16 static PGconn *get_db_conn(ClusterInfo *cluster, const char *db_name);
17 
18 
19 /*
20  * connectToServer()
21  *
22  * Connects to the desired database on the designated server.
23  * If the connection attempt fails, this function logs an error
24  * message and calls exit() to kill the program.
25  */
26 PGconn *
27 connectToServer(ClusterInfo *cluster, const char *db_name)
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\n",
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 }
45 
46 
47 /*
48  * get_db_conn()
49  *
50  * get database connection, using named database + standard params for cluster
51  */
52 static PGconn *
53 get_db_conn(ClusterInfo *cluster, const char *db_name)
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 }
75 
76 
77 /*
78  * cluster_conn_opts()
79  *
80  * Return standard command-line options for connecting to this cluster when
81  * using psql, pg_dump, etc. Ideally this would match what get_db_conn()
82  * sets, but the utilities we need aren't very consistent about the treatment
83  * of database name options, so we leave that out.
84  *
85  * Result is valid until the next call to this function.
86  */
87 char *
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 }
108 
109 
110 /*
111  * executeQueryOrDie()
112  *
113  * Formats a query string from the given arguments and executes the
114  * resulting query. If the query fails, this function logs an error
115  * message and calls exit() to kill the program.
116  */
117 PGresult *
118 executeQueryOrDie(PGconn *conn, const char *fmt,...)
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\n", 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 }
145 
146 
147 /*
148  * get_major_server_version()
149  *
150  * gets the version (in unsigned int form) for the given datadir. Assumes
151  * that datadir is an absolute path to a valid pgdata directory. The version
152  * is retrieved by reading the PG_VERSION file.
153  */
154 uint32
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 get version from %s\n", cluster->pgdata);
171 
172  fclose(version_fd);
173 
174  return (100 * integer_version + fractional_version) * 100;
175 }
176 
177 
178 static void
180 {
181  stop_postmaster(true);
182 }
183 
184 
185 bool
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) ?
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\n",
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 }
314 
315 
316 void
317 stop_postmaster(bool fast)
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 
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 
335 }
336 
337 
338 /*
339  * check_pghost_envvar()
340  *
341  * Tests that PGHOST does not point to a non-local server
342  */
343 void
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 }
char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:88
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
uint32 major_version
Definition: pg_upgrade.h:270
void stop_postmaster(bool fast)
Definition: server.c:317
ControlData controldata
Definition: pg_upgrade.h:260
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:551
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
unsigned short port
Definition: pg_upgrade.h:269
return result
Definition: formatting.c:1633
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
ExecStatusType
Definition: libpq-fe.h:82
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
#define SERVER_STOP_LOG_FILE
Definition: pg_upgrade.h:60
#define QUERY_ALLOC
Definition: logging.c:26
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:27
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
char * pgopts
Definition: pg_upgrade.h:266
char major_version_str[64]
Definition: pg_upgrade.h:271
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
int int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
char * pgconfig
Definition: pg_upgrade.h:263
PGconn * conn
Definition: streamutil.c:43
#define MAXPGPATH
static struct @121 value
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:5947
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
unsigned int uint32
Definition: c.h:268
void cluster(ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:106
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...)
Definition: server.c:118
void check_pghost_envvar(void)
Definition: server.c:344
char * bindir
Definition: pg_upgrade.h:265
bool start_postmaster(ClusterInfo *cluster, bool throw_error)
Definition: server.c:186
uint32 cat_ver
Definition: pg_upgrade.h:206
static void stop_postmaster_atexit(void)
Definition: server.c:179
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
void PQclear(PGresult *res)
Definition: fe-exec.c:650
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:434
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:36
#define NULL
Definition: c.h:229
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1224
ClusterInfo * running_cluster
Definition: pg_upgrade.h:313
#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 * pgdata
Definition: pg_upgrade.h:262
char * sockdir
Definition: pg_upgrade.h:268
OSInfo os_info
Definition: pg_upgrade.c:58
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
#define BINARY_UPGRADE_SERVER_FLAG_CAT_VER
Definition: pg_upgrade.h:101
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
uint32 get_major_server_version(ClusterInfo *cluster)
Definition: server.c:155
char * user
Definition: pg_upgrade.h:307
static PGconn * get_db_conn(ClusterInfo *cluster, const char *db_name)
Definition: server.c:53
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:585