pglogger

functions/GET_CALLER_FUNCTION.pg_sql

Functions

GET_CALLER_FUNCTION
Returns the name of the function that called the function that called this function.
Syntax:
GET_CALLER_FUNCTION ()
Return values:
  • NAME Funciton: name
Copyright:
Thiemo Kellner, 2018 -
Webpage:
https://www.sourceforge.net/projects/pglogger
Version Info:
$Id: GET_CALLER_FUNCTION.pg_sql 3 2018-03-30 15:56:40Z thiemo $
Additional Info:
License LGPLv3
TODO:
  • Once PostgreSQL supports packages, integrate this function into one. For the time being, it should get installed within a schema on its own and no execution grants should be given on it.
  • Once HyperSQL supports a license tag, convert the corresponding info tag into one.

Source

001: \echo Start functions/GET_CALLER_FUNCTION.pg_sql
002: 
003: /** Returns the name of the function that called the function that called this function.
004:  *
005:  *  @function GET_CALLER_FUNCTION
006:  *  @return name Funciton name
007:  *  @version $Id: GET_CALLER_FUNCTION.pg_sql 3 2018-03-30 15:56:40Z thiemo $
008:  *  @todo Once PostgreSQL supports packages, integrate this function into one.
009:  *        For the time being, it should get installed within a schema on its
010:  *        own and no execution grants should be given on it.
011:  *  @todo Once HyperSQL supports a license tag, convert the corresponding
012:  *        info tag into one.
013:  *  @copyright Thiemo Kellner, 2018 -
014:  *  @info License LGPLv3
015:  *  @webpage https://www.sourceforge.net/projects/pglogger
016:  */
017: create or replace function GET_CALLER_FUNCTION()
018:   returns name
019:   language plpgsql
020:   volatile
021:   -- Include the hosting schema into search_path so that dblink
022:   -- can find the pglogger objects. There is no need to access
023:   -- objects in other schematas not covered with public.
024:   set search_path = :SCHEMA_NAME, public
025:   as
026: $body$
027:     declare
028:         C_PATTERN_SPLIT_SIGN constant text :=
029:           E'(\\r\\n|\\r|\\n)+';
030:         C_PATTERN_FOR_FUNCTION_NAME constant text :=
031:           E'^.*\\W(\\w+\\([^)]*\\)|inline_code_block).*$';
032:         C_REPLACEMENT_STRING constant text := E'\\1';
033: 
034:         V_STACK text;
035:         V_LINE_LOOKED_FOR text;
036:     begin
037:         GET DIAGNOSTICS V_STACK = pg_context;
038:         -- We split the stack string at line breaks into a set and put to a line
039:         -- its following line. From the lines referring to function calls
040:         -- the call of the logger function is selected. Its predecessor is the
041:         -- function that called the logger function.
042:         -- The alternative of using possibility regexp_split_to_array was
043:         -- discarded because no immediate method
044:         -- was known to search in arrays with regular expressions.
045:         with FUNCTION_STACK as (
046:             select LINE,
047:                    lead(LINE, 1) over (partition by 1) as NEXT_LINE
048:               from regexp_split_to_table(
049:                        V_STACK,
050:                        C_PATTERN_SPLIT_SIGN
051:                    ) as LINE
052:              where LINE ~ '^PL/pgSQL function .*$'
053:         )
054:         select NEXT_LINE into V_LINE_LOOKED_FOR
055:           from FUNCTION_STACK
056:          where LINE ~* '^.* (debug|log|info|notice|warning|exception)\(.*$';
057: 
058:         return regexp_replace(
059:                    V_LINE_LOOKED_FOR,
060:                    C_PATTERN_FOR_FUNCTION_NAME,
061:                    C_REPLACEMENT_STRING
062:                );
063:     end;
064: $body$;
065: 
066: comment on function GET_CALLER_FUNCTION() is 'Returns the name of the function that called the function that called this function.
067: $Header: svn+ssh://thiemo@svn.code.sf.net/p/pglogger/code/functions/GET_CALLER_FUNCTION.pg_sql 3 2018-03-30 15:56:40Z thiemo $';
068: 
069: commit; -- unlike Oracle not all ddl commit implicitly
070: 
071: \echo End functions/GET_CALLER_FUNCTION.pg_sql


pglogger
Generated by HyperSQL v3.9.8 at Wed Jul 4 07:48:48 2018