473,404 Members | 2,114 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,404 software developers and data experts.

Trigger Update Issue

Hello All

I'm running 7.3.4-1 on a RH9 box. I'm having a problem with a trigger
that seems to execute without actually performing the update that it
should. The update returns true everytime however if it is the first time
that the trigger executes on a given row, the column is not updated. The
column is updated correctly on subsequent calls.

Here is the code:

create table tabA (
id char(32) primary key not null,
Acol1 char(40) not null unique,
Acol2 integer not null,
Acol3 integer default 0 check ( Acol3 >= 0),
);

create table tabB (
id integer default nextval('tabB_id_seq'::text)
not null check (id > 0) primary key,
tabA_id char(32) not null references tabA (id)
on delete cascade on update cascade,
Bcol1 text default null,
Bcol2 text default null,
);
CREATE OR REPLACE FUNCTION "tabB_postinsert" () RETURNS TRIGGER AS '
--
-- Actions to take after inserting into tabB
--
BEGIN
-- Increment tabA.attachments
UPDATE tabA SET Acol3 = Acol3 + 1 WHERE id = NEW.tabA_id;
-- done
RETURN NEW;
END; ' LANGUAGE 'plpgsql';

CREATE TRIGGER "master_tabB_postinsert"
AFTER INSERT ON "tabB"
FOR EACH ROW EXECUTE PROCEDURE "tabB_postinsert" ();

I am using this same trigger structure on other tables without having any
issues. Any insight would be greatly appreciated.

Thanks

-b

---------------------------(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
3 3412
On Friday 05 March 2004 14:00, beer wrote:
Hello All

I'm running 7.3.4-1 on a RH9 box. I'm having a problem with a trigger
that seems to execute without actually performing the update that it
should. The update returns true everytime however if it is the first time
that the trigger executes on a given row, the column is not updated. The
column is updated correctly on subsequent calls.
Perhaps put some debug code in and insert into tabB manually
CREATE OR REPLACE FUNCTION "tabB_postinsert" () RETURNS TRIGGER AS '
--
-- Actions to take after inserting into tabB
-- DECLARE
numrows int4;
BEGIN
-- Increment tabA.attachments
UPDATE tabA SET Acol3 = Acol3 + 1 WHERE id = NEW.tabA_id; GET DIAGNOSTICTS numrows = ROW_COUNT;
RAISE NOTICE ''Updated % rows with id = %'',numrows, NEW.tabA_id; -- done
RETURN NEW;
END; ' LANGUAGE 'plpgsql';


Insert into tabB from psql and you should see a NOTICE message telling you
what is happening.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #2
Richard

Thanks for the suggestion. I had tried something similar using FOUND but
that didnt not give me the number of rows touched.

According to the output, 1 row was updated, however when I select on the
row the value is still 0.

-b
On Friday 05 March 2004 14:00, beer wrote:
Hello All

I'm running 7.3.4-1 on a RH9 box. I'm having a problem with a trigger
that seems to execute without actually performing the update that it
should. The update returns true everytime however if it is the first
time
that the trigger executes on a given row, the column is not updated.
The
column is updated correctly on subsequent calls.


Perhaps put some debug code in and insert into tabB manually
CREATE OR REPLACE FUNCTION "tabB_postinsert" () RETURNS TRIGGER AS '
--
-- Actions to take after inserting into tabB
--

DECLARE
numrows int4;
BEGIN
-- Increment tabA.attachments
UPDATE tabA SET Acol3 = Acol3 + 1 WHERE id = NEW.tabA_id;

GET DIAGNOSTICTS numrows = ROW_COUNT;
RAISE NOTICE ''Updated % rows with id = %'',numrows, NEW.tabA_id;
-- done
RETURN NEW;
END; ' LANGUAGE 'plpgsql';


Insert into tabB from psql and you should see a NOTICE message telling you
what is happening.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #3
"beer" <be**@cmu.edu> writes:
I'm running 7.3.4-1 on a RH9 box. I'm having a problem with a trigger
that seems to execute without actually performing the update that it
should. The update returns true everytime however if it is the first time
that the trigger executes on a given row, the column is not updated. The
column is updated correctly on subsequent calls.


I couldn't reproduce this. I created the tables and trigger and then
did:

regression=# insert into tabA values('id1','col1', 2, 32);
INSERT 154119 1
regression=# insert into tabB values(1,'id1','col1','col2');
INSERT 154120 1
regression=# select * from tabA;
id | acol1 | acol2 | acol3
----------------------------------+------------------------------------------+-------+-------
id1 | col1 | 2 | 33
(1 row)

regression=# insert into tabB values(2,'id1','col1','col2');
INSERT 154121 1
regression=# select * from tabA;
id | acol1 | acol2 | acol3
----------------------------------+------------------------------------------+-------+-------
id1 | col1 | 2 | 34
(1 row)

It looks fine to me ... what are you doing differently?

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4

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

Similar topics

1
by: Gent | last post by:
am using FOR UPDATE triggers to audit a table that has 67 fields. My problem is that this slows down the system significantly. I have narrowed down the problem to the size (Lines of code) that need...
3
by: takilroy | last post by:
Hi, Does anyone know of a simple way to do this? I want to create an insert trigger for a table and if the record already exists based on some criteria, I want to update the table with the...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
3
by: Ricardo Corsi | last post by:
Hi, It is possible to call a trigger (inside a sql server..) with asp.net ? how can i do that ? thks!
6
by: Robert Fitzpatrick | last post by:
On 7.4.2 I have a trigger that I want to update any existing boolean values to false if a new one in that group is declare true by inserting a new record or updating an existing record: ohc=#...
9
by: steven | last post by:
Does anyone know how to do the following. I'm trying to mimic replication with triggers. I have 2 databases, each have these 2 tables. 1. USERS ID int NAME varchar(20)
13
by: dennis | last post by:
Hello, I'm having trouble solving the following problem with DB2 UDB 8.2. I need to create a trigger that performs certain extra constraint validations (temporal uniqueness). One of the tables...
8
by: Benzine | last post by:
Hi, I have an issue with my replication at the moment. I will try to describe the scenario accurately. I am using MS SQL 2000 SP4 with Merge Replication. Subscribers connect to the publisher...
2
by: paulmac106 | last post by:
Hi, I need to update a field in about 20 records on a table. The table has an update trigger (which updates the field whenever a record is updated). As a result I'm getting an error: "Subquery...
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.