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

Commit13503eb

Browse files
committed
Diagnose !indisvalid in more SQL functions.
pgstatindex failed with ERRCODE_DATA_CORRUPTED, of the "can't-happen"class XX. The other functions succeeded on an empty index; they mighthave malfunctioned if the failed index build left torn I/O or othercomplex state. Report an ERROR in statistics functions pgstatindex,pgstatginindex, pgstathashindex, and pgstattuple. Report DEBUG1 andskip all index I/O in maintenance functions brin_desummarize_range,brin_summarize_new_values, brin_summarize_range, andgin_clean_pending_list. Back-patch to v11 (all supported versions).Discussion:https://postgr.es/m/20231001195309.a3@google.com
1 parent6ec9e99 commit13503eb

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

‎contrib/pgstattuple/pgstatindex.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
237237
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
238238
errmsg("cannot access temporary tables of other sessions")));
239239

240+
/*
241+
* A !indisready index could lead to ERRCODE_DATA_CORRUPTED later, so exit
242+
* early. We're capable of assessing an indisready&&!indisvalid index,
243+
* but the results could be confusing. For example, the index's size
244+
* could be too low for a valid index of the table.
245+
*/
246+
if (!rel->rd_index->indisvalid)
247+
ereport(ERROR,
248+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
249+
errmsg("index \"%s\" is not valid",
250+
RelationGetRelationName(rel))));
251+
240252
/*
241253
* Read metapage
242254
*/
@@ -523,6 +535,13 @@ pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
523535
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
524536
errmsg("cannot access temporary indexes of other sessions")));
525537

538+
/* see pgstatindex_impl */
539+
if (!rel->rd_index->indisvalid)
540+
ereport(ERROR,
541+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
542+
errmsg("index \"%s\" is not valid",
543+
RelationGetRelationName(rel))));
544+
526545
/*
527546
* Read metapage
528547
*/
@@ -600,6 +619,13 @@ pgstathashindex(PG_FUNCTION_ARGS)
600619
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
601620
errmsg("cannot access temporary indexes of other sessions")));
602621

622+
/* see pgstatindex_impl */
623+
if (!rel->rd_index->indisvalid)
624+
ereport(ERROR,
625+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
626+
errmsg("index \"%s\" is not valid",
627+
RelationGetRelationName(rel))));
628+
603629
/* Get the information we need from the metapage. */
604630
memset(&stats,0,sizeof(stats));
605631
metabuf=_hash_getbuf(rel,HASH_METAPAGE,HASH_READ,LH_META_PAGE);

‎contrib/pgstattuple/pgstattuple.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
259259
}
260260
elseif (rel->rd_rel->relkind==RELKIND_INDEX)
261261
{
262+
/* see pgstatindex_impl */
263+
if (!rel->rd_index->indisvalid)
264+
ereport(ERROR,
265+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
266+
errmsg("index \"%s\" is not valid",
267+
RelationGetRelationName(rel))));
268+
262269
switch (rel->rd_rel->relam)
263270
{
264271
caseBTREE_AM_OID:

‎src/backend/access/brin/brin.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,8 +1100,14 @@ brin_summarize_range(PG_FUNCTION_ARGS)
11001100
errmsg("could not open parent table of index \"%s\"",
11011101
RelationGetRelationName(indexRel))));
11021102

1103-
/* OK, do it */
1104-
brinsummarize(indexRel,heapRel,heapBlk, true,&numSummarized,NULL);
1103+
/* see gin_clean_pending_list() */
1104+
if (indexRel->rd_index->indisvalid)
1105+
brinsummarize(indexRel,heapRel,heapBlk, true,&numSummarized,NULL);
1106+
else
1107+
ereport(DEBUG1,
1108+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1109+
errmsg("index \"%s\" is not valid",
1110+
RelationGetRelationName(indexRel))));
11051111

11061112
/* Roll back any GUC changes executed by index functions */
11071113
AtEOXact_GUC(false,save_nestlevel);
@@ -1183,12 +1189,21 @@ brin_desummarize_range(PG_FUNCTION_ARGS)
11831189
errmsg("could not open parent table of index \"%s\"",
11841190
RelationGetRelationName(indexRel))));
11851191

1186-
/*the revmap does the hard work */
1187-
do
1192+
/*see gin_clean_pending_list() */
1193+
if (indexRel->rd_index->indisvalid)
11881194
{
1189-
done=brinRevmapDesummarizeRange(indexRel,heapBlk);
1195+
/* the revmap does the hard work */
1196+
do
1197+
{
1198+
done=brinRevmapDesummarizeRange(indexRel,heapBlk);
1199+
}
1200+
while (!done);
11901201
}
1191-
while (!done);
1202+
else
1203+
ereport(DEBUG1,
1204+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1205+
errmsg("index \"%s\" is not valid",
1206+
RelationGetRelationName(indexRel))));
11921207

11931208
relation_close(indexRel,ShareUpdateExclusiveLock);
11941209
relation_close(heapRel,ShareUpdateExclusiveLock);

‎src/backend/access/gin/ginfast.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,6 @@ gin_clean_pending_list(PG_FUNCTION_ARGS)
10331033
Oidindexoid=PG_GETARG_OID(0);
10341034
RelationindexRel=index_open(indexoid,RowExclusiveLock);
10351035
IndexBulkDeleteResultstats;
1036-
GinStateginstate;
10371036

10381037
if (RecoveryInProgress())
10391038
ereport(ERROR,
@@ -1065,8 +1064,26 @@ gin_clean_pending_list(PG_FUNCTION_ARGS)
10651064
RelationGetRelationName(indexRel));
10661065

10671066
memset(&stats,0,sizeof(stats));
1068-
initGinState(&ginstate,indexRel);
1069-
ginInsertCleanup(&ginstate, true, true, true,&stats);
1067+
1068+
/*
1069+
* Can't assume anything about the content of an !indisready index. Make
1070+
* those a no-op, not an error, so users can just run this function on all
1071+
* indexes of the access method. Since an indisready&&!indisvalid index
1072+
* is merely awaiting missed aminsert calls, we're capable of processing
1073+
* it. Decline to do so, out of an abundance of caution.
1074+
*/
1075+
if (indexRel->rd_index->indisvalid)
1076+
{
1077+
GinStateginstate;
1078+
1079+
initGinState(&ginstate,indexRel);
1080+
ginInsertCleanup(&ginstate, true, true, true,&stats);
1081+
}
1082+
else
1083+
ereport(DEBUG1,
1084+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1085+
errmsg("index \"%s\" is not valid",
1086+
RelationGetRelationName(indexRel))));
10701087

10711088
index_close(indexRel,RowExclusiveLock);
10721089

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp