Sunday, January 3, 2016

How a database block is oragnized

Understanding  Database Block:

Database Blocks 
  Free used and unused blocks
Free blocks
Free blocks are blocks below the high water mark whose filling is below the pctused attribute. If an insert statement is executed, Oracle tries to insert the row in a free block. If it doesn't find a free block, it tries to insert it in an unused block. Free blocks of a segment are recorded in the segment's free list. See also dbms_space.free_blocks pctfree controls the threshold when a free block becomes a non-free block. 
Unused blocks
A block that has never been written to is a unused block. Unused blocks are beyond the high water mark. The number of unused blocks can be found with dbms_space.unused_space. 
Used blocks
A block that has been written to is a used blocks. Used blocks are below the high water mark. A free block is also a used block; but a used block is not always a free block. 
Getting the count of used and free blocks
With free blocks and unused blocks it is possible to get the count of used and free blocks, respectively, within a segment. 
Size of db blocks
Since Oracle 10g, it's possible to define multiple block sizes for a database. However, one of the sizes is the standard size. The size of the (standard) database block is determined by the db_block_size init parameter. This standard block size is the block size of blocks in the system tablepsace. 
Interested transaction list (ITL)
See initrans and maxtrans. Each ITL uses 24 bytes overhead. 
Block header
All blocks have a header that consists of: 
Block type
1=undo segment header, 2=undo segment block, 5=data segment header, 7=temporary table, 11=data file header, 14=Unlimited rollback segment header, 15=Unlimited deferred rollback segment header, 16=Unlimited data segment header, 17=Unlimited data segment header with FL groups, 18=Extent map block 
Block format
Is it an oracle 7 or oracle 8, (or 9, or 10???) formatted block? 
Relative database address 
SCN 
SCN sequence number 
Check value
Is used for integrity checking at the block level if db_block_checksum is set. 
Tail
The tail is not at the beginning of a block but at its end: it is used as a block consistency check. That is, the value must be equal to SCN + block type + SCN sequence number. 

RBA
The redo log address (RBA) plays an important role. . To be finished. 
Checking integrity of blocks
The integrity of database blocks can be checked with dbverify [dbv] (the database verify tool) or dbms_dbverify. 
Overhead
Not all bytes in a datablock actually store inserted (or selectable) data. Some bytes are also used for Oracle to organize the data so that it can be found again. These (additional) data are called overhead. The following applies for tables: The overhead consists of 
fixed header 
transaction header 
table directory 
row directory 
row header 
column size 
fixed header + transaction header + table directory + row directory = block header. 
Fixed header
The size of the block header is 57 bytes. 
Transaction header
The size of the transaction header is dependant on the initrans parameter. Specifically, its size is at least 23*initrans bytes. It can grow up to 23*maxtrans bytes. 
Table directory
The table directory is used to find the start position of each row (or row directory). The size of the table directory is 4 bytes*number of tables. Number of tables is important for cluster tables. For other tables, it's 1 (eg 4 bytes). 
Row directory
The row directory uses 2 bytes per stored row. 
Row header
For non cluster tables, the row header is 3 bytes. Each stored row has one row header. One byte is used to store flags, one byte to indicate if the row is locked (for example because it's updated but not commited), and one byte for the column count. 
Column size
Each column within a row needs at least 1 byte indicating the size of the data in the column. For varchar2's longer than 250 bytes, 3 bytes are used. 
PCTFREE
When calculating the amount of storable data in a block, the pctfree must also be considered. 
Corrupt blocks
Blocks might become corrupt. Information about corrupt blocks is recorded in the control files. 
Dirty blocks
to be finished... 
Checking block data integrity in memory
Oracle allows to check the integrity of data within a database block before it is written from memory (that is: buffer cache) to the harddisk by setting the parameter db_block_checksum to full. If a data integrity error is detected, an ORA-00227, ORA-00368 or ORA-01578 is raised. 
Database block address
Each block in the database is uniquely identified with the database block address (dba). 
x$bh
With x$bh, it is possible to query some information about db_blocks in the buffer cache.  

How to identify if it is a memory bottleneck or a hardware issue in a DB server

How to identify if it is a memory bottleneck or a hardware issue for a DB server:

If The server was hung due to out of memory error.

When all memory is exhausted, the out of memory killer (oom-killer) process begins killing processes and try to keep server alive.

 But if, the memory recover ended with rebooting the server it indicates a hardware issue.



Below are the error logs available in the server before the reboot.



Apr  4 09:10:09 testoraclerac-d kernel: Swap cache: add 0, delete 0, find 0/0, race 0+0

Apr  4 09:10:09 testoraclerac-d kernel: 22954 pages of slabcache

Apr  4 09:10:09 testoraclerac-d kernel: 722 pages of kernel stacks

Apr  4 09:10:09 testoraclerac-d kernel: 13 lowmem pagetables, 17746 highmem pagetables

Apr  4 09:10:09 testoraclerac-d kernel: Free swap:            0kB

Apr  4 10:20:09 testoraclerac-d kernel: Out of Memory: Killed process 15944 (oracle).

Apr  4 10:30:19 testoraclerac-d kernel: Out of Memory: Killed process 22600 (oracle).

Apr  4 10:30:19 testoraclerac-d kernel: Out of Memory: Killed process 22600 (oracle).

Apr  4 10:37:56 testoraclerac-d kernel: Out of Memory: Killed process 29002 (oracle).

Apr  4 10:37:56 testoraclerac-d kernel: Out of Memory: Killed process 29002 (oracle).

Apr  4 11:55:06 testoraclerac-d kernel: Memory: 4025116k/4653052k available (1710k kernel code, 98984k reserved, 1296k data, 228k init, 3211208k highmem)

Apr  4 11:55:14 testoraclerac-d kernel: Total HugeTLB memory allocated, 0

Apr  4 11:55:14 testoraclerac-d kernel: Freeing initrd memory: 1455k freed

Apr  4 11:55:16 testoraclerac-d kernel: Freeing unused kernel memory: 228k freed

How to remove and add or resubmit a Job to oracle scheduler using DBMS_JOB


How to remove and add or resubmit a Job to oracle scheduler using DBMS_JOB

Get the JOB ID or NUMBER
Select job from user_jobs where what like '%testjob_jobs.schedule_jobs%';

1342086

Remove the job from the scheduler:

exec DBMS_JOB.REMOVE(1342086);
or 
BEGIN 
  SYS.DBMS_JOB.REMOVE(984909);
COMMIT;
END;
/



Add or resubmit the job to the scheduler:

DECLARE
  X NUMBER;
BEGIN
  SYS.DBMS_JOB.SUBMIT
  ( job       => X 
   ,what      => 'declare out_list varchar2(32767); begin testjob_jobs.schedule_jobs(job,out_list,''N'',''N''); end;'
   ,next_date => to_date('10/01/2012 07:00:46','dd/mm/yyyy hh24:mi:ss')
   ,interval  => 'sysdate + 5/(24*60)'
   ,no_parse  => FALSE
  );
  SYS.DBMS_OUTPUT.PUT_LINE('Job Number is: ' || to_char(x));
COMMIT;
END;
/

How to modify or create a sequence with a new value

How to modify or create a sequence with a new value:-


Say the name of the sequence is SEQ_SUBSCRIPTION_NUMBER or SEQ__SOURCE_COMPANY_ID.


Check the current value of the sequence:


select  SEQ_SUBSCRIPTION_NUMBER.nextval from dual;


19154970


Check the current value of the sequence:


select  SEQ__SOURCE_COMPANY_ID.nextval from dual;


19005240


Alter the sequence by incrementing with positive or negative number as requred,this will reset the sequence.


 ALTER SEQUENCE TAPET.SEQ__SOURCE_COMPANY_ID increment BY -97250085 ;

 

 ALTER SEQUENCE TAPET.SEQ__SOURCE_COMPANY_ID increment BY 1

 

 select  SEQ__SOURCE_COMPANY_ID.nextval from dual;

 

 

 106483655


Now say you have to modify the sequence to some new starting value.

 

 8074950

 

 8074950


In that case drop and recreate the sequence with that starting value:

 

 DROP SEQUENCE TAPET.SEQ__SOURCE_COMPANY_ID;


CREATE SEQUENCE TAPET.SEQ_SUBSCRIPTION_NUMBER

  START WITH 8074950

  INCREMENT BY 1

  MAXVALUE 308999999

  MINVALUE 8074950

  NOCYCLE

  CACHE 20;

Wednesday, December 30, 2015

Remotely export and import between two database

How to do export import data remotely between two Oracle DB.

This could be helpful when we want to do a reguar copy of production data into pre prod for certain table.

 

The script below can be useful for the same:


#!/bin/bash
. $HOME/.bash_profile
################################################################################
#

################################################################################
# Revision History
################################################################################
# ---> For debugging purposes only

################################################################################
# supplemental area for extra information

################################################################################
# Parameters
HOME_BASE=/spare/tmp
TODAY=`date +"%Y%m%d"`
SCRIPT_DIR=${HOME_BASE}/admin/bin
SQL_DIR=${HOME_BASE}/admin/sql
EXP_DIR=${HOME_BASE}/admin/dump
LOG_DIR=${HOME_BASE}/admin/log
LOADER=${ORACLE_HOME}/bin/sqlldr
SQLPLUS=${ORACLE_HOME}/bin/sqlplus
EXP=${ORACLE_HOME}/bin/exp
EXPORT_OWNER=$1
EXPORT_PASSWD=$2
TNS_PPOD=PPRSADB_PPod
TNS_PROD=value_to_be_set
SCHEMA_OWNER=$3
SCHEMA_PASSWD=$4
LOG_FILE=$LOG_DIR/IIN_RUN.log
LOG_TEMP_FILE=/tmp/PIN_BIN_log.tmp
CRITICAL=NO
HOST=`hostname`
FTP_ERROR=0
EXP_ERR=0
FILES_HOOPED=0
FAILURE_CODE=1


################################################################################
if [[ ! -x $HOME_BASE/admin/bin/load_PIN_BIN.ksh ]]; then
    print "file load_PIN_BIN.ksh does not have execute permission."
    chmod +x  $HOME_BASE/bin/load_PIN_BIN.ksh
    return 1
fi


if [ -s ${LOG_TEMP_FILE} ] ; then
   rm ${LOG_TEMP_FILE}
fi

################################################################################
   echo "`date +"%D-%T"`: " >> ${LOG_FILE}

################################################################################
###                             MAIN                                         ###
################################################################################
# recreate log file for each run
if [ ! -f ${LOG_FILE} ]; then
   touch ${LOG_FILE}
else
   rm ${LOG_FILE}
   touch ${LOG_FILE}
fi

# LogMsg ${DASHED_LINE}

## Export the PTS_PIN_BIN from PPod.

exp $EXPORT_OWNER/$EXPORT_PASSWD@$TNS_PPOD  file=$EXP_DIR/PP_PIN_BIN$TODAY.dmp tables=PTSADB.PTS_PIN_BIN statistics=none buffer=2000000 log=$LOG_DIR/EXPPIN_BIN_PP$TODAY.log


## Take a backup of PTS_PIN_BIN table from production

exp $SCHEMA_OWNER/$SCHEMA_PASSWD  file=$EXP_DIR/PROD_PIN_BIN$TODAY.dmp tables=DTSPROD.PTS_PIN_BIN statistics=none buffer=2000000 log=$LOG_DIR/EXPPIN_BIN_PRO$TODAY.log

### Truncate the table PTS_PIN_BIN from production

${SQLPLUS} -silent << EOLINE
${SCHEMA_OWNER}/${SCHEMA_PASSWD}
set echo off
set heading on
set pagesize 0
set feedback off
set linesize 250
spool ${LOG_DIR}/PIN_BIN_CLEANUP.log
@${SQL_DIR}/PIN_BIN_CLEANUP.sql
spool off
exit;
EOLINE



### IMPORT THE TABLE

imp $SCHEMA_OWNER/$SCHEMA_PASSWD tables=PTS_PIN_BIN FROMUSER=PTSADB TOUSER=DTSPROD  rows=y ignore=y grants=n commit=y file=$EXP_DIR/PP_PIN_BIN$TODAY.dmp log=$LOG_DIR/IMP_PIN_BIN_PPT$TODAY.log




#CleanUp


IN-DOUBT TRANSACTIONS IN Distributed Environment:-

How to Handle IN-DOUBT TRANSACTIONS IN Distributed Environment:


A transaction is in-doubt when there is a failure during any aspect of the two-phase commit.

Distributed transactions become in-doubt in the following ways:

A server machine running Oracle software crashes.

A network connection between two or more Oracle databases involved in distributed processing is disconnected.

An unhandled software error occurs.

STATE Column of DBA_2PC_PENDING

collecting     This category normally applies only to the global coordinator or local coordinators. The node is currently collecting information from other database servers before it can decide whether it can prepare.
prepared     The node has prepared and may or may not have acknowledged this to its local coordinator with a prepared message. However, no commit request has been received. The node remains prepared, holding any local resource locks necessary for the transaction to commit.
committed     The node (any type) has committed the transaction, but other nodes involved in the transaction may not have done the same. That is, the transaction is still pending at one or more nodes.
forced commit     A pending transaction can be forced to commit at the discretion of a database administrator. This entry occurs if a transaction is manually committed at a local node by a database administrator. 
forced abort (rollback)    


Scenarios 1:

PD team will inform DBA to look into the indoubt transaction issue.
From database point of view we will not come across any issue regards to database.
Once we got information from PD on this, the following steps needs to be followed to rollback the indoubt transactions.

a) logon to the database and check the STATUS column of DBA_2PC_PENDING table.
We will find one or more indoubt transactions. Check for GLOBAL_TRAN_ID column.
b) Execute the below statement,

ROLLBACK FORCE ‘LOCAL_TRAN_ID’;
COMMIT;

c) If we check once again the DBA_2PC_PENDING table, this transaction will be rolled back.

Scenarios 2:

If the transaction did not rollback after the above steps in scenarios 1 then we need to bounce the database.

Though we are DBA on this application, we can startup and shutdown the database.

a) After executing the ROLLBACK FORCE ‘global_tran_id’ , still we find the entry in the DBA_2PC_PENDING table.
b) We will bounce the database after all the applications are brought down.
c) Again follow the steps mentioned in scenarios 1 point (a), (b) and (c).

BEGIN

   DBMS_TRANSACTION.PURGE_LOST_DB_ENTRY ('LOCAL_TRAN_ID');

END;

EXECUTE DBMS_TRANSACTION.PURGE_LOST_DB_ENTRY ('LOCAL_TRAN_ID');

Tuesday, December 29, 2015

Oracle BLOB Inserting and retrieving data:

  How to insert and select data in Oracle BLOB column:

SQL> CREATE TABLE LABORAOBJECTS

(
  NAME        VARCHAR2(128 BYTE)                NOT NULL,
  OBJECTDATA  BLOB
)  2    3    4    5  ;

Table created.

SQL> Insert into LABORAOBJECTS(NAME, OBJECTDATA)  Values ('BlobXYZabcPool.stage', RAWTOHEX('22'));

1 row created.

SQL> commit;

Commit complete.

SQL> select * from LABORAOBJECTS;

NAME
--------------------------------------------------------------------------------
OBJECTDATA
--------------------------------------------------------------------------------
BlobXYZabcPool.stage
3232


This will give you the hexadecimal value to get the exact value which we have inserted use the below method:



SQL>  select * from LABORAOBJECTS;

NAME                                                                OBJECTDATA
------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------
BlobXYZabcPool.stage                                                3232

SQL> col OBJECTDATA format a56
SQL> /

NAME                                                                OBJECTDATA
------------------------------------------------------------------- --------------------------------------------------------
BlobXYZabcPool.stage                                                3232



SQL> SELECT ASCIISTR(CHR(TO_NUMBER('3232','xxxx'))) from dual;

ASCIISTR(C
----------
22
---------------