Oracle by Example brandingManaging and Using Pluggable Database Snapshots in a Carousel

section 0Before 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

section 1Enable PDBs for the PDB Snapshot Carousel

  1. Log in to the CDB root.
    sqlplus / AS SYSDBA
  2. 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 using ALTER DATABASE SET MAX_PDB_SNAPSHOTS in a PDB.

  3. Create a directory for the  pdb_1 PDB.
    mkdir -p /u02/app/oracle/oradata/ORCL/pdb_1
  4. Create the PDB_1 in ORCL 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;  
  5. 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.

  6. 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
    
  7. Create a directory for the pdb_2 PDB.
    mkdir -p /u02/app/oracle/oradata/ORCL/pdb_2
  8. Create pdb_2 in ORCL 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;
  9. Open PDB_2.
    ALTER PLUGGABLE DATABASE pdb_2 OPEN;
  10. 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
    
  11. 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.


section 2Create Manual PDB Snapshots in a Carousel

  1. Log in to PDB_1.
    CONNECT sys@PDB_1 AS SYSDBA 
    Enter password: password
  2. Create the schema_app1 application account.
    CREATE USER schema_app1 IDENTIFIED BY password; 
  3. Grant privileges to schema_app1.
    GRANT create session, create table, unlimited tablespace TO schema_app1; 
  4. Create the schema_app1.tab1 table.
    CREATE TABLE schema_app1.tab1 (C number, label VARCHAR2(40));
  5. Insert rows in the schema_app1.tab1 table.
    INSERT INTO schema_app1.tab1 VALUES (1,'Label1');
    COMMIT;
  6. Create the first manual PDB snapshot for pdb_1.
    ALTER PLUGGABLE DATABASE pdb_1 SNAPSHOT pdb1_first_snap;
  7. 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
  8. Apply application changes to the table.
    INSERT INTO schema_app1.tab1 VALUES (2,'Label2');
    COMMIT;
  9. Create another table with rows.
    CREATE TABLE schema_app1.tab2 (C number, description VARCHAR2(40));
    INSERT INTO schema_app1.tab2 VALUES (1,'Description1');
    COMMIT;
  10. Create the second pdb1_second_snap PDB snapshot.
    ALTER PLUGGABLE DATABASE pdb_1 SNAPSHOT pdb1_second_snap;
  11. Verify that the second PDB snapshot is created. Read the list in code2 for typical output.
    SELECT con_name, snapshot_name, snapshot_scn, snapshot_time, full_snapshot_path 
    FROM   cdb_pdb_snapshots;
    
    After creating the second snapshot, the application still creates tables and loads data.
  12. Create another table with rows.
    CREATE TABLE schema_app1.tab3 (COL1 number, Description VARCHAR2(10));
    INSERT INTO schema_app1.tab3 VALUES (1,'Desc1');
    COMMIT;
  13. Leave PDB_1, and check that PDB snapshots are automatically created for pdb_2. Log in to PDB_2.
    CONNECT sys@PDB_2 AS SYSDBA 
    Enter password: password
  14. 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;
  15. 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;
  16. 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.


section 3Disable PDB Snapshot Creation in a Carousel

  1. 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.

  2. Verify that PDB_2 is disabled for PDB snapshot creation.
    SELECT pdb_name, snapshot_mode FROM cdb_pdbs;
    
    PDB_NAME SNAPSH
    -------- ------
    PDB_2    NONE
  3. Quit the session.
    EXIT

section 4Create 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.

  1. Create a directory for the pdb1_from_first_snap PDB.
    mkdir -p /u02/app/oracle/oradata/ORCL/pdb1_from_first_snap
  2. Log in to the CDB root.
    sqlplus / AS SYSDBA 
  3. Create the pdb1_from_first_snap PDB from the pdb1_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;
  4. Open pdb1_from_first_snap.
    ALTER PLUGGABLE DATABASE pdb1_from_first_snap OPEN;
    
  5. Connect to pdb1_from_first_snap.
    CONNECT sys@pdb1_from_first_snap AS SYSDBA
    Enter password: password
  6. To verify that the data corresponds to the application at the pdb1_first_snap time, view the schema_app1.tab1 table.
    SELECT * FROM schema_app1.tab1;
             C LABEL
    ---------- ----------------------------------------
             1 Label1
  7. 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
  8. 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.

  9. Connect to the CDB root.
    CONNECT / AS SYSDBA 
  10. Create a directory for the pdb1_from_second_snap PDB.
    HOST mkdir -p /u02/app/oracle/oradata/ORCL/pdb1_from_second_snap
  11. Create the pdb1_from_second_snap PDB from the pdb1_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;
  12. Open pdb1_from_second_snap.
    ALTER PLUGGABLE DATABASE pdb1_from_second_snap OPEN;
    
  13. Connect to pdb1_from_second_snap.
    CONNECT sys@pdb1_from_second_snap AS SYSDBA
    Enter password: password
  14. To verify that the data corresponds to the application at pdb1_second_snap time, view the schema_app1.tab1 table.
    SELECT * FROM schema_app1.tab1;
    
             C LABEL
    ---------- ----------------------------------------
             1 Label1
             2 Label2
  15. View the schema_app1.tab2 table.
    SELECT * FROM schema_app1.tab2;
    
             C DESCRIPTION
    ---------- ----------------------------------------
             1 Description1

section 5Drop PDB Snapshots of a Carousel

  1. Connect to PDB_1.
    CONNECT sys@PDB_1 AS SYSDBA
    Enter password: password
  2. Drop the pdb1_first_snap PDB snapshot.
    ALTER PLUGGABLE DATABASE pdb_1 DROP SNAPSHOT PDB1_FIRST_SNAP;
  3. 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 the USING SNAPSHOT clause, then the SNAPSHOT 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
    
  4. Quit the session.
    EXIT