Book Image

PostgreSQL Server Programming - Second Edition

Book Image

PostgreSQL Server Programming - Second Edition

Overview of this book

Table of Contents (21 chapters)
PostgreSQL Server Programming Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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 whose main purpose 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, the 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 the 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...