mirror of
https://github.com/postgres/postgres.git
synced 2025-12-12 02:37:31 +03:00
Handle SPIErrors raised directly in PL/Python code.
If a PL/Python function raises an SPIError (or one if its subclasses) directly with python's raise statement, treat it the same as an SPIError generated internally. In particular, if the user sets the sqlstate attribute, preserve that. Oskari Saarenmaa and Jan Urbański, reviewed by Karl O. Pinc.
This commit is contained in:
@@ -298,3 +298,33 @@ plpy.execute(rollback)
|
||||
$$ LANGUAGE plpythonu;
|
||||
|
||||
SELECT manual_subxact_prepared();
|
||||
|
||||
/* raising plpy.spiexception.* from python code should preserve sqlstate
|
||||
*/
|
||||
CREATE FUNCTION plpy_raise_spiexception() RETURNS void AS $$
|
||||
raise plpy.spiexceptions.DivisionByZero()
|
||||
$$ LANGUAGE plpythonu;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
SELECT plpy_raise_spiexception();
|
||||
EXCEPTION WHEN division_by_zero THEN
|
||||
-- NOOP
|
||||
END
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
/* setting a custom sqlstate should be handled
|
||||
*/
|
||||
CREATE FUNCTION plpy_raise_spiexception_override() RETURNS void AS $$
|
||||
exc = plpy.spiexceptions.DivisionByZero()
|
||||
exc.sqlstate = 'SILLY'
|
||||
raise exc
|
||||
$$ LANGUAGE plpythonu;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
SELECT plpy_raise_spiexception_override();
|
||||
EXCEPTION WHEN SQLSTATE 'SILLY' THEN
|
||||
-- NOOP
|
||||
END
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
Reference in New Issue
Block a user