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

Commit ecd222e

Browse files
committed
Support VERBOSE option in REINDEX command.
When this option is specified, a progress report is printed as each index is reindexed. Per discussion, we agreed on the following syntax for the extensibility of the options. REINDEX (flexible options) { INDEX | ... } name Sawada Masahiko. Reviewed by Robert Haas, Fabrízio Mello, Alvaro Herrera, Kyotaro Horiguchi, Jim Nasby and me. Discussion: CAD21AoA0pK3YcOZAFzMae+2fcc3oGp5zoRggDyMNg5zoaWDhdQ@mail.gmail.com
1 parent 4b8f797 commit ecd222e

File tree

14 files changed

+112
-46
lines changed

14 files changed

+112
-46
lines changed

doc/src/sgml/ref/reindex.sgml

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
REINDEX { INDEX | TABLE | SCHEMA | DATABASE | SYSTEM } <replaceable class="PARAMETER">name</replaceable>
24+
REINDEX [ ( { VERBOSE } [, ...] ) ] { INDEX | TABLE | SCHEMA | DATABASE | SYSTEM } <replaceable class="PARAMETER">name</replaceable>
2525
</synopsis>
2626
</refsynopsisdiv>
2727

@@ -150,6 +150,15 @@ REINDEX { INDEX | TABLE | SCHEMA | DATABASE | SYSTEM } <replaceable class="PARAM
150150
</para>
151151
</listitem>
152152
</varlistentry>
153+
154+
<varlistentry>
155+
<term><literal>VERBOSE</literal></term>
156+
<listitem>
157+
<para>
158+
Prints a progress report as each index is reindexed.
159+
</para>
160+
</listitem>
161+
</varlistentry>
153162
</variablelist>
154163
</refsect1>
155164

src/backend/catalog/index.c

+17-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "utils/inval.h"
6464
#include "utils/lsyscache.h"
6565
#include "utils/memutils.h"
66+
#include "utils/pg_rusage.h"
6667
#include "utils/syscache.h"
6768
#include "utils/tuplesort.h"
6869
#include "utils/snapmgr.h"
@@ -3184,13 +3185,17 @@ IndexGetRelation(Oid indexId, bool missing_ok)
31843185
* reindex_index - This routine is used to recreate a single index
31853186
*/
31863187
void
3187-
reindex_index(Oid indexId, bool skip_constraint_checks, char persistence)
3188+
reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
3189+
int options)
31883190
{
31893191
Relation iRel,
31903192
heapRelation;
31913193
Oid heapId;
31923194
IndexInfo *indexInfo;
31933195
volatile bool skipped_constraint = false;
3196+
PGRUsage ru0;
3197+
3198+
pg_rusage_init(&ru0);
31943199

31953200
/*
31963201
* Open and lock the parent heap relation. ShareLock is sufficient since
@@ -3334,6 +3339,14 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence)
33343339
heap_close(pg_index, RowExclusiveLock);
33353340
}
33363341

3342+
/* Log what we did */
3343+
if (options & REINDEXOPT_VERBOSE)
3344+
ereport(INFO,
3345+
(errmsg("index \"%s\" was reindexed",
3346+
get_rel_name(indexId)),
3347+
errdetail("%s.",
3348+
pg_rusage_show(&ru0))));
3349+
33373350
/* Close rels, but keep locks */
33383351
index_close(iRel, NoLock);
33393352
heap_close(heapRelation, NoLock);
@@ -3375,7 +3388,7 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence)
33753388
* index rebuild.
33763389
*/
33773390
bool
3378-
reindex_relation(Oid relid, int flags)
3391+
reindex_relation(Oid relid, int flags, int options)
33793392
{
33803393
Relation rel;
33813394
Oid toast_relid;
@@ -3466,7 +3479,7 @@ reindex_relation(Oid relid, int flags)
34663479
RelationSetIndexList(rel, doneIndexes, InvalidOid);
34673480

34683481
reindex_index(indexOid, !(flags & REINDEX_REL_CHECK_CONSTRAINTS),
3469-
persistence);
3482+
persistence, options);
34703483

34713484
CommandCounterIncrement();
34723485

@@ -3501,7 +3514,7 @@ reindex_relation(Oid relid, int flags)
35013514
* still hold the lock on the master table.
35023515
*/
35033516
if ((flags & REINDEX_REL_PROCESS_TOAST) && OidIsValid(toast_relid))
3504-
result |= reindex_relation(toast_relid, flags);
3517+
result |= reindex_relation(toast_relid, flags, options);
35053518

35063519
return result;
35073520
}

src/backend/commands/cluster.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
15321532
else if (newrelpersistence == RELPERSISTENCE_PERMANENT)
15331533
reindex_flags |= REINDEX_REL_FORCE_INDEXES_PERMANENT;
15341534

1535-
reindex_relation(OIDOldHeap, reindex_flags);
1535+
reindex_relation(OIDOldHeap, reindex_flags, 0);
15361536

15371537
/*
15381538
* If the relation being rebuild is pg_class, swap_relation_files()

src/backend/commands/indexcmds.c

+15-10
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ ChooseIndexColumnNames(List *indexElems)
16811681
* Recreate a specific index.
16821682
*/
16831683
Oid
1684-
ReindexIndex(RangeVar *indexRelation)
1684+
ReindexIndex(RangeVar *indexRelation, int options)
16851685
{
16861686
Oid indOid;
16871687
Oid heapOid = InvalidOid;
@@ -1706,7 +1706,7 @@ ReindexIndex(RangeVar *indexRelation)
17061706
persistence = irel->rd_rel->relpersistence;
17071707
index_close(irel, NoLock);
17081708

1709-
reindex_index(indOid, false, persistence);
1709+
reindex_index(indOid, false, persistence, options);
17101710

17111711
return indOid;
17121712
}
@@ -1775,7 +1775,7 @@ RangeVarCallbackForReindexIndex(const RangeVar *relation,
17751775
* Recreate all indexes of a table (and of its toast table, if any)
17761776
*/
17771777
Oid
1778-
ReindexTable(RangeVar *relation)
1778+
ReindexTable(RangeVar *relation, int options)
17791779
{
17801780
Oid heapOid;
17811781

@@ -1785,7 +1785,8 @@ ReindexTable(RangeVar *relation)
17851785

17861786
if (!reindex_relation(heapOid,
17871787
REINDEX_REL_PROCESS_TOAST |
1788-
REINDEX_REL_CHECK_CONSTRAINTS))
1788+
REINDEX_REL_CHECK_CONSTRAINTS,
1789+
options))
17891790
ereport(NOTICE,
17901791
(errmsg("table \"%s\" has no indexes",
17911792
relation->relname)));
@@ -1802,7 +1803,8 @@ ReindexTable(RangeVar *relation)
18021803
* That means this must not be called within a user transaction block!
18031804
*/
18041805
void
1805-
ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind)
1806+
ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
1807+
int options)
18061808
{
18071809
Oid objectOid;
18081810
Relation relationRelation;
@@ -1938,11 +1940,14 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind)
19381940
PushActiveSnapshot(GetTransactionSnapshot());
19391941
if (reindex_relation(relid,
19401942
REINDEX_REL_PROCESS_TOAST |
1941-
REINDEX_REL_CHECK_CONSTRAINTS))
1942-
ereport(DEBUG1,
1943-
(errmsg("table \"%s.%s\" was reindexed",
1944-
get_namespace_name(get_rel_namespace(relid)),
1945-
get_rel_name(relid))));
1943+
REINDEX_REL_CHECK_CONSTRAINTS,
1944+
options))
1945+
1946+
if (options & REINDEXOPT_VERBOSE)
1947+
ereport(INFO,
1948+
(errmsg("table \"%s.%s\" was reindexed",
1949+
get_namespace_name(get_rel_namespace(relid)),
1950+
get_rel_name(relid))));
19461951
PopActiveSnapshot();
19471952
CommitTransactionCommand();
19481953
}

src/backend/commands/tablecmds.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ ExecuteTruncate(TruncateStmt *stmt)
12341234
/*
12351235
* Reconstruct the indexes to match, and we're done.
12361236
*/
1237-
reindex_relation(heap_relid, REINDEX_REL_PROCESS_TOAST);
1237+
reindex_relation(heap_relid, REINDEX_REL_PROCESS_TOAST, 0);
12381238
}
12391239

12401240
pgstat_count_truncate(rel);

src/backend/nodes/copyfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -3856,6 +3856,7 @@ _copyReindexStmt(const ReindexStmt *from)
38563856
COPY_SCALAR_FIELD(kind);
38573857
COPY_NODE_FIELD(relation);
38583858
COPY_STRING_FIELD(name);
3859+
COPY_SCALAR_FIELD(options);
38593860

38603861
return newnode;
38613862
}

src/backend/nodes/equalfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,7 @@ _equalReindexStmt(const ReindexStmt *a, const ReindexStmt *b)
19351935
COMPARE_SCALAR_FIELD(kind);
19361936
COMPARE_NODE_FIELD(relation);
19371937
COMPARE_STRING_FIELD(name);
1938+
COMPARE_SCALAR_FIELD(options);
19381939

19391940
return true;
19401941
}

src/backend/parser/gram.y

+36-21
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
463463
%type <node> explain_option_arg
464464
%type <defelt> explain_option_elem
465465
%type <list> explain_option_list
466+
467+
%type <ival> reindex_target_type reindex_target_multitable
468+
%type <ival> reindex_option_list reindex_option_elem
469+
466470
%type <node> copy_generic_opt_arg copy_generic_opt_arg_list_item
467471
%type <defelt> copy_generic_opt_elem
468472
%type <list> copy_generic_opt_list copy_generic_opt_arg_list
@@ -7387,52 +7391,63 @@ DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_d
73877391
*
73887392
* QUERY:
73897393
*
7390-
* REINDEX type <name>
7394+
* REINDEX [ (options) ] type <name>
73917395
*****************************************************************************/
73927396

73937397
ReindexStmt:
7394-
REINDEX INDEX qualified_name
7398+
REINDEX reindex_target_type qualified_name
73957399
{
73967400
ReindexStmt *n = makeNode(ReindexStmt);
7397-
n->kind = REINDEX_OBJECT_INDEX;
7401+
n->kind = $2;
73987402
n->relation = $3;
73997403
n->name = NULL;
7404+
n->options = 0;
74007405
$$ = (Node *)n;
74017406
}
7402-
| REINDEX TABLE qualified_name
7407+
| REINDEX reindex_target_multitable name
74037408
{
74047409
ReindexStmt *n = makeNode(ReindexStmt);
7405-
n->kind = REINDEX_OBJECT_TABLE;
7406-
n->relation = $3;
7407-
n->name = NULL;
7408-
$$ = (Node *)n;
7409-
}
7410-
| REINDEX SCHEMA name
7411-
{
7412-
ReindexStmt *n = makeNode(ReindexStmt);
7413-
n->kind = REINDEX_OBJECT_SCHEMA;
7410+
n->kind = $2;
74147411
n->name = $3;
74157412
n->relation = NULL;
7413+
n->options = 0;
74167414
$$ = (Node *)n;
74177415
}
7418-
| REINDEX SYSTEM_P name
7416+
| REINDEX '(' reindex_option_list ')' reindex_target_type qualified_name
74197417
{
74207418
ReindexStmt *n = makeNode(ReindexStmt);
7421-
n->kind = REINDEX_OBJECT_SYSTEM;
7422-
n->name = $3;
7423-
n->relation = NULL;
7419+
n->kind = $5;
7420+
n->relation = $6;
7421+
n->name = NULL;
7422+
n->options = $3;
74247423
$$ = (Node *)n;
74257424
}
7426-
| REINDEX DATABASE name
7425+
| REINDEX '(' reindex_option_list ')' reindex_target_multitable name
74277426
{
74287427
ReindexStmt *n = makeNode(ReindexStmt);
7429-
n->kind = REINDEX_OBJECT_DATABASE;
7430-
n->name = $3;
7428+
n->kind = $5;
7429+
n->name = $6;
74317430
n->relation = NULL;
7431+
n->options = $3;
74327432
$$ = (Node *)n;
74337433
}
74347434
;
7435-
7435+
reindex_target_type:
7436+
INDEX { $$ = REINDEX_OBJECT_INDEX; }
7437+
| TABLE { $$ = REINDEX_OBJECT_TABLE; }
7438+
;
7439+
reindex_target_multitable:
7440+
SCHEMA { $$ = REINDEX_OBJECT_SCHEMA; }
7441+
| SYSTEM_P { $$ = REINDEX_OBJECT_SYSTEM; }
7442+
| DATABASE { $$ = REINDEX_OBJECT_DATABASE; }
7443+
;
7444+
reindex_option_list:
7445+
reindex_option_elem { $$ = $1; }
7446+
| reindex_option_list ',' reindex_option_elem { $$ = $1 | $3; }
7447+
;
7448+
reindex_option_elem:
7449+
VERBOSE { $$ = REINDEXOPT_VERBOSE; }
7450+
;
74367451

74377452
/*****************************************************************************
74387453
*

src/backend/tcop/utility.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,10 @@ standard_ProcessUtility(Node *parsetree,
762762
switch (stmt->kind)
763763
{
764764
case REINDEX_OBJECT_INDEX:
765-
ReindexIndex(stmt->relation);
765+
ReindexIndex(stmt->relation, stmt->options);
766766
break;
767767
case REINDEX_OBJECT_TABLE:
768-
ReindexTable(stmt->relation);
768+
ReindexTable(stmt->relation, stmt->options);
769769
break;
770770
case REINDEX_OBJECT_SCHEMA:
771771
case REINDEX_OBJECT_SYSTEM:
@@ -781,7 +781,7 @@ standard_ProcessUtility(Node *parsetree,
781781
(stmt->kind == REINDEX_OBJECT_SCHEMA) ? "REINDEX SCHEMA" :
782782
(stmt->kind == REINDEX_OBJECT_SYSTEM) ? "REINDEX SYSTEM" :
783783
"REINDEX DATABASE");
784-
ReindexMultipleTables(stmt->name, stmt->kind);
784+
ReindexMultipleTables(stmt->name, stmt->kind, stmt->options);
785785
break;
786786
default:
787787
elog(ERROR, "unrecognized object type: %d",

src/include/catalog/index.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extern void validate_index(Oid heapId, Oid indexId, Snapshot snapshot);
115115
extern void index_set_state_flags(Oid indexId, IndexStateFlagsAction action);
116116

117117
extern void reindex_index(Oid indexId, bool skip_constraint_checks,
118-
char relpersistence);
118+
char relpersistence, int options);
119119

120120
/* Flag bits for reindex_relation(): */
121121
#define REINDEX_REL_PROCESS_TOAST 0x01
@@ -124,7 +124,7 @@ extern void reindex_index(Oid indexId, bool skip_constraint_checks,
124124
#define REINDEX_REL_FORCE_INDEXES_UNLOGGED 0x08
125125
#define REINDEX_REL_FORCE_INDEXES_PERMANENT 0x10
126126

127-
extern bool reindex_relation(Oid relid, int flags);
127+
extern bool reindex_relation(Oid relid, int flags, int options);
128128

129129
extern bool ReindexIsProcessingHeap(Oid heapOid);
130130
extern bool ReindexIsProcessingIndex(Oid indexOid);

src/include/commands/defrem.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ extern ObjectAddress DefineIndex(Oid relationId,
2929
bool check_rights,
3030
bool skip_build,
3131
bool quiet);
32-
extern Oid ReindexIndex(RangeVar *indexRelation);
33-
extern Oid ReindexTable(RangeVar *relation);
34-
extern void ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind);
32+
extern Oid ReindexIndex(RangeVar *indexRelation, int options);
33+
extern Oid ReindexTable(RangeVar *relation, int options);
34+
extern void ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
35+
int options);
3536
extern char *makeObjectName(const char *name1, const char *name2,
3637
const char *label);
3738
extern char *ChooseRelationName(const char *name1, const char *name2,

src/include/nodes/parsenodes.h

+5
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,10 @@ typedef struct ConstraintsSetStmt
27822782
* REINDEX Statement
27832783
* ----------------------
27842784
*/
2785+
2786+
/* Reindex options */
2787+
#define REINDEXOPT_VERBOSE 1 << 0 /* print progress info */
2788+
27852789
typedef enum ReindexObjectType
27862790
{
27872791
REINDEX_OBJECT_INDEX, /* index */
@@ -2797,6 +2801,7 @@ typedef struct ReindexStmt
27972801
ReindexObjectType kind; /* REINDEX_OBJECT_INDEX, REINDEX_OBJECT_TABLE, etc. */
27982802
RangeVar *relation; /* Table or index to reindex */
27992803
const char *name; /* name of database to reindex */
2804+
int options; /* Reindex options flags */
28002805
} ReindexStmt;
28012806

28022807
/* ----------------------

src/test/regress/expected/create_index.out

+8
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,14 @@ explain (costs off)
28312831
Index Cond: ((thousand = 1) AND (tenthous = 1001))
28322832
(2 rows)
28332833

2834+
--
2835+
-- REINDEX (VERBOSE)
2836+
--
2837+
CREATE TABLE reindex_verbose(id integer primary key);
2838+
\set VERBOSITY terse
2839+
REINDEX (VERBOSE) TABLE reindex_verbose;
2840+
INFO: index "reindex_verbose_pkey" was reindexed
2841+
DROP TABLE reindex_verbose;
28342842
--
28352843
-- REINDEX SCHEMA
28362844
--

src/test/regress/sql/create_index.sql

+8
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,14 @@ RESET enable_indexscan;
965965
explain (costs off)
966966
select * from tenk1 where (thousand, tenthous) in ((1,1001), (null,null));
967967

968+
--
969+
-- REINDEX (VERBOSE)
970+
--
971+
CREATE TABLE reindex_verbose(id integer primary key);
972+
\set VERBOSITY terse
973+
REINDEX (VERBOSE) TABLE reindex_verbose;
974+
DROP TABLE reindex_verbose;
975+
968976
--
969977
-- REINDEX SCHEMA
970978
--

0 commit comments

Comments
 (0)