43.9. Utility Functions

The plpy module also provides the functions plpy.debug(``_msg_), plpy.log(``_msg_), plpy.info(``_msg_), plpy.notice(``_msg_), plpy.warning(``_msg_), plpy.error(``_msg_), and plpy.fatal(``_msg_). plpy.error and plpy.fatal actually raise a Python exception which, if uncaught, propagates out to the calling query, causing the current transaction or subtransaction to be aborted. raise plpy.Error(``_msg_) and raise plpy.Fatal(``_msg_) are equivalent to calling plpy.error and plpy.fatal, respectively. The other functions only generate messages of different priority levels. Whether messages of a particular priority are reported to the client, written to the server log, or both is controlled by the log_min_messages and client_min_messages configuration variables. See Chapter 18 for more information.

Another set of utility functions are plpy.quote_literal(``_string_), plpy.quote_nullable(``_string_), and plpy.quote_ident(``_string_). They are equivalent to the built-in quoting functions described in Section 9.4. They are useful when constructing ad-hoc queries. A PL/Python equivalent of dynamic SQL from Example 40-1 would be:

plpy.execute("UPDATE tbl SET %s = %s WHERE key = %s" % (
    plpy.quote_ident(colname),
    plpy.quote_nullable(newvalue),
    plpy.quote_literal(keyvalue)))