Wednesday, April 11, 2018

Capturing and decrypting Deadlock information from SQL Server Part 1


Capturing and decoding Deadlock information from SQL Server Part 1


We will have two articles in this series.
1. Different ways to capture deadlock
2. Decoding deadlock data from captured deadlock data

I know this is very common issue which DBA might not be able to completely resolve but DBA can provide detailed information regarding Deadlock.

Let us see what are the different ways we can capture deadlock data from SQL Server.

From SQL Server 2012 onwards we can capture deadlock data directly from System Health Extended event session, for this we don’t need enable any trace flags.
Earlier to SQL Server 2012, we need to enable flags 1204 and 1222 to get detailed deadlock information.
You can check my blog to find how to enable trace flags.

Below is the script to capture deadlock, This code is robust enough to check for SQL Server version, to get data from System health Event session or from SQL Server error log. If it does not find any information it will give suggestions as well.

SET NOCOUNT ON
DECLARE @stDate AS DATETIME = '2016-06-13 04:49:26'                                --Please key in the UTC time at which alert created
DECLARE @DecelockText AS VARCHAR (100) = 'Process ID 113'     --Replace 113 with the process ID part of deadlock

DECLARE @endDate AS DATETIME
SELECT @stDate = DATEADD(MILLISECOND,DATEDIFF(MILLISECOND,getutcdate(),GETDATE()),@stDate)
SET @endDate = DATEADD(MINUTE, 1, @stDate)
SET @stDate  = DATEADD(MINUTE, -2, @stDate)


IF (CAST(LEFT(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max)),CHARINDEX('.',CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max))) - 1) + '.' + REPLACE(RIGHT(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max)), LEN(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max))) - CHARINDEX('.',CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max)))),'.','') AS numeric(18,10)) ) >= 11
BEGIN
                DECLARE @PATH VARCHAR(500)
                SELECT  @PATH= [path] FROM sys.dm_os_server_diagnostics_log_configurations WITH (NOLOCK) OPTION (RECOMPILE);

                SET @PATH = @PATH + 'system_health*.xel'

                CREATE TABLE #Deadloc_Trace (DLGraph XML, Execution_time datetime)

                INSERT INTO #Deadloc_Trace

                SELECT CONVERT(xml, event_data).query('/event/data/value/child::*'),
                                   CONVERT(xml, event_data).value('(event[@name="xml_deadlock_report"]/@timestamp)[1]','datetime') as Execution_Time
                FROM sys.fn_xe_file_target_read_file(@PATH, null, null, null)

                WHERE object_name like 'xml_deadlock_report'


                SELECT * FROM  #Deadloc_Trace WHERE Execution_Time BETWEEN @stDate and @endDate

                DROP TABLE #Deadloc_Trace
END
ELSE
                BEGIN
                CREATE TABLE #TraceStatus(
                TraceFlag varchar(100)
                , [Status] int
                , [Global] int
                ,  [Session] int)

                insert #TraceStatus(TraceFlag, [Status], [Global], [Session])
                EXEC('DBCC TRACESTATUS(1204,1222) WITH NO_INFOMSGS;')
               
                IF EXISTS (select * from #TraceStatus where Global > 0)
                BEGIN
                                CREATE TABLE #ErrLog(LogDate datetime, ProcessInfo sysname, LogText nvarchar(1000));

                                INSERT INTO #ErrLog
                                EXEC xp_readerrorlog 0, 1, @DecelockText;

                                SELECT TOP 1 @stDate = LogDate
                                FROM #ErrLog
                                ORDER BY LogDate DESC;

                                SET @endDate = DATEADD(minute,2,@stDate)

                                --select @stDate, @endDate

                                EXEC master.dbo.xp_readerrorlog 0, 1, null, null,@stDate, @endDate

                                DROP TABLE #ErrLog;
                END
                ELSE
                BEGIN
                                PRINT 'Currently deadlock traces are not enabled and hence we cannot gather more information.
                We need to enable Deadlock traces 1204,1222 to capture deadlock information in future.
                '
                END

                DROP TABLE #TraceStatus;
END


Hope this helps someone thanks for checking in.


Monday, April 9, 2018

Start SQL Server Management Studio (SSMS) Or connect to SQL Server using NT Authority\ System


Start SQL Server Management Studio (SSMS) Or connect to SQL Server using NT Authority\ System


Recently we have faced a problem where sysadmin access for all of our accounts were removed accidently and SA was in disabled state.

Later we have noticed that one of the possible solution to this case is to connect SQL Server using NT Authority\System as this is default user till SQL Server 2008 R2 who will have sysadmin access on DB instance. From SQL Server 2012 onwards NT Authority\System user will not have sysadmin access and this article might not help. Below are quick steps to connect SSMS using NT Authority\System:

1. Download PS tools from below link:
2. Extract them to a folder.
3. Right click on CMD and run as administrator.
4. Navigate to folder where you have extracted zip file downloaded part of above link.
5. Run below command to start SSMS using NT Authority\System user

psexec -i -s SSMS

Done you should be able to access DB instance and provide access to the users you need. Hope this helps someone as it did to me.

Sunday, April 8, 2018

SQL Server patching and Fixing Missing MSI and MSP files issue


SQL Server patching and Fixing Missing MSI and MSP files issue


I know by looking at this article name many of you already might think that is well known issue and addressed on below MS Link.
https://support.microsoft.com/en-us/help/969052

However in this article I will describe an interesting new way I came across in recent past. Which I found be better and easy way  as this uses a GUI tool.

Recently one of my friend found this interesting tool and shared details with me. I thought why to stop with me and here is this article.

In this article we will see how easily FixSQLMSI.exe can fix Missing MSI and MSP files issues. This tool is engineer name “Simon Su” and available on below GitHub link.



Please find how to use steps below:
1. Download the package from above link.
2. Execute the exe.
3. Select the SQL Server Install path and click scan.
4. It will check for both Missing MSI, MSP files and any miss matched files (missed matched files would be shown if you have cached any wrong files in installer folder)
5. Once scanning is done check for Status and Action columns to find and fix any issues. You will find many more details like Source path, command to fix and so on.
6. If you have any missing files, click on Fix It button, if source path is intact it will fix automatically if not it will ask for source path, please provide the same to fix automatically.
7. Once you are done you may select log button to generate log and share details.
8. More detailed document is published by author as part of attached link you may check the same.

It is better to use this exe all the times before you patch or if you find any issues while patching any SQL Server instance.

Note: this is just a guide that might help you. Please take your own decision to use it or not.

Configure a Linux Service to Start Automatically After a Crash or Reboot

Configure a Linux Service to Start Automatically After a Crash or Reboot Below articles defines how to start a service automatically after ...