Querying Datafiles, ASM and Segments in Oracle11g

Querying Datafiles, ASM and Segments in Oracle11g (including tablespaces) in Oracle11g, 10g and 9i. This is stuff I have online on my main website and in books, but I’ve tweaked it a bit over the years.

ASM:

set wrap off lines 132 pages 5000;
col name format a30

select name,state,total_mb,free_mb from v$asm_diskgroup;

select name,state
   ,round(sum(total_mb/1024),0) "UsedGb"
   ,round(sum(free_mb/1024),0) "FreeGb" 
from v$asm_diskgroup 
group by grouping sets(name,state);

Temp space:

select tablespace_name,file_name from dba_data_files
union
select tablespace_name,file_name from dba_temp_files 
order by 1,2;

Datafiles:

col tbs format a15
col Tb format 990
col Gb format 999,990
col Mb format 999,999,990
col file_name format a30
select tablespace_name "Tbs",autoextensible,maxbytes
                ,round(maxbytes/1024/1024,0) "Mb"
                ,round(maxbytes/1024/1024/1024,0) "Gb"
                ,round(maxbytes/1024/1024/1024/1024,0) "Tb"
                , file_name 
from dba_data_files 
order by tablespace_name;

Tablespaces with free space:

set wrap off lines 132 pages 5000;
col GBUsed format 9990
col GBFree format 9990
col %Free format 990

select      a.TABLESPACE_NAME
          , sum(round((nvl(a.BYTES,0))/1024/1024/1024,0)) "GBHighWater"
          , sum(round((nvl(a.BYTES,0)-nvl(b.BYTES,0))/1024/1024/1024,0)) "GBUsed"
          , sum(round(nvl(b.BYTES,0)/1024/1024/1024,0)) "GBFree"
          , sum(round(100-round(((nvl(a.BYTES,0)-nvl(b.BYTES,0))/nvl(a.BYTES,0))*100,0),0)) "%Free"
from      
                (
                                select    TABLESPACE_NAME,
                                                sum(BYTES) BYTES 
                                from      dba_data_files 
                                group    by TABLESPACE_NAME
                )
                a,
                (
                                select    TABLESPACE_NAME,
                                                sum(BYTES) BYTES ,
                                                max(BYTES) largest 
                                from      dba_free_space 
                                group    by TABLESPACE_NAME
                )
                b
where   a.TABLESPACE_NAME=b.TABLESPACE_NAME
group by rollup(a.tablespace_name)
order by a.TABLESPACE_NAME;

Temp tablespaces:


set wrap off lines 132 pages 5000;
col GBUsed format 9990
select	  TABLESPACE_NAME
	, round(sum(BYTES)/1024/1024/1024,0) "GbUsed" 
	, round(sum(maxbytes)/1024/1024/1024,0)	"MaxGb"
from dba_temp_files 
group by TABLESPACE_NAME
order by TABLESPACE_NAME;

Tablespace attributes:

col tab format a10
select 	 tablespace_name "tbs"
	,bigfile
	,contents
	,extent_management "ext"
	,allocation_type "alltyp"
	,segment_space_management "ssm"
from dba_tablespaces
order by tablespace_name;

Segment space – be careful with this one as it can be kinda slow:

set lines 132 wrap off pages 5000;
col Tbs format a10;
col owner format a10;
col segt format a10;
col segn format a10;
col Tb format 990;
col Gb format 999990;
col Mb format 999999990;
SELECT   tablespace_name "Tbs"
        , owner
                , segment_type "SegT"
                , segment_name "SegN"
                , sum(ROUND(bytes/1024/1024/1024/1024)) "Tb"
                , sum(ROUND(bytes/1024/1024/1024)) "Gb"
                , sum(ROUND(bytes/1024/1024)) "Mb"
                , sum(bytes) "Bytes"
                , sum(blocks) "Blocks"
FROM dba_segments
where owner='Mrs Ada Foo'
group by owner, tablespace_name, segment_type, segment_name;

Oracle Table, Column and Constraint Metadata


--look at tables
select * from dba_tables 
order by table_name;

--adding columns
select * from dba_tables t join dba_tab_columns c on(c.table_name = t.table_name)
...where t.owner='...';

--now just the constraints:
set wrap off linesize 132 pages 80
column tab format a30
column key format a16
column cons format a30
select	 t.table_name "Tab"
	,decode(t.constraint_type,'P','Primary','R','Foreign','U','Unique/Natural','Inversion') "Key"
	,t.constraint_name "Cons"
from dba_constraints t 
where t.constraint_type in ('P','R','U')
--and t.owner in(' ...')
--and t.table_name like ' ... '
order by t.owner, t.table_name, t.constraint_type;

--now add the columns in as well:
set wrap off linesize 132 pages 80
column owner format a10
column tab format a30
column key format a16
column cons format a30
column col format a30
column pos format 990
BREAK ON tab NODUPLICATES SKIP 2 ON NAME NODUPLICATES 
select	 t.owner
	,t.table_name "Tab"
	,decode(t.constraint_type,'P','Primary','R','Foreign','U','Unique/Natural','Inversion') "Key"
	,t.constraint_name "Cons"
	,c.column_name "Col"
	,c.position "Pos"
from dba_constraints t join dba_cons_columns c on(t.owner=c.owner and t.table_name = c.table_name and t.constraint_name = c.constraint_name)
where t.constraint_type in ('P','R','U')
--and t.owner in(' ... ')
--and t.table_name in (' ... ')
order by t.owner,t.table_name, t.constraint_type, c.position;

Oracle Auto Restart

This page covers Oracle Auto Restart and automatically stopping and starting an Oracle Database on Linux and UNIX restart.

In the /etc/oratab file you might have to set the flags to Y (traditionally this has always been the default).

RH4 for Oracle10g may also have an incorrect setting for the lsnrctl program that prevents listener startup in an ASM installation.

The Startup & Shutdown Script

Add this file /etc/init.d/oracle:

#!/bin/sh
# chkconfig: 345 95 30
# description: Oracle auto start-stop script.
ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
case "$1" in
        'start')
                su - oracle -c $ORACLE_HOME/bin/dbstart				#Database - handles multiple s
                su - oracle -c "$ORACLE_HOME/bin/lsnrctl start"			#Listener
                su - oracle -c "$ORACLE_HOME/bin/lsnrctl dbsnmp_start"		#Agent (might be installed elsewhere with RAC and/or a multiple database environment)
                exit 0
        ;;
        'stop')
                su - oracle -c "$ORACLE_HOME/bin/lsnrctl dbsnmp_stop"           #Agent (might be installed elsewhere with RAC and/or a multiple database environment)
                su - oracle -c "$ORACLE_HOME/bin/lsnrctl stop"
                su - oracle -c $ORACLE_HOME/bin/dbshut
        ;;
        *)
                echo "usage: start or stop"
                exit 0
        ;;
esac
exit 0

Setting the Shutdown & Startup Script to Execute on Operating System Restart

Automate the execution of the /etc/init.d script by adding script copies or symbolic links to the startup and shutdown directory, ie. the /etc/rc2.d and /etc/rc0.d directories respectively.

The shutdown directory is /etc/rc0.d, which will execute process (K)ill scripts on *NIX shutdown where those scripts are executed in order of the integers in the filenames, ie. K02test1 is executed before K03test2. Place the oracle shutdown scripts above K00<name> but as close to K00<name> as possible, ie. execute the Oracle process shutdown first.

cd /etc/rc0.d
ln -s /etc/init.d/oracle K30oracle

The startup directory is /etc/rc2.d. Note that the startup directory contains both (K)ill and (S)tartup scripts, or links. If anything needs to be killed before starting when *NIX starts-up then this is where it goes. Place the oracle startup link in the sequence of file/link names as far to the end of the list as possible.

cd /etc/rc2.d
ln -s /etc/init.d/oracle S95oracle

And the:

chmod 755 /etc/init.d/oracle

Please note that this page was constructed from a Solaris blade server environment but should apply to Linux and other flavors of UNIX as well.

Continue reading

Using Agile to Develop with Lowered Risk

This page covers using Agile to develop with lowered risk as compared to the Waterfall method.

What is Agile? It’s an approach to development that allows a project to be broken up into distinctly and autonomously developed sections, in a sequence; the event sequence implying that you can’t have multiple teams concurrently developing multiple dependent tracks for the same project because each step is likely to be dependent on subsequent ones. So Agile is a bit like working with an object database or an object SDK like Java, allowing a task to broken into mutually exclusive parts or black-boxed objects.

The opposite of Agile is called the Waterfall method of development for software, which actually stems from the manufacturing industry such as building a plant to make cars; so just imagine how expensive it is to rearrange the bits and pieces in a Caterpillar tractor factory just after factory’s been built. Software development is much easier to rearrange mid-project or even after it’s all built, as compared to some kind of hugely expensive manufacturing or engineering project. Yes it is!

So Agile was devised and is now being gradually adopted easily by some, and painfully by some of the more traditional types of software engineers. Then again not every software development project can be built using the Agile method but given the advent of object technology it would make sense that many software development projects can be built in simpler to manage sections. So why isn’t everyone using Agile? Tradition? I hope not.

Sometime in the recent past some of the processing associated with data warehousing was renamed to something called business intelligence, and the reason was because data warehousing had become of age to a certain extent in that its ownership and purpose was being highlighted as being one of business oriented. So it is now a recognized fact that the business side rather than the operational side of a company has more of a vested interest in guiding what data warehouses deliver to its customers, which is also the business side of a company (data warehouses are often for internal customers). The reason why is because a data warehouse can be used to forecast future patterns based on past trends and thus what the data warehouse delivers can have a major effect on how the business is run.

The result is that the Waterfall approach of functional specifications defining a software product from start through to completion, is now changed to a financially leaner step-by-step process that is broken down into somewhat autonomous parts, which can be developed one after the other. Also flexibility is built into the process such that the business users can literally changer their minds anywhere during the development process, even in the middle of a sprint, probably driving the developers a little crazy with frustration. Consequently the business users do need to have an understanding that they cannot place unreasonable demands on technical developers such as allowing deadlines to be adjusted as constant changes are made to requirements, never meeting any kind of deadline whatsoever.

In conclusion the Agile approach involves ownership by business users as opposed to software developers, in addition to lots of frequent changes without the need for technical people to be sticklers for sticking to requirements documentation; the idea is to allow for the business users to change their minds during the development process so as to allow for a product that matches business requirements rather than a technical specification that could be out of date the day it’s printed. Also tolerance to code rewrites and a lack of fixed price constrained contracts are important facets of the Agile approach – they would be. The big problem with the Waterfall approach is that it is inflexible as it requires sticking to a specification that runs through a project right from start to finish, and any changes during the process are not only expensive but also even psychologically difficult to deal with – and also very frustrating. In short, the Waterfall approach is really a very traditional and retrogressive approach, and possibly even culturally inappropriate to a field that is by its very nature progressive because it is constantly undergoing enormous change, increasing in capabilities every eighteen months – so why did it take us so long to start using the Agile approach? It should not have.

Stop a Rollback in Oracle

This page covers stop, abort or termination of a Rollback in Oracle.

“You can abort rollback but I think it can mess up the database”, was my last answer I gave when I was asked this question. I also searched Google for “abort a rollback in oracle” and I found some interesting details that I had never found because the client was attempting a restart, and obviously that restart was still trying to do the rollback (or perhaps rollback the rollback itself):

For Google abort a rollback in oracle

Or google stop a rollback in oracle

Just don’t forget to reset the trace context at the end again otherwise you might have a very bad day some time soon :)

Also, I seen this particular issue before with huge performance slow downs as a result of rollbacks and/or attempts to abort of those rollbacks when working with fast refresh materialized, and perhaps forgotten bu incredibly large materialized view logs. If you think about it: the materialized view is constantly being appended to, and you perform a fast refresh that may be large or has not been done for a while, and maybe you’re trying to do replication over a global network … what happens is perhaps the append process might catch up with the fast refresh process in terms of transactional snapshot containment and then you might get a lot of conflict between the two competing processes. And you probably won’t hit a snapshot too old error either because there isn’t enough rollback to actually run out of rollback space, and so on and so forth.

Memory in Linux

Check memory space in Linux:

grep MemTotal /proc/meminfo

Check swap space in Linux:

grep SwapTotal /proc/meminfo

Use the

free

command to find RAM and swap space. To increase swap space and if you’ve installed Linux with default setup then all space will be used and chances are you can move extents from an already allocated volume to swap. Oracle Enterprise Linux installs with swap space default size of 1Gb and Oracle Database and Cluster services wants at least 1.5Gb of swap space, which is odd.