1616
1717
1818static void vacuum_one_database (const char * dbname ,bool full ,bool verbose ,
19- bool and_analyze ,bool analyze_only ,bool freeze ,
19+ bool and_analyze ,bool analyze_only ,bool analyze_in_stages , bool freeze ,
2020const char * table ,const char * host ,const char * port ,
2121const char * username ,enum trivalue prompt_password ,
2222const char * progname ,bool echo );
2323static void vacuum_all_databases (bool full ,bool verbose ,bool and_analyze ,
24- bool analyze_only ,bool freeze ,
24+ bool analyze_only ,bool analyze_in_stages , bool freeze ,
2525const char * maintenance_db ,
2626const char * host ,const char * port ,
2727const char * username ,enum trivalue prompt_password ,
@@ -50,6 +50,7 @@ main(int argc, char *argv[])
5050{"full" ,no_argument ,NULL ,'f' },
5151{"verbose" ,no_argument ,NULL ,'v' },
5252{"maintenance-db" ,required_argument ,NULL ,2 },
53+ {"analyze-in-stages" ,no_argument ,NULL ,3 },
5354{NULL ,0 ,NULL ,0 }
5455};
5556
@@ -67,6 +68,7 @@ main(int argc, char *argv[])
6768bool quiet = false;
6869bool and_analyze = false;
6970bool analyze_only = false;
71+ bool analyze_in_stages = false;
7072bool freeze = false;
7173bool alldb = false;
7274bool full = false;
@@ -130,6 +132,9 @@ main(int argc, char *argv[])
130132case 2 :
131133maintenance_db = pg_strdup (optarg );
132134break ;
135+ case 3 :
136+ analyze_in_stages = analyze_only = true;
137+ break ;
133138default :
134139fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),progname );
135140exit (1 );
@@ -189,7 +194,7 @@ main(int argc, char *argv[])
189194exit (1 );
190195}
191196
192- vacuum_all_databases (full ,verbose ,and_analyze ,analyze_only ,freeze ,
197+ vacuum_all_databases (full ,verbose ,and_analyze ,analyze_only ,analyze_in_stages , freeze ,
193198maintenance_db ,host ,port ,username ,
194199prompt_password ,progname ,echo ,quiet );
195200}
@@ -212,15 +217,15 @@ main(int argc, char *argv[])
212217for (cell = tables .head ;cell ;cell = cell -> next )
213218{
214219vacuum_one_database (dbname ,full ,verbose ,and_analyze ,
215- analyze_only ,
220+ analyze_only ,analyze_in_stages ,
216221freeze ,cell -> val ,
217222host ,port ,username ,prompt_password ,
218223progname ,echo );
219224}
220225}
221226else
222227vacuum_one_database (dbname ,full ,verbose ,and_analyze ,
223- analyze_only ,
228+ analyze_only ,analyze_in_stages ,
224229freeze ,NULL ,
225230host ,port ,username ,prompt_password ,
226231progname ,echo );
@@ -230,9 +235,26 @@ main(int argc, char *argv[])
230235}
231236
232237
238+ static void
239+ run_vacuum_command (PGconn * conn ,const char * sql ,bool echo ,const char * dbname ,const char * table ,const char * progname )
240+ {
241+ if (!executeMaintenanceCommand (conn ,sql ,echo ))
242+ {
243+ if (table )
244+ fprintf (stderr ,_ ("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s" ),
245+ progname ,table ,dbname ,PQerrorMessage (conn ));
246+ else
247+ fprintf (stderr ,_ ("%s: vacuuming of database \"%s\" failed: %s" ),
248+ progname ,dbname ,PQerrorMessage (conn ));
249+ PQfinish (conn );
250+ exit (1 );
251+ }
252+ }
253+
254+
233255static void
234256vacuum_one_database (const char * dbname ,bool full ,bool verbose ,bool and_analyze ,
235- bool analyze_only ,bool freeze ,const char * table ,
257+ bool analyze_only ,bool analyze_in_stages , bool freeze ,const char * table ,
236258const char * host ,const char * port ,
237259const char * username ,enum trivalue prompt_password ,
238260const char * progname ,bool echo )
@@ -300,25 +322,38 @@ vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyz
300322appendPQExpBuffer (& sql ," %s" ,table );
301323appendPQExpBufferStr (& sql ,";" );
302324
303- if (! executeMaintenanceCommand ( conn , sql . data , echo ) )
325+ if (analyze_in_stages )
304326{
305- if (table )
306- fprintf (stderr ,_ ("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s" ),
307- progname ,table ,dbname ,PQerrorMessage (conn ));
308- else
309- fprintf (stderr ,_ ("%s: vacuuming of database \"%s\" failed: %s" ),
310- progname ,dbname ,PQerrorMessage (conn ));
311- PQfinish (conn );
312- exit (1 );
327+ const char * stage_commands []= {
328+ "SET default_statistics_target=1; SET vacuum_cost_delay=0;" ,
329+ "SET default_statistics_target=10; RESET vacuum_cost_delay;" ,
330+ "RESET default_statistics_target;"
331+ };
332+ const char * stage_messages []= {
333+ gettext_noop ("Generating minimal optimizer statistics (1 target)" ),
334+ gettext_noop ("Generating medium optimizer statistics (10 targets)" ),
335+ gettext_noop ("Generating default (full) optimizer statistics" )
336+ };
337+ int i ;
338+
339+ for (i = 0 ;i < 3 ;i ++ )
340+ {
341+ puts (gettext (stage_messages [i ]));
342+ executeCommand (conn ,stage_commands [i ],progname ,echo );
343+ run_vacuum_command (conn ,sql .data ,echo ,dbname ,table ,progname );
344+ }
313345}
346+ else
347+ run_vacuum_command (conn ,sql .data ,echo ,dbname ,NULL ,progname );
348+
314349PQfinish (conn );
315350termPQExpBuffer (& sql );
316351}
317352
318353
319354static void
320355vacuum_all_databases (bool full ,bool verbose ,bool and_analyze ,bool analyze_only ,
321- bool freeze ,const char * maintenance_db ,
356+ bool analyze_in_stages , bool freeze ,const char * maintenance_db ,
322357const char * host ,const char * port ,
323358const char * username ,enum trivalue prompt_password ,
324359const char * progname ,bool echo ,bool quiet )
@@ -343,6 +378,7 @@ vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_onl
343378}
344379
345380vacuum_one_database (dbname ,full ,verbose ,and_analyze ,analyze_only ,
381+ analyze_in_stages ,
346382freeze ,NULL ,host ,port ,username ,prompt_password ,
347383progname ,echo );
348384}
@@ -369,6 +405,8 @@ help(const char *progname)
369405printf (_ (" -V, --version output version information, then exit\n" ));
370406printf (_ (" -z, --analyze update optimizer statistics\n" ));
371407printf (_ (" -Z, --analyze-only only update optimizer statistics\n" ));
408+ printf (_ (" --analyze-in-stages only update optimizer statistics, in multiple\n"
409+ " stages for faster results\n" ));
372410printf (_ (" -?, --help show this help, then exit\n" ));
373411printf (_ ("\nConnection options:\n" ));
374412printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));