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/parser/parse_utilcmd.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'src/backend/parser/parse_utilcmd.c') diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index f9218f48aa7..15a1dab8c5a 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -1562,15 +1562,12 @@ generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, amrec = (Form_pg_am) GETSTRUCT(ht_am); /* Extract indcollation from the pg_index tuple */ - datum = SysCacheGetAttr(INDEXRELID, ht_idx, - Anum_pg_index_indcollation, &isnull); - Assert(!isnull); + datum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx, + Anum_pg_index_indcollation); indcollation = (oidvector *) DatumGetPointer(datum); /* Extract indclass from the pg_index tuple */ - datum = SysCacheGetAttr(INDEXRELID, ht_idx, - Anum_pg_index_indclass, &isnull); - Assert(!isnull); + datum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx, Anum_pg_index_indclass); indclass = (oidvector *) DatumGetPointer(datum); /* Begin building the IndexStmt */ @@ -1641,13 +1638,8 @@ generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, Assert(conrec->contype == CONSTRAINT_EXCLUSION); /* Extract operator OIDs from the pg_constraint tuple */ - datum = SysCacheGetAttr(CONSTROID, ht_constr, - Anum_pg_constraint_conexclop, - &isnull); - if (isnull) - elog(ERROR, "null conexclop for constraint %u", - constraintId); - + datum = SysCacheGetAttrNotNull(CONSTROID, ht_constr, + Anum_pg_constraint_conexclop); deconstruct_array_builtin(DatumGetArrayTypeP(datum), OIDOID, &elems, NULL, &nElems); for (i = 0; i < nElems; i++) @@ -1898,9 +1890,8 @@ generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid, statsrec = (Form_pg_statistic_ext) GETSTRUCT(ht_stats); /* Determine which statistics types exist */ - datum = SysCacheGetAttr(STATEXTOID, ht_stats, - Anum_pg_statistic_ext_stxkind, &isnull); - Assert(!isnull); + datum = SysCacheGetAttrNotNull(STATEXTOID, ht_stats, + Anum_pg_statistic_ext_stxkind); arr = DatumGetArrayTypeP(datum); if (ARR_NDIM(arr) != 1 || ARR_HASNULL(arr) || @@ -2228,7 +2219,6 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) Form_pg_index index_form; oidvector *indclass; Datum indclassDatum; - bool isnull; int i; /* Grammar should not allow this with explicit column list */ @@ -2327,9 +2317,9 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) parser_errposition(cxt->pstate, constraint->location))); /* Must get indclass the hard way */ - indclassDatum = SysCacheGetAttr(INDEXRELID, index_rel->rd_indextuple, - Anum_pg_index_indclass, &isnull); - Assert(!isnull); + indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, + index_rel->rd_indextuple, + Anum_pg_index_indclass); indclass = (oidvector *) DatumGetPointer(indclassDatum); for (i = 0; i < index_form->indnatts; i++) -- cgit v1.2.3