CREATE OR REPLACE FUNCTION recompile_all_functions() RETURNS INTEGER AS $func$
DECLARE
	lr_rec RECORD;
	li_x INTEGER;
BEGIN

	FOR lr_rec IN 
	SELECT 
		p.oid as oid
	FROM 
		pg_catalog.pg_proc p
        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
        LEFT JOIN pg_language l ON l.oid = p.prolang
	WHERE 
	    	NOT p.proisagg
	    AND pg_catalog.pg_function_is_visible(p.oid)
    	AND n.nspname != 'pg_catalog'
	    AND NOT p.proname IN ('recompile_all_functions', 'recompile_function')
	    AND l.lanname = 'plpgsql'
	LOOP
		li_x := recompile_function(lr_rec.oid);
	END LOOP;


	RETURN 0;
END;
$func$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION recompile_function(a_oid oid) RETURNS INTEGER AS $func$
DECLARE
  lv_name TEXT;
  lv_fullname TEXT;
BEGIN

  SELECT INTO lv_name proname FROM pg_proc 
  WHERE 
  		oid = a_oid
  ;

  lv_fullname := a_oid::regprocedure;
  
  EXECUTE 'ALTER FUNCTION ' || lv_fullname || ' RENAME TO ugly_function_name';
  lv_fullname := a_oid::regprocedure;
  EXECUTE 'ALTER FUNCTION ' || lv_fullname || ' RENAME TO ' || lv_name;

  RETURN 0;

END;
$func$ LANGUAGE 'plpgsql';
