Syntax error while creating a trigger in PostgreSQL - 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();

Related

Pass the query result to the function

I created a function that takes as a parameter a string by which i am looking for the desired element in the Bus table. After that i create a trigger that will fire after inserting into the Maintenance table. Here i have a problem: i specify that when changing the table, call the function and pass the last added element there, but the trigger is not created.
I looked for similar questions and saw that you need to take the query in brackets, but it did not help.
Ask for your help!
Function:
create function set_status(model_ varchar(50)) returns void as $$
update Bus set technical_condition = 'don`t work' where model = model_;
$$ LANGUAGE sql;
Trigger:
create trigger check_insert
after insert on Maintenance
for each row
execute procedure set_status((select model from Maintenance order by id_m desc limit 1));
First off your trigger function must be of the form:
create or replace function <function_name>()
returns trigger
language plpgsql
as $$
begin
...
end;
$$;
The language specification may come either before the code or after it. Moreover it must be defined returning trigger and as taking no parameters. See documentation.
You can achieve what you want by moving the select status ... query into the trigger function itself.
create or replace function set_status()
returns trigger
language plpgsql
as $$
begin
update bus
set technical_condition =
(select model
from maintenance
order by id_m desc
limit 1
) ;
return null;
end;
$$;
create trigger check_insert
after insert on maintenance
for each row
execute procedure set_status();
NOTE: Not Tested.

Function that is called inside the trigger is not recognized

I am trying to create my first trigger in postgres but I keep receiving this: [42883] ERROR: function clear_article_flag does not exist.
What I am trying to do is: when a new row is inserted on articles with a non 'automatic' author to set is_automatic flag to false for the specific id.
CREATE OR REPLACE FUNCTION clear_article_flag()
RETURNS trigger
LANGUAGE plpgsql AS
$$
BEGIN
update flags
set is_automatic = false where id= new.id;
return NEW;
END ;
$$;
CREATE TRIGGER maintain_dummy_flag
AFTER INSERT
ON articles
FOR EACH ROW
WHEN ( new.author not in ('automatic') )
EXECUTE PROCEDURE clear_article_flag();```
My mistake, being the first time when I've created a trigger and a sql function, I didn't know that first I should run the part that creates the function and then the trigger.

Postgres triggers and producers (column "new" of relation does not exist)

I am trying to create a trigger and procedure to update a last_changed_timestamp column upon UPDATE and INSERT.
I can register the function and trigger just fine, but when I try to update a record I receive the error.
CREATE OR REPLACE FUNCTION update_my_table_last_changed_timestamp()
RETURNS trigger AS
$BODY$
BEGIN
UPDATE my_table SET NEW.last_changed_timestamp = NOW();
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_update_my_table_last_changed_timestamp
BEFORE UPDATE
ON my_table
FOR EACH ROW
EXECUTE PROCEDURE update_my_table_last_changed_timestamp();
column "new" of relation "my_table" does not exist
I also do not fully understand how update_my_table_last_changed_timestamp knows which row it's suppose to update, nor if there were parameters passed to it, how the I would get those variables from the trigger to the procedure.
Modify the NEW record, there is no need to update.
BEGIN
NEW.last_changed_timestamp = NOW();
RETURN NEW;
END;
Read in the documentation: Overview of Trigger Behavior
If you still want to access a (other )table in the update trigger.
You can add to beginning of your trigger body the following:
EXECUTE format('SET search_path TO %I', TG_TABLE_SCHEMA);
For some reason with the update trigger it can happen that you're not on the correct search_path (i believe some old psql version have this)

Event trigger to change owner on CREATE

I'm trying to create a PostgreSQL event trigger, that fires whenever a user creates anything (table, view, function, sequence...) and sets the owner of this newly created thing to a certain role.
So far I have the even trigger itself, that fires on any CREATE command:
CREATE EVENT TRIGGER setOwnerToMyRole ON ddl_command_end
WHEN tg_tag LIKE 'CREATE%'
EXECUTE PROCEDURE setOwnerToMyRole();
But I am having problems with the function itself:
CREATE FUNCTION setOwnerToMyRole() RETURNS event_trigger LANGUAGE plpgsql
AS $$ BEGIN
ALTER <type> <name> OWNER TO myRole
END; $$;
How do I get the type (as in table, view, etc.) and how do i get the name of the newly created thing?
edit:
Looking at this question and this question and of course CREATE EVENT TRIGGER and Trigger Procedures, this is currently not really possible :(
This works pretty well for me, although it's a little broader net than needed. It is based on code at https://blog.hagander.net/setting-owner-at-create-table-237/
CREATE OR REPLACE FUNCTION trg_create_set_owner()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
obj record;
BEGIN
-- postgresql 9.5 or later:
-- FOR obj in SELECT table_name FROM pg_event_trigger_ddl_commands() WHERE command_tag='CREATE TABLE'
FOR obj IN SELECT tablename FROM pg_tables WHERE tableowner = current_user LOOP
EXECUTE format('ALTER TABLE %s OWNER TO my_group', obj.tablename);
END LOOP;
END;
$$;
CREATE EVENT TRIGGER trg_create_set_owner
ON ddl_command_end
WHEN tag IN ('CREATE TABLE', 'CREATE TABLE AS')
EXECUTE PROCEDURE trg_create_set_owner();
Guess, I should answer this question properly and not just in an edit:
What I want is currently not possible. Perhaps a future update to Postgres will add more functionality to eventtriggers.
Sources:
http://www.postgresql.org/docs/current/static/sql-createeventtrigger.html
http://www.postgresql.org/docs/current/static/plpgsql-trigger.html
How to get the name of the altered table in a Postgres event trigger?
How to get SQL text from Postgres event trigger

How do I trigger or call a function on INSERT to one table based on result of another table?

I know how to do this using SQL Server or Sybase, but can't seem to get it right using Postgres 9.4. I have a FUNCTION that is working as intended:
CREATE OR REPLACE FUNCTION "public"."welcome"(u_id INT DEFAULT 1234)
RETURNS "void"
AS $$
INSERT INTO my_table_1 (title,body,category,user_id,created_at)
VALUES ('Welcome!', '<some text>','general',u_id,now())
$$ LANGUAGE sql
This FUNCTION works as expected when called as SELECT welcome(1234);
However, what I'm trying to do is call or trigger this FUNCTION based on the condition AFTER a new user gets inserted into another table and that user is the first and only user:
INSERT INTO my_table_2 (user_id,name,...) VALUES (5678,'John Doe',...);
and the following condition is met:
SELECT COUNT(*) from my_table_2 WHERE <my conditions are met>;
returns exactly and only 1
So, symantically, I'm trying to accomplish:
call TRIGGER welcome(1234) AFTER INSERT into my_table_2 where my conditions are met
I've seen some examples, and don't quite understand the CREATE FUNCTION x() AS TRIGGER syntax, and it seems that Postgres is steering me in this direction, but examples are lacking in my case. Help! (and thanks in advance!)
In the SQL standard, you define a trigger that fires a trigger function when a certain action is taking place. In your case you want to create an AFTER INSERT trigger whose trigger function calls your "welcome" function.
First the trigger function:
CREATE FUNCTION call_welcome() RETURNS trigger AS $$
DECLARE
cnt integer;
BEGIN
SELECT count(*) INTO cnt FROM my_table_2 WHERE ...;
IF cnt = 1 THEN
PERFORM welcome(NEW.user_id);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
And the trigger:
CREATE TRIGGER tr_call_welcome
AFTER INSERT ON my_table_2
FOR EACH ROW EXECUTE PROCEDURE call_welcome();