Thursday, September 3, 2015

How to unplug a PDB and plug it back into the same CDB

HIGH LEVEL STEPS
=================
ps -ef | grep smon
.oraenv
cdb01
sqlplus / as sysdba
SQL@localhost> select name, open_mode from V$database;
SQL@localhost> select name, open_mode from V$PDBS;
SQL@localhost>alter sesssion set container =PDB003
SQL> alter pluggable database pdb003 close immediate;
SQL> alter pluggable database PDB003 unplug into '/app/oracle/oradata/pdb003.xml';  
SQL> select * from CDB_PDBS; 
SQL> drop pluggable database PDB003;
SQL> create pluggable database PDB003 using '/app/oracle/oradata/pdb003.xml' nocopy; 
SQL> select PDB_NAME, STATUS from CDB_PDBS;
SQL> alter session set container=PDB003; 
SQL> select count(*) from marcov.T1; 
SQL> select * from CDB_PDBS;
======================================================================

How to unplug a PDB and plug it back into the same CDB

You have just unplugged a PDB and want to know how to eventually plug it back into the same container.
Let's assume I have a pluggable database named PDB003 and I am just able to unplug it using the following commands:
view plainprint?

SQL> alter pluggable database pdb003 close immediate;  

Pluggable database altered.

SQL> alter pluggable database PDB003 unplug into '/app/oracle/oradata/pdb003.xml';  

Pluggable database altered.
If you query the CDB_PDBS view you can receive useful information about all PDBs contained: id, name, unique identifiers, SCN at the time of creation and the status.
The STATUS column has several values:
NEW, the pdb has been created but never opened
NORMAL, pdb available to be used
UNPLUGGED, the pdb has been unplugged
NEEDS UPGRADE, the pdb must be upgraded
CONVERTING, a non-CDB is going to be converted into a pdb
UNUSABLE, the pdb cannot be used (creation in progress)
In my case the PDB003 pluggable database is in an UNPLUGGED status as expected:
view plainprint?
SQL> select * from CDB_PDBS;  

    PDB_ID PDB_NAME     DBID    CON_UID GUID        STATUS    CREATION_SCN     CON_ID
---------- ---------- ---------- ---------- -------------------------------- ------------- ------------ ----------
  2 PDB$SEED   4063489868 4063489868 E19363E52C005C9AE045000000000001 NORMAL      233   1
  3 PDB001     1701599811 1701599811 E1F26215682E1142E045000000000001 NORMAL  1821405   1
  4 PDB002     1497027100 1497027100 E1F329ECE0F411E6E045000000000001 NORMAL  1844390   1
  5 PDB003     1448206714 1448206714 E2B9BE56B8B936CEE045000000000001 UNPLUGGED  1997215   1
To plug again the pluggable database PDB003 inside the original container (CDB001) you cannot use a syntax like: ALTER PLUGGABLE DATABASE ... PLUG (it doesn't exist in the "SQL Language Reference" manual a PLUG clause) ...
view plainprint?
SQL> alter pluggable database PDB003 plug;
alter pluggable database PDB003 plug
                                *
ERROR at line 1:
ORA-00922: missing or invalid option
... neither you can simply use CREATE PLUGGABLE DATABASE ... USING ...
view plainprint?
SQL> create pluggable database PDB003 using '/app/oracle/oradata/pdb003.xml' nocopy tempfile reuse;
create pluggable database PDB003 using '/app/oracle/oradata/pdb003.xml' nocopy tempfile reuse
*
ERROR at line 1:
ORA-65012: Pluggable database PDB003 already exists.
You need first to drop the pluggable database (DROP PLUGGABLE DATABASE command) and then create it again (CREATE PLUGGABLE DATABASE command).
In this way the pluggable database is plugged again into the same CDB and using the same PDB name.
Have a look at the following commands:
view plainprint?
SQL> drop pluggable database PDB003;  

Pluggable database dropped.

SQL> create pluggable database PDB003 using '/app/oracle/oradata/pdb003.xml' nocopy;                 
  
Pluggable database created.

SQL> select PDB_NAME, STATUS from CDB_PDBS;

PDB_NAME   STATUS  
---------- -------------
PDB$SEED   NORMAL
PDB001     NORMAL
PDB002     NORMAL
PDB003     NEW  

SQL> alter session set container=PDB003;  

Session altered.

SQL> alter database open;

Database altered.

SQL> select count(*) from marcov.T1;

  COUNT(*)
----------
       100
Have a look again at the CDB_PDBS view and note the differences compared with the previous results.
All the unique identifiers of PDB003 have been recreated.
view plainprint?
SQL> select * from CDB_PDBS;

    PDB_ID PDB_NAME     DBID    CON_UID GUID        STATUS    CREATION_SCN     CON_ID
---------- ---------- ---------- ---------- -------------------------------- ------------- ------------ ----------
  2 PDB$SEED   4063489868 4063489868 E19363E52C005C9AE045000000000001 NORMAL      233   1
  3 PDB001     1701599811 1701599811 E1F26215682E1142E045000000000001 NORMAL  1821405   1
  4 PDB002     1497027100 1497027100 E1F329ECE0F411E6E045000000000001 NORMAL  1844390   1
  5 PDB003     1448206714 1448206714 E2B9BE56B8B936CEE045000000000001 NORMAL  3110422   1

No comments:

Post a Comment