From 2b75c38b707a070922231de667a0bd08ee71b268 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Wed, 22 Oct 2025 09:55:17 +0900 Subject: Add error_on_null(), checking if the input is the null value This polymorphic function produces an error if the input value is detected as being the null value; otherwise it returns the input value unchanged. This function can for example become handy in SQL function bodies, to enforce that exactly one row was returned. Author: Joel Jacobson Reviewed-by: Vik Fearing Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/ece8c6d1-2ab1-45d5-ba12-8dec96fc8886@app.fastmail.com Discussion: https://postgr.es/m/de94808d-ed58-4536-9e28-e79b09a534c7@app.fastmail.com --- src/backend/utils/adt/misc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/backend/utils/adt/misc.c') diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c index 7cb7716e58b..fa1cb675027 100644 --- a/src/backend/utils/adt/misc.c +++ b/src/backend/utils/adt/misc.c @@ -186,6 +186,20 @@ pg_num_nonnulls(PG_FUNCTION_ARGS) PG_RETURN_INT32(nargs - nulls); } +/* + * error_on_null() + * Check if the input is the NULL value + */ +Datum +pg_error_on_null(PG_FUNCTION_ARGS) +{ + if (PG_ARGISNULL(0)) + ereport(ERROR, + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("null value not allowed"))); + + PG_RETURN_DATUM(PG_GETARG_DATUM(0)); +} /* * current_database() -- cgit v1.2.3