Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2cbc3c1

Browse files
committed
Rework logic and simplify syntax of REINDEX DATABASE/SYSTEM
Per discussion, this commit includes a couple of changes to these two flavors of REINDEX: * The grammar is changed to make the name of the object optional, hence one can rebuild all the indexes of the wanted area by specifying only "REINDEX DATABASE;" or "REINDEX SYSTEM;". Previously, the object name was mandatory and had to match the name of the database on which the command is issued. * REINDEX DATABASE is changed to ignore catalogs, making this task only possible with REINDEX SYSTEM. This is a historical change, but there was no way to work only on the indexes of a database without touching the catalogs. We have discussed more approaches here, like the addition of an option to skip the catalogs without changing the original behavior, but concluded that what we have here is for the best. This builds on top of the TAP tests introduced in 5fb5b6c, showing the change in behavior for REINDEX SYSTEM. reindexdb is updated so as we do not issue an extra REINDEX SYSTEM when working on a database in the non-concurrent case, something that was confusing when --concurrently got introduced, so this simplifies the code. Author: Simon Riggs Reviewed-by: Ashutosh Bapat, Bernd Helmle, Álvaro Herrera, Cary Huang, Michael Paquier Discussion: https://postgr.es/m/CANbhV-H=NH6Om4-X6cRjDWfH_Mu1usqwkuYVp-hwdB_PSHWRfg@mail.gmail.com
1 parent 5fb5b6c commit 2cbc3c1

File tree

5 files changed

+51
-37
lines changed

5 files changed

+51
-37
lines changed

doc/src/sgml/ref/reindex.sgml

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
REINDEX [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] { INDEX | TABLE | SCHEMA | DATABASE | SYSTEM } [ CONCURRENTLY ] <replaceable class="parameter">name</replaceable>
24+
REINDEX [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] { INDEX | TABLE | SCHEMA } [ CONCURRENTLY ] <replaceable class="parameter">name</replaceable>
25+
REINDEX [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] { DATABASE | SYSTEM } [ CONCURRENTLY ] [ <replaceable class="parameter">name</replaceable> ]
2526

2627
<phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
2728

@@ -126,8 +127,9 @@ REINDEX [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] { IN
126127
<term><literal>DATABASE</literal></term>
127128
<listitem>
128129
<para>
129-
Recreate all indexes within the current database.
130-
Indexes on shared system catalogs are also processed.
130+
Recreate all indexes within the current database, except system
131+
catalogs.
132+
Indexes on shared system catalogs are not processed.
131133
This form of <command>REINDEX</command> cannot be executed inside a
132134
transaction block.
133135
</para>
@@ -154,8 +156,8 @@ REINDEX [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] { IN
154156
The name of the specific index, table, or database to be
155157
reindexed. Index and table names can be schema-qualified.
156158
Presently, <command>REINDEX DATABASE</command> and <command>REINDEX SYSTEM</command>
157-
can only reindex the current database, so their parameter must match
158-
the current database's name.
159+
can only reindex the current database. Their parameter is optional,
160+
and it must match the current database's name.
159161
</para>
160162
</listitem>
161163
</varlistentry>

src/backend/commands/indexcmds.c

+16-5
Original file line numberDiff line numberDiff line change
@@ -2877,11 +2877,16 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
28772877
bool concurrent_warning = false;
28782878
bool tablespace_warning = false;
28792879

2880-
AssertArg(objectName);
28812880
Assert(objectKind == REINDEX_OBJECT_SCHEMA ||
28822881
objectKind == REINDEX_OBJECT_SYSTEM ||
28832882
objectKind == REINDEX_OBJECT_DATABASE);
28842883

2884+
/*
2885+
* This matches the options enforced by the grammar, where the object name
2886+
* is optional for DATABASE and SYSTEM.
2887+
*/
2888+
AssertArg(objectName || objectKind != REINDEX_OBJECT_SCHEMA);
2889+
28852890
if (objectKind == REINDEX_OBJECT_SYSTEM &&
28862891
(params->options & REINDEXOPT_CONCURRENTLY) != 0)
28872892
ereport(ERROR,
@@ -2906,13 +2911,13 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
29062911
{
29072912
objectOid = MyDatabaseId;
29082913

2909-
if (strcmp(objectName, get_database_name(objectOid)) != 0)
2914+
if (objectName && strcmp(objectName, get_database_name(objectOid)) != 0)
29102915
ereport(ERROR,
29112916
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
29122917
errmsg("can only reindex the currently open database")));
29132918
if (!pg_database_ownercheck(objectOid, GetUserId()))
29142919
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE,
2915-
objectName);
2920+
get_database_name(objectOid));
29162921
}
29172922

29182923
/*
@@ -2970,9 +2975,15 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
29702975
!isTempNamespace(classtuple->relnamespace))
29712976
continue;
29722977

2973-
/* Check user/system classification, and optionally skip */
2978+
/*
2979+
* Check user/system classification. SYSTEM processes all the
2980+
* catalogs, and DATABASE processes everything that's not a catalog.
2981+
*/
29742982
if (objectKind == REINDEX_OBJECT_SYSTEM &&
2975-
!IsSystemClass(relid, classtuple))
2983+
!IsCatalogRelationOid(relid))
2984+
continue;
2985+
else if (objectKind == REINDEX_OBJECT_DATABASE &&
2986+
IsCatalogRelationOid(relid))
29762987
continue;
29772988

29782989
/*

src/backend/parser/gram.y

+24-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
560560
%type <defelt> generic_option_elem alter_generic_option_elem
561561
%type <list> generic_option_list alter_generic_option_list
562562

563-
%type <ival> reindex_target_type reindex_target_multitable
563+
%type <ival> reindex_target_type reindex_target_multitable reindex_name_optional
564564

565565
%type <node> copy_generic_opt_arg copy_generic_opt_arg_list_item
566566
%type <defelt> copy_generic_opt_elem
@@ -9115,6 +9115,24 @@ ReindexStmt:
91159115
makeDefElem("concurrently", NULL, @3));
91169116
$$ = (Node *) n;
91179117
}
9118+
| REINDEX reindex_name_optional
9119+
{
9120+
ReindexStmt *n = makeNode(ReindexStmt);
9121+
n->kind = $2;
9122+
n->name = NULL;
9123+
n->relation = NULL;
9124+
n->params = NIL;
9125+
$$ = (Node *)n;
9126+
}
9127+
| REINDEX '(' utility_option_list ')' reindex_name_optional
9128+
{
9129+
ReindexStmt *n = makeNode(ReindexStmt);
9130+
n->kind = $5;
9131+
n->name = NULL;
9132+
n->relation = NULL;
9133+
n->params = $3;
9134+
$$ = (Node *)n;
9135+
}
91189136
| REINDEX '(' utility_option_list ')' reindex_target_type opt_concurrently qualified_name
91199137
{
91209138
ReindexStmt *n = makeNode(ReindexStmt);
@@ -9151,6 +9169,11 @@ reindex_target_multitable:
91519169
| SYSTEM_P { $$ = REINDEX_OBJECT_SYSTEM; }
91529170
| DATABASE { $$ = REINDEX_OBJECT_DATABASE; }
91539171
;
9172+
/* For these options the name is optional */
9173+
reindex_name_optional:
9174+
SYSTEM_P { $$ = REINDEX_OBJECT_SYSTEM; }
9175+
| DATABASE { $$ = REINDEX_OBJECT_DATABASE; }
9176+
;
91549177

91559178
/*****************************************************************************
91569179
*

src/bin/scripts/reindexdb.c

-12
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,6 @@ reindex_one_database(ConnParams *cparams, ReindexType type,
360360
{
361361
case REINDEX_DATABASE:
362362

363-
/*
364-
* Database-wide parallel reindex requires special processing.
365-
* If multiple jobs were asked, we have to reindex system
366-
* catalogs first as they cannot be processed in parallel.
367-
*/
368-
if (concurrently)
369-
pg_log_warning("cannot reindex system catalogs concurrently, skipping all");
370-
else
371-
run_reindex_command(conn, REINDEX_SYSTEM, PQdb(conn), echo,
372-
verbose, concurrently, false,
373-
tablespace);
374-
375363
/* Build a list of relations from the database */
376364
process_list = get_parallel_object_list(conn, process_type,
377365
user_list, echo);

src/bin/scripts/t/090_reindexdb.pl

+4-14
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@
8383
'SQL REINDEX run');
8484
my $relnode_info = $node->safe_psql('postgres', $compare_relfilenodes);
8585
is( $relnode_info,
86-
qq(pg_constraint|pg_constraint_oid_index|relfilenode has changed
87-
pg_constraint|pg_toast.pg_toast_<oid>_index|relfilenode has changed
86+
qq(pg_constraint|pg_constraint_oid_index|relfilenode is unchanged
87+
pg_constraint|pg_toast.pg_toast_<oid>_index|relfilenode is unchanged
8888
test1|pg_toast.pg_toast_<oid>_index|relfilenode has changed
8989
test1|test1x|relfilenode has changed),
9090
'relfilenode change after REINDEX DATABASE');
@@ -235,11 +235,6 @@
235235
$node->command_fails(
236236
[ 'reindexdb', '-j', '2', '-i', 'i1', 'postgres' ],
237237
'parallel reindexdb cannot process indexes');
238-
$node->issues_sql_like(
239-
[ 'reindexdb', '-j', '2', 'postgres' ],
240-
qr/statement:\ REINDEX SYSTEM postgres;
241-
.*statement:\ REINDEX TABLE public\.test1/s,
242-
'parallel reindexdb for database issues REINDEX SYSTEM first');
243238
# Note that the ordering of the commands is not stable, so the second
244239
# command for s2.t2 is not checked after.
245240
$node->issues_sql_like(
@@ -249,13 +244,8 @@
249244
$node->command_ok(
250245
[ 'reindexdb', '-j', '2', '-S', 's3' ],
251246
'parallel reindexdb with empty schema');
252-
$node->command_checks_all(
247+
$node->command_ok(
253248
[ 'reindexdb', '-j', '2', '--concurrently', '-d', 'postgres' ],
254-
0,
255-
[qr/^$/],
256-
[
257-
qr/^reindexdb: warning: cannot reindex system catalogs concurrently, skipping all/s
258-
],
259-
'parallel reindexdb for system with --concurrently skips catalogs');
249+
'parallel reindexdb on database, concurrently');
260250

261251
done_testing();

0 commit comments

Comments
 (0)