Git Product home page Git Product logo

darlingdata's Introduction

Darling Data: SQL Server Troubleshooting Scripts

licence badge

Navigatory

Who are these scripts for?

You need to troubleshoot performance problems with SQL Server, and you need to do it now.

You don't have time to track down a bunch of DMVs, figure out Extended Events, wrestle with terrible SSMS interfaces, or learn XML.

These scripts aren't a replacement for a mature monitoring tool, but they do a good job of capturing important issues and reporting on existing diagnostic data

Support

Right now, all support and Q&A is handled on GitHub. Please be patient; it's just me over here answering questions, fixing bugs, and adding new features.

As far as compatibility goes, they're only guaranted to work on Microsoft-supported SQL Server versions.

Older versions are either missing too much information, or simply aren't compatible (Hello, Extended Events. Hello, Query Store) with the intent of the script.

If you have questions about performance tuning, or SQL Server in general, you'll wanna hit a Q&A site:

Back to top

Pressure Detector

Is your client/server relationship on the rocks? Are queries timing out, dragging along, or causing CPU fans to spin out of control?

All you need to do is hit F5 to get information about:

  • Wait stats since startup
  • Database file size, stall, and activity
  • tempdb configuration details
  • Memory consumers
  • Low memory indicators
  • Memory configuration and allocation
  • Current query memory grants, along with other execution details
  • CPU configuration and retained utilization details
  • Thread count and current usage
  • Any current THREADPOOL waits (best observed with the DAC)
  • Currently executing queries, along with other execution details

For a video walkthrough of the script and the results, head over here.

Current valid parameter details:

parameter_name data_type description valid_inputs defaults
@what_to_check varchar areas to check for pressure "all", "cpu", and "memory" all
@skip_queries bit if you want to skip looking at running queries 0 or 1 0
@skip_plan_xml bit if you want to skip getting plan XML 0 or 1 0
@minimum_disk_latency_ms smallint low bound for reporting disk latency a reasonable number of milliseconds for disk latency 100
@cpu_utilization_threshold smallint low bound for reporting high cpu utlization a reasonable cpu utlization percentage 50
@skip_waits bit skips waits when you do not need them on every run 0 or 1 0
@skip_perfmon bit skips perfmon counters when you do not need them on every run 0 or 1 0
@sample_seconds tinyint take a sample of your server's metrics a valid tinyint: 0-255 0
@help bit how you got here 0 or 1 0
@debug bit prints dynamic sql, displays parameter and variable values, and table contents 0 or 1 0
@version varchar OUTPUT; for support none none; OUTPUT
@version_date datetime OUTPUT; for support none none; OUTPUT

Back to top

Human Events

Extended Events are hard. You don't know which ones to use, when to use them, or how to get useful information out of them.

This procedure is designed to make them easier for you, by creating event sessions to help you troubleshoot common scenarios:

  • Blocking: blocked process report
  • Query performance: query execution metrics an actual execution plans
  • Compiles: catch query compilations
  • Recompiles: catch query recompilations
  • Wait Stats: server wait stats, broken down by query and database

The default behavior is to run a session for a set period of time to capture information, but you can also set sessions up to data to permanent tables.

For execution examples, see here: Examples

If you set up sessions to capture long term data, you'll need an agent job set up to poll them. You can find an example of that here: Examples

Misuse of this procedure can harm performance. Be very careful about introducing observer overhead, especially when gathering query plans. Be even more careful when setting up permanent sessions!

More resources:

Current valid parameter details:

parameter name description valid_inputs defaults
@event_type sysname used to pick which session you want to run "blocking", "query", "waits", "recompiles", "compiles" and certain variations on those words "query"
@query_duration_ms int (>=) used to set a minimum query duration to collect data for an integer 500 (ms)
@query_sort_order nvarchar when you use the "query" event, lets you choose which metrics to sort results by "cpu", "reads", "writes", "duration", "memory", "spills", and you can add "avg" to sort by averages, e.g. "avg cpu" "cpu"
@skip_plans bit when you use the "query" event, lets you skip collecting actual execution plans 1 or 0 0
@blocking_duration_ms int (>=) used to set a minimum blocking duration to collect data for an integer 500 (ms)
@wait_type nvarchar (inclusive) filter to only specific wait types a single wait type, or a CSV list of wait types "all", which uses a list of "interesting" waits
@wait_duration_ms int (>=) used to set a minimum time per wait to collect data for an integer 10 (ms)
@client_app_name sysname (inclusive) filter to only specific app names a stringy thing intentionally left blank
@client_hostname sysname (inclusive) filter to only specific host names a stringy thing intentionally left blank
@database_name sysname (inclusive) filter to only specific databases a stringy thing intentionally left blank
@session_id nvarchar (inclusive) filter to only a specific session id, or a sample of session ids an integer, or "sample" to sample a workload intentionally left blank
@sample_divisor int the divisor for session ids when sampling a workload, e.g. SPID % 5 an integer 5
@username sysname (inclusive) filter to only a specific user a stringy thing intentionally left blank
@object_name sysname (inclusive) to only filter to a specific object name a stringy thing intentionally left blank
@object_schema sysname (inclusive) the schema of the object you want to filter to; only needed with blocking events a stringy thing dbo
@requested_memory_mb int (>=) the memory grant a query must ask for to have data collected an integer 0
@seconds_sample int the duration in seconds to run the event session for an integer 10
@gimme_danger bit used to override default minimums for query, wait, and blocking durations. 1 or 0 0
@keep_alive bit creates a permanent session, either to watch live or log to a table from 1 or 0 0
@custom_name nvarchar if you want to custom name a permanent session a stringy thing intentionally left blank
@output_database_name sysname the database you want to log data to a valid database name intentionally left blank
@output_schema_name sysname the schema you want to log data to a valid schema dbo
@delete_retention_days int how many days of logged data you want to keep a POSITIVE integer 3 (days)
@cleanup bit deletes all sessions, tables, and views. requires output database and schema. 1 or 0 0
@max_memory_kb bigint set a max ring buffer size to log data to an integer 102400
@version varchar to make sure you have the most recent bits none, output none, output
@version_date datetime to make sure you have the most recent bits none, output none, output
@debug bit use to print out dynamic SQL 1 or 0 0
@help bit well you're here so you figured this one out 1 or 0 0

Back to top

Human Events Block Viewer

This was originally a companion script to analyze the blocked process report Extended Event created by sp_HumanEvents, but has since turned into its own monster.

It will work on any Extended Event that captures the blocked process report. If you need to set that up, run these two pieces of code.

Enable the blocked process report:

EXEC sys.sp_configure
    N'show advanced options',
    1;
RECONFIGURE;
GO

EXEC sys.sp_configure
    N'blocked process threshold',
    5; --Seconds
RECONFIGURE;
GO

Set up the Extended Event:

CREATE EVENT SESSION 
    blocked_process_report
ON SERVER
    ADD EVENT 
        sqlserver.blocked_process_report
    ADD TARGET 
        package0.event_file
    (
        SET filename = N'bpr'
    )
WITH
(
    MAX_MEMORY = 4096KB,
    EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS,
    MAX_DISPATCH_LATENCY = 5 SECONDS,
    MAX_EVENT_SIZE = 0KB,
    MEMORY_PARTITION_MODE = NONE,
    TRACK_CAUSALITY = OFF,
    STARTUP_STATE = ON
);

ALTER EVENT SESSION
    blocked_process_report
ON SERVER 
    STATE = START;

Once it has data collected, you can analyze it using this command:

EXEC dbo.sp_HumanEventsBlockViewer
    @session_name = N'blocked_process_report';

Current valid parameter details:

parameter_name data_type description valid_inputs defaults
@session_name nvarchar name of the extended event session to pull from extended event session name capturing sqlserver.blocked_process_report keeper_HumanEvents_blocking
@target_type sysname target of the extended event session event_file or ring_buffer NULL
@start_date datetime2 filter by date a reasonable date NULL; will shortcut to last 7 days
@end_date datetime2 filter by date a reasonable date NULL
@database_name sysname filter by database name a database that exists on this server NULL
@object_name sysname filter by table name a schema-prefixed table name NULL
@help bit how you got here 0 or 1 0
@debug bit dumps raw temp table contents 0 or 1 0
@version varchar OUTPUT; for support none; OUTPUT none; OUTPUT
@version_date datetime OUTPUT; for support none; OUTPUT none; OUTPUT

Back to top

Quickie Store

This procedure will dig into Query Store data for a specific database, or all databases with Query Store enabled.

It's designed to run as quickly as possible, but there are some circumstances that prevent me from realizing my ultimate dream.

The big upside of using this stored procedure over the GUI is that you can search for specific items in Query Store, by:

  • query_id
  • plan_id
  • query hash
  • sql handle
  • module name
  • query text
  • query type (ad hoc or from a module)

You can also choose to filter out specific queries by those, too.

And you can do all that without worrying about incorrect data from the GUI, which doesn't handle UTC conversion correctly when filtering data.

By default, it will return the top 10 queries by average CPU. You can configure all sorts of things to look at queries by other metrics, or just specific queries.

Use the @expert_mode parameter to return additional details.

More examples can be found here: Examples

More resources:

Current valid parameter details:

parameter_name data_type description valid_inputs defaults
@database_name sysname the name of the database you want to look at query store in a database name with query store enabled NULL; current non-system database name if NULL
@sort_order varchar the runtime metric you want to prioritize results by cpu, logical reads, physical reads, writes, duration, memory, tempdb, executions, recent cpu
@top bigint the number of queries you want to pull back a positive integer between 1 and 9,223,372,036,854,775,807 10
@start_date datetimeoffset the begin date of your search, will be converted to UTC internally January 1, 1753, through December 31, 9999 the last seven days
@end_date datetimeoffset the end date of your search, will be converted to UTC internally January 1, 1753, through December 31, 9999 NULL
@timezone sysname user specified time zone to override dates displayed in results SELECT tzi.* FROM sys.time_zone_info AS tzi; NULL
@execution_count bigint the minimum number of executions a query must have a positive integer between 1 and 9,223,372,036,854,775,807 NULL
@duration_ms bigint the minimum duration a query must have to show up in results a positive integer between 1 and 9,223,372,036,854,775,807 NULL
@execution_type_desc nvarchar the type of execution you want to filter by (success, failure) regular, aborted, exception NULL
@procedure_schema sysname the schema of the procedure you're searching for a valid schema in your database NULL; dbo if NULL and procedure name is not NULL
@procedure_name sysname the name of the programmable object you're searching for a valid programmable object in your database NULL
@include_plan_ids nvarchar a list of plan ids to search for a string; comma separated for multiple ids NULL
@include_query_ids nvarchar a list of query ids to search for a string; comma separated for multiple ids NULL
@include_query_hashes nvarchar a list of query hashes to search for a string; comma separated for multiple hashes NULL
@include_plan_hashes nvarchar a list of query plan hashes to search for a string; comma separated for multiple hashes NULL
@include_sql_handles nvarchar a list of sql handles to search for a string; comma separated for multiple handles NULL
@ignore_plan_ids nvarchar a list of plan ids to ignore a string; comma separated for multiple ids NULL
@ignore_query_ids nvarchar a list of query ids to ignore a string; comma separated for multiple ids NULL
@ignore_query_hashes nvarchar a list of query hashes to ignore a string; comma separated for multiple hashes NULL
@ignore_plan_hashes nvarchar a list of query plan hashes to ignore a string; comma separated for multiple hashes NULL
@ignore_sql_handles nvarchar a list of sql handles to ignore a string; comma separated for multiple handles NULL
@query_text_search nvarchar query text to search for a string; leading and trailing wildcards will be added if missing NULL
@escape_brackets bit Set this bit to 1 to search for query text containing square brackets (common in .NET Entity Framework and other ORM queries) 0 or 1 0
@escape_character nchar Sets the ESCAPE character for special character searches, defaults to the SQL standard backslash () character some escape character, SQL standard is backslash () \
@only_queries_with_hints bit only return queries with query hints 0 or 1 0
@only_queries_with_feedback bit only return queries with query feedback 0 or 1 0
@only_queries_with_variants bit only return queries with query variants 0 or 1 0
@only_queries_with_forced_plans bit only return queries with forced plans 0 or 1 0
@only_queries_with_forced_plan_failures bit only return queries with forced plan failures 0 or 1 0
@wait_filter varchar wait category to search for; category details are below cpu, lock, latch, buffer latch, buffer io, log io, network io, parallelism, memory NULL
@query_type varchar filter for only ad hoc queries or only from queries from modules ad hoc, adhoc, proc, procedure, whatever. NULL
@expert_mode bit returns additional columns and results 0 or 1 0
@format_output bit returns numbers formatted with commas 0 or 1 1
@get_all_databases bit looks for query store enabled databases and returns combined results from all of them 0 or 1 0
@workdays bit use this to filter out weekends and after-hours queries 0 or 1 0
@work_start time use this to set a specific start of your work days a time like 8am, 9am or something 9am
@work_end time use this to set a specific end of your work days a time like 5pm, 6pm or something 5pm
@help bit how you got here 0 or 1 0
@debug bit prints dynamic sql, statement length, parameter and variable values, and raw temp table contents 0 or 1 0
@troubleshoot_performance bit set statistics xml on for queries against views 0 or 1 0
@version varchar OUTPUT; for support none; OUTPUT none; OUTPUT
@version_date datetime OUTPUT; for support none; OUTPUT none; OUTPUT

Back to top

Health Parser

The system health extended event has been around for a while, hiding in the shadows, and collecting all sorts of crazy information about your SQL Server.

The problem is, hardly anyone ever looks at it, and when they do, they realize how awful the Extended Events GUI is. Or that if they want to dig deeper into anything, they're going to have to parse XML.

This stored procedure takes all that pain away.

Note that it focuses on performance data, and does not output errors or security details, or any of the other non-performance related data.

Typical result set will show you

  • Queries with significant waits
  • Waits by count
  • Waits by duration
  • Potential I/O issues
  • CPU task details
  • Memory conditions
  • Overall system health
  • A limited version of the blocked process report
  • XML deadlock report
  • Query plans for queries involved in blocking and deadlocks (when available)

More resources:

Current valid parameter details:

parameter_name data_type description valid_inputs defaults
@what_to_check varchar areas of system health to check all, waits, disk, cpu, memory, system, locking all
@start_date datetimeoffset earliest date to show data for, will be internally converted to UTC a reasonable date seven days back
@end_date datetimeoffset latest date to show data for, will be internally converted to UTC a reasonable date current date
@warnings_only bit only show rows where a warning was reported NULL, 0, 1 0
@database_name sysname database name to show blocking events for the name of a database NULL
@wait_duration_ms bigint minimum wait duration the minimum duration of a wait for queries with interesting waits 0
@wait_round_interval_minutes bigint interval to round minutes to for wait stats interval to round minutes to for top wait stats by count and duration 60
@skip_locks bit skip the blocking and deadlocking section 0 or 1 0
@pending_task_threshold int minimum number of pending tasks to display a valid integer 10
@debug bit prints dynamic sql, selects from temp tables 0 or 1 0
@help bit how you got here 0 or 1 0
@version varchar OUTPUT; for support none none; OUTPUT
@version_date datetime OUTPUT; for support none none; OUTPUT

Back to top

Log Hunter

The SQL Server error log can have a lot of good information in it about what's goin on, whether it's right or wrong.

The problem is that it's hard to know what to look for, and what else was going on once you filter it.

It's another notoriously bad Microoft GUI, just like Query Store and Extended Events.

I created sp_LogHunter to search through your error logs for the important stuff, with some configurability for you, and return everything ordered by log entry time.

It helps you give you a fuller, better picture of any bad stuff happening.

More resources:

Current valid parameter details:

parameter_name data_type description valid_inputs defaults
@days_back int how many days back you want to search the logs an integer; will be converted to a negative number automatically -7
@start_date datetime if you want to search a specific time frame a datetime value NULL
@end_date datetime if you want to search a specific time frame a datetime value NULL
@custom_message nvarchar if you want to search for a custom string something specific you want to search for. no wildcards or substitions. NULL
@custom_message_only bit only search for the custom string NULL, 0, 1 0
@first_log_only bit only search through the first error log NULL, 0, 1 0
@language_id int to use something other than English SELECT DISTINCT m.language_id FROM sys.messages AS m ORDER BY m.language_id; 1033
@help bit how you got here NULL, 0, 1 0
@debug bit dumps raw temp table contents NULL, 0, 1 0
@version varchar OUTPUT; for support OUTPUT; for support none; OUTPUT
@version_date datetime OUTPUT; for support OUTPUT; for support none; OUTPUT

Back to top

darlingdata's People

Contributors

actions-user avatar baleng avatar billfinch avatar blitzerik avatar claudioessilva avatar davedustin avatar drewfurgiuele avatar erikdarling avatar erikdarlingdata avatar hannahvernon avatar holidasa avatar indexseek avatar kendra-little avatar litknd avatar micke314 avatar mjswart avatar mnemonic23 avatar mssqlserverdba avatar olegstrutinskii avatar platzer avatar reecegoding avatar shaunaustin-koderly avatar wqweto avatar zikato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

darlingdata's Issues

sp_HumanEvents error handling - transaction rollback

Hi, the transaction rollback part in the TRY CATCH block seems wrong. This is the current code:
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;

First, as you don't begin any transaction you should not roll back any; if there is a transaction open, it is not yours to rollback. Second, @@TRANCOUNT can be more than 1 and in that case ROLLBACK will just decrement @@TRANCOUNT by 1, but it will not do anything else. My suggestion is to leave the transaction part alone and remove this piece of code.

Problem collecting from XE QUERY "too many levels"

Version of the script
Latest

What is the current behavior?
SQL Agent collection job fails

Generating insert table statement for keeper_HumanEvents_query
[SQLSTATE 01000] (Message 50000) Error number 6335 with severity 16 and a state of 101 in procedure sp_HumanEvents on line 2896 XML datatype instance has too many levels of nested nodes. Maximum allowed depth is 128 levels.
[SQLSTATE 42000] (Error 50000) XML datatype instance has too many levels of nest... The step failed.

If the current behavior is a bug, please provide the steps to reproduce.
I don't know how to reproduce this behavior.

What is the expected behavior?
Just run and fill tables with monitored data

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?

Microsoft SQL Server 2016 (SP2-CU12) (KB4536648) - 13.0.5698.0 (X64) Feb 15 2020 01:47:30 Copyright (c) Microsoft Corporation Enterprise Edition: Core-based Licensing (64-bit) on Windows Server 2012 R2 Standard 6.3 (Build 9600: ) (Hypervisor)

Error at line 2021 when running with @event_type = 'blocking'

Version of the script
1.5

What is the current behavior?
The following error is thrown:
Msg 205, Level 16, State 1, Procedure dbo.sp_HumanEvents, Line 2021 [Batch Start Line 1]
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

Cause:
The column sqlhandle is missing from the INSERT INTO #blocking statement.

If the current behavior is a bug, please provide the steps to reproduce.
Run
EXEC dbo.sp_HumanEvents @event_type = 'blocking', @seconds_sample = 1, @database_name = '{aDatabase}';
Replacing {aDatabase} with the name of any available database

What is the expected behavior?
The sproc should not error out and should report on any blocked queries as per the documentation.

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
Tested (and reproduced) on Windows Server 2019 with SQL 15.0.4033.1

get_numbers fails on NULL strings

The functions work but they don't handle NULL strings.

CREATE TABLE #dummy (col1 VARCHAR(50))

INSERT INTO #dummy VALUES (NULL)
INSERT INTO #dummy VALUES ('ABC123')

SELECT  d.col1, gn.*
FROM #dummy d
    CROSS APPLY dbo.get_numbers(d.col1) AS gn

Msg 1014, Level 15, State 1, Line 25
A TOP or FETCH clause contains an invalid value.

-- works if you denullify on the way in
SELECT  d.col1, gn.*
FROM #dummy d
    CROSS APPLY dbo.get_numbers(ISNULL(d.col1, '')) AS gn

col1   numbers_only
------ ------------
NULL   NULL
ABC123 123

If you update the function code to handle the NULLs you don't have to worry about it as the caller. Not sure the impact on performance but something like:

WITH x
  AS ( SELECT TOP (LEN(ISNULL(@string, '')))

Then call without errors, without checking for NULL:

SELECT  d.col1, gn.*
FROM #dummy d
    CROSS APPLY dbo.get_numbers(d.col1) AS gn

col1   numbers_only
------ ------------
NULL   NULL
ABC123 123

sp_pressure_detector

SELECT @Version = '1.0', @versiondate = '20200301';

For versions of SQL Server if they don't have the following 2 columns.
deqmg.reserved_worker_count and deqmg.used_worker_count

The following error message occurs.
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near '.'.
Msg 156, Level 15, State 1, Line 24
Incorrect syntax near the keyword 'ORDER'

Consider Changing the placement of the ,

            waits.wait_type'
            + CASE WHEN @helpful_new_columns = 1
                   THEN N',
            deqmg.reserved_worker_count,
            deqmg.used_worker_count,'
                   ELSE N''
            END

to

            waits.wait_type,'
            + CASE WHEN @helpful_new_columns = 1
                   THEN N'
            deqmg.reserved_worker_count,
            deqmg.used_worker_count,'
                   ELSE N''
            END

Would be nice to have the query text in the query plan

Is your feature request related to a problem? Please describe.
When looking at the query plan from sp_HumanEvents, the query text is missing.

Describe the solution you'd like
Would be nice to have the query text in the query plan

Describe alternatives you've considered
You could have look in the column statement_text but ...

Are you ready to build the code for the feature?
Sure, just need to get friendly with GitHub

SP_humanevents

SP_humanevents failing with below error on SQl server 2012 Always on SP2.
sp_humanevents @event_type = 'Queries',@seconds_sample = 30;

Error:
Msg 50000, Level 16, State 1, Procedure sp_HumanEvents, Line 3050 [Batch Start Line 0]

Msg 25623, Level 16, State 3, Line 2
The event action name, "sqlserver.query_hash_signed", is invalid, or the object could not be found

Azure SQL Database

Which script is your question about?

sp_PressureDetector

Attached is an Azure SQL Database Compatible Version of sp_PressureDetector.

sp_PressureDetector.txt

If you comment out the "DAC-enabled" check it works perfectly.

Thanks again for these Erik - brilliant scripts to have.

Execution count for queries is 1

Started using this for real today, fantastic.

The latest version surfaced a bug in execution count for queries.
This should not do the OVER() part since you already group by the same columns.

WITH queries AS 
             (
                 SELECT COUNT_BIG(*) OVER ( PARTITION BY q.query_plan_hash_signed,
                                                         q.query_hash_signed,
                                                         q.plan_handle ) AS executions,
                        q.query_plan_hash_signed,
                        q.query_hash_signed,
                        q.plan_handle
                 FROM #queries AS q
                 GROUP BY --q.event_time,
                          q.query_plan_hash_signed,
                          q.query_hash_signed,
                          q.plan_handle

Clean up mode

Add a flag to clean up after ourselves.

  • events
  • tables
  • views

User will have to supply the database to target for views and tables, but can re-use existing variables for that.

sp_QuickieStore Execution errors

Version of the script
sp_QuickieStore 2021
Executing on SQL Server 2017 Standard

What is the current behavior?
Compiles ok but getting the following error when executing with any parameters

Msg 50000, Level 11, State 1, Procedure sp_QuickieStore, Line 4300 [Batch Start Line 0]
error while (null)
offending query:
(null)
Msg 50000, Level 11, State 1, Procedure sp_QuickieStore, Line 76 [Batch Start Line 0]
This procedure only runs on supported versions of SQL Server

If the current behavior is a bug, please provide the steps to reproduce.
Execute with any variables flags, etc or debug

What is the expected behavior?
Program runs without error

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
SQL Server 2017 on Windows Server 2016

Add views on top of logging tables to present data better

Logging to tables is done, but it's raw data. Getting data INTO the tables in good shape would have been terrible. As-is, it was pretty tedious getting everything right. Anyway, now I need to get the view data into a presentable format for people. They'll have to be created dynamically in the proc, which sucks, but maybe I can use synonyms.

Invalid object name '#waits_agg' @debug = 1 mode

Version of the script
SELECT @Version = '1.0', @version_date = '20200301';

What is the current behavior?
Msg 208, Level 16, State 0, Procedure dbo.sp_HumanEvents, Line 1442 [Batch Start Line 12]
Invalid object name '#waits_agg'.

If the current behavior is a bug, please provide the steps to reproduce.
EXEC dbo.sp_HumanEvents @event_type = 'blocking', @debug = 1;

What is the expected behavior?
#waits_agg is not relevant?

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
WIN10 / SQL2019

sp_HumanEvents: Memory Grant info is gonna be wacky in views

The only one of the "query" events that gets memory grant information is the query plan event, which

  • Reports it for the batch which sucks for procs
  • But has it in the query plans

I can fix this in the proc easily by hitting the xml memory grant info real quick. It's not going to be as easy in the views. My options are to give misleading details for queries that are in stored procs, or add xml parsing to the view to try to get correct information. Neither one is awesome.

Make Azure SQL DB Compatible

This is an easy fix, all I need to do is change the event session to ON DATABASE instead of ON SERVER for it to work.

Blocking Waittime in View

Version of the script
SELECT @Version = '1.0', @version_date = '20200301';

What is the current behavior?
The wait_time is updated for all individual events when you test the same blocking situation twice.
So if the wait on the first session was 10 sec and the other session is 20 sec, the first session is updated also with 20 sec.

If the current behavior is a bug, please provide the steps to reproduce.
Create a blocking situation check the waittime in the table dbo.keeper_HumanEvents_blocking
Wait a little bit and do the step above again.

What is the expected behavior?
The wait_times of the two sessions should differ, group by transaction_id?

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
WIN10 / SQL2019

Username validation for AD accounts doesn't work

It seems like if domain accounts are tied to MSAs or gMSAs then my crafty username check doesn't work.

Not sure if I should

  • Skip the check and just validate the string like I do others
  • Skip the check if the username has a \ in it
  • Something else?

Stored procedure creation script does not complete on SQL Server 2017 Web Edition

Version of the script
@Version = '1.5', @version_date = '20200501'

What is the current behavior?
When attempting to run the install script on SQL Server 2017 Web Edition (14.0.3370.1), the initial, almost empty stored procedure is created. However, the rest of the script throws the errors below:

Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'PROCEDURE'.
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'BEGIN'.
Msg 137, Level 15, State 1, Line 23
Must declare the scalar variable "@version".
Msg 137, Level 15, State 2, Line 25
Must declare the scalar variable "@help".
Msg 137, Level 15, State 2, Line 307
Must declare the scalar variable "@keep_alive".
Msg 137, Level 15, State 2, Line 309
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 311
Must declare the scalar variable "@keep_alive".
Msg 137, Level 15, State 2, Line 313
Must declare the scalar variable "@event_type".
Msg 156, Level 15, State 1, Line 315
Incorrect syntax near the keyword 'IF'.
Msg 137, Level 15, State 2, Line 317
Must declare the scalar variable "@max_memory_kb".
Msg 319, Level 15, State 1, Line 318
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 1, Line 319
Must declare the scalar variable "@max_memory_kb".
Msg 137, Level 15, State 2, Line 326
Must declare the scalar variable "@max_memory_kb".
Msg 137, Level 15, State 2, Line 384
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 385
Must declare the scalar variable "@client_app_name".
Msg 137, Level 15, State 2, Line 386
Must declare the scalar variable "@client_hostname".
Msg 137, Level 15, State 2, Line 387
Must declare the scalar variable "@database_name".
Msg 137, Level 15, State 2, Line 388
Must declare the scalar variable "@session_id".
Msg 137, Level 15, State 2, Line 389
Must declare the scalar variable "@username".
Msg 137, Level 15, State 2, Line 390
Must declare the scalar variable "@object_name".
Msg 137, Level 15, State 2, Line 391
Must declare the scalar variable "@object_schema".
Msg 137, Level 15, State 2, Line 392
Must declare the scalar variable "@custom_name".
Msg 137, Level 15, State 2, Line 393
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 394
Must declare the scalar variable "@output_schema_name".
Msg 137, Level 15, State 2, Line 395
Must declare the scalar variable "@database_name".
Msg 137, Level 15, State 2, Line 396
Must declare the scalar variable "@database_name".
Msg 137, Level 15, State 2, Line 399
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 403
Must declare the scalar variable "@event_type".
Msg 319, Level 15, State 1, Line 404
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 409
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 1, Line 421
Must declare the scalar variable "@query_duration_ms".
Msg 137, Level 15, State 2, Line 425
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 1, Line 436
Must declare the scalar variable "@wait_duration_ms".
Msg 137, Level 15, State 2, Line 440
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 1, Line 451
Must declare the scalar variable "@blocking_duration_ms".
Msg 137, Level 15, State 2, Line 455
Must declare the scalar variable "@query_sort_order".
Msg 137, Level 15, State 2, Line 457
Must declare the scalar variable "@query_sort_order".
Msg 319, Level 15, State 1, Line 458
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 1, Line 459
Must declare the scalar variable "@query_sort_order".
Msg 137, Level 15, State 2, Line 463
Must declare the scalar variable "@wait_type".
Msg 137, Level 15, State 2, Line 470
Must declare the scalar variable "@wait_type".
Msg 137, Level 15, State 2, Line 473
Must declare the scalar variable "@wait_type".
Msg 137, Level 15, State 2, Line 477
Must declare the scalar variable "@wait_type".
Msg 137, Level 15, State 2, Line 498
Must declare the scalar variable "@client_app_name".
Msg 137, Level 15, State 2, Line 516
Must declare the scalar variable "@client_app_name".
Msg 137, Level 15, State 2, Line 543
Must declare the scalar variable "@wait_type".
Msg 137, Level 15, State 2, Line 553
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 561
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 565
Must declare the scalar variable "@fully_formed_babby".
Msg 319, Level 15, State 1, Line 566
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 571
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 591
Must declare the scalar variable "@database_name".
Msg 137, Level 15, State 2, Line 593
Must declare the scalar variable "@database_name".
Msg 137, Level 15, State 2, Line 595
Must declare the scalar variable "@database_name".
Msg 319, Level 15, State 1, Line 596
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 602
Must declare the scalar variable "@session_id".
Msg 137, Level 15, State 2, Line 606
Must declare the scalar variable "@session_id".
Msg 319, Level 15, State 1, Line 607
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 612
Must declare the scalar variable "@sample_divisor".
Msg 137, Level 15, State 2, Line 623
Must declare the scalar variable "@seconds_sample".
Msg 137, Level 15, State 2, Line 626
Must declare the scalar variable "@seconds_sample".
Msg 137, Level 15, State 2, Line 628
Must declare the scalar variable "@gimme_danger".
Msg 137, Level 15, State 2, Line 641
Must declare the scalar variable "@seconds_sample".
Msg 137, Level 15, State 2, Line 642
Must declare the scalar variable "@seconds_sample".
Msg 137, Level 15, State 2, Line 648
Must declare the scalar variable "@seconds_sample".
Msg 137, Level 15, State 2, Line 684
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 686
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 688
Must declare the scalar variable "@output_database_name".
Msg 319, Level 15, State 1, Line 689
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 695
Must declare the scalar variable "@output_schema_name".
Msg 137, Level 15, State 2, Line 699
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 702
Must declare the scalar variable "@s_sql".
Msg 137, Level 15, State 2, Line 703
Must declare the scalar variable "@s_out".
Msg 137, Level 15, State 2, Line 705
Must declare the scalar variable "@output_schema_name".
Msg 137, Level 15, State 2, Line 711
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 716
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 722
Must declare the scalar variable "@output_schema_name".
Msg 137, Level 15, State 2, Line 731
Must declare the scalar variable "@custom_name".
Msg 137, Level 15, State 2, Line 734
Must declare the scalar variable "@custom_name".
Msg 319, Level 15, State 1, Line 735
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 736
Must declare the scalar variable "@custom_name".
Msg 319, Level 15, State 1, Line 737
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 742
Must declare the scalar variable "@delete_retention_days".
Msg 137, Level 15, State 1, Line 744
Must declare the scalar variable "@delete_retention_days".
Msg 137, Level 15, State 2, Line 750
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 764
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 775
Must declare the scalar variable "@query_duration_ms".
Msg 137, Level 15, State 2, Line 777
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 779
Must declare the scalar variable "@query_duration_ms".
Msg 137, Level 15, State 2, Line 782
Must declare the scalar variable "@blocking_duration_ms".
Msg 137, Level 15, State 2, Line 784
Must declare the scalar variable "@blocking_duration_ms".
Msg 137, Level 15, State 2, Line 786
Must declare the scalar variable "@wait_duration_ms".
Msg 137, Level 15, State 2, Line 788
Must declare the scalar variable "@wait_duration_ms".
Msg 137, Level 15, State 2, Line 790
Must declare the scalar variable "@client_app_name".
Msg 137, Level 15, State 2, Line 792
Must declare the scalar variable "@client_app_name".
Msg 137, Level 15, State 2, Line 794
Must declare the scalar variable "@client_hostname".
Msg 137, Level 15, State 2, Line 796
Must declare the scalar variable "@client_hostname".
Msg 137, Level 15, State 2, Line 798
Must declare the scalar variable "@database_name".
Msg 137, Level 15, State 2, Line 800
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 802
Must declare the scalar variable "@database_name".
Msg 137, Level 15, State 2, Line 804
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 806
Must declare the scalar variable "@database_name".
Msg 137, Level 15, State 2, Line 809
Must declare the scalar variable "@session_id".
Msg 137, Level 15, State 2, Line 811
Must declare the scalar variable "@session_id".
Msg 137, Level 15, State 2, Line 813
Must declare the scalar variable "@session_id".
Msg 137, Level 15, State 2, Line 815
Must declare the scalar variable "@session_id".
Msg 137, Level 15, State 2, Line 817
Must declare the scalar variable "@sample_divisor".
Msg 137, Level 15, State 2, Line 820
Must declare the scalar variable "@username".
Msg 137, Level 15, State 2, Line 822
Must declare the scalar variable "@username".
Msg 137, Level 15, State 2, Line 824
Must declare the scalar variable "@object_name".
Msg 137, Level 15, State 2, Line 826
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 829
Must declare the scalar variable "@fully_formed_babby".
Msg 137, Level 15, State 2, Line 832
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 834
Must declare the scalar variable "@object_name".
Msg 137, Level 15, State 2, Line 837
Must declare the scalar variable "@requested_memory_mb".
Msg 137, Level 15, State 2, Line 839
Must declare the scalar variable "@requested_memory_mb".
Msg 137, Level 15, State 2, Line 840
Must declare the scalar variable "@requested_memory_kb".
Msg 137, Level 15, State 2, Line 842
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 850
Must declare the scalar variable "@wait_type".
Msg 137, Level 15, State 2, Line 854
Must declare the scalar variable "@wait_type".
Msg 137, Level 15, State 2, Line 885
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 943
Must declare the scalar variable "@session_with".
Msg 137, Level 15, State 2, Line 944
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 950
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 956
Must declare the scalar variable "@keep_alive".
Msg 137, Level 15, State 2, Line 995
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1001
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 1034
Must declare the scalar variable "@debug".
Msg 319, Level 15, State 1, Line 1041
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 1140
Must declare the scalar variable "@debug".
Msg 319, Level 15, State 1, Line 1147
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 1229
Must declare the scalar variable "@query_sort_order".
Msg 137, Level 15, State 2, Line 1232
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 1252
Must declare the scalar variable "@debug".
Msg 319, Level 15, State 1, Line 1259
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 1298
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1339
Must declare the scalar variable "@debug".
Msg 319, Level 15, State 1, Line 1346
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 1385
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 1404
Must declare the scalar variable "@debug".
Msg 319, Level 15, State 1, Line 1411
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 1452
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1469
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 1485
Must declare the scalar variable "@gimme_danger".
Msg 137, Level 15, State 2, Line 1490
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1550
Must declare the scalar variable "@event_type".
Msg 137, Level 15, State 2, Line 1579
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1613
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1648
Must declare the scalar variable "@keep_alive".
Msg 137, Level 15, State 2, Line 1650
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1658
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1701
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1731
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 1766
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1803
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1816
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1828
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1913
Must declare the scalar variable "@output_database_name".
Msg 319, Level 15, State 1, Line 1914
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 1916
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 1940
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1982
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 1987
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2003
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2014
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2284
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2304
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2311
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2319
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2331
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2353
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2358
Must declare the scalar variable "@delete_retention_days".
Msg 137, Level 15, State 2, Line 2368
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 2377
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2393
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 2396
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2398
Must declare the scalar variable "@executer".
Msg 319, Level 15, State 1, Line 2399
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 2403
Must declare the scalar variable "@executer".
Msg 137, Level 15, State 2, Line 2415
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 2418
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2420
Must declare the scalar variable "@executer".
Msg 319, Level 15, State 1, Line 2421
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 137, Level 15, State 2, Line 2425
Must declare the scalar variable "@executer".
Msg 137, Level 15, State 2, Line 2435
Must declare the scalar variable "@output_database_name".
Msg 137, Level 15, State 2, Line 2438
Must declare the scalar variable "@debug".
Msg 137, Level 15, State 2, Line 2446
Must declare the scalar variable "@debug".
Msg 178, Level 15, State 1, Line 2458
A RETURN statement with a return value cannot be used in this context.
Msg 8180, Level 16, State 1, Procedure sp_describe_parameter_encryption, Line 1 [Batch Start Line 15]
Statement(s) could not be prepared.
An error occurred while executing batch. Error message is: Internal error. Metadata for parameter '@pfb85fa3a74bc497b9a6efd2420ed5861' in statement or procedure 'ALTER PROCEDURE dbo.sp_HumanEvents
@event_type sysname=N'query', @query_duration_ms INT=500, @query_sort_order NVARCHAR (10)=N'cpu', @blocking_duration_ms INT=500, @wait_type NVARCHAR (4000)=N'ALL', @wait_duration_ms INT=10, @client_app_name sysname=N'', @client_hostname sysname=N'', @database_name sysname=N'', @session_id NVARCHAR (7)=N'', @sample_divisor INT=5, @username sysname=N'', @object_name sysname=N'', @object_schema sysname=N'dbo', @requested_memory_mb INT=0, @seconds_sample INT=10, @gimme_danger BIT=0, @keep_alive BIT=0, @custom_name NVARCHAR (256)=N'', @output_database_name sysname=N'', @output_schema_name sysname=N'dbo', @delete_retention_days INT=3, @cleanup BIT=0, @max_memory_kb BIGINT=102400, @version VARCHAR (30)=NULL OUTPUT, @version_date DATETIME=NULL OUTPUT, @debug BIT=0, @help BIT=0
WITH RECOMPILE
AS
BEGIN
    SET NOCOUNT, XACT_ABORT ON;
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
    SELECT @version = '1.5',
           @version_date = '20200501';
    IF @help = 1
 ...' is missing in resultset returned by sp_describe_parameter_encryption.

Completion time: 2021-02-18T20:09:06.6604991+02:00

If the current behavior is a bug, please provide the steps to reproduce.

  1. Copy the raw script for sp_HumanEvents.sql from Github
  2. Paste it into SSMS and select the master database from the dropdown. Ensure the database server is running SQL 2017 Web edition.
  3. Run the script. Observe the errors in the messages window.

What is the expected behavior?
The stored procedure should be installed in the master database with no errors. The script runs perfectly in SQL Server 2017 development edition.

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
SQL Server 2017 Web Edition (14.0.3370.1) on Windows 2012R2. This is the first time I am attempting to run this script.

arithmetic overflow error for type int

Version of the script
20200501

What is the current behavior?
arithmetic overflow error for type int when insert into keeper_HumanEvents_blocking

If the current behavior is a bug, please provide the steps to reproduce.
This only happens on an extraordinarily busy server, I think the transactionID is greater than the int range

What is the expected behavior?
As discussed

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
SQL 2017 Windows 2016. This is my first attempt to use this
if I change the int to Bigint for transaction_id then everything will work. for table keeper_HumanEvents_blocking
N' transaction_id BIGINT NULL, resource_owner_type NVARCHAR(256) NULL, monitor_loop INT NULL, spid INT NULL, ecid INT NULL, query_text NVARCHAR(MAX) NULL, ' +

sp_HumanEvents: Add ability to use lightweight plan collection XE

Add a flag to use this XE instead of the other plan collection XE: query_post_execution_plan_profile

CREATE EVENT SESSION query_thread_profile ON SERVER
ADD EVENT sqlserver.query_thread_profile
(
  ACTION(sqlserver.database_name, 
         sqlserver.plan_handle, 
         sqlserver.query_hash_signed, 
         sqlserver.query_plan_hash_signed, 
         sqlserver.sql_text))

sp_HumanEvents: Add functionality to write ring buffer data off to tables

Would need a mechanism sort of like sp_AllNightLog to grab from active sessions and push to tables.

What'd have to happen:

  • Sessions would need to be created with @keepalive = 1
  • That would prefix the session type with "permanent" or something
  • A separate code path would run and grab new data (which means I'd need a logging mechanism to keep track of when the last collection was) from any XE session with permanentHumanEvents in the name

Running from Agent

Version of the script

SELECT @Version = '1.5', @version_date = '20200501';

What is the current behavior?

First, run:

sp_HumanEvents
      @event_type = 'query'
    , @query_duration_ms = 100
    , @client_hostname = 'VPS'
    , @database_name = 'V2_GATE'
    , @sample_divisor = 1
    , @object_schema = 'Web'
    , @keep_alive = 1

Then create job with step code below

sp_HumanEvents
      @output_database_name = 'dbaTools'
    , @output_schema_name = 'xeWeb'

Then I do first test run with code above (job step) in SSMS

Output

Do we skip to the GOTO and log tables?
Skipping all the other stuff and going to data logging
Starting data collection.
Sessions without tables found, starting loop.
While, while, while...
Updating #human_events_worker to set is_table_created for keeper_HumanEvents_query
Setting next id after 1 out of 1 total
Found views to create, beginning!
#view_check doesn't exist, creating and populating
Updating #view_check with output database (dbaTools) and schema (xeWeb)
Updating #view_check with table names
Starting view creation loop
creating view humanevents_queries
Setting next id after 3 out of 3 total
Sessions that need data found, starting loop.
Generating insert table statement for keeper_HumanEvents_query
Setting next id after 1 out of 1 total
Found views to create, beginning!
Sessions that need data found, starting loop.
Generating insert table statement for keeper_HumanEvents_query
Setting next id after 1 out of 1 total
Query was canceled by user.

For the second attempt got this error:

Updating #human_events_worker to set is_table_created for keeper_HumanEvents_query
Setting next id after 1 out of 1 total
Found views to create, beginning!
#view_check doesn't exist, creating and populating
Updating #view_check with output database (dbaTools) and schema (xeWeb)
Updating #view_check with table names
Starting view creation loop
creating view humanevents_queries
Msg 50000, Level 16, State 1, Procedure sp_HumanEvents, Line 3110 [Batch Start Line 11]
Error number 2714 with severity 16 and a state of 3 in procedure HumanEvents_Queries on line 1
There is already an object named 'HumanEvents_Queries' in the database.
Msg 2714, Level 16, State 3, Procedure HumanEvents_Queries, Line 1 [Batch Start Line 11]
There is already an object named 'HumanEvents_Queries' in the database.

What is the expected behavior?

That just works =)

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?

sp_HumanEvents: Add ability to change XE definition settings

Stuff people might wanna change:

  • max memory
  • use a file instead of the ring buffer
  • change startup state

I don't wanna go down the causality tracking route right now, because I'm not sure how helpful it is to any of the events I'm grabbing. It seems more useful with multiple events in one session.

It also adds some additional stuff to the XML parsing which wouldn't populate with it off, which means I'd be stuck using dynamic SQL with XML parsing. Yeesh.

sp_HumanEvents: More reliable error handling

Right now I have error handling "implemented", but the catch block doesn't seem to fire reliably. I need to figure out why, so that orphaned sessions aren't left up on the server.

requested_memory_mb error

Msg 50000, Level 16, State 1, Procedure sp_HumanEvents, Line 3105 [Batch Start Line 1]

Msg 25713, Level 16, State 15, Line 3
The value specified for event attribute or predicate source, "requested_memory_kb", event, "query_post_execution_showplan", is invalid.

EXEC dbo.sp_HumanEvents @event_type = 'query', @query_duration_ms = 1000, @seconds_sample = 20, @requested_memory_mb = 1024;

Microsoft SQL Server 2016 (SP2-CU11) (KB4527378) - 13.0.5598.27 (X64)
Nov 27 2019 18:09:22
Copyright (c) Microsoft Corporation
Enterprise Edition: Core-based Licensing (64-bit) on Windows Server 2016 Datacenter 10.0 (Build 14393: ) (Hypervisor)

Help error message on running sp_HumanEvents

I run this using latest version in master:
EXEC sp_HumanEvents @output_database_name = N'HumanEvents', @output_schema_name = N'dbo';

and the messages tab has this at the end:

Generating insert table statement for keeper_HumanEvents_query
Msg 50000, Level 16, State 1, Procedure sp_HumanEvents, Line 3049 [Batch Start Line 5]

Msg 248, Level 16, State 1, Line 6
The conversion of the nvarchar value '8918235844' overflowed an int column.

Is this fixed in dev?

Won't Run for SQL Server 2016 SP1 CU15 GDR

Version of the script

@Version = '1', @version_date = '20210423';

What is the current behavior?

Won't run on SQL Server 2016 build 13.0.4604.0. It reports:

Msg 50000, Level 11, State 1, Procedure sp_QuickieStore, Line 4312 [Batch Start Line 3]
error while (null)
offending query:
(null)
Msg 50000, Level 11, State 1, Procedure sp_QuickieStore, Line 83 [Batch Start Line 3]
This procedure only runs on supported versions of SQL Server

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?

13.0.4604.0
4505221 Security update for SQL Server 2016 SP1 CU15 GDR: July 9, 2019 CVE-2019-1068

I don't know about previous versions of the procedure.

Thanks!

Possible enhancement to dbo.get_numbers and dbo.get_letters

Hi, These are great functions and work very well and quickly as you say.

Having followed some of your suggested background material and landed on Jeff Moden's article on string splitting, I notice that he uses an inline CTE instead of a Tally Table and so I experimented with your code and a piece I borrowed from his and came up with the versions in the files in the attached zip file
Get_Numbers and Get_Letters Amended.zip
.
I would be interested to know whether you feel there is any added value or is it just an over complication?

Regards,

Patrick Holmes

Suggestion to improve XML shredding performance

From what I can tell, #human_events_xml ends up with 1 row with entire XML. If possible it is often a good thing to pre-shred that to smaler parts. Here it looks like you could change #human_events_xml to have 1 row for each event instead and do this change without changing any other code and all will hopefully be fast and sweet.
For me with a session capturing about 50 queries the part that shreds the XML went from 4 seconds to 0.8 seconds.
Would be really interesting to see if you see the same. Perhaps you have other problem areas to test?

Sugested code change filling #human_events_xml

--Dump whatever we got into a temp table
declare @X xml;

select @X = convert(xml, t.target_data)
from sys.dm_xe_session_targets as T
  join sys.dm_xe_sessions as s
    on S.address = T.event_session_address
where S.name = @session_name 
  and T.target_name = N'ring_buffer' 
option (recompile);

select E.X.query('.') as human_events_xml
into #human_events_xml
from @X.nodes('/RingBufferTarget/event') as E(X)
option (recompile);

sp_HumanEvents raiserror/throw behavior

Which script is your question about?

Script Name: sp_HumanEvents

Script version

1.5

Is your question about how they work, or the results?
Why it does what it does

Okay, what's your question?

The catch block has a RAISERROR on line 3015 just before a THROW. What is the purpose of the RAISERROR in that place, to tell if there was any error in the ROLLBACK at 2986? If yes, that ROLLBACK should be in a TRY block, if not, line 3015 returns exactly the same information as the THROW, so line 3016 is either wrong or redundant.

This code shows the behavior: the ERROR_NUMBER() and other functions capture the first error in the TRY block, not the second one.

BEGIN TRY
RAISERROR(N'first error', 16, 1) WITH NOWAIT;
END TRY
BEGIN CATCH
DECLARE @msg NVARCHAR(2048) = N'';
RAISERROR(N'second error', 16, 1) WITH NOWAIT;
SELECT @msg += N'Error number '
+ RTRIM(ERROR_NUMBER())
+ N' with severity '
+ RTRIM(ERROR_SEVERITY())
+ N' and a state of '
+ RTRIM(ERROR_STATE())
+ N' in procedure '
+ COALESCE(ERROR_PROCEDURE(),'')
+ N' on line '
+ RTRIM(ERROR_LINE())
+ NCHAR(10)
+ ERROR_MESSAGE();
RAISERROR(@msg, 16, 1) WITH NOWAIT;
THROW;
END CATCH

HumanEvents_Blocking fails in query

@Version = '1.5', @version_date = '20200501';

Running
SELECT TOP 1000 * FROM dbo.HumanEvents_Blocking;

Ouput:
Msg 530, Level 16, State 1, Line 2
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.

Running the following works fine:
select top 1000 * from dbo.[eeper_HumanEvents_blocking

SQL Server 2019 Development CU4

sp_HumanEvents - Azure SQL Database DMV Error

Version of the script
SELECT @Version = '1.5', @version_date = '20200501';

What is the current behavior?
Several errors when running sp_HumanEvents in Azure SQL Database

If the current behavior is a bug, please provide the steps to reproduce.
EXEC [dbo].[sp_HumanEvents] @event_type = 'query',
@query_duration_ms = 1000,
@database_name = 'MyDB',
@keep_alive = 1;

EXEC sp_HumanEvents @output_database_name = N'MyDB', @output_schema_name = N'log';

/*
The first statement works fine and the event starts. However the second errors due to numerous references to sys.server_event_sessions, which I believe should be sys.database_event_sessions for Azure DB. There are also invalid references to dm_xe_database_session_targets and dm_xe_database_session.

Msg 208, Level 16, State 1, Procedure sp_HumanEvents, Line 2140 [Batch Start Line 6]
Invalid object name 'sys.server_event_sessions'.
*/
What is the expected behavior?
It appears that the @Azure parameter needs checking in these additional places and alternate statements
written. I quickly modified all references in my version

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
Azure SQL Database

Azure DB/MI Detection

SELECT @Version = '1.5', @version_date = '20200501';

Current behavior - when running on a SQL Managed Instance, it properly detects that I'm in Azure, however, two issues immediately arise:

1 - The start commands for the event session refer to SERVER and not DATABASE as the path for Azure SQL DB should be.

2 - When in an MI (as I am), I have access to SERVER EVENTS. When I run a version where I just force the @Azure bit to be 0, it runs perfectly fine.

So:

  • the @Start_SQL needs to be fixed so that if the event is created for database, it starts it in database. other locations may be affected.

  • Is there a way to detect if running on a Managed Instance rather than Azure SQL? If so, should treat as if traditional.

Lovely product, looking forward to adding this to my arsenal.

Thanks,
Jonathan

Make it easier to filter wait stats

Should add some keywords here, like:

  • CPU
  • Memory
  • Disk
  • Lock
  • Poison

To grab related waits from each category, rather than making people list them out.

There are too many executions

Version of the script
SELECT @Version = '1.5', @version_date = '20200501';

What is the current behavior?
With event_type query the number of executions is counted twice.

If the current behavior is a bug, please provide the steps to reproduce.
Start sp_HumanEvents with @event_type = 'query', @gimme_danger = 1
Quickly jump to another tab and run a query three times, no more no less. Three shall be the number thou shalt execute, and the number of the executing shall be three.

What is the expected behavior?
The number of executions reported by sp_HumanEvents should be three.

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
SQL Server 2019

sp_HumanEvents - Output Query

sp_HumanEvents

Current version (updated yesterday)

Hi Erik

Great shout out from Brent today on these scripts

"Live Coding T-SQL with Microsoft SQL Server" on Twitch

I'm trying to figure out where the difference is

Same version of sp_HumanEvents and StackOverflow

I get a different output to Brent (story of my life....!!)

See screen shots below

Any idea why they would be different -apologies if this is blindingly obvious

Cheers Erik

Love your stuff

Human_Events_Brent

Human_Events_Me

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.