Skip to content

Commit da24813

Browse files
committed
Fix vacuumdb --analyze-in-stages --all order
When running vacuumdb --analyze-in-stages --all, it needs to run the first stage across all databases before the second one, instead of running all stages in a database before processing the next one. Also respect the --quiet option with --analyze-in-stages.
1 parent 95d737f commit da24813

File tree

2 files changed

+72
-24
lines changed

2 files changed

+72
-24
lines changed

src/bin/scripts/t/102_vacuumdb_stages.pl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use strict;
22
use warnings;
33
use TestLib;
4-
use Test::More tests => 1;
4+
use Test::More tests => 2;
55

66
my $tempdir = tempdir;
77
start_test_server $tempdir;
@@ -15,3 +15,20 @@
1515
.*statement:\ RESET\ default_statistics_target;
1616
.*statement:\ ANALYZE/sx,
1717
'analyze three times');
18+
19+
20+
issues_sql_like(
21+
[ 'vacuumdb', '--analyze-in-stages', '--all' ],
22+
qr/.*statement:\ SET\ default_statistics_target=1;\ SET\ vacuum_cost_delay=0;
23+
.*statement:\ ANALYZE.*
24+
.*statement:\ SET\ default_statistics_target=1;\ SET\ vacuum_cost_delay=0;
25+
.*statement:\ ANALYZE.*
26+
.*statement:\ SET\ default_statistics_target=10;\ RESET\ vacuum_cost_delay;
27+
.*statement:\ ANALYZE.*
28+
.*statement:\ SET\ default_statistics_target=10;\ RESET\ vacuum_cost_delay;
29+
.*statement:\ ANALYZE.*
30+
.*statement:\ RESET\ default_statistics_target;
31+
.*statement:\ ANALYZE.*
32+
.*statement:\ RESET\ default_statistics_target;
33+
.*statement:\ ANALYZE/sx,
34+
'analyze more than one database in stages');

src/bin/scripts/vacuumdb.c

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717

1818
static void vacuum_one_database(const char *dbname, bool full, bool verbose,
19-
bool and_analyze, bool analyze_only, bool analyze_in_stages, bool freeze,
19+
bool and_analyze, bool analyze_only, bool analyze_in_stages, int stage, bool freeze,
2020
const char *table, const char *host, const char *port,
2121
const char *username, enum trivalue prompt_password,
22-
const char *progname, bool echo);
22+
const char *progname, bool echo, bool quiet);
2323
static void vacuum_all_databases(bool full, bool verbose, bool and_analyze,
2424
bool analyze_only, bool analyze_in_stages, bool freeze,
2525
const char *maintenance_db,
@@ -217,18 +217,18 @@ main(int argc, char *argv[])
217217
for (cell = tables.head; cell; cell = cell->next)
218218
{
219219
vacuum_one_database(dbname, full, verbose, and_analyze,
220-
analyze_only, analyze_in_stages,
220+
analyze_only, analyze_in_stages, -1,
221221
freeze, cell->val,
222222
host, port, username, prompt_password,
223-
progname, echo);
223+
progname, echo, quiet);
224224
}
225225
}
226226
else
227227
vacuum_one_database(dbname, full, verbose, and_analyze,
228-
analyze_only, analyze_in_stages,
228+
analyze_only, analyze_in_stages, -1,
229229
freeze, NULL,
230230
host, port, username, prompt_password,
231-
progname, echo);
231+
progname, echo, quiet);
232232
}
233233

234234
exit(0);
@@ -254,10 +254,10 @@ run_vacuum_command(PGconn *conn, const char *sql, bool echo, const char *dbname,
254254

255255
static void
256256
vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyze,
257-
bool analyze_only, bool analyze_in_stages, bool freeze, const char *table,
257+
bool analyze_only, bool analyze_in_stages, int stage, bool freeze, const char *table,
258258
const char *host, const char *port,
259259
const char *username, enum trivalue prompt_password,
260-
const char *progname, bool echo)
260+
const char *progname, bool echo, bool quiet)
261261
{
262262
PQExpBufferData sql;
263263

@@ -334,14 +334,36 @@ vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyz
334334
gettext_noop("Generating medium optimizer statistics (10 targets)"),
335335
gettext_noop("Generating default (full) optimizer statistics")
336336
};
337-
int i;
338337

339-
for (i = 0; i < 3; i++)
338+
if (stage == -1)
339+
{
340+
int i;
341+
342+
/* Run all stages. */
343+
for (i = 0; i < 3; i++)
344+
{
345+
if (!quiet)
346+
{
347+
puts(gettext(stage_messages[i]));
348+
fflush(stdout);
349+
}
350+
executeCommand(conn, stage_commands[i], progname, echo);
351+
run_vacuum_command(conn, sql.data, echo, dbname, table, progname);
352+
}
353+
}
354+
else
340355
{
341-
puts(gettext(stage_messages[i]));
342-
executeCommand(conn, stage_commands[i], progname, echo);
356+
/* Otherwise, we got a stage from vacuum_all_databases(), so run
357+
* only that one. */
358+
if (!quiet)
359+
{
360+
puts(gettext(stage_messages[stage]));
361+
fflush(stdout);
362+
}
363+
executeCommand(conn, stage_commands[stage], progname, echo);
343364
run_vacuum_command(conn, sql.data, echo, dbname, table, progname);
344365
}
366+
345367
}
346368
else
347369
run_vacuum_command(conn, sql.data, echo, dbname, NULL, progname);
@@ -360,27 +382,36 @@ vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_onl
360382
{
361383
PGconn *conn;
362384
PGresult *result;
363-
int i;
385+
int stage;
364386

365387
conn = connectMaintenanceDatabase(maintenance_db, host, port,
366388
username, prompt_password, progname);
367389
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
368390
PQfinish(conn);
369391

370-
for (i = 0; i < PQntuples(result); i++)
392+
/* If analyzing in stages, then run through all stages. Otherwise just
393+
* run once, passing -1 as the stage. */
394+
for (stage = (analyze_in_stages ? 0 : -1);
395+
stage < (analyze_in_stages ? 3 : 0);
396+
stage++)
371397
{
372-
char *dbname = PQgetvalue(result, i, 0);
398+
int i;
373399

374-
if (!quiet)
400+
for (i = 0; i < PQntuples(result); i++)
375401
{
376-
printf(_("%s: vacuuming database \"%s\"\n"), progname, dbname);
377-
fflush(stdout);
378-
}
402+
char *dbname = PQgetvalue(result, i, 0);
379403

380-
vacuum_one_database(dbname, full, verbose, and_analyze, analyze_only,
381-
analyze_in_stages,
382-
freeze, NULL, host, port, username, prompt_password,
383-
progname, echo);
404+
if (!quiet)
405+
{
406+
printf(_("%s: vacuuming database \"%s\"\n"), progname, dbname);
407+
fflush(stdout);
408+
}
409+
410+
vacuum_one_database(dbname, full, verbose, and_analyze, analyze_only,
411+
analyze_in_stages, stage,
412+
freeze, NULL, host, port, username, prompt_password,
413+
progname, echo, quiet);
414+
}
384415
}
385416

386417
PQclear(result);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy