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, "DB 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;
149
char *output_filename = log_opts.filename;
152
* On Win32, we can't send both pg_upgrade output and pg_ctl output to the
153
* same file because we get the error: "The process cannot access the file
154
* because it is being used by another process." so we have to send all
155
* other output to 'nul'.
157
char *output_filename = DEVNULL;
160
if (!exit_hook_registered)
163
atexit(stop_postmaster_atexit);
165
on_exit(stop_postmaster_on_exit);
167
exit_hook_registered = true;
171
* Using autovacuum=off disables cleanup vacuum and analyze, but freeze
172
* vacuums can still happen, so we set autovacuum_freeze_max_age to its
173
* maximum. We assume all datfrozenxid and relfrozen values are less than
174
* a gap of 2000000000 from the current xid counter, so autovacuum will
177
snprintf(cmd, sizeof(cmd),
178
SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
179
"-o \"-p %d %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
180
cluster->bindir, output_filename, cluster->pgdata, cluster->port,
181
(cluster->controldata.cat_ver >=
182
BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
183
"-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
186
exec_prog(true, "%s", cmd);
188
/* Check to see if we can connect to the server; if not, report it. */
189
if ((conn = get_db_conn(cluster, "template1")) == NULL ||
190
PQstatus(conn) != CONNECTION_OK)
194
pg_log(PG_FATAL, "unable to connect to %s postmaster started with the command: %s\n"
195
"Perhaps pg_hba.conf was not set to \"trust\".",
196
CLUSTER_NAME(cluster), cmd);
200
os_info.running_cluster = cluster;
205
stop_postmaster(bool fast)
211
char *output_filename = log_opts.filename;
213
/* See comment in start_postmaster() about why win32 output is ignored. */
214
char *output_filename = DEVNULL;
217
if (os_info.running_cluster == &old_cluster)
219
bindir = old_cluster.bindir;
220
datadir = old_cluster.pgdata;
222
else if (os_info.running_cluster == &new_cluster)
224
bindir = new_cluster.bindir;
225
datadir = new_cluster.pgdata;
228
return; /* no cluster running */
230
snprintf(cmd, sizeof(cmd),
231
SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" %s stop >> "
232
"\"%s\" 2>&1" SYSTEMQUOTE,
233
bindir, output_filename, datadir, fast ? "-m fast" : "",
236
exec_prog(fast ? false : true, "%s", cmd);
238
os_info.running_cluster = NULL;
243
* check_for_libpq_envvars()
245
* tests whether any libpq environment variables are set.
246
* Since pg_upgrade connects to both the old and the new server,
247
* it is potentially dangerous to have any of these set.
249
* If any are found, will log them and cancel.
252
check_for_libpq_envvars(void)
254
PQconninfoOption *option;
255
PQconninfoOption *start;
258
/* Get valid libpq env vars from the PQconndefaults function */
260
start = PQconndefaults();
262
for (option = start; option->keyword != NULL; option++)
268
if (strcmp(option->envvar, "PGCLIENTENCODING") == 0)
271
value = getenv(option->envvar);
272
if (value && strlen(value) > 0)
277
"libpq env var %-20s is currently set to: %s\n", option->envvar, value);
282
/* Free the memory that libpq allocated on our behalf */
283
PQconninfoFree(start);
287
"libpq env vars have been found and listed above, please unset them for pg_upgrade\n");