44 * Revisions by Christopher B. Browne, Liberty RMS
55 * Win32 Service code added by Dave Page
66 *
7- * $PostgreSQL: pgsql/contrib/pg_autovacuum/pg_autovacuum.c,v 1.28 2005/01/24 00:13:38 neilc Exp $
7+ * $PostgreSQL: pgsql/contrib/pg_autovacuum/pg_autovacuum.c,v 1.29 2005/01/26 22:25:13 tgl Exp $
88 */
99
1010#include "postgres_fe.h"
@@ -90,7 +90,7 @@ log_entry(const char *logentry, int level)
9090{
9191/*
9292 * Note: Under Windows we dump the log entries to the normal
93- * stderr/logfile as well, otherwise it can be a pain to debug
93+ * stderr/logfile as well, otherwise it can be a pain to debug
9494 * service install failures etc.
9595 */
9696
@@ -556,6 +556,9 @@ init_db_list(void)
556556Dllist * db_list = DLNewList ();
557557db_info * dbs = NULL ;
558558PGresult * res = NULL ;
559+ #ifdef WIN32
560+ int k = 0 ;
561+ #endif
559562
560563DLAddHead (db_list ,DLNewElem (init_dbinfo ((char * )"template1" ,0 ,0 )));
561564if (DLGetHead (db_list )== NULL )
@@ -572,6 +575,30 @@ init_db_list(void)
572575dbs = ((db_info * )DLE_VAL (DLGetHead (db_list )));
573576dbs -> conn = db_connect (dbs );
574577
578+ #ifdef WIN32
579+ while (dbs -> conn == NULL && !appMode && k < 10 )
580+ {
581+ int j ;
582+
583+ /* Pause for 30 seconds to allow the database to start up */
584+ log_entry ("Pausing 30 seconds to allow the database to startup completely" ,LVL_INFO );
585+ fflush (LOGOUTPUT );
586+ ServiceStatus .dwWaitHint = 10 ;
587+ for (j = 0 ;j < 6 ;j ++ )
588+ {
589+ pg_usleep (5000000 );
590+ ServiceStatus .dwCheckPoint ++ ;
591+ SetServiceStatus (hStatus ,& ServiceStatus );
592+ fflush (LOGOUTPUT );
593+ }
594+
595+ /* now try again */
596+ log_entry ("Attempting to connect again." ,LVL_INFO );
597+ dbs -> conn = db_connect (dbs );
598+ k ++ ;
599+ }
600+ #endif
601+
575602if (dbs -> conn != NULL )
576603{
577604res = send_query (FROZENOID_QUERY ,dbs );
@@ -904,7 +931,7 @@ db_connect(db_info * dbi)
904931PQfinish (db_conn );
905932db_conn = NULL ;
906933}
907-
934+
908935return db_conn ;
909936}/* end of db_connect() */
910937
@@ -980,44 +1007,44 @@ static void
9801007perform_maintenance_command (db_info * dbi ,tbl_info * tbl ,int operation )
9811008{
9821009char buf [256 ];
983-
984- /*
1010+
1011+ /*
9851012 * Set the vacuum_cost variables if supplied on command line
986- */
1013+ */
9871014if (args -> av_vacuum_cost_delay != -1 )
988- {
1015+ {
9891016snprintf (buf ,sizeof (buf ),"set vacuum_cost_delay = %d" ,
9901017args -> av_vacuum_cost_delay );
9911018send_query (buf ,dbi );
9921019}
9931020if (args -> av_vacuum_cost_page_hit != -1 )
994- {
1021+ {
9951022snprintf (buf ,sizeof (buf ),"set vacuum_cost_page_hit = %d" ,
9961023args -> av_vacuum_cost_page_hit );
9971024send_query (buf ,dbi );
9981025}
9991026if (args -> av_vacuum_cost_page_miss != -1 )
1000- {
1027+ {
10011028snprintf (buf ,sizeof (buf ),"set vacuum_cost_page_miss = %d" ,
10021029args -> av_vacuum_cost_page_miss );
10031030send_query (buf ,dbi );
10041031}
10051032if (args -> av_vacuum_cost_page_dirty != -1 )
1006- {
1033+ {
10071034snprintf (buf ,sizeof (buf ),"set vacuum_cost_page_dirty = %d" ,
10081035args -> av_vacuum_cost_page_dirty );
10091036send_query (buf ,dbi );
10101037}
10111038if (args -> av_vacuum_cost_limit != -1 )
1012- {
1039+ {
10131040snprintf (buf ,sizeof (buf ),"set vacuum_cost_limit = %d" ,
10141041args -> av_vacuum_cost_limit );
10151042send_query (buf ,dbi );
10161043}
1017-
1044+
10181045/*
1019- * if ((relisshared = t and database != template1) or
1020- * if operation = ANALYZE_ONLY)
1046+ * if ((relisshared = t and database != template1) or
1047+ * if operation = ANALYZE_ONLY)
10211048 * then only do an analyze
10221049 */
10231050if ((tbl -> relisshared > 0 && strcmp ("template1" ,dbi -> dbname )!= 0 )||
@@ -1027,14 +1054,14 @@ perform_maintenance_command(db_info * dbi, tbl_info * tbl, int operation)
10271054snprintf (buf ,sizeof (buf ),"VACUUM ANALYZE %s" ,tbl -> table_name );
10281055else
10291056return ;
1030-
1057+
10311058if (args -> debug >=1 )
10321059{
10331060sprintf (logbuffer ,"Performing: %s" ,buf );
10341061log_entry (logbuffer ,LVL_DEBUG );
10351062fflush (LOGOUTPUT );
10361063}
1037-
1064+
10381065send_query (buf ,dbi );
10391066
10401067update_table_thresholds (dbi ,tbl ,operation );
@@ -1085,7 +1112,7 @@ get_cmd_args(int argc, char *argv[])
10851112args -> port = 0 ;
10861113
10871114/*
1088- * Cost-Based Vacuum Delay Settings for pg_autovacuum
1115+ * Cost-Based Vacuum Delay Settings for pg_autovacuum
10891116 */
10901117args -> av_vacuum_cost_delay = -1 ;
10911118args -> av_vacuum_cost_page_hit = -1 ;
@@ -1255,7 +1282,7 @@ usage(void)
12551282fprintf (stderr ," [-m] vacuum_cost_page_miss (default=none)\n" );
12561283fprintf (stderr ," [-n] vacuum_cost_page_dirty (default=none)\n" );
12571284fprintf (stderr ," [-l] vacuum_cost_limit (default=none)\n" );
1258-
1285+
12591286fprintf (stderr ," [-U] username (libpq default)\n" );
12601287fprintf (stderr ," [-P] password (libpq default)\n" );
12611288fprintf (stderr ," [-H] host (libpq default)\n" );
@@ -1307,10 +1334,10 @@ print_cmd_args(void)
13071334log_entry (logbuffer ,LVL_INFO );
13081335sprintf (logbuffer ," args->analyze_scaling_factor=%f" ,args -> analyze_scaling_factor );
13091336log_entry (logbuffer ,LVL_INFO );
1310-
1337+
13111338if (args -> av_vacuum_cost_delay != -1 )
13121339sprintf (logbuffer ," args->av_vacuum_cost_delay=%d" ,args -> av_vacuum_cost_delay );
1313- else
1340+ else
13141341sprintf (logbuffer ," args->av_vacuum_cost_delay=(default)" );
13151342log_entry (logbuffer ,LVL_INFO );
13161343if (args -> av_vacuum_cost_page_hit != -1 )
@@ -1333,7 +1360,7 @@ print_cmd_args(void)
13331360else
13341361sprintf (logbuffer ," args->av_vacuum_cost_limit=(default)" );
13351362log_entry (logbuffer ,LVL_INFO );
1336-
1363+
13371364sprintf (logbuffer ," args->debug=%d" ,args -> debug );
13381365log_entry (logbuffer ,LVL_INFO );
13391366
@@ -1450,8 +1477,8 @@ InstallService(void)
14501477if (args -> av_vacuum_cost_page_dirty != -1 )
14511478sprintf (szCommand ,"%s -d %d" ,szCommand ,args -> av_vacuum_cost_page_dirty );
14521479if (args -> av_vacuum_cost_limit != -1 )
1453- sprintf (szCommand ,"%s -d %d" ,szCommand ,args -> av_vacuum_cost_limit );
1454-
1480+ sprintf (szCommand ,"%s -d %d" ,szCommand ,args -> av_vacuum_cost_limit );
1481+
14551482/* And write the new value */
14561483if (RegSetValueEx (hk ,"ImagePath" ,0 ,REG_EXPAND_SZ , (LPBYTE )szCommand , (DWORD )strlen (szCommand )+ 1 ))
14571484return -4 ;