set client_min_messages to warning; create extension if not exists plpgsql_check; set client_min_messages to notice; -- -- Tests of the control functions of the profiler and of the checks of -- the arguments of the profiler functions. -- -- the profiler is enabled and disabled by a function, which reports the -- new state and returns it select plpgsql_check_profiler(true); NOTICE: profiler is active plpgsql_check_profiler ------------------------ t (1 row) select plpgsql_check_profiler(false); NOTICE: profiler is not active plpgsql_check_profiler ------------------------ f (1 row) -- when the argument is omitted or null, the state is only reported. The -- reported notices describe the configuration of the shared memory, -- which depends on the preloading of the library and on the size of the -- allocated segment, so they are suppressed here and only the returned -- value is checked. set client_min_messages to warning; select plpgsql_check_profiler(); plpgsql_check_profiler ------------------------ f (1 row) select plpgsql_check_profiler(true); plpgsql_check_profiler ------------------------ t (1 row) select plpgsql_check_profiler(); plpgsql_check_profiler ------------------------ t (1 row) set plpgsql_check.use_shared_stats_when_it_possible to off; select plpgsql_check_profiler(null); plpgsql_check_profiler ------------------------ t (1 row) set plpgsql_check.use_shared_stats_when_it_possible to default; select plpgsql_check_profiler(null); plpgsql_check_profiler ------------------------ t (1 row) select plpgsql_check_profiler(false); plpgsql_check_profiler ------------------------ f (1 row) set client_min_messages to notice; -- the fake queryid hook is installed and removed only once, the -- repeated calls do nothing select plpgsql_profiler_install_fake_queryid_hook(); plpgsql_profiler_install_fake_queryid_hook -------------------------------------------- (1 row) select plpgsql_profiler_install_fake_queryid_hook(); plpgsql_profiler_install_fake_queryid_hook -------------------------------------------- (1 row) select plpgsql_profiler_remove_fake_queryid_hook(); plpgsql_profiler_remove_fake_queryid_hook ------------------------------------------- (1 row) select plpgsql_profiler_remove_fake_queryid_hook(); plpgsql_profiler_remove_fake_queryid_hook ------------------------------------------- (1 row) -- the coverage functions are not strict, so they check the argument -- themselves select plpgsql_coverage_statements(null::text); ERROR: the first argument should not be null select plpgsql_coverage_branches(null::text); ERROR: the first argument should not be null select plpgsql_coverage_statements(null::regprocedure); ERROR: the first argument should not be null select plpgsql_coverage_branches(null::regprocedure); ERROR: the first argument should not be null -- the profile of a function which was never executed while the profiler -- was active is empty, and the function is not listed create function pc_f1() returns void as $$ begin raise exception 'pc_f1 failed'; end; $$ language plpgsql; do $$ begin perform pc_f1(); exception when others then raise notice 'catched: %', sqlerrm; end; $$; NOTICE: catched: pc_f1 failed select stmtid, exec_stmts, stmtname from plpgsql_profiler_function_statements_tb('pc_f1'); stmtid | exec_stmts | stmtname --------+------------+----------------- 1 | | statement block 2 | | RAISE (2 rows) -- a function whose whole body is written on a single line has all the -- statements on one row of the profile set plpgsql_check.profiler to on; create function pc_f2(a int) returns int as $$ declare b int; begin b := a; if b > 0 then b := -b; end if; return b; end $$ language plpgsql; select pc_f2(1); pc_f2 ------- -1 (1 row) select lineno, stmt_lineno, exec_stmts, source from plpgsql_profiler_function_tb('pc_f2'); lineno | stmt_lineno | exec_stmts | source --------+-------------+-------------+----------------------------------------------------------------------------- 1 | 1 | {1,1,1,1,1} | declare b int; begin b := a; if b > 0 then b := -b; end if; return b; end (1 row) select plpgsql_coverage_statements('pc_f2(int)'); plpgsql_coverage_statements ----------------------------- 1 (1 row) select plpgsql_coverage_branches('pc_f2(int)'); plpgsql_coverage_branches --------------------------- 0.5 (1 row) set plpgsql_check.profiler to off; select plpgsql_profiler_reset_all(); plpgsql_profiler_reset_all ---------------------------- (1 row) drop function pc_f2(int); drop function pc_f1();