Skip to content

Commit d107176

Browse files
vacuumdb: Add --dry-run.
This new option instructs vacuumdb to print, but not execute, the VACUUM and ANALYZE commands that would've been sent to the server. Author: Corey Huinker <corey.huinker@gmail.com> Reviewed-by: Chao Li <li.evan.chao@gmail.com> Reviewed-by: Kirill Reshke <reshkekirill@gmail.com> Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de> Discussion: https://postgr.es/m/CADkLM%3DckHkX7Of5SrK7g0LokPUwJ%3Dkk8JU1GXGF5pZ1eBVr0%3DQ%40mail.gmail.com
1 parent 7508169 commit d107176

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

doc/src/sgml/ref/vacuumdb.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ PostgreSQL documentation
171171
</listitem>
172172
</varlistentry>
173173

174+
<varlistentry>
175+
<term><option>--dry-run</option></term>
176+
<listitem>
177+
<para>
178+
Print, but do not execute, the vacuum and analyze commands that would
179+
have been sent to the server.
180+
</para>
181+
</listitem>
182+
</varlistentry>
183+
174184
<varlistentry>
175185
<term><option>-e</option></term>
176186
<term><option>--echo</option></term>

src/bin/scripts/t/100_vacuumdb.pl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@
169169
[ 'vacuumdb', '--schema' => '"Foo"', 'postgres' ],
170170
qr/VACUUM \(SKIP_DATABASE_STATS\) "Foo".bar/,
171171
'vacuumdb --schema');
172+
$node->issues_sql_unlike(
173+
[ 'vacuumdb', '--schema' => '"Foo"', 'postgres', '--dry-run' ],
174+
qr/VACUUM \(SKIP_DATABASE_STATS\) "Foo".bar/,
175+
'vacuumdb --dry-run');
172176
$node->issues_sql_like(
173177
[ 'vacuumdb', '--schema' => '"Foo"', '--schema' => '"Bar"', 'postgres' ],
174178
qr/VACUUM\ \(SKIP_DATABASE_STATS\)\ "Foo".bar
@@ -241,6 +245,14 @@
241245
CREATE TABLE regression_vacuumdb_test AS select generate_series(1, 10) a, generate_series(2, 11) b;
242246
ALTER TABLE regression_vacuumdb_test ADD COLUMN c INT GENERATED ALWAYS AS (a + b);
243247
|);
248+
$node->issues_sql_unlike(
249+
[
250+
'vacuumdb', '--analyze-only', '--dry-run',
251+
'--missing-stats-only', '-t',
252+
'regression_vacuumdb_test', 'postgres'
253+
],
254+
qr/statement:\ ANALYZE/sx,
255+
'--missing-stats-only --dry-run');
244256
$node->issues_sql_like(
245257
[
246258
'vacuumdb', '--analyze-only',

src/bin/scripts/vacuumdb.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ main(int argc, char *argv[])
5959
{"no-process-main", no_argument, NULL, 12},
6060
{"buffer-usage-limit", required_argument, NULL, 13},
6161
{"missing-stats-only", no_argument, NULL, 14},
62+
{"dry-run", no_argument, NULL, 15},
6263
{NULL, 0, NULL, 0}
6364
};
6465

@@ -207,6 +208,9 @@ main(int argc, char *argv[])
207208
case 14:
208209
vacopts.missing_stats_only = true;
209210
break;
211+
case 15:
212+
vacopts.dry_run = true;
213+
break;
210214
default:
211215
/* getopt_long already emitted a complaint */
212216
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
@@ -303,6 +307,10 @@ main(int argc, char *argv[])
303307
pg_fatal("cannot use the \"%s\" option without \"%s\" or \"%s\"",
304308
"missing-stats-only", "analyze-only", "analyze-in-stages");
305309

310+
if (vacopts.dry_run && !vacopts.quiet)
311+
pg_log_info("Executing in dry-run mode.\n"
312+
"No commands will be sent to the server.");
313+
306314
ret = vacuuming_main(&cparams, dbname, maintenance_db, &vacopts,
307315
&objects, tbl_count,
308316
concurrentCons,
@@ -345,6 +353,7 @@ help(const char *progname)
345353
printf(_(" --buffer-usage-limit=SIZE size of ring buffer used for vacuum\n"));
346354
printf(_(" -d, --dbname=DBNAME database to vacuum\n"));
347355
printf(_(" --disable-page-skipping disable all page-skipping behavior\n"));
356+
printf(_(" --dry-run show the commands that would be sent to the server\n"));
348357
printf(_(" -e, --echo show the commands being sent to the server\n"));
349358
printf(_(" -f, --full do full vacuuming\n"));
350359
printf(_(" -F, --freeze freeze row transaction information\n"));

src/bin/scripts/vacuuming.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ static SimpleStringList *retrieve_objects(PGconn *conn,
4242
static void free_retrieved_objects(SimpleStringList *list);
4343
static void prepare_vacuum_command(PGconn *conn, PQExpBuffer sql,
4444
vacuumingOptions *vacopts, const char *table);
45-
static void run_vacuum_command(PGconn *conn, vacuumingOptions *vacopts,
46-
const char *sql, const char *table);
45+
static void run_vacuum_command(ParallelSlot *free_slot,
46+
vacuumingOptions *vacopts, const char *sql,
47+
const char *table);
4748

4849
/*
4950
* Executes vacuum/analyze as indicated. Returns 0 if the plan is carried
@@ -340,7 +341,11 @@ vacuum_one_database(ConnParams *cparams,
340341
if (vacopts->mode == MODE_ANALYZE_IN_STAGES)
341342
{
342343
initcmd = stage_commands[stage];
343-
executeCommand(conn, initcmd, vacopts->echo);
344+
345+
if (vacopts->dry_run)
346+
printf("%s\n", initcmd);
347+
else
348+
executeCommand(conn, initcmd, vacopts->echo);
344349
}
345350
else
346351
initcmd = NULL;
@@ -383,7 +388,7 @@ vacuum_one_database(ConnParams *cparams,
383388
* through ParallelSlotsGetIdle.
384389
*/
385390
ParallelSlotSetHandler(free_slot, TableCommandResultHandler, NULL);
386-
run_vacuum_command(free_slot->connection, vacopts, sql.data, tabname);
391+
run_vacuum_command(free_slot, vacopts, sql.data, tabname);
387392

388393
cell = cell->next;
389394
} while (cell != NULL);
@@ -407,7 +412,7 @@ vacuum_one_database(ConnParams *cparams,
407412
}
408413

409414
ParallelSlotSetHandler(free_slot, TableCommandResultHandler, NULL);
410-
run_vacuum_command(free_slot->connection, vacopts, cmd, NULL);
415+
run_vacuum_command(free_slot, vacopts, cmd, NULL);
411416

412417
if (!ParallelSlotsWaitCompletion(sa))
413418
ret = EXIT_FAILURE; /* error already reported by handler */
@@ -995,20 +1000,25 @@ prepare_vacuum_command(PGconn *conn, PQExpBuffer sql,
9951000

9961001
/*
9971002
* Send a vacuum/analyze command to the server, returning after sending the
998-
* command.
1003+
* command. If dry_run is true, the command is printed but not sent to the
1004+
* server.
9991005
*
10001006
* Any errors during command execution are reported to stderr.
10011007
*/
10021008
static void
1003-
run_vacuum_command(PGconn *conn, vacuumingOptions *vacopts,
1009+
run_vacuum_command(ParallelSlot *free_slot, vacuumingOptions *vacopts,
10041010
const char *sql, const char *table)
10051011
{
1006-
bool status;
1012+
bool status = true;
1013+
PGconn *conn = free_slot->connection;
10071014

1008-
if (vacopts->echo)
1015+
if (vacopts->echo || vacopts->dry_run)
10091016
printf("%s\n", sql);
10101017

1011-
status = PQsendQuery(conn, sql) == 1;
1018+
if (vacopts->dry_run)
1019+
ParallelSlotSetIdle(free_slot);
1020+
else
1021+
status = PQsendQuery(conn, sql) == 1;
10121022

10131023
if (!status)
10141024
{

src/bin/scripts/vacuuming.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ typedef struct vacuumingOptions
5353
bool missing_stats_only;
5454
bool echo;
5555
bool quiet;
56+
bool dry_run;
5657
} vacuumingOptions;
5758

5859
/* Valid values for vacuumingOptions->objfilter */

0 commit comments

Comments
 (0)