mirror of
https://github.com/postgres/postgres.git
synced 2026-02-11 14:53:31 -05:00
Global error handling led to confusion and was hard to manage. With this change, errors from PostgreSQL are immediately reported to Python as exceptions. This requires setting a Python exception after reporting the caught PostgreSQL error as a warning, because PLy_elog destroys the Python exception state. Ideally, all places where PostgreSQL errors need to be reported back to Python should be wrapped in subtransactions, to make going back to Python from a longjmp safe. This will be handled in a separate patch. Jan Urbański
77 lines
2.3 KiB
Text
77 lines
2.3 KiB
Text
-- first some tests of basic functionality
|
|
CREATE LANGUAGE plpython2u;
|
|
-- really stupid function just to get the module loaded
|
|
CREATE FUNCTION stupid() RETURNS text AS 'return "zarkon"' LANGUAGE plpythonu;
|
|
select stupid();
|
|
stupid
|
|
--------
|
|
zarkon
|
|
(1 row)
|
|
|
|
-- check 2/3 versioning
|
|
CREATE FUNCTION stupidn() RETURNS text AS 'return "zarkon"' LANGUAGE plpython2u;
|
|
select stupidn();
|
|
stupidn
|
|
---------
|
|
zarkon
|
|
(1 row)
|
|
|
|
-- test multiple arguments
|
|
CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text
|
|
AS
|
|
'keys = list(u.keys())
|
|
keys.sort()
|
|
out = []
|
|
for key in keys:
|
|
out.append("%s: %s" % (key, u[key]))
|
|
words = a1 + " " + a2 + " => {" + ", ".join(out) + "}"
|
|
return words'
|
|
LANGUAGE plpythonu;
|
|
select argument_test_one(users, fname, lname) from users where lname = 'doe' order by 1;
|
|
argument_test_one
|
|
-----------------------------------------------------------------------
|
|
jane doe => {fname: jane, lname: doe, userid: 1, username: j_doe}
|
|
john doe => {fname: john, lname: doe, userid: 2, username: johnd}
|
|
willem doe => {fname: willem, lname: doe, userid: 3, username: w_doe}
|
|
(3 rows)
|
|
|
|
-- check module contents
|
|
CREATE FUNCTION module_contents() RETURNS text AS
|
|
$$
|
|
contents = list(filter(lambda x: not x.startswith("__"), dir(plpy)))
|
|
contents.sort()
|
|
return ", ".join(contents)
|
|
$$ LANGUAGE plpythonu;
|
|
select module_contents();
|
|
module_contents
|
|
-------------------------------------------------------------------------------------------
|
|
Error, Fatal, SPIError, debug, error, execute, fatal, info, log, notice, prepare, warning
|
|
(1 row)
|
|
|
|
CREATE FUNCTION elog_test() RETURNS void
|
|
AS $$
|
|
plpy.debug('debug')
|
|
plpy.log('log')
|
|
plpy.info('info')
|
|
plpy.info(37)
|
|
plpy.info()
|
|
plpy.info('info', 37, [1, 2, 3])
|
|
plpy.notice('notice')
|
|
plpy.warning('warning')
|
|
plpy.error('error')
|
|
$$ LANGUAGE plpythonu;
|
|
SELECT elog_test();
|
|
INFO: info
|
|
CONTEXT: PL/Python function "elog_test"
|
|
INFO: 37
|
|
CONTEXT: PL/Python function "elog_test"
|
|
INFO: ()
|
|
CONTEXT: PL/Python function "elog_test"
|
|
INFO: ('info', 37, [1, 2, 3])
|
|
CONTEXT: PL/Python function "elog_test"
|
|
NOTICE: notice
|
|
CONTEXT: PL/Python function "elog_test"
|
|
WARNING: warning
|
|
CONTEXT: PL/Python function "elog_test"
|
|
ERROR: PL/Python: plpy.Error: error
|
|
CONTEXT: PL/Python function "elog_test"
|