CREATE OR REPLACE FUNCTION ddl_command_test()
RETURNS event_trigger
AS $$
DECLARE
obj record;
BEGIN
FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands()
LOOP
RAISE NOTICE '% commnad on oid: %',
tg_tag,
obj.objid;
RAISE NOTICE 'triggered';
END LOOP;
END; $$ LANGUAGE plpgsql;
CREATE EVENT TRIGGER test_ddl ON ddl_command_end
EXECUTE FUNCTION ddl_command_test();
While pg_event_trigger_ddl_commands() function returns info for table creation:
CREATE TABLE test_table(
col int
);
It not prints notification message when table is dropped:
DROP TABLE test_table;
Don't get why, because event-trigger-matrix shows that ddl_command_end includes DROP TABLE command also.
Although the documentation event-trigger-matrix says that ddl_command_end can be used for DROP statements, I also struggled with that issue in the past.
Thereby, I found this workaround, which involves creating a specific function that fetches FROM pg_event_trigger_dropped_objects(), to notify when the DROP statement is used.
CREATE OR REPLACE FUNCTION ddl_drop_command_test()
RETURNS event_trigger
AS $$
DECLARE
obj record;
BEGIN
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
LOOP
RAISE NOTICE '% commnad on oid: %',
tg_tag,
obj.objid;
RAISE NOTICE 'triggered';
END LOOP;
END; $$ LANGUAGE plpgsql;
Further, you need to use ON sql_drop to create your event trigger. The WHEN TAG can be incremented with DROP SCHEMA and other Postgres objects.
CREATE EVENT TRIGGER drop_test ON sql_drop
WHEN TAG IN ('DROP TABLE')
EXECUTE PROCEDURE ddl_drop_command_test();
Related
I have a table in PostgretSQL. I want to build a trigger that avoids to update colb1 column. This column has five alternatives: Rea, Can, Loa, Mul, Alm. So the trigger doesn't let upload values from Rea to Can.
I've built this function but this is fail.
CREATE OR REPLACE FUNCTION example_trigger()
RETURNS trigger AS
$BODY$
BEGIN
new.colb1.tabl1 = 'Rea' := old.colb1.tabl1 = 'Can';
new.colb1.tabl1 = 'Can' := old.colb1.tabl1 = 'Rea';
RETURN new;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
DROP TRIGGER IF EXISTS trigger_name ON table1;
CREATE TRIGGER trigger_name BEFORE UPDATE ON table1
FOR EACH ROW EXECUTE PROCEDURE example_trigger();
I edited my post with changes:
CREATE OR REPLACE FUNCTION example_trigger()
RETURNS TRIGGER AS
$$
BEGIN
NEW.colb1 := OLD.colb1;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER not_changes
BEFORE UPDATE
ON tabl1
FOR EACH ROW
EXECUTE PROCEDURE example_trigger();
It's not exactly that I want because I want that the values Loa, Mul and Alm can be update.
A slight modification of your function to stop the modification of colb1 from Rea or Can to Can or Rea.
CREATE OR REPLACE FUNCTION example_trigger()
RETURNS TRIGGER AS
$$
BEGIN
IF OLD.colb1 in ('Rea', Can') AND NEW.colb1 IN ('Rea', 'Can') THEN
NEW.colb1 := OLD.colb1;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
This will not stop a user from using an intermediate UPDATE to change to one of Loa/Mul/Alm and then to either Rea or Can
I want to write SQL trigger in postgresql.
create trigger add_car on samochod
after insert
as
begin
declare k_inserted cursor
for select car_model, mileage from inserted
declare #car_model(30), #mileage varchar(30)
open k_inserted
fetch next from k_inserted into #car_model(30), #mileage varchar(30)
while ##FETCH_STATUS = 0
begin
print 'Added car: '
print 'Model: ' + #car_model
print 'Mileage: ' + #mileage
fetch next from k_inserted into #car_model(30), #mileage varchar(30)
end
close k_inserted
deallocate k_inserted
end
INSERT INTO car(car_model, mileage)
VALUES ('Audi', 15011646);
That trigger actually works for me. Then i tried in postgresql.
create function add_car()
RETURNS TRIGGER
as
$$
declare
car_model varchar(30);
mileage varchar(30);
k_inserted cursor for select new.car_model, new.mileage from car;
begin
open k_inserted;
fetch next from k_inserted into car_model, mileage;
while (FOUND)
LOOP
RAISE NOTICE 'Added car: ';
RAISE NOTICE 'Marka: %', car_model;
RAISE NOTICE 'Mileage: %', mileage;
fetch next from k_inserted into car_model, mileage;
end LOOP;
close k_inserted;
return new;
end;
$$ LANGUAGE plpgsql;
CREATE TRIGGER add_car
after INSERT on car
for each row EXECUTE PROCEDURE add_car();
It works but it prints me 6 times the same thing (i have 6 cars in my table)
How can I fix it so that it prints as many times as the cars I added
You declared your trigger as for each row so there is no need for a loop or a select "from inserted" (something that SQL Server doesn't have). The trigger will be called once for each row you insert, so just print the values from the new record
create function add_car()
RETURNS TRIGGER
as
$$
begin
RAISE NOTICE 'Added car: ';
RAISE NOTICE 'Marka: %', new.car_model;
RAISE NOTICE 'Mileage: %', new.mileage;
return new;
end;
$$ LANGUAGE plpgsql;
If you do want a statement level trigger to mimic SQL Server's behaviour, you can do the following:
A trigger function using the inserted transition table:
create function add_car()
RETURNS TRIGGER
as
$$
declare
l_row record;
begin
for l_row in select * from inserted
LOOP
RAISE NOTICE 'Added car: ';
RAISE NOTICE 'Marka: %', l_row.car_model;
RAISE NOTICE 'Mileage: %', l_row.mileage;
end LOOP;
return new;
end;
$$ LANGUAGE plpgsql;
And the corresponding definition of a statement level trigger:
CREATE TRIGGER add_car
after INSERT on car
REFERENCING NEW TABLE AS inserted
for each statement EXECUTE PROCEDURE add_car();
i want create event trigger for create table or select into,
eg:
when create table xxxx must table name bigen with 'temp'
my code
CREATE OR REPLACE FUNCTION create_table_func()
RETURNS event_trigger
AS
$$
DECLARE
V_TABLE name := TG_TABLE_NAME;
BEGIN
if V_TABLE !~ '^temp'
then
RAISE EXCEPTION 'must bigen with temp';
end if;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE EVENT TRIGGER create_table_1 ON ddl_command_start
WHEN TAG IN ('SELECT INTO')
EXECUTE PROCEDURE create_table_func();
but when execute
select * into test11 from test_bak
[Err] ERROR: column "tg_table_name" does not exist
this is my code ,it's meet my needs
code:
CREATE OR REPLACE FUNCTION trg_create_table_func()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
obj record;
BEGIN
FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands() WHERE command_tag in ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
LOOP
if obj.object_identity !~ 'public.temp_'
THEN
raise EXCEPTION 'The table name must begin with temp_';
end if;
END LOOP;
END;
$$;
CREATE EVENT TRIGGER trg_create_table ON ddl_command_end
WHEN TAG IN ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
EXECUTE PROCEDURE trg_create_table_func();
out recods
[Err] ERROR: The table name must begin with temp_
CONTEXT: PL/pgSQL function trg_create_table_func() line 10 at RAISE
it's cool ~
The special variable TG_TABLE_NAME is only supported in normal triggers, not in event triggers (there is not always an associated table!).
The documentation has a list of functions that can return context information in an event trigger.
You could use pg_event_trigger_ddl_commands() to get the information you need, but that only works in ddl_command_end event triggers. That should work for you; I don't see a reason why the trigger should not run at the end of the statement.
Below is the function where the records are stored in a record variable for each iteration. Here the table name is hardcoded for cursor bound variable. Is there is any way I can pass the table name as a parameter through this function?
CREATE OR REPLACE FUNCTION test1()
RETURNS SETOF refcursor AS
$BODY$
DECLARE
curs2 CURSOR FOR SELECT * FROM datas.test1000;
begin
FOR recordvar IN curs2 LOOP
RAISE NOTICE 'recordvar: %',recordvar;
END LOOP ;
end;
$BODY$
language plpgsql;
No, not for a bound cursor.
But you can easily pass a name for opening an unbound cursor. There is an example in the manual doing precisely that.
Your function could look like this:
CREATE OR REPLACE FUNCTION test2(_tbl regclass)
RETURNS void AS
$func$
DECLARE
_curs refcursor;
rec record;
BEGIN
OPEN _curs FOR EXECUTE
'SELECT * FROM ' || _tbl;
LOOP
FETCH NEXT FROM _curs INTO rec;
EXIT WHEN rec IS NULL;
RAISE NOTICE 'rec: %', rec;
END LOOP;
END
$func$ language plpgsql;
The special FOR loop can only be used with bound cursors. I supplied an alternative.
More explanation in this closely related answer:
Update record of a cursor where the table name is a parameter
I use the object identifier type regclass to pass the table name to avoid SQL injection.
More about that in this related answer on dba.SE:
I created a function which writes information about table deletions.
And another function which simply adds a trigger call after delete.
But I would like to store the whole row as string into my table.
According to Postgresql Documentation it should work by adding "OLD.*" into a text based column. But it fails telling me that I try to put too many columns into this table.
OLD is from type RECORD. And i want to have it in my text field like "value1,value2,value3" or it could be "colname:value,colname2:value". I dont care, I just want to see the row which has been deleted.
Another approach can be to log all delete queries from pg_stat_activity. But I don't know how to do that. Simply accessing pg_stat_activity every second would cause too much traffic I guess.
My table is simple:
create table delete_history (date timestamp, tablename varchar(100), data text);
This is my function:
CREATE or REPLACE FUNCTION ondelete() RETURNS TRIGGER AS $$
BEGIN
INSERT INTO delete_history VALUES (CURRENT_TIMESTAMP, TG_TABLE_NAME, OLD.*);
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
This is my trigger:
CREATE OR REPLACE FUNCTION history_create_triggers() RETURNS void
AS $$
DECLARE
r RECORD;
BEGIN
FOR r IN SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type='BASE TABLE' LOOP
EXECUTE 'CREATE TRIGGER log_history AFTER DELETE ON public.' || r.table_name || ' FOR EACH ROW EXECUTE PROCEDURE ondelete();';
END LOOP;
END;
$$ LANGUAGE plpgsql;
You can convert type record into text:
CREATE or REPLACE FUNCTION ondelete() RETURNS TRIGGER AS $$
BEGIN
INSERT INTO delete_history VALUES (CURRENT_TIMESTAMP, TG_TABLE_NAME, OLD::text);
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
sql fiddle demo
another approach could be converting your row into JSON with row_to_json function (if you have version 9.2):
CREATE or REPLACE FUNCTION ondelete() RETURNS TRIGGER AS $$
BEGIN
INSERT INTO delete_history VALUES (CURRENT_TIMESTAMP, TG_TABLE_NAME, row_to_json(OLD));
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
sql fiddle demo
Another approach can be convert your data to hstore
CREATE or REPLACE FUNCTION ondelete() RETURNS TRIGGER AS $$
BEGIN
INSERT INTO delete_history VALUES (CURRENT_TIMESTAMP, TG_TABLE_NAME, hstore(OLD));
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
I can't test it now - sqlfiddle is not allowing to use hstore.