summaryrefslogtreecommitdiff
path: root/src/backend/utils/cache
diff options
context:
space:
mode:
authorDaniel Gustafsson2023-03-25 21:49:33 +0000
committerDaniel Gustafsson2023-03-25 21:49:33 +0000
commitd435f15fff3cf3cf5d6cfcfd63e21acc0f737829 (patch)
tree9be0836e44c3b6809bf2f6910e9fd4a45ef1f217 /src/backend/utils/cache
parente33967b13bbc6e4e1c1b5e9ecd1c45148cffcc53 (diff)
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 <[email protected]> Reviewed-by: Peter Eisentraut <[email protected]> Reviewed-by: David Rowley <[email protected]> Discussion: https://postgr.es/m/[email protected]
Diffstat (limited to 'src/backend/utils/cache')
-rw-r--r--src/backend/utils/cache/lsyscache.c21
-rw-r--r--src/backend/utils/cache/partcache.c10
-rw-r--r--src/backend/utils/cache/syscache.c27
3 files changed, 36 insertions, 22 deletions
diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c
index c07382051d6..c7607895cdd 100644
--- a/src/backend/utils/cache/lsyscache.c
+++ b/src/backend/utils/cache/lsyscache.c
@@ -3195,7 +3195,6 @@ get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple);
int i;
Datum val;
- bool isnull;
ArrayType *statarray;
Oid arrayelemtype;
int narrayelem;
@@ -3219,11 +3218,8 @@ get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
if (flags & ATTSTATSSLOT_VALUES)
{
- val = SysCacheGetAttr(STATRELATTINH, statstuple,
- Anum_pg_statistic_stavalues1 + i,
- &isnull);
- if (isnull)
- elog(ERROR, "stavalues is null");
+ val = SysCacheGetAttrNotNull(STATRELATTINH, statstuple,
+ Anum_pg_statistic_stavalues1 + i);
/*
* Detoast the array if needed, and in any case make a copy that's
@@ -3267,11 +3263,8 @@ get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
if (flags & ATTSTATSSLOT_NUMBERS)
{
- val = SysCacheGetAttr(STATRELATTINH, statstuple,
- Anum_pg_statistic_stanumbers1 + i,
- &isnull);
- if (isnull)
- elog(ERROR, "stanumbers is null");
+ val = SysCacheGetAttrNotNull(STATRELATTINH, statstuple,
+ Anum_pg_statistic_stanumbers1 + i);
/*
* Detoast the array if needed, and in any case make a copy that's
@@ -3479,7 +3472,6 @@ get_index_column_opclass(Oid index_oid, int attno)
HeapTuple tuple;
Form_pg_index rd_index;
Datum datum;
- bool isnull;
oidvector *indclass;
Oid opclass;
@@ -3501,10 +3493,7 @@ get_index_column_opclass(Oid index_oid, int attno)
return InvalidOid;
}
- datum = SysCacheGetAttr(INDEXRELID, tuple,
- Anum_pg_index_indclass, &isnull);
- Assert(!isnull);
-
+ datum = SysCacheGetAttrNotNull(INDEXRELID, tuple, Anum_pg_index_indclass);
indclass = ((oidvector *) DatumGetPointer(datum));
Assert(attno <= indclass->dim1);
diff --git a/src/backend/utils/cache/partcache.c b/src/backend/utils/cache/partcache.c
index 4f53d472333..5f3516ad0c2 100644
--- a/src/backend/utils/cache/partcache.c
+++ b/src/backend/utils/cache/partcache.c
@@ -130,15 +130,13 @@ RelationBuildPartitionKey(Relation relation)
/* But use the hard way to retrieve further variable-length attributes */
/* Operator class */
- datum = SysCacheGetAttr(PARTRELID, tuple,
- Anum_pg_partitioned_table_partclass, &isnull);
- Assert(!isnull);
+ datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
+ Anum_pg_partitioned_table_partclass);
opclass = (oidvector *) DatumGetPointer(datum);
/* Collation */
- datum = SysCacheGetAttr(PARTRELID, tuple,
- Anum_pg_partitioned_table_partcollation, &isnull);
- Assert(!isnull);
+ datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
+ Anum_pg_partitioned_table_partcollation);
collation = (oidvector *) DatumGetPointer(datum);
/* Expressions */
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"
@@ -1100,6 +1101,32 @@ SysCacheGetAttr(int cacheId, HeapTuple tup,
}
/*
+ * 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
*
* Get the hash value that would be used for a tuple in the specified cache