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/commands/tablecmds.c | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) (limited to 'src/backend/commands/tablecmds.c') diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 9a877f90d36..9b0a0142d3b 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -11116,7 +11116,6 @@ ATExecValidateConstraint(List **wqueue, Relation rel, char *constrName, List *children = NIL; ListCell *child; NewConstraint *newcon; - bool isnull; Datum val; char *conbin; @@ -11171,11 +11170,8 @@ ATExecValidateConstraint(List **wqueue, Relation rel, char *constrName, newcon->refindid = InvalidOid; newcon->conid = con->oid; - val = SysCacheGetAttr(CONSTROID, tuple, - Anum_pg_constraint_conbin, &isnull); - if (isnull) - elog(ERROR, "null conbin for constraint %u", con->oid); - + val = SysCacheGetAttrNotNull(CONSTROID, tuple, + Anum_pg_constraint_conbin); conbin = TextDatumGetCString(val); newcon->qual = (Node *) stringToNode(conbin); @@ -11277,7 +11273,6 @@ transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid, HeapTuple indexTuple = NULL; Form_pg_index indexStruct = NULL; Datum indclassDatum; - bool isnull; oidvector *indclass; int i; @@ -11329,9 +11324,8 @@ transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid, RelationGetRelationName(pkrel)))); /* Must get indclass the hard way */ - indclassDatum = SysCacheGetAttr(INDEXRELID, indexTuple, - Anum_pg_index_indclass, &isnull); - Assert(!isnull); + indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, indexTuple, + Anum_pg_index_indclass); indclass = (oidvector *) DatumGetPointer(indclassDatum); /* @@ -11424,13 +11418,11 @@ transformFkeyCheckAttrs(Relation pkrel, heap_attisnull(indexTuple, Anum_pg_index_indexprs, NULL)) { Datum indclassDatum; - bool isnull; oidvector *indclass; /* Must get indclass the hard way */ - indclassDatum = SysCacheGetAttr(INDEXRELID, indexTuple, - Anum_pg_index_indclass, &isnull); - Assert(!isnull); + indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, indexTuple, + Anum_pg_index_indclass); indclass = (oidvector *) DatumGetPointer(indclassDatum); /* @@ -13582,7 +13574,6 @@ TryReuseForeignKey(Oid oldId, Constraint *con) { HeapTuple tup; Datum adatum; - bool isNull; ArrayType *arr; Oid *rawarr; int numkeys; @@ -13595,10 +13586,8 @@ TryReuseForeignKey(Oid oldId, Constraint *con) if (!HeapTupleIsValid(tup)) /* should not happen */ elog(ERROR, "cache lookup failed for constraint %u", oldId); - adatum = SysCacheGetAttr(CONSTROID, tup, - Anum_pg_constraint_conpfeqop, &isNull); - if (isNull) - elog(ERROR, "null conpfeqop for constraint %u", oldId); + adatum = SysCacheGetAttrNotNull(CONSTROID, tup, + Anum_pg_constraint_conpfeqop); arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */ numkeys = ARR_DIMS(arr)[0]; /* test follows the one in ri_FetchConstraintInfo() */ -- cgit v1.2.3