X-Original-To: pgsql-interfaces-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id C87CB9FB339 for ; Mon, 31 Jul 2006 10:05:55 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 85313-05 for ; Mon, 31 Jul 2006 10:05:39 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 74C0E9FB319 for ; Mon, 31 Jul 2006 10:05:24 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from mail009.ownmail.com (unknown [203.199.110.93]) by svr4.postgresql.org (Postfix) with ESMTP id 5678E5AF035 for ; Mon, 31 Jul 2006 13:05:11 +0000 (GMT) Received: (from domains@localhost) by mail009.ownmail.com (8.12.11/8.12.10) id k6VD1sJ3030107; Mon, 31 Jul 2006 18:31:54 +0530 Date: Mon, 31 Jul 2006 18:31:54 +0530 Message-Id: <200607311301.k6VD1sJ3030107@mail009.ownmail.com> X-Originating-IP: 203.199.110.93 Reply-To: From: To: pgsql-interfaces@postgresql.org Subject: Re: SPI_getvalue fails MIME-Version: 1.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=2.022 tagged_above=0 required=5 tests=AWL, NO_REAL_NAME, SPF_SOFTFAIL X-Spam-Level: ** Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Archive-Number: 200607/20 X-Sequence-Number: 6210 Here is the code:- #include #include "postgres.h" #include "executor/spi.h" #include "commands/trigger.h" #include #include #include #include #include #include #include #include #include #include #include #include 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