Wednesday, May 27, 2015

Oracle : LOGGING / NOLOGGING

LOGGING is a keyword that used on creating the index, table or tablespace. If we use LOGGING when creating the object then DML operations on the objects are logged in redo log file. If we use NOLOGGING when creating the object, in some cases DML operations on the objects are not logged in redo log file. There are a lot of questions asked on the forums about LOGGING/NOLOGGING. Some of the questions as follows: What is the advantage of NOLOGGING? When should we use NOLOGGING ? If NOLOGGING is used on the tablespace level, can I use LOGGING on the tables which are in the NOLOGGING tablespace?

OK. Let us examine one by one.

let’s create a NOLOGGING tablespace.

CREATE TABLESPACE NOLOGGING_TS DATAFILE
‘/data_oracle/nologging_ts.dbf’ SIZE 1024M AUTOEXTEND OFF
NOLOGGING
ONLINE
PERMANENT
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 10M
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;
Now, I will create a table in this tablespace. And I will not specify LOGGING or NOLOGGING when creating the table.

CREATE TABLE oracle_test(id NUMBER)
TABLESPACE nologging_ts;

Check the table LOGGING status with following query. You will see LOGGING=NO

SELECT table_name, logging
FROM dba_tables
WHERE tablespace_name = ‘NOLOGGING_TS’
TABLE_NAME                   LOGGING
—————————— ——-
ORACLE_TEST                         NO

Now, I will create table in NOLOGGING_TS tablespace with LOGGING clause.

CREATE TABLE oracle_test2 (id NUMBER) TABLESPACE nologging_ts LOGGING;

Rerun above query to see table LOGGING status. You will see LOGGING=YES

SELECT tablespace_name, logging
FROM dba_tables
WHERE table_name = ‘ORACLE_TEST2′
TABLE_NAME                                                                            LOGGING
——————————                                                                  ——-
ORACLE_TEST                         NOORACLE_TEST2                       YES

In that case, If you specify NOLOGGING on the tablespace level, then default logging status will be  NOLOGGING that the objects to be created within the tablespace.
If you create a table as NOLOGGING then you can change its logging status to LOGGING. Or the opposite might happen.

ALTER TABLE oracle_test LOGGING;
ALTER TABLE oracle_test2 NOLOGGING;
This situation applies in tablespace. If you create a tablespace as NOLOGGING then you can change its logging status to LOGGING.

ALTER TABLESPACE nologging_ts LOGGING;
After changing a tablespace logging status to LOGGING, NOLOGGING created objects will remain as NOLOGGING. If you create a new table in this tablespace then created table logging status will be LOGGING. In some cases, logging can continue on NOLOGGING. So what are these conditions?


Table mode
Insert mode
Archive Log Mode
Redo generation
LOGGING
APPEND
ARCHIVE LOG
REDO generated
NOLOGGING
APPEND
ARCHIVE LOG
no REDO
LOGGING
No APPEND
ARCHIVE LOG
REDO generated
NOLOGGING
No APPEND
ARCHIVE LOG
REDO generated
LOGGING
APPEND
NO ARCHIVE LOG
no REDO
NOLOGGING
APPEND
NO ARCHIVE LOG
no REDO
LOGGING
No APPEND
NO ARCHIVE LOG
REDO generated
NOLOGGING
No APPEND
NO ARCHIVE LOG
REDO generated


“Direct Path Load” operations on the NOLOGGING table are not generated redo. If “force logging” is enabled then always redo is generated. Even if table is LOGGING mode, the “Direct Path Load” operations doesn’t generate redo log  in NO ARCHIVE LOG. Even if the database in ARCHIVE LOG mode, redo is not generated on NOLOGGING table.

Direct Load Insert is a faster way of running an INSERT statement. It is particularly useful for inserting large numbers of rows. Direct Load Insert differs from Conventional Insert in that it bypasses the buffer cache.

Creating a table as NOLOGGING takes less time than to create LOGGING. Let a simple test.

SQL> set timing on
SQL> create table oracle_logging logging as select * from dba_tables;
Table created.
Elapsed: 00:00:01.20

Now let’s create it with nologging.

SQL> create table oracle_nologging nologging as select * from dba_tables;
Table created.
Elapsed: 00:00:00.60
And there is an important point. If you delete a row from NOLOGGING table then you can rollback. Baceause of  it is not depended on LOGGING/NOLOGGING. It is depended on UNDO segments. Let’s do a simple test.

Create a NOLOGGING table.
CREATE TABLE oracle_test3 (
isim varchar2(10)
)
NOLOGGING;

insert a row

insert into oracle_test3 values(‘oracle’);
commit;
delete a row

delete from oracle_test3;
After rollback, you can see deleted row. So, the rollback processis not associated with  LOGGING / NOLOGGING .

rollback;select * from oracle_test3;



Monday, May 4, 2015

Oracle Crontab

Linux Crontab Format


MIN HOUR DOM MON DOW CMD
Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax)

Field          Description                  Allowed Value
MIN           Minute field                  0 to 59
HOUR        Hour field                     0 to 23
DOM          Day of Month               1-31
MON          Month field                  1-12
DOW          Day Of Week               0-6
CMD          Command                     Any command to be executed.

  • Scheduling a Job For a Specific Time


The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.

Please note that the time field uses 24 hours format. So, for 10 AM use 10, and for 10 PM use 22.

30 08 10 06 * /home/drive/full-backup
30 – 30th Minute
08 – 08 AM
10 – 10th Day
06 – 6th Month (June)
* – Every day of the week

  • Schedule a Job For More Than One Instance (e.g. Twice a Day)


The following script take a incremental backup twice a day every day.

This example executes the specified incremental backup shell script (incremental-backup) at 11:00 and 16:00 on every day. The comma separated value in a field specifies that the command needs to be executed in all the mentioned time.

00 11,16 * * * /home/drive/bin/incremental-backup
00 – 0th Minute (Top of the hour)
11,16 – 11 AM and 4 PM
* – Every day
* – Every month
* – Every day of the week


  • Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)


If you wanted a job to be scheduled for every hour with in a specific range of time then use the following.

Cron Job everyday during working hours
This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m

00 09-18 * * * /home/drive/bin/check-db-status
00 – 0th Minute (Top of the hour)
09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
* – Every day
* – Every month
* – Every day of the week
Cron Job every weekday during working hours

This example checks the status of the database every weekday (i.e excluding Sat and Sun) during the working hours 9 a.m – 6 p.m.

00 09-18 * * 1-5 /home/drive/bin/check-db-status
00 – 0th Minute (Top of the hour)
09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
* – Every day
* – Every month
1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)

  • How to View Crontab Entries?


View Current Logged-In User’s Crontab entries
To view your crontab entries type crontab -l from your unix account as shown below.

user@dev-db$ crontab -l
@yearly /home/drive/annual-maintenance
*/10 * * * * /home/drive/check-disk-space

[Note: This displays crontab of the current logged in user]

View Root Crontab entries
Login as root user (su – root) and do crontab -l as shown below.

root@dev-db# crontab -l
no crontab for root

Crontab HowTo: View Other Linux User’s Crontabs entries
To view crontab entries of other Linux users, login to root and use -u {username} -l as shown below.

root@dev-db# crontab -u user2 -l
@monthly /home/user2/monthly-backup
00 09-18 * * * /home/user2/check-db-status

  • How to Edit Crontab Entries?


Edit Current Logged-In User’s Crontab entries
To edit a crontab entries, use crontab -e as shown below. By default this will edit the current logged-in users crontab.

user@dev-db$ crontab -e
@yearly /home/drive/centos/bin/annual-maintenance
*/10 * * * * /home/drive/debian/bin/check-disk-space
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C

[Note: This will open the crontab file in Vim editor for editing.
Please note cron created a temporary /tmp/crontab.XX... ]
When you save the above temporary file with :wq, it will save the crontab and display the following message indicating the crontab is successfully modified.

~
"crontab.XXXXyjWkHw" 2L, 83C written
crontab: installing new crontab
Edit Root Crontab entries
Login as root user (su – root) and do crontab -e as shown below.

root@dev-db# crontab -e

Edit Other Linux User’s Crontab File entries
To edit crontab entries of other Linux users, login to root and use -u {username} -e as shown below.

root@dev-db# crontab -u user2 -e
@monthly /home/user2/fedora/bin/monthly-backup
00 09-18 * * * /home/user2/ubuntu/bin/check-db-status
~
~
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C

  • Schedule a Job for Every Minute Using Cron.


Ideally you may not have a requirement to schedule a job every minute. But understanding this example will will help you understand the other examples mentioned below in this article.

* * * * * CMD
The * means all the possible unit — i.e every minute of every hour through out the year. More than using this * directly, you will find it very useful in the following cases.

When you specify */5 in minute field means every 5 minutes.
When you specify 0-10/2 in minute field mean every 2 minutes in the first 10 minute.
Thus the above convention can be used for all the other 4 fields.

  • Schedule a Background Cron Job For Every 10 Minutes.


Use the following, if you want to check the disk space every 10 minutes.

*/10 * * * * /home/drive/check-disk-space
It executes the specified command check-disk-space every 10 minutes through out the year. But you may have a requirement of executing the command only during office hours or vice versa. The above examples shows how to do those things.

Instead of specifying values in the 5 fields, we can specify it using a single keyword as mentioned below.

There are special cases in which instead of the above 5 fields you can use @ followed by a keyword — such as reboot, midnight, yearly, hourly.

Table: Cron special keywords and its meaning
Keyword Equivalent
@yearly 0 0 1 1 *
@daily 0 0 * * *
@hourly 0 * * * *
@reboot Run at startup.


  • Schedule a Job For First Minute of Every Year using @yearly


If you want a job to be executed on the first minute of every year, then you can use the @yearly cron keyword as shown below.

This will execute the system annual maintenance using annual-maintenance shell script at 00:00 on Jan 1st for every year.

@yearly /home/drive/red-hat/bin/annual-maintenance

  • Schedule a Cron Job Beginning of Every Month using @monthly


It is as similar as the @yearly as above. But executes the command monthly once using @monthly cron keyword.

This will execute the shell script tape-backup at 00:00 on 1st of every month.

@monthly /home/drive/suse/bin/tape-backup

  • Schedule a Background Job Every Day using @daily


Using the @daily cron keyword, this will do a daily log file cleanup using cleanup-logs shell scriptat 00:00 on every day.

@daily /home/drive/arch-linux/bin/cleanup-logs "day started"

  • How to Execute a Linux Command After Every Reboot using @reboot?


Using the @reboot cron keyword, this will execute the specified command once after the machine got booted every time.

@reboot CMD

  • How to Disable/Redirect the Crontab Mail Output using MAIL keyword?


By default crontab sends the job output to the user who scheduled the job. If you want to redirect the output to a specific user, add or update the MAIL variable in the crontab as shown below.

user2@dev-db$ crontab -l
MAIL="user2"

@yearly /home/drive/annual-maintenance
*/10 * * * * /home/drive/check-disk-space

[Note: Crontab of the current logged in user with MAIL variable]

If you wanted the mail not to be sent to anywhere, i.e to stop the crontab output to be emailed, add or update the MAIL variable in the crontab as shown below.

MAIL=""


  • How to Execute a Linux Cron Jobs Every Second Using Crontab.


You cannot schedule a every-second cronjob. Because in cron the minimum unit you can specify is minute. In a typical scenario, there is no reason for most of us to run any job every second in the system.

  • Specify PATH Variable in the Crontab


All the above examples we specified absolute path of the Linux command or the shell-script that needs to be executed.

For example, instead of specifying /home/drive/tape-backup, if you want to just specify tape-backup, then add the path /home/drive to the PATH variable in the crontab as shown below.

user2@dev-db$ crontab -l

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/drive

@yearly annual-maintenance
*/10 * * * * check-disk-space

[Note: Crontab of the current logged in user with PATH variable]


  • Installing Crontab From a Cron File


Instead of directly editing the crontab file, you can also add all the entries to a cron-file first. Once you have all thoese entries in the file, you can upload or install them to the cron as shown below.

user2@dev-db$ crontab -l
no crontab for user2

$ cat cron-file.txt
@yearly /home/drive/annual-maintenance
*/10 * * * * /home/drive/check-disk-space

user2@dev-db$ crontab cron-file.txt

user2@dev-db$ crontab -l
@yearly /home/drive/annual-maintenance
*/10 * * * * /home/drive/check-disk-space

Note: This will install the cron-file.txt to your crontab, which will also remove your old cron entries. So, please be careful while uploading cron entries from a cron-file.txt.

Saturday, May 2, 2015

SRVCTL Commands for SCAN Listener

SRVCTL command for SCAN Listner:


To add SCAN Listener

    Command: srvctl add scan_listener [-l lsnr_name_prefix] [-s] [-p "[TCP:]port_list[/IPC:key][/NMP:pipe_name][/TCPS:s_port] [/SDP:port]"]
    Example: srvctl add scan_listener -l scan1

To Remove Scan Listener

    Command: srvctl remove scan_listener [-f]
    Example: srvctl remove scan_listener -f

Note: Below command is using ordinal_number parameter so An ordinal number is that identifies which SCAN VIP you want to start. The range of values you can specify for this option is 1 to 3.

To Start Scan Listener

    Command: srvctl start scan_listener [-n node_name] [-i ordinal_number]
    Example: srvctl start scan_listener -n instance1 -i 1

To Stop Scan Listener

    Command: srvctl stop scan_listener [-i ordinal_number] [-f]
    Example: srvctl stop scan_listener -i 3

To check the status of Scan Listner

    Command: srvctl status scan_listener [-i ordinal_number]
    Example: srvctl status scan_listener -i 1

To Enable Scan Listener

    Command: srvctl enable scan_listener [-i ordinal_number]
    Example: srvctl enable scan_listener -i 2

To Disable Scan Listener

    Command: srvctl disable scan_listener [-i ordinal_number]
    Example: srvctl disable scan_listener -i 1

To Configure Scan Listener

    Command: srvctl config scan_listener [-i ordinal_number]
    Example: srvctl config scan_listener -i 3

To Modify Scan Listener

    Command: srvctl modify scan_listener {-p [TCP:]port[/IPC:key][/NMP:pipe_name] [/TCPS:s_port][/SDP:port] -u }
    Example: srvctl modify scan_listener -u -p TCP:1521

To relocate Scan Listener

    Command: srvctl relocate scan_listener -i ordinal_number [-n node_name]
    Example: srvctl relocate scan_listener -i 1 -n instance1