Use Trace Flag 902 (TF902) to Bring Up Your SQL Service in Failed Post Upgrade Script

Published Nov 14 2021 06:29 AM 942 Views
Microsoft

Summary: This article discusses using TF902 as a quick workaround to bring up your SQL Server service if it fails to be started due to post upgrade script failure after installing SQL Server patch. Further troubleshooting steps to address the post upgrade script failure have been discussed in later part of this article.

 

Please be noted that it is not suggested to keep your SQL Server service in this state for long since your SQL Server is in an incomplete patch installation status and considered instable for your production environment. This is because part of your SQL DLL files, MSP files or related registry key record might have already been upgraded while a few other files or metadata are not upgraded. 

 

Why We Need To Run Post Upgrade Script After SQL Patch?

------------------------------------------------------------------

After you have finished most part of installing the SQL patch (the scenarioengine.exe process and a few other related SQL and Windows process finished the change made to MSP file, registry key, and DLL files), SQL Server service (sqlservr.exe) still needs to perform another step - to run a few post upgrade TSQL scripts - to successfully complete the change made by patch installation. This is because the patch installation process mainly makes change to the related MSP files, registry key, and DLL files. We still need to apply a few remaining changes made in DLL into SQL database level by running a few system TSQL scripts. Major part of these TSQL scripts are stored in the 'Install' folder for your SQL instance while another part of them are encrypted (i.e. C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Install).  If the post upgrade script cannot be successfully finished , SQL Server service cannot be started successfully after the patch as the system databases' status are not ready to be started on service startup process. 

 

Symptom

======

After installing SQL Patch, your SQL Server service cannot be started.

 

SQL Server service can still generate new ERROR LOG on starting up attempt.

 

In the latest SQL ERROR LOG file, you found below error as 'Script level upgrade for database '**' failed' indicating that SQL Server failed to perform the script level upgrade for a specific system database and caused subsequent shut down(Below are sample testing error from my LAB when reproducing the same issue)

 

2021-10-14 11:27:02.98 spid7s      Error: 912, Severity: 21, State: 2.

2021-10-14 11:27:02.98 spid7s      Script level upgrade for database '%' failed because upgrade step '%' encountered error 3930, state 1, severity 16. This is a serious error condition which might interfere with regular operation and the database will be taken offline. If the error happened during upgrade of the 'master' database, it will prevent the entire SQL Server instance from starting. Examine the previous errorlog entries for errors, take the appropriate corrective actions and re-start the database so that the script upgrade steps run to completion.

2021-10-14 11:27:02.98 spid7s     Error: 3417, Severity: 21, State: 3.

2021-10-14 11:27:02.99 spid7s     Cannot recover the master database. SQL Server is unable to run. Restore master from a full backup, repair it, or rebuild it. For more information about how to rebuild the master database, see SQL Server Books Online.

2021-10-14 11:27:02.99 spid7s     SQL Server shutdown has been initiated

 

 

Quick Workaround To Bring Up SQL Service(Not Suggested to Keep it for Long)

=====================================================

We can add trace flag 902 to SQL Server service startup parameter to skip the script upgrade process in order to bring up your SQL Server service.

 

However, we suggest to resolve the failure for finishing the post upgrade script as we cannot leave SQL Server service in this state with TF902 for long. It is an incomplete upgrade state and considered unstable. This is because part of your DLL files might have already been upgraded while a few other files or metadata are not upgraded. 

 

Further Troubleshooting Steps for Post Upgrade Script Failure

=========================================

The failure of post upgrade script can be caused by issue at database level in finishing the scripts or failure at patch installation level. 

 

The latter indicates that the post upgrade failure is actually a consequence of an earlier exception in patch installation phase rather than the root cause of the patch installation failure. To go further, we need to investigate the actual exception that causes the subsequent failure.

 

 

Phase 1: Directly check the error and failed script steps for post upgrade script failure issue

 

1. As the post upgrade script will also be performed at the end of the patch installation process, you can check the latest SQL Setup Bootstrap log folder for ERRORLOG file details to see in which step and in which script do we encounter the error. 

 

One of the common causes can be an orphaned SQL login which cannot be dropped and recreated when running a few of these post upgrade scripts.

 

2. If we don't have the detailed ERRORLOG that records the script upgrade failure details, we can find a down time , remove trace flag 902 from SQL startup parameter and add trace flag 3601 to SQL Server startup parameter. Then we can try to start SQL Server service to reproduce the issue. Trace flag 3601 will dump script level upgrade details to the SQL Error Log. This is a trace flag used by Microsoft for internal troubleshooting purpose. Please use it under instruction and avoid using it in your PRODUCTION environment. Alternatively, we can configure X-event on service startup to check the errors and TSQL script details when we reproduce the issue. Please be noted that part of these scripts are encrypted.

 

Phase 2: Check the Setup Bootstrap logs for more details of the exception on SQL Server patch installation process

 

If the post upgrade script is actually a consequence of failure encountered during SQL patch installation process , then the issue is not at database level but patch installation phase. We need to investigate the exception encountered on patch installation process in order to address the issue. 

 

If this is the case, check the 'Detials.txt' and other logs on the Setup Bootstrap 'Log' folder for this issued patch installation and investigate further.(Default path: “C:\Program Files\Microsoft SQL Server\<YourSQLVersion>\Setup Bootstrap\Log\<YourIssuedPatchInstallationTime>”)

 

In some simple scenarios, minor failure can be caused by file in use or lack of permission issue and can be quickly resolved by 'Repair' option in 'setup.exe' program to repair the corrupted patch installation.  

 

DISCLAIMER : THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

%3CLINGO-SUB%20id%3D%22lingo-sub-2963393%22%20slang%3D%22en-US%22%3EUse%20Trace%20Flag%20902%20(TF902)%20to%20Bring%20Up%20Your%20SQL%20Service%20in%20Failed%20Post%20Upgrade%20Script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2963393%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3ESummary%3A%3C%2FSTRONG%3E%20This%20article%20discusses%20using%20TF902%20as%20a%20quick%20workaround%20to%20bring%20up%20your%20SQL%20Server%20service%20if%20it%20fails%20to%20be%20started%20due%20to%20post%20upgrade%20script%20failure%20after%20installing%20SQL%20Server%20patch.%20Further%20troubleshooting%20steps%20to%20address%20the%20post%20upgrade%20script%20failure%20have%20been%20discussed%20in%20later%20part%20of%20this%20article.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPlease%20be%20%3CSTRONG%3Enoted%3C%2FSTRONG%3E%20that%20it%20is%20%3CU%3E%3CSTRONG%3Enot%3C%2FSTRONG%3E%20%3C%2FU%3Esuggested%20to%20keep%20your%20SQL%20Server%20service%20in%20this%20state%20for%20long%20since%20your%20SQL%20Server%20is%20in%20an%20incomplete%20patch%20installation%20status%20and%20considered%20instable%20for%20your%20production%20environment.%26nbsp%3BThis%20is%20because%20part%20of%20your%20SQL%20DLL%20files%2C%20MSP%20files%20or%20related%20registry%20key%20record%20might%20have%20already%20been%20upgraded%20while%20a%20few%20other%20files%20or%20metadata%20are%20not%20upgraded.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EWhy%20We%20Need%20To%20Run%20Post%20Upgrade%20Script%20After%20SQL%20Patch%3F%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E------------------------------------------------------------------%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EAfter%20you%20have%20finished%20most%20part%20of%20installing%20the%20SQL%20patch%20(the%20scenarioengine.exe%20process%20and%20a%20few%20other%20related%20SQL%20and%20Windows%20process%20finished%20the%20change%20made%20to%20MSP%20file%2C%20registry%20key%2C%20and%20DLL%20files)%2C%20SQL%20Server%20service%20(sqlservr.exe)%20still%20needs%20to%20perform%20another%20step%20-%20to%20run%20a%20few%20post%20upgrade%20TSQL%20scripts%20-%20to%20successfully%20complete%20the%20change%20made%20by%20patch%20installation.%20This%20is%20because%20the%20patch%20installation%20process%20mainly%20makes%20change%20to%20the%20related%20MSP%20files%2C%20registry%20key%2C%20and%20DLL%20files.%20We%20still%20need%20to%20apply%20a%20few%20remaining%20changes%20made%20in%20DLL%20into%20SQL%20database%20level%20by%20running%20a%20few%20system%20TSQL%20scripts.%20Major%20part%20of%20these%20TSQL%20scripts%20are%20stored%20in%20the%20'Install'%20folder%20for%20your%20SQL%20instance%20while%20another%20part%20of%20them%20are%20encrypted%20(i.e.%20C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL15.MSSQLSERVER%5CMSSQL%5CInstall).%26nbsp%3B%20If%20the%20post%20upgrade%20script%20cannot%20be%20successfully%20finished%20%2C%20SQL%20Server%20service%20cannot%20be%20started%20successfully%20after%20the%20patch%20as%20the%20system%20databases'%20status%20are%20not%20ready%20to%20be%20started%20on%20service%20startup%20process.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESymptom%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3D%3D%3D%3D%3D%3D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EAfter%20installing%20SQL%20Patch%2C%20your%20SQL%20Server%20service%20cannot%20be%20started.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESQL%20Server%20service%20can%20still%20generate%20new%20ERROR%20LOG%20on%20starting%20up%20attempt.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20latest%20SQL%20ERROR%20LOG%20file%2C%20you%20found%20below%20error%20as%20'Script%20level%20upgrade%20for%20database%20'**'%20failed'%20indicating%20that%20SQL%20Server%20failed%20to%20perform%20the%20script%20level%20upgrade%20for%20a%20specific%20system%20database%20and%20caused%20subsequent%20shut%20down(Below%20are%20sample%20testing%20error%20from%20my%20LAB%20when%20reproducing%20the%20same%20issue)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2021-10-14%2011%3A27%3A02.98%20spid7s%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Error%3A%20912%2C%20Severity%3A%2021%2C%20State%3A%202.%3C%2FP%3E%0A%3CP%3E2021-10-14%2011%3A27%3A02.98%20spid7s%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20Script%20level%20upgrade%20for%20database%20'%25'%20failed%20because%20upgrade%20step%20'%25'%20encountered%20error%203930%2C%20state%201%2C%20severity%2016.%20This%20is%20a%20serious%20error%20condition%20which%20might%20interfere%20with%20regular%20operation%20and%20the%20database%20will%20be%20taken%20offline.%20If%20the%20error%20happened%20during%20upgrade%20of%20the%20'master'%20database%2C%20it%20will%20prevent%20the%20entire%20SQL%20Server%20instance%20from%20starting.%20Examine%20the%20previous%20errorlog%20entries%20for%20errors%2C%20take%20the%20appropriate%20corrective%20actions%20and%20re-start%20the%20database%20so%20that%20the%20script%20upgrade%20steps%20run%20to%20completion.%3C%2FP%3E%0A%3CP%3E2021-10-14%2011%3A27%3A02.98%20spid7s%20%26nbsp%3B%20%26nbsp%3B%20Error%3A%203417%2C%20Severity%3A%2021%2C%20State%3A%203.%3C%2FP%3E%0A%3CP%3E2021-10-14%2011%3A27%3A02.99%20spid7s%20%26nbsp%3B%20%26nbsp%3B%20Cannot%20recover%20the%20master%20database.%20SQL%20Server%20is%20unable%20to%20run.%20Restore%20master%20from%20a%20full%20backup%2C%20repair%20it%2C%20or%20rebuild%20it.%20For%20more%20information%20about%20how%20to%20rebuild%20the%20master%20database%2C%20see%20SQL%20Server%20Books%20Online.%3C%2FP%3E%0A%3CP%3E2021-10-14%2011%3A27%3A02.99%20spid7s%20%26nbsp%3B%20%26nbsp%3B%20SQL%20Server%20shutdown%20has%20been%20initiated%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EQuick%20Workaround%20To%20Bring%20Up%20SQL%20Service(Not%20Suggested%20to%20Keep%20it%20for%20Long)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EWe%20can%20add%20trace%20flag%20902%20to%20SQL%20Server%20service%20startup%20parameter%20to%20skip%20the%20script%20upgrade%20process%20in%20order%20to%20bring%20up%20your%20SQL%20Server%20service.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHowever%2C%20we%20suggest%20to%20resolve%20the%20failure%20for%20finishing%20the%20post%20upgrade%20script%20as%20we%20cannot%20leave%20SQL%20Server%20service%20in%20this%20state%20with%20TF902%20for%20long.%20It%20is%20an%20incomplete%20upgrade%20state%20and%20considered%20unstable.%20This%20is%20because%20part%20of%20your%20DLL%20files%20might%20have%20already%20been%20upgraded%20while%20a%20few%20other%20files%20or%20metadata%20are%20not%20upgraded.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EFurther%20Troubleshooting%20Steps%20for%20Post%20Upgrade%20Script%20Failure%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20color%3D%22%230000FF%22%3EThe%20failure%20of%20post%20upgrade%20script%20can%20be%20caused%20by%20issue%20at%20database%20level%20in%20finishing%20the%20scripts%20or%20failure%20at%20patch%20installation%20level.%26nbsp%3B%20%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20color%3D%22%230000FF%22%3EThe%20latter%20indicates%20that%20the%20post%20upgrade%20failure%20is%20actually%20a%20consequence%20of%20an%20earlier%20exception%20in%20patch%20installation%20phase%20rather%20than%20the%20root%20cause%20of%20the%20patch%20installation%20failure.%20To%20go%20further%2C%20we%20need%20to%20investigate%20the%20actual%20exception%20that%20causes%20the%20subsequent%20failure.%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EPhase%201%3A%20Directly%20check%20the%20error%20and%20failed%20script%20steps%20for%20post%20upgrade%20script%20failure%20issue%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E1.%20As%20the%20post%20upgrade%20script%20will%20also%20be%20performed%20at%20the%20end%20of%20the%20patch%20installation%20process%2C%20you%20can%20check%20the%20latest%20SQL%20Setup%20Bootstrap%20log%20folder%20for%20ERRORLOG%20file%20details%20to%20see%20in%20which%20step%20and%20in%20which%20script%20do%20we%20encounter%20the%20error.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20of%20the%20common%20causes%20can%20be%20an%20orphaned%20SQL%20login%20which%20cannot%20be%20dropped%20and%20recreated%20when%20running%20a%20few%20of%20these%20post%20upgrade%20scripts.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2.%20If%20we%20don't%20have%20the%20detailed%20ERRORLOG%20that%20records%20the%20script%20upgrade%20failure%20details%2C%20we%20can%20find%20a%20down%20time%20%2C%20remove%20trace%20flag%20902%20from%20SQL%20startup%20parameter%20and%20add%20trace%20flag%203601%20to%20SQL%20Server%20startup%20parameter.%20Then%20we%20can%20try%20to%20start%20SQL%20Server%20service%20to%20reproduce%20the%20issue.%20Trace%20flag%203601%20will%20dump%20script%20level%20upgrade%20details%20to%20the%20SQL%20Error%20Log.%20This%20is%20a%20trace%20flag%20used%20by%20Microsoft%20for%20internal%20troubleshooting%20purpose.%20Please%20use%20it%20under%20instruction%20and%20avoid%20using%20it%20in%20your%20PRODUCTION%20environment.%20Alternatively%2C%20we%20can%20configure%20X-event%20on%20service%20startup%20to%20check%20the%20errors%20and%20TSQL%20script%20details%20when%20we%20reproduce%20the%20issue.%20Please%20be%20noted%20that%20part%20of%20these%20scripts%20are%20encrypted.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EPhase%202%3A%3C%2FSTRONG%3E%20%3CSTRONG%3ECheck%20the%20Setup%20Bootstrap%20logs%20for%20more%20details%20of%20the%20exception%20on%20SQL%20Server%20patch%20installation%20process%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20the%20post%20upgrade%20script%20is%20actually%20a%20consequence%20of%20failure%20encountered%20during%20SQL%20patch%20installation%20process%20%2C%20then%20the%20issue%20is%20not%20at%20database%20level%20but%20patch%20installation%20phase.%20We%20need%20to%20investigate%20the%20exception%20encountered%20on%20patch%20installation%20process%20in%20order%20to%20address%20the%20issue.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20this%20is%20the%20case%2C%20check%20the%20'Detials.txt'%20and%20other%20logs%20on%20the%20Setup%20Bootstrap%20'Log'%20folder%20for%20this%20issued%20patch%20installation%20and%20investigate%20further.(Default%20path%3A%20%E2%80%9CC%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5C%3CYOURSQLVERSION%3E%5CSetup%20Bootstrap%5CLog%5C%3CYOURISSUEDPATCHINSTALLATIONTIME%3E%E2%80%9D)%3C%2FYOURISSUEDPATCHINSTALLATIONTIME%3E%3C%2FYOURSQLVERSION%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20some%20simple%20scenarios%2C%20minor%20failure%20can%20be%20caused%20by%20file%20in%20use%20or%20lack%20of%20permission%20issue%20and%20can%20be%20quickly%20resolved%20by%20'Repair'%20option%20in%20'setup.exe'%20program%20to%20repair%20the%20corrupted%20patch%20installation.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3EDISCLAIMER%20%3A%20THIS%20CODE%20AND%20INFORMATION%20IS%20PROVIDED%20%22AS%20IS%22%20WITHOUT%20WARRANTY%20OF%20ANY%20KIND%2C%20EITHER%20EXPRESSED%20OR%20IMPLIED%2C%20INCLUDING%20BUT%20NOT%20LIMITED%20TO%20THE%20IMPLIED%20WARRANTIES%20OF%20MERCHANTABILITY%20AND%20FITNESS%20FOR%20A%20PARTICULAR%20PURPOSE.%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2963393%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESetup%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Nov 14 2021 08:51 AM
Updated by: