4 * database server functions
6 * Copyright (c) 2010-2011, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/server.c
10 #include "pg_upgrade.h"
13 static PGconn *get_db_conn(ClusterInfo *cluster, const char *db_name);
19 * Connects to the desired database on the designated server.
20 * If the connection attempt fails, this function logs an error
21 * message and calls exit() to kill the program.
24 connectToServer(ClusterInfo *cluster, const char *db_name)
26 PGconn *conn = get_db_conn(cluster, db_name);
28 if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
30 pg_log(PG_REPORT, "connection to database failed: %s\n",
31 PQerrorMessage(conn));
36 printf("Failure, exiting\n");
47 * get database connection
50 get_db_conn(ClusterInfo *cluster, const char *db_name)
52 char conn_opts[MAXPGPATH];
54 snprintf(conn_opts, sizeof(conn_opts),
55 "dbname = '%s' user = '%s' port = %d", db_name, os_info.user,
58 return PQconnectdb(conn_opts);
65 * Formats a query string from the given arguments and executes the
66 * resulting query. If the query fails, this function logs an error
67 * message and calls exit() to kill the program.
70 executeQueryOrDie(PGconn *conn, const char *fmt,...)
72 static char command[8192];
75 ExecStatusType status;
78 vsnprintf(command, sizeof(command), fmt, args);
81 pg_log(PG_DEBUG, "executing: %s\n", command);
82 result = PQexec(conn, command);
83 status = PQresultStatus(result);
85 if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
87 pg_log(PG_REPORT, "SQL command failed\n%s\n%s\n", command,
88 PQerrorMessage(conn));
91 printf("Failure, exiting\n");
100 * get_major_server_version()
102 * gets the version (in unsigned int form) for the given "datadir". Assumes
103 * that datadir is an absolute path to a valid pgdata directory. The version
104 * is retrieved by reading the PG_VERSION file.
107 get_major_server_version(ClusterInfo *cluster)
109 const char *datadir = cluster->pgdata;
111 char ver_filename[MAXPGPATH];
112 int integer_version = 0;
113 int fractional_version = 0;
115 snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION", datadir);
116 if ((version_fd = fopen(ver_filename, "r")) == NULL)
119 if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 ||
120 sscanf(cluster->major_version_str, "%d.%d", &integer_version,
121 &fractional_version) != 2)
122 pg_log(PG_FATAL, "could not get version from %s\n", datadir);
126 return (100 * integer_version + fractional_version) * 100;
132 stop_postmaster_atexit(void)
134 stop_postmaster_on_exit(int exitstatus, void *arg)
137 stop_postmaster(true);
143 start_postmaster(ClusterInfo *cluster)
147 bool exit_hook_registered = false;
148 int pg_ctl_return = 0;
150 if (!exit_hook_registered)
153 atexit(stop_postmaster_atexit);
155 on_exit(stop_postmaster_on_exit);
157 exit_hook_registered = true;
161 * Using autovacuum=off disables cleanup vacuum and analyze, but freeze
162 * vacuums can still happen, so we set autovacuum_freeze_max_age to its
163 * maximum. We assume all datfrozenxid and relfrozen values are less than
164 * a gap of 2000000000 from the current xid counter, so autovacuum will
167 snprintf(cmd, sizeof(cmd),
168 SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
169 "-o \"-p %d %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
170 cluster->bindir, log_opts.filename2, cluster->pgdata, cluster->port,
171 (cluster->controldata.cat_ver >=
172 BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
173 "-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
177 * Don't throw an error right away, let connecting throw the error because
178 * it might supply a reason for the failure.
180 pg_ctl_return = exec_prog(false, "%s", cmd);
182 /* Check to see if we can connect to the server; if not, report it. */
183 if ((conn = get_db_conn(cluster, "template1")) == NULL ||
184 PQstatus(conn) != CONNECTION_OK)
186 pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
187 PQerrorMessage(conn));
190 pg_log(PG_FATAL, "could not connect to %s postmaster started with the command: %s\n",
191 CLUSTER_NAME(cluster), cmd);
195 /* If the connection didn't fail, fail now */
196 if (pg_ctl_return != 0)
197 pg_log(PG_FATAL, "pg_ctl failed to start the %s server\n",
198 CLUSTER_NAME(cluster));
200 os_info.running_cluster = cluster;
205 stop_postmaster(bool fast)
211 if (os_info.running_cluster == &old_cluster)
213 bindir = old_cluster.bindir;
214 datadir = old_cluster.pgdata;
216 else if (os_info.running_cluster == &new_cluster)
218 bindir = new_cluster.bindir;
219 datadir = new_cluster.pgdata;
222 return; /* no cluster running */
224 snprintf(cmd, sizeof(cmd),
225 SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" %s stop >> "
226 "\"%s\" 2>&1" SYSTEMQUOTE,
227 bindir, log_opts.filename2, datadir, fast ? "-m fast" : "",
230 exec_prog(fast ? false : true, "%s", cmd);
232 os_info.running_cluster = NULL;
237 * check_pghost_envvar()
239 * Tests that PGHOST does not point to a non-local server
242 check_pghost_envvar(void)
244 PQconninfoOption *option;
245 PQconninfoOption *start;
247 /* Get valid libpq env vars from the PQconndefaults function */
249 start = PQconndefaults();
251 for (option = start; option->keyword != NULL; option++)
253 if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
254 strcmp(option->envvar, "PGHOSTADDR") == 0))
256 const char *value = getenv(option->envvar);
258 if (value && strlen(value) > 0 &&
259 /* check for 'local' host values */
260 (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
261 strcmp(value, "::1") != 0 && value[0] != '/'))
263 "libpq environment variable %s has a non-local server value: %s\n",
264 option->envvar, value);
268 /* Free the memory that libpq allocated on our behalf */
269 PQconninfoFree(start);