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

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.

Related

Syntax error while creating a trigger in PostgreSQL

I created a table name Student in PostgreSQL and then I tried defining a trigger on that table but it's showing an error message in doing so.
Trigger Syntax:
CREATE TRIGGER bi_Student BEFORE INSERT ON Student as $$
FOR EACH ROW
BEGIN
raise notice 'Successfully inserted into table(%)', user;
end $$;
Table Creation Command:
create table Student(Stu_id int, Stu_Name text, Stu_Age int, Stu_address char(30));
Actually I tried to declare the execution statements directly inside the trigger only rather than calling any procedure/ function from the trigger which is working fine but I want to do in this way in PostgreSQL.
PostgreSQL doesn't support it. You need trigger function always.
As documented in the manual you need a trigger function
create function my_trigger_function()
returns trigger
as
$$
begin
raise notice 'Successfully inserted into table(%)', user;
return new; --<< important (see the manual for details)
end
$$
language plpgsql;
Not sure what you intend with the user parameter there, as that is not the table name, but the current database user. If you want to display the actual table name, you need to use TG_RELNAME instead - which is an implicit variable available in the trigger function.
And a trigger definition
CREATE TRIGGER bi_Student
BEFORE INSERT ON Student
FOR EACH ROW
execute function my_trigger_function();

Generic function to be called from multiple triggers

I have created a function and trigger to make use that the userId is uCased. The code is as followsL
CREATE OR REPLACE FUNCTION stdUserId ()
RETURNS trigger AS $stdUserId$
BEGIN
NEW.login_id = UPPER (TRIM (NEW.login_id));
RETURN NEW;
END;
$stdUserId$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS usersStdUserId ON users;
CREATE TRIGGER usersStdUserId BEFORE INSERT OR UPDATE ON users
FOR EACH ROW EXECUTE PROCEDURE stdUserId (login_id);
Now I would like to alter the function to be usable with other triggers as in:
DROP TRIGGER IF EXISTS user_rolesStdUserId ON user_roles;
CREATE TRIGGER user_rolesStdUserId BEFORE INSERT OR UPDATE ON users
FOR EACH ROW EXECUTE PROCEDURE stdUserId (user_id);
I would like a generic approach as I have a number of tables to apply this function to. I would prefer to stay away from "IF table01 .. . ELSEIF table02 .. ."
Your assistance is appreciated.

Trigger, initcap a value before a insert (PostgreSQL)

I know that there is an initcap () function, which will perform a transformation for a 'string'. But I want to know how I can perform a trigger that executes this function before inserting a value in the column name into my table client. In other words how my function 'func ()' needs to be declared.
CREATE TRIGGER trigg
BEFORE INSERT
ON client
FOR EACH ROW
EXECUTE PROCEDURE func()
CREATE FUNCTION func()
returns trigger as
$BODY$
begin
**???**
end;
$BODY$ language plpgsql;
You can access the record before the change that triggered the trigger via the OLD pseudo record and after the change via the NEW pseudo record in your trigger function. You can also change the latter.
So to change the name of your client use
NEW.name := initcap(NEW.name);
RETURN NEW;
in your trigger function body.
For more information see 42.9. Trigger Procedures.

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

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?

Postgresql Triggers: switch from after to before the trigger dont fire

i have this simple plpgsql function:
CREATE FUNCTION "update_times" () RETURNS trigger AS '
BEGIN
NEW.update_time = NOW();
RETURN NEW;
END;'
LANGUAGE "plpgsql";
--The trigger:
CREATE TRIGGER test_update_time BEFORE UPDATE ON contact FOR EACH ROW EXECUTE PROCEDURE update_times();
and this works well, but only with BEFORE triggers...
I prefern to fire the trigger after the update, so i changed the function and the trigger itself as:
CREATE FUNCTION "update_times_after" () RETURNS trigger AS '
BEGIN
OLD.update_time = NOW();
RETURN OLD;
END;'
LANGUAGE "plpgsql";
--The trigger:
CREATE TRIGGER test_update_time_after AFTER UPDATE ON contact FOR EACH ROW EXECUTE PROCEDURE update_times_after();
But the AFTER trigger dont work (the trigger dont fire or the function fail).
What i am doing wrong?
After trigger is run when the data is already saved to table. So no modification (especially on OLD.!) doesn't make any sense.
If you want to change the data that is save to disk, you have to do it BEFORE it is saved.