Before 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)
Configure
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.
- In
ORCL,
create the regularPDB1
to create the database link to connect to the future refreshable PDB inORCL
at a later point in time. First, create a directory forPDB
mkdir /u02/app/oracle/oradata/ORCL/pdb1
- Connect to the CDB root.
sqlplus sys@ORCL AS SYSDBA Enter password: password
- Before you create
PDB1,
dropPDB1.
ALTER PLUGGABLE DATABASE ALL CLOSE;
DROP PLUGGABLE DATABASE pdb1 INCLUDING DATAFILES;
- 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';
- Open
PDB1.
ALTER PLUGGABLE DATABASE pdb1 OPEN;
- Create in
ORCL
a common user.DROP USER c##u1 CASCADE; CREATE USER c##u1 IDENTIFIED BY password CONTAINER=ALL;
- 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;
- 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';
- In the primary PDB, create a
USERSWITCH.BIGTAB
table with 10000 rows. First connect toPDB1.
CONNECT system@PDB1 Enter password: password
- Create a local user.
SET sqlprompt "SQL_prim> "
DROP USER userswitch CASCADE; CREATE USER userswitch IDENTIFIED BY password;
- Grant the user system privileges.
GRANT dba, unlimited tablespace TO userswitch;
- Create the
USERSWITCH.BIGTAB
table.CREATE TABLE userswitch.bigtab (label varchar2(30));
- 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; /
- Count the rows.
SELECT count(*) FROM userswitch.bigtab; COUNT(*) ---------- 10000
- Display the data in the rows.
SELECT DISTINCT label FROM userswitch.bigtab; LABEL ------------------------------ DATA FROM PDB1
Create
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.
- Clone the
PDB_REFRESHED
PDB fromPDB1
whilePDB1
is still up and fully functional. First create the directory for thePDB_REFRESHED
PDB.mkdir -p /u02/app/oracle/oradata/ORCL/pdb_refreshed
- Connect to the CDB root.
sqlplus sys@ORCL AS SYSDBA Enter password: password
- 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;
- In the Primary Session, insert more rows into
USERSWITCH.BIGTAB
table.INSERT INTO userswitch.bigtab VALUES ('New DATA FROM PDB1'); COMMIT;
- Display the rows in
USERSWITCH.BIGTAB
table.SELECT DISTINCT label FROM userswitch.bigtab; LABEL ------------------------------ New DATA FROM PDB1 DATA FROM PDB1
- In the Refresh Session, wait for 2 minutes
before you can observe that the rows in the
USERSWITCH.BIGTAB
table are refreshed fromPDB1.
ALTER SESSION SET CONTAINER = pdb_refreshed;
ALTER PLUGGABLE DATABASE OPEN READ ONLY;
- Display the data in the rows.
SELECT DISTINCT label FROM userswitch.bigtab; LABEL ------------------------------ New DATA FROM PDB1 DATA FROM PDB1
- Count the rows.
SELECT count(*) FROM userswitch.bigtab; COUNT(*) ---------- 10001
Switch
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.
- In the Primary Session, reconnect to the CDB
root as
SYSDBA.
CONNECT sys@ORCL AS SYSDBA Enter password: password
- In the Primary Session, before you switch the
primary
PDB1
PDB to the refreshable clonePDB_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
- In the Primary Session, switch the primary
PDB1
PDB to the refreshable clonePDB_REFRESHED
PDB.ALTER SESSION SET CONTAINER = pdb1;
ALTER PLUGGABLE DATABASE REFRESH MODE EVERY 2 MINUTES FROM pdb_refreshed@link_ORCL SWITCHOVER;
- 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
- 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;
- 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
- 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;
- 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
- In the Primary Session, count the rows.
- In the Primary Session, close the refreshable
PDB to be refreshed.
ALTER PLUGGABLE DATABASE CLOSE;
SELECT count(*) FROM userswitch.bigtab;
COUNT(*)
----------
10002
Reswitch
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.
- In the Refresh Session, update data in
USERSWITCH.BIGTAB
table.UPDATE userswitch.bigtab SET label = 'DATA updated IN pdb_refreshed'; COMMIT;
- Display the data in the table.
SELECT DISTINCT label FROM userswitch.bigtab; LABEL ------------------------------ DATA updated IN pdb_refreshed
- 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;
- Display the data in the table.
SELECT DISTINCT label FROM userswitch.bigtab; LABEL ------------------------------ DATA updated IN pdb_refreshed
- Now in the Refresh Session, switch the primary
PDB_REFRESHED
PDB to the refreshable clonePDB1
PDB.ALTER SESSION SET CONTAINER = pdb_refreshed;
ALTER PLUGGABLE DATABASE REFRESH MODE EVERY 2 MINUTES FROM pdb1@link_ORCL SWITCHOVER;
- 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