postgres trigger creation - postgresql

How do I only create a trigger if it does not exist?
When I do create or replace, I get a syntax error so I am looking for a way to test for the existence of a trigger.
I can always select * from pg_trigger, but I am sure there is a more suitable way.
Thanks

Postgres can conditionally drop a trigger - see the docs. Do this before creating the trigger, then it will always work.
DROP TRIGGER IF EXISTS mytrigger ON mytable;
As Jack points out in the comments, this feature has only been available since 8.2; this has been out for more than four years though, so it should be available in your version.

CREATE TRIGGER (NameOfTrigger) AFTER INSERT OR UPDATE ON (NameOfTable)
DROP TRIGGER IF EXISTS (NameOfTrigger) ON (NameOfTable);

Related

How do I create a temporary trigger in Postgres? [duplicate]

This question already has an answer here:
Drop trigger/function at end of session in PostgreSQL?
(1 answer)
Closed last month.
I'm trying to create a system in Postgres where each client can create its own subscriptions via listen + notify + triggers. I.e. the client will listen to a channel, and then create a trigger which runs notify on that channel when its conditions have been met. The issue is that I want Postgres to clean up properly in case of improper client termination (e.g. the remote client process dies). To be more specific, I want that trigger which is calling the notify to be removed as there is no longer a listener anyways. How can I accomplish this?
I've thought about having a table to map triggers to client ids and then using that to remove triggers where the client is gone, but it seems like a not so great solution.
I found an answer to this in another question: Drop trigger/function at end of session in PostgreSQL?
In reasonably recent Postgres versions you can create a function in
pg_temp schema:
create function pg_temp.get_true() returns boolean language sql as $$ select true; $$;
select pg_temp.get_true();
This is the schema in which temporary tables are created. All its
contents, including your function, will be deleted on end of session.
You can also create triggers using temporary functions on tables. I've
just tested this and it works as expected:
create function pg_temp.ignore_writes() returns trigger language plpgsql as $$
begin
return NULL;
end;
$$;
create table test (id int);
create trigger test_ignore_writes
before insert, update, delete on test
for each row execute procedure pg_temp.ignore_writes();
Because this trigger function always returns NULL and is before [event] it should make any writes to this table to be ignored. And
indeed:
insert into test values(1);
select count(*) from test;
count
-------
0
But after logout and login this function and the trigger would not be
present anymore, so writes would work:
insert into test values(1);
select count(*) from test;
count
-------
1
But you should be aware that this is somewhat hackish — not often used
and might not be very thoroughly tested.
That's not how it works. CREATE TRIGGER requires that you either own the table or have the TRIGGER privilege on it (which nobody in their right mind will give you, because it enables you to run arbitrary code in their name). Moreover, CREATE TRIGGER requires an SHARE ROW EXCLUSIVE lock on the table and DROP TRIGGER requires an ACCESS EXCLUSIVE lock, which can be disruptive.
Create a single trigger and keep that around.

Triggers in postgresql

I am new to PostgreSQL and I am currently working on triggers but am stuck at one point.
I have two tables Student and Room.
Room id is the primary key in Room and foreign key in Student.
If I am inserting in Student, then it should check in Room whether the new data exist or not.
This is a foreign key check constraint. I hope anyone can help me with it
I dont know your code and I dont get the meaning, but I can answer generally.
In PostgreSQL a trigger is normally defined in two steps:
Define a trigger function using the CREATE FUNCTION
Bind this created trigger function to a table using CREATE TRIGGER
A trigger function is a common function, except that is has a return value type trigger (in addition, it does not take any arguments).
CREATE OR REPLACE FUNCTION trigger_function()
RETURNS trigger
AS $$ ... $$;
binding:
CREATE TRIGGER trigger_name
AFTER INSERT ON table_name
FOR EACH ROW EXECUTE PROCEDURE trigger_function();
In addition, please also consult the excellent PG documentation at PostgreSQL 9.4 Triggers

TSQL For Triggers

Sorry but I'm struggling with the online documentation.
What exactly is the difference for a FOR trigger compared to a AFTER and INSTEAD OF?
Thanks
The FOR and the AFTER trigger is the same. You specify them in the same way and they do the same thing:
CREATE TRIGGER sometrigger on sometable FOR INSERT,UPDATE,DELETE
CREATE TRIGGER sometrigger on sometable AFTER INSERT,UPDATE,DELETE
The INSTEAD OF trigger is something completely different. When you specify a trigger with the instead of then the trigger will execute instead of the insert, update and delete. This can be useful when you have views. So you can control what should be delete, updated and inserted in the views underlying tables
Here is an link to explain the triggers in more depth

execute a trigger when I create a table

I would like to know if a trigger on a system table of PostgreSQL can be executed when I create a table
I need to add 2 functions on each table of my database and I would like to do it dynamically
Thanks
This can be done with an event trigger:
CREATE OR REPLACE FUNCTION on_create_table_func()
RETURNS event_trigger AS $$
BEGIN
-- your code here
END
$$
LANGUAGE plpgsql;
CREATE EVENT TRIGGER
on_create_table ON ddl_command_end
WHEN TAG IN ('CREATE TABLE')
EXECUTE PROCEDURE on_create_table_func();
Note that there is no way to directly execute any query on the newly created table, or even get its name.
I don't know what you mean by "add 2 functions on each table" since functions don't belong to a specific table, but if you need to perform an operation specific for the new tables, this might not be for you.
I know it's an old question but now it has been implemented in version 9.3, or at least partially
http://www.postgresql.org/docs/9.3/static/event-trigger-definition.html
You're looking for "DDL Triggers". They're not implemented in PostgreSQL. Neither you can add triggers to system tables. Look at this forum entry:
Adding ddl audit trigger

Learning about DB2 triggers

Hi I am trying to figure out the syntax for triggers. I have two tables one called tagged_in and the other notification. So I want to make a trigger where when an insert is called in tagged_in I want to insert a tuple in notification.
The manuals are available at the DB2 InfoCenter. Did you read the CREATE TRIGGER statement information yet? If not, why not? If so, what did you try, and what error did you get?
Something like this
CREATE TRIGGER TEST_TRIGGER
AFTER INSERT ON TAGGED_IN
REFERENCING NEW AS NEW_TAG
FOR EACH ROW
BEGIN ATOMIC
INSERT INTO NOTIFICATION
VALUES (NEW_TAG.FIELD1,NEW_TAG.FIELD2);
END