I have been working on creating the trigger for one materialized view like this in PSQL using supruser:
returns trigger language plpgsql
as $$
begin
refresh materialized view mat_view;
return null;
end $$;
create trigger refresh_mat_view
after insert or update or delete or truncate
on table1 for each statement
execute procedure refresh_mat_view();
create trigger refresh_mat_view
after insert or update or delete or truncate
on table2 for each statement
execute procedure refresh_mat_view();
After set up the trigger, when others tried to insert records in table1, it showed failed to insert and must be the owner of the mat_view. Is there any way that I can get around this for creating the trigger and also allow others to insert?
https://blog.rustprooflabs.com/2021/07/postgres-permission-mat-view
Issue can be solved here by creating a group of role to be act as an owner or using a cron job
Related
I have a weird problem. I am trying to write a TRIGGER that whenever DEPTNO in table SCOTT.DEPT is updated, then it updates my other Table ( SCOTT.EMP ) with a new DEPTNO.
I got a ORA-01031: insufficient privileges error, which is weird, because I'm logged in as system, I've made this table myself (create .. ) and my other trigger that I wrote is working properly.
Btw. Is this trigger written correctly?
Any help would be appreciated.
CREATE OR REPLACE TRIGGER Trigger1
BEFORE DELETE OR INSERT OR UPDATE OF DEPTNO ON SCOTT.DEPT
FOR EACH ROW
BEGIN
UPDATE SCOTT.EMP
SET DEPTNO = :NEW.DEPTNO;
END;
I am logged as System"
You need to be careful with that. SYSTEM is an Oracle owned account, used for maintaining the database, so there are risks to changing its schema.
In this case you have created the trigger in the SYSTEM schema because you didn't prefix the trigger name with anything. Normally triggers are owned by the schema which owns the underlying table, in this case SCOTT.
I suggest you drop the trigger from the SYSTEM schema and re-create it in SCOTT.
I am trying to create trigger TRIG_USER in Oracle db. Is there a way to control this trigger using the when clause.I want the trigger to activate only if there a entry in the table trigger_switch
I am looking something like this.
CREATE TRIGGER TRIG_USER
AFTER INSERT OR UPDATE OR DELETE ON USER_TABLE
FOR EACH ROW WHEN (IF EXISTS (SELECT 1 FROM trigger_switch))
BEGIN
[...]
END;
I am searching for a way, to store only the tables and columns that are added in the database with the one who created it (nachine_name) in a log table.
I tried to add a trigger on sys table user_tab_cols but I cannot do that Why cannot I create triggers on objects owned by SYS?
The system table user_objects will give me the date when a table created, but I want also to know which machine created it. and I also want to track the column creation and modification and log them in a table.
Is that possible ? is there a way for that ?
you can create a database event trigger:
CREATE OR REPLACE TRIGGER log_ddl_event_trg
AFTER DDL
ON DATABASE
DECLARE
v_sql_list ora_name_list_t;
v_sql_txt VARCHAR2(2500);
BEGIN
FOR i in 1..ORA_SQL_TXT(v_sql_list) LOOP
v_sql_txt := v_sql_txt || v_sql_list(i);
EXIT WHEN length(v_sql_txt ) >= 2000;
END LOOP;
...
END;
/
in the Trigger, you can get the executed ddl-statement using the ORA_SQL_TXT() Funktion, and then log it in the table together with the other data (log_date, user etc.).
I am trying to change the trigger script for my database. My trigger name is ARCH_USER_UPD_TRG and this puts any updates or deletes on the USER table into a Z_USER table
I am dropping a column from the USER table and now need to modify the trigger script to no longer use this column.
How do I modify the PL/SQL script of an oracle trigger?
A trigger is similar to a package or a procedure, so you can simply use
create or replace trigger triggerName
...
declare
...
begin
...
end;
The easy solution would be to Drop and Create the trigger once again with the modified SQL script code.
DROP TRIGGER ARCH_USER_UPD_TRG;
CREATE TRIGGER ARCH_USER_UPD_TRG
//rest of code body
I have a trigger that is fire on inserting a new record in table in that i want to insert new record in the same table.
My trigger is :
create or replace trigger inst_table
after insert on test_table referencing new as new old as old
for each row
declare
df_name varchar2(500);
df_desc varchar2(2000);
begin
df_name := :new.name;
df_desc := :new.description;
if inserting then
FOR item IN (SELECT pid FROM tbl2 where pid not in(1))
LOOP
insert into test_table (name,description,pid) values(df_name,df_desc,item.pid);
END LOOP;
end if;
end;
its give a error like
ORA-04091: table TEST_TABLE is mutating, trigger/function may not see it
i think it is preventing me to insert into same table.
so how can i insert this new record in to same table.
Note :- I am using Oracle as database
Mutation happens any time you have a row-level trigger that modifies the table that you're triggering on. The problem, is that Oracle can't know how to behave. You insert a row, the trigger itself inserts a row into the same table, and Oracle gets confused, cause, those inserts into the table due to the trigger, are they subject to the trigger action too?
The solution is a three-step process.
1.) Statement level before trigger that instantiates a package that will keep track of the rows being inserted.
2.) Row-level before or after trigger that saves that row info into the package variables that were instantiated in the previous step.
3.) Statement level after trigger that inserts into the table, all the rows that are saved in the package variable.
An example of this can be found here:
http://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551198119097816936
Hope that helps.
I'd say that you should look at any way OTHER than triggers to achieve this. As mentioned in the answer from Mark Bobak, the trigger is inserting a row and then for each row inserted by the trigger, that then needs to call the trigger to insert more rows.
I'd look at either writing a stored procedure to create the insert or just insert via a sub-query rather than by values.
Triggers can be used to solve simple problems but when solving more complicated problems they will just cause headaches.
It would be worth reading through the answers to this duplicate question posted by APC and also these this article from Tom Kyte. BTW, the article is also referenced in the duplicate question but the link is now out of date.
Although after complaining about how bad triggers are, here is another solution.
Maybe you need to look at having two tables. Insert the data into the test_table table as you currently do. But instead of having the trigger insert additional rows into the test_table table, have a detail table with the data. The trigger can then insert all the required rows into the detail table.
You may again encounter the mutating trigger error if you have a delete cascade foreign key relationship between the two tables so it might be best to avoid that.