44.9. Utility Functions#
Theplpy
module also provides the functions
plpy.debug( |
plpy.log( |
plpy.info( |
plpy.notice( |
plpy.warning( |
plpy.error( |
plpy.fatal( |
plpy.error
andplpy.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(
andmsg
)raise plpy.Fatal(
are equivalent to callingmsg
)plpy.error(
andmsg
)plpy.fatal(
, respectively but themsg
)raise
form does not allow passing keyword arguments. 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 thelog_min_messages andclient_min_messages configuration variables. SeeChapter 19 for more information.
Themsg
argument is given as a positional argument. For backward compatibility, more than one positional argument can be given. In that case, the string representation of the tuple of positional arguments becomes the message reported to the client.
The following keyword-only arguments are accepted:
detail |
hint |
sqlstate |
schema_name |
table_name |
column_name |
datatype_name |
constraint_name |
The string representation of the objects passed as keyword-only arguments is used to enrich the messages reported to the client. For example:
CREATE FUNCTION raise_custom_exception() RETURNS void AS $$plpy.error("custom exception message", detail="some info about exception", hint="hint for users")$$ LANGUAGE plpython3u;=# SELECT raise_custom_exception();ERROR: plpy.Error: custom exception messageDETAIL: some info about exceptionHINT: hint for usersCONTEXT: Traceback (most recent call last): PL/Python function "raise_custom_exception", line 4, in <module> hint="hint for users")PL/Python function "raise_custom_exception"
Another set of utility functions areplpy.quote_literal(
,string
)plpy.quote_nullable(
, andstring
)plpy.quote_ident(
. They are equivalent to the built-in quoting functions described inSection 9.4. They are useful when constructing ad-hoc queries. A PL/Python equivalent of dynamic SQL fromExample 41.1 would be:string
)
plpy.execute("UPDATE tbl SET %s = %s WHERE key = %s" % ( plpy.quote_ident(colname), plpy.quote_nullable(newvalue), plpy.quote_literal(keyvalue)))