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

Commitecd222e

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

File tree

14 files changed

+112
-46
lines changed

14 files changed

+112
-46
lines changed

‎doc/src/sgml/ref/reindex.sgml

Lines changed: 10 additions & 1 deletion
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

Lines changed: 17 additions & 4 deletions
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(OidindexId,boolskip_constraint_checks,charpersistence)
3188+
reindex_index(OidindexId,boolskip_constraint_checks,charpersistence,
3189+
intoptions)
31883190
{
31893191
RelationiRel,
31903192
heapRelation;
31913193
OidheapId;
31923194
IndexInfo*indexInfo;
31933195
volatileboolskipped_constraint= false;
3196+
PGRUsageru0;
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(Oidrelid,intflags)
3391+
reindex_relation(Oidrelid,intflags,intoptions)
33793392
{
33803393
Relationrel;
33813394
Oidtoast_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
returnresult;
35073520
}

‎src/backend/commands/cluster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
15321532
elseif (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

Lines changed: 15 additions & 10 deletions
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,intoptions)
16851685
{
16861686
OidindOid;
16871687
OidheapOid=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
returnindOid;
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,intoptions)
17791779
{
17801780
OidheapOid;
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(constchar*objectName,ReindexObjectTypeobjectKind)
1806+
ReindexMultipleTables(constchar*objectName,ReindexObjectTypeobjectKind,
1807+
intoptions)
18061808
{
18071809
OidobjectOid;
18081810
RelationrelationRelation;
@@ -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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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
returnnewnode;
38613862
}

‎src/backend/nodes/equalfuncs.c

Lines changed: 1 addition & 0 deletions
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

Lines changed: 36 additions & 21 deletions
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_typereindex_target_multitable
468+
%type<ival>reindex_option_listreindex_option_elem
469+
466470
%type<node>copy_generic_opt_argcopy_generic_opt_arg_list_item
467471
%type<defelt>copy_generic_opt_elem
468472
%type<list>copy_generic_opt_listcopy_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-
REINDEXINDEXqualified_name
7398+
REINDEXreindex_target_typequalified_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-
|REINDEXTABLEqualified_name
7407+
|REINDEXreindex_target_multitablename
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-
|REINDEXSCHEMAname
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-
|REINDEXSYSTEM_Pname
7416+
|REINDEX'('reindex_option_list')'reindex_target_typequalified_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-
|REINDEXDATABASEname
7425+
|REINDEX'('reindex_option_list')'reindex_target_multitablename
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,10 @@ standard_ProcessUtility(Node *parsetree,
762762
switch (stmt->kind)
763763
{
764764
caseREINDEX_OBJECT_INDEX:
765-
ReindexIndex(stmt->relation);
765+
ReindexIndex(stmt->relation,stmt->options);
766766
break;
767767
caseREINDEX_OBJECT_TABLE:
768-
ReindexTable(stmt->relation);
768+
ReindexTable(stmt->relation,stmt->options);
769769
break;
770770
caseREINDEX_OBJECT_SCHEMA:
771771
caseREINDEX_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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extern void validate_index(Oid heapId, Oid indexId, Snapshot snapshot);
115115
externvoidindex_set_state_flags(OidindexId,IndexStateFlagsActionaction);
116116

117117
externvoidreindex_index(OidindexId,boolskip_constraint_checks,
118-
charrelpersistence);
118+
charrelpersistence,intoptions);
119119

120120
/* Flag bits for reindex_relation(): */
121121
#defineREINDEX_REL_PROCESS_TOAST0x01
@@ -124,7 +124,7 @@ extern void reindex_index(Oid indexId, bool skip_constraint_checks,
124124
#defineREINDEX_REL_FORCE_INDEXES_UNLOGGED0x08
125125
#defineREINDEX_REL_FORCE_INDEXES_PERMANENT0x10
126126

127-
externboolreindex_relation(Oidrelid,intflags);
127+
externboolreindex_relation(Oidrelid,intflags,intoptions);
128128

129129
externboolReindexIsProcessingHeap(OidheapOid);
130130
externboolReindexIsProcessingIndex(OidindexOid);

‎src/include/commands/defrem.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ extern ObjectAddress DefineIndex(Oid relationId,
2929
boolcheck_rights,
3030
boolskip_build,
3131
boolquiet);
32-
externOidReindexIndex(RangeVar*indexRelation);
33-
externOidReindexTable(RangeVar*relation);
34-
externvoidReindexMultipleTables(constchar*objectName,ReindexObjectTypeobjectKind);
32+
externOidReindexIndex(RangeVar*indexRelation,intoptions);
33+
externOidReindexTable(RangeVar*relation,intoptions);
34+
externvoidReindexMultipleTables(constchar*objectName,ReindexObjectTypeobjectKind,
35+
intoptions);
3536
externchar*makeObjectName(constchar*name1,constchar*name2,
3637
constchar*label);
3738
externchar*ChooseRelationName(constchar*name1,constchar*name2,

‎src/include/nodes/parsenodes.h

Lines changed: 5 additions & 0 deletions
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+
#defineREINDEXOPT_VERBOSE 1 << 0/* print progress info */
2788+
27852789
typedefenumReindexObjectType
27862790
{
27872791
REINDEX_OBJECT_INDEX,/* index */
@@ -2797,6 +2801,7 @@ typedef struct ReindexStmt
27972801
ReindexObjectTypekind;/* REINDEX_OBJECT_INDEX, REINDEX_OBJECT_TABLE, etc. */
27982802
RangeVar*relation;/* Table or index to reindex */
27992803
constchar*name;/* name of database to reindex */
2804+
intoptions;/* Reindex options flags */
28002805
}ReindexStmt;
28012806

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

‎src/test/regress/expected/create_index.out

Lines changed: 8 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,14 @@ RESET enable_indexscan;
965965
explain (costs off)
966966
select*from tenk1where (thousand, tenthous)in ((1,1001), (null,null));
967967

968+
--
969+
-- REINDEX (VERBOSE)
970+
--
971+
CREATETABLEreindex_verbose(idintegerprimary key);
972+
\set VERBOSITY terse
973+
REINDEX (VERBOSE) TABLE reindex_verbose;
974+
DROPTABLE reindex_verbose;
975+
968976
--
969977
-- REINDEX SCHEMA
970978
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp