Oracle Multitenant PDB Lifecycle Step by Step: Create, Clone, Unplug, Plug In and Drop
Updated: Oct 24
From Oracle Database 21c onwards, the multitenant architecture is the only architecture: every database is a container database (CDB) holding one or more pluggable databases (PDBs). In 19c it is still optional but strongly recommended, and you can run up to three user PDBs per CDB without the Multitenant option licence.
Managing PDBs is a core DBA skill. This post goes through the whole lifecycle of a PDB, from creation to removal, with each step explained.
The Building Blocks
CDB$ROOT (the root) holds the Oracle-supplied metadata and common users. You connect to it as SYS to manage the CDB.
PDB$SEED is a read-only template used to create new PDBs.
Each PDB looks like a normal, independent database to applications: its own users, tablespaces and data.
Common users (names starting with C## by default) exist in all containers. Local users exist only in one PDB.
Each PDB gets its own service, registered with the listener, which is how applications connect.
Step 1: Connect to the CDB and Look Around
Start in the root and list the containers. Always check which container you are in before running commands, because many commands behave differently in the root and in a PDB.
SQL |
sqlplus / as sysdba
SHOW CON_NAME SHOW PDBS
SELECT con_id, name, open_mode, restricted, ROUND(total_size/1024/1024/1024,1) AS size_gb FROM v$pdbs ORDER BY con_id; |
What to expect: SHOW CON_NAME returns CDB$ROOT. SHOW PDBS lists PDB$SEED (READ ONLY) and each PDB with its open mode, for example READ WRITE or MOUNTED.
Step 2: Prepare File Placement
When Oracle creates a PDB it needs to know where to put its datafiles. The easiest way is Oracle Managed Files: set DB_CREATE_FILE_DEST and Oracle names and places the files for you. Without OMF, you must give a FILE_NAME_CONVERT clause in every create or clone command.
SQL |
SHOW PARAMETER db_create_file_dest
-- ASM ALTER SYSTEM SET db_create_file_dest = '+DATA' SCOPE=BOTH SID='*';
-- File system -- ALTER SYSTEM SET db_create_file_dest = '/u02/oradata' SCOPE=BOTH; |
Step 3: Create a New PDB from the Seed
Creating a PDB from the seed copies the seed's files and creates a local administrator user who receives the PDB_DBA role. It usually takes less than a minute.
SQL |
CREATE PLUGGABLE DATABASE salespdb ADMIN USER pdbadmin IDENTIFIED BY "Str0ng#Passw0rd" DEFAULT TABLESPACE sales_data DATAFILE SIZE 1G AUTOEXTEND ON NEXT 512M STORAGE (MAXSIZE 200G);
-- Without OMF, add for example: -- FILE_NAME_CONVERT = ('/u02/oradata/CDB1/pdbseed/', '/u02/oradata/CDB1/salespdb/') |
What this does: Oracle creates SALESPDB with its own SYSTEM, SYSAUX, UNDO (with local undo, the default since 12.2) and TEMP, a default tablespace SALES_DATA for new users, and an administrator PDBADMIN. STORAGE (MAXSIZE 200G) limits the total size of all the PDB's tablespaces, which protects the other PDBs from a runaway one.
Step 4: Open the PDB and Make It Open Automatically
A new PDB is in MOUNTED mode. Open it, then save its state so it opens automatically every time the CDB starts. Without SAVE STATE, the PDB stays MOUNTED after a CDB restart on a single instance, and applications cannot connect.
SQL |
ALTER PLUGGABLE DATABASE salespdb OPEN; ALTER PLUGGABLE DATABASE salespdb SAVE STATE;
SELECT con_name, instance_name, state FROM dba_pdb_saved_states; |
What to expect: DBA_PDB_SAVED_STATES shows SALESPDB with STATE OPEN. On RAC, PDB open modes are usually managed through Clusterware services instead: srvctl add service -db cdb1 -service sales_svc -pdb salespdb -preferred cdb11,cdb12, and the PDB opens when its service starts.
Step 5: Connect to the PDB
You can switch into a PDB from the root with ALTER SESSION, which is handy for DBA work. Applications always connect through the PDB's service via the listener.
SQL |
-- From a SYSDBA session in the root ALTER SESSION SET CONTAINER = salespdb; SHOW CON_NAME
-- Back to the root ALTER SESSION SET CONTAINER = CDB$ROOT; |
Shell |
# The PDB service is registered with the listener lsnrctl status | grep -i salespdb
# Connect from a client with EZConnect sqlplus pdbadmin/"Str0ng#Passw0rd"@//dbhost01.example.com:1521/salespdb |
Tip: Create an extra application service inside the PDB (for example with srvctl or DBMS_SERVICE) instead of letting applications use the default PDB service. You can then stop, move or relocate the application service independently of the PDB itself.
Step 6: Create a Local Hot Clone
Cloning a PDB is the fastest way to give developers or testers a copy of production. With local undo and ARCHIVELOG mode, the source PDB can stay open read-write during the clone (a "hot clone"); Oracle uses the redo generated during the copy to make the clone consistent.
SQL |
CREATE PLUGGABLE DATABASE salespdb_test FROM salespdb;
ALTER PLUGGABLE DATABASE salespdb_test OPEN;
SELECT name, open_mode FROM v$pdbs WHERE name LIKE 'SALESPDB%'; |
What to expect: A new PDB SALESPDB_TEST with a full copy of the data. The time depends on the size of the PDB and the speed of the storage.
Step 7: Clone a PDB from Another CDB over a Database Link
You can also clone a PDB that lives in a different CDB, even on another server. This is also a simple way to migrate a PDB. The clone pulls the files over a database link.
SQL |
-- 1. In the SOURCE CDB root: a common user that may clone its PDBs CREATE USER c##clone_user IDENTIFIED BY "Cl0ne#Pass" CONTAINER = ALL; GRANT CREATE SESSION, CREATE PLUGGABLE DATABASE TO c##clone_user CONTAINER = ALL;
-- 2. In the TARGET CDB root: a database link to the source CREATE DATABASE LINK cdb1_link CONNECT TO c##clone_user IDENTIFIED BY "Cl0ne#Pass" USING '//dbhost01.example.com:1521/cdb1_service';
-- 3. In the TARGET CDB root: clone it CREATE PLUGGABLE DATABASE salespdb_copy FROM salespdb@cdb1_link; ALTER PLUGGABLE DATABASE salespdb_copy OPEN; |
What this does: The target CDB connects to the source through the link, copies all datafiles of SALESPDB and creates SALESPDB_COPY. Both CDBs must use compatible character sets and options, and the target must be the same or a higher version (a clone to a higher version must then be upgraded before it can be opened normally).
Step 8: Check Plug-In Violations
After any clone or plug-in, check PDB_PLUG_IN_VIOLATIONS. It lists differences between the PDB and its new CDB: patches not applied, options not installed, parameter mismatches and so on. A PDB with errors opens in RESTRICTED mode.
SQL |
SELECT name, cause, type, status, message, action FROM pdb_plug_in_violations WHERE status <> 'RESOLVED' ORDER BY name, type; |
What to expect: Ideally no rows. A common case is a patch level difference: the action column then suggests running datapatch, which applies the missing SQL changes inside the PDB.
Step 9: Unplug a PDB
Unplugging detaches a PDB from its CDB and writes an XML manifest describing its files. You do this to move a PDB to another CDB by moving (or sharing) the files, rather than copying over the network.
SQL |
ALTER PLUGGABLE DATABASE salespdb_test CLOSE IMMEDIATE; -- On RAC: ALTER PLUGGABLE DATABASE salespdb_test CLOSE IMMEDIATE INSTANCES = ALL;
ALTER PLUGGABLE DATABASE salespdb_test UNPLUG INTO '/u05/pdb/salespdb_test.xml';
SELECT name, open_mode FROM v$pdbs WHERE name = 'SALESPDB_TEST'; -- Remove it from this CDB, keeping the files for the plug-in DROP PLUGGABLE DATABASE salespdb_test KEEP DATAFILES; |
What this does: The XML file lists every datafile and the PDB's properties. KEEP DATAFILES removes the PDB from the CDB's control file but leaves the files on disk for the target CDB to use.
Tip: Instead of an XML file you can unplug into a .pdb archive (UNPLUG INTO '/u05/pdb/salespdb_test.pdb'). The archive contains the manifest and all datafiles in one compressed file, which is easier to copy.
Step 10: Check Compatibility and Plug the PDB In
On the target CDB, first check that the PDB is compatible. Then plug it in. NOCOPY uses the files where they are, COPY copies them to the target's file destination, and MOVE moves them.
SQL |
-- On the TARGET CDB root SET SERVEROUTPUT ON BEGIN IF DBMS_PDB.CHECK_PLUG_COMPATIBILITY( pdb_descr_file => '/u05/pdb/salespdb_test.xml', pdb_name => 'SALESPDB_TEST') THEN DBMS_OUTPUT.PUT_LINE('Compatible'); ELSE DBMS_OUTPUT.PUT_LINE('Not compatible: see PDB_PLUG_IN_VIOLATIONS'); END IF; END; /
CREATE PLUGGABLE DATABASE salespdb_test USING '/u05/pdb/salespdb_test.xml' COPY TEMPFILE REUSE;
ALTER PLUGGABLE DATABASE salespdb_test OPEN; ALTER PLUGGABLE DATABASE salespdb_test SAVE STATE; |
What to expect: "Compatible", then the PDB is created and opens READ WRITE. If it opens RESTRICTED, check PDB_PLUG_IN_VIOLATIONS (Step 8) and fix what it reports.
Step 11: Limit What a PDB Can Use
In a consolidated CDB, one busy PDB should not starve the others. You can set memory, CPU and I/O limits per PDB, from inside the PDB.
SQL |
ALTER SESSION SET CONTAINER = salespdb;
ALTER SYSTEM SET sga_target = 8G SCOPE=BOTH; ALTER SYSTEM SET pga_aggregate_limit = 6G SCOPE=BOTH; ALTER SYSTEM SET cpu_count = 4 SCOPE=BOTH; ALTER SYSTEM SET max_iops = 5000 SCOPE=BOTH;
ALTER PLUGGABLE DATABASE STORAGE (MAXSIZE 300G); |
What this does: The PDB can use at most 8 GB of SGA and 6 GB of PGA, 4 CPUs worth of CPU time (enforced by Resource Manager), 5,000 I/O operations per second (on Exadata, I/O limits are managed with IORM instead), and 300 GB of storage.
Step 12: Drop a PDB Safely
When a PDB is no longer needed, drop it with its datafiles. This cannot be undone, so take a final backup if there is any doubt, and check the name twice.
SQL |
-- In the root ALTER SESSION SET CONTAINER = CDB$ROOT;
-- Optional final backup -- RMAN> BACKUP PLUGGABLE DATABASE salespdb_copy;
ALTER PLUGGABLE DATABASE salespdb_copy CLOSE IMMEDIATE; DROP PLUGGABLE DATABASE salespdb_copy INCLUDING DATAFILES;
SHOW PDBS |
What to expect: The PDB disappears from SHOW PDBS and its files are removed from disk. If the data might ever be needed again, keep an RMAN backup or a Data Pump export of the PDB from before the drop, because restoring a dropped PDB from CDB backups is much harder than restoring an existing one.
Summary
The PDB lifecycle comes down to a handful of commands: CREATE PLUGGABLE DATABASE from the seed, OPEN and SAVE STATE, hot clones locally or over a database link, UNPLUG and plug in with a compatibility check, resource limits per PDB, and DROP when done. Always check PDB_PLUG_IN_VIOLATIONS after a clone or plug-in, and always confirm which container you are connected to before running a command.
Comments