PostgreSQL Source Code  git master
server.c File Reference
#include "postgres_fe.h"
#include "common/connect.h"
#include "fe_utils/string_utils.h"
#include "libpq/pqcomm.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 369 of file server.c.

370 {
372  PQconninfoOption *start;
373 
374  /* Get valid libpq env vars from the PQconndefaults function */
375 
376  start = PQconndefaults();
377 
378  if (!start)
379  pg_fatal("out of memory");
380 
381  for (option = start; option->keyword != NULL; option++)
382  {
383  if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
384  strcmp(option->envvar, "PGHOSTADDR") == 0))
385  {
386  const char *value = getenv(option->envvar);
387 
388  if (value && strlen(value) > 0 &&
389  /* check for 'local' host values */
390  (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
391  strcmp(value, "::1") != 0 && !is_unixsock_path(value)))
392  pg_fatal("libpq environment variable %s has a non-local server value: %s",
393  option->envvar, value);
394  }
395  }
396 
397  /* Free the memory that libpq allocated on our behalf */
398  PQconninfoFree(start);
399 }
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1819
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:6781
static struct @154 value
#define pg_fatal(...)
static bool is_unixsock_path(const char *path)
Definition: pqcomm.h:67

References is_unixsock_path(), pg_fatal, PQconndefaults(), PQconninfoFree(), and value.

Referenced by setup().

◆ cluster_conn_opts()

char* cluster_conn_opts ( ClusterInfo cluster)

Definition at line 92 of file server.c.

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 }
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:108
static char * buf
Definition: pg_test_fsync.c:73
OSInfo os_info
Definition: pg_upgrade.c:66
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
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 appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:429
char * user
Definition: pg_upgrade.h:345

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendShellString(), buf, cluster(), createPQExpBuffer(), os_info, resetPQExpBuffer(), and OSInfo::user.

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

◆ connectToServer()

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

Definition at line 28 of file server.c.

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 }
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
#define _(x)
Definition: elog.c:90
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6948
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6895
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4669
@ CONNECTION_OK
Definition: libpq-fe.h:60
exit(1)
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ PG_REPORT
Definition: pg_upgrade.h:271
#define printf(...)
Definition: port.h:244
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
PGconn * conn
Definition: streamutil.c:55

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

Referenced by check_for_data_types_usage(), check_for_incompatible_polymorphics(), check_for_isn_and_int8_passing_mismatch(), check_for_pg_role_prefix(), check_for_prepared_transactions(), check_for_tables_with_oids(), check_for_user_defined_encoding_conversions(), check_for_user_defined_postfix_ops(), check_is_install_user(), check_loadable_libraries(), check_new_cluster_logical_replication_slots(), check_new_cluster_subscription_configuration(), check_old_cluster_subscription_state(), check_proper_datallowconn(), create_logical_replication_slots(), get_db_infos(), get_db_subscription_count(), get_loadable_libraries(), get_old_cluster_logical_slot_infos(), get_rel_infos(), get_tablespace_paths(), get_template0_info(), old_9_6_invalidate_hash_indexes(), report_extension_updates(), set_frozenxids(), and set_locale_and_encoding().

◆ executeQueryOrDie()

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

Definition at line 122 of file server.c.

123 {
124  static char query[QUERY_ALLOC];
125  va_list args;
126  PGresult *result;
127  ExecStatusType status;
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 
137  if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
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 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3371
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2224
ExecStatusType
Definition: libpq-fe.h:98
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:100
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:103
static void const char * fmt
va_end(args)
va_start(args, fmt)
#define QUERY_ALLOC
Definition: pg_upgrade.h:23
@ PG_VERBOSE
Definition: pg_upgrade.h:268
#define vsnprintf
Definition: port.h:237

References _, generate_unaccent_rules::args, conn, exit(), fmt, pg_log(), PG_REPORT, PG_VERBOSE, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), printf, QUERY_ALLOC, va_end(), va_start(), and vsnprintf.

Referenced by connectToServer().

◆ get_db_conn()

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

Definition at line 57 of file server.c.

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 }
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:733
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:545

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

Referenced by connectToServer(), and start_postmaster().

◆ get_major_server_version()

uint32 get_major_server_version ( ClusterInfo cluster)

Definition at line 159 of file server.c.

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 }
#define MAXPGPATH
#define snprintf
Definition: port.h:238

References cluster(), MAXPGPATH, pg_fatal, and snprintf.

Referenced by check_data_dir().

◆ start_postmaster()

bool start_postmaster ( ClusterInfo cluster,
bool  report_and_exit_on_error 
)

Definition at line 198 of file server.c.

199 {
200  char cmd[MAXPGPATH * 4 + 1000];
201  PGconn *conn;
202  bool pg_ctl_return = false;
203  char socket_string[MAXPGPATH + 200];
204  PQExpBufferData pgoptions;
205 
206  static bool exit_hook_registered = false;
207 
208  if (!exit_hook_registered)
209  {
210  atexit(stop_postmaster_atexit);
211  exit_hook_registered = true;
212  }
213 
214  socket_string[0] = '\0';
215 
216 #if !defined(WIN32)
217  /* prevent TCP/IP connections, restrict socket access */
218  strcat(socket_string,
219  " -c listen_addresses='' -c unix_socket_permissions=0700");
220 
221  /* Have a sockdir? Tell the postmaster. */
222  if (cluster->sockdir)
223  snprintf(socket_string + strlen(socket_string),
224  sizeof(socket_string) - strlen(socket_string),
225  " -c %s='%s'",
226  (GET_MAJOR_VERSION(cluster->major_version) <= 902) ?
227  "unix_socket_directory" : "unix_socket_directories",
228  cluster->sockdir);
229 #endif
230 
231  initPQExpBuffer(&pgoptions);
232 
233  /*
234  * Construct a parameter string which is passed to the server process.
235  *
236  * Turn off durability requirements to improve object creation speed, and
237  * we only modify the new cluster, so only use it there. If there is a
238  * crash, the new cluster has to be recreated anyway. fsync=off is a big
239  * win on ext4.
240  */
241  if (cluster == &new_cluster)
242  appendPQExpBufferStr(&pgoptions, " -c synchronous_commit=off -c fsync=off -c full_page_writes=off");
243 
244  /*
245  * Use max_slot_wal_keep_size as -1 to prevent the WAL removal by the
246  * checkpointer process. If WALs required by logical replication slots
247  * are removed, the slots are unusable. This setting prevents the
248  * invalidation of slots during the upgrade. We set this option when
249  * cluster is PG17 or later because logical replication slots can only be
250  * migrated since then. Besides, max_slot_wal_keep_size is added in PG13.
251  */
252  if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
253  appendPQExpBufferStr(&pgoptions, " -c max_slot_wal_keep_size=-1");
254 
255  /*
256  * Use -b to disable autovacuum and logical replication launcher
257  * (effective in PG17 or later for the latter).
258  */
259  snprintf(cmd, sizeof(cmd),
260  "\"%s/pg_ctl\" -w -l \"%s/%s\" -D \"%s\" -o \"-p %d -b%s %s%s\" start",
261  cluster->bindir,
263  SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
264  pgoptions.data,
265  cluster->pgopts ? cluster->pgopts : "", socket_string);
266 
267  termPQExpBuffer(&pgoptions);
268 
269  /*
270  * Don't throw an error right away, let connecting throw the error because
271  * it might supply a reason for the failure.
272  */
273  pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
274  /* pass both file names if they differ */
275  (strcmp(SERVER_LOG_FILE,
276  SERVER_START_LOG_FILE) != 0) ?
277  SERVER_LOG_FILE : NULL,
278  report_and_exit_on_error, false,
279  "%s", cmd);
280 
281  /* Did it fail and we are just testing if the server could be started? */
282  if (!pg_ctl_return && !report_and_exit_on_error)
283  return false;
284 
285  /*
286  * We set this here to make sure atexit() shuts down the server, but only
287  * if we started the server successfully. We do it before checking for
288  * connectivity in case the server started but there is a connectivity
289  * failure. If pg_ctl did not return success, we will exit below.
290  *
291  * Pre-9.1 servers do not have PQping(), so we could be leaving the server
292  * running if authentication was misconfigured, so someday we might went
293  * to be more aggressive about doing server shutdowns even if pg_ctl
294  * fails, but now (2013-08-14) it seems prudent to be cautious. We don't
295  * want to shutdown a server that might have been accidentally started
296  * during the upgrade.
297  */
298  if (pg_ctl_return)
300 
301  /*
302  * pg_ctl -w might have failed because the server couldn't be started, or
303  * there might have been a connection problem in _checking_ if the server
304  * has started. Therefore, even if pg_ctl failed, we continue and test
305  * for connectivity in case we get a connection reason for the failure.
306  */
307  if ((conn = get_db_conn(cluster, "template1")) == NULL ||
309  {
310  pg_log(PG_REPORT, "\n%s", PQerrorMessage(conn));
311  if (conn)
312  PQfinish(conn);
313  if (cluster == &old_cluster)
314  pg_fatal("could not connect to source postmaster started with the command:\n"
315  "%s",
316  cmd);
317  else
318  pg_fatal("could not connect to target postmaster started with the command:\n"
319  "%s",
320  cmd);
321  }
322  PQfinish(conn);
323 
324  /*
325  * If pg_ctl failed, and the connection didn't fail, and
326  * report_and_exit_on_error is enabled, fail now. This could happen if
327  * the server was already running.
328  */
329  if (!pg_ctl_return)
330  {
331  if (cluster == &old_cluster)
332  pg_fatal("pg_ctl failed to start the source server, or connection failed");
333  else
334  pg_fatal("pg_ctl failed to start the target server, or connection failed");
335  }
336 
337  return true;
338 }
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:85
ClusterInfo new_cluster
Definition: pg_upgrade.c:65
ClusterInfo old_cluster
Definition: pg_upgrade.c:64
#define SERVER_START_LOG_FILE
Definition: pg_upgrade.h:67
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:44
LogOpts log_opts
Definition: util.c:17
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:27
static void stop_postmaster_atexit(void)
Definition: server.c:191
char * logdir
Definition: pg_upgrade.h:314
ClusterInfo * running_cluster
Definition: pg_upgrade.h:351

References appendPQExpBufferStr(), cluster(), conn, CONNECTION_OK, PQExpBufferData::data, exec_prog(), get_db_conn(), GET_MAJOR_VERSION, initPQExpBuffer(), log_opts, LogOpts::logdir, MAXPGPATH, new_cluster, old_cluster, os_info, pg_fatal, pg_log(), PG_REPORT, PQerrorMessage(), PQfinish(), PQstatus(), OSInfo::running_cluster, SERVER_LOG_FILE, SERVER_START_LOG_FILE, snprintf, stop_postmaster_atexit(), and termPQExpBuffer().

◆ stop_postmaster()

void stop_postmaster ( bool  in_atexit)

Definition at line 342 of file server.c.

343 {
345 
347  cluster = &old_cluster;
348  else if (os_info.running_cluster == &new_cluster)
349  cluster = &new_cluster;
350  else
351  return; /* no cluster running */
352 
353  exec_prog(SERVER_STOP_LOG_FILE, NULL, !in_atexit, !in_atexit,
354  "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
355  cluster->bindir, cluster->pgconfig,
356  cluster->pgopts ? cluster->pgopts : "",
357  in_atexit ? "-m fast" : "-m smart");
358 
359  os_info.running_cluster = NULL;
360 }
#define SERVER_STOP_LOG_FILE
Definition: pg_upgrade.h:68

References cluster(), exec_prog(), new_cluster, old_cluster, os_info, 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().

◆ stop_postmaster_atexit()

static void stop_postmaster_atexit ( void  )
static

Definition at line 191 of file server.c.

192 {
193  stop_postmaster(true);
194 }
void stop_postmaster(bool in_atexit)
Definition: server.c:342

References stop_postmaster().

Referenced by start_postmaster().