473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Error loading trigger in C

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

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
2 2040
Juan Jose Costello Levien <jc*******@datafull.com> writes:
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'.


Does it work if you take pgAdmin out of the loop? E.g. in psql:

CREATE FUNCTION trigf ...
CREATE TRIGGER ...

-Doug

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
Doug,

You were right, psql let me insert the trigger.
But when I insert a new row in the table 'visita' (the one that has the
trigger), the result must be an audit new row on 'visita_log' because of
the trigger, and pgAccess reports that the connection was lost. Maybe is
something wrong with the trigger... I don't know what it is... (this is
the first time I program a trigger with PgSQL...)

Thanks in advance.

Juan
Doug McNaught wrote:
Juan Jose Costello Levien <jc*******@datafull.com> writes:
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'.


Does it work if you take pgAdmin out of the loop? E.g. in psql:

CREATE FUNCTION trigf ...
CREATE TRIGGER ...

-Doug

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Teja via SQLMonster.com | last post by:
I made a trigger for delete just like this. " CREATE TRIGGER ON . FOR DELETE AS Declare @severity int, @IdNmbr nvarchar(10) Set @Severity = 0
1
by: Jen S | last post by:
I feel like I'm missing something obvious here, but I'm stumped... I have a stored procedure with code that looks like: INSERT INTO MyTableA ( ...fields... ) VALUES (...values...) IF...
12
by: Bob Stearns | last post by:
I am trying to create a duplicate prevention trigger: CREATE TRIGGER is3.ard_u_unique BEFORE UPDATE OF act_recov_date ON is3.flushes REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN...
1
by: VM | last post by:
How can I manually run the code of an event? For example, I want to run a Mouse click event without actually clicking doing a Mouse click? Thanks.
3
by: Steve Richter | last post by:
here is an error I get when I open my class library project: "The class ResultsTable can be designed, but is not the first class in the file. Visual Studio requires that designers use the first...
3
by: HK guy | last post by:
How to write a program that can check the web page loading time? Before I have written a browser that use IE as the core, but it does not support frame. Since the documentcomplete event will...
3
by: danceli | last post by:
After loading the BCP files that are created during the trigger/ reporting events I've noticed that the data in the table is missing records. I've also noticed that the missing records (records in...
2
by: wugon.net | last post by:
Problem: after inser trigger encounter error sql0348 Env:db2 v8 + fp 13 + win xp Description: we build two after insert triggers DB2.TRG1, DB2.TRG2 on base table DB2.TEST1, insert data into...
5
by: Bruno Rafael Moreira de Barros | last post by:
function test1() { trigger_error('My error'); } application.php //code... test1(); //code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.