Обсуждение: PostgreSQL 8.4 crash on user defined C language function

Поиск
Список
Период
Сортировка

PostgreSQL 8.4 crash on user defined C language function

От
Vincas Dargis
Дата:
SORRY FOR DUPLICATE EMAIL, I guess I'm really in pretty bad shape...

Hello,

I am experimenting with user defined functions in C, though I have
problem with stability.

It always crash on palloc() call, SPI_tuptable->tupdesc dereference
but not on SPI_* calls, for example.

Here is one modified example:

PGDLLIMPORT Datum test(PG_FUNCTION_ARGS)
{
       char *command;
       int ret;
       int proc;

       command = "SELECT 1";

       elog(INFO,"1");

       SPI_connect();

       elog(INFO,"2");

       ret = SPI_exec(command, 1);

       elog(INFO,"3");

       proc = SPI_processed;

       elog(INFO,"4");
       /*
        * If some rows were fetched, print them via elog(INFO).
        */
       if (ret > 0 && SPI_tuptable != NULL)
       {
           TupleDesc tupdesc = SPI_tuptable->tupdesc;
           SPITupleTable *tuptable = SPI_tuptable;
           char buf[8192];
           int i, j;

           elog(INFO,"5");
           for (j = 0; j < proc; j++)
           {
               HeapTuple tuple = tuptable->vals[j];

               elog(INFO,"6");
               for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
               {
                       snprintf(buf + strlen (buf), sizeof(buf) -
strlen(buf), " %s%s",
                                SPI_getvalue(tuple, tupdesc, i),
                                (i == tupdesc->natts) ? " " : " |");

                       elog(INFO,"7");
               }
               elog(INFO, "EXECQ: %s", buf);
           }
       }

       SPI_finish();
       pfree(command);

}

"elog(INFO,"5");" line is not executed, I have to restart crashed
server after error:

LOG:  server process (PID 1628) was terminated by exception 0xC0000005
HINT:  See C include file "ntstatus.h" for a description of the
hexadecimal value.
LOG:  terminating any other active server processes

0xC0000005 is access violation, but I can't follow why.

I am using Visual Studio 2005 SP1 compiler. Since mine PostgreSQL 8.4
server uses msvcr71.dll, not 80, maybe it's compiler incompatibility
of some kind?

Re: PostgreSQL 8.4 crash on user defined C language function

От
Merlin Moncure
Дата:
On Wed, Apr 4, 2012 at 10:46 AM, Vincas Dargis <vindrg@gmail.com> wrote:
> SORRY FOR DUPLICATE EMAIL, I guess I'm really in pretty bad shape...
>
> Hello,
>
> I am experimenting with user defined functions in C, though I have
> problem with stability.
>
> It always crash on palloc() call, SPI_tuptable->tupdesc dereference
> but not on SPI_* calls, for example.
>
> Here is one modified example:
>
> PGDLLIMPORT Datum test(PG_FUNCTION_ARGS)
> {
>       char *command;
>       int ret;
>       int proc;
>
>       command = "SELECT 1";
>
>       elog(INFO,"1");
>
>       SPI_connect();
>
>       elog(INFO,"2");
>
>       ret = SPI_exec(command, 1);
>
>       elog(INFO,"3");
>
>       proc = SPI_processed;
>
>       elog(INFO,"4");
>       /*
>        * If some rows were fetched, print them via elog(INFO).
>        */
>       if (ret > 0 && SPI_tuptable != NULL)
>       {
>           TupleDesc tupdesc = SPI_tuptable->tupdesc;
>           SPITupleTable *tuptable = SPI_tuptable;
>           char buf[8192];
>           int i, j;
>
>           elog(INFO,"5");
>           for (j = 0; j < proc; j++)
>           {
>               HeapTuple tuple = tuptable->vals[j];
>
>               elog(INFO,"6");
>               for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
>               {
>                       snprintf(buf + strlen (buf), sizeof(buf) -
> strlen(buf), " %s%s",
>                                SPI_getvalue(tuple, tupdesc, i),
>                                (i == tupdesc->natts) ? " " : " |");
>
>                       elog(INFO,"7");
>               }
>               elog(INFO, "EXECQ: %s", buf);
>           }
>       }
>
>       SPI_finish();
>       pfree(command);
>
> }
>
> "elog(INFO,"5");" line is not executed, I have to restart crashed
> server after error:
>
> LOG:  server process (PID 1628) was terminated by exception 0xC0000005
> HINT:  See C include file "ntstatus.h" for a description of the
> hexadecimal value.
> LOG:  terminating any other active server processes
>
> 0xC0000005 is access violation, but I can't follow why.
>
> I am using Visual Studio 2005 SP1 compiler. Since mine PostgreSQL 8.4
> server uses msvcr71.dll, not 80, maybe it's compiler incompatibility
> of some kind?

did you remember to set up the V1 calling convention macro?

merlin

Re: PostgreSQL 8.4 crash on user defined C language function

От
Vincas Dargis
Дата:

2012.04.04 20:37, Merlin Moncure rašė:
> did you remember to set up the V1 calling convention macro?
>
> merlin
>

Yes, I did remember. This kind of inconsistency bugs me, why SPI_*
works, pmalloc not?

Vincas.

Re: PostgreSQL 8.4 crash on user defined C language function

От
"iihero"
Дата:
Hi,

In your code, only the last line "pfree(command);"  is wrong.  No need to put it there. Since you use the constant string for the variable command.

In my test,

here is the result:

iihero=# select test();
INFO:  1
INFO:  2
INFO:  3
INFO:  4
INFO:  5
INFO:  6
INFO:  7
INFO:  EXECQ:  1
 test
------

(1 row)


------------------
~~~~~~~~~~~~~~~~~~~~~~~~~
iihero
http://www.sql9.com
http://www.sql6.com
~~~~~~~~~~~~~~~~~~~~~~~~~

 
 
 
------------------ Original ------------------
Date:  Thu, Apr 5, 2012 01:37 AM
To:  "Vincas Dargis"<vindrg@gmail.com>;
Cc:  "pgsql-general"<pgsql-general@postgresql.org>;
Subject:  Re: [GENERAL] PostgreSQL 8.4 crash on user defined C language function
 
On Wed, Apr 4, 2012 at 10:46 AM, Vincas Dargis <vindrg@gmail.com> wrote:
> SORRY FOR DUPLICATE EMAIL, I guess I'm really in pretty bad shape...
>
> Hello,
>
> I am experimenting with user defined functions in C, though I have
> problem with stability.
>
> It always crash on palloc() call, SPI_tuptable->tupdesc dereference
> but not on SPI_* calls, for example.
>
> Here is one modified example:
>
> PGDLLIMPORT Datum test(PG_FUNCTION_ARGS)
> {
>       char *command;
>       int ret;
>       int proc;
>
>       command = "SELECT 1";
>
>       elog(INFO,"1");
>
>       SPI_connect();
>
>       elog(INFO,"2");
>
>       ret = SPI_exec(command, 1);
>
>       elog(INFO,"3");
>
>       proc = SPI_processed;
>
>       elog(INFO,"4");
>       /*
>        * If some rows were fetched, print them via elog(INFO).
>        */
>       if (ret > 0 && SPI_tuptable != NULL)
>       {
>           TupleDesc tupdesc = SPI_tuptable->tupdesc;
>           SPITupleTable *tuptable = SPI_tuptable;
>           char buf[8192];
>           int i, j;
>
>           elog(INFO,"5");
>           for (j = 0; j < proc; j++)
>           {
>               HeapTuple tuple = tuptable->vals[j];
>
>               elog(INFO,"6");
>               for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
>               {
>                       snprintf(buf + strlen (buf), sizeof(buf) -
> strlen(buf), " %s%s",
>                                SPI_getvalue(tuple, tupdesc, i),
>                                (i == tupdesc->natts) ? " " : " |");
>
>                       elog(INFO,"7");
>               }
>               elog(INFO, "EXECQ: %s", buf);
>           }
>       }
>
>       SPI_finish();
>       pfree(command);
>
> }
>
> "elog(INFO,"5");" line is not executed, I have to restart crashed
> server after error:
>
> LOG:  server process (PID 1628) was terminated by exception 0xC0000005
> HINT:  See C include file "ntstatus.h" for a description of the
> hexadecimal value.
> LOG:  terminating any other active server processes
>
> 0xC0000005 is access violation, but I can't follow why.
>
> I am using Visual Studio 2005 SP1 compiler. Since mine PostgreSQL 8.4
> server uses msvcr71.dll, not 80, maybe it's compiler incompatibility
> of some kind?

did you remember to set up the V1 calling convention macro?

merlin

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Re: PostgreSQL 8.4 crash on user defined C language function

От
Vincas Dargis
Дата:
Yeah, original example took command from arguments, I simply forgot to
revert changes to original.

Though it's not the problem, it is something with calling other
functions under some kind environment I got. Your test was successful
for "obviously it should work" reason, but it's something wrong for me
that server crashes before executing "INFO 5"...


2012.04.05 02:38, iihero rašė:
> Hi,
>
> In your code, only the last line "pfree(command);" is wrong. No need to
> put it there. Since you use the constant string for the variable command.
>
> In my test,
>
> here is the result:
>
> iihero=# select test();
> INFO: 1
> INFO: 2
> INFO: 3
> INFO: 4
> INFO: 5
> INFO: 6
> INFO: 7
> INFO: EXECQ: 1
> test
> ------
>
> (1 row)
>
>
> ------------------
> ~~~~~~~~~~~~~~~~~~~~~~~~~
> iihero
> http://www.sql9.com
> http://www.sql6.com
> ~~~~~~~~~~~~~~~~~~~~~~~~~

Re: PostgreSQL 8.4 crash on user defined C language function

От
"iihero"
Дата:
If possible,  could you use a compiled version of PG8.4, and debug your code via attaching the server process.   Thus you can get more details why it crashed in your codeline.
 
 
------------------ Original ------------------
Date:  Thu, Apr 5, 2012 02:09 PM
To:  "iihero"<iihero@qq.com>;
Cc:  "Merlin Moncure"<mmoncure@gmail.com>; "pgsql-general"<pgsql-general@postgresql.org>;
Subject:  Re: [GENERAL] PostgreSQL 8.4 crash on user defined C language function
 
Yeah, original example took command from arguments, I simply forgot to
revert changes to original.

Though it's not the problem, it is something with calling other
functions under some kind environment I got. Your test was successful
for "obviously it should work" reason, but it's something wrong for me
that server crashes before executing "INFO 5"...


2012.04.05 02:38, iihero ra??:
> Hi,
>
> In your code, only the last line "pfree(command);" is wrong. No need to
> put it there. Since you use the constant string for the variable command.
>
> In my test,
>
> here is the result:
>
> iihero=# select test();
> INFO: 1
> INFO: 2
> INFO: 3
> INFO: 4
> INFO: 5
> INFO: 6
> INFO: 7
> INFO: EXECQ: 1
> test
> ------
>
> (1 row)
>
>
> ------------------
> ~~~~~~~~~~~~~~~~~~~~~~~~~
> iihero
> http://www.sql9.com
> http://www.sql6.com
> ~~~~~~~~~~~~~~~~~~~~~~~~~

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general