Monday, September 1, 2014

Auditing functions and stored procedures in Oracle

How to know that a specific procedure or function has been executed by which user and when?

Lot many times we would like to track the execution of a fuction or SP. For example, who executed the fuction / SP?  What time this was executed?  and many more. You need to do that with the help of auditing database. Here I have shown an example of how to enable audit on function.

Please go through the below steps. Hope this helps you.


C:\Users\Ashish>sqlplus "/as sysdba"

SQL*Plus: Release 11.2.0.1.0 Production on Thu Aug 7 15:17:34 2014

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> show parameter audit

NAME                                 TYPE                 VALUE
----------------------------- -----------          ------------------------------
audit_file_dest                   string             C:ASHISH\ADMIN\PRIMARY\ADUMP
audit_sys_operations         boolean          FALSE
audit_trail                          string              DB
SQL> set pages 1000
SQL> set lines 180
SQL>
SQL>
SQL> conn ashish/ashish
Connected.
SQL>
SQL>
SQL> create or replace function TEST_AUDIT_BY_ACCESS
  2  return date
  3  is
  4  begin
  5  return sysdate;
  6  end;
  7  /

Function created.

SQL>
SQL>
SQL>
SQL> conn sys as sysdba
Enter password:
Connected.
SQL>
SQL> audit execute on ashish.TEST_AUDIT_BY_ACCESS by access;

Audit succeeded.

SQL> select os_username,username,obj_name,owner from dba_audit_trail where obj_name='TEST_AUDIT_BY_ACCESS';

no rows selected

SQL> conn ashish/ashish
Connected.

SQL> select TEST_AUDIT_BY_ACCESS from dual;

TEST_AUDI
---------
07-AUG-14


SQL> conn sys as sysdba
Enter password:
Connected.
SQL>
SQL>  select os_username,username,obj_name,owner from dba_audit_trail where obj_name='TEST_AUDIT_BY_ACCESS';

OS_USERNAME        USERNAME         OBJ_NAME                                         OWNER
---------------                    -------------        ---------------------                                  -------------
Ashish                            ASHISH          TEST_AUDIT_BY_ACCESS                ASHISH

SQL>



You can fetch more columns from dba_audit_trail as per your requirement.


SQL> desc dba_audit_trail
 Name                                                                                                  Null?    Type
 ----------------------------------------------------------------------------------------------------- --------

--------------------------------------------------------------------
 OS_USERNAME                                                                                                    VARCHAR2(255)
 USERNAME                                                                                                       VARCHAR2(30)
 USERHOST                                                                                                       VARCHAR2(128)
 TERMINAL                                                                                                       VARCHAR2(255)
 TIMESTAMP                                                                                                      DATE
 OWNER                                                                                                          VARCHAR2(30)
 OBJ_NAME                                                                                                       VARCHAR2(128)
 ACTION                                                                                                NOT NULL NUMBER
 ACTION_NAME                                                                                                    VARCHAR2(28)
 NEW_OWNER                                                                                                      VARCHAR2(30)
 NEW_NAME                                                                                                       VARCHAR2(128)
 OBJ_PRIVILEGE                                                                                                  VARCHAR2(16)
 SYS_PRIVILEGE                                                                                                  VARCHAR2(40)
 ADMIN_OPTION                                                                                                   VARCHAR2(1)
 GRANTEE                                                                                                        VARCHAR2(30)
 AUDIT_OPTION                                                                                                   VARCHAR2(40)
 SES_ACTIONS                                                                                                    VARCHAR2(19)
 LOGOFF_TIME                                                                                                    DATE
 LOGOFF_LREAD                                                                                                   NUMBER
 LOGOFF_PREAD                                                                                                   NUMBER
 LOGOFF_LWRITE                                                                                                  NUMBER
 LOGOFF_DLOCK                                                                                                   VARCHAR2(40)
 COMMENT_TEXT                                                                                                   VARCHAR2(4000)
 SESSIONID                                                                                             NOT NULL NUMBER
 ENTRYID                                                                                               NOT NULL NUMBER
 STATEMENTID                                                                                           NOT NULL NUMBER
 RETURNCODE                                                                                            NOT NULL NUMBER
 PRIV_USED                                                                                                      VARCHAR2(40)
 CLIENT_ID                                                                                                      VARCHAR2(64)
 ECONTEXT_ID                                                                                                    VARCHAR2(64)
 SESSION_CPU                                                                                                    NUMBER
 EXTENDED_TIMESTAMP                                                                                             TIMESTAMP(6) WITH TIME

ZONE
 PROXY_SESSIONID                                                                                                NUMBER
 GLOBAL_UID                                                                                                     VARCHAR2(32)
 INSTANCE_NUMBER                                                                                                NUMBER
 OS_PROCESS                                                                                                     VARCHAR2(16)
 TRANSACTIONID                                                                                                  RAW(8)
 SCN                                                                                                            NUMBER
 SQL_BIND                                                                                                       NVARCHAR2(2000)
 SQL_TEXT                                                                                                       NVARCHAR2(2000)
 OBJ_EDITION_NAME                                                                                               VARCHAR2(30)
 DBID                                                                                                           NUMBER

Thursday, April 24, 2014

Drop table commands ends up with ORA-02449

Hello,

Many may have noticed that when you try to drop table we face ORA-02449 error. This is due to the primary key in the table which is referring to the foreign key of another table.

Error occurred is :

ORA-02449: unique/primary keys in table referenced by foreign keys


To overcome this situation, make sure you do not need this table. And then fire below command to delete the table. This drop statement with cascade constraints option will delete the primary key constraints from the table and drop the table.


DROP TABLE EMP CASCADE CONSTRAINTS;

Monday, February 24, 2014

Unable to drop ASM diskgroup getting ORA-15027

Hello,

Recently I came across such situations that I was unable to drop the ASM diskgroup. I was getting  ORA-15027 . Later I realized that the spfile fot the ASM instance is available in the diskgroup.

To solve this, first we need to move the spfile to different location. Once that is done, we can delete the desired diskgroup.



StateTypeRebalAUTotal_MBFree_MBName
MOUNTEDEXTERNN104857614880421487967DATA


 Here is the desired, diskgroup to be dropped.

ASMCMD> dropdg -r DATA
ORA-15039: diskgroup not dropped
ORA-15027: active use of diskgroup "DATA" precludes its dismount (DBD ERROR: OCIStmtExecute)
ASMCMD> spget
+DATA/asm/asmparameterfile/registry.253.840344755
ASMCMD> spmove '+DATA/asm/asmparameterfile/registry.253.840344755' '/ora/app/oracle/product/11.2.0.3/grid/dbs/spfile+ASM.ora';
ORA-15032: not all alterations performed
ORA-15028: ASM file '+DATA/asm/asmparameterfile/registry.253.840344755' not dropped; currently being accessed (DBD ERROR: OCIStmtExecute)
ASMCMD> spget
/ora/app/oracle/product/11.2.0.3/grid/dbs/spfile+ASM.ora;
ASMCMD> exit
-bash-3.2$ sqlplus "/as sysasm"

SQL*Plus: Release 11.2.0.3.0 Production on Mon Feb 24 08:31:24 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Automatic Storage Management option

SQL> shut immediate
ASM diskgroups volume disabled
ASM diskgroups dismounted
ASM instance shutdown
SQL> startup
ASM instance started

Total System Global Area  413372416 bytes
Fixed Size                  2228904 bytes
Variable Size             385977688 bytes
ASM Cache                  25165824 bytes
ASM diskgroups mounted
ASM diskgroups volume enabled
SQL> show parameter spfile

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
spfile                               string      /ora/app/oracle/product/11.2.0
                                                 .3/grid/dbs/spfile+ASM.ora;
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Automatic Storage Management option
-bash-3.2$ asmcmd
ASMCMD> lsdg
State    Type    Rebal  Sector  Block       AU  Total_MB  Free_MB  Req_mir_free_MB  Usable_file_MB  Offline_disks  Voting_files  Name
MOUNTED  EXTERN  N         512   4096  1048576   1488042  1487967                0         1487967              0             N  DATA/
ASMCMD> spget
/ora/app/oracle/product/11.2.0.3/grid/dbs/spfile+ASM.ora;
ASMCMD> dropdg -r DATA
ASMCMD> lsdg
ASMCMD> exit


 Good luck !!!!!!!!!!!!!!!!!!!!

Move asm spfile from file system to asm filesystem

Hello ,

Here is the below step to copy the asm spfile from non asm filesystem to asm filesystem.

1.) Make sure you have pfile in grid home

bash-3.2$ ls -ltr
total 24
-rw-r--r-- 1 oracle oinst 2851 May 15  2009 init.ora
-rw-r----- 1 oracle oinst 1536 Mar 29 05:06 orapw+ASM
-rw-rw---- 1 oracle oinst 1200 Mar 29 08:31 ab_+ASM.dat
-rw-rw---- 1 oracle oinst 1544 Mar 29 09:02 hc_+ASM.dat
-rw-r--r-- 1 oracle oinst  359 Mar 29 09:06 init+ASM.ora

bash-3.2$ sqlplus "/as sysasm"

SQL*Plus: Release 11.2.0.3.0 Production on Mon Feb 24 09:11:12 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup pfile='init+ASM.ora'
ASM instance started

Total System Global Area  413372416 bytes
Fixed Size                  2228904 bytes
Variable Size             385977688 bytes
ASM Cache                  25165824 bytes
ASM diskgroups mounted
ASM diskgroups volume enabled
SQL> show parameter pfile

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
spfile                               string
SQL> show parameter spfile

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
spfile                               string

2.) Create spfile from pfile

SQL> create spfile='+DATA' from pfile='init+ASM.ora';

File created.


SQL> shut immediate
ASM diskgroups volume disabled
ASM diskgroups dismounted
ASM instance shutdown
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Automatic Storage Management option
bash-3.2$ sqlplus "/as sysasm"

SQL*Plus: Release 11.2.0.3.0 Production on Mon Feb 24 09:17:00 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ASM instance started

Total System Global Area  413372416 bytes
Fixed Size                  2228904 bytes
Variable Size             385977688 bytes
ASM Cache                  25165824 bytes
ASM diskgroups mounted
ASM diskgroups volume enabled
SQL> show parameter spfile

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
spfile                               string      +DATA/asm/asmparameterfile/registry.253.849870843


That's all.


Error you may encounter is :



ORA-15124: ASM file name contains an invalid alias name

Solution is ==> Don't try to give absolute name for spfile, just mention the diskgroup name as above example.