Flashback Pluggable Database (PDB) en Ambiente Multi-Tenant con Oracle Database 12c R2.

Introducción:
Oracle Database 12c R2 ahora permite que las Bases de Datos, Pluggable Databases (PDBs) que pertenecen a un CDB puedan ser rebobinadas por separado, sin afectar al resto. Es una gran ventaja que no se contaba en la version 12cR1 ya que habia que rebobinar todo el CDB (Container Database) completo o trabajar con arquitectura No-Container Database (NCDB) para poder contar con esta independencia.
Agregando esta funcionalidad ahora las Bases de Datos PDB’s pueden ser rebobinadas ante errores por ejecutar DML’s erroneas que afectan datos masivamente, o DDL’s catastróficas que afectan en cascada muchas estructuras.
Configuración del Ambiente
Operating System:   Oracle Enterprise Linux 6.8 (Santiago)
Database:     Oracle Database 12c R2 (12.2.0.1.0)
Oracle Clusterware:   Oracle Clusterware 12c R2 (12.2.0.1.0)
Hostname:     tstldb10, tstldb102
IP addresses:     192.168.0.61, 192.168.0.62
Cluster Name:     TSTLDB01
Container Database (CDB): PRODCDB
Pluggable Database (PDB): PRODPDB

Pre-requisitos
Habilitar el modo ARCHIVELOG en el CDB
Verificamos que el CDB$ROOT esta en modo ARCHIVELOG
[oracle@tstldb101 trace]$ . oraenv
ORACLE_SID = [oracle] ? prodcdb1
The Oracle base remains unchanged with value /u01/app/oracle
[oracle@tstldb101 trace]$ sqlplus / as sysdba

SQL*Plus: Release 12.2.0.1.0 Production on Tue Mar 14 00:53:15 2017
Copyright (c) 1982, 2016, Oracle.  All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> select log_mode from v$database;

LOG_MODE
-------------------
ARCHIVELOG

Creamos una table TEST con datos en la PDB: PRODPDB
Creamos la table TEST con 10 registros con el propósito de mostrar como trabaja el FLASHBACK.
SQL> select inst_id,name, open_mode from gv$containers order by 2,1;

INST_ID           NAME                OPEN_MODE
-------------   ----------------    --------------------
1                 CDB$ROOT            READ WRITE
2                 CDB$ROOT            READ WRITE
1                 PDB$SEED            READ ONLY
2                 PDB$SEED            READ ONLY
1                 PRODPDB             READ WRITE
2                 PRODPDB             READ WRITE

6 rows selected.

SQL> alter session set container = PRODPDB;
Session altered.

Creamos la tabla y cargamos datos
SQL> create table test_user.test (id number);
Table created.

SQL> begin
  2  for i in 1..10
  3  loop
  4  insert into test_user.test values (i);
  5  end loop;
  6  commit;
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL> select * from test_user.test;

        ID
----------
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10

10 rows selected.

Creamos un Guaranteed Restore Point (GRP)
En este ejemplo vamos a crear un ‘Guaranteed Restore Point’ aunque garantizar un punto de restauración es un poco excesivo para lo ociosa que está esta base de TEST, lo haremos porque es un pre-requisito para asegurar el exito de la restauración. La diferencia entre un ‘Guaranteed Restore Point’ y un ‘Restore Point’ normal es que el primero garantiza la restauración a cualquier punto entre el presente y el punto de restauración, por lo que los flashbacklogs serán conservados desde el punto de restauración garantizado en adelante en la Flash Recovery Area (FRA). Los registros de flashback no se eliminan en respuesta a la presión espacial si se requiere que satisfagan la garantía. Esto implica que a partir de agregar un punto de restauracion garantizado se debe monitorear el espacio en la FRA porque no va a reusar el espacio de los flashback logs.
Es interesante destacar que no se necesita tener flashback habilitado para el CDB para crear Restore Points (puntos de restauración) en la PDB. Si tenemos que crear un punto de restuaración en una PDB mientras el CDB no tiene flashback habilitado,el valor en la V$DATABASE de la columna FLASHBACK_ON cambiará de NO a RESTORE POINT ONLY.
SQL>  select flashback_on from v$database;

FLASHBACK_ON
--------------------------
NO

SQL> create restore point BEFORE_DML guarantee flashback database;
Restore point created.

SQL> select flashback_on from v$database;

FLASHBACK_ON
----------------------------------
RESTORE POINT ONLY

Verificamos la entrada en el Alert Log
Cuando se ejecuta el comando Restore Point, podemos ver que se levanta el proceso RVWR en el CDB y tendrá una entrada indicando el nombre del restore point y el nombre de la PDB que regenera el restore point.
Starting background process RVWR
2017-03-14T01:00:08.210974-04:00
RVWR started with pid=60, OS id=437
2017-03-14T01:00:12.721691-04:00
PRODPDB(3):Allocated 15937344 bytes in shared pool for flashback generation buffer
2017-03-14T01:00:25.846052-04:00
PRODPDB(3):Created guaranteed restore point BEFORE_DML

Borramos la table TEST de la PDB
Haremos daño a la tabla TEST eliminandola y luego depurando DBA_RECYCLEBIN para que tampoco nos quede en la papelera.
SQL> drop table test_user.test;
Table dropped.

SQL> purge dba_recyclebin;
DBA Recyclebin purged.

Realizamos una verificación para asegurarnos que los datos de la table TEST ya no estan disponibles.
SQL> select * from test_user.test;
select * from test_user.test
                        *
ERROR at line 1:
ORA-00942: table or view does not exist

Flashback de la Pluggable Database (PDB)
Ahora que tenemos la table TEST irreparable rebobinaremos la PDB al RESTORE POINT que fue creado antes de eliminar la tabla.
Primero tenemos que cerrar la PDB en todas las instancias del cluster, al cerrar la PDB queda en estado montada, MOUNT.
[oracle@tstldb101 trace]$ . oraenv
ORACLE_SID = [oracle] ? prodcdb1
The Oracle base remains unchanged with value /u01/app/oracle

[oracle@tstldb101 trace]$ sqlplus / as sysdba
SQL*Plus: Release 12.2.0.1.0 Production on Tue Mar 14 01:18:25 2017
Copyright (c) 1982, 2016, Oracle.  All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> select inst_id,name, open_mode from gv$containers order by 2,1;

INST_ID       NAME                  OPEN_MODE
------------  ------------------    --------------------
1             CDB$ROOT              READ WRITE
2             CDB$ROOT              READ WRITE
1             PDB$SEED              READ ONLY
2             PDB$SEED              READ ONLY
1             PRODPDB               READ WRITE
2             PRODPDB               READ WRITE

6 rows selected.

SQL> alter pluggable database PRODPDB close instances=ALL;
Pluggable database altered.

SQL> select inst_id,name, open_mode from gv$containers order by 2,1;

INST_ID          NAME                OPEN_MODE
-------------   ----------------    --------------------
1               CDB$ROOT             READ WRITE
2               CDB$ROOT             READ WRITE
1               PDB$SEED             READ ONLY
2               PDB$SEED             READ ONLY
1               PRODPDB              MOUNTED
2               PRODPDB              MOUNTED
6 rows selected.

Ahora que la PDB esta cerrada podemos ejecutar el comando Flashback Database en ella.
SQL> flashback pluggable database PRODPDB to restore point BEFORE_DML;
Flashback complete.

Verificamos la Entrada en el Alert Log
2017-03-14T01:20:45.900268-04:00
flashback pluggable database PRODPDB to restore point BEFORE_DML
2017-03-14T01:20:47.003048-04:00
Flashback Restore Start
Restore Flashback Pluggable Database PRODPDB (3) until change 1484920
Flashback Restore Complete
Flashback Media Recovery Start
2017-03-14T01:20:48.736628-04:00
Serial Media Recovery started
2017-03-14T01:20:49.049432-04:00
Recovery of Online Redo Log: Thread 1 Group 2 Seq 2 Reading mem 0
  Mem# 0: +DATA/PRODCDB/ONLINELOG/group_2.262.938558255
  Mem# 1: +FRA/PRODCDB/ONLINELOG/group_2.258.938558271
2017-03-14T01:20:49.090049-04:00
Recovery of Online Redo Log: Thread 2 Group 4 Seq 2 Reading mem 0
  Mem# 0: +DATA/PRODCDB/ONLINELOG/group_4.271.938559081
  Mem# 1: +FRA/PRODCDB/ONLINELOG/group_4.260.938559091
2017-03-14T01:20:49.409836-04:00
Incomplete Recovery applied until change 1484920 time 03/14/2017 01:08:25
Flashback Media Recovery Complete
Flashback Pluggable Database PRODPDB (3) recovered until change 1484920
Completed: flashback pluggable database PRODPDB to restore point BEFORE_DML

Because the restore point was created 

SQL> alter pluggable database PRODPDB open instances=ALL;
alter pluggable database PRODPDB open instances=ALL
*
ERROR at line 1:
ORA-65107: Error encountered when processing the current task on instance:1
ORA-01113: file 14 needs media recovery
ORA-01110: data file 14:
'+DATA/PRODCDB/4AA8F71F10B96DB0E0533D00A8C0638A/DATAFILE/users.278.938559493'

Este error da debido a que el flashback fue realizado al punto de restauración que habia sido creado mientras la PDB estaba en modo READ WRITE, por ello necesitamos abrir la PDB con la opción RESETLOGS y luego ejecutar otra vez el comando OPEN para que abra en la PDB en todas las instancias de nuestro cluster.
SQL> alter pluggable database PRODPDB open resetlogs;
Pluggable database altered.

SQL> alter pluggable database PRODPDB open instances=ALL;
Pluggable database altered.

Verificamos la Entrada en el Alert Log
2017-03-14T01:20:45.900268-04:00
flashback pluggable database PRODPDB to restore point BEFORE_DML
2017-03-14T01:20:47.003048-04:00
Flashback Restore Start
Restore Flashback Pluggable Database PRODPDB (3) until change 1484920
Flashback Restore Complete
Flashback Media Recovery Start
2017-03-14T01:20:48.736628-04:00
Serial Media Recovery started
2017-03-14T01:20:49.049432-04:00
Recovery of Online Redo Log: Thread 1 Group 2 Seq 2 Reading mem 0
  Mem# 0: +DATA/PRODCDB/ONLINELOG/group_2.262.938558255
  Mem# 1: +FRA/PRODCDB/ONLINELOG/group_2.258.938558271
2017-03-14T01:20:49.090049-04:00
Recovery of Online Redo Log: Thread 2 Group 4 Seq 2 Reading mem 0
  Mem# 0: +DATA/PRODCDB/ONLINELOG/group_4.271.938559081
  Mem# 1: +FRA/PRODCDB/ONLINELOG/group_4.260.938559091
2017-03-14T01:20:49.409836-04:00
Incomplete Recovery applied until change 1484920 time 03/14/2017 01:08:25
Flashback Media Recovery Complete
Flashback Pluggable Database PRODPDB (3) recovered until change 1484920
Completed: flashback pluggable database PRODPDB to restore point BEFORE_DML
2017-03-14T01:22:22.628569-04:00
alter pluggable database PRODPDB open instances=ALL
2017-03-14T01:22:22.735406-04:00
PRODPDB(3):Autotune of undo retention is turned on.
2017-03-14T01:22:22.883561-04:00
PRODPDB(3):This instance was first to open pluggable database PRODPDB 
(container=3)

Pdb PRODPDB hit error 1113 during open read write (1) and will be closed.
2017-03-14T01:22:22.995375-04:00
Errors in file /u01/app/oracle/diag/rdbms/prodcdb/prodcdb1/trace/
prodcdb1_ppa7_1957.trc:

ORA-01113: file 14 needs media recovery
ORA-01110: data file 14: '+DATA/PRODCDB/4AA8F71F10B96DB0E0533D00A8C0638A/
DATAFILE/users.278.938559493'

PRODPDB(3):JIT: pid 1957 requesting stop
PRODPDB(3):detach called for domid 3 (domuid: 0x41e01c9f, options: 0x10, 
pid: 1957)

2017-03-14T01:22:23.205075-04:00
Errors in file /u01/app/oracle/diag/rdbms/prodcdb/prodcdb1/trace/
prodcdb1_ppa7_1957.trc:

ORA-01113: file 14 needs media recovery
ORA-01110: data file 14: '+DATA/PRODCDB/4AA8F71F10B96DB0E0533D00A8C0638A/
DATAFILE/users.278.938559493'

ORA-65107 signalled during: alter pluggable database PRODPDB open 
instances=ALL...

2017-03-14T01:24:35.278898-04:00
alter pluggable database PRODPDB open resetlogs
2017-03-14T01:24:35.564485-04:00
Online datafile 14
Online datafile 13
Online datafile 12
Online datafile 11
Online datafile 10
PRODPDB(3):Autotune of undo retention is turned on.
PRODPDB(3):This instance was first to open pluggable database PRODPDB 
(container=3)

PRODPDB(3):attach called for domid 3 (domuid: 0x41e01c9f, options: 0x0,
 pid: 5746)

PRODPDB(3):queued attach broadcast request 0x78e89368
2017-03-14T01:24:35.935673-04:00
* allocate domain 3, valid ? 1
 all enqueues go to domain 0
2017-03-14T01:24:36.413698-04:00
PRODPDB(3):Endian type of dictionary set to little
2017-03-14T01:24:38.178066-04:00
PRODPDB(3):[5746] Successfully onlined Undo Tablespace 2.
PRODPDB(3):Undo initialization finished serial:0 start:16869251 
end:16870398 diff:1147 ms (1.1 seconds)

PRODPDB(3):Database Characterset for PRODPDB is AL32UTF8
PRODPDB(3):JIT: pid 5746 requesting stop
2017-03-14T01:24:39.346088-04:00
PRODPDB(3):detach called for domid 3 (domuid: 0x41e01c9f, options: 
0x0, pid: 5746)

PRODPDB(3):queued detach broadcast request 0x78e89310
2017-03-14T01:24:39.452824-04:00
freeing rdom 3
PRODPDB(3):Autotune of undo retention is turned on.
2017-03-14T01:24:40.111054-04:00
PRODPDB(3):This instance was first to open pluggable database PRODPDB 
(container=3)

2017-03-14T01:24:40.355393-04:00
PRODPDB(3):attach called for domid 3 (domuid: 0x41e01c9f, options: 
0x0, pid: 5746)

PRODPDB(3):queued attach broadcast request 0x78e892b8
2017-03-14T01:24:40.394141-04:00
* allocate domain 3, valid ? 1
 all enqueues go to domain 0
2017-03-14T01:24:40.867286-04:00
PRODPDB(3):Endian type of dictionary set to little
2017-03-14T01:24:42.505342-04:00
PRODPDB(3):[5746] Successfully onlined Undo Tablespace 2.
PRODPDB(3):Undo initialization finished serial:0 start:16873543 
end:16874610 diff:1067 ms (1.1 seconds)

PRODPDB(3):Pluggable database PRODPDB dictionary check beginning
PRODPDB(3):Pluggable Database PRODPDB Dictionary check complete
PRODPDB(3):Database Characterset for PRODPDB is AL32UTF8
2017-03-14T01:24:44.273160-04:00
PRODPDB(3):Opatch validation is skipped for PDB PRODPDB (con_id=0)
2017-03-14T01:24:47.946543-04:00
PRODPDB(3):Opening pdb with no Resource Manager plan active
2017-03-14T01:24:50.875522-04:00
Starting control autobackup
2017-03-14T01:24:53.590768-04:00
Control autobackup written to DISK device

handle '+FRA/PRODCDB/AUTOBACKUP/2017_03_14/s_938568290.269.938568293'

Pluggable database PRODPDB closed
Completed: alter pluggable database PRODPDB open resetlogs
2017-03-14T01:25:20.369712-04:00
alter pluggable database PRODPDB open instances=ALL
2017-03-14T01:25:28.316750-04:00
Completed: alter pluggable database PRODPDB open instances=ALL

Ahora que la PDB esta abierta en todas las instancias podemos confirmar si nuestra tabla TEST eliminada la hemos recuperado.
SQL> alter session set container = PRODPDB;
Session altered.

SQL> select * from test_user.test;

ID
----------
1
2
3
4
5
6
7
8
9
10

10 rows selected.

Exitoso!!! La table TEST esta de vuelta en la PDB. Ahora lo que vamos a hacer es eliminar nuestro RESTORE POINT y observar que hace la instancia Oracle por atrás.
Eliminamos el RESTORE POINT
SQL> drop restore point BEFORE_DML;
Restore point dropped.

Verificamos la Entrada en el Alert Log 
2017-03-14T01:35:02.079060-04:00
PRODPDB(3):Drop guaranteed restore point BEFORE_DML
2017-03-14T01:35:02.256528-04:00
RVWR shutting down
2017-03-14T01:35:05.265401-04:00
PRODPDB(3):Deleted Oracle managed file +FRA/PRODCDB/
4AA8F71F10B96DB0E0533D00A8C0638A/FLASHBACK/log_1.268.938567291

PRODPDB(3):Deleted Oracle managed file +FRA/PRODCDB/FLASHBACK/
log_2.267.938567297

PRODPDB(3):Deleted Oracle managed file +FRA/PRODCDB/FLASHBACK/
log_3.266.938567299

PRODPDB(3):Deleted Oracle managed file +FRA/PRODCDB/FLASHBACK/
log_4.265.938567307

PRODPDB(3):Guaranteed restore point BEFORE_DML dropped

Vemos en esta salida del alert.log de arriba que los flashback logs asociados con el Restore Point fueron eliminados.
Pasos Principales:
  • Habilitar el modo ARCHIVELOG en el Container (CDB)
  • Crear un Punta de Restauración Garantizado (Guaranteed Restore Point) en la Pluggable Database (PDB)
  • Cerrar la Pluggable Database (PDB)
  • Flashback la Pluggable Database (PDB) al RESTORE POINT
  • Abrir la Pluggable Database (PDB) con la opción RESET LOGS

Conclusión
Es posible ahora en Oracle Database 12c R2 ejecutar el comando Flashback Database para una Pluggable Database (PDB). Esto reduce drasticamente el costo de realizar un Point In Time Recovery de la PDB ya que ahora no se necesita restaurar el Container Database (CDB) entero para volver una PDB a un punto anterior en el tiempo. Lo que significa que ahora cada PDB esta verdaderamente separada una de otra, tanto en el tiempo de ejecución como durante el escenario de recuperación.

Comentarios

Publicar un comentario

Entradas populares de este blog

Installing Oracle GoldenGate for Oracle 12c

Replicación de datos con múltiples procesos "Extract" y "Replicat" con el modo de captura integrada utilizando Oracle GoldenGate 12c

MANUAL DATABASE (CDB AND PDB) CREATION VIA SQL*PLUS