September 4th, 2007

Sybase UPDATEs taking forever/timing out? Time to dump tran!

I’m a programmer, not a DBA, but I do have to be able to perform basic database configuration and maintenance tasks. One thing that’s come up, a couple times, over the past few years, is a transaction log getting full.

There are a couple of reasons this can happen:

  • poorly written queries that run for a very long time
  • applications neglecting to end transactions, when they encounter error conditions

Here are a few useful commands I’ve found that deal with transaction logs (Note: I’m using Sybase ASE 12.5.1):

check for long-running transactions (found here):
use master;

SELECT * FROM syslogshold
WHERE dbid = db_id(’MyDbName’);

transaction log space free:
SELECT CONVERT(CHAR,
(lct_admin('logsegment_freepages', 4) -
lct_admin('reserved_for_rollbacks', 4)) * 2)

transaction log space total:
SELECT SUM(size)
FROM master..sysusages
WHERE dbid = db_id('MyDbName')
AND segmap & 4 = 4 -- logsegment

dump the transaction log (clears it out, so things can get moving again!):
***WARNING*** Use this command at your OWN RISK! Please do some additional reading before executing the following statement, so that you fully understand the implications. The transaction log is there for a reason, and, from what I understand, you should not tamper with it unless you know what you’re doing. On the occasions that I’ve used this, it was a last resort. It solved my problems, with no ill effects, but it might not solve yours!

dump tran myDbName with truncate_only;
commit;

For additional information/reference, Sybase’s online books sometimes come in handy. When trying to find out how to dump transaction logs, I referred to their dump transaction page.

Another site I’ve found useful*, on numerous occasions, is Sybase 101. I found a related tip, there, for tips, there, for Dealing With Server Failure, which addresses what to do when your log fills up completely, such that you can’t even restart the server.

*Beware: I just noticed that Sybase 101 has become less useful than it used to be, because useful pages are automatically redirecting to one main start page. If they were just trying to prevent deep linking from the outside world, I could understand this behavior a little more; but it’s even happening when I click other links from their own site! Navigation works fine with javascript disabled, though.

February 3rd, 2006

Finding and un-removing files that I did a ‘cvs remove’ on yesterday

Last July, I posted a bash shell script called ‘cvsrecent.bsh’, the purpose of which was “to get a list of CVS revisions and log messages for all checkins within the past (some number) of days.”

I’ve continued to use this script, to this day, but I found a shortcoming this morning that I hadn’t noticed before. It doesn’t find files that have been removed from your working directory or the repository. I removed some files from my working directory and did ‘cvs remove’ on them yesterday, and today I decided I need to undo that.

So, I wrote a set of commands to find and check out the last version of all files that were deleted some number of days ago. Here are the components I used:

First, a bash function to get previous revision of a file:
function cvsprev()
{
cvs -q log $@ | grep “^revision ” | head -2 | tail -1 | cut -f2 -d” ”
}

Next, a bash function to get the date for some number of days ago, and format it the way cvs likes it:
function getdaysagodate()
{
date –date=”$1 days ago” +”%Y-%m-%d”
}

Finally, here’s the set of commands I used to:

  1. find all the files that CVS put in the “Attic” some number of days ago (in my case, I used 1 (one) since I removed the files yesterday).
  2. for each file:
    1. get the full working-directory path
    2. find the last revision number, before it was removed
    3. do a ‘cvs update -r’ to check out the last version of the file

for f in `cvs -Q log -SNd “>=$(getdaysagodate 1)” | grep -A1 Attic | sed -e “s/^.*Attic.*$//” -e “s/^\-\-$//” -e “s/Working file: //”`;
do cvs update -r `cvsprev $f` $f;
done

Another command could be added to check those files back into CVS automatically, but I wasn’t 100% certain that I wanted to check all of them back in. I mainly just wanted to find them, and look at them. Maybe sometime I’ll update the cvsrecent.bsh script, so it can handle files that have been removed. At least I’m half-way there after having written these commands today.