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 ,
2020 const char * table , const char * host , const char * port ,
2121 const char * username , enum trivalue prompt_password ,
2222 const 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 ,
2525 const char * maintenance_db ,
2626 const char * host , const char * port ,
2727 const 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[])
6768 bool quiet = false;
6869 bool and_analyze = false;
6970 bool analyze_only = false;
71+ bool analyze_in_stages = false;
7072 bool freeze = false;
7173 bool alldb = false;
7274 bool full = false;
@@ -130,6 +132,9 @@ main(int argc, char *argv[])
130132 case 2 :
131133 maintenance_db = pg_strdup (optarg );
132134 break ;
135+ case 3 :
136+ analyze_in_stages = analyze_only = true;
137+ break ;
133138 default :
134139 fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
135140 exit (1 );
@@ -189,7 +194,7 @@ main(int argc, char *argv[])
189194 exit (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 ,
193198 maintenance_db , host , port , username ,
194199 prompt_password , progname , echo , quiet );
195200 }
@@ -212,15 +217,15 @@ main(int argc, char *argv[])
212217 for (cell = tables .head ; cell ; cell = cell -> next )
213218 {
214219 vacuum_one_database (dbname , full , verbose , and_analyze ,
215- analyze_only ,
220+ analyze_only , analyze_in_stages ,
216221 freeze , cell -> val ,
217222 host , port , username , prompt_password ,
218223 progname , echo );
219224 }
220225 }
221226 else
222227 vacuum_one_database (dbname , full , verbose , and_analyze ,
223- analyze_only ,
228+ analyze_only , analyze_in_stages ,
224229 freeze , NULL ,
225230 host , port , username , prompt_password ,
226231 progname , 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 ,
236258 const char * host , const char * port ,
237259 const char * username , enum trivalue prompt_password ,
238260 const char * progname , bool echo )
@@ -300,25 +322,38 @@ vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyz
300322 appendPQExpBuffer (& sql , " %s" , table );
301323 appendPQExpBufferStr (& 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+
314349 PQfinish (conn );
315350 termPQExpBuffer (& 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 ,
322357 const char * host , const char * port ,
323358 const char * username , enum trivalue prompt_password ,
324359 const 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
345380 vacuum_one_database (dbname , full , verbose , and_analyze , analyze_only ,
381+ analyze_in_stages ,
346382 freeze , NULL , host , port , username , prompt_password ,
347383 progname , echo );
348384 }
@@ -369,6 +405,8 @@ help(const char *progname)
369405 printf (_ (" -V, --version output version information, then exit\n" ));
370406 printf (_ (" -z, --analyze update optimizer statistics\n" ));
371407 printf (_ (" -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" ));
372410 printf (_ (" -?, --help show this help, then exit\n" ));
373411 printf (_ ("\nConnection options:\n" ));
374412 printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));
0 commit comments