Modification of Trigger in Oracle Database - oracle

If a trigger is already in function what will be the effect if i will replace a small part of it ,so many records are getting inserted in table A continuously in which the trigger is applied.
Trigger X is running in table A,
total records are getting insterted per minute are 1000
If i have replaced the trigger what will be the Impact of It for those service who are accesing the table.
Thanks in advance

A CREATE TRIGGER DDL should have no impact on your running DML transactions. Exclusive locks are not required on the table in order to add the trigger.
A CREATE OR REPLACE DDL is slightly different. It has to change an existing object. If the trigger is actively firing, the new trigger will try to lock the trigger object in the library cache before altering it. No impact on the table.
I generally observe that triggers execute immediately.
If you have tested your trigger, you should have no issues. If the trigger is correct, it will go into effect, and the impact shall be according to the logic that you've written in the trigger. The act of creating the trigger is not of concern, but the correctness of the trigger code is. So test it well.
Any transactions underway at the time you create the trigger will finish without firing the trigger.
Any future transactions will fire the trigger.

Related

Oracle - When does the trigger after insert run?

I have a table in my Oracle database. The table is used as a messaging queue. Sender process writes to it and the receiver process reads from it. I want to update the inserted messages under certain conditions before receiver reads it. If I set a trigger to "UPDATE ROW AFTER INSERT", when exactly will Oracle run it? Will Oracle handle the trigger as the first thing after insert? To be clear, will Oracle run the trigger before the receiver reads the inserted message?
You want to change the data to be inserted in a before row trigger. See for an explanation of the difference of before and after triggers this question:
difference before and after trigger in oracle
The reader will only be able to see any data after the sender process has committed the changes.

How many triggers we can have on a single table in Oracle DB

I have a confusion on maximum numbers of different type of triggers we can have on a single table in oracle db.
Do you encounter any problems due to any limit?
You can have triggers of these timing points:
BEFORE statement triggers
BEFORE row triggers
AFTER row triggers
AFTER statement triggers
In case you have more than one trigger at a timing point the order of execution is undetermined, thus it should be very uncommon to have many triggers for the same timing point.
Oracle documentation says:
You cannot control the order in which multiple row triggers fire.
If two or more triggers are defined with the same timing point, and the order in which they fire is important, then you can control the firing order using the FOLLOWS clause.
You can have more than one trigger at same point of time.
You can achieve that using FOLLOWS keyword while creating trigger.

Suggest Event pattern design in database

Can anyone suggest a good event pattern design or framework for changes in table in oracle.
Changes are not just the based on the column value change,but also the business driven logic .All the logging should be driven by some event setup.
At the end we track the changes and drive the business logic based on the changes .
I might be talking too high level,sorry for that :)
Assume TableA needs to be tracked for ColumnA.
Create AUDIT_TRAIL Table with columns TABLENAME, COLUMNNAME, OLDVALUE,NEWVALUE, DATEANDTIME, PK_SEQ
Create a Trigger to poll TableA for any change in ColumnA, and insert them into AUDIT_TRAIL (PK_SEQ should be a Oracle Sequence Number, DATEANDTIME should be from sysdate)
Something like this for trigger
CREATE OR REPLACE TRIGGER trg_table_audit
before insert or update or delete on tableA REFERENCING NEW AS NEW OLD AS OLD
for each row
begin
if inserting or deleting then
insert into audittrail (....)
Reading the fine manual: Using Triggers to Write Audit Data to a Separate Table:
You can use triggers to supplement the built-in auditing features of Oracle Database. The trigger that you create records user actions to a separate database table. When an activity fires the trigger, the trigger records the action in this table. Triggers are useful when you want to record customized information such as before-and-after changes to a table.
Reading even more the fine manual: PL/SQL Triggers:
A trigger is like a stored procedure that Oracle Database invokes automatically whenever a specified event occurs.
On top of that you can build a setup engine that turns triggers on and off. The "business logic" can later read the data recorded by the triggers.
I assume you're not interested in auditing. For the details see e.g. Verifying Security Access with Auditing.
How about you start with using TRIGGERS? And then from there, you could call procedures/functions from a package body for tracking the changes on the table/s.

Calling Oracle autonomous stored procedure from trigger

I have an Oracle trigger which is calling a stored procedure that has PRAGMA AUTONOMOUS_TRANSACTION defined. The values that are passed from the trigger have been committed already but it appears that the values are not available in the stored procedure? I'm not positive of this since the ability to debug/log/commit is difficult and the timing of the output is confusing me a bit. I'd like to know if it's expected that any passed values are simply available in the stored procedure regardless of the AUTONOMOUS_TRANSACTION?
Thanks
Values passed in to a stored procedure as parameters will always be available to the stored procedure. It doesn't matter whether the procedure is declared using an autonomous transaction.
Code running in an autonomous transaction cannot see changes made by the calling transaction. 9 times out of 10, when people are describing problems seeing the data they expect, this is the source of the problem.
If your stored procedure is doing anything other than writing something to a log table, I would be exceptionally cautious about using autonomous transactions. If you are using autonomous transactions for anything other than logging, you are almost certainly using them incorrectly. And you are probably introducing a whole host of bugs related to race conditions and transactional integrity.
"The trigger logic is conditionally
updating Table B which calls the
stored procedure to select from the
values on Table A so that Table B can
be updated with a calculated value. "
Perhaps Table B really ought to be a Materialized View derived from Table A? We can build a lot of complexity into the WHERE clauses of the queries which populate MViews. Find out more.
If you have a row level trigger on table_x, then that trigger can be fired multiple times by the same statement as different rows are impacted by that statement.
The order in which those rows are impacted is indeterminate. As such, the state of table_x is indeterminate during the execution of a row level trigger. This is why the MUTATING TABLE exception is raised.
An autonomous transaction 'cheats' by looking at the committed state of the table (ie excluding all changes made by that statement, and other statements in the transaction).
If you want a stored procedure to look at the state of table_x in response to activity on that table, then it needs to be done after all the rows changes have been made (ie in a statement level trigger, not a row level trigger).
The design pattern for this is often to set a flag (package level variable) in a row level trigger, check the flag in an AFTER statement level trigger, and if necessary action it and reset it.

How to call the Triggers in user deefined way?

I created the Employee table which contains EmpNo,EName,EDesignation as its fields.Also i created the 3 Triggers namely Trigger_1,Trigger_2 and Trigger_3.All the Triggers are Statement level triggers and fired after the update done in the table.Now i want the following orders in which the triggers are going to fired when the update statement is executed.
The Order is
Trigger_3,
Trigger_1,
Trigger_2
Can anyone tell me the way to fire the trigger events in userdefined way?I m using Oracle 9i
Trigger Evaluation Order
Quote from Oracle documentation:
Although any trigger can run a
sequence of operations either in-line
or by calling procedures, using
multiple triggers of the same type
enhances database administration by
permitting the modular installation of
applications that have triggers on the
same tables.
Oracle Database executes all triggers
of the same type before executing
triggers of a different type. If you
have multiple triggers of the same
type on a single table, then Oracle
Database chooses an arbitrary order to
execute these triggers.
Each subsequent trigger sees the
changes made by the previously fired
triggers. Each trigger can see the old
and new values. The old values are the
original values, and the new values
are the current values, as set by the
most recently fired UPDATE or INSERT
trigger.
To ensure that multiple triggered
actions occur in a specific order, you
must consolidate these actions into a
single trigger (for example, by having
the trigger call a series of
procedures).
see also http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
Have one trigger with the contents of the three.
If you cannot, for reasons for modularization, reusability..., create three stored procedures and call these one by one in the single trigger.
Upgrade to 11g and you can define trigger execution order

Resources