Track User Defined Function Execution Counts and Runtimes

In YSQL we can track function call counts and time used by enabling the track_functions parameter.

The default is none, but you can specify pl to track only procedural-language functions or to all to also track SQL and C language functions.

Example:

				
					yugabyte=# CREATE TABLE t(c1 INT PRIMARY KEY, c2 VARCHAR);
CREATE TABLE

yugabyte=# INSERT INTO t VALUES(1, 'A'), (2, 'B'), (3, 'C');
INSERT 0 3

yugabyte=# CREATE OR REPLACE FUNCTION t_tab (c2_IN VARCHAR) RETURNS TABLE(c1_out INT, c2_out VARCHAR)
yugabyte-# LANGUAGE plpgsql
yugabyte-# AS $function$
yugabyte$# BEGIN
yugabyte$#   RETURN query SELECT c1, c2 FROM t WHERE c2 = c2_IN;
yugabyte$# END
yugabyte$# $function$
yugabyte-# ;
CREATE FUNCTION

yugabyte=# SHOW track_functions;
 track_functions
-----------------
 none
(1 row)

yugabyte=# SELECT t_tab('A');
 t_tab
-------
 (1,A)
(1 row)

yugabyte=# SET track_functions = 'pl';
SET

yugabyte=# SELECT t_tab('A');
 t_tab
-------
 (1,A)
(1 row)

yugabyte=# SELECT t_tab('A');
 t_tab
-------
 (1,A)
(1 row)

yugabyte=# SELECT t_tab('A');
 t_tab
-------
 (1,A)
(1 row)

yugabyte=# SELECT * FROM pg_stat_user_functions;
 funcid | schemaname | funcname | calls | total_time | self_time
--------+------------+----------+-------+------------+-----------
  16460 | public     | t_tab    |     3 |      6.349 |     6.349
(1 row)
				
			

To get the average times, simply divide them by the number of calls.

				
					yugabyte=# select *, total_time / calls AS total_time_avg, self_time / calls AS self_time_avg from pg_stat_user_functions;
 funcid | schemaname | funcname | calls | total_time | self_time | total_time_avg | self_time_avg
--------+------------+----------+-------+------------+-----------+----------------+---------------
  16460 | public     | t_tab    |     2 |      3.632 |     3.632 |          1.816 |         1.816
(1 row)
				
			

Have Fun!

Like my Diablo IV character's name?