Re: Dynamic binding in plpgsql function - Mailing list pgsql-general

From Merlin Moncure
Subject Re: Dynamic binding in plpgsql function
Date
Msg-id AANLkTikNwpmZbQ9tfibUaRwCxdmHzwno6bgT15boMwD_@mail.gmail.com
Whole thread Raw
In response to Re: Dynamic binding in plpgsql function  (Pavel Stehule <pavel.stehule@gmail.com>)
Responses Re: Dynamic binding in plpgsql function  (Dmitriy Igrishin <dmitigr@gmail.com>)
List pgsql-general
On Tue, Mar 1, 2011 at 5:17 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:
> Hello
>
> 2011/3/2 Pierre Racine <Pierre.Racine@sbf.ulaval.ca>:
>> Hi,
>>
>> I would like to write a generic plpgsql function with a text parameter being a callback function name so that my
generalfunction can call this callback function. e.g.: 
>>
>> CREATE OR REPLACE FUNCTION ST_MyCallbackFunction(y int)
>>    RETURNS int AS $$
>>    DECLARE
>>    BEGIN
>>        RETURN someCalculationBasedOnY;
>>    END;
>>    $$ LANGUAGE 'plpgsql';
>>
>> CREATE OR REPLACE FUNCTION ST_MyGeneralFunction(callback text)
>>    RETURNS SETOF geomval AS $$
>>    DECLARE
>>        x integer;
>>        y integer;
>>    BEGIN
>>        y := somecalculation;
>>        x := 'callback'(y);  --This is what I need
>
> EXECUTE 'SELECT ' || callback || '($1)' USING y INTO x;
>
> there are no other way than EXECUTE
>
> attention - there is a sql injection risk

another way if you are willing to write some C is to wrap and expose
OidFunctionCall1 so it takes oid of function you want to call.

merlin

pgsql-general by date:

Previous
From: "chris r."
Date:
Subject: Re: pg_dump slow with bytea data
Next
From: Dmitriy Igrishin
Date:
Subject: Re: Dynamic binding in plpgsql function