]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/server.c
In pg_upgrade, fix the -l/log option to work on Windows.
[postgresql] / contrib / pg_upgrade / server.c
1 /*
2  *      server.c
3  *
4  *      database server functions
5  *
6  *      Copyright (c) 2010-2011, PostgreSQL Global Development Group
7  *      contrib/pg_upgrade/server.c
8  */
9
10 #include "pg_upgrade.h"
11
12
13 static PGconn *get_db_conn(ClusterInfo *cluster, const char *db_name);
14
15
16 /*
17  * connectToServer()
18  *
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.
22  */
23 PGconn *
24 connectToServer(ClusterInfo *cluster, const char *db_name)
25 {
26         PGconn     *conn = get_db_conn(cluster, db_name);
27
28         if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
29         {
30                 pg_log(PG_REPORT, "connection to database failed: %s\n",
31                            PQerrorMessage(conn));
32
33                 if (conn)
34                         PQfinish(conn);
35
36                 printf("Failure, exiting\n");
37                 exit(1);
38         }
39
40         return conn;
41 }
42
43
44 /*
45  * get_db_conn()
46  *
47  * get database connection
48  */
49 static PGconn *
50 get_db_conn(ClusterInfo *cluster, const char *db_name)
51 {
52         char            conn_opts[MAXPGPATH];
53
54         snprintf(conn_opts, sizeof(conn_opts),
55                          "dbname = '%s' user = '%s' port = %d", db_name, os_info.user,
56                          cluster->port);
57
58         return PQconnectdb(conn_opts);
59 }
60
61
62 /*
63  * executeQueryOrDie()
64  *
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.
68  */
69 PGresult *
70 executeQueryOrDie(PGconn *conn, const char *fmt,...)
71 {
72         static char command[8192];
73         va_list         args;
74         PGresult   *result;
75         ExecStatusType status;
76
77         va_start(args, fmt);
78         vsnprintf(command, sizeof(command), fmt, args);
79         va_end(args);
80
81         pg_log(PG_DEBUG, "executing: %s\n", command);
82         result = PQexec(conn, command);
83         status = PQresultStatus(result);
84
85         if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
86         {
87                 pg_log(PG_REPORT, "SQL command failed\n%s\n%s\n", command,
88                            PQerrorMessage(conn));
89                 PQclear(result);
90                 PQfinish(conn);
91                 printf("Failure, exiting\n");
92                 exit(1);
93         }
94         else
95                 return result;
96 }
97
98
99 /*
100  * get_major_server_version()
101  *
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.
105  */
106 uint32
107 get_major_server_version(ClusterInfo *cluster)
108 {
109         const char *datadir = cluster->pgdata;
110         FILE       *version_fd;
111         char            ver_filename[MAXPGPATH];
112         int                     integer_version = 0;
113         int                     fractional_version = 0;
114
115         snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION", datadir);
116         if ((version_fd = fopen(ver_filename, "r")) == NULL)
117                 return 0;
118
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);
123
124         fclose(version_fd);
125
126         return (100 * integer_version + fractional_version) * 100;
127 }
128
129
130 static void
131 #ifdef HAVE_ATEXIT
132 stop_postmaster_atexit(void)
133 #else
134 stop_postmaster_on_exit(int exitstatus, void *arg)
135 #endif
136 {
137         stop_postmaster(true);
138
139 }
140
141
142 void
143 start_postmaster(ClusterInfo *cluster)
144 {
145         char            cmd[MAXPGPATH];
146         PGconn     *conn;
147         bool            exit_hook_registered = false;
148         int                     pg_ctl_return = 0;
149
150         if (!exit_hook_registered)
151         {
152 #ifdef HAVE_ATEXIT
153                 atexit(stop_postmaster_atexit);
154 #else
155                 on_exit(stop_postmaster_on_exit);
156 #endif
157                 exit_hook_registered = true;
158         }
159
160         /*
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
165          * not touch them.
166          */
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",
174                          log_opts.filename2);
175
176         /*
177          * Don't throw an error right away, let connecting throw the error because
178          * it might supply a reason for the failure.
179          */
180         pg_ctl_return = exec_prog(false, "%s", cmd);
181
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)
185         {
186                 pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
187                            PQerrorMessage(conn));
188                 if (conn)
189                         PQfinish(conn);
190                 pg_log(PG_FATAL, "could not connect to %s postmaster started with the command: %s\n",
191                            CLUSTER_NAME(cluster), cmd);
192         }
193         PQfinish(conn);
194
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));
199
200         os_info.running_cluster = cluster;
201 }
202
203
204 void
205 stop_postmaster(bool fast)
206 {
207         char            cmd[MAXPGPATH];
208         const char *bindir;
209         const char *datadir;
210
211         if (os_info.running_cluster == &old_cluster)
212         {
213                 bindir = old_cluster.bindir;
214                 datadir = old_cluster.pgdata;
215         }
216         else if (os_info.running_cluster == &new_cluster)
217         {
218                 bindir = new_cluster.bindir;
219                 datadir = new_cluster.pgdata;
220         }
221         else
222                 return;                                 /* no cluster running */
223
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" : "",
228                          log_opts.filename2);
229
230         exec_prog(fast ? false : true, "%s", cmd);
231
232         os_info.running_cluster = NULL;
233 }
234
235
236 /*
237  * check_pghost_envvar()
238  *
239  * Tests that PGHOST does not point to a non-local server
240  */
241 void
242 check_pghost_envvar(void)
243 {
244         PQconninfoOption *option;
245         PQconninfoOption *start;
246
247         /* Get valid libpq env vars from the PQconndefaults function */
248
249         start = PQconndefaults();
250
251         for (option = start; option->keyword != NULL; option++)
252         {
253                 if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
254                                                            strcmp(option->envvar, "PGHOSTADDR") == 0))
255                 {
256                         const char *value = getenv(option->envvar);
257
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] != '/'))
262                                 pg_log(PG_FATAL,
263                                            "libpq environment variable %s has a non-local server value: %s\n",
264                                            option->envvar, value);
265                 }
266         }
267
268         /* Free the memory that libpq allocated on our behalf */
269         PQconninfoFree(start);
270 }