Skip to content

Commit f3af534

Browse files
committed
Support multiple -t/--table arguments for more commands
On top of the previous support in pg_dump, add support to specify multiple tables (by using the -t option multiple times) to pg_restore, clsuterdb, reindexdb and vacuumdb. Josh Kupershmidt, reviewed by Karl O. Pinc
1 parent 36bdfa5 commit f3af534

File tree

15 files changed

+191
-111
lines changed

15 files changed

+191
-111
lines changed

doc/src/sgml/ref/clusterdb.sgml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ PostgreSQL documentation
2424
<command>clusterdb</command>
2525
<arg rep="repeat"><replaceable>connection-option</replaceable></arg>
2626
<group choice="opt"><arg choice="plain"><option>--verbose</option></arg><arg choice="plain"><option>-v</option></arg></group>
27-
<arg choice="opt"><group choice="plain"><arg choice="plain"><option>--table</option></arg><arg choice="plain"><option>-t</option></arg></group> <replaceable>table</replaceable> </arg>
27+
28+
<arg choice="plain" rep="repeat">
29+
<arg choice="opt">
30+
<group choice="plain">
31+
<arg choice="plain"><option>--table</option></arg>
32+
<arg choice="plain"><option>-t</option></arg>
33+
</group>
34+
<replaceable>table</replaceable>
35+
</arg>
36+
</arg>
37+
2838
<arg choice="opt"><replaceable>dbname</replaceable></arg>
2939
</cmdsynopsis>
3040

@@ -117,6 +127,8 @@ PostgreSQL documentation
117127
<listitem>
118128
<para>
119129
Cluster <replaceable class="parameter">table</replaceable> only.
130+
Multiple tables can be clustered by writing multiple
131+
<option>-t</> switches.
120132
</para>
121133
</listitem>
122134
</varlistentry>

doc/src/sgml/ref/pg_restore.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@
400400
<term><option>--table=<replaceable class="parameter">table</replaceable></option></term>
401401
<listitem>
402402
<para>
403-
Restore definition and/or data of named table only. This can be
403+
Restore definition and/or data of named table only. Multiple tables
404+
may be specified with multiple <option>-t</> switches. This can be
404405
combined with the <option>-n</option> option to specify a schema.
405406
</para>
406407
</listitem>

doc/src/sgml/ref/reindexdb.sgml

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,27 @@ PostgreSQL documentation
2323
<cmdsynopsis>
2424
<command>reindexdb</command>
2525
<arg rep="repeat"><replaceable>connection-option</replaceable></arg>
26-
<group choice="opt">
27-
<group choice="plain">
28-
<arg choice="plain"><option>--table</option></arg>
29-
<arg choice="plain"><option>-t</option></arg>
30-
</group>
31-
<replaceable>table</replaceable>
32-
</group>
33-
<group choice="opt">
34-
<group choice="plain">
35-
<arg choice="plain"><option>--index</option></arg>
36-
<arg choice="plain"><option>-i</option></arg>
37-
</group>
38-
<replaceable>index</replaceable>
39-
</group>
26+
27+
<arg choice="plain" rep="repeat">
28+
<arg choice="opt">
29+
<group choice="plain">
30+
<arg choice="plain"><option>--table</option></arg>
31+
<arg choice="plain"><option>-t</option></arg>
32+
</group>
33+
<replaceable>table</replaceable>
34+
</arg>
35+
</arg>
36+
37+
<arg choice="plain" rep="repeat">
38+
<arg choice="opt">
39+
<group choice="plain">
40+
<arg choice="plain"><option>--index</option></arg>
41+
<arg choice="plain"><option>-i</option></arg>
42+
</group>
43+
<replaceable>index</replaceable>
44+
</arg>
45+
</arg>
46+
4047
<arg choice="opt"><replaceable>dbname</replaceable></arg>
4148
</cmdsynopsis>
4249

@@ -128,6 +135,8 @@ PostgreSQL documentation
128135
<listitem>
129136
<para>
130137
Recreate <replaceable class="parameter">index</replaceable> only.
138+
Multiple indexes can be recreated by writing multiple
139+
<option>-i</> switches.
131140
</para>
132141
</listitem>
133142
</varlistentry>
@@ -158,6 +167,8 @@ PostgreSQL documentation
158167
<listitem>
159168
<para>
160169
Reindex <replaceable class="parameter">table</replaceable> only.
170+
Multiple tables can be reindexed by writing multiple
171+
<option>-t</> switches.
161172
</para>
162173
</listitem>
163174
</varlistentry>

doc/src/sgml/ref/vacuumdb.sgml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ PostgreSQL documentation
2424
<command>vacuumdb</command>
2525
<arg rep="repeat"><replaceable>connection-option</replaceable></arg>
2626
<arg rep="repeat"><replaceable>option</replaceable></arg>
27-
<arg choice="opt">
28-
<group choice="plain">
29-
<arg choice="plain"><option>--table</option></arg>
30-
<arg choice="plain"><option>-t</option></arg>
31-
</group>
32-
<replaceable>table</replaceable>
33-
<arg choice="opt">( <replaceable class="parameter">column</replaceable> [,...] )</arg>
27+
28+
<arg choice="plain" rep="repeat">
29+
<arg choice="opt">
30+
<group choice="plain">
31+
<arg choice="plain"><option>--table</option></arg>
32+
<arg choice="plain"><option>-t</option></arg>
33+
</group>
34+
<replaceable>table</replaceable>
35+
<arg choice="opt">( <replaceable class="parameter">column</replaceable> [,...] )</arg>
36+
</arg>
3437
</arg>
38+
3539
<arg choice="opt"><replaceable>dbname</replaceable></arg>
3640
</cmdsynopsis>
3741

@@ -147,6 +151,8 @@ PostgreSQL documentation
147151
Clean or analyze <replaceable class="parameter">table</replaceable> only.
148152
Column names can be specified only in conjunction with
149153
the <option>--analyze</option> or <option>--analyze-only</option> options.
154+
Multiple tables can be vacuumed by writing multiple
155+
<option>-t</> switches.
150156
</para>
151157
<tip>
152158
<para>

src/bin/pg_dump/common.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -898,24 +898,6 @@ simple_oid_list_append(SimpleOidList *list, Oid val)
898898
list->tail = cell;
899899
}
900900

901-
void
902-
simple_string_list_append(SimpleStringList *list, const char *val)
903-
{
904-
SimpleStringListCell *cell;
905-
906-
/* this calculation correctly accounts for the null trailing byte */
907-
cell = (SimpleStringListCell *)
908-
pg_malloc(sizeof(SimpleStringListCell) + strlen(val));
909-
cell->next = NULL;
910-
strcpy(cell->val, val);
911-
912-
if (list->tail)
913-
list->tail->next = cell;
914-
else
915-
list->head = cell;
916-
list->tail = cell;
917-
}
918-
919901
bool
920902
simple_oid_list_member(SimpleOidList *list, Oid val)
921903
{
@@ -928,16 +910,3 @@ simple_oid_list_member(SimpleOidList *list, Oid val)
928910
}
929911
return false;
930912
}
931-
932-
bool
933-
simple_string_list_member(SimpleStringList *list, const char *val)
934-
{
935-
SimpleStringListCell *cell;
936-
937-
for (cell = list->head; cell; cell = cell->next)
938-
{
939-
if (strcmp(cell->val, val) == 0)
940-
return true;
941-
}
942-
return false;
943-
}

src/bin/pg_dump/dumputils.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <ctype.h>
1818

1919
#include "dumputils.h"
20+
#include "dumpmem.h"
2021

2122
#include "parser/keywords.h"
2223

@@ -1352,3 +1353,35 @@ exit_nicely(int code)
13521353

13531354
exit(code);
13541355
}
1356+
1357+
void
1358+
simple_string_list_append(SimpleStringList *list, const char *val)
1359+
{
1360+
SimpleStringListCell *cell;
1361+
1362+
/* this calculation correctly accounts for the null trailing byte */
1363+
cell = (SimpleStringListCell *)
1364+
pg_malloc(sizeof(SimpleStringListCell) + strlen(val));
1365+
1366+
cell->next = NULL;
1367+
strcpy(cell->val, val);
1368+
1369+
if (list->tail)
1370+
list->tail->next = cell;
1371+
else
1372+
list->head = cell;
1373+
list->tail = cell;
1374+
}
1375+
1376+
bool
1377+
simple_string_list_member(SimpleStringList *list, const char *val)
1378+
{
1379+
SimpleStringListCell *cell;
1380+
1381+
for (cell = list->head; cell; cell = cell->next)
1382+
{
1383+
if (strcmp(cell->val, val) == 0)
1384+
return true;
1385+
}
1386+
return false;
1387+
}

src/bin/pg_dump/dumputils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ typedef enum /* bits returned by set_dump_section */
2727
DUMP_UNSECTIONED = 0xff
2828
} DumpSections;
2929

30+
typedef struct SimpleStringListCell
31+
{
32+
struct SimpleStringListCell *next;
33+
char val[1]; /* VARIABLE LENGTH FIELD */
34+
} SimpleStringListCell;
35+
36+
typedef struct SimpleStringList
37+
{
38+
SimpleStringListCell *head;
39+
SimpleStringListCell *tail;
40+
} SimpleStringList;
41+
42+
3043
typedef void (*on_exit_nicely_callback) (int code, void *arg);
3144

3245
extern int quote_all_identifiers;
@@ -75,4 +88,8 @@ __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3), noreturn));
7588
extern void on_exit_nicely(on_exit_nicely_callback function, void *arg);
7689
extern void exit_nicely(int code) __attribute__((noreturn));
7790

91+
extern void simple_string_list_append(SimpleStringList *list, const char *val);
92+
extern bool simple_string_list_member(SimpleStringList *list, const char *val);
93+
94+
7895
#endif /* DUMPUTILS_H */

src/bin/pg_dump/pg_backup.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "postgres_fe.h"
2727

2828
#include "pg_dump.h"
29+
#include "dumputils.h"
2930

3031
#include "libpq-fe.h"
3132

@@ -125,9 +126,9 @@ typedef struct _restoreOptions
125126
int selTable;
126127
char *indexNames;
127128
char *functionNames;
128-
char *tableNames;
129129
char *schemaNames;
130130
char *triggerNames;
131+
SimpleStringList tableNames;
131132

132133
int useDB;
133134
char *dbname;

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt)
24932493
{
24942494
if (!ropt->selTable)
24952495
return 0;
2496-
if (ropt->tableNames && strcmp(ropt->tableNames, te->tag) != 0)
2496+
if (ropt->tableNames.head != NULL && (!(simple_string_list_member(&ropt->tableNames, te->tag))))
24972497
return 0;
24982498
}
24992499
else if (strcmp(te->desc, "INDEX") == 0)

src/bin/pg_dump/pg_dump.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,6 @@ typedef struct SimpleOidList
5858
SimpleOidListCell *tail;
5959
} SimpleOidList;
6060

61-
typedef struct SimpleStringListCell
62-
{
63-
struct SimpleStringListCell *next;
64-
char val[1]; /* VARIABLE LENGTH FIELD */
65-
} SimpleStringListCell;
66-
67-
typedef struct SimpleStringList
68-
{
69-
SimpleStringListCell *head;
70-
SimpleStringListCell *tail;
71-
} SimpleStringList;
7261

7362
/*
7463
* The data structures used to store system catalog information. Every
@@ -533,9 +522,7 @@ extern CollInfo *findCollationByOid(Oid oid);
533522
extern NamespaceInfo *findNamespaceByOid(Oid oid);
534523

535524
extern void simple_oid_list_append(SimpleOidList *list, Oid val);
536-
extern void simple_string_list_append(SimpleStringList *list, const char *val);
537525
extern bool simple_oid_list_member(SimpleOidList *list, Oid val);
538-
extern bool simple_string_list_member(SimpleStringList *list, const char *val);
539526

540527
extern void parseOidArray(const char *str, Oid *array, int arraysize);
541528

src/bin/pg_dump/pg_restore.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ main(int argc, char **argv)
234234
case 't': /* Dump data for this table only */
235235
opts->selTypes = 1;
236236
opts->selTable = 1;
237-
opts->tableNames = pg_strdup(optarg);
237+
simple_string_list_append(&opts->tableNames, optarg);
238238
break;
239239

240240
case 'U':
@@ -424,7 +424,7 @@ usage(const char *progname)
424424
printf(_(" -P, --function=NAME(args) restore named function\n"));
425425
printf(_(" -s, --schema-only restore only the schema, no data\n"));
426426
printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
427-
printf(_(" -t, --table=NAME restore named table\n"));
427+
printf(_(" -t, --table=NAME restore named table(s)\n"));
428428
printf(_(" -T, --trigger=NAME restore named trigger\n"));
429429
printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
430430
printf(_(" -1, --single-transaction restore as a single transaction\n"));

src/bin/scripts/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dropdb: dropdb.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
3232
droplang: droplang.o common.o print.o mbprint.o | submake-libpq
3333
dropuser: dropuser.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
3434
clusterdb: clusterdb.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
35-
vacuumdb: vacuumdb.o common.o | submake-libpq
35+
vacuumdb: vacuumdb.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
3636
reindexdb: reindexdb.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
3737

3838
dumputils.c keywords.c: % : $(top_srcdir)/src/bin/pg_dump/%

src/bin/scripts/clusterdb.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ main(int argc, char *argv[])
5858
bool echo = false;
5959
bool quiet = false;
6060
bool alldb = false;
61-
char *table = NULL;
6261
bool verbose = false;
62+
SimpleStringList tables = {NULL, NULL};
6363

6464
progname = get_progname(argv[0]);
6565
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
@@ -98,7 +98,7 @@ main(int argc, char *argv[])
9898
alldb = true;
9999
break;
100100
case 't':
101-
table = pg_strdup(optarg);
101+
simple_string_list_append(&tables, optarg);
102102
break;
103103
case 'v':
104104
verbose = true;
@@ -140,9 +140,10 @@ main(int argc, char *argv[])
140140
progname);
141141
exit(1);
142142
}
143-
if (table)
143+
144+
if (tables.head != NULL)
144145
{
145-
fprintf(stderr, _("%s: cannot cluster a specific table in all databases\n"),
146+
fprintf(stderr, _("%s: cannot cluster specific table(s) in all databases\n"),
146147
progname);
147148
exit(1);
148149
}
@@ -162,9 +163,21 @@ main(int argc, char *argv[])
162163
dbname = get_user_name(progname);
163164
}
164165

165-
cluster_one_database(dbname, verbose, table,
166-
host, port, username, prompt_password,
167-
progname, echo);
166+
if (tables.head != NULL)
167+
{
168+
SimpleStringListCell *cell;
169+
170+
for (cell = tables.head; cell; cell = cell->next)
171+
{
172+
cluster_one_database(dbname, verbose, cell->val,
173+
host, port, username, prompt_password,
174+
progname, echo);
175+
}
176+
}
177+
else
178+
cluster_one_database(dbname, verbose, NULL,
179+
host, port, username, prompt_password,
180+
progname, echo);
168181
}
169182

170183
exit(0);
@@ -253,7 +266,7 @@ help(const char *progname)
253266
printf(_(" -d, --dbname=DBNAME database to cluster\n"));
254267
printf(_(" -e, --echo show the commands being sent to the server\n"));
255268
printf(_(" -q, --quiet don't write any messages\n"));
256-
printf(_(" -t, --table=TABLE cluster specific table only\n"));
269+
printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
257270
printf(_(" -v, --verbose write a lot of output\n"));
258271
printf(_(" -V, --version output version information, then exit\n"));
259272
printf(_(" -?, --help show this help, then exit\n"));

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