FLASHBACK TABLE

L'istruzione FLASHBACK è utilizzato per
  • recuperare oggetti, dati e dipendenze tra oggetti;
  • recuperare tabelle droppate per errore;
  • recuperare dati manipolati con una serie di DML;
  • confrontare dati in intervalli di tempo differenti.
Durante l'operazione di flashback table, tutte le tabelle impattate vengono poste in modalità exclusive dml locks.
Con l'istruzione flashback table è possibile recuperare i dati di una tabella in un determinato intervallo di tempo. Utilizza gli undo data nei segmenti di undo.
Non è necessario abilitare la modalità flashback database.


FLASHBACK TABLE nome_tabella TO SCN valore_scn [ENABLE TRIGGERS];
FLASHBACK TABLE nome_tabella TO TIMESTAMP to_timestamp('03/04/2019 08:05','dd/mm/yyyy hh24:mi:ss') [ENABLE TRIGGERS];
FLASHBACK TABLE nome_tabella TO RESTORE POINT punto_di_restore [ENABLE TRIGGERS];

Durante l'operazione di flashback vengono disabilitati i triggers ed occorre abilitarli successivamente al termine della operazione, a meno che non si utilizzi l'opzione enable triggers.

SCN, System Change Number, è un progressivo numerico assegnato automaticamente e in tempo reale dal database ad ogni transazione committata.
Per risalire all'ultimo SCN utilizzare la seguente funzione.

select DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER from dual;

oppure la vista del dizionario dati

select CURRENT_SCN from V$DATABASE ;


E s e m p i o

Supponiamo che il nome del dipartimento con id 20 viene aggiornato per sbaglio e di voler recuperare la vecchia denominazione.
Utilizziamo la pseudocolonna ORA_ROWSCN per recuperare il valore SCN associato alla riga di una tabella.

select ORA_ROWSCN , department_name from   emp_1 where department_id = 20;
ORA_ROWSCN  department_name
--------------------------
2398039     Marketing

update emp_1 set department_name ='TOWER1' where department_id = 20;
Commit;

select ORA_ROWSCN , department_name from   emp_1 where department_id= 20;
ORA_ROWSCN  department_name
--------------------------
2398081     TOWER1

FLASHBACK TABLE emp_1 TO SCN 2398039;

Errore SQL: ORA-08189: impossibile eseguire l'operazione Flashback Table sulla tabella. Lo spostamento delle righe non è abilitato.
08189. 00000 -  "cannot flashback the table because row movement is not enabled"
*Cause:    An attempt was made to perform Flashback Table operation on a table for
           which row movement has not been enabled. Because the Flashback Table
           does not preserve the rowids, it is necessary that row
           movement be enabled on the table.
*Action:   Enable row movement on the table

Di default non è possibile recuperare un vecchio stato di una tabella se non viene abilitato l'opzione ROW MOVEMENT.

ALTER TABLE emp_1 ENABLE ROW MOVEMENT;

A questo punto se si ripete l'esempio di prima è possibile risalire ad un vecchio stato della tabella.

select ORA_ROWSCN , department_name from   emp_1 where DEPARTMENT_ID = 20;
ORA_ROWSCN  department_name
--------------------------
2477592 TOWER1

update emp_1 set department_name ='Marketing' where DEPARTMENT_ID = 20;
commit;

select ORA_ROWSCN , department_name from   emp_1 where DEPARTMENT_ID = 20;
ORA_ROWSCN  department_name
--------------------------
2478587 Marketing

FLASHBACK TABLE emp_1 TO SCN 2477592;
select ORA_ROWSCN , department_name from   emp_1 where DEPARTMENT_ID = 20;
ORA_ROWSCN  department_name
--------------------------
2478643 TOWER1

E' possibile ricavare il valore di un SCN in una data e viceversa usando le funzioni seguenti.

select  SCN_TO_TIMESTAMP(2478643) from dual;
timesptamp
----------------------------
02-DIC-15 16:52:43,000000000

select TIMESTAMP_TO_SCN  ('02-DIC-15 16:52:43,000000000') from dual;
scn
-----
2478629

Dalla seconda query si evince che il valori SCN non sono gli stessi anche se si usa lo stesso valore di Timestamp. Non c'è una relazione biunivoca.

Se si vuole effettuare il restore della tabella in uno specifico momento conviene usare l'oggetto RESTORE POINT che identifica uno specifico istante con un valore di snc o timestamp.

select ORA_ROWSCN , department_name from   hr.emp_1 where department_id = 100;
ORA_ROWSCN , department_name
----------------------------
2502370 Finance

CREATE RESTORE POINT pt_dep1;

update hr.emp_1 set department_name ='TestingFactory' where DEPARTMENT_ID = 100;
commit;

select ORA_ROWSCN , department_name from   hr.emp_1 where DEPARTMENT_ID = 100;
ORA_ROWSCN , department_name
----------------------------
2506314 TestingFactory

FLASHBACK TABLE hr.emp_1 TO RESTORE POINT pt_dep1;

select ORA_ROWSCN , department_name from   hr.emp_1 where DEPARTMENT_ID = 100;
ORA_ROWSCN , department_name
----------------------------
2506372 Finance

Post popolari in questo blog

ORA-12154: TNS: il listener non è attualmente a conoscenza del servizio richiesto nel descrittore di connessione

Create e Drop Pluggable Database