FAQ
Hi,

What is the best way to call C-Language function from another one?

Now I see only one method - to call SPI_execute("EXECUTE funcName(...);",
false, 0).
For example:


PG_FUNCTION_INFO_V1(calculateAccount);
PG_FUNCTION_INFO_V1(calculateAccountService);

Datum calculateAccount(PG_FUNCTION_ARGS);
Datum calculateAccountService(PG_FUNCTION_ARGS);

Datum calculateAccount(PG_FUNCTION_ARGS)
{
// Arguments
int32 accountId = PG_GETARG_INT32(0);
DateADT startDate = PG_GETARG_DATEADT(1);
DateADT endDate = PG_GETARG_DATEADT(2);
char command[1000];
...

SPI_connect();

snprintf(command, sizeof(command), "execute calculateAccountService(%d,
%d);", accountId, serviceId);
SPI_execute(command, false, 0);

SPI_finish();

PG_RETURN_NULL();
}

Datum calculateAccountService(PG_FUNCTION_ARGS)
{
int32 accountId = PG_GETARG_INT32(0);
int32 serviceId = PG_GETARG_INT32(1);

...

PG_RETURN_NULL();
}


--
Best regards,
Ilya Urikh.

Search Discussions

  • Tom Lane at May 13, 2009 at 11:44 pm

    Ilya Urikh writes:
    What is the best way to call C-Language function from another one?
    Try DirectFunctionCallN --- there are many examples in the source code.

    regards, tom lane
  • Ilya Urikh at May 14, 2009 at 7:04 am
    Thanks! That's help.

    When I compile functions in the same library they work fine. But if I put
    one to another shared library function execution failed with error:
    can't load library "/usr/lib/pgsql/calculateAccount.so":
    /usr/lib/pgsql/calculateAccount.so: undefined symbol:
    calculateAccountService

    Details:
    ------------------------------------------------------
    File calculateAccount.c is compiled to calculateAccount.so:

    Datum calculateAccount(PG_FUNCTION_ARGS)
    {
    ...
    DirectFunctionCall3(calculateAccountService, accountId,
    atoi(SPI_getvalue(tuple, tupdesc, 1)), startDate);

    PG_RETURN_NULL();
    }

    -----------------------------------------------------
    File calculateAccountService.c is compiled to calculateAccountService.so:

    Datum calculateAccountService(PG_FUNCTION_ARGS)
    {
    ...

    PG_RETURN_VOID();
    }

    ------------------------------------------
    Define the functions to PostgreSQL

    CREATE OR REPLACE FUNCTION calculateAccountService(integer, smallint, date)
    RETURNS void AS
    'calculateAccountService.so', 'calculateAccountService'
    LANGUAGE C STRICT;

    CREATE OR REPLACE FUNCTION calculateAccount(integer, date, date)
    RETURNS void AS
    'calculateAccount.so', 'calculateAccount'
    LANGUAGE C STRICT;

    ----------------------------------------
    Try to execute:
    SELECT calculateAccount(2, '2008-02-23', '2009-04-13');

    And receive Error message:
    can't load library "/usr/lib/pgsql/calculateAccount.so":
    /usr/lib/pgsql/calculateAccount.so: undefined symbol:
    calculateAccountService


    Thanks in advance,

    Ilya Urikh.

    On Thu, May 14, 2009 at 10:44 AM, Tom Lane wrote:

    Ilya Urikh <ilya.urikh@gmail.com> writes:
    What is the best way to call C-Language function from another one?
    Try DirectFunctionCallN --- there are many examples in the source code.

    regards, tom lane


    --
    Best regards,
    Ilya Urikh.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppgsql-novice @
categoriespostgresql
postedMay 13, '09 at 11:39p
activeMay 14, '09 at 7:04a
posts3
users2
websitepostgresql.org
irc#postgresql

2 users in discussion

Ilya Urikh: 2 posts Tom Lane: 1 post

People

Translate

site design / logo © 2021 Grokbase