Home / Guides / Compare trace flags between SQL Server instances
Compare trace flags between SQL Server instances
A query is fast on one server and slow on the other, or a plan changes after a failover, and in the end the cause is a trace flag that someone turned on years ago on one instance only. Trace flags are instance settings, so nothing copies them to the other servers, and an availability group does not either. This guide shows the two places a flag can come from, the read-only queries that list both, a PowerShell script that compares any number of instances flag by flag, and where the comparison stops.
Short answer.
Run DBCC TRACESTATUS(-1) on every instance to see the flags that are on now, and read the -T entries of the
startup parameters (sys.dm_server_registry, or the Startup Parameters tab in SQL Server Configuration Manager) to see
the flags that come back after a restart. A flag that is on now but not at startup is lost at the next restart; a flag in the startup
parameters but not on now was turned off by hand. Compare both lists on each server.
The two places a global trace flag lives
- On now.
DBCC TRACEON (2528, -1)turns a flag on globally, for every connection. Microsoft's documentation says the effect of enabling a global flag this way is lost when the server restarts. A flag can also be on for just one session (without-1); those are not what you want to compare. - At startup. The
-Tstartup option starts the instance with a flag on, so it survives restarts. The parameters are stored in the registry and edited on the Startup Parameters tab of the SQL Server service in SQL Server Configuration Manager; a change takes effect when the Database Engine is restarted.
Use an uppercase -T with no space before the number. Microsoft notes that a lowercase -t is accepted but sets
other internal flags that only SQL Server support engineers need, so a lowercase one is not a way to enable a flag.
List them on one instance
The flags that are on now, globally and for your session:
DBCC TRACESTATUS(-1) WITH NO_INFOMSGS;
The result has the columns TraceFlag, Status, Global and Session. Any member of the public role can run it. The startup parameters of the instance you are connected to:
SELECT value_name, CONVERT(nvarchar(4000), value_data) AS value_data
FROM sys.dm_server_registry
WHERE registry_key LIKE N'%\Parameters' AND value_name LIKE N'SQLArg%'
ORDER BY value_name;
This DMV needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later) and works for
a Windows instance. You get one row per parameter: SQLArg0, SQLArg1 and SQLArg2 are the paths of
master, the error log and the master log, and each startup flag is a row like -T3226.
Compare many instances in PowerShell
The script reads both lists from each instance with your Windows login, builds one row per flag and one column per instance, and marks
the rows where the instances differ. It changes nothing. It uses System.Data.SqlClient, which ships with Windows PowerShell
5.1, so no module is needed. It trusts the server certificate in the connection string; remove TrustServerCertificate=True
if your servers have a certificate your machine trusts.
<#
.SYNOPSIS
Compare trace flags (on now, global) and startup -T parameters across SQL Server instances. Read-only.
.EXAMPLE
.\Compare-TraceFlags.ps1 -Instance 'SQL01','SQL02','SQL03\REPORTING'
#>
param(
[Parameter(Mandatory)][string[]]$Instance
)
function Get-TraceFlagState {
param([string]$Instance)
$cs = "Server=$Instance;Database=master;Integrated Security=SSPI;TrustServerCertificate=True;Connect Timeout=10;Application Name=TraceFlagCheck"
$conn = New-Object System.Data.SqlClient.SqlConnection $cs
try {
$conn.Open()
$now = [System.Collections.Generic.HashSet[int]]::new()
$startup = [System.Collections.Generic.HashSet[int]]::new()
# On now: global flags (Global = 1)
$cmd = $conn.CreateCommand()
$cmd.CommandText = 'DBCC TRACESTATUS(-1) WITH NO_INFOMSGS;'
$r = $cmd.ExecuteReader()
while ($r.Read()) {
if ($r['Global'] -eq 1 -and $r['Status'] -eq 1) { [void]$now.Add([int]$r['TraceFlag']) }
}
$r.Close()
# Set at startup: -T<number> entries in the startup parameters (capital T only)
$cmd.CommandText = @"
SELECT CONVERT(nvarchar(4000), value_data)
FROM sys.dm_server_registry
WHERE registry_key LIKE N'%\Parameters' AND value_name LIKE N'SQLArg%';
"@
$r = $cmd.ExecuteReader()
while ($r.Read()) {
$v = [string]$r[0] # value_data carries a trailing padding character, so do not anchor the end
if ($v -cmatch '^-T(\d+)') { [void]$startup.Add([int]$Matches[1]) }
}
$r.Close()
[pscustomobject]@{ Instance = $Instance; Now = $now; Startup = $startup; Error = $null }
}
catch {
[pscustomobject]@{ Instance = $Instance; Now = $null; Startup = $null; Error = $_.Exception.Message }
}
finally { $conn.Dispose() }
}
$states = foreach ($i in $Instance) { Get-TraceFlagState -Instance $i }
foreach ($s in $states | Where-Object Error) {
Write-Warning "$($s.Instance): $($s.Error)"
}
$ok = @($states | Where-Object { -not $_.Error })
if ($ok.Count -eq 0) { return }
$all = $ok | ForEach-Object { $_.Now; $_.Startup } | Sort-Object -Unique
$rows = foreach ($flag in $all) {
$row = [ordered]@{ Flag = $flag }
foreach ($s in $ok) {
$n = $s.Now.Contains([int]$flag); $t = $s.Startup.Contains([int]$flag)
$row[$s.Instance] = if ($n -and $t) { 'on' }
elseif ($n) { 'on now, not at startup' }
elseif ($t) { 'at startup, not on now' }
else { 'off' }
}
$same = ($ok | ForEach-Object { $row[$_.Instance] } | Sort-Object -Unique).Count -eq 1
$row['Differs'] = -not $same
[pscustomobject]$row
}
$rows | Format-Table -AutoSize
if (-not ($rows | Where-Object Differs)) { Write-Host 'All instances match.' }
Save it as C:\Scripts\Compare-TraceFlags.ps1 and run it with the instance names:
C:\Scripts\Compare-TraceFlags.ps1 -Instance SQL01, SQL02, SQL03\REPORTING
# Example output
Flag SQL01 SQL02 Differs
---- ----- ----- -------
3226 on at startup, not on now True
4199 on now, not at startup on now, not at startup False
8017 off on now, not at startup True
Tested on a Windows 11 test machine with SQL Server 2025 Developer and SQL Server 2022 Express instances. Flags 3226 and 4199 were turned on
with DBCC TRACEON and 3226 was also added to the startup parameters in the registry (then the service restarted). The script showed the
states above, skipped an instance that does not exist with a warning, and printed "All instances match." when the same instance was given twice.
One thing to know: value_data in sys.dm_server_registry carries a trailing padding character, so a match that ends
the pattern with $ finds nothing; the script matches only the start. The Express instance also had flag 8017 on by default, which
is why it appears in the output without anyone setting it. A lowercase -t9999 startup entry was listed by the DMV and turned flag 9999 on,
and the script correctly reported it as "on now, not at startup". It was not run against production servers or against an availability group.
Why it matters on availability group replicas
An availability group copies its databases to each replica. A trace flag is not stored in a database: it is a setting of the instance, in
that server's own startup parameters or in memory. So a flag set on the old primary does not exist on the new one, and after a failover
the workload runs without it. Compare the replicas before a planned failover, and after any maintenance in which someone restarted an instance
(a restart wipes every flag that was only turned on with DBCC TRACEON).
Where comparing this way stops
- Some flags are not meant to match. A flag that Microsoft documents for one edition or version, or that someone set on one server on purpose for a test, shows up as a difference. The script cannot tell a mistake from a decision; you decide, and the list of trace flags on Microsoft Learn says what each flag does and which versions it applies to.
- Session flags and other startup options. Only global flags and uppercase
-Tentries are compared. Other startup options (-m,-k,-Eand so on) are not. - Flags that need a restart. Some flags only work when set at startup;
DBCC TRACEONon those does not do what you want, so the fix is the startup parameter and a restart. Check the flag's entry in Microsoft's list. - Fixing is manual. Turn a flag on or off now with
DBCC TRACEONorDBCC TRACEOFFwith-1, and make it permanent in SQL Server Configuration Manager. Linux instances use mssql-conf, and the startup-parameter query above does not apply to them. - A snapshot. The script tells you what differs now. It does not run by itself and keeps no history.
Questions
How do I see which trace flags are enabled on SQL Server?
Run DBCC TRACESTATUS(-1) to list the flags that are enabled globally and for your session; any member of the public role can run it. To see the flags that will be on after a restart, read the -T entries of the startup parameters in SQL Server Configuration Manager or from sys.dm_server_registry.
Does a trace flag survive a restart?
Only if it is set as a startup parameter with -T (uppercase, no space before the number). Microsoft documents that the effect of enabling a global flag with DBCC TRACEON is lost on server restart.
Are trace flags copied to the other replicas of an availability group?
No. A trace flag is an instance setting, not data in a database, so an availability group does not copy it. Set the same flags on every replica and compare them before a failover.
What is the difference between -T and -t in the startup parameters?
-T with an uppercase T enables the trace flag you name. Microsoft notes that a lowercase t is accepted but sets other internal flags that only SQL Server support engineers need, so use the uppercase form.
Is there a tool that compares trace flags on a schedule?
SQLParity compares trace flags between the servers you group together, on a schedule or on request, and reports the differences. It can turn a flag on or off globally when you ask it to; startup parameters are never changed by it and stay a manual step.
Related
- Compare linked servers between two SQL Servers
- Compare SQL Agent jobs between two SQL Servers
- SQLParity: keep your SQL Servers the same
- All guides
Sources (Microsoft Learn): DBCC TRACESTATUS, Trace flags, Database Engine Service startup options, Configure server startup options, sys.dm_server_registry.