Re: calling a C function from pgsql function

Lists: pgsql-general
From: tamanna madaan <tamanna(dot)madaan(at)globallogic(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: calling a C function from pgsql function
Date: 2012-08-29 13:18:38
Message-ID: CAD4qJ_Lq-u=guDQgRgEUsS_RzUEwOsSrpfz4W4cbHD2tKZZ6Gg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi All

I have created a function in C language . This is to be called from a pgsql
function .
Let say the C language function name is "test1" and pgsql function name is
"test" .

test1 is called from test three times with different arguments . For
example :

test ()
begin

test1(arg1); --test1 is a C library function returning an int
test1(arg2);
test1(arg3);

end;

Now, my questions are :

1. what are the ways to get the value returned by this library function .
2. when test1 is called, will it wait for "test1" to return before
continuing with the next statement in "test" or it will continue with the
next statement parallely while "test1" is being executed.
3. Moreover, will test1 be called sequentially in order for all the three
arguments i.e arg1 first and arg3 in last. Or there can be a different
order.

Thanks..
Tamanna

--

Tamanna | Associate Consultant
GlobalLogic Inc. | Innovation by Design
ARGENTINA | CHILE | CHINA | INDIA | ISRAEL | UKRAINE | UK | USA
Office: 0120-4062000 x 2971
www.globallogic.com

http://www.globallogic.com/email_disclaimer.txt


From: Craig Ringer <ringerc(at)ringerc(dot)id(dot)au>
To: tamanna madaan <tamanna(dot)madaan(at)globallogic(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: calling a C function from pgsql function
Date: 2012-08-29 13:35:39
Message-ID: 503E1AAB.8060504@ringerc.id.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 08/29/2012 09:18 PM, tamanna madaan wrote:

> 1. what are the ways to get the value returned by this library function .

You need to wrap the C library function with a PostgreSQL C extension
that uses the fmgr.h APIs, so it's callable from SQL and can return a
result to SQL. See:

http://www.postgresql.org/docs/current/static/xfunc-c.html

> 2. when test1 is called, will it wait for "test1" to return before
> continuing with the next statement in "test" or it will continue with
> the next statement parallely while "test1" is being executed.

The call will return when the C wrapper function returns, so they'll be
executed in series not in parallel.

> 3. Moreover, will test1 be called sequentially in order for all the
> three arguments i.e arg1 first and arg3 in last. Or there can be a
> different order.

The calls will be executed in the order they appear in the PL/PgSQL
function.

--
Craig Ringer


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: tamanna madaan <tamanna(dot)madaan(at)globallogic(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: calling a C function from pgsql function
Date: 2012-08-29 13:37:22
Message-ID: CAFj8pRCo2mbeSpLfjtQequa=rCoGxtCmS4TRoPRNypvUF5VVPQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello

see http://www.postgresql.org/docs/9.1/static/xfunc-c.html

plpgsql can call any sql function, so you have to register your C
function as custom postgresql sql function

Regards

Pavel Stehule

2012/8/29 tamanna madaan <tamanna(dot)madaan(at)globallogic(dot)com>:
> Hi All
>
> I have created a function in C language . This is to be called from a pgsql
> function .
> Let say the C language function name is "test1" and pgsql function name is
> "test" .
>
> test1 is called from test three times with different arguments . For
> example :
>
> test ()
> begin
>
> test1(arg1); --test1 is a C library function returning an int
> test1(arg2);
> test1(arg3);
>
> end;
>
> Now, my questions are :
>
> 1. what are the ways to get the value returned by this library function .
> 2. when test1 is called, will it wait for "test1" to return before
> continuing with the next statement in "test" or it will continue with the
> next statement parallely while "test1" is being executed.
> 3. Moreover, will test1 be called sequentially in order for all the three
> arguments i.e arg1 first and arg3 in last. Or there can be a different
> order.
>
> Thanks..
> Tamanna
>
>
>
>
>
>
>
>
> --
>
> Tamanna | Associate Consultant
> GlobalLogic Inc. | Innovation by Design
> ARGENTINA | CHILE | CHINA | INDIA | ISRAEL | UKRAINE | UK | USA
> Office: 0120-4062000 x 2971
> www.globallogic.com
>
> http://www.globallogic.com/email_disclaimer.txt
>