PL/PgSQL creating a conditional trigger on insert - sql

I'm working on my first ever Trigger. When I'm doing an INSERT on table I want to conditionaly remove rows from other table.
Here is a trigger:
CREATE OR REPLACE FUNCTION clear_seen_by()
RETURNS trigger AS
$BODY$
BEGIN
IF (OLD.popup = '1') THEN
DELETE FROM news_seen_by;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Invoked by:
CREATE TRIGGER clear_seen_by
AFTER INSERT
ON news
FOR EACH STATEMENT
EXECUTE PROCEDURE clear_seen_by();
As an error I see that NEW or OLD (if I motify the trigger) is not declared/unknown. Where is the problem?

In an INSERT statement you do not have an OLD record defined.
You should use NEW.popup instead, and also declare the trigger to be FOR EACH ROW.
CREATE OR REPLACE FUNCTION clear_seen_by() RETURNS trigger AS
$BODY$
BEGIN
IF (NEW.popup = '1') THEN
DELETE FROM news_seen_by;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
CREATE TRIGGER
clear_seen_by
AFTER INSERT ON
news
FOR EACH ROW EXECUTE PROCEDURE
clear_seen_by();

You declare a trigger FOR EACH STATEMENT. Maybe you need FOR EACH ROW?
FOR EACH STATEMENT triggers do not have NEW and OLD.

Related

String modification trigger is not working

I am trying to write a trigger that executes after the INSERT completes on only newly inserted rows (I do not need to execute on old rows). The trigger should modify inserted string using split_part and replace functions.
The queries are as follows:
DROP TRIGGER IF EXISTS date_extraction ON t32upb
drop function if exists date_extraction();
CREATE FUNCTION date_extraction() RETURNS trigger AS $$
begin
NEW.filename := replace(split_part(old.filename,'_', 2), '.tif', '');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER
date_extraction
AFTER INSERT on
sentinel_2.t32upb
FOR EACH STATEMENT EXECUTE PROCEDURE
date_extraction();
What is wrong with the query? it is not working

Trigger error in postgreSQL

I am attempting to create a trigger which will delete rows when a certain where clause criteria is met but it throws out an error. What am I doing wrong?
CREATE TRIGGER unknowns
AFTER INSERT
ON "Amazon".salesdatapcr
FOR EACH ROW
EXECUTE PROCEDURE delete_my_rows();
CREATE OR REPLACE FUNCTION delete_my_rows()
RETURNS trigger AS
$BODY$
BEGIN
DELETE FROM "Amazon".salesdatapcr WHERE "Builder" = 'unknown';
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
You must encapsulate your DELETE statement in a function, so the trigger will become:
CREATE TRIGGER unknowns
AFTER INSERT
ON "Amazon".salesdatapcr
FOR EACH ROW
EXECUTE PROCEDURE delete_my_rows();
So you must create a function as follow:
CREATE OR REPLACE FUNCTION delete_my_rows()
RETURNS trigger AS
$BODY$
BEGIN
DELETE FROM "Amazon".salesdatapcr WHERE "Builder" = 'unknown';
RETURN NEW;
END;
HERE you can find a brief guide on trigger creation

How to send current schema and affected table with notification function/trigger?

When a row is inserted or updated in a specific table (in this example it's the table called 'fpl'). How can I include the affected table and schema in the notification?
SQL as follows:
CREATE TRIGGER fpl_event
AFTER INSERT OR UPDATE ON fpl
FOR EACH ROW
EXECUTE PROCEDURE fpl_notify();
CREATE OR REPLACE FUNCTION fpl_notify()
RETURNS trigger AS $$
BEGIN
NOTIFY dbNotification, 'something got insereted in fpl!';
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;
Update:
CREATE OR REPLACE FUNCTION fpl_notify() RETURNS trigger
AS
$$
BEGIN
EXECUTE format('notify dbNotification, ''%s''', TG_TABLE_SCHEMA);
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;
read trigger special variables

Trigger for update timestamp field

I'm trying to create trigger, that updates my lastmodified field on insert/update. Here's my trigger:
CREATE OR REPLACE TRIGGER timestamp_trigger BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE PROCEDURE timestamp_update;
and my procedure:
CREATE OR REPLACE FUNCTION timestamp_update()
RETURNS TRIGGER
AS
$$
BEGIN
UPDATE orders
SET lastmodified = getdate()
WHERE orderid = new.orderid
RETURN new;
END;
$$ language plpgsql;
Unfortunately when I try to update some field, field isn't updated. Also when i create row, the lastmodified field isn't updated.
Do not use update in a trigger like this because it may lead to endless recursive loop.
If you want to modify inserted/updated row, just change the new record:
CREATE OR REPLACE FUNCTION timestamp_update()
RETURNS TRIGGER
AS
$$
BEGIN
NEW.lastmodified = now();
RETURN new;
END;
$$ language plpgsql;
The syntax create or replace trigger is incorrect.
CREATE TRIGGER timestamp_trigger
BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE PROCEDURE timestamp_update();

Trigger Not Executing Yet It's Created

I have a trigger function I'm trying to have execute in Postgres.
It compiles and adds the trigger, however it does not insert the value into the table as I had hoped.
The function it uses looks like this:
CREATE OR REPLACE FUNCTION
calc_gnpDifference(n integer, o integer)
RETURNS NUMERIC AS $$
SELECT $1 ::numeric - $2::numeric AS gnpDifference;
$$ LANGUAGE SQL;
And the Trigger part:
CREATE OR REPLACE FUNCTION autoCalculate() RETURNS TRIGGER AS $$
BEGIN
IF NEW.gnp_old > NEW.gnp_old THEN
NEW.gnpDifference := calc_gnpDifference(NEW.gnp_old, NEW.gnp);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER insertDifference ON country;
CREATE TRIGGER insertDifference BEFORE INSERT ON country
FOR EACH ROW EXECUTE PROCEDURE autoCalculate();
However, when I insert data, the trigger does not update the gnpDifference field as I had hoped. Thoughts on why this might be happening?
Obviously this condition: IF NEW.gnp_old > NEW.gnp_old will never be true so the trigger will never have any effect.