Why can't I shrink a transaction log file, even after backup? - sql-server

I have a database that has a 28gig transaction log file. Recovery mode is simple. I just took a full backup of the database, and then ran both:
backup log dbmcms with truncate_only
DBCC SHRINKFILE ('Wxlog0', TRUNCATEONLY)
The name of the db is db_mcms and the name of the transaction log file is Wxlog0.
Neither has helped. I'm not sure what to do next.

Thank you to everyone for answering.
We finally found the issue. In sys.databases, log_reuse_wait_desc was equal to 'replication'. Apparently this means something to the effect of SQL Server waiting for a replication task to finish before it can reuse the log space.
Replication has never been used on this DB or this server was toyed with once upon a time on this db. We cleared the incorrect state by running sp_removedbreplication. After we ran this, backup log and dbcc shrinkfile worked just fine.
Definitely one for the bag-of-tricks.
Sources:
http://social.technet.microsoft.com/Forums/pt-BR/sqlreplication/thread/34ab68ad-706d-43c4-8def-38c09e3bfc3b
http://www.eggheadcafe.com/conversation.aspx?messageid=34020486&threadid=33890705

You may run into this problem if your database is set to autogrow the log & you end up with lots of virtual log files.
Run DBCC LOGINFO('databasename') & look at the last entry, if this is a 2 then your log file wont shrink. Unlike data files virtual log files cannot be moved around inside the log file.
You will need to run BACKUP LOG and DBCC SHRINKFILE several times to get the log file to shrink.
For extra bonus points run DBBC LOGINFO in between log & shirks

'sp_removedbreplication' didn't solve the issue for me as SQL just returned saying that the Database wasn't part of a replication...
I found my answer here:
http://www.sql-server-performance.com/forum/threads/log-file-fails-to-truncate.25410/
http://blogs.msdn.com/b/sqlserverfaq/archive/2009/06/01/size-of-the-transaction-log-increasing-and-cannot-be-truncated-or-shrinked-due-to-snapshot-replication.aspx
Basically I had to create a replication, reset all of the replication pointers to Zero; then delete the replication I had just made.
i.e.
Execute SP_ReplicationDbOption {DBName},Publish,true,1
GO
Execute sp_repldone #xactid = NULL, #xact_segno = NULL, #numtrans = 0, #time = 0, #reset = 1
GO
DBCC ShrinkFile({LogFileName},0)
GO
Execute SP_ReplicationDbOption {DBName},Publish,false,1
GO

Don't you need this
DBCC SHRINKFILE ('Wxlog0', 0)
Just be sure that you are aware of the dangers: see here: Do not truncate your ldf files!
And here Backup Log with Truncate_Only: Like a Bear Trap

This answer has been lifted from here and is posted here in case the other thread gets deleted:
The fact that you have non-distributed LSN in the log is the problem.
I have seen this once before not sure why we dont unmark the
transaction as replicated. We will investigate this internally. You
can execute the following command to unmark the transaction as
replicated
EXEC sp_repldone #xactid = NULL, #xact_segno = NULL, #numtrans = 0, #time = 0, #reset = 1
At this point you should be able to truncate the log.

I've had the same issue in the past. Normally a shrink and a trn backup need to occur multiple times. In extreme cases I set the DB to "Simple" recovery and then run a shrink operation on the log file. That always works for me. However recently I had a situation where that would not work. The issue was caused by a long running query that did not complete, so any attempts to shrink were useless until I could kill that process then run my shrink operations. We are talking a log file that grew to 60 GB and is now shrunk to 500 MB.
Remember, as soon as you change from FULL to Simple recovery mode and do the shrink, dont forget to set it back to FULL. Then immediately afterward you must do a FULL DB backup.

If you set the recovery mode on the database in 2005 (don't know for pre-2005) it will drop the log file all together and then you can put it back in full recovery mode to restart/recreate the logfile. We ran into this with SQL 2005 express in that we couldn't get near the 4GB limit with data until we changed the recovery mode.

I know this is a few years old, but wanted to add some info.
I found on very large logs, specifically when the DB was not set to backup transaction logs (logs were very big), the first backup of the logs would not set log_reuse_wait_desc to nothing but leave the status as still backing up. This would block the shrink. Running the backup a second time properly reset the log_reuse_wait_desc to NOTHING, allowing the shrink to process.

Have you tried from within SQL Server management studio with the GUI. Right click on the database, tasks, shrink, files. Select filetype=Log.
I worked for me a week ago.

Try creating another full backup after you backup the log w/ truncate_only (IIRC you should do this anyway to maintain the log chain). In simple recovery mode, your log shouldn't grow much anyway since it's effectively truncated after every transaction. Then try specifying the size you want the logfile to be, e.g.
-- shrink log file to c. 1 GB
DBCC SHRINKFILE (Wxlog0, 1000);
The TRUNCATEONLY option doesn't rearrange the pages inside the log file, so you might have an active page at the "end" of your file, which could prevent it from being shrunk.
You can also use DBCC SQLPERF(LOGSPACE) to make sure that there really is space in the log file to be freed.

Try to use target size you need insted of TRUNCATEONLY in DBCC:
DBCC SHRINKFILE ('Wxlog0', 1)
And check this to articles:
http://msdn.microsoft.com/en-us/library/ms189493(SQL.90).aspx
http://support.microsoft.com/kb/907511
Edit:
You can try to move allocated pages to the beginning of the log file first with
DBCC SHRINKFILE ('Wxlog0', NOTRUNCATE)
and after that
DBCC SHRINKFILE ('Wxlog0', 1)

Put the DB back into Full mode, run the transaction log backup (not just a full backup) and then the shrink.
After it's shrunk, you can put the DB back into simple mode and it txn log will stay the same size.

I had the same problem. I ran an index defrag process but the transaction log became full and the defrag process errored out. The transaction log remained large.
I backed up the transaction log then proceeded to shrink the transaction log .ldf file. However the transaction log did not shrink at all.
I then issued a "CHECKPOINT" followed by "DBCC DROPCLEANBUFFER" and was able to shrink the transaction log .ldf file thereafter

You cannot shrink a transaction log smaller than its initially created size.

I tried all the solutions listed and none of them worked. I ended up having to do a sp_detach_db, then deleting the ldf file and re-attaching the database forcing it to create a new ldf file. That worked.

Related

DBCC SHRINKFILE shows no errors but doesn't shrink the log file

I have a production DB restored in the dev environment on SQL Server 2012.
I wanted to shrink the log file to save HDD space. I looked through SO and other resources but none of the suggestions work for me.
I took the full DB backup and the Log backup. I set the recovery mode to simple. There's no mirroring set up. Yet the command DBCC SHRINKFILE does not shrink the file, but also does not show any errors. The same command works perfectly well on other DBs in the same server. I also tried right click -> tasks -> shrink -> files -> shrink file to, and got the same result, i.e. no errors but the file size remains unchanged.
What else can I try?
This can happen, even if you've followed the proper instructions of setting the database into SIMPLE recovery mode. Usually this happens because there is a virtual log still being used.
If you run:
DBCC LOGINFO;
You'll see if any virtual logs are in use (Status is 2). When shrinking files, empty virtual logs are physically removed starting at the end of the file until it hits the first used status. This is why shrinking a transaction log file sometimes shrinks it part way, but does remove all free virtual logs which you may expect.
This can happen if there is any active transactions still running, which you can debug by querying sys.dm_tran_active_transactions.

SQL Server Log File Is Huge

Currently my db logs for my production SQL Server 2008 R2 server is growing out of control:
DATA file: D:\Data...\MyDB.mdf = 278859 MB on disk
LOG file: L:\Logs...\MyDB_1.ldf = 394542 MB on disk
The server mentioned above has daily backups scheduled #1am & translog backups every 15 min.
The database is replicated in full recovery model to a subscriber. Replciation is pushed from the node above (publisher). That same db log file on the subscriber is ~< 100 GB on disk.
What I did to try and fix:
Run a full backup of the db (takes 1h:47m)
Run the translog backup job which runs every 15 min. (takes 1m:20s)
Run another full backup of the db
Nothing above has worked so I then attempt to shrink the log files which doesn't work either using DBCC SHRINKFILE. The size doesn't ever change.
Can anyone please tell me what is wrong or what I need to do as a SQL Server DBA to resolve the above issue?
Possible things that may stop you from shrinking the translog file:
Long running transaction is occurring on your database
Your replication distribution agent runs quite frequent
Looking at the size of your translog file size, most likely it was caused by the 2nd possibility.
Your replication distribution agent runs quite frequent
SQL Server log reader agent marks the translog file as being used and prevent them from being shrunk, which is what SQL Server does after the translog file is backed up. If this process happens frequent and long enough, this could prevent your translog file from being shrunk on translog scheduled back up.
Look at this MSDN transactional explaination and how to modify log reader agent.
And a thread in MSDN forum that describe similar problem, there is DBCC query here that helps you identify running transaction that may be blocking the translog file (DBCC OPENTRAN).
Long running transaction is occurring on your database
You can check wheter any long running transaction is happening by using DBCC OPENTRAN and what process is running then decide what to do with it. As soon as the long running transaction is finished you should be able to shrink the log file.
After running sp_who2, I noticed a long running transaction on the log that was growing uncontrollably. I used kill on that SPID and not I'm proceeding to shrink the log file.
You should make blank database with same table and migrate your old database data to blank database from migration script.
for eg:
INSERT INTO customers(cust_id, Name, Address)
SELECT cust_id, Name, Address
FROM olddb.customers
--this script should run in new blank database
You can manually shrink you log file
1.right click your database > task > shrink > file > file type=log
than ok

How to Shrink Transaction log in sql server database in replication

Hi I'm having a production database and its replicated report database. How to shrink the transaction log files in the production database as the log file size is increasing. I had tried DBCC SHRINKFILE and SHRINKDATABASE commands but it does not work for me. I can't detach and shrink and attach back as the db in replication. Please help me in this issue.
First check what is causing your database to not shrink by running:
SELECT name, log_reuse_wait_desc FROM sys.DATABASES
If you are blocked by a transaction, find which one with:
DBCC OPENTRAN
Kill the transaction and shrink your db.
If the cause of the blocking is 'REPLICATION' and you are sure that your replicas are in sync, you might need to reset the status of replicated transactions. To see the status of what the database still think needs to be replicated use:
DBCC loginfo
You can reset this by first turning the Reader agent off (I usually just turn the whole SQL Server Agent off), and then run that query on the database for which you want to fix the replication issue:
EXEC sp_repldone #xactid = NULL, #xact_segno = NULL, #numtrans = 0, #time= 0, #reset = 1
Close the connection where you executed that query and restart SQL Server Agent (or just the Reader Agent). You should be all set to shrink your db now.
The database won't let you remove transaction data that isn't backed up. First you have to back up the transaction log, then you can shrink it.
Do you have a regular backup schedule in place?
If not, I suggest you read this excellent article: 8 Steps to better Transaction Log throughput
Shrink the logfile with dbcc shrinkfile
Then truncate the log file using
Backup databaseName with truncate_only
Then shrink the logfile again
I used Red-Gate's SQL Backup tool to take care of the backing up. Then I just use the management console to issue a shrink command on the log file (telling it to rearrange the pages before releasing unused space).
Works like a charm.

Sql 2008 Restoring a backed up DB fails - what else can i do?

i'm trying to restore our live DB onto our Dev box. To do this, I when onto production, TASKS -> backup Db. it created a 4Gig file. I zipped this down to 2.2Gig. download that to my dev server.
On my dev server, i create a new DB (called 'xxxxx') and then Tasks -> restore DB from file. I give it the .bak file name, overwrite all and go.
When it gets to 40% it fails. here's the screenie:
alt text http://img19.imageshack.us/img19/1/restorefailurejl5.png
Now, i can manually zip up the .mdf and .log files, zip them, download them and then attach them to my dev server sql instance. I did that last night actually, to get this working .. so that worked.
But i'm not sure why the backup/restore method didn't work? I've downloaded the .bak file a few times (in case the download was corrupt). i tried to rezip and re-backup the live server a few times also .. but after about the 5 download of a 2Gig file, I'm starting to get grumpy :)
I've tried doing a DBCC CHECKDB('live db name', RESTORE_REBUILD) and that worked fine and THEN backup, download, restore, fail.
My live DB is sql2008 x64bit and my dev box is x86 (32 bit), so i'm not sure if that's an issue. Both servers are versions 10.0 RTM.
I don't want to have to stop the db to be able to copy the .mdf/.log files (cause u can't access them while the db is running, i believe) .. which is why i prefer the backup/restore method.
Any suggestions?
UPDATE 1
Before i posted this question, I did do a DBCC CHECKDB ('xxxxx', RESTORE_REBUILD). I noted this in my initial post, which i've now just highlighted for future reference.
When i get a chance to stop the DB, i'll post the end results here (and save the text output incase someone asks for some other info).
UPDATE 2
I tried to restore the backup to a dummy live db i created. Failed (same error message). I'm running a DBCC CHECKDB('LiveDB') right now. Just before I did this, i stopped the SQL Server service & manually copy-backedup the .mdb and .log files.
UPDATE 3
This is the result from the DBCC CHECKDB ('LiveDb')
DBCC results
for 'Addresses' There are 1689363 rows
in 101624 pages for object 'Addresses'
DBCC results for 'Thumbnails' There
are 1197 rows in 30 pages for object
'Thumbnails' ..
CHECKDB found 0 allocation errors and
0 consistency errors in database
'LiveDb' DBCC execution completed. If
DBCC printed error messages, contact
your administrator.
hmm :( Any further ideas? Should i run DBCC again with RESTORE_REBUILD or some other magic argument?
Final Update
Ok - this is sooo weird. After running CHECK DBCC and then doing another full backup (using the same sql script that we have been using for eons) the restore now works fine! I just don't get it :(
Looks like i can close the job now. I just don't get it :(
This is IMPORTANT, you may potentially have an issue with the live database.
Have you performed DBCC CHECKDB on your production server recently? If not, do so at the next possible maintenance window.
You should ideally be performing CHECKDB before you take your full backups (although not always practical) in order to validate that the backups you are generating, are of a database that has been confirmed to be without issue.
Please feel free to pose further questions. You can contact me directly if you require additional assistance.
Cheers, John
We do the same thing every morning.
RESTORE DATABASE Accounts FROM DISK = '\\server\F$\SQL_BACKUP\DB.bak' WITH REPLACE
This works fine for us. I'm not sure if the x64 > x32 will have an impact, I wasn't aware that data was stored differently between those kinds of platforms. We basically dump our live database every night, a process on our SQL server then picks them up, and another process sends them to be backed up to disk.
The command above will create you a new database and import the data, it's worth a shot.
Sounds like your production database has some corruption. Run DBCC CHECKDB on your production database and see what is returned.
Does a restore on the live machine (to a different db) fail? If so, try running DBCC CHECKDB.

How Can I Manage SQL Server Log Size

I'm trying to manage the size of a SQL Server 2008 log file. I have a reporting database that is loaded once a day. The Simple recovery model is the best fit as there are no transactions other than the morning load, and I can re-create those records. My goals are to have the transaction log at a fixed size, large enough that it doesn't have to allocate new space during the load.
My problem is that the log keeps growing. I know that the log is supposed to be marked for re-use, but why does the log keep growing? The log_reuse_wait_desc shows "Nothing".
I can shrink the log file to 1 MB, so I know there are no transactions in it. I can set up a job to do this, but I would prefer to leave the log at 500MB, which is enough to handle the daily loads. If I do this, the log grows.
How can I keep the log at a consistent size?
Please note: Shrinking the log file does not address this issue. I know how to do that. I'm trying to find a way for space in the transaction log to be re-used.
The log file is used for transactional purposes, even if the database is in SIMPLE RECOVERY MODE. If the LOG size is truly growing beyond 500 MB, then someone is running a query or stored procedure that is requiring that space.
For example, perhaps you are creating indexes against your reporting tables. That will be done inside a transaction so that the changes can be rolled back in case of an error. However, the used space will be released afterward for other transactions once complete.
So, if the log size is starting at 1MB and increasing to say, 700MB, then something is being done that requires that space. If you lock the size to 500MB, you will eventually receive a "log file out of space" error.
But if you really want to fix it at 500MB, you can do the following:
(I'm using SQL Server 2005)
Launch Microsoft SQL Server Management Studio
Locate your database and right-click on it. Select Properties.
Click on Files section
Locate the LOG FILE line.
Change the Initial Size to: 500
Locate the Autogrowth section and click on the ellipse (...)
Uncheck "Enable Autogrowth". Click OK.
Click OK to make the change.
Note: You can also set a maximum log file size in the "autogrowth section".
Alternatively, you can use the following script to make the change. Replace DATABASENAME with the appropriate value. Also change the Log File Name if required.
USE [master]
GO
ALTER DATABASE [DatabaseName] MODIFY FILE ( NAME = N'DATABASENAME_Log', SIZE = 512000KB , FILEGROWTH = 0)
GO
create a maintenance job that backups the DB and shrinks the log
The DBCC Shrinkfile command allows you to specify a target size:
DBCC SHRINKFILE (DataFile1, 7)
Shrinks the file DataFile1 to 7MB
Document at: MSDN
Off the top of my head you can use DBCC SHRINKFILE to do this.

Resources