Обсуждение: SPI_getvalue fails
Hello group, I get a coredump if I try to use SPI_getvalue. Upon writing some logs, I see that postgresql gets the insert statement properlywith all the values non null but the variable trigdata->tg_trigtuple is NULL. I am not able to figure out why it happens so. I would be very grateful if you can help me with this friends. With warm regards. Vivek J. Joshi. vivek@staff.ownmail.com Trikon electronics Pvt. Ltd. All science is either physics or stamp collecting. -- Ernest Rutherford
On Mon, Jul 31, 2006 at 12:29:25PM +0530, vivek@staff.ownmail.com wrote: > I get a coredump if I try to use SPI_getvalue. Upon writing some > logs, I see that postgresql gets the insert statement properly > with all the values non null but the variable trigdata->tg_trigtuple > is NULL. I am not able to figure out why it happens so. The connection between trigdata->tg_trigtuple being NULL and SPI_getvalue() causing a core dump isn't clear from what you've written. It sounds like you're writing a C function that's called via a trigger -- is that correct? Could you post the CREATE TRIGGER statement and the function's code? -- Michael Fuhr
Here is the code:- #include <stdio.h> #include "postgres.h" #include "executor/spi.h" #include "commands/trigger.h" #include <math.h> #include <sys/time.h> #include <time.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <errno.h> #include <stddef.h> #include <dirent.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> extern Datum trigast(PG_FUNCTION_ARGS); extern char **environ; int do_log(char *log) { FILE *fp=fopen("/home/vivek/logs/trig.log","a"); if (fp) { time_t t = time(NULL); struct tm *c_date = localtime(&t); char *t_now=strdup(asctime(c_date)); t_now[strlen(t_now)-1]=0; fprintf(fp,"%s-trigfaxes-%s\n",t_now,log); fclose(fp); return 1; } return -1; } PG_FUNCTION_INFO_V1(trigast); Datum trigast(PG_FUNCTION_ARGS) {do_log("STARTING TRIGGER");HeapTuple rettuple;HeapTuple trigtuple;TupleDesc tupdesc;int ret, i;bool isnull;char *disposition;//13char *when;char logs[1024];do_log(" Checking fcinfo");if (CALLED_AS_TRIGGER(fcinfo)) { TriggerData *trigdata= (TriggerData *) fcinfo->context; SPI_connect(); do_log("GOT TRIGTUPLE"); rettuple = trigdata->tg_trigtuple; char *relation=SPI_getrelname(trigdata->tg_relation); sprintf(logs,"Relation is %s", relation); do_log(logs); trigtuple = trigdata->tg_trigtuple; tupdesc = trigdata->tg_relation->rd_att; if( tupdesc== NULL || trigtuple == NULL ){ do_log("tupdesc OR trigtuple == NULL" ); if( tupdesc ==NULL ) do_log("tupdesc is NULL" ); if( trigtuple == NULL ) do_log("trigtuple is NULL" ); } else do_log("tupdesc && trigtuple ARE NOT NULL" ); do_log("Starting to get values"); disposition=SPI_getvalue(trigtuple, tupdesc,13); sprintf(logs, "value is %s",disposition); do_log(logs); // doesnot reach here. }SPI_finish(); return PointerGetDatum(rettuple); } /* logs are:- Mon Jul 31 18:20:12 2006-trigfaxes-STARTING TRIGGER Mon Jul 31 18:20:12 2006-trigfaxes- Checking fcinfo Mon Jul 31 18:20:12 2006-trigfaxes-GOT TRIGTUPLE Mon Jul 31 18:20:12 2006-trigfaxes-Relation is ast_cdr Mon Jul 31 18:20:12 2006-trigfaxes-tupdesc OR trigtuple == NULL Mon Jul 31 18:20:12 2006-trigfaxes-trigtuple is NULL Mon Jul 31 18:20:12 2006-trigfaxes-Starting to get values */ /* gdb shows:- Program received signal SIGSEGV, Segmentation fault. 0x08121f46 in SPI_getvalue () (gdb) bt #0 0x08121f46 in SPI_getvalue () */ With warm regards. Vivek J. Joshi. vivek@staff.ownmail.com Trikon electronics Pvt. Ltd. All science is either physics or stamp collecting. -- Ernest Rutherford Michael Fuhr wrote: > >On Mon, Jul 31, 2006 at 12:29:25PM +0530, vivek@staff.ownmail.com wrote: >> I get a coredump if I try to use SPI_getvalue. Upon writing some >> logs, I see that postgresql gets the insert statement properly >> with all the values non null but the variable trigdata->tg_trigtuple >> is NULL. I am not able to figure out why it happens so. > >The connection between trigdata->tg_trigtuple being NULL and >SPI_getvalue() causing a core dump isn't clear from what you've >written. It sounds like you're writing a C function that's called >via a trigger -- is that correct? Could you post the CREATE TRIGGER >statement and the function's code? > >-- >Michael Fuhr > >---------------------------(end of broadcast)--------------------------- >TIP 6: explain analyze is your friend
On Mon, Jul 31, 2006 at 06:31:54PM +0530, vivek@staff.ownmail.com wrote: > Here is the code:- Could you post the CREATE TRIGGER statement? tg_trigtuple would be NULL if you're using a statement-level trigger instead of a row-level trigger. -- Michael Fuhr
Thanks friends, solved it.added a FOR EACH ROW and it worked. Thanks for all your support With warm regards. Vivek J. Joshi. vivek@staff.ownmail.com Trikon electronics Pvt. Ltd. All science is either physics or stamp collecting. -- Ernest Rutherford vivek@staff.ownmail.com wrote: > >Here is the code:- > >#include <stdio.h> >#include "postgres.h" >#include "executor/spi.h" >#include "commands/trigger.h" >#include <math.h> >#include <sys/time.h> >#include <time.h> >#include <sys/types.h> >#include <sys/wait.h> >#include <unistd.h> >#include <errno.h> >#include <stddef.h> >#include <dirent.h> > >#include <fcntl.h> >#include <stdlib.h> >#include <string.h> > >extern Datum trigast(PG_FUNCTION_ARGS); >extern char **environ; > >int do_log(char *log) >{ > FILE *fp=fopen("/home/vivek/logs/trig.log","a"); > if (fp) > { > time_t t = time(NULL); > struct tm *c_date = localtime(&t); > char *t_now=strdup(asctime(c_date)); > t_now[strlen(t_now)-1]=0; > fprintf(fp,"%s-trigfaxes-%s\n",t_now,log); > fclose(fp); > return 1; > } > return -1; >} > > >PG_FUNCTION_INFO_V1(trigast); >Datum >trigast(PG_FUNCTION_ARGS) >{ > do_log("STARTING TRIGGER"); > HeapTuple rettuple; > HeapTuple trigtuple; > TupleDesc tupdesc; > int ret, i; > bool isnull; > char *disposition; //13 > char *when; > char logs[1024]; > > do_log(" Checking fcinfo"); > if (CALLED_AS_TRIGGER(fcinfo)) { > TriggerData *trigdata = (TriggerData *) fcinfo->context; > SPI_connect(); > > do_log("GOT TRIGTUPLE"); > > rettuple = trigdata->tg_trigtuple; > > char *relation=SPI_getrelname(trigdata->tg_relation); > sprintf(logs,"Relation is %s", relation); > do_log(logs); > > trigtuple = trigdata->tg_trigtuple; > tupdesc = trigdata->tg_relation->rd_att; > > if( tupdesc == NULL || trigtuple == NULL ){ > do_log("tupdesc OR trigtuple == NULL" ); > if( tupdesc == NULL ) > do_log("tupdesc is NULL" ); > if( trigtuple == NULL ) > do_log( "trigtuple is NULL" ); > } > else > do_log("tupdesc && trigtuple ARE NOT NULL" ); > > > do_log("Starting to get values"); > > disposition=SPI_getvalue(trigtuple, tupdesc,13); > sprintf(logs, "value is %s", disposition); > do_log(logs); // doesnot reach here. > > } > SPI_finish(); > return PointerGetDatum(rettuple); > >} > >/* >logs are:- >Mon Jul 31 18:20:12 2006-trigfaxes-STARTING TRIGGER >Mon Jul 31 18:20:12 2006-trigfaxes- Checking fcinfo >Mon Jul 31 18:20:12 2006-trigfaxes-GOT TRIGTUPLE >Mon Jul 31 18:20:12 2006-trigfaxes-Relation is ast_cdr >Mon Jul 31 18:20:12 2006-trigfaxes-tupdesc OR trigtuple == NULL >Mon Jul 31 18:20:12 2006-trigfaxes-trigtuple is NULL >Mon Jul 31 18:20:12 2006-trigfaxes-Starting to get values >*/ > >/* >gdb shows:- >Program received signal SIGSEGV, Segmentation fault. >0x08121f46 in SPI_getvalue () >(gdb) bt >#0 0x08121f46 in SPI_getvalue () >*/ > > > > >With warm regards. > >Vivek J. Joshi. > >vivek@staff.ownmail.com >Trikon electronics Pvt. Ltd. > >All science is either physics or stamp collecting. > -- Ernest Rutherford > > > >Michael Fuhr wrote: >> >On Mon, Jul 31, 2006 at 12:29:25PM +0530, vivek@staff.ownmail.com wrote: >>> I get a coredump if I try to use SPI_getvalue. Upon writing some >>> logs, I see that postgresql gets the insert statement properly >>> with all the values non null but the variable trigdata->tg_trigtuple >>> is NULL. I am not able to figure out why it happens so. >> >>The connection between trigdata->tg_trigtuple being NULL and >>SPI_getvalue() causing a core dump isn't clear from what you've >>written. It sounds like you're writing a C function that's called >>via a trigger -- is that correct? Could you post the CREATE TRIGGER >>statement and the function's code? >> >>-- >>Michael Fuhr >> >>---------------------------(end of broadcast)--------------------------- >>TIP 6: explain analyze is your friend > > > >---------------------------(end of broadcast)--------------------------- >TIP 2: Don't 'kill -9' the postmaster