All technological notes.
Flashback DatabaseFlashback Database
Flashback DatabaseFlashback Database:
point-in-time recovery capability, which enables the database to be quickly “rewound” to a previous point in time.Features:
FLASHBACK DATABASE TO '2:05 PM'flashback logs
Flashback Database to record old block versions.
- When writes are issued to disk and Flashback Database is enabled:
- old block version is written to the flashback log
- the new block version is written to the data file
FLASHBACK DATABASE command is issued, only the changed blocks are retrieved from the flashback logs.
archived logs to the required point in time.
With Flashback Database, you can quickly bring your database to an earlier point in time by undoing all the changes that have taken place since that time.
flashback logs.Flashback logs are written sequentially and are not archived.Oracle Database server automatically creates, deletes, and resizes flashback logs in the fast recovery area.
before images in the flashback logs are used only to restore the database to a point in the past, and forward recovery is used to bring the database to a consistent state at some time in the past.
When you enable Flashback Database, the RVWR (Flashback Writer) background process is started.
Flashback Database data from the flashback buffer to the Flashback Database logs, which are circularly reused.FLASHBACK DATABASE command is issued, the flashback logs are used to restore to the blocks’ before images, and then redo data is used to roll forward to the desired flashback time.The overhead of enabling Flashback Database depends on the read/write mix of the database workload.
CDB:
CDB root without flashing back the entire CDB.CDB 整体性PDB:
PDB.datafiles belonging to a PDB can be flashed back and recovered in place.undo application that is needed to make a PDB consistent after flashback is applied in place.PDB flashback operation, the old backup of the PDB is still valid. 闪回后,旧备份还可用.CONN sys@pdbl
-- close pdb
ALTER PLUGGABLE DATABASE CLOSE;
-- flashback pdb to a scn
FLASHBACK PLUGGABLE DATABASE pdbl TO SCN 411010;
-- open pdb resetlogs
ALTER PLUGGABLE DATABASE pdbl OPEN RESETLOGS;
OPEN RESETLOGScreates a new incarnation for thePDB.- The
CDBroot must be opened when the PDBOPEN RESETLOGSis performed, except that recovering a PDB to a point in time does not affect all parts of theCDB. The whole CDB and all other PDBs are opened.- After recovering a PDB to a specified point in time, when you open the PDB by using the RESETLOGS option, a new incarnation of the PDB is created.
PDB RESETLOGS 原理:
RESETLOGS option does not perform a RESETLOGS for the CDB.独立性
control file is updated.Each redo log record carries the PDB ID in the redo header.头会记录 pdb_ID
OPEN RESETLOGS is similar to a database OPEN RESETLOGS.V$PDB_INCARNATION view:
Alternative solution
Flashback Database feature, you should use an incomplete recovery operation to return the database to a specific time. 如果不能闪回, 则执行 PITRWhen the Flashback Database operation completes, open the database:
RESETLOGS operation to allow DMLcontrol file has been restored or re-created.tablespace has been dropped.
Flashback Database to recover a data file that was dropped during the span of time you are flashing back.control file and marked offline, but it is not flashed back.data file has been reduced in size. 减少不能data files that you have configured for automatic extension. 自动扩展可以Use the TO BEFORE RESETLOGS clause to flash back to before the last RESETLOGS operation.
flashback retention target is not an absolute guarantee that flashback will be available.
fast recovery area, flashback logs may be deleted automatically. 如果 fra 空间不足, log 可能被删除fast recovery area to know how well you are meeting your retention target. FRA 的空间大小直接影响 retention targetV$FLASHBACK_DATABASE_LOG view
monitor the Flashback Database retention target:
ESTIMATED_FLASHBACK_SIZE:
fast recovery area for flashback logs to meet the current flashback retention target.The estimate is based on the workload since the instance was started or during the most recent time interval equal to the flashback retention target, whichever is shorter.
FLASHBACK_SIZE:
OLDEST_FLASHBACK_SCN and OLDEST FLASHBACK_TIME:
-- View the fast recovery area disk quota:
SELECT estimated_flashback_size, flashback_size
FROM V$FLASHBACK_DATABASE_LOG;
-- Determine the current flashback window:
SELECT oldest _flashback_scn, oldest_flashback_time
FROM V$FLASHBACK DATABASE_LOG;
V$FLASHBACK_DATABASE_STAT view
-- Monitor logging in the Flashback Database logs:
SELECT *
FROM V$FLASHBACK DATABASE_STAT;
Example:

retention time or the fast recovery area size.FLASHBACK_DATA and REDO_DATA represent the number of bytes of flashback data and redo data written, respectively, during the time intervalDB_DATA gives the number of bytes of data blocks read and written.V$RECOVERY_FILE_DEST view:
| Column | Description |
|---|---|
NAME |
Fast recovery area name, indicating location string |
SPACE_LIMIT |
Disk limit specified in the DB_RECOVERY_FILE_DEST_SIZE parameter |
SPACE_USED |
Space used by fast recovery area files (in bytes) |
SPACE_RECLAIMABLE |
Amount of space that can be reclaimed by deleting obsolete, redundant, and other low-priority files through the space management algorithm |
NUMBER_OF_FILES |
Number of files |
Flashback DatabaseConfiguaration to enable flashback features for an application:
FLASHBACK privileges.
undo settings:
UNDO_TABLESPACE='UNDOTBSL1': Setup undo tbspUNDO_MANAGEMENT='AUTO'UNDO_RETENTION=900: define what is the amount of time to keep data in your undo tablespaceHave an undo tablespace with enough space to keep the required data for flashback operations.
Undo retention:
undo retention, specified by the UNDO_RETENTION parameter.V$UNDOSTAT.TUNED_UNDORETENTION:
SELECT TUNED_UNDORETENTION
FROM V$UNDOSTAT
WHERE rownum = 1;
UNDO_RETENTION parameter:
900show parameter undo_retention
-- NAME TYPE VALUE
-- -------------- ------- -----
-- undo_retention integer 14400
ALTER SYSTEM SET UNDO_RETENTION=900 SCOPE=BOTH;
Setting the UNDO_RETENTION parameter does not guarantee that unexpired undo data is not overwritten.
guaranteeing retention.事务优先于查询, 除非保证保留guaranteed retention:
undo retention settings are enforced even if they cause transactions to fail.

retention guarantee, ongoing operations that need undo space in the segments of the tablespace may fail due to lack of space.To satisfy long retention requirements, create a Temporal History.
RETENTION GUARANTEE:
-- Enable guaranteed retention
ALTER TABLESPACE undotbsl RETENTION GUARANTEE;
-- Disable guaranteed retention
ALTER TABLESPACE undotbsl RETENTION NOGUARANTEE;
Flashback Database Configuration:
Set the retention target with the DB_FLASHBACK_RETENTION_TARGET initialization parameter.
flashback time interval depends on how much flashback data has been kept in the fast recovery area.-- uses 2,880 minutes, which is equivalent to two days.
ALTER SYSTEM
SET DB_FLASHBACK_RETENTION_TARGET=2880 SCOPE=BOTH;
Enable Flashback Database
-- enable flashback database
ALTER DATABASE FLASHBACK ON;
-- disable flashback database
ALTER DATABASE FLASHBACK OFF;
-- query whether Flashback Database is enabled
SELECT flashback_on
FROM v$database;
FLASHBACK DATABASE command:
RMAN Command:
SEQUENCE and THREAD option:
redo log sequence number and thread as a lower limit.-- rman, moun the database
-- to time
FLASHBACK DATABASE TO TIME = "TO_DATE('2009-05-27 16:00:00", 'YYYY-MM-DD HH24:MI:SS')";
-- to scn
FLASHBACK DATABASE TO SCN=23565;
-- to sequence and thread
FLASHBACK DATABASE TO SEQUENCE=223 THREAD=1;
-- sql
-- to timestampe
FLASHBACK DATABASE TO TIMESTAMP (SYSDATE-1/24) ;
-- to scn
FLASHBACK DATABASE TO SCN 53943;
-- to restore point
FLASHBACK DATABASE TO RESTORE POINT bd_load;
-- To review changes: Open in read-only mode.
-- To finalize: Open in read/write mode with RESETLOGS.
Guaranteed Restore PointsGuaranteed Restore Points:
restore points vs guaranteed restore points:
guaranteed restore points never age out of the control file 不会自动过期guaranteed restore points must be explicitly dropped. 必须显示删除Creating a guaranteed restore point at a particular SCN enforces the requirement强制要求 that you can perform a Flashback Database operation to return your database to its state at that SCN, even if flashback logging is not enabled for your database. 即使没有 logging, 都强制要求可以闪回到那个点.
flashback logging is enabled, creating a guaranteed restore point enforces the retention of flashback logs required for Flashback Database back to any point in time after the creation of the earliest guaranteed restore point.可以返回最早返回点之后任意时间点.guaranteed restore point can be used to revert a whole database to a known good state days or weeks ago, as long as there is enough disk space in the fast recovery area to store the needed logs.只要空间足够, 可以长达数周As with normal restore points, guaranteed restore points can be used to specify a point in time for RECOVER DATABASE operations. 可以用作恢复点
CREATE RESTORE POINT before upgrade
GUARANTEE FLASHBACK DATABASE;
Prerequisites to use guaranteed restore points:
ARCHIVELOG mode.FLASHBACK DATABASE requires the use of archived redo logs starting from the time of the restore point.fast recovery area must be configured.
When creating the first guaranteed restore point (or if all previously created guaranteed restore points have been dropped) and Flashback Database is not enabled
Logging for Flashback Database and guaranteed restore points involves capturing images of data file blocks before changes are applied.
The main difference between normal flashback logging and logging for guaranteed restore points is whether the logs can be deleted in response to space pressure in the fast recovery area. 普通的闪回 logging 会被删除; 保证恢复点的 logging 不会, 即使 fra 空间不够.
guaranteed restore points, then the database performs normal flashback logging.
flashback logs required to flash back to any arbitrary time between the present and the earliest currently defined guaranteed restore point.Flashback logs are not deleted in response to space pressure if they are required to satisfy the guarantee.PDB normal and guaranteed restore points are restore points that pertain to only a specific PDB and are visible within the PDB only. 只作用于 PDB
PDB restore point is essentially a bookmark for a past point for that PDB.guaranteed PDB restore point guarantees a PDB flashback to that restore point.PDB flashback uses PDB point-in-time recovery to recover the shared undo.shared undo and certain tablespaces in a clone instance and recovers the data to the same point in time.clean PDB restore point:
shared undo mode.
clean restore point is faster than other types of flashbackBenefits:
CDB restore point:
FOR PLUGGABLE DATABASE clause.If the PDB is the application root of an application container, all restore points are “clean” with local undo
Caution: PDB guaranteed restore points can potentially result in Oracle running out of space in the FRA. The DBA should remove PDB guaranteed restore points when the PDB flashback operations are completed. 必须在恢复后删除保证点
Example:
-- sql
CONNECT / AS SYSDBA
ALTER PLUGGABLE DATABASE pdbl CLOSE;
-- create clean restore point
CREATE CLEAN RESTORE POINT start_stepl
FOR PLUGGABLE DATABASE pdbl
GUARANTEE FLASHBACK DATABASE;
ALTER PLUGGABLE DATABASE pdbl OPEN;
/*
some issues
*/
ALTER PLUGGABLE DATABASE pdbl CLOSE;
-- rman
rman target /
FLASHBACK PLUGGABLE DATABASE pdbl TO RESTORE POINT start_stepl;
ALTER PLUGGABLE DATABASE pdbl OPEN RESETLOGS:
fast recovery area storage.
ASM, configure enough disk spindles, and so forth.V$FLASHBACK_DATABASE_LOG to size log space, after running workload that runs longer than the flashback retention period.Create guaranteed restore point (GRP) without enabling flashback logging.
To achieve good performance for large production databases by using Flashback Database, Oracle recommends the following:
fast recovery area, preferably without operating system file caching.
fast recovery area, including flashback logs, are typically large.fast recovery area does not have nonvolatile RAM, try to configure the file system on top of striped storage volumes with a relatively small stripe size such as 128 K.
flashback logs to be spread across multiple spindles, thereby improving performance.LOG_BUFFER initialization parameter to at least 8 MB.
flashback database logs.Flashback retention should be set to at least 60 minutes.
restore point is a user-defined name that is associated with a database point in time.
guaranteed restore point (GRP) is a special type of restore point that ensures flashback logs are kept until the restore point is used or deleted.
flashback logs are generally created in proportion to archived redo logs generated during the same retention period.GRP is created, each changed block is logged only once to maintain the GRP.GRP for fast recovery of a specific set of transactions such as a batch job and then delete the GRP to reclaim the space.sqlplus / as sysdba
SELECT flashback_on
FROM v$database;

sqlplus / as sysdba
-- enable flashback
ALTER DATABASE FLASHBACK ON;
-- query
SELECT flashback_on
FROM v$database;

rman target "'/ as sysbackup'"
BACKUP DATABASE PLUS ARCHIVELOG DELETE INPUT;
delete noprompt obsolete;
list failure;
exit
-- create guarenteed restore point
CREATE RESTORE POINT rpl GUARANTEE FLASHBACK DATABASE;
-- confirm
SELECT NAME, SCN, TIME, DATABASE_INCARNATION#,
GUARANTEE_FLASHBACK_DATABASE,STORAGE_SIZE
FROM V$RESTORE_POINT;

sqlplus / as sysdba
ALTER SESSION set container=orclpdb;
SELECT current_scn
FROM v$database;
-- 8767254
SELECT sum (salary)
FROM hr.employees;
SELECT count (*)
FROM hr.employees
WHERE department_id=90;
update hr.employees
set department_id = 90
where job_id = 'IT_PROG';
-- 5 rows updated.
update hr.employees e
set salary = least(e.salary,
(select (min_salary + max_salary)/2 * 1.10
from hr.jobs j
where j.job_id = e.job_id))
where job_id not like 'AD_%';
-- 103 rows updated.
COMMIT;
SELECT current_scn
FROM v$database;
-- 8767447
SELECT sum(salary)
FROM hr.employees;
-- 679092.4
SELECT count(*)
FROM hr.employees
WHERE department_id=90;
-- 8
ALTER SESSION set container=cdb$root;
ALTER PLUGGABLE DATABASE orclpdb CLOSE;
FLASHBACK PLUGGABLE DATABASE orclpdb TO SCN 8767254;
-- Attempt to open the pluggable database orclpdbl.
ALTER PLUGGABLE DATABASE orclpdb OPEN;
/* Error starting at line : 87 in command -
ALTER PLUGGABLE DATABASE orclpdb OPEN
Error report -
ORA-01113: file 109 needs media recovery
ORA-01110: data file 109: '/u01/app/oracle/oradata/ORCL/orclpdb/bctbs01.dbf'
01113. 00000 - "file %s needs media recovery"
*Cause: An attempt was made to online or open a database with a file that
is in need of media recovery.
*Action: First apply media recovery to the file. */
-- optional: open in read-only mode to verify
ALTER PLUGGABLE DATABASE orclpdb OPEN READ ONLY;
ALTER SESSION set container=orclpdb;
-- do som query to verify
-- Open the pluggable database orclpdbl using the reset logs option.
ALTER PLUGGABLE DATABASE orclpdb OPEN RESETLOGS;
ALTER SESSION set container = orclpdb;
show con_name
SELECT sum(salary)
FROM hr.employees;
-- 691416
SELECT count(*)
FROM hr.employees
WHERE department_id=90;
-- 3
ALTER SESSION set container=cdb$root;
-- drop restore point
DROP RESTORE POINT rpl;
-- note: rpl is in cdb root.
-- confirm
SELECT NAME, SCN, TIME, DATABASE_INCARNATION#,
GUARANTEE_FLASHBACK_DATABASE,STORAGE_SIZE
FROM V$RESTORE_POINT;