|
| 1 | +LOAD 'plpgsql'; |
| 2 | +CREATE EXTENSION IF NOT EXISTS plpgsql_check; |
| 3 | +NOTICE: extension "plpgsql_check" already exists, skipping |
| 4 | +set client_min_messages to notice; |
| 5 | +create or replace function fxtest() |
| 6 | +returns void as $$ |
| 7 | +declare |
| 8 | + v_sqlstate text; |
| 9 | + v_message text; |
| 10 | + v_context text; |
| 11 | +begin |
| 12 | + get stacked diagnostics |
| 13 | + v_sqlstate = returned_sqlstate, |
| 14 | + v_message = message_text, |
| 15 | + v_context = pg_exception_context; |
| 16 | +end; |
| 17 | +$$ language plpgsql; |
| 18 | +select * from plpgsql_check_function('fxtest'); |
| 19 | + plpgsql_check_function |
| 20 | +----------------------------------------------------------------------------------------------------------- |
| 21 | + error:0Z002:7:GET STACKED DIAGNOSTICS:GET STACKED DIAGNOSTICS cannot be used outside an exception handler |
| 22 | +(1 row) |
| 23 | + |
| 24 | +drop function fxtest(); |
| 25 | +create or replace procedure prtest() |
| 26 | +as $$ |
| 27 | +begin |
| 28 | + commit; |
| 29 | +end; |
| 30 | +$$ language plpgsql; |
| 31 | +select * from plpgsql_check_function('prtest'); --ok |
| 32 | + plpgsql_check_function |
| 33 | +------------------------ |
| 34 | +(0 rows) |
| 35 | + |
| 36 | +create or replace procedure prtest() |
| 37 | +as $$ |
| 38 | +begin |
| 39 | + begin |
| 40 | + begin |
| 41 | + commit; |
| 42 | + end; |
| 43 | + end; |
| 44 | +exception when others then |
| 45 | + raise; |
| 46 | +end; |
| 47 | +$$ language plpgsql; |
| 48 | +select * from plpgsql_check_function('prtest'); --error |
| 49 | + plpgsql_check_function |
| 50 | +--------------------------------------------------------------------- |
| 51 | + error:2D000:5:COMMIT:cannot commit while a subtransaction is active |
| 52 | +(1 row) |
| 53 | + |
| 54 | +create or replace procedure prtest() |
| 55 | +as $$ |
| 56 | +begin |
| 57 | + raise exception 'error'; |
| 58 | +exception when others then |
| 59 | + begin |
| 60 | + begin |
| 61 | + commit; |
| 62 | + end; |
| 63 | + end; |
| 64 | +end; |
| 65 | +$$ language plpgsql; |
| 66 | +select * from plpgsql_check_function('prtest'); --ok |
| 67 | + plpgsql_check_function |
| 68 | +------------------------ |
| 69 | +(0 rows) |
| 70 | + |
| 71 | +drop procedure prtest(); |
| 72 | +create function return_constant_refcursor() returns refcursor as $$ |
| 73 | +declare |
| 74 | + rc constant refcursor; |
| 75 | +begin |
| 76 | + open rc for select a from rc_test; |
| 77 | + return rc; |
| 78 | +end |
| 79 | +$$ language plpgsql; |
| 80 | +create table rc_test(a int); |
| 81 | +select * from plpgsql_check_function('return_constant_refcursor'); |
| 82 | + plpgsql_check_function |
| 83 | +------------------------------------------------------- |
| 84 | + error:22005:5:OPEN:variable "rc" is declared CONSTANT |
| 85 | +(1 row) |
| 86 | + |
| 87 | +drop table rc_test; |
| 88 | +drop function return_constant_refcursor(); |
| 89 | +create procedure p1(a int, out b int) |
| 90 | +as $$ |
| 91 | +begin |
| 92 | + b := a + 10; |
| 93 | +end; |
| 94 | +$$ language plpgsql; |
| 95 | +create function f1() |
| 96 | +returns void as $$ |
| 97 | +declare b constant int; |
| 98 | +begin |
| 99 | + call p1(10, b); |
| 100 | +end; |
| 101 | +$$ language plpgsql; |
| 102 | +select * from plpgsql_check_function('f1'); |
| 103 | + plpgsql_check_function |
| 104 | +------------------------------------------------------ |
| 105 | + error:22005:4:CALL:variable "b" is declared CONSTANT |
| 106 | +(1 row) |
| 107 | + |
| 108 | +drop function f1(); |
| 109 | +drop procedure p1(int, int); |
| 110 | +create or replace function f1() |
| 111 | +returns int as $$ |
| 112 | +declare c constant int default 100; |
| 113 | +begin |
| 114 | + return c; |
| 115 | +end; |
| 116 | +$$ language plpgsql; |
| 117 | +-- should be ok |
| 118 | +select * from plpgsql_check_function('f1'); |
| 119 | + plpgsql_check_function |
| 120 | +------------------------ |
| 121 | +(0 rows) |
| 122 | + |
| 123 | +drop function f1(); |
| 124 | +-- do not raise false warning |
| 125 | +create or replace function test_function() |
| 126 | +returns text as $$ |
| 127 | +declare s text; |
| 128 | +begin |
| 129 | + get diagnostics s = PG_CONTEXT; |
| 130 | + return s; |
| 131 | +end; |
| 132 | +$$ language plpgsql; |
| 133 | +create or replace procedure test_procedure() |
| 134 | +as $$ |
| 135 | +begin |
| 136 | + null; |
| 137 | +end; |
| 138 | +$$ language plpgsql; |
| 139 | +-- should be without any warnings |
| 140 | +select * from plpgsql_check_function('test_function', performance_warnings=>true); |
| 141 | + plpgsql_check_function |
| 142 | +------------------------ |
| 143 | +(0 rows) |
| 144 | + |
| 145 | +select * from plpgsql_check_function('test_procedure', performance_warnings=>true); |
| 146 | + plpgsql_check_function |
| 147 | +------------------------ |
| 148 | +(0 rows) |
| 149 | + |
| 150 | +drop function test_function(); |
| 151 | +drop procedure test_procedure(); |
| 152 | +-- detect dependecy in CALL statement |
| 153 | +create or replace function fx1_dep(int) |
| 154 | +returns int as $$ |
| 155 | +begin |
| 156 | + return $1; |
| 157 | +end; |
| 158 | +$$ language plpgsql; |
| 159 | +create or replace procedure px1_dep(int) |
| 160 | +as $$ |
| 161 | +begin |
| 162 | +end; |
| 163 | +$$ language plpgsql; |
| 164 | +create or replace function test_function() |
| 165 | +returns void as $$ |
| 166 | +begin |
| 167 | + call px1_dep(fx1_dep(10)); |
| 168 | +end; |
| 169 | +$$ language plpgsql; |
| 170 | +select type, schema, name, params from plpgsql_show_dependency_tb('test_function'); |
| 171 | + type | schema | name | params |
| 172 | +-----------+--------+---------+----------- |
| 173 | + FUNCTION | public | fx1_dep | (integer) |
| 174 | + PROCEDURE | public | px1_dep | (integer) |
| 175 | +(2 rows) |
| 176 | + |
| 177 | +drop function test_function(); |
| 178 | +drop procedure px1_dep(int); |
| 179 | +drop function fx1_dep(int); |
0 commit comments