Friday, July 3, 2020

How to find out tablespaces with free space < 15%

How to find out tablespaces with free space < 15%

set pagesize 300
set linesize 100
column tablespace_name format a15 heading 'Tablespace'
column sumb format 999,999,999
column extents format 9999
column bytes format 999,999,999,999
column largest format 999,999,999,999
column Tot_Size format 999,999 Heading 'Total Size(Mb)'
column Tot_Free format 999,999,999 heading 'Total Free(Kb)'
column Pct_Free format 999.99 heading '% Free'
column Max_Free format 999,999,999 heading 'Max Free(Kb)'
column Min_Add format 999,999,999 heading 'Min space add (MB)'
ttitle center 'Tablespaces With Less Than 15% Free Space' skip 2
set echo off
select a.tablespace_name,sum(a.tots/1048576) Tot_Size,
   sum(a.sumb/1024) Tot_Free,
  sum(a.sumb)*100/sum(a.tots) Pct_Free,
   ceil((((sum(a.tots) * 15) - (sum(a.sumb)*100))/85 )/1048576) Min_Add
   from
 (
  select tablespace_name,0 tots,sum(bytes) sumb
  from dba_free_space a
  group by tablespace_name
  union
 select tablespace_name,sum(bytes) tots,0 from
  dba_data_files
group by tablespace_name) a
 group by a.tablespace_name
 having sum(a.sumb)*100/sum(a.tots) < 15
 order by pct_free;

OUTPUT:--

                              Tablespaces With Less Than 15% Free Space

Tablespace      Total Size(Mb) Total Free(Kb)  % Free Min space add (MB)
--------------- -------------- -------------- ------- ------------------
SYSAUX                     570         34,880    5.98                 61
SYSTEM                     870        122,048   13.70                 14

All about SGA

All about SGA

SYS @ catdb > select value/1024/1024 mb from v$parameter where upper(name) = 'SGA_MAX_SIZE';

        MB
----------
       800

SYS @ catdb > select pool, round(sum(BYTES)/1024/1024) MB from V$SGASTAT group by pool;

POOL                 MB
------------ ----------
                    292
java pool             4
shared pool         208
large pool           12

SYS @ catdb > select name , value/1024/1024 MB
  from v$parameter
  where upper(name) in (
  'DB_CACHE_SIZE','DB_RECYCLE_CACHE_SIZE','DB_KEEP_CACHE_SIZE', '
  DB_2k_CACHE_SIZE', 'DB_8k_CACHE_SIZE', 'DB_16k_CACHE_SIZE');  2    3    4    5

NAME                                                                                     MB
-------------------------------------------------------------------------------- ----------
db_cache_size                                                                             0
db_keep_cache_size                                                                        0
db_recycle_cache_size                                                                     0

SYS @ catdb > SELECT COMPONENT , CURRENT_SIZE/1024/1024 MB
FROM V$SGA_DYNAMIC_COMPONENTS
WHERE CURRENT_SIZE <>0;  2    3

COMPONENT                                                                MB
---------------------------------------------------------------- ----------
shared pool                                                             208
large pool                                                               12
java pool                                                                 4
DEFAULT buffer cache                                                    284

SYS @ catdb > SELECT COMPONENT, ROUND(CURRENT_SIZE/1024/1024) CURRENT_SIZE ,
ROUND(MIN_SIZE/1024/1024) MIN, ROUND(MAX_SIZE/1024/1024) MAX
FROM V$MEMORY_DYNAMIC_COMPONENTS;  2    3

COMPONENT                                                        CURRENT_SIZE        MIN        MAX
---------------------------------------------------------------- ------------ ---------- ----------
shared pool                                                               208        132        208
large pool                                                                 12         12         72
java pool                                                                   4          4          4
streams pool                                                                0          0          0
SGA Target                                                                520        520        520
DEFAULT buffer cache                                                      284        284        356
KEEP buffer cache                                                           0          0          0
RECYCLE buffer cache                                                        0          0          0
DEFAULT 2K buffer cache                                                     0          0          0
DEFAULT 4K buffer cache                                                     0          0          0
DEFAULT 8K buffer cache                                                     0          0          0
DEFAULT 16K buffer cache                                                    0          0          0
DEFAULT 32K buffer cache                                                    0          0          0
Shared IO Pool                                                              0          0          0
PGA Target                                                                280        280        280
ASM Buffer Cache                                                            0          0          0

16 rows selected.

SYS @ catdb > select START_TIME, END_TIME, STATUS, COMPONENT, OPER_TYPE, OPER_MODE,
PARAMETER, INITIAL_SIZE/1024/1024 INITIAL_SIZE_MB,
TARGET_SIZE/1024/1024 TARGET_SIZE_MB, FINAL_SIZE/1024/1024 FINAL_SIZE_MB
from V$MEMORY_RESIZE_OPS
order by START_TIME, END_TIME ;  2    3    4    5

START_TIM END_TIME  STATUS    COMPONENT                                                        OPER_TYPE     OPER_MODE PARAMETER                    INITIAL_SIZE_MB TARGET_SIZE_MB FINAL_SIZE_MB
--------- --------- --------- ---------------------------------------------------------------- ------------- --------- -------------------------------------------------------------------------------- --------------- -------------- -------------
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT 8K buffer cache                                          STATIC                  db_8k_cache_size                   0       0             0
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT 4K buffer cache                                          STATIC                  db_4k_cache_size                   0       0             0
24-JUN-20 24-JUN-20 COMPLETE  RECYCLE buffer cache                                             STATIC                  db_recycle_cache_size              0       0             0
24-JUN-20 24-JUN-20 COMPLETE  KEEP buffer cache                                                STATIC                  db_keep_cache_size                 0       0             0
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT 2K buffer cache                                          STATIC                  db_2k_cache_size                   0       0             0
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             STATIC                  db_cache_size                      0     300           300
24-JUN-20 24-JUN-20 COMPLETE  ASM Buffer Cache                                                 STATIC                  db_cache_size                      0       0             0
24-JUN-20 24-JUN-20 COMPLETE  PGA Target                                                       STATIC                  pga_aggregate_target               0     280           280
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT 16K buffer cache                                         STATIC                  db_16k_cache_size                  0       0             0
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT 32K buffer cache                                         STATIC                  db_32k_cache_size                  0       0             0
24-JUN-20 24-JUN-20 COMPLETE  SGA Target                                                       STATIC                  sga_target                         0     520           520
24-JUN-20 24-JUN-20 COMPLETE  streams pool                                                     STATIC                  streams_pool_size                  0       0             0
24-JUN-20 24-JUN-20 COMPLETE  java pool                                                        STATIC                  java_pool_size                     0       4             4
24-JUN-20 24-JUN-20 COMPLETE  large pool                                                       STATIC                  large_pool_size                    0      72            72
24-JUN-20 24-JUN-20 COMPLETE  shared pool                                                      STATIC                  shared_pool_size                   0     132           132
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             INITIALIZING            db_cache_size                    300     300           300
24-JUN-20 24-JUN-20 COMPLETE  shared pool                                                      GROW          IMMEDIATE shared_pool_size                 132     136           136
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        IMMEDIATE db_cache_size                    300     296           296
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             GROW          DEFERRED  db_cache_size                    296     356           356
24-JUN-20 24-JUN-20 COMPLETE  large pool                                                       SHRINK        DEFERRED  large_pool_size                   72      12            12
24-JUN-20 24-JUN-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 136     140           140
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    356     352           352
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    352     348           348
24-JUN-20 24-JUN-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 140     144           144
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    348     344           344
24-JUN-20 24-JUN-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 144     148           148
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    344     340           340
24-JUN-20 24-JUN-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 148     152           152
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    340     336           336
24-JUN-20 24-JUN-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 152     156           156
24-JUN-20 24-JUN-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 156     160           160
24-JUN-20 24-JUN-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    336     332           332
26-JUN-20 26-JUN-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 160     168           168
26-JUN-20 26-JUN-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    332     324           324
02-JUL-20 02-JUL-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    324     316           316
02-JUL-20 02-JUL-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 168     176           176
02-JUL-20 02-JUL-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    316     308           308
02-JUL-20 02-JUL-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 176     184           184
02-JUL-20 02-JUL-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 184     192           192
02-JUL-20 02-JUL-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    308     300           300
02-JUL-20 02-JUL-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 192     200           200
02-JUL-20 02-JUL-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    300     292           292
03-JUL-20 03-JUL-20 COMPLETE  DEFAULT buffer cache                                             SHRINK        DEFERRED  db_cache_size                    292     284           284
03-JUL-20 03-JUL-20 COMPLETE  shared pool                                                      GROW          DEFERRED  shared_pool_size                 200     208           208

44 rows selected.

How To Open The Standby Database When The Primary Is Lost in oracle11g ?

How To Open The Standby Database When The Primary Is Lost in oracle11g ?
July 02, 2020


Scenario:- So this scenario shows how to open your standby database in read/write mode when you dont have any access(Lost) on primary database.
Here i'm trying to make a test case to demonstrate the following scenario.Currenly both primary and standby database are in sync.

Oracle version :-  11.2.0.1.0
Primary Database : prim
Standby Database : stand

At primary database:-
SYS @ catdb >select thread#,max(sequence#) from v$archived_log group by thread#;

   THREAD# MAX(SEQUENCE#)
---------- --------------
         1             26
  
  
At standby database:-
SYS @ catdb >select thread#,max(sequence#) from v$archived_log group by thread#;

   THREAD# MAX(SEQUENCE#)
---------- --------------
         1             26

So at this situation we totally power off the primary database server to test the scenario.

At primary:-
SYS @ catdb >shut immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS @ catdb >exit

[root@server1 ~]# poweroff

Now at standby database:-
Open the database in mount stage.
SYS @ catdb >shut immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL:> startup mount
ORACLE instance started.

Total System Global Area  413372416 bytes
Fixed Size                  2213896 bytes
Variable Size             310380536 bytes
Database Buffers           96468992 bytes
Redo Buffers                4308992 bytes
Database mounted.

SYS @ catdb >SELECT OPEN_MODE,PROTECTION_MODE,DATABASE_ROLE FROM V$DATABASE;

OPEN_MODE            PROTECTION_MODE      DATABASE_ROLE
-------------------- -------------------- ----------------
MOUNTED              MAXIMUM PERFORMANCE  PHYSICAL STANDBY


Finish the Recovery process in standby database:-
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH;

Database altered.

Once done, now activate the standby database:-
SYS @ catdb >ALTER DATABASE ACTIVATE PHYSICAL STANDBY DATABASE;

Database altered.

Check the status:-
SYS @ catdb >SELECT OPEN_MODE,PROTECTION_MODE,DATABASE_ROLE FROM V$DATABASE;

OPEN_MODE            PROTECTION_MODE      DATABASE_ROLE
-------------------- -------------------- ----------------
MOUNTED              MAXIMUM PERFORMANCE  PRIMARY


Now open the database in read/write mode.
SYS @ catdb >ALTER DATABASE OPEN;

Database altered.

SYS @ catdb >select open_mode from v$database;

OPEN_MODE
--------------------
READ WRITE

SYS @ catdb >SELECT OPEN_MODE,PROTECTION_MODE,DATABASE_ROLE FROM V$DATABASE;

OPEN_MODE            PROTECTION_MODE      DATABASE_ROLE
-------------------- -------------------- ----------------
READ WRITE           MAXIMUM PERFORMANCE  PRIMARY


So, finally the previous standby database is now a new primary database with read/write mode in open stage.

Script to delete multiple tables using a single query

Script to delete multiple tables using a single query
July 03, 2020

SYS @ catdb > create table xyz (id number);

Table created.

SYS @ catdb > create table t1 as select * from xyz;

Table created.


SYS @ catdb > SELECT * FROM TAB where TNAME IN ('T1','XYZ');

TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
T1                             TABLE
XYZ                            TABLE


SYS @ catdb >
SYS @ catdb > BEGIN

 FOR i IN (SELECT table_name FROM user_tables WHERE table_name IN ('T1','XYZ'))

 LOOP

      EXECUTE IMMEDIATE 'DROP TABLE ' || i.table_name;

  END LOOP;

END;
/  

PL/SQL procedure successfully completed.

SYS @ catdb > SELECT * FROM TAB where TNAME IN ('T1','XYZ');

no rows selected

Monitoring Temporary tablespace usage using shell script

Monitoring Temporary tablespace usage using shell script

The following script will report if temporary tablespace  hits 80% of its usage.

From oracle user:
[oracle@server1 ~]$ vi /home/oracle/check_temp_tbs.sql

select a.tablespace_name tablespace,
         d.TEMP_TOTAL_MB,
         sum (a.used_blocks * d.block_size) / 1024 / 1024 TEMP_USED_MB,
         d.TEMP_TOTAL_MB - sum (a.used_blocks * d.block_size) / 1024 / 1024 TEMP_FREE_MB
from v$sort_segment a,
        (
          select   b.name, c.block_size, sum (c.bytes) / 1024 / 1024 TEMP_TOTAL_MB
          from     v$tablespace b, v$tempfile c
          where    b.ts#= c.ts#
          group by b.name, c.block_size
        ) d
where    a.tablespace_name = d.name
group by a.tablespace_name, d.TEMP_TOTAL_MB;

Now lets create the shell-script which will monitor the space usage of temp tablespaces:-
From oracle user

[oracle@server1 ~]$ vi /home/oracle/alert_temp.sh
#!/bin/bash
export ORACLE_SID=catdb
sqlplus -S / as sysdba @/home/oracle/check_temp_tbs.sql > /home/oracle/temptbs.txt
space=`cat /home/oracle/temptbs.txt`
if [ $space -gt 80 ]; then
mail -s echo "Attention!! Low space in  Temporary Tablespaces in $ORACLE_SID instance!" shashidba1208@gmail.com
fi

:wq(save & exit)

We schedule the script which will check in every 5 mins.
[oracle@server1 ~]$ crontab -e
*/5 * * * * /home/oracle/alert_temp.sh > /dev/null

NOTE:- Please give proper permission to check_temp_tbs.sql and alert_temp.sh if require.
P.S.Make sure mailx rpm is installed and any  mailing service such as sendmail/postfix is running on the server.


What is Standby snapshot Database?


What is Standby snapshot Database?
Oracle provides an unique feature where the physical standby database can be opened in READ WRITE mode to perform update able transactions.
A snapshot standby database is a fully updatable standby database that is created by converting a physical standby database into a snapshot standby database. A snapshot standby database
receives and archives, but does not apply redo data from a primary database. Redo data received from the primary database is applied when a snapshot standby database is converted back
into a physical standby database, after discarding all local updates to the snapshot standby database.

A snapshot standby database typically diverges from its primary database over time because redo data from the primary database is not applied as it is received. Local updates to the
snapshot standby database will cause additional divergence. The data in the primary database is fully protected however, because a snapshot standby can be converted back into a
physical standby database at any time, and the redo data received from the primary will then be applied.

A snapshot standby database will allow you to make use of the data available on the physical standby database (which is the same data of the primary database), which allows the users
to test the application on a standby database which has the primary database's data before implementing it into production environment. Whenever a physical standby database is converted
into a snapshot standby database, a guaranteed restore point is automatically created. Once when the updateable transactions are completed for testing purposes on the snapshot standby
database and when you are converting back the snapshot standby to physical standby, oracle flashbacks to the restore point that was created earlier which means the transactions
which were made in standby database while it was open in READ WRITE mode will be flushed out.

The only requirement to have the snapshot standby is that FRA (Flash Recovery Area) must be configured on physical standby database. It is not necessary to have flashback enabled.

How to drop undo tablespace in oracle 11g?

How to drop undo tablespace in oracle 11g?

SYS @ catdb > set pagesize 999
SYS @ catdb > set linesize 300
SYS @ catdb > /

FILE_NAME                                                    TABLESPACE_NAME
------------------------------------------------------------ ------------------------------
/u01/app/oracle/oradata/catdb/users01.dbf                    USERS
/u01/app/oracle/oradata/catdb/undotbs01.dbf                  UNDOTBS1
/u01/app/oracle/oradata/catdb/sysaux01.dbf                   SYSAUX
/u01/app/oracle/oradata/catdb/system01.dbf                   SYSTEM
/u01/app/oracle/oradata/catdb/example01.dbf                  EXAMPLE
/u01/app/oracle/oradata/catdb/example04.dbf                  EXAMPLE
/u01/app/oracle/oradata/catdb/example02.dbf                  EXAMPLE
/u01/app/oracle/oradata/catdb/tb_name01.dbf                  TB_NAME
/u01/app/oracle/oradata/catdb/testdb.dbf                     TESTDB
/u01/app/oracle/oradata/catdb/undotbs02.dbf                  UNDOTBS1
/u01/app/oracle/oradata/catdb/system02.dbf                   SYSTEM

11 rows selected.

First create tablespace and then we will see how to drop..
SYS @ catdb > select file_name,tablespace_name from dba_data_files where TABLESPACE_NAME='UNDOTBS1';

FILE_NAME                                                    TABLESPACE_NAME
------------------------------------------------------------ ------------------------------
/u01/app/oracle/oradata/catdb/undotbs01.dbf                  UNDOTBS1
/u01/app/oracle/oradata/catdb/undotbs02.dbf                  UNDOTBS1

SYS @ catdb > drop tablespace UNDOTBS1 including contents;
drop tablespace UNDOTBS1 including contents
*
ERROR at line 1:
ORA-30013: undo tablespace 'UNDOTBS1' is currently in use

The error shows that the undo tablespace is in use.
Now to drop the current undo tablespace we need to create another undo tablespace and make it a default one.

SYS @ catdb > create undo tablespace undotbs2 datafile '/u01/app/oracle/oradata/catdb/undotbs02.dbf' size 10M reuse autoextend on maxsize 50M;
create undo tablespace undotbs2 datafile '/u01/app/oracle/oradata/catdb/undotbs02.dbf' size 10M reuse autoextend on maxsize 50M
*
ERROR at line 1:
ORA-01537: cannot add file '/u01/app/oracle/oradata/catdb/undotbs02.dbf' - file already part of database

can not create as we have already datafile of this name.
SYS @ catdb > create undo tablespace undotbs2 datafile '/u01/app/oracle/oradata/catdb/undotbs03.dbf' size 10M reuse autoextend on maxsize 50M;

Tablespace created.

SYS @ catdb > alter system set undo_tablespace=undotbs2 scope=both;

System altered.

SYS @ catdb > drop tablespace UNDOTBS1 including contents;^C

SYS @ catdb >
SYS @ catdb > select file_name,tablespace_name from dba_data_files where TABLESPACE_NAME='UNDO%';

no rows selected

SYS @ catdb > select file_name,tablespace_name from dba_data_files where TABLESPACE_NAME like 'UNDO%';

FILE_NAME                                                    TABLESPACE_NAME
------------------------------------------------------------ ------------------------------
/u01/app/oracle/oradata/catdb/undotbs01.dbf                  UNDOTBS1
/u01/app/oracle/oradata/catdb/undotbs02.dbf                  UNDOTBS1
/u01/app/oracle/oradata/catdb/undotbs03.dbf                  UNDOTBS2

SYS @ catdb > drop tablespace UNDOTBS1 including contents;

Tablespace dropped.

SYS @ catdb > select file_name,tablespace_name from dba_data_files where TABLESPACE_NAME like 'UNDO%';

FILE_NAME                                                    TABLESPACE_NAME
------------------------------------------------------------ ------------------------------
/u01/app/oracle/oradata/catdb/undotbs03.dbf                  UNDOTBS2

SYS @ catdb > select file_name,tablespace_name from dba_data_files;

FILE_NAME                                                    TABLESPACE_NAME
------------------------------------------------------------ ------------------------------
/u01/app/oracle/oradata/catdb/users01.dbf                    USERS
/u01/app/oracle/oradata/catdb/sysaux01.dbf                   SYSAUX
/u01/app/oracle/oradata/catdb/system01.dbf                   SYSTEM
/u01/app/oracle/oradata/catdb/example01.dbf                  EXAMPLE
/u01/app/oracle/oradata/catdb/example04.dbf                  EXAMPLE
/u01/app/oracle/oradata/catdb/example02.dbf                  EXAMPLE
/u01/app/oracle/oradata/catdb/tb_name01.dbf                  TB_NAME
/u01/app/oracle/oradata/catdb/testdb.dbf                     TESTDB
/u01/app/oracle/oradata/catdb/system02.dbf                   SYSTEM
/u01/app/oracle/oradata/catdb/shashi.dbf                     SHASHI
/u01/app/oracle/oradata/catdb/undotbs03.dbf                  UNDOTBS2

11 rows selected.

SYS @ catdb >