Last week I published the second post from my short 5 part series in which I demonstrate you 5 different ways to track database schema changes. Thanks to this, you always can easily answer questions about what has changed, who changed it and when. Today’s post describes how to use Extended Events Session for this purpose.
You can find my previous post here:
Extended Event Session
The third option is the Extended Events Session. This functionality has been introduced in SQL Server 2008. It provides very lightweight monitoring and event tracking system that can be very helpful in performance troubleshooting and (in our case) changes monitoring.
Microsoft announced it as a successor of SQL Trace (that has been marked as deprecated in SQL Server 2012). That means SQL Trace can be removed in one of the future versions. However, SQL Trace and SQL Server Profiler are still very popular and widely used, so I really doubt that will happen anytime soon.
In the beginning, Extended Events didn’t gain too much popularity because it was tough to use them. Now, with the help of SSMS, it’s much more comfortable.
To test this solution we have to create an Extended Events Session that captures object modification events. In our case these are:
- object_created
- object_altered
- object_deleted
Creating an Extended Events Session
You can create such session in two ways. Using T-SQL or using a Wizard in SQL Server Management Studio. I find the latter much more comfortable.
Create a Session using Wizard
To create a new Session, in Object Explorer expand Instance -> Management -> Extended Events. Right Click on Sessions and choose New Session Wizard.
On the “Set Session Properties” page provide a name for your session and decide if this session should start at server startup. Then, click the Next button.
On the next page, you can decide whether you want to use a predefined template or not. In our case, there is no template that we can use to track object schema changes. Choose “Do not use template” option and click “Next”.
On the “Select Events To Capture” page, select object_altered, object_created, and object_deleted events.
It should look like this:
On the “Capture Global Fields” page, you can decide what data you want to collect. My recommendation is to select the following ones:
- client_app_name
- client_hostname
- database_id
- database_name
- server_principal_name
- session_id
- sql_text
This gives you an overview of what really happened. You know who performed a change, from which machine, and from which application. Most importantly, you also know what SQL statement was executed. When you set this then click “Next”.
On the “Set Session Event Filters” page, you can add additional filters. That enables you, for example, to capture events just for one database instead of for all databases on your instance. Then click “Next”.
On the “Specify Session Data Storage” page, you need to decide if you want to log data to file(s) (event_file target) or maybe keep them in memory (ring_buffer target). For real-life usage, you should choose files. Here, for the demo purpose, I use ring_buffer.
When the session is already created, you can start it immediately and also can watch live data on the screen as events are captured.
Create a Session using T-SQL
The same session can be created using this script.
CREATE EVENT SESSION [CaptureObjectModifications] ON SERVER ADD EVENT sqlserver.object_altered( ACTION(sqlserver.client_app_name, sqlserver.client_hostname, sqlserver.database_id, sqlserver.database_name, sqlserver.server_principal_name, sqlserver.session_id, sqlserver.sql_text)), ADD EVENT sqlserver.object_created( ACTION(sqlserver.client_app_name, sqlserver.client_hostname, sqlserver.database_id, sqlserver.database_name, sqlserver.server_principal_name, sqlserver.session_id, sqlserver.sql_text)), ADD EVENT sqlserver.object_deleted( ACTION(sqlserver.client_app_name, sqlserver.client_hostname, sqlserver.database_id, sqlserver.database_name, sqlserver.server_principal_name, sqlserver.session_id, sqlserver.sql_text)) ADD TARGET package0.ring_buffer(SET max_events_limit=0,max_memory=102400) GO
Viewing events captured by the Extended Events Session
When the session is already created and started. It captures all object modifications. To see changes live, start the Wach Live Data view in SSMS.
You can right click on the column names to add additional columns to this view.
Now, let’s test it by executing the whole test case from the beginning of the article. Captured events are automatically displayed in the window.
To review historical data from the ring_buffer target, you need to use the T-SQL query. When you log to files you have the possibility to review their content in SSMS with View Target Data option. Use this query to select captured events for our session.
;WITH raw_data(t) AS ( SELECT CONVERT(XML, target_data) FROM sys.dm_xe_sessions AS s INNER JOIN sys.dm_xe_session_targets AS st ON s.[address] = st.event_session_address WHERE s.name = 'CaptureObjectModifications' AND st.target_name = 'ring_buffer' ), xml_data (ed) AS ( SELECT e.query('.') FROM raw_data CROSS APPLY t.nodes('RingBufferTarget/event') AS x(e) ) SELECT * --FROM xml_data; FROM ( SELECT [timestamp] = ed.value('(event/@timestamp)[1]', 'datetime'), [database_id] = ed.value('(event/data[@name="database_id"]/value)[1]', 'int'), [database_name] = ed.value('(event/action[@name="database_name"]/value)[1]', 'nvarchar(128)'), [object_type] = ed.value('(event/data[@name="object_type"]/text)[1]', 'nvarchar(128)'), [object_id] = ed.value('(event/data[@name="object_id"]/value)[1]', 'int'), [object_name] = ed.value('(event/data[@name="object_name"]/value)[1]', 'nvarchar(128)'), [session_id] = ed.value('(event/action[@name="session_id"]/value)[1]', 'int'), [login] = ed.value('(event/action[@name="server_principal_name"]/value)[1]', 'nvarchar(128)'), [client_hostname] = ed.value('(event/action[@name="client_hostname"]/value)[1]', 'nvarchar(128)'), [client_app_name] = ed.value('(event/action[@name="client_app_name"]/value)[1]', 'nvarchar(128)'), [sql_text] = ed.value('(event/action[@name="sql_text"]/value)[1]', 'nvarchar(max)'), [phase] = ed.value('(event/data[@name="ddl_phase"]/text)[1]', 'nvarchar(128)') FROM xml_data ) AS x WHERE phase = 'Commit' ORDER BY [timestamp];
What was captured by Extended Events Session?
In terms of what was captured, the Extended Event Session looks very well. It has a variety of configuration options that allow you to customize the logged details. Viewing the data collected by session, we know what was changed, when it was changed and by whom. We also have a SQL statement that was executed to perform this change.
Data retention
The Extended Events Session has many retention options for both targets. For files, we can specify the maximum files size. For the ring buffer, we can specify maximum events count and memory size. That gives users a lot of flexibility.
Extended Events summary
- Contains a lot of details
- Info WHAT exactly was changed
- Access to the executed SQL statement
- Info WHEN object was changed
- Info by WHO object was changed
- Plenty of retention options
- Possibility to save data to memory or files
- Easy to set up
- Possibility to set additional filters
- Difficult processing of captured events (T-SQL and XQuery)
- No access to the old object definition
In the next part, I will demonstrate you how to use DDL Triggers to capture database schema changes.
-Marek
Share it: