Re: Function name

From: Jack Kaufman <jack(dot)kaufman(at)sanmina(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Function name
Date: 2013-02-12 15:05:29
Message-ID: CAM=VM82UYt0eRpe1FP=xPFtyp12bEMhK1t=CzOh_4O+yfzFYZQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello, Sergey,

The code below will give you the current function name (when the parameter
to test_function_name_top is 0) and the calling function name (when the
parameter to test_function_name_top is 1). In this example, the current
function is "test_function_name" and the calling function is
"test_function_name_top". "function_name" does the work.

Like I said, it ain't pretty; but I don’t know of another way to do it.

Good luck and let me know if you have any questions,

Jack

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

--drop function test_function_name_top (function_level integer);

CREATE or replace FUNCTION test_function_name_top (function_level integer)
RETURNS text AS $$

BEGIN

return test_function_name (function_level);

END;

$$ LANGUAGE plpgsql;

--drop function test_function_name (function_level integer);

create or replace function test_function_name (function_level integer)

returns text as $$

declare

_function_name text;

begin

-- do some stuff

-- now need name of current or calling function (

/* begin - call function name function */

declare

temp_var integer;

exception_context text;

begin

temp_var := 1/0;

exception

when division_by_zero then

get stacked diagnostics exception_context = PG_EXCEPTION_CONTEXT;

_function_name := function_name (function_level, exception_context);

end;

/* end - call function name function */

-- use function name

-- do more stuff

return _function_name;

end;

$$ language plpgsql;

--drop function function_name (function_level integer, exception_context
text);

create or replace function function_name (function_level integer,
exception_context text)

returns text as $$

declare

_function_name_loc integer;

begin

if function_level <> 0 and function_level <> 1

then

return 'function level must be 0 or 1';

end if;

_function_name_loc = strpos(exception_context, 'PL/pgSQL function')
+ 18;

exception_context = substr(exception_context, _function_name_loc);

if function_level = 0 then

return 'current function = ' ||

substr(exception_context, 1, strpos(exception_context, '(') - 1);

else

_function_name_loc = strpos(exception_context, 'PL/pgSQL function') +
18;

if _function_name_loc = 18 then

return 'calling function name not found';

end if;

exception_context = substr(exception_context, _function_name_loc);

return 'calling function = ' ||

substr(exception_context, 1, strpos(exception_context, '(') - 1);

end if;

end;

$$ language plpgsql;

select * from test_function_name_top (0);

--select * from test_function_name (0);

On Tue, Feb 12, 2013 at 1:25 AM, Sergey Gigoyan <sergey(dot)gigoyan(at)gmail(dot)com>wrote:

> Thank you very much, Jack!
> I've already done this, but I need to post any changes to the name of a
> function, the variable could automatically get the function's new name.
>
> On Mon, Feb 11, 2013 at 11:58 PM, Jack Kaufman <jack(dot)kaufman(at)sanmina(dot)com>wrote:
>
>> Hello, Sergey,
>>
>> There is a way to get the current function name. However, it is not
>> exactly easy or pretty.
>>
>> So I'm curious; since it is the name of the current function you want,
>> why not just just assign the variable with the function name? (eg.,
>> fn_name text := 'this_function';)
>>
>> Jack
>>
>>
>> On Mon, Feb 11, 2013 at 8:13 AM, Sergey Gigoyan <sergey(dot)gigoyan(at)gmail(dot)com
>> > wrote:
>>
>>> How can I get current function name (for set as variable value)?
>>>
>>
>>
>>
>> --
>> Jack Kaufman
>> MDS Application Devl (US)
>> Sanmina-SCI Corporation - Owego site
>> Email: jack(dot)kaufman(at)sanmina(dot)com
>> Skype: jack_kaufman_sanm
>> 607-723-0507
>>
>> CONFIDENTIALITY
>> This e-mail message and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail message, you are hereby notified that any dissemination, distribution or copying of this e-mail message, and any attachments thereto, is strictly prohibited. If you have received this e-mail message in error, please immediately notify the sender and permanently delete the original and any copies of this email and any prints thereof.
>> ABSENT AN EXPRESS STATEMENT TO THE CONTRARY HEREINABOVE, THIS E-MAIL IS NOT INTENDED AS A SUBSTITUTE FOR A WRITING. Notwithstanding the Uniform Electronic Transactions Act or the applicability of any other law of similar substance and effect, absent an express statement to the contrary hereinabove, this e-mail message its contents, and any attachments hereto are not intended to represent an offer or acceptance to enter into a contract and are not otherwise intended to bind the sender, Sanmina Corporation (or any of its subsidiaries), or any other person or entity.
>>
>>
>>
>

--
Jack Kaufman
MDS Application Devl (US)
Sanmina-SCI Corporation - Owego site
Email: jack(dot)kaufman(at)sanmina(dot)com
Skype: jack_kaufman_sanm
607-723-0507

CONFIDENTIALITY
This e-mail message and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail message, you are hereby notified that any dissemination, distribution or copying of this e-mail message, and any attachments thereto, is strictly prohibited. If you have received this e-mail message in error, please immediately notify the sender and permanently delete the original and any copies of this email and any prints thereof.
ABSENT AN EXPRESS STATEMENT TO THE CONTRARY HEREINABOVE, THIS E-MAIL IS NOT INTENDED AS A SUBSTITUTE FOR A WRITING. Notwithstanding the Uniform Electronic Transactions Act or the applicability of any other law of similar substance and effect, absent an express statement to the contrary hereinabove, this e-mail message its contents, and any attachments hereto are not intended to represent an offer or acceptance to enter into a contract and are not otherwise intended to bind the sender, Sanmina Corporation (or any of its subsidiaries), or any other person or entity.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Keith Ouellette 2013-02-14 03:10:07 pgAgent and WAL streaming
Previous Message Kevin Grittner 2013-02-12 14:05:22 Re: