PostgreSQL Source Code  git master
server.c File Reference
#include "postgres_fe.h"
#include "fe_utils/connect.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 report_and_exit_on_error)
 
void stop_postmaster (bool in_atexit)
 
void check_pghost_envvar (void)
 

Function Documentation

◆ check_pghost_envvar()

void check_pghost_envvar ( void  )

Definition at line 354 of file server.c.

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

Referenced by setup().

355 {
357  PQconninfoOption *start;
358 
359  /* Get valid libpq env vars from the PQconndefaults function */
360 
361  start = PQconndefaults();
362 
363  if (!start)
364  pg_fatal("out of memory\n");
365 
366  for (option = start; option->keyword != NULL; option++)
367  {
368  if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
369  strcmp(option->envvar, "PGHOSTADDR") == 0))
370  {
371  const char *value = getenv(option->envvar);
372 
373  if (value && strlen(value) > 0 &&
374  /* check for 'local' host values */
375  (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
376  strcmp(value, "::1") != 0 && value[0] != '/'))
377  pg_fatal("libpq environment variable %s has a non-local server value: %s\n",
378  option->envvar, value);
379  }
380  }
381 
382  /* Free the memory that libpq allocated on our behalf */
383  PQconninfoFree(start);
384 }
#define pg_fatal(...)
Definition: pg_rewind.h:41
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:6519
static struct @143 value
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1434

◆ cluster_conn_opts()

char* cluster_conn_opts ( ClusterInfo cluster)

Definition at line 90 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_globals().

91 {
92  static PQExpBuffer buf;
93 
94  if (buf == NULL)
95  buf = createPQExpBuffer();
96  else
97  resetPQExpBuffer(buf);
98 
99  if (cluster->sockdir)
100  {
101  appendPQExpBufferStr(buf, "--host ");
102  appendShellString(buf, cluster->sockdir);
103  appendPQExpBufferChar(buf, ' ');
104  }
105  appendPQExpBuffer(buf, "--port %d --username ", cluster->port);
107 
108  return buf->data;
109 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
unsigned short port
Definition: pg_upgrade.h:266
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:67
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:429
char * sockdir
Definition: pg_upgrade.h:265
OSInfo os_info
Definition: pg_upgrade.c:61
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
char * user
Definition: pg_upgrade.h:309

◆ connectToServer()

PGconn* connectToServer ( ClusterInfo cluster,
const char *  db_name 
)

Definition at line 27 of file server.c.

References _, ALWAYS_SECURE_SEARCH_PATH_SQL, conn, CONNECTION_OK, executeQueryOrDie(), get_db_conn(), pg_log(), PG_REPORT, PQclear(), PQerrorMessage(), PQfinish(), PQstatus(), and printf.

Referenced by check_for_data_type_usage(), 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_for_tables_with_oids(), 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_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 
44 
45  return conn;
46 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6687
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4185
#define printf(...)
Definition: port.h:199
PGconn * conn
Definition: streamutil.c:54
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...)
Definition: server.c:120
void PQclear(PGresult *res)
Definition: fe-exec.c:694
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6634
static PGconn * get_db_conn(ClusterInfo *cluster, const char *db_name)
Definition: server.c:55
#define _(x)
Definition: elog.c:88

◆ executeQueryOrDie()

PGresult* executeQueryOrDie ( PGconn conn,
const char *  fmt,
  ... 
)

Definition at line 120 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(), printf, QUERY_ALLOC, status(), and vsnprintf.

Referenced by connectToServer().

121 {
122  static char query[QUERY_ALLOC];
123  va_list args;
124  PGresult *result;
126 
127  va_start(args, fmt);
128  vsnprintf(query, sizeof(query), fmt, args);
129  va_end(args);
130 
131  pg_log(PG_VERBOSE, "executing: %s\n", query);
132  result = PQexec(conn, query);
133  status = PQresultStatus(result);
134 
135  if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
136  {
137  pg_log(PG_REPORT, "SQL command failed\n%s\n%s", query,
138  PQerrorMessage(conn));
139  PQclear(result);
140  PQfinish(conn);
141  printf(_("Failure, exiting\n"));
142  exit(1);
143  }
144  else
145  return result;
146 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6687
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4185
#define printf(...)
Definition: port.h:199
ExecStatusType
Definition: libpq-fe.h:84
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define vsnprintf
Definition: port.h:192
void PQclear(PGresult *res)
Definition: fe-exec.c:694
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define _(x)
Definition: elog.c:88
#define QUERY_ALLOC
Definition: pg_upgrade.h:19

◆ get_db_conn()

static PGconn * get_db_conn ( ClusterInfo cluster,
const char *  db_name 
)
static

Definition at line 55 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().

56 {
57  PQExpBufferData conn_opts;
58  PGconn *conn;
59 
60  /* Build connection string with proper quoting */
61  initPQExpBuffer(&conn_opts);
62  appendPQExpBufferStr(&conn_opts, "dbname=");
63  appendConnStrVal(&conn_opts, db_name);
64  appendPQExpBufferStr(&conn_opts, " user=");
65  appendConnStrVal(&conn_opts, os_info.user);
66  appendPQExpBuffer(&conn_opts, " port=%d", cluster->port);
67  if (cluster->sockdir)
68  {
69  appendPQExpBufferStr(&conn_opts, " host=");
70  appendConnStrVal(&conn_opts, cluster->sockdir);
71  }
72 
73  conn = PQconnectdb(conn_opts.data);
74  termPQExpBuffer(&conn_opts);
75  return conn;
76 }
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:545
unsigned short port
Definition: pg_upgrade.h:266
PGconn * conn
Definition: streamutil.c:54
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
char * sockdir
Definition: pg_upgrade.h:265
OSInfo os_info
Definition: pg_upgrade.c:61
char * user
Definition: pg_upgrade.h:309
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:696

◆ get_major_server_version()

uint32 get_major_server_version ( ClusterInfo cluster)

Definition at line 157 of file server.c.

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

Referenced by check_data_dir().

158 {
159  FILE *version_fd;
160  char ver_filename[MAXPGPATH];
161  int v1 = 0,
162  v2 = 0;
163 
164  snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION",
165  cluster->pgdata);
166  if ((version_fd = fopen(ver_filename, "r")) == NULL)
167  pg_fatal("could not open version file \"%s\": %m\n", ver_filename);
168 
169  if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 ||
170  sscanf(cluster->major_version_str, "%d.%d", &v1, &v2) < 1)
171  pg_fatal("could not parse version file \"%s\"\n", ver_filename);
172 
173  fclose(version_fd);
174 
175  if (v1 < 10)
176  {
177  /* old style, e.g. 9.6.1 */
178  return v1 * 10000 + v2 * 100;
179  }
180  else
181  {
182  /* new style, e.g. 10.1 */
183  return v1 * 10000;
184  }
185 }
#define pg_fatal(...)
Definition: pg_rewind.h:41
char major_version_str[64]
Definition: pg_upgrade.h:268
#define MAXPGPATH
char * pgdata
Definition: pg_upgrade.h:259
#define snprintf
Definition: port.h:193

◆ start_postmaster()

bool start_postmaster ( ClusterInfo cluster,
bool  report_and_exit_on_error 
)

Definition at line 196 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().

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

◆ stop_postmaster()

void stop_postmaster ( bool  in_atexit)

Definition at line 327 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().

328 {
330 
332  cluster = &old_cluster;
333  else if (os_info.running_cluster == &new_cluster)
334  cluster = &new_cluster;
335  else
336  return; /* no cluster running */
337 
338  exec_prog(SERVER_STOP_LOG_FILE, NULL, !in_atexit, !in_atexit,
339  "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
340  cluster->bindir, cluster->pgconfig,
341  cluster->pgopts ? cluster->pgopts : "",
342  in_atexit ? "-m fast" : "-m smart");
343 
344  os_info.running_cluster = NULL;
345 }
#define SERVER_STOP_LOG_FILE
Definition: pg_upgrade.h:54
char * pgopts
Definition: pg_upgrade.h:263
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
char * pgconfig
Definition: pg_upgrade.h:260
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
void cluster(ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:102
char * bindir
Definition: pg_upgrade.h:262
ClusterInfo * running_cluster
Definition: pg_upgrade.h:315
OSInfo os_info
Definition: pg_upgrade.c:61
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:80

◆ stop_postmaster_atexit()

static void stop_postmaster_atexit ( void  )
static

Definition at line 189 of file server.c.

References stop_postmaster().

Referenced by start_postmaster().

190 {
191  stop_postmaster(true);
192 }
void stop_postmaster(bool in_atexit)
Definition: server.c:327