Before You Begin
This 15-minute tutorial shows you how to create pluggable database (PDB) snapshots in a carousel. It also shows you how to use those PDB snapshots to create new PDBs, and therefore view data as it was in the past.
Background
This tutorial demonstrates how you can create and alter PDBs by
using the new SNAPSHOT
clause to manage
snapshot carousels. You create four PDBs: PDB_1, PDB_2,
PDB1_FROM_FIRST_SNAP,
and
PDB1_FROM_SECOND_SNAP.
What Do You Need?
- Oracle Database 18c installed
- A container database (CDB)
TNSNAMES.ora
adjusted to provide connectivity to these PDBs
Enable
PDBs for the PDB Snapshot Carousel
- Log in to the CDB root.
sqlplus / AS SYSDBA
- Check the maximum number of PDB snapshots that you can
create for each PDB in a carousel.
SELECT property_name, property_value FROM database_properties WHERE property_name LIKE '%SNAP%'; PROPERTY_NAME PROPERTY_VALUE ---------------------------------------- -------------------- MAX_PDB_SNAPSHOTS 8
The
MAX_PDB_SNAPSHOTS
parameter is already set to its maximum permitted value. Because snapshots can take large amounts of disk space, you can reduce the value by usingALTER DATABASE SET MAX_PDB_SNAPSHOTS
in a PDB. - Create a directory for the
pdb_1
PDB.mkdir -p /u02/app/oracle/oradata/ORCL/pdb_1
- Create the
PDB_1
inORCL
and enable it for manual PDB snapshot creation.CREATE PLUGGABLE DATABASE pdb_1 ADMIN USER pdb_1_admin IDENTIFIED BY password ROLES=(CONNECT) CREATE_FILE_DEST='/u02/app/oracle/oradata/ORCL/pdb_1' SNAPSHOT MODE MANUAL;
- Open
PDB_1.
ALTER PLUGGABLE DATABASE pdb_1 OPEN;
By default, new PDBs are the manual PDB snapshot type. The
SNAPSHOT MODE MANUAL
clause isn't necessary. - Verify that
PDB_1
is enabled for manual PDB snapshot creation.SELECT pdb_name, snapshot_mode FROM cdb_pdbs; PDB_NAME SNAPSH -------- ------ PDB$SEED MANUAL PDB_1 MANUAL
- Create a directory for the
pdb_2
PDB.mkdir -p /u02/app/oracle/oradata/ORCL/pdb_2
- Create
pdb_2
inORCL
and enable it for automatic PDB snapshot creation. The PDB snapshots are automatically created every two minutes.CREATE PLUGGABLE DATABASE pdb_2 ADMIN USER pdb_2_admin IDENTIFIED BY password ROLES=(CONNECT) CREATE_FILE_DEST='/u02/app/oracle/oradata/ORCL/pdb_2' SNAPSHOT MODE EVERY 2 MINUTES;
- Open
PDB_2.
ALTER PLUGGABLE DATABASE pdb_2 OPEN;
- Verify that
PDB_2
is enabled for automatic PDB snapshot creation.SELECT pdb_name, snapshot_mode FROM cdb_pdbs; PDB_NAME SNAPSH -------- ------ PDB$SEED MANUAL PDB_1 MANUAL PDB_2 AUTO
- After two minutes, verify that PDB snapshots are
automatically created for
pdb_2.
Refer to the listing in code1 for an example of the output.SELECT con_name, snapshot_name, snapshot_scn, snapshot_time, full_snapshot_path FROM CDB_PDB_SNAPSHOTS;
What is the PDB snapshot type? The PDB snapshot is an archive file (
.pdb
) containing the contents of the PDB copy at snapshot creation.
Create
Manual PDB Snapshots in a Carousel
- Log in to
PDB_1.
CONNECT sys@PDB_1 AS SYSDBA Enter password: password
- Create the
schema_app1
application account.CREATE USER schema_app1 IDENTIFIED BY password;
- Grant privileges to
schema_app1.
GRANT create session, create table, unlimited tablespace TO schema_app1;
- Create the
schema_app1.tab1
table.CREATE TABLE schema_app1.tab1 (C number, label VARCHAR2(40));
- Insert rows in the
schema_app1.tab1
table.INSERT INTO schema_app1.tab1 VALUES (1,'Label1'); COMMIT;
- Create the first manual PDB snapshot for
pdb_1.
ALTER PLUGGABLE DATABASE pdb_1 SNAPSHOT pdb1_first_snap;
- Verify that the PDB snapshot is created.
SELECT con_name, snapshot_name, snapshot_scn, snapshot_time, full_snapshot_path FROM cdb_pdb_snapshots; CON_NAME SNAPSHOT_NAME SNAPSHOT_SCN SNAPSHOT_TIME -------- ------------------------- ------------ ------------- FULL_SNAPSHOT_PATH -------------------------------------------------------------------- PDB_1 PDB1_FIRST_SNAP 6103650 1516072836 /u02/app/oracle/oradata/ORCL/pdb_1/ORCL/62DC9DAB24F24794E0532133960A9CF4/datafile/snap_2828879257_6103650.pdb
- Apply application changes to the table.
INSERT INTO schema_app1.tab1 VALUES (2,'Label2'); COMMIT;
- Create another table with rows.
CREATE TABLE schema_app1.tab2 (C number, description VARCHAR2(40)); INSERT INTO schema_app1.tab2 VALUES (1,'Description1'); COMMIT;
- Create the second
pdb1_second_snap
PDB snapshot.ALTER PLUGGABLE DATABASE pdb_1 SNAPSHOT pdb1_second_snap;
- Verify that the second PDB snapshot is created. Read the
list in code2
for typical output.
After creating the second snapshot, the application still creates tables and loads data.SELECT con_name, snapshot_name, snapshot_scn, snapshot_time, full_snapshot_path FROM cdb_pdb_snapshots;
- Create another table with rows.
CREATE TABLE schema_app1.tab3 (COL1 number, Description VARCHAR2(10)); INSERT INTO schema_app1.tab3 VALUES (1,'Desc1'); COMMIT;
- Leave
PDB_1,
and check that PDB snapshots are automatically created forpdb_2.
Log in toPDB_2.
CONNECT sys@PDB_2 AS SYSDBA Enter password: password
- Check that PDB snapshots are created. Read the list in code3 for typical
output.
SELECT con_name, snapshot_name, snapshot_scn, snapshot_time, full_snapshot_path FROM cdb_pdb_snapshots;
- You can also create manual PDB snapshots for PDBs enabled
for automatic PDB snapshots. Create the
PDB2_MANUAL_SNAP
manual snapshot.ALTER PLUGGABLE DATABASE pdb_2 SNAPSHOT pdb2_manual_snap;
- Check that the manual PDB snapshot is created for
pdb_2.
Read the list in code4 for typical output.SELECT con_name, snapshot_name, snapshot_scn, snapshot_time, full_snapshot_path FROM cdb_pdb_snapshots;
Why are there still only eight PDB snapshots although more were created (see snapshot_SCN)? You can create a maximum number of eight PDB snapshots for a PDB. The oldest snapshot was dropped so that you can create a new snapshot. It works on a FIFO basis.
Disable
PDB Snapshot Creation in a Carousel
- Stop the PDB snapshot creation for
pdb_2
.ALTER PLUGGABLE DATABASE pdb_2 SNAPSHOT MODE NONE;
Are PDB snapshots that you created before this operation still available and usable? Yes. Read the list in code4.
SELECT con_name, snapshot_name, snapshot_scn, snapshot_time, full_snapshot_path FROM CDB_PDB_SNAPSHOTS;
You can flash back to the time or System Change Number (SCN) available from these snapshots.
- Verify that
PDB_2
is disabled for PDB snapshot creation.SELECT pdb_name, snapshot_mode FROM cdb_pdbs; PDB_NAME SNAPSH -------- ------ PDB_2 NONE
- Quit the session.
EXIT
Create
PDBs From PDB Snapshots of a Carousel
A user asks you to report information about the application at
the pdb1_first_snap
time. To do that, you
create a PDB from the pdb1_first_snap
PDB
snapshot.
- Create a directory for the
pdb1_from_first_snap
PDB.mkdir -p /u02/app/oracle/oradata/ORCL/pdb1_from_first_snap
- Log in to the CDB root.
sqlplus / AS SYSDBA
- Create the
pdb1_from_first_snap
PDB from thepdb1_first_snap
PDB snapshot.ALTER SESSION SET db_create_file_dest='/u02/app/oracle/oradata/ORCL/pdb1_from_first_snap';
CREATE PLUGGABLE DATABASE pdb1_from_first_snap FROM pdb_1 USING SNAPSHOT pdb1_first_snap;
- Open
pdb1_from_first_snap.
ALTER PLUGGABLE DATABASE pdb1_from_first_snap OPEN;
- Connect to
pdb1_from_first_snap.
CONNECT sys@pdb1_from_first_snap AS SYSDBA Enter password: password
- To verify that the data corresponds to the application at
the
pdb1_first_snap
time, view theschema_app1.tab1
table.SELECT * FROM schema_app1.tab1; C LABEL ---------- ---------------------------------------- 1 Label1
- View the
schema_app1.tab2
table.SELECT * FROM schema_app1.tab2; SELECT * FROM schema_app1.tab2 * ERROR at line 1: ORA-00942: table or view does not exist
- Connect to the CDB root.
CONNECT / AS SYSDBA
- Create a directory for the
pdb1_from_second_snap
PDB.HOST mkdir -p /u02/app/oracle/oradata/ORCL/pdb1_from_second_snap
- Create the
pdb1_from_second_snap
PDB from thepdb1_second_snap
PDB snapshot.ALTER SESSION SET db_create_file_dest='/u02/app/oracle/oradata/ORCL/pdb1_from_second_snap';
CREATE PLUGGABLE DATABASE pdb1_from_second_snap FROM pdb_1 USING SNAPSHOT pdb1_second_snap;
- Open
pdb1_from_second_snap.
ALTER PLUGGABLE DATABASE pdb1_from_second_snap OPEN;
- Connect to
pdb1_from_second_snap.
CONNECT sys@pdb1_from_second_snap AS SYSDBA Enter password: password
- To verify that the data corresponds to the application at
pdb1_second_snap
time, view theschema_app1.tab1
table.SELECT * FROM schema_app1.tab1; C LABEL ---------- ---------------------------------------- 1 Label1 2 Label2
- View the
schema_app1.tab2
table.SELECT * FROM schema_app1.tab2; C DESCRIPTION ---------- ---------------------------------------- 1 Description1
The user asks you to report information about
the application at the pdb1_second_snap
time.
To do that, you create a PDB from the
pdb1_second_snap
PDB snapshot.
Drop
PDB Snapshots of a Carousel
- Connect to PDB_1.
CONNECT sys@PDB_1 AS SYSDBA Enter password: password
- Drop the
pdb1_first_snap PDB snapshot.
ALTER PLUGGABLE DATABASE pdb_1 DROP SNAPSHOT PDB1_FIRST_SNAP;
- Drop the
pdb1_second_snap PDB snapshot.
ALTER PLUGGABLE DATABASE pdb_1 DROP SNAPSHOT PDB1_SECOND_SNAP;
Do the PDBs created from the dropped snapshots still exist? Yes, they still exist because PDB snapshots are created as full clones. You don't have to materialize a PDB snapshot in a carousel. Note: If you use the
SNAPSHOT COPY
clause with theUSING SNAPSHOT
clause, then theSNAPSHOT COPY
clause is ignored.CONNECT / AS SYSDBA
SHOW PDBS CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 4 PDB_1 READ WRITE NO 5 PDB_2 READ WRITE NO 7 PDB1_FROM_FIRST_SNAP READ WRITE NO 8 PDB1_FROM_SECOND_SNAP READ WRITE NO
- Quit the session.
EXIT