How I can "atomically" replace function that is executed by trigger? - postgresql

Here is code I have used to create FUNCTION and TRIGGER.
CREATE OR REPLACE FUNCTION inc_post_loves() RETURNS TRIGGER AS
$BODY$
BEGIN;
...
END;
$BODY$
language plpgsql;
CREATE TRIGGER on_post_love_create
AFTER INSERT ON "PostLoves"
FOR EACH ROW EXECUTE PROCEDURE inc_post_loves()
Now I would like to modify FUNCTION inc_post_loves().
If I will execute CREATE OR REPLACE FUNCTION inc_post_loves()... again, do I have guarantee that previously created trigger won't be deleted (like cascade) and it wont fail during replacing function?

Related

How to return a JSON in a trigger function in PostgreSQL?

I need that when inserting data into a table, a trigger function returns a JSON. But, from what I saw, a trigger function can only return a trigger. What alternatives are there for what I want to do?
-- Trigger Function
CREATE OR REPLACE FUNCTION fn_save_gps_data()
RETURNS json
LANGUAGE 'plpgsql'
AS
$$
DECLARE "gpsData" json;
BEGIN
-- PERFORM fn_return_gps_data();
SELECT json_agg(row_to_json(gps)) FROM tbl_admon_gps_data gps INTO "gpsData";
RETURN "gpsData";
END;
$$;
------------------------------------------------------------------------------
-- Trigger that executes the previous Function
CREATE TRIGGER trg_save_gps_data AFTER INSERT ON tbl_admon_gps_data
FOR EACH ROW
EXECUTE PROCEDURE fn_save_gps_data();
The value returned by an AFTER trigger is ignored. The trigger is only useful for its side effects.

PostgreSQL drop function executes given function and doesn't remove trigger function

I created the trigger function with the below code:
CREATE OR REPLACE FUNCTION snitch() RETURNS event_trigger AS $$
BEGIN
select pgr_createTopology('public.roads_noded', 0.001);
END;
$$ LANGUAGE plpgsql;
CREATE EVENT TRIGGER snitch ON ddl_command_start EXECUTE FUNCTION snitch();
It is located in the public schema. Now I want to drop this function but when I try to drop it executes the given function and the server hangs. After restarting the server, the same thing happens. I can't create or drop any table. Is there a way to remove this trigger function?
The DROP FUNCTION command is itself a DDL command so the trigger will fire. You should first remove the trigger using DROP EVENT TRIGGER, then the function.

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();

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();

SQL Functions cannot return type trigger

I'm using PostgreSQL with pgAdmin and I can't get a trigger function to work. However, as far as I am aware, you can return type trigger in PostgreSQL?
CREATE OR REPLACE FUNCTION validate_Cat()
RETURNS TRIGGER AS
$BODY$
BEGIN
-- CODE here
END;
$BODY$
LANGUAGE SQL;
CREATE TRIGGER validate_Cat
AFTER INSERT OR UPDATE ON Category
FOR EACH ROW execute procedure validate_Cat();
SOLVED, had to change language to PLPGSQL