Blog

What is Table Point In Time Recovery (PITR) in Oracle

In the past, when a table was deleted or had to come back after a certain period of time for any reason, it did a lot of unnecessary work. With Oracle Database 12C, we had the chance to bring the table back to our lives with Table Point In Time Recovery, in other words, with a single action from the backup.

In today’s article, we will recognize this feature that can be used in Oracle Database 12C and higher versions. The process logically opens a new instance for you and registers a database with your table on this instance. After registration, the table is taken out and imported into your existing database. You can do this by going to a certain date or according to the SCN or Sequence number, but there are some situations that we need to pay attention to. When you use the RECOVER command to recover tables or table partitions contained in an RMAN backup, the following limitations exist.

  • 1.The database must be on the archivemode.
  • 2.Tables and table partitions belonging to the SYS scheme cannot be recovered.
  • 3.Tables and table partitions cannot be recovered from SYSTEM and SYSAUX table fields.
  • 4.Tables and table partitions in standby databases cannot be recovered.
  • 5.Tables with NOT NULL constraints cannot be recovered with the REMAP option.

Recovering tables or table partitions in RMAN backup with example

We will explain with an example of the test database.

[oracle@dba 19C]$ rman target /

 

Recovery Manager: Release 19.0.0.0.0 – Production on Tue Nov 9 08:48:36 2021

Version 19.3.0.0.0

 

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

 

connected to target database: TESTDB (DBID=2870051281)

RMAN> backup database plus archivelog;

Starting backup at 09-NOV-21

current log archived

using target database control file instead of recovery catalog

allocated channel: ORA_DISK_1

channel ORA_DISK_1: SID=55 device type=DISK

channel ORA_DISK_1: starting archived log backup set

channel ORA_DISK_1: specifying archived log(s) in backup set

input archived log thread=1 sequence=8 RECID=2 STAMP=1088140117

input archived log thread=1 sequence=9 RECID=1 STAMP=1088140117

input archived log thread=1 sequence=10 RECID=3 STAMP=1088140119

input archived log thread=1 sequence=11 RECID=4 STAMP=1088140119

input archived log thread=1 sequence=12 RECID=5 STAMP=1088140149

input archived log thread=1 sequence=13 RECID=6 STAMP=1088140149

input archived log thread=1 sequence=14 RECID=7 STAMP=1088140149

input archived log thread=1 sequence=15 RECID=8 STAMP=1088140150

input archived log thread=1 sequence=16 RECID=9 STAMP=1088143059

input archived log thread=1 sequence=17 RECID=10 STAMP=1088143061

input archived log thread=1 sequence=18 RECID=11 STAMP=1088143064

input archived log thread=1 sequence=19 RECID=12 STAMP=1088143064

input archived log thread=1 sequence=20 RECID=13 STAMP=1088144664

input archived log thread=1 sequence=21 RECID=14 STAMP=1088145096

input archived log thread=1 sequence=22 RECID=15 STAMP=1088145757

input archived log thread=1 sequence=23 RECID=16 STAMP=1088145772

input archived log thread=1 sequence=24 RECID=17 STAMP=1088148296

input archived log thread=1 sequence=25 RECID=18 STAMP=1088148660

input archived log thread=1 sequence=26 RECID=19 STAMP=1088148675

input archived log thread=1 sequence=27 RECID=20 STAMP=1088148871

input archived log thread=1 sequence=28 RECID=21 STAMP=1088148887

input archived log thread=1 sequence=29 RECID=22 STAMP=1088150482

input archived log thread=1 sequence=30 RECID=23 STAMP=1088150486

input archived log thread=1 sequence=31 RECID=24 STAMP=1088150489

input archived log thread=1 sequence=32 RECID=25 STAMP=1088150490

input archived log thread=1 sequence=33 RECID=26 STAMP=1088150492

input archived log thread=1 sequence=34 RECID=27 STAMP=1088150539

input archived log thread=1 sequence=35 RECID=28 STAMP=1088150562

input archived log thread=1 sequence=36 RECID=29 STAMP=1088150728

input archived log thread=1 sequence=37 RECID=30 STAMP=1088150743

input archived log thread=1 sequence=38 RECID=31 STAMP=1088150800

input archived log thread=1 sequence=39 RECID=32 STAMP=1088152564

input archived log thread=1 sequence=40 RECID=33 STAMP=1088152579

input archived log thread=1 sequence=41 RECID=34 STAMP=1088153341

channel ORA_DISK_1: starting piece 1 at 09-NOV-21

channel ORA_DISK_1: finished piece 1 at 09-NOV-21

piece handle=/data/TESTPDB/backup/180dnpnu_1_1 tag=TAG20211109T084902 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 09-NOV-21

 

Starting backup at 09-NOV-21

using channel ORA_DISK_1

channel ORA_DISK_1: starting full datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

input datafile file number=00001 name=/data/app/oracle/oradata/TESTDB/system01.dbf

input datafile file number=00003 name=/data/app/oracle/oradata/TESTDB/sysaux01.dbf

input datafile file number=00004 name=/data/app/oracle/oradata/TESTDB/undotbs01.dbf

input datafile file number=00007 name=/data/app/oracle/oradata/TESTDB/users01.dbf

channel ORA_DISK_1: starting piece 1 at 09-NOV-21

channel ORA_DISK_1: finished piece 1 at 09-NOV-21

piece handle=/data/TESTPDB/backup/190dnpnv_1_1 tag=TAG20211109T084903 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07

channel ORA_DISK_1: starting full datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

input datafile file number=00010 name=/data/app/oracle/oradata/TESTDB/TESTPDB/sysaux01.dbf

input datafile file number=00009 name=/data/app/oracle/oradata/TESTDB/TESTPDB/system01.dbf

input datafile file number=00011 name=/data/app/oracle/oradata/TESTDB/TESTPDB/undotbs01.dbf

input datafile file number=00012 name=/data/app/oracle/oradata/TESTDB/TESTPDB/users01.dbf

channel ORA_DISK_1: starting piece 1 at 09-NOV-21

channel ORA_DISK_1: finished piece 1 at 09-NOV-21

piece handle=/data/TESTPDB/backup/1a0dnpo7_1_1 tag=TAG20211109T084903 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03

channel ORA_DISK_1: starting full datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

input datafile file number=00006 name=/data/app/oracle/oradata/TESTDB/pdbseed/sysaux01.dbf

input datafile file number=00005 name=/data/app/oracle/oradata/TESTDB/pdbseed/system01.dbf

input datafile file number=00008 name=/data/app/oracle/oradata/TESTDB/pdbseed/undotbs01.dbf

channel ORA_DISK_1: starting piece 1 at 09-NOV-21

channel ORA_DISK_1: finished piece 1 at 09-NOV-21

piece handle=/data/TESTPDB/backup/1b0dnpoa_1_1 tag=TAG20211109T084903 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03

Finished backup at 09-NOV-21

 

Starting backup at 09-NOV-21

current log archived

using channel ORA_DISK_1

channel ORA_DISK_1: starting archived log backup set

channel ORA_DISK_1: specifying archived log(s) in backup set

input archived log thread=1 sequence=42 RECID=35 STAMP=1088153357

channel ORA_DISK_1: starting piece 1 at 09-NOV-21

channel ORA_DISK_1: finished piece 1 at 09-NOV-21

piece handle=/data/TESTPDB/backup/1c0dnpod_1_1 tag=TAG20211109T084917 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 09-NOV-21

 

Starting Control File and SPFILE Autobackup at 09-NOV-21

piece handle=/data/app/oracle/product/19C/dbhome_3/dbs/c-2870051281-20211109-04 comment=NONE

 

Finished Control File and SPFILE Autobackup at 09-NOV-21

 

Recovery process by date

Now we will prepare a suitable environment for recovery. For example, we will recover according to the date. We will check our database clock and delete a sample table. We want to return our database clock ”09/11/2021 08:50:25” to this hour. There is a directory named ”/data/tempdata/TESTPDB” on the file system. The database will temporarily use this location.

 

[oracle@dba 19C]$ sqlplus NAS2/abc123@TESTPDB

 

SQL*Plus: Release 19.0.0.0.0 – Production on Tue Nov 9 08:49:45 2021

Version 19.3.0.0.0

 

Copyright (c) 1982, 2019, Oracle.  All rights reserved.

 

Last Successful login time: Tue Nov 09 2021 08:47:14 +05:30

 

Connected to:

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 – Production

Version 19.3.0.0.0

 

SQL> alter session set nls_date_format = ‘dd/mm/yyyy hh24:mi:ss’;

 

Session altered.

 

SQL> select sysdate from dual;

 

SYSDATE

——————-

09/11/2021 08:50:25

Delete table

First of all, we delete our table according to the scenario.

SQL> drop table test;

 

Table dropped.

Starting recovery

We start recovery with history.

 

RMAN>  recover table ‘NAS2’.’TEST’of pluggable database TESTPDB until time “to_date(’09/11/2021 08:50:25′,’ dd/mm/yyyy hh24:mi:ss’)” AUXILIARY DESTINATION ‘/data/tempdata/TESTPDB’;

Using SCN

If you want to do it with SCN, you can use the code below.

 

SQL> SELECT DBMS_FLASHBACK.get_system_change_number FROM dual;

GET_SYSTEM_CHANGE_NUMBER

————————

                 2364401

recover table ‘NAS2′.’TEST’ of pluggable database TESTPDB UNTIL SCN 2364401 AUXILIARY DESTINATION ‘/data/tempdata/TESTPDB‘;

 

Recovery startup

We’re starting the recovery.

 

Starting recover at 09-NOV-21

using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 42 is already on disk as file /data/app/oracle/product/19C/dbhome             _3/dbs/arch/1_42_1086723155.dbf

archived log for thread 1 with sequence 43 is already on disk as file /data/app/oracle/product/19C/dbhome             _3/dbs/arch/1_43_1086723155.dbf

archived log file name=/data/app/oracle/product/19C/dbhome_3/dbs/arch/1_42_1086723155.dbf thread=1 sequen             ce=42

archived log file name=/data/app/oracle/product/19C/dbhome_3/dbs/arch/1_43_1086723155.dbf thread=1 sequen             ce=43

media recovery complete, elapsed time: 00:00:00

Finished recover at 09-NOV-21

sql statement: alter database open read only

contents of Memory Script:

{

sql clone ‘alter pluggable database  TESTPDB open read only’;

}

executing Memory Script

sql statement: alter pluggable database  TESTPDB open read only

contents of Memory Script:

{

   sql clone “create spfile from memory”;

   shutdown clone immediate;

   startup clone nomount;

   sql clone “alter system set  control_files =

  ”/data/tempdata/TESTPDB/TESTDB/controlfile/o1_mf_jrmx5bsg_.ctl” comment=

 ”RMAN set” scope=spfile”;

   shutdown clone immediate;

   startup clone nomount;

# mount database

sql clone ‘alter database mount clone database’;

}

executing Memory Script

sql statement: create spfile from memory

database closed

database dismounted

Oracle instance shut down

connected to auxiliary database (not started)

Oracle instance started

Total System Global Area    1157627656 bytes

Fixed Size                     9133832 bytes

Variable Size                301989888 bytes

Database Buffers             838860800 bytes

Redo Buffers                   7643136 bytes

sql statement: alter system set  control_files =   ”/data/tempdata/TESTPDB/TESTDB/controlfile/o1_mf_jrmx             5bsg_.ctl” comment= ”RMAN set” scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)

Oracle instance started

Total System Global Area    1157627656 bytes

Fixed Size                     9133832 bytes

Variable Size                301989888 bytes

Database Buffers             838860800 bytes

Redo Buffers                   7643136 bytes

sql statement: alter database mount clone database

contents of Memory Script:

{

# set requested point in time

set until  time “to_date(’09/11/2021 08:50:25′,’dd/mm/yyyy hh24:mi:ss’)”;

# set destinations for recovery set and auxiliary set datafiles

set newname for datafile  12 to new;

# restore the tablespaces in the recovery set and the auxiliary set

restore clone datafile  12;

switch clone datafile all;

}

executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

Starting restore at 09-NOV-21

allocated channel: ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: SID=34 device type=DISK

channel ORA_AUX_DISK_1: starting datafile backup set restore

channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set

channel ORA_AUX_DISK_1: restoring datafile 00012 to /data/tempdata/TESTPDB/EZSF_PITR_TESTPDB_TESTDB/CF06C             16067FD5873E053C500A8C0584B/datafile/o1_mf_users_%u_.dbf

channel ORA_AUX_DISK_1: reading from backup piece /data/TESTPDB/backup/1a0dnpo7_1_1

channel ORA_AUX_DISK_1: piece handle=/data/TESTPDB/backup/1a0dnpo7_1_1 tag=TAG20211109T084903

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01

Finished restore at 09-NOV-21

datafile 12 switched to datafile copy

input datafile copy RECID=17 STAMP=1088156497 file name=/data/tempdata/TESTPDB/EZSF_PITR_TESTPDB_TESTDB/C             F06C16067FD5873E053C500A8C0584B/datafile/o1_mf_users_jrmx7r4o_.dbf

contents of Memory Script:

{

# set requested point in time

set until  time “to_date(’09/11/2021 08:50:25′,’dd/mm/yyyy hh24:mi:ss’)”;

# online the datafiles restored or switched

sql clone ‘TESTPDB’ “alter database datafile

 12 online”;

# recover and open resetlogs

recover clone database tablespace  “TESTPDB”:”USERS”, “SYSTEM”, “TESTPDB”:”SYSTEM”, “UNDOTBS1”, “TESTPDB”             :”UNDOTBS1″, “SYSAUX”, “TESTPDB”:”SYSAUX” delete archivelog;

alter clone database open resetlogs;

}

executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  12 online

Starting recover at 09-NOV-21

using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 42 is already on disk as file /data/app/oracle/product/19C/dbhome             _3/dbs/arch/1_42_1086723155.dbf

archived log for thread 1 with sequence 43 is already on disk as file /data/app/oracle/product/19C/dbhome             _3/dbs/arch/1_43_1086723155.dbf

archived log file name=/data/app/oracle/product/19C/dbhome_3/dbs/arch/1_42_1086723155.dbf thread=1 sequen             ce=42

archived log file name=/data/app/oracle/product/19C/dbhome_3/dbs/arch/1_43_1086723155.dbf thread=1 sequen             ce=43

media recovery complete, elapsed time: 00:00:00

Finished recover at 09-NOV-21

database opened

contents of Memory Script:

{

sql clone ‘alter pluggable database  TESTPDB open’;

}

executing Memory Script

sql statement: alter pluggable database  TESTPDB open

contents of Memory Script:

{

# create directory for datapump import

sql ‘TESTPDB’ “create or replace directory

TSPITR_DIROBJ_DPDIR as ”

/data/tempdata/TESTPDB””;

# create directory for datapump export

sql clone ‘TESTPDB’ “create or replace directory

TSPITR_DIROBJ_DPDIR as ”

/data/tempdata/TESTPDB””;

}

executing Memory Script

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ”/data/tempdata/TESTPDB”

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ”/data/tempdata/TESTPDB”

Performing export of tables…

   EXPDP> Starting “SYS”.”TSPITR_EXP_Ezsf_qaFb”:

   EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA

   EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS

   EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE

   EXPDP> . . exported “NAS2″.”TEST”                               5.554 KB       4 rows

   EXPDP> Master table “SYS”.”TSPITR_EXP_Ezsf_qaFb” successfully loaded/unloaded

   EXPDP> ******************************************************************************

   EXPDP> Dump file set for SYS.TSPITR_EXP_Ezsf_qaFb is:

   EXPDP>   /data/tempdata/TESTPDB/tspitr_Ezsf_48446.dmp

   EXPDP> Job “SYS”.”TSPITR_EXP_Ezsf_qaFb” successfully completed at Tue Nov 9 09:42:13 2021 elapsed 0 00             :00:13

Export completed

contents of Memory Script:

{

# shutdown clone before import

shutdown clone abort

}

executing Memory Script

Oracle instance shut down

Performing import of tables…

   IMPDP> Master table “SYS”.”TSPITR_IMP_Ezsf_jgje” successfully loaded/unloaded

   IMPDP> Starting “SYS”.”TSPITR_IMP_Ezsf_jgje”:

   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE

   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA

   IMPDP> . . imported “NAS2″.”TEST”                               5.554 KB       4 rows

   IMPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS

   IMPDP> Job “SYS”.”TSPITR_IMP_Ezsf_jgje” successfully completed at Tue Nov 9 09:42:23 2021 elapsed 0 00             :00:04

Import completed

Removing automatic instance

Automatic instance removed

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/CF06C16067FD5873E053C500A8C0584B/datafile/o1_mf_tem             p_jrmx5x4v_.tmp deleted

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/datafile/o1_mf_temp_jrmx5vj0_.tmp deleted

auxiliary instance file /data/tempdata/TESTPDB/EZSF_PITR_TESTPDB_TESTDB/onlinelog/o1_mf_3_jrmx7twd_.log d             eleted

auxiliary instance file /data/tempdata/TESTPDB/EZSF_PITR_TESTPDB_TESTDB/onlinelog/o1_mf_2_jrmx7t2k_.log d             eleted

auxiliary instance file /data/tempdata/TESTPDB/EZSF_PITR_TESTPDB_TESTDB/onlinelog/o1_mf_1_jrmx7syq_.log d             eleted

auxiliary instance file /data/tempdata/TESTPDB/EZSF_PITR_TESTPDB_TESTDB/CF06C16067FD5873E053C500A8C0584B/             datafile/o1_mf_users_jrmx7r4o_.dbf deleted

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/CF06C16067FD5873E053C500A8C0584B/datafile/o1_mf_sys             aux_jrmx5qc6_.dbf deleted

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/datafile/o1_mf_sysaux_jrmx5j9n_.dbf deleted

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/CF06C16067FD5873E053C500A8C0584B/datafile/o1_mf_und             otbs1_jrmx5qc9_.dbf deleted

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/datafile/o1_mf_undotbs1_jrmx5j9p_.dbf deleted

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/CF06C16067FD5873E053C500A8C0584B/datafile/o1_mf_sys             tem_jrmx5qc8_.dbf deleted

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/datafile/o1_mf_system_jrmx5j9k_.dbf deleted

auxiliary instance file /data/tempdata/TESTPDB/TESTDB/controlfile/o1_mf_jrmx5bsg_.ctl deleted

auxiliary instance file tspitr_Ezsf_48446.dmp deleted

Finished recover at 09-NOV-21

RMAN>

 

Added instance

 

Transactions are completed. As you can see, Oracle is doing what we used to do manually in the past. As an example, you can see the added instance while performing the process on the screen below.

 

added instance

Export for table

 

Our table has been recovered with the two examples we have given successfully. 

[oracle@dba ~]$ sqlplus NAS2/abc123@TESTPDB

SQL*Plus: Release 19.0.0.0.0 – Production on Tue Nov 9 11:33:34 2021

Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.

Last Successful login time: Tue Nov 09 2021 09:02:13 +05:30

Connected to:

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 – Production

Version 19.3.0.0.0

SQL> select * from test;

    SERIAL NAME

———- —–

         1 One

         2 Two

         3 Three

         4 Four

SQL>

 

If you want, you can do not recover but export for this table.

 

RMAN> recover table’NAS2′.’TEST’  of pluggable database TESTPDB UNTIL SCN 2364401 AUXILIARY DESTINATION ‘/data/tempdata/TESTDB‘ DATAPUMP DESTINATION ‘/tempdata/export’   DUMP FILE ‘TEST_TABLE.dmp‘   NOTABLEIMPORT;

 

Recovery under a different name

Or you can recover the table with a different name.

 

RMAN> recover table’NAS2′.’TEST’ of pluggable database TESTPDB until time “to_date(’04/11/2020 22:59:12′,‘dd/mm/yyyy hh24:mi:ss’)” AUXILIARY DESTINATION ‘/data/tempdata/TESTPDB’ REMAP TABLE ‘NAS2′.’TEST’:’TEST_BCK’;

 

Different tablespace

Finally, as you can recover the table with a different name, you can get it on different tablespaces.

 

 

RMAN> recover table ‘NAS2.’TEST’ of pluggable database TESTPDB  until time “to_date(”04/11/2020 22:59:12”,’dd/mm/yyyy hh24:mi:ss’)” AUXILIARY DESTINATION ‘/data/tempdata/TESTDB’ REMAP TABLE ‘NAS2′.’TEST’:’EXPERT’TEST_BCK’ REMAP TABLESPACE ‘TS_NAS2′:’TS_OTHER’;

Follow me

Contact us for Training/ Job Support

Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.