Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit47bc9ce

Browse files
author
Amit Kapila
committed
Add --parallel option to vacuumdb command.
Commit40d964e allowed vacuum command to leverage multiple CPUs byinvoking parallel workers to process indexes. This commit provides a'--parallel' option to specify the parallel degree used by vacuum command.Author: Masahiko Sawada, with few modifications by meReviewed-by: Mahendra Singh and Amit KapilaDiscussion:https://postgr.es/m/CAD21AoDTPMgzSkV4E3SFo1CH_x50bf5PqZFQf4jmqjk-C03BWg@mail.gmail.com
1 parent01d9676 commit47bc9ce

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

‎doc/src/sgml/ref/vacuumdb.sgml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ PostgreSQL documentation
226226
</listitem>
227227
</varlistentry>
228228

229+
<varlistentry>
230+
<term><option>-P <replaceable class="parameter">parallel_degree</replaceable></option></term>
231+
<term><option>--parallel=<replaceable class="parameter">parallel_degree</replaceable></option></term>
232+
<listitem>
233+
<para>
234+
Specify the parallel degree of <firstterm>parallel vacuum</firstterm>.
235+
This allows the vacuum to leverage multiple CPUs to process indexes.
236+
See <xref linkend="sql-vacuum"/>.
237+
</para>
238+
<note>
239+
<para>
240+
This option is only available for servers running
241+
<productname>PostgreSQL</productname> 13 and later.
242+
</para>
243+
</note>
244+
</listitem>
245+
</varlistentry>
246+
229247
<varlistentry>
230248
<term><option>-q</option></term>
231249
<term><option>--quiet</option></term>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use PostgresNode;
55
use TestLib;
6-
use Test::Moretests=>44;
6+
use Test::Moretests=>49;
77

88
program_help_ok('vacuumdb');
99
program_version_ok('vacuumdb');
@@ -48,6 +48,14 @@
4848
$node->command_fails(
4949
['vacuumdb','--analyze-only','--disable-page-skipping','postgres' ],
5050
'--analyze-only and --disable-page-skipping specified together');
51+
$node->issues_sql_like(
52+
['vacuumdb','-P', 2,'postgres' ],
53+
qr/statement: VACUUM\(PARALLEL 2\).*;/,
54+
'vacuumdb -P 2');
55+
$node->issues_sql_like(
56+
['vacuumdb','-P', 0,'postgres' ],
57+
qr/statement: VACUUM\(PARALLEL 0\).*;/,
58+
'vacuumdb -P 0');
5159
$node->command_ok([qw(vacuumdb -Z --table=pg_am dbname=template1)],
5260
'vacuumdb with connection string');
5361

@@ -81,6 +89,9 @@
8189
$node->command_fails(
8290
['vacuumdb','--analyze','--table','vactable(c)','postgres' ],
8391
'incorrect column name with ANALYZE');
92+
$node->command_fails(
93+
['vacuumdb','-P', -1,'postgres' ],
94+
'negative parallel degree');
8495
$node->issues_sql_like(
8596
['vacuumdb','--analyze','--table','vactable(a, b)','postgres' ],
8697
qr/statement: VACUUM\(ANALYZE\) public.vactable\(a, b\);/,

‎src/bin/scripts/vacuumdb.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef struct vacuumingOptions
3535
boolskip_locked;
3636
intmin_xid_age;
3737
intmin_mxid_age;
38+
intparallel_workers;/* >= 0 indicates user specified the
39+
* parallel degree, otherwise -1 */
3840
}vacuumingOptions;
3941

4042

@@ -87,6 +89,7 @@ main(int argc, char *argv[])
8789
{"full",no_argument,NULL,'f'},
8890
{"verbose",no_argument,NULL,'v'},
8991
{"jobs",required_argument,NULL,'j'},
92+
{"parallel",required_argument,NULL,'P'},
9093
{"maintenance-db",required_argument,NULL,2},
9194
{"analyze-in-stages",no_argument,NULL,3},
9295
{"disable-page-skipping",no_argument,NULL,4},
@@ -116,14 +119,15 @@ main(int argc, char *argv[])
116119

117120
/* initialize options to all false */
118121
memset(&vacopts,0,sizeof(vacopts));
122+
vacopts.parallel_workers=-1;
119123

120124
pg_logging_init(argv[0]);
121125
progname=get_progname(argv[0]);
122126
set_pglocale_pgservice(argv[0],PG_TEXTDOMAIN("pgscripts"));
123127

124128
handle_help_version_opts(argc,argv,"vacuumdb",help);
125129

126-
while ((c=getopt_long(argc,argv,"h:p:U:wWeqd:zZFat:fvj:",long_options,&optindex))!=-1)
130+
while ((c=getopt_long(argc,argv,"h:p:U:wWeqd:zZFat:fvj:P:",long_options,&optindex))!=-1)
127131
{
128132
switch (c)
129133
{
@@ -183,6 +187,14 @@ main(int argc, char *argv[])
183187
exit(1);
184188
}
185189
break;
190+
case'P':
191+
vacopts.parallel_workers=atoi(optarg);
192+
if (vacopts.parallel_workers<0)
193+
{
194+
pg_log_error("parallel vacuum degree must be a non-negative integer");
195+
exit(1);
196+
}
197+
break;
186198
case2:
187199
maintenance_db=pg_strdup(optarg);
188200
break;
@@ -258,6 +270,23 @@ main(int argc, char *argv[])
258270
/* allow 'and_analyze' with 'analyze_only' */
259271
}
260272

273+
/* Prohibit full and analyze_only options with parallel option */
274+
if (vacopts.parallel_workers >=0)
275+
{
276+
if (vacopts.analyze_only)
277+
{
278+
pg_log_error("cannot use the \"%s\" option when performing only analyze",
279+
"parallel");
280+
exit(1);
281+
}
282+
if (vacopts.full)
283+
{
284+
pg_log_error("cannot use the \"%s\" option when performing full",
285+
"parallel");
286+
exit(1);
287+
}
288+
}
289+
261290
setup_cancel_handler(NULL);
262291

263292
/* Avoid opening extra connections. */
@@ -405,6 +434,13 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
405434
exit(1);
406435
}
407436

437+
if (vacopts->parallel_workers >=0&&PQserverVersion(conn)<130000)
438+
{
439+
pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
440+
"--parallel","13");
441+
exit(1);
442+
}
443+
408444
if (!quiet)
409445
{
410446
if (stage!=ANALYZE_NO_STAGE)
@@ -823,6 +859,14 @@ prepare_vacuum_command(PQExpBuffer sql, int serverVersion,
823859
appendPQExpBuffer(sql,"%sANALYZE",sep);
824860
sep=comma;
825861
}
862+
if (vacopts->parallel_workers >=0)
863+
{
864+
/* PARALLEL is supported since v13 */
865+
Assert(serverVersion >=130000);
866+
appendPQExpBuffer(sql,"%sPARALLEL %d",sep,
867+
vacopts->parallel_workers);
868+
sep=comma;
869+
}
826870
if (sep!=paren)
827871
appendPQExpBufferChar(sql,')');
828872
}
@@ -886,6 +930,7 @@ help(const char *progname)
886930
printf(_(" -j, --jobs=NUM use this many concurrent connections to vacuum\n"));
887931
printf(_(" --min-mxid-age=MXID_AGE minimum multixact ID age of tables to vacuum\n"));
888932
printf(_(" --min-xid-age=XID_AGE minimum transaction ID age of tables to vacuum\n"));
933+
printf(_(" -P, --parallel=PARALLEL_DEGREE use this many background workers for vacuum, if available\n"));
889934
printf(_(" -q, --quiet don't write any messages\n"));
890935
printf(_(" --skip-locked skip relations that cannot be immediately locked\n"));
891936
printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table(s) only\n"));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp