PostgreSQL Source Code  git master
server.c
Go to the documentation of this file.
1 /*
2  * server.c
3  *
4  * database server functions
5  *
6  * Copyright (c) 2010-2022, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/server.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include "common/connect.h"
13 #include "fe_utils/string_utils.h"
14 #include "libpq/pqcomm.h"
15 #include "pg_upgrade.h"
16 
17 static PGconn *get_db_conn(ClusterInfo *cluster, const char *db_name);
18 
19 
20 /*
21  * connectToServer()
22  *
23  * Connects to the desired database on the designated server.
24  * If the connection attempt fails, this function logs an error
25  * message and calls exit() to kill the program.
26  */
27 PGconn *
28 connectToServer(ClusterInfo *cluster, const char *db_name)
29 {
30  PGconn *conn = get_db_conn(cluster, db_name);
31 
32  if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
33  {
35 
36  if (conn)
37  PQfinish(conn);
38 
39  printf(_("Failure, exiting\n"));
40  exit(1);
41  }
42 
44 
45  return conn;
46 }
47 
48 
49 /*
50  * get_db_conn()
51  *
52  * get database connection, using named database + standard params for cluster
53  *
54  * Caller must check for connection failure!
55  */
56 static PGconn *
57 get_db_conn(ClusterInfo *cluster, const char *db_name)
58 {
59  PQExpBufferData conn_opts;
60  PGconn *conn;
61 
62  /* Build connection string with proper quoting */
63  initPQExpBuffer(&conn_opts);
64  appendPQExpBufferStr(&conn_opts, "dbname=");
65  appendConnStrVal(&conn_opts, db_name);
66  appendPQExpBufferStr(&conn_opts, " user=");
67  appendConnStrVal(&conn_opts, os_info.user);
68  appendPQExpBuffer(&conn_opts, " port=%d", cluster->port);
69  if (cluster->sockdir)
70  {
71  appendPQExpBufferStr(&conn_opts, " host=");
72  appendConnStrVal(&conn_opts, cluster->sockdir);
73  }
74 
75  conn = PQconnectdb(conn_opts.data);
76  termPQExpBuffer(&conn_opts);
77  return conn;
78 }
79 
80 
81 /*
82  * cluster_conn_opts()
83  *
84  * Return standard command-line options for connecting to this cluster when
85  * using psql, pg_dump, etc. Ideally this would match what get_db_conn()
86  * sets, but the utilities we need aren't very consistent about the treatment
87  * of database name options, so we leave that out.
88  *
89  * Result is valid until the next call to this function.
90  */
91 char *
93 {
94  static PQExpBuffer buf;
95 
96  if (buf == NULL)
98  else
100 
101  if (cluster->sockdir)
102  {
103  appendPQExpBufferStr(buf, "--host ");
104  appendShellString(buf, cluster->sockdir);
106  }
107  appendPQExpBuffer(buf, "--port %d --username ", cluster->port);
109 
110  return buf->data;
111 }
112 
113 
114 /*
115  * executeQueryOrDie()
116  *
117  * Formats a query string from the given arguments and executes the
118  * resulting query. If the query fails, this function logs an error
119  * message and calls exit() to kill the program.
120  */
121 PGresult *
122 executeQueryOrDie(PGconn *conn, const char *fmt,...)
123 {
124  static char query[QUERY_ALLOC];
125  va_list args;
126  PGresult *result;
128 
129  va_start(args, fmt);
130  vsnprintf(query, sizeof(query), fmt, args);
131  va_end(args);
132 
133  pg_log(PG_VERBOSE, "executing: %s", query);
134  result = PQexec(conn, query);
135  status = PQresultStatus(result);
136 
138  {
139  pg_log(PG_REPORT, "SQL command failed\n%s\n%s", query,
141  PQclear(result);
142  PQfinish(conn);
143  printf(_("Failure, exiting\n"));
144  exit(1);
145  }
146  else
147  return result;
148 }
149 
150 
151 /*
152  * get_major_server_version()
153  *
154  * gets the version (in unsigned int form) for the given datadir. Assumes
155  * that datadir is an absolute path to a valid pgdata directory. The version
156  * is retrieved by reading the PG_VERSION file.
157  */
158 uint32
160 {
161  FILE *version_fd;
162  char ver_filename[MAXPGPATH];
163  int v1 = 0,
164  v2 = 0;
165 
166  snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION",
167  cluster->pgdata);
168  if ((version_fd = fopen(ver_filename, "r")) == NULL)
169  pg_fatal("could not open version file \"%s\": %m", ver_filename);
170 
171  if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 ||
172  sscanf(cluster->major_version_str, "%d.%d", &v1, &v2) < 1)
173  pg_fatal("could not parse version file \"%s\"", ver_filename);
174 
175  fclose(version_fd);
176 
177  if (v1 < 10)
178  {
179  /* old style, e.g. 9.6.1 */
180  return v1 * 10000 + v2 * 100;
181  }
182  else
183  {
184  /* new style, e.g. 10.1 */
185  return v1 * 10000;
186  }
187 }
188 
189 
190 static void
192 {
193  stop_postmaster(true);
194 }
195 
196 
197 bool
198 start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
199 {
200  char cmd[MAXPGPATH * 4 + 1000];
201  PGconn *conn;
202  bool pg_ctl_return = false;
203  char socket_string[MAXPGPATH + 200];
204 
205  static bool exit_hook_registered = false;
206 
207  if (!exit_hook_registered)
208  {
209  atexit(stop_postmaster_atexit);
210  exit_hook_registered = true;
211  }
212 
213  socket_string[0] = '\0';
214 
215 #if !defined(WIN32)
216  /* prevent TCP/IP connections, restrict socket access */
217  strcat(socket_string,
218  " -c listen_addresses='' -c unix_socket_permissions=0700");
219 
220  /* Have a sockdir? Tell the postmaster. */
221  if (cluster->sockdir)
222  snprintf(socket_string + strlen(socket_string),
223  sizeof(socket_string) - strlen(socket_string),
224  " -c %s='%s'",
225  (GET_MAJOR_VERSION(cluster->major_version) <= 902) ?
226  "unix_socket_directory" : "unix_socket_directories",
227  cluster->sockdir);
228 #endif
229 
230  /*
231  * Use -b to disable autovacuum.
232  *
233  * Turn off durability requirements to improve object creation speed, and
234  * we only modify the new cluster, so only use it there. If there is a
235  * crash, the new cluster has to be recreated anyway. fsync=off is a big
236  * win on ext4.
237  *
238  * Force vacuum_defer_cleanup_age to 0 on the new cluster, so that
239  * vacuumdb --freeze actually freezes the tuples.
240  */
241  snprintf(cmd, sizeof(cmd),
242  "\"%s/pg_ctl\" -w -l \"%s/%s\" -D \"%s\" -o \"-p %d -b%s %s%s\" start",
243  cluster->bindir,
245  SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
246  (cluster == &new_cluster) ?
247  " -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c vacuum_defer_cleanup_age=0" : "",
248  cluster->pgopts ? cluster->pgopts : "", socket_string);
249 
250  /*
251  * Don't throw an error right away, let connecting throw the error because
252  * it might supply a reason for the failure.
253  */
254  pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
255  /* pass both file names if they differ */
256  (strcmp(SERVER_LOG_FILE,
257  SERVER_START_LOG_FILE) != 0) ?
258  SERVER_LOG_FILE : NULL,
259  report_and_exit_on_error, false,
260  "%s", cmd);
261 
262  /* Did it fail and we are just testing if the server could be started? */
263  if (!pg_ctl_return && !report_and_exit_on_error)
264  return false;
265 
266  /*
267  * We set this here to make sure atexit() shuts down the server, but only
268  * if we started the server successfully. We do it before checking for
269  * connectivity in case the server started but there is a connectivity
270  * failure. If pg_ctl did not return success, we will exit below.
271  *
272  * Pre-9.1 servers do not have PQping(), so we could be leaving the server
273  * running if authentication was misconfigured, so someday we might went
274  * to be more aggressive about doing server shutdowns even if pg_ctl
275  * fails, but now (2013-08-14) it seems prudent to be cautious. We don't
276  * want to shutdown a server that might have been accidentally started
277  * during the upgrade.
278  */
279  if (pg_ctl_return)
281 
282  /*
283  * pg_ctl -w might have failed because the server couldn't be started, or
284  * there might have been a connection problem in _checking_ if the server
285  * has started. Therefore, even if pg_ctl failed, we continue and test
286  * for connectivity in case we get a connection reason for the failure.
287  */
288  if ((conn = get_db_conn(cluster, "template1")) == NULL ||
290  {
291  pg_log(PG_REPORT, "\n%s", PQerrorMessage(conn));
292  if (conn)
293  PQfinish(conn);
294  if (cluster == &old_cluster)
295  pg_fatal("could not connect to source postmaster started with the command:\n"
296  "%s",
297  cmd);
298  else
299  pg_fatal("could not connect to target postmaster started with the command:\n"
300  "%s",
301  cmd);
302  }
303  PQfinish(conn);
304 
305  /*
306  * If pg_ctl failed, and the connection didn't fail, and
307  * report_and_exit_on_error is enabled, fail now. This could happen if
308  * the server was already running.
309  */
310  if (!pg_ctl_return)
311  {
312  if (cluster == &old_cluster)
313  pg_fatal("pg_ctl failed to start the source server, or connection failed");
314  else
315  pg_fatal("pg_ctl failed to start the target server, or connection failed");
316  }
317 
318  return true;
319 }
320 
321 
322 void
323 stop_postmaster(bool in_atexit)
324 {
326 
328  cluster = &old_cluster;
329  else if (os_info.running_cluster == &new_cluster)
330  cluster = &new_cluster;
331  else
332  return; /* no cluster running */
333 
334  exec_prog(SERVER_STOP_LOG_FILE, NULL, !in_atexit, !in_atexit,
335  "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
336  cluster->bindir, cluster->pgconfig,
337  cluster->pgopts ? cluster->pgopts : "",
338  in_atexit ? "-m fast" : "-m smart");
339 
340  os_info.running_cluster = NULL;
341 }
342 
343 
344 /*
345  * check_pghost_envvar()
346  *
347  * Tests that PGHOST does not point to a non-local server
348  */
349 void
351 {
353  PQconninfoOption *start;
354 
355  /* Get valid libpq env vars from the PQconndefaults function */
356 
357  start = PQconndefaults();
358 
359  if (!start)
360  pg_fatal("out of memory");
361 
362  for (option = start; option->keyword != NULL; option++)
363  {
364  if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
365  strcmp(option->envvar, "PGHOSTADDR") == 0))
366  {
367  const char *value = getenv(option->envvar);
368 
369  if (value && strlen(value) > 0 &&
370  /* check for 'local' host values */
371  (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
372  strcmp(value, "::1") != 0 && !is_unixsock_path(value)))
373  pg_fatal("libpq environment variable %s has a non-local server value: %s",
374  option->envvar, value);
375  }
376  }
377 
378  /* Free the memory that libpq allocated on our behalf */
379  PQconninfoFree(start);
380 }
bool exec_prog(const char *log_filename, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:86
unsigned int uint32
Definition: c.h:442
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:110
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
#define _(x)
Definition: elog.c:90
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1447
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:6576
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6743
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6690
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4130
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:707
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3240
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2225
static struct @143 value
@ CONNECTION_OK
Definition: libpq-fe.h:60
ExecStatusType
Definition: libpq-fe.h:95
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100
static void const char * fmt
va_end(args)
exit(1)
va_start(args, fmt)
#define pg_fatal(...)
#define MAXPGPATH
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
static char * buf
Definition: pg_test_fsync.c:67
OSInfo os_info
Definition: pg_upgrade.c:64
ClusterInfo new_cluster
Definition: pg_upgrade.c:63
ClusterInfo old_cluster
Definition: pg_upgrade.c:62
#define SERVER_START_LOG_FILE
Definition: pg_upgrade.h:67
#define SERVER_STOP_LOG_FILE
Definition: pg_upgrade.h:68
#define QUERY_ALLOC
Definition: pg_upgrade.h:23
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:44
LogOpts log_opts
Definition: util.c:17
@ PG_VERBOSE
Definition: pg_upgrade.h:238
@ PG_REPORT
Definition: pg_upgrade.h:241
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:27
#define vsnprintf
Definition: port.h:237
#define snprintf
Definition: port.h:238
#define printf(...)
Definition: port.h:244
static bool is_unixsock_path(const char *path)
Definition: pqcomm.h:55
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...)
Definition: server.c:122
static PGconn * get_db_conn(ClusterInfo *cluster, const char *db_name)
Definition: server.c:57
void check_pghost_envvar(void)
Definition: server.c:350
bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
Definition: server.c:198
char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:92
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
void stop_postmaster(bool in_atexit)
Definition: server.c:323
uint32 get_major_server_version(ClusterInfo *cluster)
Definition: server.c:159
static void stop_postmaster_atexit(void)
Definition: server.c:191
PGconn * conn
Definition: streamutil.c:54
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:429
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:545
char * logdir
Definition: pg_upgrade.h:283
char * user
Definition: pg_upgrade.h:313
ClusterInfo * running_cluster
Definition: pg_upgrade.h:319