Skip to content

Commit 1a10df9

Browse files
author
Steeve Lennmark
committed
Add support for repacking an entire schema
This adds the option -c (--schema).
1 parent 53906c4 commit 1a10df9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

bin/pg_repack.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ static bool analyze = true;
219219
static bool alldb = false;
220220
static bool noorder = false;
221221
static SimpleStringList table_list = {NULL, NULL};
222+
static SimpleStringList schema_list = {NULL, NULL};
222223
static char *orderby = NULL;
223224
static char *tablespace = NULL;
224225
static bool moveidx = false;
@@ -239,6 +240,7 @@ static pgut_option options[] =
239240
{
240241
{ 'b', 'a', "all", &alldb },
241242
{ 'l', 't', "table", &table_list },
243+
{ 'l', 'c', "schema", &schema_list },
242244
{ 'b', 'n', "no-order", &noorder },
243245
{ 's', 'o', "order-by", &orderby },
244246
{ 's', 's', "tablespace", &tablespace },
@@ -312,6 +314,10 @@ main(int argc, char *argv[])
312314
ereport(ERROR,
313315
(errcode(EINVAL),
314316
errmsg("cannot repack specific table(s) in all databases")));
317+
if (schema_list.head)
318+
ereport(ERROR,
319+
(errcode(EINVAL),
320+
errmsg("cannot repack specific schema(s) in all databases")));
315321
repack_all_databases(orderby);
316322
}
317323
else
@@ -555,12 +561,14 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
555561
const char **params = NULL;
556562
int iparam = 0;
557563
size_t num_tables;
564+
size_t num_schemas;
558565
size_t num_params;
559566

560567
num_tables = simple_string_list_size(table_list);
568+
num_schemas = simple_string_list_size(schema_list);
561569

562570
/* 1st param is the user-specified tablespace */
563-
num_params = num_tables + 1;
571+
num_params = num_tables + num_schemas + 1;
564572
params = pgut_malloc(num_params * sizeof(char *));
565573

566574
initStringInfo(&sql);
@@ -596,6 +604,19 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
596604
}
597605
appendStringInfoString(&sql, ")");
598606
}
607+
else if (num_schemas)
608+
{
609+
appendStringInfoString(&sql, "schemaname IN (");
610+
for (cell = schema_list.head; cell; cell = cell->next)
611+
{
612+
/* Construct schema name placeholders to be used by PQexecParams */
613+
appendStringInfo(&sql, "$%d", iparam + 1);
614+
params[iparam++] = cell->val;
615+
if (cell->next)
616+
appendStringInfoString(&sql, ", ");
617+
}
618+
appendStringInfoString(&sql, ")");
619+
}
599620
else
600621
{
601622
appendStringInfoString(&sql, "pkid IS NOT NULL");
@@ -1895,6 +1916,7 @@ pgut_help(bool details)
18951916
printf("Options:\n");
18961917
printf(" -a, --all repack all databases\n");
18971918
printf(" -t, --table=TABLE repack specific table only\n");
1919+
printf(" -c, --schema=SCHEMA repack specific schema only\n");
18981920
printf(" -s, --tablespace=TBLSPC move repacked tables to a new tablespace\n");
18991921
printf(" -S, --moveidx move repacked indexes to TBLSPC too\n");
19001922
printf(" -o, --order-by=COLUMNS order by columns instead of cluster keys\n");

0 commit comments

Comments
 (0)