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

Commit123b0d1

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 parentf31ccb5 commit123b0d1

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
*/
@@ -538,6 +550,13 @@ pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
538550
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
539551
errmsg("cannot access temporary indexes of other sessions")));
540552

553+
/* see pgstatindex_impl */
554+
if (!rel->rd_index->indisvalid)
555+
ereport(ERROR,
556+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
557+
errmsg("index \"%s\" is not valid",
558+
RelationGetRelationName(rel))));
559+
541560
/*
542561
* Read metapage
543562
*/
@@ -615,6 +634,13 @@ pgstathashindex(PG_FUNCTION_ARGS)
615634
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
616635
errmsg("cannot access temporary indexes of other sessions")));
617636

637+
/* see pgstatindex_impl */
638+
if (!rel->rd_index->indisvalid)
639+
ereport(ERROR,
640+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
641+
errmsg("index \"%s\" is not valid",
642+
RelationGetRelationName(rel))));
643+
618644
/* Get the information we need from the metapage. */
619645
memset(&stats,0,sizeof(stats));
620646
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
@@ -260,6 +260,13 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
260260
caseRELKIND_SEQUENCE:
261261
returnpgstat_heap(rel,fcinfo);
262262
caseRELKIND_INDEX:
263+
/* see pgstatindex_impl */
264+
if (!rel->rd_index->indisvalid)
265+
ereport(ERROR,
266+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
267+
errmsg("index \"%s\" is not valid",
268+
RelationGetRelationName(rel))));
269+
263270
switch (rel->rd_rel->relam)
264271
{
265272
caseBTREE_AM_OID:

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

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

1034-
/* OK, do it */
1035-
brinsummarize(indexRel,heapRel,heapBlk, true,&numSummarized,NULL);
1034+
/* see gin_clean_pending_list() */
1035+
if (indexRel->rd_index->indisvalid)
1036+
brinsummarize(indexRel,heapRel,heapBlk, true,&numSummarized,NULL);
1037+
else
1038+
ereport(DEBUG1,
1039+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1040+
errmsg("index \"%s\" is not valid",
1041+
RelationGetRelationName(indexRel))));
10361042

10371043
/* Roll back any GUC changes executed by index functions */
10381044
AtEOXact_GUC(false,save_nestlevel);
@@ -1117,12 +1123,21 @@ brin_desummarize_range(PG_FUNCTION_ARGS)
11171123
errmsg("could not open parent table of index %s",
11181124
RelationGetRelationName(indexRel))));
11191125

1120-
/*the revmap does the hard work */
1121-
do
1126+
/*see gin_clean_pending_list() */
1127+
if (indexRel->rd_index->indisvalid)
11221128
{
1123-
done=brinRevmapDesummarizeRange(indexRel,heapBlk);
1129+
/* the revmap does the hard work */
1130+
do
1131+
{
1132+
done=brinRevmapDesummarizeRange(indexRel,heapBlk);
1133+
}
1134+
while (!done);
11241135
}
1125-
while (!done);
1136+
else
1137+
ereport(DEBUG1,
1138+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1139+
errmsg("index \"%s\" is not valid",
1140+
RelationGetRelationName(indexRel))));
11261141

11271142
relation_close(indexRel,ShareUpdateExclusiveLock);
11281143
relation_close(heapRel,ShareUpdateExclusiveLock);

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,6 @@ gin_clean_pending_list(PG_FUNCTION_ARGS)
10351035
Oidindexoid=PG_GETARG_OID(0);
10361036
RelationindexRel=index_open(indexoid,RowExclusiveLock);
10371037
IndexBulkDeleteResultstats;
1038-
GinStateginstate;
10391038

10401039
if (RecoveryInProgress())
10411040
ereport(ERROR,
@@ -1067,8 +1066,26 @@ gin_clean_pending_list(PG_FUNCTION_ARGS)
10671066
RelationGetRelationName(indexRel));
10681067

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

10731090
index_close(indexRel,RowExclusiveLock);
10741091

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp