Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 59 additions & 52 deletions instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh
Original file line number Diff line number Diff line change
@@ -1,90 +1,97 @@
#!/bin/bash
# ***************************************************************************
# Author: Fu Le Qing (Roking)
# Email: leqingfu@cn.ibm.com
# Date: 10-31-2018
# Author: Fu Le Qing (Roking)
# Email: leqingfu@cn.ibm.com
# Date: 10-31-2018
#
# Description: This script updates statistics of tables,
# associated indexes in the database, and sends an email
# to a specified email list.
# Description: This script updates statistics of tables,
# associated indexes in the database, and sends an email
# to a specified email list.
#
# ******** THIS NEEDS TO BE RUN AS INSTANCE OWNER. **************
#
# Revision history:
# 10-31-2018 Fu Le Qing (Roking)
# Original version
# 11-16-2018 Fu Le Qing (Roking)
# Skip the tables which are ongoing with reorg
# 09-08-2023 Fu Le Qing (Roking)
# Update for MAS
# Revision history:
# 10-31-2018 Fu Le Qing (Roking)
# Original version
# 11-16-2018 Fu Le Qing (Roking)
# Skip the tables which are ongoing with reorg
# 09-08-2023 Fu Le Qing (Roking)
# Update for MAS
#
# ***************************************************************************
#
# ***************************************************************************

# -- Source the Props file
if [ -f /mnt/backup/bin/.PROPS ]
then
. /mnt/backup/bin/.PROPS
DOW=`date | awk '{print $1}'`
if [ ${DOW} != ${DAYOFFULL} ]
then
exit 0
fi
DOW=`date | awk '{print $1}'`
fi

instance=`whoami`
instance_home=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${instance}" | awk -F ',' '{print $5}'| sed 's/\/sqllib//'`
# -- Standard Parameters
INSTANCE=`whoami`
INSTANCE_HOME=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${INSTANCE}" | awk -F ',' '{print $5}'| sed 's/\/sqllib//'`
DOW=`date | awk '{print $1}'`
mkdir -p ${INSTANCE_HOME}/maintenance/logs
DATESTAMP=`date "+%Y-%m-%d-%H.%M.%S"`

pidfile="$instance_home/.`basename ${0}`.pid"
if [ -e ${pidfile} ] && $kill -0 `cat ${pidfile}` 2>/dev/null
pidfile="${INSTANCE_HOME}/.`basename ${0}`.pid"
if [ -e ${pidfile} ] && $kill -0 `cat ${pidfile}` 2>/dev/null
then
exit 0
fi

echo $$ > ${pidfile}
trap "rm -f ${pidfile}; exit" SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM EXIT

if [ ! -f "$instance_home/sqllib/db2profile" ]
if [ ! -f "${INSTANCE_HOME}/sqllib/db2profile" ]
then
echo "ERROR - $instance_home/sqllib/db2profile not found"
echo "ERROR - ${INSTANCE_HOME}/sqllib/db2profile not found"
exit 1
else
. $instance_home/sqllib/db2profile
. ${INSTANCE_HOME}/sqllib/db2profile
fi

RUNSTATS_TMP_FILE="$instance_home/.runstats.sql"
REBIND_TMP_FILE="$instance_home/.rebind.sql"

mkdir -p $instance_home/maintenance/logs
DATESTAMP=`date "+%Y-%m-%d-%H.%M.%S"`
RUNSTATS_TMP_FILE="${INSTANCE_HOME}/bin/.runstats.sql"
REBIND_TMP_FILE="${INSTANCE_HOME}/bin/.rebind.sql"

for db in `db2 list db directory | grep -B 5 Indirect | grep "Database name" | cut -d= -f2`
for DB in `db2 list db directory | grep -B 5 Indirect | grep "Database name" | cut -d= -f2`
do
role=`db2 get db cfg for ${db} | grep "HADR database role" | cut -d= -f2 |sed 's/ //g'`
RUNSTATS_REBIND_LOG="${INSTANCE_HOME}/maintenance/logs/runstats_rebind_${DB}_${DATESTAMP}.log"

role=`db2 get db cfg for ${DB} | grep "HADR database role" | cut -d= -f2 |sed 's/ //g'`
if [ "$role" != "STANDBY" ]; then
if [ -f $RUNSTATS_TMP_FILE ]
then
rm $RUNSTATS_TMP_FILE
if [[ -f ${RUNSTATS_TMP_FILE} ]]; then
rm ${RUNSTATS_TMP_FILE}
fi
if [ -f $REBIND_TMP_FILE ]
then
rm $REBIND_TMP_FILE
if [[ -f ${REBIND_TMP_FILE} ]]; then
rm ${REBIND_TMP_FILE}
fi
db2 connect to ${db} | tee $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP}
if [ $? -eq 0 ]; then
db2 -x "select 'RUNSTATS ON TABLE \"' ||rtrim(tab.tabschema)||'\".\"'|| tab.tabname ||'\" WITH DISTRIBUTION ON KEY COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS;'

db2 connect to ${DB} | tee ${RUNSTATS_REBIND_LOG}
if [[ $? -eq 0 ]]; then
db2 -x "select 'RUNSTATS ON TABLE \"' ||rtrim(tab.tabschema)||'\".\"'|| tab.tabname ||'\" WITH DISTRIBUTION ON KEY COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS;'
from syscat.tables tab left join sysibmadm.SNAPTAB_REORG reg on tab.tabschema=reg.TABSCHEMA and tab.tabname=reg.TABNAME and reg.REORG_STATUS not in ('COMPLETED','STOPPED')
where tab.type='T' and reg.tabname is null" > $RUNSTATS_TMP_FILE
#db2 -x "select 'rebind package \"' ||rtrim(PKGSCHEMA)||'\".\"'|| PKGNAME ||'\";' from syscat.packages where PKGSCHEMA not in ('NULLID','NULLIDR1','NULLIDRA','SYSIBMADM','SYSIBMINTERNAL') and PKGSCHEMA not like 'NULL%' " > $REBIND_TMP_FILE
echo "Begin processing of runstats @ $DATESTAMP ..." | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP}
db2 -txvf $RUNSTATS_TMP_FILE | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP}
echo "End processing of runstats @ $DATESTAMP" | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP}
where tab.type='T' and reg.tabname is null" > ${RUNSTATS_TMP_FILE}

echo -e "Begin processing of runstats @ ${DATESTAMP} ...\n" | tee -a ${RUNSTATS_REBIND_LOG}
db2 -txvf ${RUNSTATS_TMP_FILE} | tee -a ${RUNSTATS_REBIND_LOG}
echo -e "\nEnd processing of runstats @ ${DATESTAMP}" | tee -a ${RUNSTATS_REBIND_LOG}
rm ${RUNSTATS_TMP_FILE}

#echo "Begin processing of rebind @ $DATESTAMP ..." | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP}
#db2 -txvf $REBIND_TMP_FILE | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP}
#echo "End processing of rebind @ $DATESTAMP" | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP}
rm $RUNSTATS_TMP_FILE
#rm $REBIND_TMP_FILE
if [[ ${DOW} == ${DAYOFFULL} ]]; then

db2 -x "select 'rebind package \"' ||rtrim(PKGSCHEMA)||'\".\"'|| PKGNAME ||'\";' from syscat.packages where PKGSCHEMA not like 'SYSIBM%' and PKGSCHEMA not like 'NULL%' " > ${REBIND_TMP_FILE}

echo -e "\n ----------------------------------------------- " | tee -a ${RUNSTATS_REBIND_LOG}
echo -e "Begin processing of rebind @ ${DATESTAMP} ...\n" | tee -a ${RUNSTATS_REBIND_LOG}
db2 -txvf ${REBIND_TMP_FILE} | tee -a ${RUNSTATS_REBIND_LOG}
echo -e "\nEnd processing of rebind @ ${DATESTAMP}" | tee -a ${RUNSTATS_REBIND_LOG}

rm ${REBIND_TMP_FILE}
fi
db2 terminate
fi
fi
fi
done