Flashback transaction Query

Source: Internet
Author: User

    • Principle: Based on undo
    • A flashback transaction query can be viewed as a diagnostic tool that queries database changes at the transaction level
    • Flashback_transaction_query
      • Retrieve transaction information for all tables involved in a transaction.
      • Provides the SQL statements that's can use to undo The changes made by a particular transaction
    • Need to open the minimum additional log:
      ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;


Cases:

 
   
  
  1. \**查询所有事务信息*\
  2. SQL> SELECT operation, undo_sql, table_name FROM flashback_transaction_query;
  
 
  1. \**返回指定事务的信息*\
  2. SQL> SELECT operation, undo_sql, table_name FROM flashback_transaction_query WHERE xid = HEXTORAW(‘8C0024003A000000‘) ORDER BY undo_change#;
  3. SELECT xid,
  4. operation,
  5. start_scn,
  6. commit_scn,
  7. logon_user,
  8. undo_sql
  9. FROM flashback_transaction_query
  10. WHERE xid = HEXTORAW(‘000400070000004F‘);
  
 
  1. \**返回指定时间间隔的事务信息*\
  2. SQL> SELECT operation, undo_sql, table_name FROM flashback_transaction_query WHERE start_timestamp >= TO_TIMESTAMP (‘2003-10-21 11:00:00‘,‘YYYY-MM-DD HH:MI:SS‘)
  3. AND commit_timestamp <= TO_TIMESTAMP(‘2003-10-21 11:30:00‘,‘YYYY-MM-DD HH:MI:SS‘);
  4. SELECT xid,
  5. logon_user
  6. FROM flashback_transaction_query
  7. WHERE xid IN ( SELECT versions_xid
  8. FROM personnel
  9. VERSIONS BETWEEN
  10. TIMESTAMP TO_TIMESTAMP(‘2007-03-21 19:30:00‘, ‘YYYY-MM-DD HH24:MI:SS‘)
  11. AND TO_TIMESTAMP(‘2007-03-22 04:30:00‘, ‘YYYY-MM-DD HH24:MI:SS‘) );
Real-Operating scenes

1. Open additional logs and authorize

 
   
  
  1. alter database add supplemental log data;
  2. grant SELECT ANY TRANSACTION to hr;

2. Generating transactions

  
 
  1. insert into hr.departments
  2. (department_id,department_name,manager_id,location_id)
  3. values (999,‘SETI‘,100,1700);
  4. update hr.employees set department_id=999
  5. where employee_id=200;
  6. commit;

3. Get the transaction number and get the undo SQL by the transaction number

  
 
  1. select
  2. versions_xid,versions_startscn,department_id,department_name
  3. from hr.departments
  4. versions between timestamp minvalue and maxvalue
  5. where department_id=999
  6. order by 2 nulls first;
  7. set line 200
  8. col undo_sql for a90
  9. SELECT operation, undo_sql, table_name FROM flashback_transaction_query WHERE xid = HEXTORAW(‘10000A0039030000‘) ORDER BY undo_change#;
  10. OPERATION UNDO_SQL TABLE_NAME
  11. ---------- ------------------------------------------------------------------------- ----------------- ------------------------------
  12. update UPDATE span class= "str" > "HR" "EMPLOYEES" set department_id " = ' where ROWID = ' AAAWXEAAEAAAAZTAAC ' ; EMPLOYEES
  13. INSERT delete from "HR"."DEPARTMENTS" where ROWID = ‘AAAWXZAAEAAAAZMAAb‘; DEPARTMENTS
  14. BEGIN
  15. ----如果需要回退,执行上面的undo_sql即可

Rollback a transaction using the following PL/SQL statement

  
 
  1. begin
  2. for rec in
  3. (select undo_sql
  4. from flashback_transaction_query
  5. where xid=‘10000A0039030000‘)
  6. loop
  7. if rec.undo_sql is not null then
  8. execute immediate substr(rec.undo_sql,1,length(rec.undo_sql)-1);
  9. end if;
  10. end loop;
  11. commit;
  12. end;
  13. /


From for notes (Wiz)

Flashback transaction Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.