Friday, 14 January 2022

Changing database name and/or database id using NID

 Changing database name and/or database id using NID

 

Oracle NID utility

NID is utility to change the database name and/or database id.

Steps:

1) Backup the database
2) shutdown the database
3) startup mount, the database

4) Here you use the NID utility, through command execute the following

 nid target=sys/<password>@<old_database> dbname=<new_database_name>

For example 
For change local database philips to samsung do the following

set oracle_sid=philips
nid target=sys/oracle123$ dbname=samsung

After these modification will be made to control file and datafile

NOTE: If you use setname=y at end of command then it will change the db name only DBID will remain the same.

Now shut the database
SQL>shut immediate

Now we need to change the db_name in the pfile or spfile
modify the pfile and start the database, if using spfile mount the database
and issue


SQL>alter system set db_name=samsung scope=spfile;
SQL>shut immediate

If required modify the service id (in windows)
then open the database with resetlogs

SQL>alter database open resetlogs;


====@@@====

Note:If it is in windows

delete existing services,and recreate service with new name.

oradim -delete -sid  DB_NAME

If it is in unix   change the name in crontab.

change the name of pfile and   file location if your standard format need to be updated.
1)In Pfile Edit dbname,and control file location

2)Copy the control files into new location from old location.

-->startup mount pfile='<name>'

-->shutdown immediate;

-->move all datafile and redo log files into new location;

-->startup mount pfile='<name>'

-->change all datafile location in DB level.

alter database rename file '<old file name location>' to '<new file location>';

-->alter database open;

-->create spfile from pfile='<>'

-->start database with spfile.


Change service name in TNS entry as well.

Oracle Datapump Parameters and its purpose (EXPDP & IMPDP)

 Oracle Datapump Parameters and its purpose (EXPDP & IMPDP)

Datapump Utility is provide by Oracle corporation for data movement from one to another database also we can use datapump for re-origination of objects which helps for defragmentation in database. its introduced in 10G version which is faster than traditional exp & imp Process.


Parameter Names for IMPDP in oracle (export data pump).


ATTACH                Attach to existing job, e.g. ATTACH [=job name].

COMPRESSION           Reduce size of dumpfile contents where valid keyword values are: (METADATA_ONLY) and NONE.

CONTENT               Specifies data to unload where the valid keywords are: (ALL), DATA_ONLY, and METADATA_ONLY.

DIRECTORY             Directory object to be used for dumpfiles and logfiles.

DUMPFILE              List of destination dump files (expdat.dmp), e.g. DUMPFILE=scott1.dmp, scott2.dmp, dmpdir:scott3.dmp.

ENCRYPTION_PASSWORD   Password key for creating encrypted column data.

ESTIMATE              Calculate job estimates where the valid keywords are: (BLOCKS) and STATISTICS.

ESTIMATE_ONLY         Calculate job estimates without performing the export.

EXCLUDE               Exclude specific object types, e.g. EXCLUDE=TABLE:EMP.

FILESIZE              Specify the size of each dumpfile in units of bytes.

FLASHBACK_SCN         SCN used to set session snapshot back to.

FLASHBACK_TIME        Time used to get the SCN closest to the specified time.

FULL                  Export entire database (N).

HELP                  Display Help messages (N).

INCLUDE               Include specific object types, e.g. INCLUDE=TABLE_DATA.

JOB_NAME              Name of export job to create.

LOGFILE               Log file name (export.log).

NETWORK_LINK          Name of remote database link to the source system.

NOLOGFILE             Do not write logfile (N).

PARALLEL              Change the number of active workers for current job.

PARFILE               Specify parameter file.

QUERY                 Predicate clause used to export a subset of a table.

SAMPLE                Percentage of data to be exported; 

SCHEMAS               List of schemas to export (login schema).

STATUS                Frequency (secs) job status is to be monitored where the default (0) will show new status when available.

TABLES                Identifies a list of tables to export - one schema only.

TABLESPACES           Identifies a list of tablespaces to export.

TRANSPORT_FULL_CHECK  Verify storage segments of all tables (N).

TRANSPORT_TABLESPACES List of tablespaces from which metadata will be unloaded.

VERSION               Version of objects to export where valid keywords are: (COMPATIBLE), LATEST, or any valid database version.



Parameter Names for IMPDP in oracle (Import data pump).

ATTACH                     Attach to existing job, e.g. ATTACH [=job name].
CONTENT                  Specifies data to load where the valid keywords are:(ALL), DATA_ONLY, and METADATA_ONLY.
DIRECTORY              Directory object to be used for dump, log, and sql files.
DUMPFILE                 List of dumpfiles to import from (expdat.dmp),e.g. DUMPFILE=scott1.dmp, scott2.dmp, dmpdir:scott3.dmp.
ENCRYPTION_PASSWORD    Password key for accessing encrypted column data.This parameter is not valid for network import jobs.
ESTIMATE                  Calculate job estimates where the valid keywords are:(BLOCKS) and STATISTICS.
EXCLUDE                   Exclude specific object types, e.g. EXCLUDE=TABLE:EMP.
FLASHBACK_SCN    SCN used to set session snapshot back to.
FLASHBACK_TIME         Time used to get the SCN closest to the specified time.
FULL                            Import everything from source (Y).
HELP                            Display help messages (N).
INCLUDE                    Include specific object types, e.g. INCLUDE=TABLE_DATA.
JOB_NAME                 Name of import job to create.
LOGFILE                     Log file name (import.log).
NETWORK_LINK      Name of remote database link to the source system.
NOLOGFILE               Do not write logfile.
PARALLEL                  Change the number of active workers for current job.
PARFILE                      Specify parameter file.
QUERY                        Predicate clause used to import a subset of a table.
REMAP_DATAFILE         Redefine datafile references in all DDL statements.
REMAP_SCHEMA           Objects from one schema are loaded into another schema.
REMAP_TABLESPACE       Tablespace object are remapped to another tablespace.
REUSE_DATAFILES        Tablespace will be initialized if it already exists (N).
SCHEMAS                List of schemas to import.
SKIP_UNUSABLE_INDEXES  Skip indexes that were set to the Index Unusable state.
SQLFILE                Write all the SQL DDL to a specified file.
STATUS                 Frequency (secs) job status is to be monitored where the default (0) will show new status when available.
STREAMS_CONFIGURATION  Enable the loading of Streams metadata
TABLE_EXISTS_ACTION    Action to take if imported object already exists.Valid keywords: (SKIP), APPEND, REPLACE and TRUNCATE.
TABLES                 Identifies a list of tables to import.
TABLESPACES            Identifies a list of tablespaces to import.
TRANSFORM              Metadata transform to apply to applicable objects.Valid transform keywords: SEGMENT_ATTRIBUTES, STORAGE OID, and PCTSPACE.
TRANSPORT_DATAFILES    List of datafiles to be imported by transportable mode.
TRANSPORT_FULL_CHECK   Verify storage segments of all tables (N).
TRANSPORT_TABLESPACES  List of tablespaces from which metadata will be loaded. Only valid in NETWORK_LINK mode import operations.
VERSION                Version of objects to export where valid keywords are: (COMPATIBLE), LATEST, or any valid database version.Only valid for NETWORK_LINK and SQLFILE.



Hope this is going to help to understand datapump parameters and its purpose. 

Questions are welcomed !! 

Thursday, 13 January 2022

Oracle Queries to check space or storage related details.

 Below are the few queries to check space related stuff in Oracle database.


How to Check Tablespace Details:

set pagesize 2000 lines 200
col TOTAL_MB  format 999,999,999,999
col FREE_MB   format 999,999,999,999
col USED_MB   format 999,999,999,999
col PCT_FREE format 999 heading 'PERCENT|FREE' just center
col TS_NAME format a30
SELECT a.tablespace_name TS_NAME,
       SUM(a.tots) TOTAL_MB,
       SUM(a.sumb) FREE_MB,
       SUM(a.tots)- SUM(a.sumb) USED_MB,
       SUM(a.sumb)*100/SUM(a.tots) PCT_FREE
FROM    ( SELECT tablespace_name,
                   0 tots,
                   SUM(bytes)/(1024*1024) sumb
              FROM DBA_FREE_SPACE a
              GROUP BY tablespace_name
            UNION
            SELECT     tablespace_name,SUM(bytes)/(1024*1024) tots,0
              FROM     DBA_DATA_FILES
              GROUP BY tablespace_name) a
GROUP BY a.tablespace_name
UNION
SELECT
        f.tablespace_name TS_NAME,
        sum(d.bytes/(1024*1024)) TOTAL_MB,
        round(SUM(ROUND(((f.bytes_free + f.bytes_used) - nvl(p.bytes_used, 0)) / 1024 / 1024, 2)),0) FREE_MB,
        round(SUM(ROUND(nvl(p.bytes_used, 0) / 1024 / 1024, 2)),0) USED_MB,
        (100-round((SUM(ROUND(nvl(p.bytes_used, 0)/1024/1024, 2)) * 100) / (SUM(ROUND((f.bytes_free + f.bytes_used)/1024/ 1024, 2))),2)) PCT_FREE
FROM
        sys.v_$temp_space_header f,
        dba_temp_files d,
        sys.v_$temp_extent_pool p
WHERE f.tablespace_name(+) = d.tablespace_name
AND   f.file_id(+) = d.file_id
AND   p.file_id(+) = d.file_id
GROUP BY f.tablespace_name;


How to Check  Tablespace details with additional Information.

set feed off

column "tablespace_name" heading "Tablespace | Name" format a20
column "FileCount" heading "File | Count" format 999999
column "Size(MB)" heading "Size | (MB)" format 999,999,999.99
column "Free(MB)" heading "Free | (MB)" format 999,999,999.99
column "Used(MB)" heading "Used | (MB)" format 999,999,999.99
column "Max Ext(MB)" heading "Max Ext | (MB)" format 999,999,999
column "%Free" heading "% | Free" format 999.99
column "%Free Ext" heading "% | Free Ext" format 999.99
column "Graph" heading "Graph" format a11
column tablespace_name heading "Tablespace | Name" format a20


SELECT
   ts.tablespace_name, "File Count",
   TRUNC("SIZE(MB)", 2) "Size(MB)",
   TRUNC(fr."FREE(MB)", 2) "Free(MB)",
   TRUNC("SIZE(MB)" - "FREE(MB)", 2) "Used(MB)",
   df."MAX_EXT" "Max Ext(MB)",
   (fr."FREE(MB)" / df."SIZE(MB)") * 100 "% Free",
   RPAD('*', TRUNC(CEIL((fr."FREE(MB)" / df."SIZE(MB)") * 100)/10), '*')    "Graph"
FROM
   (SELECT tablespace_name,
   SUM (bytes) / (1024 * 1024) "FREE(MB)"
   FROM dba_free_space
    GROUP BY tablespace_name) fr,
(SELECT tablespace_name, SUM(bytes) / (1024 * 1024) "SIZE(MB)", COUNT(*)
"File Count", SUM(maxbytes) / (1024 * 1024) "MAX_EXT"
FROM dba_data_files
GROUP BY tablespace_name) df,
(SELECT tablespace_name
FROM dba_tablespaces) ts
WHERE fr.tablespace_name = df.tablespace_name (+)
AND fr.tablespace_name = ts.tablespace_name (+)
ORDER BY "% Free" desc
/


How to Check Particular tablespace and its datafile size details:

set linesize 200
col tablespace_name format a25
col file_name format a70
select file_name,file_id,tablespace_name,bytes/1024/1024,AUTOEXTENSIBLE, MAXBYTES/1024 from dba_data_files where tablespace_name='$TBSNAME';

How to Check  Current DB Size:

select
"Reserved_Space(GB)", "Reserved_Space(GB)" - "Free_Space(GB)" "Used_Space(GB)","Free_Space(GB)"
from(
select 
(select sum(bytes/1024/(1024*1024)) from dba_data_files) "Reserved_Space(GB)",
(select sum(bytes/1024/(1024*1024)) from dba_free_space) "Free_Space(GB)"
from dual
);

How to Check DB Growth details:

SET LINESIZE 200
SET PAGESIZE 200
COL "Database Size" FORMAT a13
COL "Used Space" FORMAT a11
COL "Used in %" FORMAT a11
COL "Free in %" FORMAT a11
COL "Database Name" FORMAT a13
COL "Free Space" FORMAT a12
COL "Growth DAY" FORMAT a11
COL "Growth WEEK" FORMAT a12
COL "Growth DAY in %" FORMAT a16
COL "Growth WEEK in %" FORMAT a16
SELECT
(select min(creation_time) from v$datafile) "Create Time",
(select name from v$database) "Database Name",
ROUND((SUM(USED.BYTES) / 1024 / 1024 /1024),2) || ' GB' "Database Size",
ROUND((SUM(USED.BYTES) / 1024 / 1024 /1024 ) - ROUND(FREE.P / 1024 / 1024 /1024 ),2) || ' GB' "Used Space",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024 ) - (FREE.P / 1024 / 1024 /1024 )) / ROUND(SUM(USED.BYTES) / 1024 / 1024 /1024,2)*100,2) || '% GB' "Used in %",
ROUND((FREE.P / 1024 / 1024 /1024 ),2) || ' GB' "Free Space",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024 ) - ((SUM(USED.BYTES) / 1024 / 1024 /1024 ) - ROUND(FREE.P / 1024 / 1024 /1024 )))/ROUND(SUM(USED.BYTES) / 1024 / 1024 /1024,2 )*100,2) || '% MB' "Free in %",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024) - (FREE.P / 1024 / 1024 /1024 ))/(select sysdate-min(creation_time) from v$datafile),2) || ' GB' "Growth DAY",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024) - (FREE.P / 1024 / 1024 /1024))/(select sysdate-min(creation_time) from v$datafile)/ROUND((SUM(USED.BYTES) / 1024 / 1024/1024 ),2)*100,3) || '% MB' "Growth DAY in %",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024) - (FREE.P / 1024 / 1024 /1024))/(select sysdate-min(creation_time) from v$datafile)*7,2) || ' GB' "Growth WEEK",
ROUND((((SUM(USED.BYTES) / 1024 / 1024 /1024) - (FREE.P / 1024 / 1024 /1024 ))/(select sysdate-min(creation_time) from v$datafile)/ROUND((SUM(USED.BYTES) / 1024 / 1024 /1024 ),2)*100)*7,3) || '% GB' "Growth WEEK in %"
FROM    (SELECT BYTES FROM V$DATAFILE
UNION ALL
SELECT BYTES FROM V$TEMPFILE
UNION ALL
SELECT BYTES FROM V$LOG) USED,
(SELECT SUM(BYTES) AS P FROM DBA_FREE_SPACE) FREE
GROUP BY FREE.P;


How to Check  Size of table/ object

  • select tablespace_name ,round(sum(bytes/1024/1024)) MB from dba_extents where segment_name in ('&1') group by tablespace_name;
  • select segment_name,segment_type,bytes/1024/1024 MB  from dba_segments where segment_type='TABLE' and segment_name='&OBJECT_NAME';

How to Check  Size of schema

  • select owner,sum(bytes/1024/1024) SIZE_IN_MB from dba_segments where owner='&1' group by owner;
  • select owner,sum(bytes/1024/1024/1024) SIZE_IN_GB from dba_segments group by owner order by size_in_gb;
  • select tablespace_name,sum(bytes/1024/1024) SIZE_IN_MB from dba_segments where owner='&USERNAME' group by tablespace_name;
  • select owner,sum(bytes/1024/1024) SIZE_IN_MB from dba_segments where owner='&1' group by owner;


These queries will help in your day-to-day activities for space related queries. Please let know if you would like to need more queries.

Questions are welcomed. :) 😀

Oracle Datagaurd SwitchOver Steps

 Oracle Datagaurd Switchover steps manual and using DGBroker. 


Manual Steps.

On Primary:

  • 1.select name,instance_name,open_mode,database_role, switchover_status  from v$database,v$instance;
  • 2. alter database commit to switchover to physical standby with session shutdown;
  • 3. shut immediate;

On Standby:

  • 1.     alter database recover managed standby database cancel;
  • 2.  alter database commit to switchover to primary with session shutdown;
  • 3.  alter database open;
  • 4.  select name, open_mode, log_mode from v$database;
  • 5.  shut immediate;
  • 6.  startup
  • 7.  alter system switch logfile;

On New standby (old primary)

  • 1.     startup nomount;
  • 2.     alter database mount standby database;
  • 3.     Alter database recover managed standby database disconnect from session.

4.     Check for the sync status.



prompt ++++++++++++++++++++LAST SEQUENCE RECIEVED FROM PRODUCTION and APPLIED ON STANDBY+++++++++++++++++++++++++


set time on

set lines 200
select name, open_mode, database_role from v$database;

SELECT PROCESS, STATUS FROM V$MANAGED_STANDBY where PROCESS like '%MRP%';

show parameter fal

select process, thread#, sequence#, status from v$managed_standby where process='MRP0';



SELECT ARCH.THREAD# "Thread", ARCH.SEQUENCE# "Last Sequence Received", APPL.SEQUENCE# "Last Sequence Applied", (ARCH.SEQUENCE# - APPL.SEQUENCE#) "Difference" FROM 
(SELECT THREAD# ,SEQUENCE# FROM V$ARCHIVED_LOG WHERE (THREAD#,FIRST_TIME ) IN (SELECT THREAD#,MAX(FIRST_TIME) FROM V$ARCHIVED_LOG GROUP BY THREAD#)) ARCH, 
(SELECT THREAD# ,SEQUENCE# FROM V$LOG_HISTORY WHERE (THREAD#,FIRST_TIME ) IN (SELECT THREAD#,MAX(FIRST_TIME) FROM V$LOG_HISTORY GROUP BY THREAD#)) APPL WHERE  ARCH.THREAD# = APPL.THREAD# ;


Sharing a real time issue after the switch over. After switch over noticed new standby is not receiving the logs from primary. 

Perform below steps in new primary only  if new standby not receiving the logs for synchronize. 

  • 1.     show parameter log_archive_dest_state_2;
  • 2.     alter system set log_archive_dest_state_2=defer scope=both;
  • 3.     alter system set log_archive_dest_state_2=enable scope=both;
  • 4.     alter system switch logfile;


Steps using DGMGRL:

 

If DGBroker is enabled in your environment then its very easy to perform the switch over. with just one command Primary database will be switched over. e.g


$dgmgrl

connect sys /

enter password

DGMGRL> switchover to 'Prod_SBY';

Performing switchover NOW, please wait...

New primary database "Prod_SBY" is opening...

Operation requires shutdown of instance "Prod" on database "Prod_PRIM"

Shutting down instance "Prod"...

ORACLE instance shut down.

Operation requires startup of instance "Prod" on database "Prod_PRIM"

Starting instance "Prod"...

ORACLE instance started.

Database mounted.

Switchover succeeded, new primary is "Prod_SBY"


I hope this going to help you for your activity. Questions are welcomed.👍



Oracle DBA

 Hey its Naresh and I am an Oracle DBA and i am here answer all your questions about Oracle DBA.

Changing database name and/or database id using NID

  Changing database name and/or database id using NID   Oracle NID utility NID is utility to change the database name and/or database id. St...