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