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

Commit61d7c7b

Browse files
committed
Prevent reindex of invalid indexes on TOAST tables
Such indexes can only be duplicated leftovers of a previously failedREINDEX CONCURRENTLY command, and a valid equivalent is guaranteed toexist. As toast indexes can only be dropped if invalid, reindexingthese would lead to useless duplicated indexes that can't be droppedanymore, except if the parent relation is dropped.Thanks to Justin Pryzby for reminding that this problem was reportedlong ago during the review of the original patch of REINDEXCONCURRENTLY, but the issue was never addressed.Reported-by: Sergei Kornilov, Justin PryzbyAuthor: Julien RouhaudReviewed-by: Michael PaquierDiscussion:https://postgr.es/m/36712441546604286%40sas1-890ba5c2334a.qloud-c.yandex.netDiscussion:https://postgr.es/m/20200216190835.GA21832@telsasoft.comBackpatch-through: 12
1 parent71e0d0a commit61d7c7b

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

‎src/backend/catalog/index.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,6 +3474,17 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
34743474
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
34753475
errmsg("cannot reindex temporary tables of other sessions")));
34763476

3477+
/*
3478+
* Don't allow reindex of an invalid index on TOAST table. This is a
3479+
* leftover from a failed REINDEX CONCURRENTLY, and if rebuilt it would
3480+
* not be possible to drop it anymore.
3481+
*/
3482+
if (IsToastNamespace(RelationGetNamespace(iRel))&&
3483+
!get_index_isvalid(indexId))
3484+
ereport(ERROR,
3485+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3486+
errmsg("cannot reindex invalid index on TOAST table")));
3487+
34773488
/*
34783489
* Also check for active uses of the index in the current transaction; we
34793490
* don't want to reindex underneath an open indexscan.
@@ -3723,6 +3734,23 @@ reindex_relation(Oid relid, int flags, int options)
37233734
foreach(indexId,indexIds)
37243735
{
37253736
OidindexOid=lfirst_oid(indexId);
3737+
OidindexNamespaceId=get_rel_namespace(indexOid);
3738+
3739+
/*
3740+
* Skip any invalid indexes on a TOAST table. These can only be
3741+
* duplicate leftovers from a failed REINDEX CONCURRENTLY, and if
3742+
* rebuilt it would not be possible to drop them anymore.
3743+
*/
3744+
if (IsToastNamespace(indexNamespaceId)&&
3745+
!get_index_isvalid(indexOid))
3746+
{
3747+
ereport(WARNING,
3748+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3749+
errmsg("cannot reindex invalid index \"%s.%s\" on TOAST table, skipping",
3750+
get_namespace_name(indexNamespaceId),
3751+
get_rel_name(indexOid))));
3752+
continue;
3753+
}
37263754

37273755
reindex_index(indexOid, !(flags&REINDEX_REL_CHECK_CONSTRAINTS),
37283756
persistence,options);

‎src/backend/commands/indexcmds.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,6 +2868,16 @@ ReindexRelationConcurrently(Oid relationOid, int options)
28682868
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
28692869
errmsg("cannot reindex system catalogs concurrently")));
28702870

2871+
/*
2872+
* Don't allow reindex for an invalid index on TOAST table, as
2873+
* if rebuild it would not be possible to drop it.
2874+
*/
2875+
if (IsToastNamespace(get_rel_namespace(relationOid))&&
2876+
!get_index_isvalid(relationOid))
2877+
ereport(ERROR,
2878+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2879+
errmsg("cannot reindex invalid index on TOAST table concurrently")));
2880+
28712881
/* Save the list of relation OIDs in private context */
28722882
oldcontext=MemoryContextSwitchTo(private_context);
28732883

‎src/backend/utils/cache/lsyscache.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,3 +3227,26 @@ get_index_column_opclass(Oid index_oid, int attno)
32273227

32283228
returnopclass;
32293229
}
3230+
3231+
/*
3232+
* get_index_isvalid
3233+
*
3234+
*Given the index OID, return pg_index.indisvalid.
3235+
*/
3236+
bool
3237+
get_index_isvalid(Oidindex_oid)
3238+
{
3239+
boolisvalid;
3240+
HeapTupletuple;
3241+
Form_pg_indexrd_index;
3242+
3243+
tuple=SearchSysCache1(INDEXRELID,ObjectIdGetDatum(index_oid));
3244+
if (!HeapTupleIsValid(tuple))
3245+
elog(ERROR,"cache lookup failed for index %u",index_oid);
3246+
3247+
rd_index= (Form_pg_index)GETSTRUCT(tuple);
3248+
isvalid=rd_index->indisvalid;
3249+
ReleaseSysCache(tuple);
3250+
3251+
returnisvalid;
3252+
}

‎src/include/utils/lsyscache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ extern char *get_namespace_name_or_temp(Oid nspid);
181181
externOidget_range_subtype(OidrangeOid);
182182
externOidget_range_collation(OidrangeOid);
183183
externOidget_index_column_opclass(Oidindex_oid,intattno);
184+
externboolget_index_isvalid(Oidindex_oid);
184185

185186
#definetype_is_array(typid) (get_element_type(typid) != InvalidOid)
186187
/* type_is_array_domain accepts both plain arrays and domains over arrays */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp