Build a trigger that avoids to update the values of one column - postgresql

I have a table in PostgretSQL. I want to build a trigger that avoids to update colb1 column. This column has five alternatives: Rea, Can, Loa, Mul, Alm. So the trigger doesn't let upload values from Rea to Can.
I've built this function but this is fail.
CREATE OR REPLACE FUNCTION example_trigger()
RETURNS trigger AS
$BODY$
BEGIN
new.colb1.tabl1 = 'Rea' := old.colb1.tabl1 = 'Can';
new.colb1.tabl1 = 'Can' := old.colb1.tabl1 = 'Rea';
RETURN new;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
DROP TRIGGER IF EXISTS trigger_name ON table1;
CREATE TRIGGER trigger_name BEFORE UPDATE ON table1
FOR EACH ROW EXECUTE PROCEDURE example_trigger();
I edited my post with changes:
CREATE OR REPLACE FUNCTION example_trigger()
RETURNS TRIGGER AS
$$
BEGIN
NEW.colb1 := OLD.colb1;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER not_changes
BEFORE UPDATE
ON tabl1
FOR EACH ROW
EXECUTE PROCEDURE example_trigger();
It's not exactly that I want because I want that the values Loa, Mul and Alm can be update.

A slight modification of your function to stop the modification of colb1 from Rea or Can to Can or Rea.
CREATE OR REPLACE FUNCTION example_trigger()
RETURNS TRIGGER AS
$$
BEGIN
IF OLD.colb1 in ('Rea', Can') AND NEW.colb1 IN ('Rea', 'Can') THEN
NEW.colb1 := OLD.colb1;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
This will not stop a user from using an intermediate UPDATE to change to one of Loa/Mul/Alm and then to either Rea or Can

Related

How to avoid recursion in an update trigger that does an update?

I need to update the vat_total_sum of goods in a record using a trigger. But the trigger fires to update the record and falls into recursion. How can I avoid this?
The code:
CREATE OR REPLACE FUNCTION vat_total_sum() RETURNS TRIGGER AS $$
BEGIN
UPDATE goods SET vat_total_sum = NEW.vat_sum / 100 * NEW.vat_percent
WHERE goods_id = new.goods_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER vat_total_sum AFTER INSERT OR UPDATE ON goods
FOR EACH ROW EXECUTE PROCEDURE vat_total_sum();
Don't use UPDATE, use a BEFORE triger and assign the new value:
CREATE OR REPLACE FUNCTION vat_total_sum() RETURNS TRIGGER AS $$
BEGIN
new.vat_total_sum := NEW.vat_sum / 100 * NEW.vat_percent;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER vat_total_sum
BEFORE INSERT OR UPDATE ON goods
FOR EACH ROW EXECUTE PROCEDURE vat_total_sum();

Trigger to update a column on update

I have written this function and this trigger:
CREATE OR REPLACE FUNCTION test4() RETURNS TRIGGER AS $BODY$
BEGIN
UPDATE "cf"
SET vol=(area*alt);
RETURN null;
END;
$BODY$
LANGUAGE plpgsql
trigger
CREATE TRIGGER trig_upd
AFTER OR UPDATE ON "cf"
FOR EACH ROW
EXECUTE PROCEDURE test4();
I have tested my function and it's ok. So I created the trigger, but I can't insert any value in the table "cf" because the system crashes.
If you want to update the value of vol for each modified row, don't use update, just assign the value. For this to work you also need to define the trigger as a before trigger:
CREATE OR REPLACE FUNCTION test4() RETURNS TRIGGER
AS
$BODY$
BEGIN
new.vol := new.area * new.alt;
RETURN new;
END;
$BODY$
LANGUAGE plpgsql;
CREATE TRIGGER trig_upd
BEFORE UPDATE ON "cf" --<< note the BEFORE!
FOR EACH ROW
EXECUTE PROCEDURE test4()

Trigger to update table column when new row is insert in postgresql?

I want to write trigger to update table when new row is insert. I am using updatea Query like
UPDATE table SET
geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)
You need to use a BEFORE trigger and then assign the new value:
CREATE OR REPLACE FUNCTION function_update()
RETURNS trigger AS
$BODY$
BEGIN
new.geom := ST_SetSRID(ST_MakePoint(new.longitude, new.latitude), 4326);
RETURN new;
END;
$BODY$
LANGUAGE plpgsql;
This can only be done in a BEFORE trigger:
CREATE TRIGGER triggerinsert
BEFORE INSERT
ON rdpr
FOR EACH ROW
EXECUTE PROCEDURE function_update();
I Solved by writing trigger function like
CREATE OR REPLACE FUNCTION function_update()
RETURNS trigger AS
$BODY$
BEGIN
new.geom := ST_SetSRID(ST_MakePoint(new.longitude, new.latitude), 4326);
RETURN new;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION function_update()
OWNER TO postgres;
and i wrote trigger to update
CREATE TRIGGER triggerinsert
Before INSERT
ON rdpr
FOR EACH ROW
EXECUTE PROCEDURE function_update();

Postgresql Trigger example

I created a trigger when a row updated trigger will change that row. When I update a column I get an error. How can I solve?
ERROR: stack depth limit exceeded
HINT: Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.
CREATE OR REPLACE FUNCTION ynt_call()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'UPDATE' THEN
UPDATE ynt.a_test SET date_time = now();
END IF;
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_call AFTER UPDATE ON ynt.a_test FOR EACH ROW EXECUTE PROCEDURE ynt_call();
Of course you're looping until the end of time, because you are updating row when updating row. You should set the field date_time using the special variable new as following:
CREATE OR REPLACE FUNCTION ynt_call()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'UPDATE' THEN
new.date_time = now();
END IF;
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_call AFTER UPDATE ON ynt.a_test FOR EACH ROW EXECUTE PROCEDURE ynt_call();

How to create a trigger that checks values?

I need help with my trigger. For example i have Query like this
INSERT INTO test(id_users,id_type,id_color) VALUES(1,3,4);
in my data base i have table with name: test
ghost, id, id_users, id_type, id_color
and i need before insert or update to check
Create trigger
Begin
select id from test where ghost = false and id_users = 26 and id_type = 3
if NO execute INSERT
if YEST exit with no action
END
How can i creat this trigger ?
There are two ways, depending on how you want to manage the problem.
If you wish to silence it, use a before trigger and return null:
create function ignore_invalid_row() returns trigger as $$
begin
if not exists(
select 1
from test
where not ghost
and id_users = new.id_users
and id_type = new.id_type
)
then
return null;
end if;
return new;
end;
$$ language plpgsql;
create trigger ignore_invalid_row_tg before insert on test
for each row execute procedure ignore_invalid_row();
If you wish to raise it, use a constraint trigger and raise an exception:
create function reject_invalid_row() returns trigger as $$
begin
if not exists(
select 1
from test
where not ghost
and id_users = new.id_users
and id_type = new.id_type
)
then
raise exception 'message';
end if;
return null;
end;
$$ language plpgsql;
create constraint trigger reject_invalid_row_tg after insert on test
for each row execute procedure reject_invalid_row();
http://www.postgresql.org/docs/current/static/sql-createtrigger.html
First, you need to create a trigger function and then the trigger, based on it:
CREATE OR REPLACE FUNCTION my_trigger_function() RETURNS TRIGGER AS $BODY$ BEGIN
IF EXISTS (SELECT id FROM test WHERE ghost = false AND id_users = 26 AND id_type = 3) THEN
return NEW;
ELSE
return NULL;
END IF; END; $BODY$ LANGUAGE 'plpgsql';
Then, you create trigger based on this function:
CREATE TRIGGER t_my_trigger BEFORE INSERT ON test FOR EACH ROW EXECUTE PROCEDURE my_trigger_function();
For more on triggers, see postgres docs.