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

Commit0a7b183

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 parent42496cb commit0a7b183

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
*/
@@ -542,6 +554,13 @@ pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
542554
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
543555
errmsg("cannot access temporary indexes of other sessions")));
544556

557+
/* see pgstatindex_impl */
558+
if (!rel->rd_index->indisvalid)
559+
ereport(ERROR,
560+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
561+
errmsg("index \"%s\" is not valid",
562+
RelationGetRelationName(rel))));
563+
545564
/*
546565
* Read metapage
547566
*/
@@ -619,6 +638,13 @@ pgstathashindex(PG_FUNCTION_ARGS)
619638
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
620639
errmsg("cannot access temporary indexes of other sessions")));
621640

641+
/* see pgstatindex_impl */
642+
if (!rel->rd_index->indisvalid)
643+
ereport(ERROR,
644+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
645+
errmsg("index \"%s\" is not valid",
646+
RelationGetRelationName(rel))));
647+
622648
/* Get the information we need from the metapage. */
623649
memset(&stats,0,sizeof(stats));
624650
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
@@ -1105,8 +1105,14 @@ brin_summarize_range(PG_FUNCTION_ARGS)
11051105
errmsg("could not open parent table of index \"%s\"",
11061106
RelationGetRelationName(indexRel))));
11071107

1108-
/* OK, do it */
1109-
brinsummarize(indexRel,heapRel,heapBlk, true,&numSummarized,NULL);
1108+
/* see gin_clean_pending_list() */
1109+
if (indexRel->rd_index->indisvalid)
1110+
brinsummarize(indexRel,heapRel,heapBlk, true,&numSummarized,NULL);
1111+
else
1112+
ereport(DEBUG1,
1113+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1114+
errmsg("index \"%s\" is not valid",
1115+
RelationGetRelationName(indexRel))));
11101116

11111117
/* Roll back any GUC changes executed by index functions */
11121118
AtEOXact_GUC(false,save_nestlevel);
@@ -1191,12 +1197,21 @@ brin_desummarize_range(PG_FUNCTION_ARGS)
11911197
errmsg("could not open parent table of index \"%s\"",
11921198
RelationGetRelationName(indexRel))));
11931199

1194-
/*the revmap does the hard work */
1195-
do
1200+
/*see gin_clean_pending_list() */
1201+
if (indexRel->rd_index->indisvalid)
11961202
{
1197-
done=brinRevmapDesummarizeRange(indexRel,heapBlk);
1203+
/* the revmap does the hard work */
1204+
do
1205+
{
1206+
done=brinRevmapDesummarizeRange(indexRel,heapBlk);
1207+
}
1208+
while (!done);
11981209
}
1199-
while (!done);
1210+
else
1211+
ereport(DEBUG1,
1212+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1213+
errmsg("index \"%s\" is not valid",
1214+
RelationGetRelationName(indexRel))));
12001215

12011216
relation_close(indexRel,ShareUpdateExclusiveLock);
12021217
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