From fa488a64fa430f2220fea8a8ffbdc285ade16dd6 Mon Sep 17 00:00:00 2001 From: "Andrey M. Borodin" Date: Fri, 16 Jun 2023 14:58:49 +0300 Subject: [PATCH v1] Add corruption error codes to relcache entries In many cases relcache can detect catalog corruption. This commit marks messages of such errors with ERRCODE_DATA_CORRUPTED. --- src/backend/utils/cache/relcache.c | 34 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index 8a08463c2b..975d49b915 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -577,8 +577,10 @@ RelationBuildTupleDesc(Relation relation) attnum = attp->attnum; if (attnum <= 0 || attnum > RelationGetNumberOfAttributes(relation)) - elog(ERROR, "invalid attribute number %d for relation \"%s\"", - attp->attnum, RelationGetRelationName(relation)); + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg_internal("invalid attribute number %d for relation \"%s\"", + attp->attnum, RelationGetRelationName(relation)))); memcpy(TupleDescAttr(relation->rd_att, attnum - 1), attp, @@ -655,8 +657,10 @@ RelationBuildTupleDesc(Relation relation) table_close(pg_attribute_desc, AccessShareLock); if (need != 0) - elog(ERROR, "pg_attribute catalog is missing %d attribute(s) for relation OID %u", - need, RelationGetRelid(relation)); + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg_internal("pg_attribute catalog is missing %d attribute(s) for relation OID %u", + need, RelationGetRelid(relation)))); /* * The attcacheoff values we read from pg_attribute should all be -1 @@ -1172,8 +1176,10 @@ retry: } break; default: - elog(ERROR, "invalid relpersistence: %c", - relation->rd_rel->relpersistence); + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg_internal("invalid relpersistence: %c", + relation->rd_rel->relpersistence))); break; } @@ -1348,8 +1354,10 @@ RelationInitPhysicalAddr(Relation relation) RelationGetRelid(relation) != ClassOidIndexId, true); if (!HeapTupleIsValid(phys_tuple)) - elog(ERROR, "could not find pg_class entry for %u", - RelationGetRelid(relation)); + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg_internal("could not find pg_class entry for %u", + RelationGetRelid(relation)))); physrel = (Form_pg_class) GETSTRUCT(phys_tuple); relation->rd_rel->reltablespace = physrel->reltablespace; @@ -1366,8 +1374,10 @@ RelationInitPhysicalAddr(Relation relation) RelationMapOidToFilenumber(relation->rd_id, relation->rd_rel->relisshared); if (!RelFileNumberIsValid(relation->rd_locator.relNumber)) - elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u", - RelationGetRelationName(relation), relation->rd_id); + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg_internal("could not find relation mapping for relation \"%s\", OID %u", + RelationGetRelationName(relation), relation->rd_id))); } /* @@ -1599,7 +1609,9 @@ IndexSupportInitialize(oidvector *indclass, OpClassCacheEnt *opcentry; if (!OidIsValid(indclass->values[attIndex])) - elog(ERROR, "bogus pg_index tuple"); + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg_internal("bogus pg_index tuple"))); /* look up the info for this opclass, using a cache */ opcentry = LookupOpclassInfo(indclass->values[attIndex], -- 2.37.1 (Apple Git-137.1)