Error reporting from C functions
One thing which went unexplained in the previous sample was the error reporting part:
if (ARR_NDIM(input_array) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("use only one-dimensional arrays!")));
All error reporting and other off-channel messaging in PostgreSQL is done using the ereport(<errorlevel>, rest)
macro. The main purpose of which is to make error reporting look like a function call.
The only parameter which is processed directly by ereport()
is the first argument error level, or perhaps more exactly severity level or log level. All the other parameters are actually function calls which independently generate and store additional error information in the system to be written to logs and/or be sent to client. Being placed in the argument list of the ereport()
makes sure that these other functions are called before the actual error is reported. This is important because in the...