Dear Readers,
In this article, we will see the following Moving Online Data files in Oracle 12c.
In order to rename, move and relocate a datafile up to 11g, we had to put a tablespace or database completely offline. As we can see here drawback is Database is not available this whole maintenance activity.
In a nutshell prior to 12c, we need downtime windows to relocate, rename or copy a datafile. From 12c, one single step performs the required action while the database remains entirely available in read and write for users, without any data loss.
The oracle database 12c online move datafile feature provides the capability to move an online datafile from one kind of storage system to another (Like an example from non-asm to asm) while the database is open and accessing the file. While datafile moving online you can perform operation like Data Manipulation Language and Data Definition Language. You can create indexes, tables, rebuild indexes online. You can select tables and partition data. If objects are compressed while the data file is moved, the compression remain the same.
Let’s look at 12c new feature, which is being able to move online data file and partition as part of 12c.
Set your database environment and check your datafile
1 2 3 4 5 6 7 8 9 |
[ora12c@CentOS7 ~]$ . oraenv ORACLE_SID = [orcl] ? The Oracle base remains unchanged with value /u01/app/ora12c [ora12c@CentOS7 ~]$ sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on Tue Jan 29 19:10:26 2019 Copyright (c) 1982, 2014, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options |
Check datafile location
1 2 3 4 5 6 7 8 9 10 11 12 13 |
SQL> select name from v$datafile; NAME -------------------------------------------------------------------------------- /u01/app/ora12c/oradata/orcl/system01.dbf /u01/app/ora12c/oradata/orcl/sysaux01.dbf /u01/app/ora12c/oradata/orcl/undotbs01.dbf /u01/app/ora12c/oradata/orcl/pdbseed/system01.dbf /u01/app/ora12c/oradata/orcl/users01.dbf /u01/app/ora12c/oradata/orcl/pdbseed/sysaux01.dbf /u01/app/ora12c/oradata/orcl/pdborcl/system01.dbf /u01/app/ora12c/oradata/orcl/pdborcl/sysaux01.dbf /u01/app/ora12c/oradata/orcl/pdborcl/SAMPLE_SCHEMA_users01.dbf /u01/app/ora12c/oradata/orcl/pdborcl/example01.dbf |
Creating one directory here, to perform our task
1 |
SQL> !mkdir /u01/app/ora12c/oradata/orcl/move |
When an oracle managed file used, we need to set first db_create_file_dest but as you can see above db_create_file_dest is not set
1 2 3 4 5 6 7 8 9 |
SQL> CREATE TABLESPACE KTEXPTS DATAFILE '/u01/app/ora12c/oradata/orcl/ktexpts01.dbf' SIZE 4096M AUTOEXTEND ON NEXT 64M MAXSIZE 8192M LOGGING ONLINE EXTENT MANAGEMENT LOCAL AUTOALLOCATE SEGMENT SPACE MANAGEMENT AUTO FLASHBACK ON; Tablespace created. |
Let’s check that our datafile with the help of below command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
SQL> select name from v$datafile; NAME -------------------------------------------------------------------------------- /u01/app/ora12c/oradata/orcl/system01.dbf /u01/app/ora12c/oradata/orcl/sysaux01.dbf /u01/app/ora12c/oradata/orcl/undotbs01.dbf /u01/app/ora12c/oradata/orcl/pdbseed/system01.dbf /u01/app/ora12c/oradata/orcl/users01.dbf /u01/app/ora12c/oradata/orcl/pdbseed/sysaux01.dbf /u01/app/ora12c/oradata/orcl/pdborcl/system01.dbf /u01/app/ora12c/oradata/orcl/pdborcl/sysaux01.dbf /u01/app/ora12c/oradata/orcl/pdborcl/SAMPLE_SCHEMA_users01.dbf /u01/app/ora12c/oradata/orcl/pdborcl/example01.dbf /u01/app/ora12c/oradata/orcl/ktexpts01.dbf 11 rows selected. |
1 2 |
SQL> !ls -lrt /u01/app/ora12c/oradata/orcl/move total 0 |
As you can see above directory that it is empty so let’s move datafile. To move datafile look below command
1 2 3 4 |
SQL> alter database move datafile '/u01/app/ora12c/oradata/orcl/ktexpts01.dbf' to '/u01/app/ora12c/oradata/orcl/move/ktexpts01.dbf'; 2 Database altered. |
Now check again at location that you are able to find ktexpts01.dbf datafile or not
1 2 3 |
SQL> !ls -l /u01/app/ora12c/oradata/orcl/move/ total 4194316 -rw-r-----. 1 ora12c oinstall 4294975488 Jan 29 20:29 ktexpts01.dbf |
Now you can see above that our datafile has been moved successfully.
Here, we have two more option that you would like to know
1.KEEP => You can move the datafile to new location and keep that datafile at original location also. So basically you will have backup copy at original place.
1 2 3 4 5 6 7 8 9 |
SQL> SELECT file_id, file_name FROM dba_data_files; FILE_ID FILE_NAME ---------- -------------------------------------------------- 1 /u01/app/ora12c/oradata/orcl/system01.dbf 3 /u01/app/ora12c/oradata/orcl/sysaux01.dbf 4 /u01/app/ora12c/oradata/orcl/undotbs01.dbf 6 /u01/app/ora12c/oradata/orcl/users01.dbf 12 /u01/app/ora12c/oradata/orcl/move/ktexpts01.dbf |
Move datafile using KEEP
1 2 |
SQL> ALTER DATABASE MOVE DATAFILE 12 TO '/u01/app/ora12c/oradata/orcl/ktexpts01.dbf' KEEP; Database altered. |
Check datafile location
1 2 3 4 5 |
SQL> SELECT file_id, file_name FROM dba_data_files WHERE file_id = 12; FILE_ID FILE_NAME ---------- -------------------------------------------------- 12 /u01/app/ora12c/oradata/orcl/ktexpts01.dbf |
Check datafile at physical level
1 2 3 4 5 6 |
SQL> !ls -l /u01/app/ora12c/oradata/orcl/move/ -rw-r-----. 1 ora12c oinstall 4294975488 Jan 29 20:42 ktexpts01.dbf SQL> !ls -l /u01/app/ora12c/oradata/orcl/ktexpts01.* -rw-r-----. 1 ora12c oinstall 4294975488 Jan 29 20:42 /u01/app/ora12c/oradata/orcl/ktexpts01.dbf |
As you can see above command at both place we can find datafile ktexpts01.dbf.
2.REUSE => It just reuses the existing data file that is at that location (overwrite datafile with the same name)
1 2 3 |
SQL> ALTER DATABASE MOVE DATAFILE 12 TO '/u01/app/ora12c/oradata/orcl/move/ktexpts01.dbf' REUSE; Database altered. |
Lets check datafile from DB level.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SQL> SELECT file#, name FROM v$datafile; FILE# NAME ---------- ----------------------------------------------------------------- 1 /u01/app/ora12c/oradata/orcl/system01.dbf 3 /u01/app/ora12c/oradata/orcl/sysaux01.dbf 4 /u01/app/ora12c/oradata/orcl/undotbs01.dbf 5 /u01/app/ora12c/oradata/orcl/pdbseed/system01.dbf 6 /u01/app/ora12c/oradata/orcl/users01.dbf 7 /u01/app/ora12c/oradata/orcl/pdbseed/sysaux01.dbf 8 /u01/app/ora12c/oradata/orcl/pdborcl/system01.dbf 9 /u01/app/ora12c/oradata/orcl/pdborcl/sysaux01.dbf 10 /u01/app/ora12c/oradata/orcl/pdborcl/SAMPLE_SCHEMA_users01.dbf 11 /u01/app/ora12c/oradata/orcl/pdborcl/example01.dbf 12 /u01/app/ora12c/oradata/orcl/move/ktexpts01.dbf |
In case you are moving datafiles which belongs to Pluggable database(Container environment ).Below command showing you container and pluggable database file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SQL> column NAME for a65 SQL> SELECT file#, name FROM v$datafile; FILE# NAME ---------- ----------------------------------------------------------------- 1 /u01/app/ora12c/oradata/orcl/system01.dbf 3 /u01/app/ora12c/oradata/orcl/sysaux01.dbf 4 /u01/app/ora12c/oradata/orcl/undotbs01.dbf 5 /u01/app/ora12c/oradata/orcl/pdbseed/system01.dbf 6 /u01/app/ora12c/oradata/orcl/users01.dbf 7 /u01/app/ora12c/oradata/orcl/pdbseed/sysaux01.dbf 8 /u01/app/ora12c/oradata/orcl/pdborcl/system01.dbf 9 /u01/app/ora12c/oradata/orcl/pdborcl/sysaux01.dbf 10 /u01/app/ora12c/oradata/orcl/pdborcl/SAMPLE_SCHEMA_users01.dbf 11 /u01/app/ora12c/oradata/orcl/pdborcl/example01.dbf 12 /u01/app/ora12c/oradata/orcl/move/ktexpts01.dbf |
If we will try to move file that belong to PDB from container database then it will show an error like below
1 2 3 4 5 |
SQL> ALTER DATABASE MOVE DATAFILE 11 TO '/u01/app/ora12c/oradata/orcl/move/example01.dbf'; ALTER DATABASE MOVE DATAFILE 11 TO '/u01/app/ora12c/oradata/orcl/move/example01.dbf' * ERROR at line 1: ORA-01516: nonexistent log file, data file, or temporary file "11" |
If we want to move pluggable database file then we need to enter/log-in into pluggable database after that we can move database normally as above we did in container database.
1 2 |
SQL> alter session set container=PDBORCL; Session altered. |
1 2 3 4 5 6 7 |
SQL> show con_name CON_NAME ------------------------------ PDBORCL SQL> ALTER DATABASE MOVE DATAFILE 11 TO '/u01/app/ora12c/oradata/orcl/move/example01.dbf' ; Database altered. |
Check datafile location
1 2 3 4 5 |
SQL> SELECT file_id, file_name FROM dba_data_files WHERE file_id = 11; FILE_ID FILE_NAME ---------- -------------------------------------------------- 11 /u01/app/ora12c/oradata/orcl/move/example01.dbf |
As you can see above we have successfully moved pluggable database file also.
The ALTER DATABASE MOVE DATAFILE syntax does not work for redo log files, control files and temporary files means we cannot move this files with the help of this 12c new feature.
V$SESSION_LONGOPS view to display ongoing online move operation like number of blocks moved so far. Each ongoing operation has one row.
There is two points that we need to keep in mind while performing above operation.
- After moving datafile, Oracle automatically deletes old data file and also prevent the user from overwriting an existing file.
- This whole operation requires twice the size of the files to be copied as when you started to move datafile, oracle first make the copy of the datafile after successfully completed, pointers to the datafile are updated and the old file is removed from the file system.
This feature is not compatible with OFFLINE datafile but it is compatible with block media recovery, read only tablespace, read write tablespace.
If you execute flashback database when file is moving, flashback database will not change file name to the original file name but it will restore old contents of the datafile.
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform