99
1010#include "pg_upgrade.h"
1111
12- #define POSTMASTER_UPTIME 20
1312
14- #define STARTUP_WARNING_TRIES 2
15-
16-
17- static pgpid_t get_postmaster_pid (const char * datadir );
18- static bool test_server_conn (ClusterInfo * cluster ,int timeout );
13+ static PGconn * get_db_conn (ClusterInfo * cluster ,const char * db_name );
1914
2015
2116/*
@@ -28,14 +23,7 @@ static bool test_server_conn(ClusterInfo *cluster, int timeout);
2823PGconn *
2924connectToServer (ClusterInfo * cluster ,const char * db_name )
3025{
31- unsigned short port = cluster -> port ;
32- char connectString [MAXPGPATH ];
33- PGconn * conn ;
34-
35- snprintf (connectString ,sizeof (connectString ),
36- "dbname = '%s' user = '%s' port = %d" ,db_name ,os_info .user ,port );
37-
38- conn = PQconnectdb (connectString );
26+ PGconn * conn = get_db_conn (cluster ,db_name );
3927
4028if (conn == NULL || PQstatus (conn )!= CONNECTION_OK )
4129{
@@ -53,6 +41,24 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
5341}
5442
5543
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+
5662/*
5763 * executeQueryOrDie()
5864 *
@@ -90,38 +96,6 @@ executeQueryOrDie(PGconn *conn, const char *fmt,...)
9096}
9197
9298
93- /*
94- * get_postmaster_pid()
95- *
96- * Returns the pid of the postmaster running on datadir. pid is retrieved
97- * from the postmaster.pid file
98- */
99- static pgpid_t
100- get_postmaster_pid (const char * datadir )
101- {
102- FILE * pidf ;
103- long pid ;
104- char pid_file [MAXPGPATH ];
105-
106- snprintf (pid_file ,sizeof (pid_file ),"%s/postmaster.pid" ,datadir );
107- pidf = fopen (pid_file ,"r" );
108-
109- if (pidf == NULL )
110- return (pgpid_t )0 ;
111-
112- if (fscanf (pidf ,"%ld" ,& pid )!= 1 )
113- {
114- fclose (pidf );
115- pg_log (PG_FATAL ,"%s: invalid data in PID file \"%s\"\n" ,
116- os_info .progname ,pid_file );
117- }
118-
119- fclose (pidf );
120-
121- return (pgpid_t )pid ;
122- }
123-
124-
12599/*
126100 * get_major_server_version()
127101 *
@@ -169,20 +143,20 @@ void
169143start_postmaster (ClusterInfo * cluster )
170144{
171145char cmd [MAXPGPATH ];
172- const char * bindir ;
173- const char * datadir ;
174- unsigned short port ;
146+ PGconn * conn ;
175147bool exit_hook_registered = false;
176148#ifndef WIN32
177149char * output_filename = log_opts .filename ;
178150#else
151+ /*
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'.
156+ */
179157char * output_filename = DEVNULL ;
180158#endif
181159
182- bindir = cluster -> bindir ;
183- datadir = cluster -> pgdata ;
184- port = cluster -> port ;
185-
186160if (!exit_hook_registered )
187161{
188162#ifdef HAVE_ATEXIT
@@ -194,10 +168,6 @@ start_postmaster(ClusterInfo *cluster)
194168}
195169
196170/*
197- * On Win32, we can't send both pg_upgrade output and pg_ctl output to the
198- * same file because we get the error: "The process cannot access the file
199- * because it is being used by another process." so we have to send all
200- * other output to 'nul'.
201171 * Using autovacuum=off disables cleanup vacuum and analyze, but freeze
202172 * vacuums can still happen, so we set autovacuum_freeze_max_age to its
203173 * maximum. We assume all datfrozenxid and relfrozen values are less than
@@ -207,22 +177,26 @@ start_postmaster(ClusterInfo *cluster)
207177snprintf (cmd ,sizeof (cmd ),
208178SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
209179"-o \"-p %d %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE ,
210- bindir ,output_filename ,datadir , port ,
180+ cluster -> bindir ,output_filename ,cluster -> pgdata , cluster -> port ,
211181 (cluster -> controldata .cat_ver >=
212182BINARY_UPGRADE_SERVER_FLAG_CAT_VER ) ?"-b" :
213183"-c autovacuum=off -c autovacuum_freeze_max_age=2000000000" ,
214184log_opts .filename );
215185
216186exec_prog (true,"%s" ,cmd );
217187
218- /* wait for the server to start properly */
219-
220- if (test_server_conn (cluster ,POSTMASTER_UPTIME )== false)
221- pg_log (PG_FATAL ," Unable to start %s postmaster with the command: %s\nPerhaps pg_hba.conf was not set to \"trust\"." ,
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 )
191+ {
192+ if (conn )
193+ PQfinish (conn );
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\"." ,
222196CLUSTER_NAME (cluster ),cmd );
197+ }
198+ PQfinish (conn );
223199
224- if ((os_info .postmasterPID = get_postmaster_pid (datadir ))== 0 )
225- pg_log (PG_FATAL ," Unable to get postmaster pid\n" );
226200os_info .running_cluster = cluster ;
227201}
228202
@@ -233,6 +207,12 @@ stop_postmaster(bool fast)
233207char cmd [MAXPGPATH ];
234208const char * bindir ;
235209const char * datadir ;
210+ #ifndef WIN32
211+ char * output_filename = log_opts .filename ;
212+ #else
213+ /* See comment in start_postmaster() about why win32 output is ignored. */
214+ char * output_filename = DEVNULL ;
215+ #endif
236216
237217if (os_info .running_cluster == & old_cluster )
238218{
@@ -247,69 +227,18 @@ stop_postmaster(bool fast)
247227else
248228return ;/* no cluster running */
249229
250- /* See comment in start_postmaster() about why win32 output is ignored. */
251230snprintf (cmd ,sizeof (cmd ),
252231SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" %s stop >> "
253232"\"%s\" 2>&1" SYSTEMQUOTE ,
254- bindir ,
255- #ifndef WIN32
256- log_opts .filename ,datadir ,fast ?"-m fast" :"" ,log_opts .filename );
257- #else
258- DEVNULL ,datadir ,fast ?"-m fast" :"" ,DEVNULL );
259- #endif
233+ bindir ,output_filename ,datadir ,fast ?"-m fast" :"" ,
234+ output_filename );
235+
260236exec_prog (fast ? false : true,"%s" ,cmd );
261237
262- os_info .postmasterPID = 0 ;
263238os_info .running_cluster = NULL ;
264239}
265240
266241
267- /*
268- * test_server_conn()
269- *
270- * tests whether postmaster is running or not by trying to connect
271- * to it. If connection is unsuccessfull we do a sleep of 1 sec and then
272- * try the connection again. This process continues "timeout" times.
273- *
274- * Returns true if the connection attempt was successfull, false otherwise.
275- */
276- static bool
277- test_server_conn (ClusterInfo * cluster ,int timeout )
278- {
279- unsigned short port = cluster -> port ;
280- PGconn * conn = NULL ;
281- char con_opts [MAX_STRING ];
282- int tries ;
283- bool ret = false;
284-
285- snprintf (con_opts ,sizeof (con_opts ),
286- "dbname = 'template1' user = '%s' port = %d " ,os_info .user ,port );
287-
288- for (tries = 0 ;tries < timeout ;tries ++ )
289- {
290- sleep (1 );
291- if ((conn = PQconnectdb (con_opts ))!= NULL &&
292- PQstatus (conn )== CONNECTION_OK )
293- {
294- PQfinish (conn );
295- ret = true;
296- break ;
297- }
298-
299- if (tries == STARTUP_WARNING_TRIES )
300- prep_status ("Trying to start %s server " ,
301- CLUSTER_NAME (cluster ));
302- else if (tries > STARTUP_WARNING_TRIES )
303- pg_log (PG_REPORT ,"." );
304- }
305-
306- if (tries > STARTUP_WARNING_TRIES )
307- check_ok ();
308-
309- return ret ;
310- }
311-
312-
313242/*
314243 * check_for_libpq_envvars()
315244 *