A trigger that detects that an UPDATE wouldn't change a row - postgresql

I wrote the following trigger:
CREATE FUNCTION trig_func() RETURNS trigger AS $$
BEGIN
IF NEW = OLD
THEN -- update would do nothing, doing something...
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trig BEFORE UPDATE ON some_table
FOR EACH ROW EXECUTE PROCEDURE trig_func();
It makes it clear what I'd like to achieve, but what is the proper thing to put in place of NEW = OLD?

The is distinct from operator can compare complete rows and will handle nulls correctly.
So you want
if new is not distinct from old then
...
end if;

Related

I Want to stop an update for one column in relation using trigger function.But instead of not updating,code the code is updating the records

I have written for preventing a update using trigger function in pgsql.
Here lamtapproved is the column for which i want to stop an update
create or replace function Prevent_update() returns trigger as $$
begin
if new.lamtapproved=old.lamtapproved
then raise exception 'Update is not allowed';
end if;
return new;
end;
$$ language 'plpgsql'
Using the above trigger function i want to prevent the updates in the

PostgreSql Trigger does not work when called 'insert'

The code below is my code to create trigger function to change column "pass".
create or replace function change_pass()
returns trigger as
$$
begin
NEW.pass := 'XXXXXXXXX';
return NEW;
end
$$
language plpgsql;
create trigger change_pass
AFTER insert or update on "D_ACCOUNT"
for each row execute procedure change_pass();
When i called insert, i did not see any changes in my data.
Can anyone explain to me where i was wrong?
You need a BEFORE trigger to change values in the NEW record:
create trigger change_pass
BEFORE insert or update on "D_ACCOUNT"
for each row execute procedure change_pass();

PL/pgSQL: Update a table using a record

I have an insert trigger in a PostgreSQL 9.5 database which copies the row being inserted to another table:
CREATE OR REPLACE FUNCTION data_record_insert () RETURNS TRIGGER AS $x$
BEGIN
EXECUTE 'INSERT INTO data_record_backup VALUES ($1.*)' USING new;
RETURN new;
END;
$x$ LANGUAGE PLPGSQL;
However, I want to be able to use Postgres' INSERT ... ON CONFLICT upsert statement to insert rows into these tables but can't figure out how to write the trigger. It'd be something like this:
CREATE OR REPLACE FUNCTION data_record_insert () RETURNS TRIGGER AS $x$
BEGIN
EXECUTE 'INSERT INTO data_record_backup VALUES ($1.*)
ON CONFLICT (pk) DO UPDATE SET column_names($1) = $1.*' USING new;
RETURN new;
END;
$x$ LANGUAGE PLPGSQL;
But obviously that SET column_names($1) = $1.* is wrong, it's just something I made up. Is there some way to achieve this?

Trigger update another table

I have been trying to write a trigger function that updates the rows in the child table when the parent is changed for a while now.
I have read Trigger procedure documentation but i have not really grasped how to build the functions.
This is what I have tried that does not work...
CREATE FUNCTION myschema.update_child() RETURNS trigger AS
$BODY$
BEGIN
UPDATE myschema.child
set new.number = parent.number
FROM myschema.parent
WHERE id = "id";
RETURN NEW;
END
$BODY$
LANGUAGE plpgsql
Then the trigger
CREATE TRIGGER update_child_after_update
AFTER INSERT OR UPDATE OR DELETE
ON myschema.child
FOR EACH ROW
EXECUTE PROCEDURE myschema.update_child();
Does anyone have some tips to give?
Best regards
You don't need to use the parent table in the body of the trigger function, because the values from the parent table are available in the function in the special variable OLD and NEW. In this case you only need NEW.
If you need the trigger only on update, than define a update-only trigger:
CREATE or replace FUNCTION update_child() RETURNS trigger AS
$BODY$
BEGIN
UPDATE child
set number = NEW.number
WHERE id = NEW.id;
RETURN NEW;
END
$BODY$
LANGUAGE plpgsql;
CREATE TRIGGER update_child_after_update
AFTER UPDATE
ON parent
FOR EACH ROW
EXECUTE PROCEDURE update_child();

Postgres - update statement as a trigger

I've been playing around for the last hour or more trying to put an update statement into a trigger. I understand the concept of an UPDATE statement and the below works just fine
UPDATE cars SET country = 'France';
What I want is to put this into a trigger so that when the cars table is updated, the column country will automatically be updated with France.
I've played around with adapting Functions and Triggers that I've found out on the interweb but I'm obviously making the statement wrong as either they don't execute or they execute but don't update the country field when a new record is added.
CREATE FUNCTION update_country () RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'UPDATE') THEN
UPDATE cars SET country = 'France' WHERE id = New.id;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql; --The trigger used to update a table.
CREATE TRIGGER update_country_col BEFORE UPDATE ON cars FOR EACH ROW EXECUTE PROCEDURE update_country();
The above scripts executes but does not add France to the country column.
The function was adapted from a statement that I found out on the web.
Postgres 9.1.
I know that the answer is going to be so simple!
In update triggers you should modify NEW record.
Also, you may need to return NEW record from procedure.
So, you should use following procedure instead of yours:
CREATE FUNCTION update_country () RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'UPDATE') THEN
NEW.country = 'France';
END IF;
RETURN NEW;
END; $$ LANGUAGE plpgsql;