From d435f15fff3cf3cf5d6cfcfd63e21acc0f737829 Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson Date: Sat, 25 Mar 2023 22:49:33 +0100 Subject: Add SysCacheGetAttrNotNull for guaranteed not-null attrs When extracting an attr from a cached tuple in the syscache with SysCacheGetAttr the isnull parameter must be checked in case the attr cannot be NULL. For cases when this is known beforehand, a wrapper is introduced which perform the errorhandling internally on behalf of the caller, invoking an elog in case of a NULL attr. Reviewed-by: Tom Lane Reviewed-by: Peter Eisentraut Reviewed-by: David Rowley Discussion: https://postgr.es/m/AD76405E-DB45-46B6-941F-17B1EB3A9076@yesql.se --- src/backend/utils/cache/syscache.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/backend/utils/cache/syscache.c') diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c index 94abede512b..4e4a34bde80 100644 --- a/src/backend/utils/cache/syscache.c +++ b/src/backend/utils/cache/syscache.c @@ -77,6 +77,7 @@ #include "catalog/pg_user_mapping.h" #include "lib/qunique.h" #include "utils/catcache.h" +#include "utils/lsyscache.h" #include "utils/rel.h" #include "utils/syscache.h" @@ -1099,6 +1100,32 @@ SysCacheGetAttr(int cacheId, HeapTuple tup, isNull); } +/* + * SysCacheGetAttrNotNull + * + * As above, a version of SysCacheGetAttr which knows that the attr cannot + * be NULL. + */ +Datum +SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, + AttrNumber attributeNumber) +{ + bool isnull; + Datum attr; + + attr = SysCacheGetAttr(cacheId, tup, attributeNumber, &isnull); + + if (isnull) + { + elog(ERROR, + "unexpected null value in cached tuple for catalog %s column %s", + get_rel_name(cacheinfo[cacheId].reloid), + NameStr(TupleDescAttr(SysCache[cacheId]->cc_tupdesc, attributeNumber - 1)->attname)); + } + + return attr; +} + /* * GetSysCacheHashValue * -- cgit v1.2.3