473,756 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_FUNCTI ON_ARGS);
PG_FUNCTION_INF O_V1(trigf);
Datum
trigf(PG_FUNCTI ON_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_TRI GGER(fcinfo))

elog(ERROR, "trigf: no se llamó por el administrador de triggers");
/* datos que devuelve el ejecutor */

if (TRIGGER_FIRED_ BY_UPDATE(trigd ata->tg_event))

rettuple = trigdata->tg_newtuple;

else

rettuple = trigdata->tg_trigtuple ;
/* chequeo de valores nulos */

if (!TRIGGER_FIRED _BY_DELETE(trig data->tg_event)

&& TRIGGER_FIRED_B EFORE(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(S PI_getbinval(SP I_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(r ettuple, 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 2070
Juan Jose Costello Levien <jc*******@data full.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*******@data full.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
1743
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
4336
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 (@@ERROR <> 0) BEGIN ROLLBACK TRANSACTION; RAISERROR('An error occurred in the stored proc.', 16, 1);
12
4762
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 (N.act_recov_date IS NOT NULL) BEGIN ATOMIC select count(*) into v_n from is3.flushes
1
4477
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
10839
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 class in the file. Move the class code so that it is the first class in the file and try loading the designer again." What does that mean, "the class xxx can be designed"? thanks,
3
2579
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 trigger if the frame is completed. So i don't know how to get the time for the whole document complete. Or beside the browser, any other method to do this. Thank you.
3
3934
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 the table but not in the BCP out files) seem to occur in contiguous blocks. Since the complete set of records exists in the table, I assume this points to an issue in the way the TableUpdate script/ Triggers interact with the system. But i tried...
2
3369
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 DB2.TEST1 encounter error sql0348 but we re-create trigger DB2.TRG2 before DB2.TRG1 and re-insert data
5
2644
by: Bruno Rafael Moreira de Barros | last post by:
function test1() { trigger_error('My error'); } application.php //code... test1(); //code...
0
9455
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9838
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.