4 * database server functions
6 * Copyright (c) 2010-2012, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/server.c
12 #include "pg_upgrade.h"
15 static PGconn *get_db_conn(ClusterInfo *cluster, const char *db_name);
21 * Connects to the desired database on the designated server.
22 * If the connection attempt fails, this function logs an error
23 * message and calls exit() to kill the program.
26 connectToServer(ClusterInfo *cluster, const char *db_name)
28 PGconn *conn = get_db_conn(cluster, db_name);
30 if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
32 pg_log(PG_REPORT, "connection to database failed: %s\n",
33 PQerrorMessage(conn));
38 printf("Failure, exiting\n");
49 * get database connection
52 get_db_conn(ClusterInfo *cluster, const char *db_name)
54 char conn_opts[MAXPGPATH];
56 snprintf(conn_opts, sizeof(conn_opts),
57 "dbname = '%s' user = '%s' port = %d", db_name, os_info.user,
60 return PQconnectdb(conn_opts);
67 * Formats a query string from the given arguments and executes the
68 * resulting query. If the query fails, this function logs an error
69 * message and calls exit() to kill the program.
72 executeQueryOrDie(PGconn *conn, const char *fmt,...)
74 static char command[8192];
77 ExecStatusType status;
80 vsnprintf(command, sizeof(command), fmt, args);
83 pg_log(PG_VERBOSE, "executing: %s\n", command);
84 result = PQexec(conn, command);
85 status = PQresultStatus(result);
87 if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
89 pg_log(PG_REPORT, "SQL command failed\n%s\n%s\n", command,
90 PQerrorMessage(conn));
93 printf("Failure, exiting\n");
102 * get_major_server_version()
104 * gets the version (in unsigned int form) for the given datadir. Assumes
105 * that datadir is an absolute path to a valid pgdata directory. The version
106 * is retrieved by reading the PG_VERSION file.
109 get_major_server_version(ClusterInfo *cluster)
112 char ver_filename[MAXPGPATH];
113 int integer_version = 0;
114 int fractional_version = 0;
116 snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION",
118 if ((version_fd = fopen(ver_filename, "r")) == NULL)
121 if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 ||
122 sscanf(cluster->major_version_str, "%d.%d", &integer_version,
123 &fractional_version) != 2)
124 pg_log(PG_FATAL, "could not get version from %s\n", cluster->pgdata);
128 return (100 * integer_version + fractional_version) * 100;
133 stop_postmaster_atexit(void)
135 stop_postmaster(true);
141 start_postmaster(ClusterInfo *cluster)
145 bool exit_hook_registered = false;
146 int pg_ctl_return = 0;
148 if (!exit_hook_registered)
150 atexit(stop_postmaster_atexit);
151 exit_hook_registered = true;
155 * Using autovacuum=off disables cleanup vacuum and analyze, but freeze
156 * vacuums can still happen, so we set autovacuum_freeze_max_age to its
157 * maximum. We assume all datfrozenxid and relfrozen values are less than
158 * a gap of 2000000000 from the current xid counter, so autovacuum will
161 snprintf(cmd, sizeof(cmd),
162 SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
163 "-o \"-p %d %s %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
164 cluster->bindir, SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
165 (cluster->controldata.cat_ver >=
166 BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
167 "-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
168 cluster->pgopts ? cluster->pgopts : "", SERVER_START_LOG_FILE);
171 * Don't throw an error right away, let connecting throw the error because
172 * it might supply a reason for the failure.
174 pg_ctl_return = exec_prog(false, true,
175 /* pass both file names if the differ */
176 (strcmp(SERVER_LOG_FILE, SERVER_START_LOG_FILE) == 0) ?
178 SERVER_LOG_FILE " or " SERVER_START_LOG_FILE,
181 /* Check to see if we can connect to the server; if not, report it. */
182 if ((conn = get_db_conn(cluster, "template1")) == NULL ||
183 PQstatus(conn) != CONNECTION_OK)
185 pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
186 PQerrorMessage(conn));
189 pg_log(PG_FATAL, "could not connect to %s postmaster started with the command: %s\n",
190 CLUSTER_NAME(cluster), cmd);
194 /* If the connection didn't fail, fail now */
195 if (pg_ctl_return != 0)
196 pg_log(PG_FATAL, "pg_ctl failed to start the %s server\n",
197 CLUSTER_NAME(cluster));
199 os_info.running_cluster = cluster;
204 stop_postmaster(bool fast)
207 ClusterInfo *cluster;
209 if (os_info.running_cluster == &old_cluster)
210 cluster = &old_cluster;
211 else if (os_info.running_cluster == &new_cluster)
212 cluster = &new_cluster;
214 return; /* no cluster running */
216 snprintf(cmd, sizeof(cmd),
217 SYSTEMQUOTE "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" "
218 "%s stop >> \"%s\" 2>&1" SYSTEMQUOTE,
219 cluster->bindir, cluster->pgconfig,
220 cluster->pgopts ? cluster->pgopts : "",
221 fast ? "-m fast" : "", SERVER_STOP_LOG_FILE);
223 exec_prog(fast ? false : true, true, SERVER_STOP_LOG_FILE, "%s", cmd);
225 os_info.running_cluster = NULL;
230 * check_pghost_envvar()
232 * Tests that PGHOST does not point to a non-local server
235 check_pghost_envvar(void)
237 PQconninfoOption *option;
238 PQconninfoOption *start;
240 /* Get valid libpq env vars from the PQconndefaults function */
242 start = PQconndefaults();
244 for (option = start; option->keyword != NULL; option++)
246 if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
247 strcmp(option->envvar, "PGHOSTADDR") == 0))
249 const char *value = getenv(option->envvar);
251 if (value && strlen(value) > 0 &&
252 /* check for 'local' host values */
253 (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
254 strcmp(value, "::1") != 0 && value[0] != '/'))
256 "libpq environment variable %s has a non-local server value: %s\n",
257 option->envvar, value);
261 /* Free the memory that libpq allocated on our behalf */
262 PQconninfoFree(start);