Create a trigger update [closed] - postgresql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Create a trigger that automatically when inserting data into the builds_result table updated the value of the updated_time field of the apps table to the current date and time. Error is given in on apps.updated_time
create trigger upd after insert
on builds_result
for each row
execute procedure new apps.updated_time = now();

Here's a brief overview of how to setup a trigger.
First create the trigger function:
CREATE OR REPLACE FUNCTION my_trigger_function()
RETURNS TRIGGER AS
$BODY$
BEGIN
UPDATE apps
SET updated_time = NOW();
RETURN NEW;
END
$BODY$
LANGUAGE plpgsql;
It's very simple, just updates the table (notice it will update all records, this is simply because you haven't said which particular record should be updated) and returns NEW, which is the record that was inserted in builds_result.
Then you create a trigger on the builds_result table to execute this function:
CREATE TRIGGER upd
AFTER INSERT ON builds_result
FOR EACH ROW
EXECUTE PROCEDURE my_trigger_function();
That's it. Now that trigger function will be called every time a row is inserted into the builds_result table.

Related

Error on an SQL trigger between 2 tables, one of which has a foreign key on the other [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Hello I am having the same problem with a trigger, I do not understand my error.
The script compiles but when I delete an element I have this error
CREATE OR REPLACE FUNCTION delete_foreign_key() RETURNS TRIGGER AS $_$
BEGIN
delete from planning.ordrelivraison_history
where ordrelivraison_history.ordrelivraisonid = old.planning.ordrelivraison.ordreid;
RETURN OLD;
END;
$_$ LANGUAGE plpgsql;
Create trigger delete_foreign_key_in_order_delivery_history
after delete
on planning.ordrelivraison_history
for each row
execute procedure delete_foreign_key();
------------------------------------------------------
delete
from planning.ordrelivraison
where ordreid = 1656
Voici l'erreur
ERROR: ERROR: UPDATE or DELETE on the "delivery order" table violates the foreign key constraint
"Ordrelivraison_history_ordreid" from the "ordrelivraison_history" table
DETAIL: The key (orderid) = (1656) is always referenced from the “ordrelivraison_history” table.
SQL state: 23503
Your trouble in you are firing the trigger at the wrong time. You declare it as AFTER delete but that makes it an after statement trigger, unfortunately the trigger function does not get the old/new rows on after statement trigger. See CREATE TRIGGER documentation. Change your trigger to:
Create trigger delete_foreign_key_in_order_delivery_history
after delete
on ordrelivraison_history
for each row
execute procedure delete_foreign_key();

Insert %rowtype data into another table - plpgsql / Postgres [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have one table called empl, and have another table new_empl with the same columns and definition with empl. Is it possible to insert records from v_record in new_empl using the following code ?
DECLARE
v_record empl%rowtype;
BEGIN
Insert into new_empl values v_record;
END;
There are too many columns in empl table and I want to avoid listing them.
The above snippet would work for Oracle, but for Postgres/pgplsql, the following snippet works like a charm:
DECLARE
v_record empl%rowtype;
BEGIN
Insert into new_empl values (v_record.*);
END;
Is it possible to insert records from v_record in new_empl using the
following code
Yes its possible however the way you are doing insert will not insert anything as nothing to assigned to the variable v_record . Rather you can do something like below:
DECLARE
v_record empl%rowtype;
BEGIN
Insert into new_empl Select * from empl;
END;
But why you want to do it in a PLSQL block when you can do it SQL itself.
Insert into new_empl select * from empl;

Calculating age from dob in PostgreSQL using trigger [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to calculate the age of a person when a record is inserted or updated in table staff but somehow the below trigger function does not work:
create or replace function Age()
returns trigger as $body$
declare
dob1 date;
age1 integer;
BEGIN
select dob into dob1 from staff;
select age(current_date,dob) into age1;
update staff set age=age1;
END; $body$ language plpgsql;
create trigger trig_age
before insert or update on staff
FOR EACH ROW
execute procedure age();
I'm not sure about this above code. I want a trigger to calculate and insert the AGE when inserting DOB in some table
First of all, the name of the trigger function may not be the most appropriate. There is already an overloaded regular function by that name and while the parameters differ and PostgreSQL will therefore accept it, you might confuse whomever has to work with your code.
Otherwise you are not far off. The principal change is that you should not call an UPDATE in a trigger function for the same table that the trigger is fired for: you may get an infinite loop. Just use assignments to the NEW implicit parameter. Since you obviously want the age in years of a person, you should get that specific date_part() into field age.
CREATE OR REPLACE FUNCTION age_in_years() RETURNS TRIGGER AS $body$
BEGIN
NEW.age := date_part('year', CURRENT_TIME - NEW.dob::timestamp);
RETURN NEW; -- or the insert or update would fail
END;
$body$ LANGUAGE plpgsql;
Setting the values in the NEW parameter and returning that parameter at the end of the function will change the values that will be inserted or updated into the table.
However, a person's age may change in between updates so you are probably better off dropping the column age from table staff (and thus also the trigger) and then creating a view that calculates the age whenever that view is accessed:
CREATE VIEW staff_today AS
SELECT *, date_part('year', CURRENT_TIME - dob::timestamp) AS age
FROM staff;
Like so you will always get the correct age.

Is there a way to declare postgresql row immutable given a condition?

I'm creating records in a transaction.
begin;
update parent_records where id=5 set closed = now();
insert into child_records ...;
commit;
I want to prevent inserting new child_records once the parent record is closed. It seems like setting a rule on parent_records to blow up on update actions when closed would solve the problem, as the transaction would fail.
I could do the update with where closed is null then check in app code if any rows were updated and rollback, but i would rather the constraint be in the database itself.
How do i mark the parent row immutable (updates fail with error) when a condition is met (closed column not null)?
Use a trigger, e.g.:
create function before_update_on_parent_records()
returns trigger language plpgsql as $$
begin
if old.closed is not null then
raise exception 'cannot update because the row is closed';
end if;
return new;
end $$;
create trigger before_update_on_parent_records
before update on parent_records
for each row execute procedure before_update_on_parent_records();

postgresql triggers after update

The following function ,update_sessioninfo(), should only update changed columns. The New.* columns are being updated to some incorrect values after running:
update freeradius.radacct set acctsessiontime=25 where radacctid=3;
function
CREATE OR REPLACE FUNCTION update_sessioninfo() RETURNS trigger AS $radacct_update$
BEGIN
-- update the updated records
update freeradius.day_guiding_usage set acctstoptime=New.acctstoptime,acctsessiontime=New.acctsessiontime,connectinfo_start=New.connectinfo_start,connectinfo_stop=New.connectinfo_stop,acctinputoctets=New.acctinputoctets,acctoutputoctets=New.acctoutputoctets,acctterminatecause=New.acctterminatecause where acctsessionid=Old.acctsessionid;
RETURN NULL;
END;
$radacct_update$ LANGUAGE plpgsql;
The trigger is below
CREATE TRIGGER radacct_update AFTER UPDATE ON freeradius.radacct
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE procedure update_sessioninfo();
WHEN (OLD.* IS DISTINCT FROM NEW.*) means that there's something changed => old row is different from new row, but when update happens it will update all columns of your table.
See examples and documentation here: http://www.postgresql.org/docs/9.2/static/sql-createtrigger.html