Search This Blog

Monday, November 4, 2019

Determing the optimal UNDO tablespace size


SELECT d.undo_size/(1024*1024) "ACTUAL UNDO SIZE [MByte]",
       SUBSTR(e.value,1,25) "UNDO RETENTION [Sec]",
       (TO_NUMBER(e.value) * TO_NUMBER(f.value) *
       g.undo_block_per_sec) / (1024*1024)
      "NEEDED UNDO SIZE [MByte]"
  FROM (
       SELECT SUM(a.bytes) undo_size
         FROM v$datafile a,
              v$tablespace b,
              dba_tablespaces c
        WHERE c.contents = 'UNDO'
          AND c.status = 'ONLINE'
          AND b.name = c.tablespace_name
          AND a.ts# = b.ts#
       ) d,
      v$parameter e,
       v$parameter f,
       (
       SELECT MAX(undoblks/((end_time-begin_time)*3600*24))
         undo_block_per_sec
         FROM v$undostat
       ) g
 WHERE e.name = 'undo_retention'
  AND f.name = 'db_block_size';

Killing Oracle Sessions (ALTER SYSTEM KILL / DISCONNECT SESSION)


ALTER SYSTEM KILL SESSION

The basic syntax for killing a session is shown below.
SQL> ALTER SYSTEM KILL SESSION 'sid,serial#';
In a RAC environment, you optionally specify the INST_ID, shown when querying the GV$SESSION view. This allows you to kill a session on different RAC node.
SQL> ALTER SYSTEM KILL SESSION 'sid,serial#,@inst_id';
The KILL SESSION command doesn't actually kill the session. It merely asks the session to kill itself. In some situations, like waiting for a reply from a remote database or rolling back transactions, the session will not kill itself immediately and will wait for the current operation to complete. In these cases the session will have a status of "marked for kill". It will then be killed as soon as possible.
In addition to the syntax described above, you can add the IMMEDIATE clause.
SQL> ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
This does not affect the work performed by the command, but it returns control back to the current session immediately, rather than waiting for confirmation of the kill.
If the marked session persists for some time you may consider killing the process at the operating system level. Before doing this it's worth checking to see if it is performing a rollback. You can do this by running this script (session_undo.sql). If the USED_UREC value is decreasing for the session in question you should leave it to complete the rollback rather than killing the session at the operating system level.

ALTER SYSTEM DISCONNECT SESSION

The ALTER SYSTEM DISCONNECT SESSION  syntax is an alternative method for killing Oracle sessions. Unlike the KILL SESSION command which asks the session to kill itself, the DISCONNECT SESSION command kills the dedicated server process (or virtual circuit when using Shared Sever), which is equivalent to killing the server process from the operating system. The basic syntax is similar to the KILL SESSION command with the addition of the POST_TRANSACTION clause. The SID and SERIAL# values of the relevant session can be substituted into one of the following statements.
SQL> ALTER SYSTEM DISCONNECT SESSION 'sid,serial#' POST_TRANSACTION;
SQL> ALTER SYSTEM DISCONNECT SESSION 'sid,serial#' IMMEDIATE;
The POST_TRANSACTION clause waits for ongoing transactions to complete before disconnecting the session, while the IMMEDIATE clause disconnects the session and ongoing transactions are rolled back immediately.
The POST_TRANSACTION and IMMEDIATE clauses can be used together, but the documentation states that in this case the IMMEDIATE clause is ignored. In addition, the syntax diagram suggests both clauses are optional, but in reality, one or both must be specified or you receive an error.
SQL> alter system disconnect session '30,7';
alter system disconnect session '30,7'
                                     *
ERROR at line 1:
ORA-02000: missing POST_TRANSACTION or IMMEDIATE keyword

SQL>
This command means you should never need to switch to the operating system to kill sessions, which reduces the chances of killing the wrong process.

ALTER SYSTEM CANCEL SQL (18c+)

The ALTER SYSTEM CANCEL SQL command was introduced in Oracle Database 18c to cancel a SQL statement in a session, providing an alternative to killing a rogue session.
ALTER SYSTEM CANCEL SQL 'SID, SERIAL[, @INST_ID][, SQL_ID]';

The Windows Approach

To kill the session on the Windows operating system, first identify the session, then substitute the relevant SID and SPID values into the following command issued from the command line.
C:\> orakill ORACLE_SID spid
The session thread should be killed immediately and all resources released.

The UNIX Approach

Warning: If you are using the Multithreaded Model in Oracle 12c, you should not attempt to kill operating system processes. To know why, read this.
To kill the session on UNIX or Linux operating systems, first identify the session, then substitute the relevant SPID into the following command.
% kill spid
If after a few minutes the process hasn't stopped, terminate the session using the following.
% kill -9 spid
If in doubt check that the SPID matches the UNIX PROCESSID shown using.
% ps -ef | grep ora
The session thread should be killed immediately and all resources released.

Identify the Session to be Killed

Killing sessions can be very destructive if you kill the wrong session, so be very careful when identifying the session to be killed. If you kill a session belonging to a background process you will cause an instance crash.
Identify the offending session using the [G]V$SESSION and [G]V$PROCESS views as follows.
SET LINESIZE 100
COLUMN spid FORMAT A10
COLUMN username FORMAT A10
COLUMN program FORMAT A45

SELECT s.inst_id,
       s.sid,
       s.serial#,
       --s.sql_id,
       p.spid,
       s.username,
       s.program
FROM   gv$session s
       JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
WHERE  s.type != 'BACKGROUND';

   INST_ID   SID  SERIAL# SPID  USERNAME   PROGRAM
---------- ------ -------- ---- ---------- ---------------------------------------------
         1   30      15   3859   TEST       sqlplus@oel5-11gr2.localdomain (TNS V1-V3)
         1   23      287  3834   SYS        sqlplus@oel5-11gr2.localdomain (TNS V1-V3)
         1   40      387  4663              oracle@oel5-11gr2.localdomain (J000)
         1   38      125  4665              oracle@oel5-11gr2.localdomain (J001)

SQL>
The SID and SERIAL# values of the relevant session can then be substituted into the commands in the previous sections.

Online Redo Log Switch Frequency

 set pages 999 lines 400  
 col h0 format 999  
 col h1 format 999  
 col h2 format 999  
 col h3 format 999  
 col h4 format 999  
 col h5 format 999  
 col h6 format 999  
 col h7 format 999  
 col h8 format 999  
 col h9 format 999  
 col h10 format 999  
 col h11 format 999  
 col h12 format 999  
 col h13 format 999  
 col h14 format 999  
 col h15 format 999  
 col h16 format 999  
 col h17 format 999  
 col h18 format 999  
 col h19 format 999  
 col h20 format 999  
 col h21 format 999  
 col h22 format 999  
 col h23 format 999  
 SELECT TRUNC (first_time) "Date", inst_id, TO_CHAR (first_time, 'Dy') "Day",  
  COUNT (1) "Total",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '00', 1, 0)) "h0",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '01', 1, 0)) "h1",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '02', 1, 0)) "h2",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '03', 1, 0)) "h3",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '04', 1, 0)) "h4",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '05', 1, 0)) "h5",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '06', 1, 0)) "h6",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '07', 1, 0)) "h7",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '08', 1, 0)) "h8",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '09', 1, 0)) "h9",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '10', 1, 0)) "h10",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '11', 1, 0)) "h11",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '12', 1, 0)) "h12",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '13', 1, 0)) "h13",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '14', 1, 0)) "h14",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '15', 1, 0)) "h15",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '16', 1, 0)) "h16",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '17', 1, 0)) "h17",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '18', 1, 0)) "h18",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '19', 1, 0)) "h19",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '20', 1, 0)) "h20",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '21', 1, 0)) "h21",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '22', 1, 0)) "h22",  
  SUM (DECODE (TO_CHAR (first_time, 'hh24'), '23', 1, 0)) "h23",  
  ROUND (COUNT (1) / 24, 2) "Avg"  
 FROM gv$log_history  
 WHERE thread# = inst_id  
 AND first_time > sysdate -7  
 GROUP BY TRUNC (first_time), inst_id, TO_CHAR (first_time, 'Dy')  
 ORDER BY 1,2;  

Restore spfile from autobackup

Restore SPFILE no AUTOBACKUP found RMAN-06172
Today i received an error while restoring spfile from autobackup.
RMAN> restore spfile from autobackup;
Starting restore at 04-NOV-19
using channel ORA_DISK_1
channel ORA_DISK_1: looking for AUTOBACKUP on day: 20191104
channel ORA_DISK_1: looking for AUTOBACKUP on day: 20191103
channel ORA_DISK_1: looking for AUTOBACKUP on day: 20191102
channel ORA_DISK_1: looking for AUTOBACKUP on day: 20191101
channel ORA_DISK_1: looking for AUTOBACKUP on day: 20191031
channel ORA_DISK_1: looking for AUTOBACKUP on day: 20191030
channel ORA_DISK_1: looking for AUTOBACKUP on day: 20191029
channel ORA_DISK_1: no AUTOBACKUP in 7 days found
RMAN-00571: =======================================================
RMAN-00569: ===== ERROR MESSAGE STACK FOLLOWS ======
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 11/04/2019 09:41:38
RMAN-06172: no AUTOBACKUP found or specified handle is not a valid copy or piece

To rectify the error:
RMAN> set dbid=1551070519
RMAN> restore spfile from autobackup db_recovery_file_dest='/u01/oracle/flash_recovery_area' db_name='ORCL';

Similarly to restore controlfile from autobackup

RMAN> restore controlfile from autobackup;

Sunday, September 20, 2015

ORA-01665: control file is not a standby control file when starting standby database after failover

When i was doing FAILOVER, it switched properly to other Server (Now Primary). When i tried to check standby server, i received ORA-01665: control file is not a standby control file

SQL> STARTUP MOUNT
ORACLE instance started.
Database mounted.
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT;
ERROR at line 1:
ORA-01665: control file is not a standby control file
SQL> SELECT database_role FROM v$database;
DATABASE_ROLE
—————-
PRIMARY

The steps i followed to solve this issue:
SQL> ALTER DATABASE CONVERT TO PHYSICAL STANDBY;
Database altered.
SQL> STARTUP MOUNT
ORACLE instance started.
Database mounted.
SQL> SELECT database_role FROM v$database;
DATABASE_ROLE
—————-
PHYSICAL STANDBY
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT;
Database altered.
If you are using DG Broker, then reinstate standby database using below command.
DGMGRL> reinstate database STDBY;


Also check if archives are being transferred to standby location from primary.

Wednesday, August 25, 2010

Running out of space? Want to move Oracle Datafiles?

Are you running out of space in the file system where database files (data files) are installed? Try out this tip in order to move some of your data files from one drive to another and update the datafile location in your database.

The operating system: Oracle Enterprise Linux
The Database: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0
In our environment we have installed our database's files (data files) in /oracle/oradata/mydb location. The drive is getting up to 99% of utilization. Now we will move the system01.dbf from the above mentioned location to a new location in /oracle/hdb1/oradata/mydb location. /oracle/hdb1 is mounted from /dev/hdb1. The drive is nearly empty, that's why I chose it.
Now for the real moving part, we will perform the following steps:
Login to SQL* Plus and shutdown the database
Logout from SQL* Plus and move the files from the source directory to destination directory.
Login to SQL* Plus as /nolog
Connect as SYSDBA to the database to an idle instance.
Issue startup mount command to bring up the database.
Issue ALTER DATABASE RENAME command to rename the data files from the source directory to destination directory.
Now finally open the database.
The above mentioned are the brief steps by which we can achieve our goal. Now to demonstrate see the following commands as well so that it serves as a live example:
Step 1
oracle@astrn10: sqlplus /nolog
SQL> conn /as sysdba
SQL> shutdown immediate;
Troubleshooting: If you are not able to get the SQL prompt, check your ORACLE_HOME, and ORACLE_SID.

Step 2
SQL> exit;
oracle@astrn10: mv /oracle/oradata/mydb/system01.dbf /oracle/hdb1/oradata/mydb/
Now check whether the file have been moved or not by issuing the following command:
oracle@astrn10: ls /oracle/hdb1/oradata/mydb/
total 429924352
-rw-r----- 1 oracle oinstall 429924352 Feb 12 11:00 system01.dbf
Now we are ready for the next step.

Step 3
oracle@astrn10: sqlplus /nolog
SQL>

Step 4
SQL> conn /as sysdba
Connected to idle instance.

Step 5
SQL> startup mount;
Database mounted.

Step 6
SQL> alter database rename file '/oracle/oradata/mydb/system01.dbf' to '/oracle/hdb1/oradata/mydb/system01.dbf';
Database altered.

Step 7
SQL> alter database open;
Database opened.
That's all. We are done with our agenda for moving data files from one drive to another. If this where Windows/any other operating system, then copy files as per your operating system commands/interface in Step 2.
In order to copy more files (in this demonstration we have moved only one file), then repeat Step #2 and Step # 6 for each file.
#End of tip