1 CREATE FUNCTION elog_test() RETURNS void
3 plpy.debug('debug', detail='some detail')
4 plpy.log('log', detail='some detail')
5 plpy.info('info', detail='some detail')
7 plpy.info('the question', detail=42);
8 plpy.info('This is message text.',
9 detail='This is detail text',
10 hint='This is hint text.',
12 schema_name='any info about schema',
13 table_name='any info about table',
14 column_name='any info about column',
15 datatype_name='any info about datatype',
16 constraint_name='any info about constraint')
17 plpy.notice('notice', detail='some detail')
18 plpy.warning('warning', detail='some detail')
19 plpy.error('stop on error', detail='some detail', hint='some hint')
20 $$ LANGUAGE plpythonu;
27 INFO: This is message text.
28 DETAIL: This is detail text
29 HINT: This is hint text.
34 ERROR: plpy.Error: stop on error
37 CONTEXT: Traceback (most recent call last):
38 PL/Python function "elog_test", line 18, in <module>
39 plpy.error('stop on error', detail='some detail', hint='some hint')
40 PL/Python function "elog_test"
41 DO $$ plpy.info('other types', detail=(10, 20)) $$ LANGUAGE plpythonu;
46 from datetime import date
47 plpy.info('other types', detail=date(2016, 2, 26))
48 $$ LANGUAGE plpythonu;
52 basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
53 plpy.info('other types', detail=basket)
54 $$ LANGUAGE plpythonu;
56 DETAIL: ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
58 DO $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu;
59 ERROR: ValueError: invalid SQLSTATE code
60 CONTEXT: Traceback (most recent call last):
61 PL/Python anonymous code block, line 1, in <module>
62 plpy.info('wrong sqlstate', sqlstate='54444A')
63 PL/Python anonymous code block
64 DO $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu;
65 ERROR: TypeError: 'blabla' is an invalid keyword argument for this function
66 CONTEXT: Traceback (most recent call last):
67 PL/Python anonymous code block, line 1, in <module>
68 plpy.info('unsupported argument', blabla='fooboo')
69 PL/Python anonymous code block
70 DO $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu;
71 ERROR: TypeError: argument 'message' given by name and position
72 CONTEXT: Traceback (most recent call last):
73 PL/Python anonymous code block, line 1, in <module>
74 plpy.info('first message', message='second message')
75 PL/Python anonymous code block
76 DO $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu;
77 ERROR: TypeError: argument 'message' given by name and position
78 CONTEXT: Traceback (most recent call last):
79 PL/Python anonymous code block, line 1, in <module>
80 plpy.info('first message', 'second message', message='third message')
81 PL/Python anonymous code block
82 -- raise exception in python, handle exception in plgsql
83 CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
84 _sqlstate text DEFAULT NULL,
85 _schema_name text DEFAULT NULL,
86 _table_name text DEFAULT NULL,
87 _column_name text DEFAULT NULL,
88 _datatype_name text DEFAULT NULL,
89 _constraint_name text DEFAULT NULL)
92 "message": _message, "detail": _detail, "hint": _hint,
93 "sqlstate": _sqlstate, "schema_name": _schema_name, "table_name": _table_name,
94 "column_name": _column_name, "datatype_name": _datatype_name,
95 "constraint_name": _constraint_name
98 plpy.error(**dict((k, v) for k, v in iter(kwargs.items()) if v))
99 $$ LANGUAGE plpythonu;
100 SELECT raise_exception('hello', 'world');
101 ERROR: plpy.Error: hello
103 CONTEXT: Traceback (most recent call last):
104 PL/Python function "raise_exception", line 9, in <module>
105 plpy.error(**dict((k, v) for k, v in iter(kwargs.items()) if v))
106 PL/Python function "raise_exception"
107 SELECT raise_exception('message text', 'detail text', _sqlstate => 'YY333');
108 ERROR: plpy.Error: message text
110 CONTEXT: Traceback (most recent call last):
111 PL/Python function "raise_exception", line 9, in <module>
112 plpy.error(**dict((k, v) for k, v in iter(kwargs.items()) if v))
113 PL/Python function "raise_exception"
114 SELECT raise_exception(_message => 'message text',
115 _detail => 'detail text',
116 _hint => 'hint text',
117 _sqlstate => 'XX555',
118 _schema_name => 'schema text',
119 _table_name => 'table text',
120 _column_name => 'column text',
121 _datatype_name => 'datatype text',
122 _constraint_name => 'constraint text');
123 ERROR: plpy.Error: message text
126 CONTEXT: Traceback (most recent call last):
127 PL/Python function "raise_exception", line 9, in <module>
128 plpy.error(**dict((k, v) for k, v in iter(kwargs.items()) if v))
129 PL/Python function "raise_exception"
130 SELECT raise_exception(_message => 'message text',
131 _hint => 'hint text',
132 _schema_name => 'schema text',
133 _column_name => 'column text',
134 _constraint_name => 'constraint text');
135 ERROR: plpy.Error: message text
137 CONTEXT: Traceback (most recent call last):
138 PL/Python function "raise_exception", line 9, in <module>
139 plpy.error(**dict((k, v) for k, v in iter(kwargs.items()) if v))
140 PL/Python function "raise_exception"
150 __datatype_name text;
151 __constraint_name text;
154 PERFORM raise_exception(_message => 'message text',
155 _detail => 'detail text',
156 _hint => 'hint text',
157 _sqlstate => 'XX555',
158 _schema_name => 'schema text',
159 _table_name => 'table text',
160 _column_name => 'column text',
161 _datatype_name => 'datatype text',
162 _constraint_name => 'constraint text');
163 EXCEPTION WHEN SQLSTATE 'XX555' THEN
164 GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT,
165 __detail = PG_EXCEPTION_DETAIL,
166 __hint = PG_EXCEPTION_HINT,
167 __sqlstate = RETURNED_SQLSTATE,
168 __schema_name = SCHEMA_NAME,
169 __table_name = TABLE_NAME,
170 __column_name = COLUMN_NAME,
171 __datatype_name = PG_DATATYPE_NAME,
172 __constraint_name = CONSTRAINT_NAME;
173 RAISE NOTICE 'handled exception'
174 USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
175 'schema_name:(%s), table_name:(%s), column_name:(%s), datatype_name:(%s), constraint_name:(%s)',
176 __message, __detail, __hint, __sqlstate, __schema_name,
177 __table_name, __column_name, __datatype_name, __constraint_name);
181 NOTICE: handled exception
182 DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint text), sqlstate: (XX555), schema_name:(schema text), table_name:(table text), column_name:(column text), datatype_name:(datatype text), constraint_name:(constraint text)
183 -- The displayed context is different between Python2 and Python3,
184 -- but that's not important for this test.
185 \set SHOW_CONTEXT never
188 plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
189 except Exception as e:
192 $$ LANGUAGE plpythonu;
193 INFO: (119577128, None, 'some hint', None, 0, None, 'users_tab', None, 'user_type', None)
194 ERROR: plpy.SPIError: plpy.Error: my message
198 plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
199 except Exception as e:
200 plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
202 $$ LANGUAGE plpythonu;
203 INFO: sqlstate: XX987, hint: some hint, table_name: users_tab, datatype_name: user_type
204 ERROR: plpy.Error: my message