Oracle by Example brandingSwitching Over a Refreshable Cloned PDB

section 0Before You Begin

This 15-minute tutorial shows you how to reverse the roles of a primary pluggable database (PDB) and its refreshable clone PDB. The refreshable clone PDB can be made the primary PDB while the primary PDB would become the refreshable clone.

Background

Oracle Database 12c allows a cloned PDB to be automatically refreshed every n minutes from the source PDB.

Oracle Database 18c allows a cloned PDB automatically refreshed from a source PDB to be reversed to the primary PDB, and the primary PDB be reversed to the automatically refreshable cloned PDB.

What Do You Need?

  • Familiarity with Oracle Database 12c automatic refreshable clone PDBs
  • Oracle Database 18c installed
  • A container database (CDB)

section 1Configure the Primary PDB

In this section, you'll create a database link to connect to PDB_REFRESHED in ORCL. This session is called Primary Session.

  1. In ORCL, create the regular PDB1 to create the database link to connect to the future refreshable PDB in ORCL at a later point in time. First, create a directory for PDB
    mkdir /u02/app/oracle/oradata/ORCL/pdb1
  2. Connect to the CDB root.
    sqlplus sys@ORCL AS SYSDBA
    Enter password: password
  3. Before you create PDB1, drop PDB1.
    ALTER PLUGGABLE DATABASE ALL CLOSE;
    DROP PLUGGABLE DATABASE pdb1 INCLUDING DATAFILES;
  4. Create PDB1.
    ALTER SESSION SET DB_CREATE_FILE_DEST='/u02/app/oracle/oradata/ORCL/pdb1';
    CREATE PLUGGABLE DATABASE pdb1
           ADMIN USER pdb_admin IDENTIFIED BY password
           ROLES=(CONNECT)
           CREATE_FILE_DEST='/u02/app/oracle/oradata/ORCL/pdb1';
  5. Open PDB1.
    ALTER PLUGGABLE DATABASE pdb1 OPEN;
  6. Create in ORCL a common user.
    DROP USER c##u1 CASCADE;
    CREATE USER c##u1 IDENTIFIED BY password CONTAINER=ALL;
  7. Grant the common user the SYSOPER administrative system privilege and other privileges.
    GRANT create session, resource, create any table, unlimited tablespace, sysoper, 
          create pluggable database TO c##u1 CONTAINER = ALL;
    
  8. in ORCL, create a public fixed-user database link that relies on the common user that you created in a previous step.
    DROP PUBLIC DATABASE LINK link_ORCL;
    CREATE PUBLIC DATABASE LINK link_ORCL CONNECT TO c##u1 IDENTIFIED BY password 
           USING 'ORCL';
    
  9. In the primary PDB, create a USERSWITCH.BIGTAB table with 10000 rows. First connect to PDB1.
    CONNECT system@PDB1
    Enter password: password
  10. Create a local user.
    SET sqlprompt "SQL_prim> "
    DROP USER userswitch CASCADE;
    CREATE USER userswitch IDENTIFIED BY password;
  11. Grant the user system privileges.
    GRANT dba, unlimited tablespace TO userswitch;
  12. Create the USERSWITCH.BIGTAB table.
    CREATE TABLE userswitch.bigtab (label varchar2(30));
  13. Load rows into USERSWITCH.BIGTAB table.
    BEGIN FOR i IN 1..10000 LOOP
        INSERT INTO userswitch.bigtab VALUES ('DATA FROM PDB1');
        COMMIT; 
        END LOOP;
        END;
    /
  14. Count the rows.
    SELECT count(*) FROM userswitch.bigtab;
    
    COUNT(*)
    ----------
         10000
  15. Display the data in the rows.
    SELECT DISTINCT label FROM userswitch.bigtab;
    
    LABEL
    ------------------------------
    DATA FROM PDB1

section 2Create the Refreshable Cloned PDB

In this section, you'll create the PDB_REFRESHED PDB automatically refreshed from PDB1 every 2 minutes. This session is called Refresh Session.

  1. Clone the PDB_REFRESHED PDB from PDB1 while PDB1 is still up and fully functional. First create the directory for the PDB_REFRESHED PDB.
    mkdir -p /u02/app/oracle/oradata/ORCL/pdb_refreshed
  2. Connect to the CDB root.
    sqlplus sys@ORCL AS SYSDBA
    Enter password: password
  3. Create the PDB_REFRESHED PDB.
    SET sqlprompt "SQL_refresh> "
    DROP PLUGGABLE DATABASE pdb_refreshed INCLUDING DATAFILES;
    ALTER SESSION SET DB_CREATE_FILE_DEST= '/u02/app/oracle/oradata/ORCL/pdb_refreshed';
    CREATE PLUGGABLE DATABASE pdb_refreshed FROM pdb1@link_ORCL 
               CREATE_FILE_DEST= '/u02/app/oracle/oradata/ORCL/pdb_refreshed'
               REFRESH MODE EVERY 2 MINUTES;
  4. In the Primary Session, insert more rows into USERSWITCH.BIGTAB table.
    INSERT INTO userswitch.bigtab VALUES ('New DATA FROM PDB1');
    COMMIT;
  5. Display the rows in USERSWITCH.BIGTAB table.
    SELECT DISTINCT label FROM userswitch.bigtab;
    
    LABEL
    ------------------------------
    New DATA FROM PDB1
    DATA FROM PDB1
    
  6. In the Refresh Session, wait for 2 minutes before you can observe that the rows in the USERSWITCH.BIGTAB table are refreshed from PDB1.
    ALTER SESSION SET CONTAINER = pdb_refreshed;
    ALTER PLUGGABLE DATABASE OPEN READ ONLY;
  7. Display the data in the rows.
    SELECT DISTINCT label FROM userswitch.bigtab;
    
    LABEL
    ------------------------------
    New DATA FROM PDB1
    DATA FROM PDB1
  8. Count the rows.
    SELECT count(*) FROM userswitch.bigtab;
    
    COUNT(*)
    ----------
         10001

section 3Switch Over the Refreshable Cloned PDB

In this section, you'll switch the primary PDB over to the refreshable PDB. The refreshable PDB becomes the primary PDB and conversely the primary PDB becomes the refreshable PDB.

  1. In the Primary Session, reconnect to the CDB root as SYSDBA.
    CONNECT sys@ORCL AS SYSDBA
    Enter password: password
  2. In the Primary Session, before you switch the primary PDB1 PDB to the refreshable clone PDB_REFRESHED PDB, check the status of PDBs.
    SELECT pdb_name, status FROM cdb_pdbs;
    
    PDB_NAME       STATUS
    -------------- ----------
    PDB$SEED       NORMAL
    PDB_REFRESHED  REFRESHING
    PDB1           NORMAL
    
  3. In the Primary Session, switch the primary PDB1 PDB to the refreshable clone PDB_REFRESHED PDB.
    ALTER SESSION SET CONTAINER = pdb1;
    ALTER PLUGGABLE DATABASE REFRESH MODE EVERY 2 MINUTES       
                    FROM pdb_refreshed@link_ORCL SWITCHOVER;
  4. In the Primary Session, verify the status of the refreshable PDB1.
    ALTER SESSION SET CONTAINER = cdb$root;
    SELECT pdb_name, status FROM cdb_pdbs;
    
    PDB_NAME       STATUS
    -------------- ----------
    PDB$SEED       NORMAL
    PDB_REFRESHED  NORMAL
    PDB1           REFRESHING
    
  5. In the Refresh Session, start and commit a transaction in the primary PDB_REFRESHED.
    ALTER SESSION SET CONTAINER = pdb_refreshed;
    INSERT INTO userswitch.bigtab VALUES ('New DATA FROM pdb_refreshed');
    COMMIT;
  6. In the Refresh Session, display the data in the table.
    SELECT DISTINCT label FROM userswitch.bigtab;
    
    LABEL
    ------------------------------
    New DATA FROM PDB1
    DATA FROM PDB1
    New DATA FROM pdb_refreshed
  7. In the Primary Session, wait for 2 minutes before you can observe that the data in PDB1 is refreshed.
    ALTER SESSION SET CONTAINER = pdb1;
    ALTER PLUGGABLE DATABASE OPEN READ ONLY;
  8. In the Primary Session, display the data in the table.
    SELECT DISTINCT label FROM userswitch.bigtab;
    
    LABEL
    ------------------------------
    New DATA FROM PDB1
    DATA FROM PDB1
    New DATA FROM pdb_refreshed
  9. In the Primary Session, count the rows.
  10. SELECT count(*) FROM userswitch.bigtab;
    
    COUNT(*)
    ----------
         10002
  11. In the Primary Session, close the refreshable PDB to be refreshed.
    ALTER PLUGGABLE DATABASE CLOSE;

section 4Reswitch

In this section, you'll observe that you can switch back and forth from one PDB to its refreshed cloned PDB, PDB_REFRESHED and back to PDB1. PDB1 becomes the primary PDB and PDB_REFRESHED becomes the refreshable PDB.

  1. In the Refresh Session, update data in USERSWITCH.BIGTAB table.
    UPDATE userswitch.bigtab SET label = 'DATA updated IN pdb_refreshed';
    COMMIT;
    
  2. Display the data in the table.
    SELECT DISTINCT label FROM userswitch.bigtab;
    
    LABEL
    ------------------------------
    DATA updated IN pdb_refreshed
  3. In the Primary Session, wait for 2 minutes before you can observe that the data in PDB1 is refreshed.
    ALTER SESSION SET CONTAINER = pdb1;
    ALTER PLUGGABLE DATABASE OPEN READ ONLY;
  4. Display the data in the table.
    SELECT DISTINCT label FROM userswitch.bigtab;
    
    LABEL
    ------------------------------
    DATA updated IN pdb_refreshed
  5. Now in the Refresh Session, switch the primary PDB_REFRESHED PDB to the refreshable clone PDB1 PDB.
    ALTER SESSION SET CONTAINER = pdb_refreshed;
    ALTER PLUGGABLE DATABASE REFRESH MODE EVERY 2 MINUTES
             FROM pdb1@link_ORCL SWITCHOVER;
  6. Verify the status of the refreshable PDB_ REFRESHED.
    ALTER SESSION SET CONTAINER = CDB$ROOT;
    SELECT pdb_name, status FROM cdb_pdbs;
    
    PDB_NAME       STATUS
    -------------- ----------
    PDB$SEED       NORMAL
    PDB_REFRESHED  REFRESHING
    PDB1           NORMAL