Skip to content

Commit bf277a4

Browse files
author
Steeve Lennmark
committed
Add support for dry run -N (--dryrun)
1 parent 53906c4 commit bf277a4

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

bin/pg_repack.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ static SimpleStringList r_index = {NULL, NULL};
226226
static bool only_indexes = false;
227227
static int wait_timeout = 60; /* in seconds */
228228
static int jobs = 0; /* number of concurrent worker conns. */
229+
static bool dryrun = false;
229230

230231
/* buffer should have at least 11 bytes */
231232
static char *
@@ -240,6 +241,7 @@ static pgut_option options[] =
240241
{ 'b', 'a', "all", &alldb },
241242
{ 'l', 't', "table", &table_list },
242243
{ 'b', 'n', "no-order", &noorder },
244+
{ 'b', 'N', "dry-run", &dryrun },
243245
{ 's', 'o', "order-by", &orderby },
244246
{ 's', 's', "tablespace", &tablespace },
245247
{ 'b', 'S', "moveidx", &moveidx },
@@ -513,9 +515,12 @@ repack_all_databases(const char *orderby)
513515
dbname = PQgetvalue(result, i, 0);
514516

515517
elog(INFO, "repacking database \"%s\"", dbname);
516-
ret = repack_one_database(orderby, errbuf, sizeof(errbuf));
517-
if (!ret)
518-
elog(INFO, "database \"%s\" skipped: %s", dbname, errbuf);
518+
if (!dryrun)
519+
{
520+
ret = repack_one_database(orderby, errbuf, sizeof(errbuf));
521+
if (!ret)
522+
elog(INFO, "database \"%s\" skipped: %s", dbname, errbuf);
523+
}
519524
}
520525

521526
CLEARPGRES(result);
@@ -1018,6 +1023,9 @@ repack_one_table(const repack_table *table, const char *orderby)
10181023
elog(DEBUG2, "sql_update : %s", table->sql_update);
10191024
elog(DEBUG2, "sql_pop : %s", table->sql_pop);
10201025

1026+
if (dryrun)
1027+
return;
1028+
10211029
/*
10221030
* 1. Setup advisory lock and trigger on main table.
10231031
*/
@@ -1693,6 +1701,10 @@ repack_table_indexes(PGresult *index_details)
16931701
continue;
16941702
}
16951703

1704+
elog(INFO, "repacking index \"%s\".\"index_%u\"", schema_name, index);
1705+
if (dryrun)
1706+
continue;
1707+
16961708
params[0] = utoa(index, buffer[0]);
16971709
res = execute("SELECT repack.repack_indexdef($1, $2, $3, true)", 3,
16981710
params);
@@ -1731,6 +1743,9 @@ repack_table_indexes(PGresult *index_details)
17311743
getstr(index_details, i, 0));
17321744
}
17331745

1746+
if (dryrun)
1747+
return true;
1748+
17341749
/* If we did not successfully repack any indexes, e.g. because of some
17351750
* error affecting every CREATE INDEX attempt, don't waste time with
17361751
* the ACCESS EXCLUSIVE lock on the table, and return false.
@@ -1899,6 +1914,7 @@ pgut_help(bool details)
18991914
printf(" -S, --moveidx move repacked indexes to TBLSPC too\n");
19001915
printf(" -o, --order-by=COLUMNS order by columns instead of cluster keys\n");
19011916
printf(" -n, --no-order do vacuum full instead of cluster\n");
1917+
printf(" -N, --dry-run print what would have been repacked\n");
19021918
printf(" -j, --jobs=NUM Use this many parallel jobs for each table\n");
19031919
printf(" -i, --index=INDEX move only the specified index\n");
19041920
printf(" -x, --only-indexes move only indexes of the specified table\n");

doc/pg_repack.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Options:
121121
-S, --moveidx move repacked indexes to *TBLSPC* too
122122
-o, --order-by=COLUMNS order by columns instead of cluster keys
123123
-n, --no-order do vacuum full instead of cluster
124+
-N, --dry-run print what would have been repacked
124125
-j, --jobs=NUM Use this many parallel jobs for each table
125126
-i, --index=INDEX move only the specified index
126127
-x, --only-indexes move only indexes of the specified table
@@ -161,6 +162,9 @@ Reorg Options
161162
Perform an online VACUUM FULL. Since version 1.2 this is the default for
162163
non-clustered tables.
163164

165+
``-N``, ``--dry-run``
166+
List what would be repacked and exit.
167+
164168
``-j``, ``--jobs``
165169
Create the specified number of extra connections to PostgreSQL, and
166170
use these extra connections to parallelize the rebuild of indexes
@@ -443,6 +447,7 @@ Releases
443447
* Bugfix: correctly handle key indexes with options such as DESC, NULL
444448
FIRST/LAST, COLLATE (pg_repack issue #3).
445449
* More helpful program output and error messages.
450+
* Added ``--dry-run`` to do a dry run.
446451

447452
* pg_repack 1.1.8
448453

0 commit comments

Comments
 (0)