16
16
17
17
18
18
static 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 ,
20
20
const char * table , const char * host , const char * port ,
21
21
const char * username , enum trivalue prompt_password ,
22
22
const char * progname , bool echo );
23
23
static 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 ,
25
25
const char * maintenance_db ,
26
26
const char * host , const char * port ,
27
27
const char * username , enum trivalue prompt_password ,
@@ -50,6 +50,7 @@ main(int argc, char *argv[])
50
50
{"full" , no_argument , NULL , 'f' },
51
51
{"verbose" , no_argument , NULL , 'v' },
52
52
{"maintenance-db" , required_argument , NULL , 2 },
53
+ {"analyze-in-stages" , no_argument , NULL , 3 },
53
54
{NULL , 0 , NULL , 0 }
54
55
};
55
56
@@ -67,6 +68,7 @@ main(int argc, char *argv[])
67
68
bool quiet = false;
68
69
bool and_analyze = false;
69
70
bool analyze_only = false;
71
+ bool analyze_in_stages = false;
70
72
bool freeze = false;
71
73
bool alldb = false;
72
74
bool full = false;
@@ -130,6 +132,9 @@ main(int argc, char *argv[])
130
132
case 2 :
131
133
maintenance_db = pg_strdup (optarg );
132
134
break ;
135
+ case 3 :
136
+ analyze_in_stages = analyze_only = true;
137
+ break ;
133
138
default :
134
139
fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
135
140
exit (1 );
@@ -189,7 +194,7 @@ main(int argc, char *argv[])
189
194
exit (1 );
190
195
}
191
196
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 ,
193
198
maintenance_db , host , port , username ,
194
199
prompt_password , progname , echo , quiet );
195
200
}
@@ -212,15 +217,15 @@ main(int argc, char *argv[])
212
217
for (cell = tables .head ; cell ; cell = cell -> next )
213
218
{
214
219
vacuum_one_database (dbname , full , verbose , and_analyze ,
215
- analyze_only ,
220
+ analyze_only , analyze_in_stages ,
216
221
freeze , cell -> val ,
217
222
host , port , username , prompt_password ,
218
223
progname , echo );
219
224
}
220
225
}
221
226
else
222
227
vacuum_one_database (dbname , full , verbose , and_analyze ,
223
- analyze_only ,
228
+ analyze_only , analyze_in_stages ,
224
229
freeze , NULL ,
225
230
host , port , username , prompt_password ,
226
231
progname , echo );
@@ -230,9 +235,26 @@ main(int argc, char *argv[])
230
235
}
231
236
232
237
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
+
233
255
static void
234
256
vacuum_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 ,
236
258
const char * host , const char * port ,
237
259
const char * username , enum trivalue prompt_password ,
238
260
const char * progname , bool echo )
@@ -300,25 +322,38 @@ vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyz
300
322
appendPQExpBuffer (& sql , " %s" , table );
301
323
appendPQExpBufferStr (& sql , ";" );
302
324
303
- if (! executeMaintenanceCommand ( conn , sql . data , echo ) )
325
+ if (analyze_in_stages )
304
326
{
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
+ }
313
345
}
346
+ else
347
+ run_vacuum_command (conn , sql .data , echo , dbname , NULL , progname );
348
+
314
349
PQfinish (conn );
315
350
termPQExpBuffer (& sql );
316
351
}
317
352
318
353
319
354
static void
320
355
vacuum_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 ,
322
357
const char * host , const char * port ,
323
358
const char * username , enum trivalue prompt_password ,
324
359
const char * progname , bool echo , bool quiet )
@@ -343,6 +378,7 @@ vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_onl
343
378
}
344
379
345
380
vacuum_one_database (dbname , full , verbose , and_analyze , analyze_only ,
381
+ analyze_in_stages ,
346
382
freeze , NULL , host , port , username , prompt_password ,
347
383
progname , echo );
348
384
}
@@ -369,6 +405,8 @@ help(const char *progname)
369
405
printf (_ (" -V, --version output version information, then exit\n" ));
370
406
printf (_ (" -z, --analyze update optimizer statistics\n" ));
371
407
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" ));
372
410
printf (_ (" -?, --help show this help, then exit\n" ));
373
411
printf (_ ("\nConnection options:\n" ));
374
412
printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));
0 commit comments