Skip to content

Commit fd6ec93

Browse files
committed
Add error codes to some corruption log messages
In some cases we have elog(ERROR) while corruption is certain and we can give a clear error code ERRCODE_DATA_CORRUPTED or ERRCODE_INDEX_CORRUPTED. Author: Andrey Borodin <x4mmm@yandex-team.ru> Discussion: https://www.postgresql.org/message-id/flat/25F6C686-6442-4A6B-BAF8-A6F7B84B16DE@yandex-team.ru
1 parent b2a3d70 commit fd6ec93

File tree

5 files changed

+70
-44
lines changed

5 files changed

+70
-44
lines changed

src/backend/access/heap/heapam_handler.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
423423

424424
/* otherwise xmin should not be dirty... */
425425
if (TransactionIdIsValid(SnapshotDirty.xmin))
426-
elog(ERROR, "t_xmin is uncommitted in tuple to be updated");
426+
ereport(ERROR,
427+
(errcode(ERRCODE_DATA_CORRUPTED),
428+
errmsg_internal("t_xmin is uncommitted in tuple to be updated")));
427429

428430
/*
429431
* If tuple is being updated by other transaction then we

src/backend/access/heap/tuptoaster.c

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,35 +1966,43 @@ toast_fetch_datum(struct varlena *attr)
19661966
* Some checks on the data we've found
19671967
*/
19681968
if (residx != nextidx)
1969-
elog(ERROR, "unexpected chunk number %d (expected %d) for toast value %u in %s",
1970-
residx, nextidx,
1971-
toast_pointer.va_valueid,
1972-
RelationGetRelationName(toastrel));
1969+
ereport(ERROR,
1970+
(errcode(ERRCODE_DATA_CORRUPTED),
1971+
errmsg_internal("unexpected chunk number %d (expected %d) for toast value %u in %s",
1972+
residx, nextidx,
1973+
toast_pointer.va_valueid,
1974+
RelationGetRelationName(toastrel))));
19731975
if (residx < numchunks - 1)
19741976
{
19751977
if (chunksize != TOAST_MAX_CHUNK_SIZE)
1976-
elog(ERROR, "unexpected chunk size %d (expected %d) in chunk %d of %d for toast value %u in %s",
1977-
chunksize, (int) TOAST_MAX_CHUNK_SIZE,
1978-
residx, numchunks,
1979-
toast_pointer.va_valueid,
1980-
RelationGetRelationName(toastrel));
1978+
ereport(ERROR,
1979+
(errcode(ERRCODE_DATA_CORRUPTED),
1980+
errmsg_internal("unexpected chunk size %d (expected %d) in chunk %d of %d for toast value %u in %s",
1981+
chunksize, (int) TOAST_MAX_CHUNK_SIZE,
1982+
residx, numchunks,
1983+
toast_pointer.va_valueid,
1984+
RelationGetRelationName(toastrel))));
19811985
}
19821986
else if (residx == numchunks - 1)
19831987
{
19841988
if ((residx * TOAST_MAX_CHUNK_SIZE + chunksize) != ressize)
1985-
elog(ERROR, "unexpected chunk size %d (expected %d) in final chunk %d for toast value %u in %s",
1986-
chunksize,
1987-
(int) (ressize - residx * TOAST_MAX_CHUNK_SIZE),
1988-
residx,
1989-
toast_pointer.va_valueid,
1990-
RelationGetRelationName(toastrel));
1989+
ereport(ERROR,
1990+
(errcode(ERRCODE_DATA_CORRUPTED),
1991+
errmsg_internal("unexpected chunk size %d (expected %d) in final chunk %d for toast value %u in %s",
1992+
chunksize,
1993+
(int) (ressize - residx * TOAST_MAX_CHUNK_SIZE),
1994+
residx,
1995+
toast_pointer.va_valueid,
1996+
RelationGetRelationName(toastrel))));
19911997
}
19921998
else
1993-
elog(ERROR, "unexpected chunk number %d (out of range %d..%d) for toast value %u in %s",
1994-
residx,
1995-
0, numchunks - 1,
1996-
toast_pointer.va_valueid,
1997-
RelationGetRelationName(toastrel));
1999+
ereport(ERROR,
2000+
(errcode(ERRCODE_DATA_CORRUPTED),
2001+
errmsg_internal("unexpected chunk number %d (out of range %d..%d) for toast value %u in %s",
2002+
residx,
2003+
0, numchunks - 1,
2004+
toast_pointer.va_valueid,
2005+
RelationGetRelationName(toastrel))));
19982006

19992007
/*
20002008
* Copy the data into proper place in our result
@@ -2010,10 +2018,12 @@ toast_fetch_datum(struct varlena *attr)
20102018
* Final checks that we successfully fetched the datum
20112019
*/
20122020
if (nextidx != numchunks)
2013-
elog(ERROR, "missing chunk number %d for toast value %u in %s",
2014-
nextidx,
2015-
toast_pointer.va_valueid,
2016-
RelationGetRelationName(toastrel));
2021+
ereport(ERROR,
2022+
(errcode(ERRCODE_DATA_CORRUPTED),
2023+
errmsg_internal("missing chunk number %d for toast value %u in %s",
2024+
nextidx,
2025+
toast_pointer.va_valueid,
2026+
RelationGetRelationName(toastrel))));
20172027

20182028
/*
20192029
* End scan and close relations

src/backend/access/nbtree/nbtinsert.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,10 +1568,12 @@ _bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, Buffer cbuf,
15681568
if (sopaque->btpo_prev != origpagenumber)
15691569
{
15701570
memset(rightpage, 0, BufferGetPageSize(rbuf));
1571-
elog(ERROR, "right sibling's left-link doesn't match: "
1572-
"block %u links to %u instead of expected %u in index \"%s\"",
1573-
oopaque->btpo_next, sopaque->btpo_prev, origpagenumber,
1574-
RelationGetRelationName(rel));
1571+
ereport(ERROR,
1572+
(errcode(ERRCODE_INDEX_CORRUPTED),
1573+
errmsg_internal("right sibling's left-link doesn't match: "
1574+
"block %u links to %u instead of expected %u in index \"%s\"",
1575+
oopaque->btpo_next, sopaque->btpo_prev, origpagenumber,
1576+
RelationGetRelationName(rel))));
15751577
}
15761578

15771579
/*
@@ -1827,8 +1829,10 @@ _bt_insert_parent(Relation rel,
18271829
_bt_relbuf(rel, rbuf);
18281830

18291831
if (pbuf == InvalidBuffer)
1830-
elog(ERROR, "failed to re-find parent key in index \"%s\" for split pages %u/%u",
1831-
RelationGetRelationName(rel), bknum, rbknum);
1832+
ereport(ERROR,
1833+
(errcode(ERRCODE_INDEX_CORRUPTED),
1834+
errmsg_internal("failed to re-find parent key in index \"%s\" for split pages %u/%u",
1835+
RelationGetRelationName(rel), bknum, rbknum)));
18321836

18331837
/* Recursively update the parent */
18341838
_bt_insertonpg(rel, NULL, pbuf, buf, stack->bts_parent,

src/backend/access/nbtree/nbtpage.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,8 +1192,10 @@ _bt_lock_branch_parent(Relation rel, BlockNumber child, BTStack stack,
11921192
stack->bts_btentry = child;
11931193
pbuf = _bt_getstackbuf(rel, stack);
11941194
if (pbuf == InvalidBuffer)
1195-
elog(ERROR, "failed to re-find parent key in index \"%s\" for deletion target page %u",
1196-
RelationGetRelationName(rel), child);
1195+
ereport(ERROR,
1196+
(errcode(ERRCODE_INDEX_CORRUPTED),
1197+
errmsg_internal("failed to re-find parent key in index \"%s\" for deletion target page %u",
1198+
RelationGetRelationName(rel), child)));
11971199
parent = stack->bts_blkno;
11981200
poffset = stack->bts_offset;
11991201

@@ -1611,9 +1613,11 @@ _bt_mark_page_halfdead(Relation rel, Buffer leafbuf, BTStack stack)
16111613
itemid = PageGetItemId(page, nextoffset);
16121614
itup = (IndexTuple) PageGetItem(page, itemid);
16131615
if (BTreeInnerTupleGetDownLink(itup) != rightsib)
1614-
elog(ERROR, "right sibling %u of block %u is not next child %u of block %u in index \"%s\"",
1615-
rightsib, target, BTreeInnerTupleGetDownLink(itup),
1616-
BufferGetBlockNumber(topparent), RelationGetRelationName(rel));
1616+
ereport(ERROR,
1617+
(errcode(ERRCODE_INDEX_CORRUPTED),
1618+
errmsg_internal("right sibling %u of block %u is not next child %u of block %u in index \"%s\"",
1619+
rightsib, target, BTreeInnerTupleGetDownLink(itup),
1620+
BufferGetBlockNumber(topparent), RelationGetRelationName(rel))));
16171621

16181622
/*
16191623
* Any insert which would have gone on the leaf block will now go to its
@@ -1878,8 +1882,10 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
18781882
target, RelationGetRelationName(rel));
18791883
}
18801884
if (opaque->btpo_prev != leftsib)
1881-
elog(ERROR, "left link changed unexpectedly in block %u of index \"%s\"",
1882-
target, RelationGetRelationName(rel));
1885+
ereport(ERROR,
1886+
(errcode(ERRCODE_INDEX_CORRUPTED),
1887+
errmsg_internal("left link changed unexpectedly in block %u of index \"%s\"",
1888+
target, RelationGetRelationName(rel))));
18831889

18841890
if (target == leafblkno)
18851891
{
@@ -1911,10 +1917,12 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
19111917
page = BufferGetPage(rbuf);
19121918
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
19131919
if (opaque->btpo_prev != target)
1914-
elog(ERROR, "right sibling's left-link doesn't match: "
1915-
"block %u links to %u instead of expected %u in index \"%s\"",
1916-
rightsib, opaque->btpo_prev, target,
1917-
RelationGetRelationName(rel));
1920+
ereport(ERROR,
1921+
(errcode(ERRCODE_INDEX_CORRUPTED),
1922+
errmsg_internal("right sibling's left-link doesn't match: "
1923+
"block %u links to %u instead of expected %u in index \"%s\"",
1924+
rightsib, opaque->btpo_prev, target,
1925+
RelationGetRelationName(rel))));
19181926
rightsib_is_rightmost = P_RIGHTMOST(opaque);
19191927
*rightsib_empty = (P_FIRSTDATAKEY(opaque) > PageGetMaxOffsetNumber(page));
19201928

src/backend/access/nbtree/nbtsearch.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,8 +2113,10 @@ _bt_get_endpoint(Relation rel, uint32 level, bool rightmost,
21132113
if (opaque->btpo.level == level)
21142114
break;
21152115
if (opaque->btpo.level < level)
2116-
elog(ERROR, "btree level %u not found in index \"%s\"",
2117-
level, RelationGetRelationName(rel));
2116+
ereport(ERROR,
2117+
(errcode(ERRCODE_INDEX_CORRUPTED),
2118+
errmsg_internal("btree level %u not found in index \"%s\"",
2119+
level, RelationGetRelationName(rel))));
21182120

21192121
/* Descend to leftmost or rightmost child page */
21202122
if (rightmost)

0 commit comments

Comments
 (0)