Update trigger not firing in plpgsql - sql

I have an update trigger and insert trigger on a table nums with two columns namely name,number.Whenever data is updated or inserted then these triggers are executed.
CREATE OR REPLACE FUNCTION add_log()
RETURNS trigger AS
$BODY$
DECLARE
account_type varchar;
BEGIN
IF (TG_OP = 'INSERT') THEN
INSERT INTO log
VALUES(
now(),
'inserted data is name : '||NEW.name||' num : '||NEW.number
);
RETURN NEW;
ELSEIF (TG_OP = 'update') THEN
INSERT INTO log VALUES
(
now(),
'updated record with old num :'||OLD.number||' with new num : '||NEW.number
);
RETURN OLD;
END IF;
RETURN null;
END;
$BODY$
LANGUAGE plpgsql
creating trigger:
CREATE TRIGGER add_log_trigger
AFTER INSERT OR UPDATE
ON nums
FOR EACH ROW
EXECUTE PROCEDURE add_log();
When an insert operation is performed, insert trigger is being fired.But when update operation is performed no update trigger is being fired.Why?

String comparison is case-sensitive in Postgres. Try TG_OP = 'UPDATE' instead of TG_OP = 'update'.

Related

PostgreSQL trigger not executing after update [duplicate]

Here is my trigger function
CREATE OR REPLACE FUNCTION test_table_insert()
RETURNS TRIGGER
AS $$
BEGIN
IF NEW.id IS NULL THEN
RAISE EXCEPTION 'id is null';
END IF;
UPDATE e_sub_agreement SET ro_id = NEW.id WHERE id = NEW.id;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER test_table_insert AFTER INSERT ON e_sub_agreement FOR EACH ROW EXECUTE PROCEDURE test_table_insert();
The problem is that it doesn't update table e_sub_agreement. I checked NEW value and everything is good. It returns with the new id. If I change where statement id = "some existing id in table", then it works. It changes ro_id to the new id. How is it possible? My guess is that data has not been inserted into table and trigger function can't find row with the given id. But it's not how trigger's after insert function works. What's the magic?
An AFTER trigger can not change anything. Running an additional UPDATE is also quite inefficient. Change this to a BEFORE trigger and assign the value you want:
CREATE OR REPLACE FUNCTION test_table_insert()
RETURNS TRIGGER
AS $$
BEGIN
IF NEW.id IS NULL THEN
RAISE EXCEPTION 'id is null';
END IF;
NEW.ro_id := NEW.id;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER test_table_insert
BEFORE INSERT ON e_sub_agreement
FOR EACH ROW EXECUTE PROCEDURE test_table_insert();
Note that the NOT NULL check is better done by defining the column as NOT NULL.

Updates/inserts not working in postgres trigger function

I have following function and trigger-
CREATE OR REPLACE FUNCTION function_copyX() RETURNS trigger AS
$BODY$
BEGIN
WITH
updates (id) AS (
UPDATE tbl_history
SET price=0
FROM tbl
WHERE tbl.id = tbl_history.id
AND tbl_history.price=1
RETURNING tbl_history.id
)
INSERT INTO tbl_history
SELECT id,1 FROM tbl;
RETURN new;
END;
$BODY$
language plpgsql;
create TRIGGER T_X
AFTER INSERT ON TBL
FOR EACH ROW
execute procedure function_copyX();
So, whenever I am inserting any record in TBL, I expect inserts/updates in the TBL_HISTORY but this does not work in trigger function.
But if I execute this 'WITH code' separately it works.
What could be the issue? please help.
Thanks
Below modification worked!
CREATE OR REPLACE FUNCTION function_copyX() RETURNS trigger AS
$BODY$
BEGIN
WITH
updates (id) AS (
UPDATE tbl_history
SET price=0 WHERE id =NEW.id AND price=1
RETURNING id
)
INSERT INTO tbl_history (id,price) values (NEW.id,1);
RETURN new;
END;
$BODY$
language plpgsql;
create TRIGGER T_X
AFTER INSERT ON TBL
FOR EACH ROW
execute procedure function_copyX();

Rollback trigger on insertion conflict

I had this :
CREATE FUNCTION upsert_user(u_name text, u_fullname text, u_email text, u_suffix text) RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
userid users.id_user%TYPE;
BEGIN
LOOP
-- first try to update
UPDATE users SET "fullname" = u_fullname, "email" = u_email, "suffix" = u_suffix WHERE "name" = u_name RETURNING "id_user" INTO userid;
-- check if the row is found
IF FOUND THEN
RETURN userid;
END IF;
-- not found so insert the row
BEGIN
INSERT INTO users ("name", "fullname", "email", "suffix") VALUES (u_name, u_fullname, u_email, u_suffix) RETURNING "id_user" INTO userid;
RETURN userid;
EXCEPTION WHEN unique_violation THEN
-- do nothing and loop
END;
END LOOP;
END;
$$;
CREATE TRIGGER link_entity
BEFORE INSERT
ON public.users
FOR EACH ROW
EXECUTE PROCEDURE public.link_entity();
CREATE FUNCTION link_entity() RETURNS trigger
LANGUAGE plpgsql
AS $$ DECLARE
entityid integer;
BEGIN
INSERT INTO privileges_entities (name) VALUES (NEW.name) RETURNING privileges_entities.id_entity INTO entityid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
NEW.ref_entity := entityid;
RETURN NEW;
END;
$$;
After updated postgresql to version 9.5, I modified the function upsert_user to use the new instruction ON CONFLICT:
CREATE FUNCTION upsert_user(u_name text, u_fullname text, u_email text, u_suffix text) RETURNS integer
LANGUAGE sql
AS $$
INSERT INTO users (name, fullname, email, suffix)
VALUES (u_name, u_fullname, u_email, u_suffix)
ON CONFLICT (name) DO UPDATE SET name=EXCLUDED.name, fullname=EXCLUDED.fullname, email=EXCLUDED.email, suffix=EXCLUDED.suffix
RETURNING id_user;
$$;
The problem is that, now, new rows are inserted in the privileges_entities table even if insertion into the users table fails.
Is it possible to rollback the trigger if the insertion of the user leads to a conflict?
This is indeed a side-effect of using the new ON CONFLICT clause.
My solution here would be to add a check into the link_entity() function itself and prevent it from continuing if the user already exists. Like this:
CREATE FUNCTION link_entity() RETURNS trigger
LANGUAGE plpgsql
AS $$ DECLARE
entityid integer;
nameExists boolean;
BEGIN
EXECUTE format('SELECT EXISTS(SELECT 1 FROM %I.%I WHERE name = NEW.name)', TG_TABLE_SCHEMA, TG_TABLE_NAME) INTO nameExists;
IF nameExists THEN
RETURN NEW; -- just return, entity already linked
END IF;
INSERT INTO privileges_entities (name) VALUES (NEW.name) RETURNING privileges_entities.id_entity INTO entityid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
NEW.ref_entity := entityid;
RETURN NEW;
END;
$$;

create a trigger before insert

I want to create a trigger before insert and check inside the trigger function the summary_id values of the new row to be inserted, to see if the summary_id already exists in the table. if it exists then the trigger should return null as I don't want to insert duplicate values. I have written this function, but, when I tried to add a new duplicate row, it was inserted successfully!
CREATE OR REPLACE FUNCTION trigger_ack_bi()
RETURNS trigger AS $$
--DECLARE
-- max_points INTEGER;
BEGIN
IF(TG_OP = 'INSERT') THEN
IF NEW.ack_summary_id = (SELECT ack_summary_id FROM scm_main.tbl_ack WHERE scm_main.tbl_ack.ack_summary_id = NEW.ack_summary_id LIMIT 1) THEN
RETURN NULL;
ELSE
RETURN NEW;
END IF;
END IF;
END;
$$LANGUAGE plpgsql;
CREATE TRIGGER trigger_ack_bi BEFORE INSERT ON
scm_main.tbl_ack
FOR EACH ROW
EXECUTE PROCEDURE trigger_ack_bi()
ALTER FUNCTION trigger_ack_bi() OWNER TO postgres;
I'd recommend setting column to be NOT NULL and creating a PK on it...
alter table scm_main.tbl_ack alter column ack_summary_id set NOT NULL;
alter table scm_main.tbl_ack add primary key (ack_summary_id);
it seems the trigger function is correct. But, it is running in silent mode. raising an exception was what I needed to make sure that duplication had been prevented. Thank you for your other solutions.
CREATE OR REPLACE FUNCTION trigger_ack_bi()
RETURNS trigger AS $$
--DECLARE
-- max_points INTEGER;
BEGIN
IF(TG_OP = 'INSERT') THEN
IF NEW.ack_summary_id IS NULL THEN
RAISE EXCEPTION 'summaryID cannot be null';
END IF;
IF NEW.ack_summary_id = (SELECT ack_summary_id FROM scm_main.tbl_ack WHERE scm_main.tbl_ack.ack_summary_id = NEW.ack_summary_id LIMIT 1) THEN
RAISE EXCEPTION 'duplication prevented..';
RETURN NULL;
ELSE
RETURN NEW;
END IF;
END IF;
END;
$$LANGUAGE plpgsql;
CREATE TRIGGER trigger_ack_bi BEFORE INSERT ON
scm_main.tbl_ack
FOR EACH ROW
EXECUTE PROCEDURE trigger_ack_bi()

Postgresql trigger row not affected

I have try to make a trigger on a table call product. After inserting a product i get an error, that "NEW" variable is not affected yet.
Here is the trigger and user-defind function :
CREATE OR REPLACE FUNCTION updateProduitQteInitiale() RETURNS TRIGGER AS $example_table$
BEGIN
IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
UPDATE produit set qtestock= NEW.qteinitial where produit.pid = NEW.pid ;
return NEW ;
END IF;
RETURN NULL;
END;
$example_table$ LANGUAGE plpgsql;
And here is the trigger:
CREATE TRIGGER qteInitialTrigger AFTER INSERT OR UPDATE ON produit
FOR EACH ROW EXECUTE PROCEDURE updateProduitQteInitiale();
Is pid your primary id? Not sure what you want to do but I assume you want to set to the column qtestock value from column qteinitial (on the same row!). If I guessed it right then you can do this:
CREATE OR REPLACE FUNCTION updateProduitQteInitiale() RETURNS TRIGGER AS $example_table$
BEGIN
NEW.qtestock = NEW.qteinitial;
return NEW;
END;
$example_table$ LANGUAGE plpgsql;
CREATE TRIGGER qteInitialTrigger BEFORE INSERT OR UPDATE ON produit
FOR EACH ROW EXECUTE PROCEDURE updateProduitQteInitiale();