New DBA position at Federated Coop Ltd, Saskatoon

As summer 2015 comes in, I can say much has happened in this last year. I relocated to Saskatoon to be part of a challenging and excellent company. I spent much of this last year planning the move, and adapt to our new surroundings, in this beautiful city. I’ll be doing lots of Oracle and DB2 stuff so stay tuned. I will also get more involved with SQL Server, so you might get postings for SQL Server tips.

Implement year() month() and day() in Oracle 9i, 10g,11g and 12c as in DB2

One neat things I find about DB2 is the straightforward syntax of its commands.

I wanted to do range partitioning on an invoice table base on the invoice date field. However the invoice detail table does not have the invoice date.
Perhaps I could implement list partitioning on the invoice detail table, so was trying to extract the highest invoice number  for each year.  So my reflex was to code this:

select max(nofact) from   pos.factures group  by year(datefact);

But the Oracle syntax requires this:

group  by extract(year from datefact);

It’s not as straightforward as DB2, and a code migration would require extensive changes.

Here is the function to do this the DB2 way.  You may want to create a public synonym for that as well. The month() and day() are simple to do from here.

create or replace function year(in_timest in timestamp)
return number is out_yr number(4,0);
begin
  select extract( year from in_timest )
  into   out_yr
  from   dual;
  return(out_yr);
end;
/
grant execute on year to public
/

Enjoy !

enable multi threads (multiple subprocesses) in your shell scripts

Hi all,

It’s been a while since I last wrote. Having just completed a stimulating and challenging contract, I figured I’d have the time to perhaps share one things or two in this blog.  I am planning on getting to know better  Oracle 12c,  and write my next DB2 v10 advanced backup and recovery book.

One thing you’d probably find very useful is having a way to parallelize some processes. For example, you want to calculate the stats on a series of tables, let’s say 45 tables, as part of a maintenance process. Instead of having to go through these tables sequentially, taking 45 minutes, you’d want to regroup in three sub-processes running in parallel, each taking care of 15 tables. (you could use it for exports, imports, or anything else).  This solution could bring down the total time to 15 minutes or so.  Here’s how:

#!/bin/ksh
 . ~/.profile
DB_NAME="MYDB"
echo "STATS-START#2 `date`" > stats.log
# this does the actual work on each table
runstats_set_profile()
{
  TABNAME=$1
  printf "runstats on table DB2INST1.%s begin  at %s\n" ${TABNAME} "`date "+%T"`" >> stats_cutoff10day_${CUTOFF}.log
  db2    "runstats on table DB2INST1.${TABNAME} on key columns and  indexes all set profile"
  printf "runstats on table DB2INST1.%s ending at %s\n" ${TABNAME} "`date "+%T"`" >> stats_cutoff10day_${CUTOFF}.log
}

# SET #2 will execute in background, because of the trailing &
( echo "STATS-START#2 `date`" >> stats.log
  db2 connect to ${DB_NAME} >> stats.log
  runstats_set_profile "TAB16" ...
  runstats_set_profile "TAB30"  
  echo "STATS-STOP #2  `date`" >> stats.log
) &

# SET #3 will also execute in background
( echo "STATS-START#3 `date`" >> stats.log   
  db2 connect to ${DB_NAME} >> stats.log   
  runstats_set_profile "TAB31" ...   
  runstats_set_profile "TAB45"   
  echo "STATS-STOP #3  `date`" >> stats.log 
) &

# SET #1 is part of the main process
 echo "STATS-START#1 `date`" >> stats.log
 db2 connect to ${DB_NAME} >> stats.log
 runstats_set_profile "TAB01" ...
 runstats_set_profile "TAB15"
 echo "STATS-STOP #1  `date`" >> stats.log

Note that in this implementation, we cannot tell if SET #2 or #3 will finish before SET #1 is complete. The script may return right away in some cases, but the subprocesses are still running. In this example,  I send a message to the log indicating a stats set is complete. Just grep the log file to make sure the stats sets are all completed.

grep STATS-ST stats.log

STATS-START#2 Sun Sep  2 14:32:40 EST 2012
STATS-START#3 Sun Sep  2 14:32:40 EST 2012
STATS-START#1 Sun Sep  2 14:32:41 EST 2012
STATS-STOP #2 Sun Sep  2 14:33:30 EST 2012 
STATS-STOP #1 Sun Sep  2 14:45:21 EST 2012 
STATS-STOP #3 Sun Sep  2 14:46:18 EST 2012

I cut down minutes if not hours of processing time with this. I hope you find it useful too !

Book review: IBM COGNOS Business Intelligence

A practical book for new users and experienced users alike.
This book covers Cognos BI in a well-written and straightforward walkthrough.

A new user will feel comfortable learning the basics.
Experienced administrators, developers, and business users
will find lots of useful tips and practical explanations
in order to extend their knowledge and use of Cognos BI tools.

With just the right dose of screen shots, this book will walk you through
workspace, reports, query,analysis, and much more, in a step by step manner.
Some finer points in the user interface are well explained, and help you find your way.

I highly recommend this book to fellow administators and developers.
IBM Cognos Business Intelligence

Date arithmetic for shell scripts

One thing I found out when writing shell scripts, is that we often need to calculate dates, be it to name yesterday’s log, or compute a cutoff date to cleanup files or data.

The date command does a good job, but for today only. When you want to go one or two days in the past, this is getting more complicated. As Boromir would say: “One does not simply use date – 1 and format the result.”

In some situations a database can provide easily this information. In other cases you don’t want to use a database connection just for this purpose. This is where my trick comes in.

In the example below we want to compute a 10 day cutoff date, to do a purge of obsolete records. We can subtract the number of seconds we have in 10 days, so ( 10 days X 24 hrs X 60 min X 60 = 864000 ). The awk command come in handy, since it can do just that :


[robert@mordor DB2]$ date
Tue Jun 19 20:14:22 EDT 2012
[robert@mordor DB2]$ date | awk '{cutoff=systime()-864000; print strftime("%C%y%m%d",cutoff)}'
20120609

Now this is great for Linux systems, but for AIX systems, awk does not have strftime, so we have to come up with somthing else. A perl script can do the job just as easily.

[robert@mordor DB2]$ perl -e 'my($s,$m,$h,$dy,$mo,$yr) = localtime(time-864000); printf "%4.4d%2.2d%2.2d\n",$yr+1900,$mo+1,$dy;'
20120609

So, have fun scripting …

DB2 9.7 / 10.1 Reroute archived logs

Hi again,

In this topic we will see how we can reroute archived logs to a new location wihtout touching DB2 configuration, so this can be done live, without downtime. Here is the scenario:

  • All archived logs  for production, uat, and test sit on the same file system. Not an ideal setup, mind you, but this is the actual configuration for one of my clients. We will correct this, but now onto our scenario.
  • Someone has some tests to do in QA for purging, and has lots of rows to delete. Now I think you’ve got already a good idea of what’s coming next.
  • Now with the delete, having 50 millions or so records to delete, you can guess what happened to the file system. So I got this call at 2:00 am to free some space and do something before the next run.

This case was temporary for the duration of the purge activity on QA. This affected production, since we’ve explained that all logs go to the same file system. I did not want to change the configuration of the QA database, since it would have required down time, a ticket nightmare I did not want to put up with.

So here’s the deal, I got this file system I could use, separated from the production logs file system. I had enough space to put logs for QA, without having to worry for production.

# Copied logs over to the new place
cd /db2/backup/logstarget/qa/
cp -p /db2/backup/logssource/qa/* .
 without prompting"
# Now go back to the original directory
cd /db2/backup/logssource 

# We rename the directory
mv qa qa.orig

# and create a symbolic link to the target directory
ln -s /db2/backup/logstarget/qa/ qa

The symbolic link makes it work. This was only a temporary measure for this case. Make sure there is little activity on the database. We wouldn’t want to change the link while DB2 is archiving a new log.

Have fun !

DB2 9.1 and 9.7 Online redirected restore

In my book, “DB2 9.7 Advanced Administration Cookbook“, I discussed redirected restores. However, this was from an offline backup. If you only use online backups, you may still achieve the same goal using the commands I used below.

It took a while for me to grasp the online redirected restore, and figure out the error messages. So let me share this with you, and avoid wasting your time on an otherwise simple task. Don’t forget to use the same window throughout this procedure.

Having an online redirected restores means you have to deal with logs and rolling forward the database. We’ll have to assume here that the backup has been made with the “include logs” option. These logs will be extracted into a logtarget directory, which we will create first, and specify in the restore command below:

mkdir -p /db2/backup/20120425_prod_support/logtarget
db2 "restore database bcgapps from '/db2/backup/20120425_prod_support'
 taken at 20120425134406
 into bcgapps1
 logtarget '/db2/backup/20120425_prod_support/logtarget'
 newlogpath '/db2/db2inst1/bcgapps1/archlogs'
 with 2 buffers buffer 1024
 redirect parallelism 1
 without prompting"

DB2 will print a message indicating restore is completed.

SQL1277W  A redirected restore operation is being performed.  Table space
configuration can now be viewed and table spaces that do not use automatic
storage can have their containers reconfigured.
DB20000I  The RESTORE DATABASE command completed successfully.

You have to set the path for the tablespaces, which we will do here:

db2 "set tablespace containers for  0
     using ( path '/db2/db2inst1/bcgapps1/system' )"
db2 "set tablespace containers for  1
     using ( path '/db2/db2inst1/bcgapps1/tables' )"
db2 "set tablespace containers for  2
     using ( path '/db2/db2inst1/bcgapps1/user' )"
db2 "set tablespace containers for  3
     using ( path '/db2/db2inst1/bcgapps1/log' )"
db2 "set tablespace containers for  4
     using ( path '/db2/db2inst1/bcgapps1/dup' )"
db2 "set tablespace containers for  5
     using ( path '/db2/db2inst1/bcgapps1/profile' )"
db2 "set tablespace containers for  6
     using ( path '/db2/db2inst1/bcgapps1/temp' )"
db2 "set tablespace containers for  7
     using ( path '/db2/db2inst1/bcgapps1/user_temp' )"
db2 "set tablespace containers for  8
     using ( path '/db2/db2inst1/bcgapps1/systoolspace' )"
db2 "set tablespace containers for  9
     using ( path '/db2/db2inst1/bcgapps1/systoolstmpspace' )"

Once all tablespaces have been set, here is the actual restore:

db2 "restore database bcgapps continue"

If you open another window and look at the logtarget directory, you will see the logs extracted from the backup,

    db2inst1@nodedb21:/db2/dbbackup/BACKUP/20120425_prod_support/logtarget> ls -al
    total 408
    drwxrwxr-x    2 db2inst1 db2iadm1        256 Apr 26 08:57 .
    drwxrwxr-x    4 db2inst1 db2iadm1       4096 Apr 26 08:50 ..
    -rw-------    1 db2inst1 db2iadm1     106496 Apr 26 08:57 S0001712.LOG
    -rw-------    1 db2inst1 db2iadm1      98304 Apr 26 08:57 S0001713.LOG

These logs will be used in the next step. So going back to the restore window:

db2 "rollforward database bcgapps1 to end of logs and complete
overflow log path ( '/db2/backup/20120425_prod_support/logtarget') "

You should see this result:

                                 Rollforward Status

 Input database alias                   = bcgapps1
 Number of nodes have returned status   = 1

 Node number                            = 0
 Rollforward status                     = not pending
 Next log file to be read               =
 Log files processed                    = S0001712.LOG - S0001713.LOG
 Last committed transaction             = 2012-04-25-18.45.15.000000 UTC

DB20000I  The ROLLFORWARD command completed successfully.

If you did not specify the logtarget option, you will see see messages like this one:

SQL4970N  Roll-forward recovery on database "BCGAPPS1" cannot reach the
specified stop point (end-of-log or point-in-time) because of missing or
corrupted log file(s) on database partition(s) "0". Roll-forward recovery
processing has halted on log file "S0001712.LOG".

Have fun !

My new book is now published !

IBM DB2 9.7 Advanced Administration Cookbook covers all the latest features with instance creation, setup, and administration of multi-partitioned database. Coauthored with Adrian Neagu, this practical cookbook provides step-by-step instructions to build and configure powerful databases, with scalability, safety and reliability features, using industry standard best practices. This book will walk you through all the important aspects of administration. You will learn to set up production capable environments with multi-partitioned databases and make the best use of hardware resources for maximum performance. With this guide you can master the different ways to implement strong databases with a High Availability architecture.

Buy it now at:
DB2 9.7 Advanced Administration Cookbook

Best wishes for 2012!

Thanks for all of you who took some time to visit this blog. I wish you all the best for 2012 for you and your loved ones;  health, freedom, peace, and prosperity.

Here is a short review of this blog in 2011.  More to come in 2012 !

Here’s an excerpt:

A New York City subway train holds 1,200 people. This blog was viewed about 4,400 times in 2011. If it were a NYC subway train, it would take about 4 trips to carry that many people.

Click here to see the complete report.

Writing a book on DB2 recipes

Hi all, it’s been a time since I haven’t written. I am currently in the process of writing a book as a coauthor It’s an advanced DB2 recipe book for DBA’s. Tell me what you’d like to see in this book.

Create your own website, free on WordPress.com.Create your own website.
Start free