Error loading trigger in C

From: Juan Jose Costello Levien <jcostello(at)datafull(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Error loading trigger in C
Date: 2004-07-01 16:44:06
Message-ID: 40E43F56.1010507@datafull.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,
I am trying to use a trigger function I wrote in C. Basically what I
want to do is to audit a table when a row is inserted into another table
by copying the row to the new table. It compiles Ok and I created a
shared library trigger.so. But when I load it into pgAdmin it tells me
'An error had occured'.

To address that I put here the source code so you can check it out:
(note: it has a oid (blob) field the row, and that could be a problem
for the query syntax, maybe there is the error)

#include "postgres.h"
#include "executor/spi.h" /* SPI */
#include "commands/trigger.h" /* triggers */
#include <stdlib.h>
#include <string.h>

extern Datum trigf(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(trigf);

Datum
trigf(PG_FUNCTION_ARGS)
{

char * query = (char *)malloc(sizeof(char *)*500);

TriggerData *trigdata = (TriggerData *) fcinfo->context;

TupleDesc tupdesc;

HeapTuple rettuple;

char *when;

bool checknull = false;

bool isnull;

int ret, i;

/* aquí nos aseguramos que se llama a la función como un trigger*/

if (!CALLED_AS_TRIGGER(fcinfo))

elog(ERROR, "trigf: no se llamó por el administrador de triggers");

/* datos que devuelve el ejecutor */

if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))

rettuple = trigdata->tg_newtuple;

else

rettuple = trigdata->tg_trigtuple;

/* chequeo de valores nulos */

if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)

&& TRIGGER_FIRED_BEFORE(trigdata->tg_event))

checknull = true;

if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))

when = "antes";

else

when = "después";

tupdesc = trigdata->tg_relation->rd_att;

/* conexión al SPI (Server Programming Interface) */

if ((ret = SPI_connect()) < 0)

elog(INFO, "trigf (disparado %s): SPI_connect devolvió %d", when, ret);

/* ejecuto el query para insertar en la tabla de auditoría */

strcpy(query, "INSERT INTO visita_log VALUES ('");

strcat(query, trigdata->tg_trigger->tgargs[0]);

strcat(query, "','");

strcat(query, trigdata->tg_trigger->tgargs[1]);

strcat(query, "','");

strcat(query, trigdata->tg_trigger->tgargs[2]);

strcat(query, "','");

strcat(query, trigdata->tg_trigger->tgargs[3]);

strcat(query, "',");

strcat(query, trigdata->tg_trigger->tgargs[4]);

strcat(query, ",'");

strcat(query, trigdata->tg_trigger->tgargs[5]);

strcat(query, "');");

SPI_exec(query, 0);

/* count(*) devuelve int8, cuidado con la conversión */

i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],
SPI_tuptable->tupdesc,

1,

&isnull));

elog (INFO, "trigf (disparado %s): hay %d filas en la tabla", when, i);

SPI_finish();

if (checknull)

{

SPI_getbinval(rettuple, tupdesc, 1, &isnull);

if (isnull)
rettuple = NULL;

}

return PointerGetDatum(rettuple);

}

// End of source code

I use PostgreSQL 7.4.1 and Mandrake 10.
The compiler sequence I used is:

gcc -I "./" -fpic -c trigger.c
gcc -I "./" -shared -o trigger.so trigger.o

and when I include it in Trigger functions pgAdmin complains with "An
error had occured" (and says nothing). What I am doing wrong?

Thanks in advance.

Juan

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-07-01 16:49:35 Re: Problems restarting after database crashed (signal 11).
Previous Message Rune Froysa 2004-07-01 16:35:36 Slow deletion of data from tables