Thread: SPI_getvalue gives a core dump

SPI_getvalue gives a core dump

From
Date:
I am using postgresql:-PostgreSQL 8.0.3 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3.4
I am trying to trigger a function on insert. I get the following error:-


LOG:  statement: INSERT INTO ast_cdr
(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield)
VALUES('2006-07-28 20:35:14','"15554216325" <15554216325>','15554216325','889','from_box',
'SIP/15554216325-5914','SIP/69.54.75.50-da64','Playback','vivek/myfile'|noanswer',3,0,'NO
ANSWER',3,'0','1154099114.198','vivek'

LOG:  statement: SELECT count(*) FROM ast_cdr
CONTEXT:  SQL statement "SELECT count(*) FROM ast_cdr"
LOG:  server process (PID 5561) was terminated by signal 11
LOG:  terminating any other active server processes
WARNING:  terminating connection because of crash of another server process
DETAIL:  The postmaster has command
ed this server process to roll back the current transaction and exit, because another server process exited abnormally
andpossibly corrupted shared memory.
 


I tried executing this:- 
SELECT count(*) FROM ast_cdr;
-[ RECORD 1 ]
count | 2

I dont know where am i going wrong. Here is the source.

TriggerData *trigdata = (TriggerData *) fcinfo->context;
TupleDesc   tupdesc;
if (CALLED_AS_TRIGGER(fcinfo)) {  ==================== it comes here and enters inside.
userfield=SPI_getvalue(trigdata->tg_trigtuple,tupdesc,17);================== it fails here because i tried to get logs
beforethis statement and it succeeded. 
 

Please help this newbie.





With warm regards.

Vivek J. Joshi.

vivek@staff.ownmail.com
Trikon electronics Pvt. Ltd.

All science is either physics or stamp collecting.               -- Ernest Rutherford





Re: SPI_getvalue gives a core dump

From
Tom Lane
Date:
<vivek@staff.ownmail.com> writes:
> I dont know where am i going wrong. Here is the source.

> TriggerData *trigdata = (TriggerData *) fcinfo->context;
> TupleDesc   tupdesc;
> if (CALLED_AS_TRIGGER(fcinfo)) {  ==================== it comes here and enters inside.
>     userfield=SPI_getvalue(trigdata->tg_trigtuple,tupdesc,17); ================== it fails here because i tried to
getlogs before this statement and it succeeded. 
 

Wrong tupdesc maybe?  You didn't show us what you are setting tupdesc
to.
        regards, tom lane


Re: SPI_getvalue gives a core dump

From
Date:
This is what i set my tupdesc to. =>  tupdesc = trigdata->tg_relation->rd_att;
i am using the code like this. the do_log function to log the flow. it gives all correct until it reaches 
the line :-
disposition=SPI_getvalue(trigdata->tg_trigtuple, tupdesc, 13);               do_log("Got disposition");         ======>
itstops logging from here.
 

               do_log("Entered the main trigger logic");  =========> success               if
(TRIGGER_FIRED_BEFORE(trigdata->tg_event))when = "before";               else when = "after ";
 
               tupdesc = trigdata->tg_relation->rd_att;               if ((ret = SPI_connect()) < 0)
  do_log("SPI_connect failed");               else {                       sprintf(logs,"SPI_connect success, fired
%s",when); ======> success                       do_log(logs);               }               ret = SPI_exec("SELECT
count(*)FROM ast_cdr", 0);
 
               if (ret < 0){                       sprintf(logs, "trigf (fired %s): SPI_exec returned %d", when, ret);
                    do_log(logs);               }
 
               /* count(*) returns int8, so be careful to convert */               i =
DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],tupdesc, 1, &isnull));               sprintf(logs, "trigf (fired %s):
thereare %d rows in ast_cdr", when, i); ====> success               do_log(logs);
 
                              disposition=SPI_getvalue(trigdata->tg_trigtuple, tupdesc, 13);  =====> failed.
  do_log("Got disposition");                          ===============> doesnot reach here.
 




With warm regards.

Vivek J. Joshi.

vivek@staff.ownmail.com
Trikon electronics Pvt. Ltd.

All science is either physics or stamp collecting.               -- Ernest Rutherford



Tom Lane wrote:
> ><vivek@staff.ownmail.com> writes:
>> I dont know where am i going wrong. Here is the source.
>
>> TriggerData *trigdata = (TriggerData *) fcinfo->context;
>> TupleDesc   tupdesc;
>> if (CALLED_AS_TRIGGER(fcinfo)) {  ==================== it comes here and 
enters inside.
>>     userfield=SPI_getvalue(trigdata->tg_trigtuple,tupdesc,17); 
================== it fails here because i tried to get logs before this 
statement and it succeeded. 
>
>Wrong tupdesc maybe?  You didn't show us what you are setting tupdesc
>to.
>
>            regards, tom lane
>
>---------------------------(end of broadcast)---------------------------
>TIP 4: Have you searched our list archives?
>
>               http://archives.postgresql.org




Re: SPI_getvalue gives a core dump

From
Tom Lane
Date:
<vivek@staff.ownmail.com> writes:
>                 i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0], tupdesc, 1, &isnull));

I think this is your problem: you're trying to use the wrong tupdesc to
retrieve from the SELECT count(*) result.  SPI_tuptable->tupdesc would
work better there.
        regards, tom lane


Re: SPI_getvalue gives a core dump

From
Date:
I have already tried it out. I tried the following:- 
char *myval
1) i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull));
2) myval=SPI_getvalue(trigdata->tg_trigtuple, tupdesc, 17);
3) myval=SPI_getvalue(trigdata->tg_trigtuple, trigdata->tg_relation->rd_att, 17);
4) myval=strdup(SPI_getvalue(trigdata->tg_trigtuple, trigdata->tg_relation->rd_att, 17));
5) if ((myval=SPI_getvalue(trigdata->tg_trigtuple, trigdata->tg_relation->rd_att, 17))==NULL) then, log and exit.


With warm regards.

Vivek J. Joshi.

vivek@staff.ownmail.com
Trikon electronics Pvt. Ltd.

All science is either physics or stamp collecting.               -- Ernest Rutherford



Tom Lane wrote:
> ><vivek@staff.ownmail.com> writes:
>>                 i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0], 
tupdesc, 1, &isnull));
>
>I think this is your problem: you're trying to use the wrong tupdesc to
>retrieve from the SELECT count(*) result.  SPI_tuptable->tupdesc would
>work better there.
>
>            regards, tom lane
>
>---------------------------(end of broadcast)---------------------------
>TIP 1: if posting/reading through Usenet, please send an appropriate
>       subscribe-nomail command to majordomo@postgresql.org so that your
>       message can get through to the mailing list cleanly