Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: SPI_getvalue fails



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(at)staff(dot)ownmail(dot)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(at)staff(dot)ownmail(dot)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





Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group