1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-16 06:01:02 +03:00

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
This commit is contained in:
Peter Eisentraut
2019-08-01 11:05:08 +02:00
parent b2a3d706b8
commit fd6ec93bf8
5 changed files with 70 additions and 44 deletions

View File

@ -423,7 +423,9 @@ tuple_lock_retry:
/* otherwise xmin should not be dirty... */ /* otherwise xmin should not be dirty... */
if (TransactionIdIsValid(SnapshotDirty.xmin)) if (TransactionIdIsValid(SnapshotDirty.xmin))
elog(ERROR, "t_xmin is uncommitted in tuple to be updated"); ereport(ERROR,
(errcode(ERRCODE_DATA_CORRUPTED),
errmsg_internal("t_xmin is uncommitted in tuple to be updated")));
/* /*
* If tuple is being updated by other transaction then we * If tuple is being updated by other transaction then we

View File

@ -1966,35 +1966,43 @@ toast_fetch_datum(struct varlena *attr)
* Some checks on the data we've found * Some checks on the data we've found
*/ */
if (residx != nextidx) if (residx != nextidx)
elog(ERROR, "unexpected chunk number %d (expected %d) for toast value %u in %s", ereport(ERROR,
residx, nextidx, (errcode(ERRCODE_DATA_CORRUPTED),
toast_pointer.va_valueid, errmsg_internal("unexpected chunk number %d (expected %d) for toast value %u in %s",
RelationGetRelationName(toastrel)); residx, nextidx,
toast_pointer.va_valueid,
RelationGetRelationName(toastrel))));
if (residx < numchunks - 1) if (residx < numchunks - 1)
{ {
if (chunksize != TOAST_MAX_CHUNK_SIZE) if (chunksize != TOAST_MAX_CHUNK_SIZE)
elog(ERROR, "unexpected chunk size %d (expected %d) in chunk %d of %d for toast value %u in %s", ereport(ERROR,
chunksize, (int) TOAST_MAX_CHUNK_SIZE, (errcode(ERRCODE_DATA_CORRUPTED),
residx, numchunks, errmsg_internal("unexpected chunk size %d (expected %d) in chunk %d of %d for toast value %u in %s",
toast_pointer.va_valueid, chunksize, (int) TOAST_MAX_CHUNK_SIZE,
RelationGetRelationName(toastrel)); residx, numchunks,
toast_pointer.va_valueid,
RelationGetRelationName(toastrel))));
} }
else if (residx == numchunks - 1) else if (residx == numchunks - 1)
{ {
if ((residx * TOAST_MAX_CHUNK_SIZE + chunksize) != ressize) if ((residx * TOAST_MAX_CHUNK_SIZE + chunksize) != ressize)
elog(ERROR, "unexpected chunk size %d (expected %d) in final chunk %d for toast value %u in %s", ereport(ERROR,
chunksize, (errcode(ERRCODE_DATA_CORRUPTED),
(int) (ressize - residx * TOAST_MAX_CHUNK_SIZE), errmsg_internal("unexpected chunk size %d (expected %d) in final chunk %d for toast value %u in %s",
residx, chunksize,
toast_pointer.va_valueid, (int) (ressize - residx * TOAST_MAX_CHUNK_SIZE),
RelationGetRelationName(toastrel)); residx,
toast_pointer.va_valueid,
RelationGetRelationName(toastrel))));
} }
else else
elog(ERROR, "unexpected chunk number %d (out of range %d..%d) for toast value %u in %s", ereport(ERROR,
residx, (errcode(ERRCODE_DATA_CORRUPTED),
0, numchunks - 1, errmsg_internal("unexpected chunk number %d (out of range %d..%d) for toast value %u in %s",
toast_pointer.va_valueid, residx,
RelationGetRelationName(toastrel)); 0, numchunks - 1,
toast_pointer.va_valueid,
RelationGetRelationName(toastrel))));
/* /*
* Copy the data into proper place in our result * Copy the data into proper place in our result
@ -2010,10 +2018,12 @@ toast_fetch_datum(struct varlena *attr)
* Final checks that we successfully fetched the datum * Final checks that we successfully fetched the datum
*/ */
if (nextidx != numchunks) if (nextidx != numchunks)
elog(ERROR, "missing chunk number %d for toast value %u in %s", ereport(ERROR,
nextidx, (errcode(ERRCODE_DATA_CORRUPTED),
toast_pointer.va_valueid, errmsg_internal("missing chunk number %d for toast value %u in %s",
RelationGetRelationName(toastrel)); nextidx,
toast_pointer.va_valueid,
RelationGetRelationName(toastrel))));
/* /*
* End scan and close relations * End scan and close relations

View File

@ -1568,10 +1568,12 @@ _bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, Buffer cbuf,
if (sopaque->btpo_prev != origpagenumber) if (sopaque->btpo_prev != origpagenumber)
{ {
memset(rightpage, 0, BufferGetPageSize(rbuf)); memset(rightpage, 0, BufferGetPageSize(rbuf));
elog(ERROR, "right sibling's left-link doesn't match: " ereport(ERROR,
"block %u links to %u instead of expected %u in index \"%s\"", (errcode(ERRCODE_INDEX_CORRUPTED),
oopaque->btpo_next, sopaque->btpo_prev, origpagenumber, errmsg_internal("right sibling's left-link doesn't match: "
RelationGetRelationName(rel)); "block %u links to %u instead of expected %u in index \"%s\"",
oopaque->btpo_next, sopaque->btpo_prev, origpagenumber,
RelationGetRelationName(rel))));
} }
/* /*
@ -1827,8 +1829,10 @@ _bt_insert_parent(Relation rel,
_bt_relbuf(rel, rbuf); _bt_relbuf(rel, rbuf);
if (pbuf == InvalidBuffer) if (pbuf == InvalidBuffer)
elog(ERROR, "failed to re-find parent key in index \"%s\" for split pages %u/%u", ereport(ERROR,
RelationGetRelationName(rel), bknum, rbknum); (errcode(ERRCODE_INDEX_CORRUPTED),
errmsg_internal("failed to re-find parent key in index \"%s\" for split pages %u/%u",
RelationGetRelationName(rel), bknum, rbknum)));
/* Recursively update the parent */ /* Recursively update the parent */
_bt_insertonpg(rel, NULL, pbuf, buf, stack->bts_parent, _bt_insertonpg(rel, NULL, pbuf, buf, stack->bts_parent,

View File

@ -1192,8 +1192,10 @@ _bt_lock_branch_parent(Relation rel, BlockNumber child, BTStack stack,
stack->bts_btentry = child; stack->bts_btentry = child;
pbuf = _bt_getstackbuf(rel, stack); pbuf = _bt_getstackbuf(rel, stack);
if (pbuf == InvalidBuffer) if (pbuf == InvalidBuffer)
elog(ERROR, "failed to re-find parent key in index \"%s\" for deletion target page %u", ereport(ERROR,
RelationGetRelationName(rel), child); (errcode(ERRCODE_INDEX_CORRUPTED),
errmsg_internal("failed to re-find parent key in index \"%s\" for deletion target page %u",
RelationGetRelationName(rel), child)));
parent = stack->bts_blkno; parent = stack->bts_blkno;
poffset = stack->bts_offset; poffset = stack->bts_offset;
@ -1611,9 +1613,11 @@ _bt_mark_page_halfdead(Relation rel, Buffer leafbuf, BTStack stack)
itemid = PageGetItemId(page, nextoffset); itemid = PageGetItemId(page, nextoffset);
itup = (IndexTuple) PageGetItem(page, itemid); itup = (IndexTuple) PageGetItem(page, itemid);
if (BTreeInnerTupleGetDownLink(itup) != rightsib) if (BTreeInnerTupleGetDownLink(itup) != rightsib)
elog(ERROR, "right sibling %u of block %u is not next child %u of block %u in index \"%s\"", ereport(ERROR,
rightsib, target, BTreeInnerTupleGetDownLink(itup), (errcode(ERRCODE_INDEX_CORRUPTED),
BufferGetBlockNumber(topparent), RelationGetRelationName(rel)); errmsg_internal("right sibling %u of block %u is not next child %u of block %u in index \"%s\"",
rightsib, target, BTreeInnerTupleGetDownLink(itup),
BufferGetBlockNumber(topparent), RelationGetRelationName(rel))));
/* /*
* Any insert which would have gone on the leaf block will now go to its * 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)
target, RelationGetRelationName(rel)); target, RelationGetRelationName(rel));
} }
if (opaque->btpo_prev != leftsib) if (opaque->btpo_prev != leftsib)
elog(ERROR, "left link changed unexpectedly in block %u of index \"%s\"", ereport(ERROR,
target, RelationGetRelationName(rel)); (errcode(ERRCODE_INDEX_CORRUPTED),
errmsg_internal("left link changed unexpectedly in block %u of index \"%s\"",
target, RelationGetRelationName(rel))));
if (target == leafblkno) if (target == leafblkno)
{ {
@ -1911,10 +1917,12 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
page = BufferGetPage(rbuf); page = BufferGetPage(rbuf);
opaque = (BTPageOpaque) PageGetSpecialPointer(page); opaque = (BTPageOpaque) PageGetSpecialPointer(page);
if (opaque->btpo_prev != target) if (opaque->btpo_prev != target)
elog(ERROR, "right sibling's left-link doesn't match: " ereport(ERROR,
"block %u links to %u instead of expected %u in index \"%s\"", (errcode(ERRCODE_INDEX_CORRUPTED),
rightsib, opaque->btpo_prev, target, errmsg_internal("right sibling's left-link doesn't match: "
RelationGetRelationName(rel)); "block %u links to %u instead of expected %u in index \"%s\"",
rightsib, opaque->btpo_prev, target,
RelationGetRelationName(rel))));
rightsib_is_rightmost = P_RIGHTMOST(opaque); rightsib_is_rightmost = P_RIGHTMOST(opaque);
*rightsib_empty = (P_FIRSTDATAKEY(opaque) > PageGetMaxOffsetNumber(page)); *rightsib_empty = (P_FIRSTDATAKEY(opaque) > PageGetMaxOffsetNumber(page));

View File

@ -2113,8 +2113,10 @@ _bt_get_endpoint(Relation rel, uint32 level, bool rightmost,
if (opaque->btpo.level == level) if (opaque->btpo.level == level)
break; break;
if (opaque->btpo.level < level) if (opaque->btpo.level < level)
elog(ERROR, "btree level %u not found in index \"%s\"", ereport(ERROR,
level, RelationGetRelationName(rel)); (errcode(ERRCODE_INDEX_CORRUPTED),
errmsg_internal("btree level %u not found in index \"%s\"",
level, RelationGetRelationName(rel))));
/* Descend to leftmost or rightmost child page */ /* Descend to leftmost or rightmost child page */
if (rightmost) if (rightmost)