{"id":561,"date":"2010-12-03T22:15:00","date_gmt":"2010-12-03T22:15:00","guid":{"rendered":"\/blogs\/jonathan\/post\/An-XEvent-a-Day-(3-of-31)-Managing-Event-Sessions.aspx"},"modified":"2017-04-13T12:17:41","modified_gmt":"2017-04-13T16:17:41","slug":"managing-extended-events-sessions","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/","title":{"rendered":"Extended Events Series (3 of 31) &#8211; Managing Event Sessions"},"content":{"rendered":"<p>\nYesterdays post, <a href=\"https:\/\/www.sqlskills.com\/blogs\/jonathan\/an-xevent-a-day-2-of-31-querying-the-extended-events-metadata\/\" target=\"_blank\">Querying the Extended Events Metadata<\/a>, showed how to discover the objects available for use in Extended Events.&nbsp; In today&#39;s post, we&rsquo;ll take a look at the DDL Commands that are used to create and manage Event Sessions based on the objects available in the system.&nbsp; Like other objects inside of SQL Server, there are three DDL commands that are used with Extended Events; <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/statements\/create-event-session-transact-sql\" target=\"_blank\">CREATE EVENT SESSION<\/a>, <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/statements\/alter-event-session-transact-sql\" target=\"_blank\">ALTER EVENT SESSION<\/a>, and <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/statements\/drop-event-session-transact-sql\" target=\"_blank\">DROP EVENT SESSION<\/a>.&nbsp; The command names are self explanatory and their purposes should be clear to most SQL Server DBA&rsquo;s.&nbsp; The books online covers the syntax in detail so I won&rsquo;t rehash all of that in this post, but will instead provide examples for each that cover specific areas of the commands.\n<\/p>\n<h1>CREATE EVENT SESSION<\/h1>\n<p>\nCreating an Event session adds the session definition to the Extended Events Engine making it available for event collection.&nbsp; Creating an Event Session requires that at least one event be added to the session, but an Event Session does not need to have a target added to it to be created.&nbsp; An individual event or target can only&nbsp; be used once in an Event Session, and the complex predicates that are possible in Extended Events mitigates the need to have the same event multiple times in a session.&nbsp; Session options are optional and if left unspecified the defaults documented in the books online will be used for the Event Session.&nbsp; An example Event Session that shows all of the options used is:\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"blue\">CREATE <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites] <\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\t<\/font><font color=\"blue\">ADD <\/font><font color=\"black\">EVENT sqlserver.file_write_completed<\/font><font color=\"gray\">(\r\n\t&nbsp;&nbsp; <\/font><font color=\"blue\">SET <\/font><font color=\"black\">collect_path <\/font><font color=\"blue\">= <\/font><font color=\"black\">1\r\n\t&nbsp;&nbsp; ACTION <\/font><font color=\"gray\">(<\/font><font color=\"black\">sqlserver.sql_text<\/font><font color=\"gray\">)\r\n\t&nbsp;&nbsp; <\/font><font color=\"blue\">WHERE <\/font><font color=\"black\">database_id <\/font><font color=\"blue\">= <\/font><font color=\"black\">2<\/font><font color=\"gray\">),\r\n\t<\/font><font color=\"blue\">ADD <\/font><font color=\"black\">EVENT sqlserver.file_written<\/font><font color=\"gray\">(\r\n\t&nbsp;&nbsp; <\/font><font color=\"blue\">WHERE <\/font><font color=\"black\">database_id <\/font><font color=\"blue\">= <\/font><font color=\"black\">2<\/font><font color=\"gray\">)\r\n\t<\/font><font color=\"blue\">ADD <\/font><font color=\"black\">TARGET package0.ring_buffer,<\/font><font color=\"gray\">\r\n\t<\/font><font color=\"blue\">ADD <\/font><font color=\"black\">TARGET package0.asynchronous_bucketizer<\/font><font color=\"gray\">(\r\n\t&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"blue\">SET <\/font><font color=\"black\">filtering_event_name<\/font><font color=\"blue\">=<\/font><font color=\"red\">&#39;sqlserver.file_write_completed&#39;<\/font><font color=\"gray\">, <\/font><font color=\"black\">source_type<\/font><font color=\"blue\">=<\/font><font color=\"black\">0<\/font><font color=\"gray\">, <\/font><font color=\"black\">source<\/font><font color=\"blue\">=<\/font><font color=\"red\">&#39;file_id&#39;<\/font><font color=\"gray\">)\r\n\t<\/font><font color=\"blue\">WITH <\/font><font color=\"gray\">(<\/font><font color=\"black\">MAX_MEMORY<\/font><font color=\"blue\">=<\/font><font color=\"black\">4096 KB<\/font><font color=\"gray\">,\r\n\t&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"black\">EVENT_RETENTION_MODE<\/font><font color=\"blue\">=<\/font><font color=\"black\">ALLOW_SINGLE_EVENT_LOSS<\/font><font color=\"gray\">,\r\n\t&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"black\">MAX_DISPATCH_LATENCY<\/font><font color=\"blue\">=<\/font><font color=\"black\">30 SECONDS<\/font><font color=\"gray\">,\r\n\t&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"black\">MAX_EVENT_SIZE<\/font><font color=\"blue\">=<\/font><font color=\"black\">0 KB<\/font><font color=\"gray\">,\r\n\t&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"black\">MEMORY_PARTITION_MODE<\/font><font color=\"blue\">=NONE<\/font><font color=\"gray\">,\r\n\t&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"black\">TRACK_CAUSALITY<\/font><font color=\"blue\">=OFF<\/font><font color=\"gray\">,\r\n\t&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"black\">STARTUP_STATE<\/font><font color=\"blue\">=OFF<\/font><font color=\"gray\">)\r\n\t<\/font><font color=\"black\">GO<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<p>\nThis event session captures the sqlserver.file_write_completed event and specifies that the event should collect the path to the file, which is a customizable column on the event, to execute the sql_text action to collect the sql_text that caused the event to fire, if it is available, and to only fire the event for database_id = 2, which happens to be tempdb.&nbsp; Notice that in the Event definition, only the ACTION specification is defined inside of a set of parenthesis, which is defined the the BOL as required in the &lt;event_definition&gt; specification:\n<\/p>\n<blockquote>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\" width=\"796\" bgcolor=\"#d3d3d3\">\n<tbody>\n<tr>\n<td width=\"794\" valign=\"top\">\n<p>\n\t\t\t\t&lt;event_definition&gt;::= <br \/>\n\t\t\t\t{ <br \/>\n\t\t\t\t&nbsp;&nbsp;&nbsp; ADD EVENT [event_module_guid].event_package_name.event_name <br \/>\n\t\t\t\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ ( { <br \/>\n\t\t\t\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ SET { event_customizable_attribute = &lt;value&gt; [ ,&#8230;n] } ] <br \/>\n\t\t\t\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ ACTION ( { [event_module_guid].event_package_name.action_name [ ,&#8230;n] } ) ] <br \/>\n\t\t\t\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ WHERE &lt;predicate_expression&gt; ] <br \/>\n\t\t\t\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } ) ] <br \/>\n\t\t\t\t}\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/blockquote>\n<p>\nModifications to a specific event are enclosed in a set of parenthesis, and then only the Action is constrained to provide a set of parenthesis in its definition.&nbsp; However, the &lt;predicate_expression&gt; can optionally include parenthetical notation to group predicate sets similar to what is possible in the TSQL WHERE clause.&nbsp; The session also captures the file_written event when the database_id = 2.\n<\/p>\n<p>\nThe session utilizes two targets, not because it necessarily provides additional meaning to the event consumption, but because I needed a valid example that shows how multiple targets are defined on a single event session at creation.&nbsp; The ring_buffer target is defined and will capture the raw event data using the default configuration options for the target, and the asynchronous_bucketizer target will count the occurrences of the sqlserver.file_write_completed events based on their specific source data element (file_id), allowing for easy identification of the files that are being written to the most frequently.\n<\/p>\n<p>\nThe session options provided in the above script correspond to the default session options that are defined if an Event Session is created without specifying the WITH() clause at all.&nbsp; The maximum buffer space allocated for the Session is 4MB, and the session allows single events to be lost if the events are generated faster than they can be dispatched to the targets, the maximum amount of time that an event can be in the buffers before being dispatched to the targets is 30 seconds, the maximum event size is not set, no partitioning of buffers is defined, causality tracking is not turned on, and the session will not start automatically when the SQL Server service starts.\n<\/p>\n<h1>ALTER EVENT SESSION<\/h1>\n<p>\nCreating an Event Session does nothing more than that in Extended Events; it simply catalogs the Event Session definition inside of the Extended Events Engine, if it passes the syntax check, and makes the Event Session available for use.&nbsp; To actually begin Event collection, the Event Session must be ALTERed to start the Event Collection.&nbsp; This is accomplished with the ALTER EVENT SESSION DDL command.\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"green\">-- ALTER the Event Session to Start it\r\n\t<\/font><font color=\"blue\">ALTER <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites]\r\n\t<\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\tSTATE<\/font><font color=\"blue\">=<\/font><font color=\"black\">START\r\n\tGO\r\n\t<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<p>\nOnce a Event Session is set to STATE=START, it becomes active inside of the Extended Events Engine and begins collecting Events that have been defined in the Session Definition.&nbsp; An active Event Session can be modified to ADD EVENTs, DROP EVENTs, ADD TARGETs, and DROP TARGETs while the Event Session remains active inside the Extended Events Engine.&nbsp; When an Event Session only has in memory targets defined on it, dropping all of the defined Events from the Event Session stops the Event Session from capturing further Events to allow for captured Event analysis while preserving the captured Event information for analysis.&nbsp; If we run a relevant workload against the tempdb database based on the Event Session created above this can be demonstrated.\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"blue\">USE <\/font><font color=\"black\">[tempdb]\r\n\tGO\r\n\t<\/font><font color=\"blue\">IF <\/font><font color=\"magenta\">OBJECT_ID<\/font><font color=\"gray\">(<\/font><font color=\"red\">&#39;Test&#39;<\/font><font color=\"gray\">) <\/font><font color=\"blue\">IS <\/font><font color=\"gray\">NOT NULL\r\n\t&nbsp;&nbsp; <\/font><font color=\"blue\">DROP TABLE <\/font><font color=\"black\">Test\r\n\t<\/font><font color=\"blue\">CREATE TABLE <\/font><font color=\"black\">Test <\/font><font color=\"gray\">(<\/font><font color=\"black\">rowid <\/font><font color=\"blue\">INT <\/font><font color=\"#434343\">IDENTITY <\/font><font color=\"blue\">PRIMARY KEY<\/font><font color=\"gray\">, <\/font><font color=\"black\">exampledata <\/font><font color=\"blue\">VARCHAR<\/font><font color=\"gray\">(<\/font><font color=\"black\">4000<\/font><font color=\"gray\">))\r\n\t<\/font><font color=\"black\">GO\r\n\t<\/font><font color=\"blue\">INSERT INTO <\/font><font color=\"black\">Test<\/font><font color=\"gray\">(<\/font><font color=\"black\">exampledata<\/font><font color=\"gray\">) <\/font><font color=\"blue\">VALUES <\/font><font color=\"gray\">(<\/font><font color=\"magenta\">REPLICATE<\/font><font color=\"gray\">(<\/font><font color=\"red\">&#39;abcd&#39;<\/font><font color=\"gray\">, <\/font><font color=\"black\">1000<\/font><font color=\"gray\">))\r\n\t<\/font><font color=\"black\">GO 100\r\n\t<\/font><font color=\"blue\">ALTER <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites]\r\n\t<\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\t<\/font><font color=\"blue\">DROP <\/font><font color=\"black\">EVENT sqlserver.file_write_completed<\/font><font color=\"gray\">,\r\n\t<\/font><font color=\"blue\">DROP <\/font><font color=\"black\">EVENT sqlserver.file_written\r\n\tGO\r\n\t<\/font><font color=\"blue\">SELECT <\/font><font color=\"magenta\">CAST<\/font><font color=\"gray\">(<\/font><font color=\"black\">target_data <\/font><font color=\"blue\">AS XML<\/font><font color=\"gray\">) \r\n\t<\/font><font color=\"blue\">FROM <\/font><font color=\"black\">sys.dm_xe_session_targets st\r\n\t<\/font><font color=\"blue\">JOIN <\/font><font color=\"black\">sys.dm_xe_sessions s <\/font><font color=\"blue\">ON <\/font><font color=\"black\">st.event_session_address <\/font><font color=\"blue\">= <\/font><font color=\"black\">s.address\r\n\t<\/font><font color=\"blue\">WHERE <\/font><font color=\"black\">s.name <\/font><font color=\"blue\">= <\/font><font color=\"red\">&#39;TrackTempdbFileWrites&#39;\r\n\t<\/font><font color=\"black\">GO<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<p>\nAfter dropping the events from the session, no further events are captured by the Event Session, which can be shown by running the example workload again and re-querying the targets.\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"blue\">USE <\/font><font color=\"black\">[tempdb]\r\n\tGO\r\n\t<\/font><font color=\"blue\">IF <\/font><font color=\"magenta\">OBJECT_ID<\/font><font color=\"gray\">(<\/font><font color=\"red\">&#39;Test&#39;<\/font><font color=\"gray\">) <\/font><font color=\"blue\">IS <\/font><font color=\"gray\">NOT NULL\r\n\t&nbsp;&nbsp; <\/font><font color=\"blue\">DROP TABLE <\/font><font color=\"black\">Test\r\n\t<\/font><font color=\"blue\">CREATE TABLE <\/font><font color=\"black\">Test <\/font><font color=\"gray\">(<\/font><font color=\"black\">rowid <\/font><font color=\"blue\">INT <\/font><font color=\"#434343\">IDENTITY <\/font><font color=\"blue\">PRIMARY KEY<\/font><font color=\"gray\">, <\/font><font color=\"black\">exampledata <\/font><font color=\"blue\">VARCHAR<\/font><font color=\"gray\">(<\/font><font color=\"black\">4000<\/font><font color=\"gray\">))\r\n\t<\/font><font color=\"black\">GO\r\n\t<\/font><font color=\"blue\">INSERT INTO <\/font><font color=\"black\">Test<\/font><font color=\"gray\">(<\/font><font color=\"black\">exampledata<\/font><font color=\"gray\">) <\/font><font color=\"blue\">VALUES <\/font><font color=\"gray\">(<\/font><font color=\"magenta\">REPLICATE<\/font><font color=\"gray\">(<\/font><font color=\"red\">&#39;abcd&#39;<\/font><font color=\"gray\">, <\/font><font color=\"black\">1000<\/font><font color=\"gray\">))\r\n\t<\/font><font color=\"black\">GO 100\r\n\t<\/font><font color=\"blue\">SELECT <\/font><font color=\"magenta\">CAST<\/font><font color=\"gray\">(<\/font><font color=\"black\">target_data <\/font><font color=\"blue\">AS XML<\/font><font color=\"gray\">) \r\n\t<\/font><font color=\"blue\">FROM <\/font><font color=\"black\">sys.dm_xe_session_targets st\r\n\t<\/font><font color=\"blue\">JOIN <\/font><font color=\"black\">sys.dm_xe_sessions s <\/font><font color=\"blue\">ON <\/font><font color=\"black\">st.event_session_address <\/font><font color=\"blue\">= <\/font><font color=\"black\">s.address\r\n\t<\/font><font color=\"blue\">WHERE <\/font><font color=\"black\">s.name <\/font><font color=\"blue\">= <\/font><font color=\"red\">&#39;TrackTempdbFileWrites&#39;\r\n\t<\/font><font color=\"black\">GO\r\n\t<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<p>\nWhile dropping events from a session that utilizes in memory targets allows for captured data evaluation, ALTERing the Session to change the event definition can be useful to react to the event information that has been captured, to provide more specific predicates, minimizing the number of events captured. To change the definition of a specific event already defined on an Event Session requires that the Event Session be ALTERed with a DROP EVENT definition for the specific Event and then that the Event Session be ALTERed with an ADD EVENT clause to add the Event back to the Event Session with the new Predicate Definition. For the purposes of the demo presented in this blog post, since we have already dropped the sqlserver.file_write_completed event, all we have to do is issue an ALTER EVENT SESSION command with an ADD EVENT specification that adds the new Event specification back to the Event Session.\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"green\">-- Add the sqlserver.file_write_completed back with new predicates\r\n\t<\/font><font color=\"blue\">ALTER <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites]\r\n\t<\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\t<\/font><font color=\"blue\">ADD <\/font><font color=\"black\">EVENT sqlserver.file_write_completed<\/font><font color=\"gray\">(\r\n\t&nbsp;&nbsp; <\/font><font color=\"blue\">SET <\/font><font color=\"black\">collect_path <\/font><font color=\"blue\">= <\/font><font color=\"black\">1\r\n\t&nbsp;&nbsp; ACTION <\/font><font color=\"gray\">(<\/font><font color=\"black\">sqlserver.sql_text<\/font><font color=\"gray\">)\r\n\t&nbsp;&nbsp; <\/font><font color=\"blue\">WHERE <\/font><font color=\"black\">database_id <\/font><font color=\"blue\">= <\/font><font color=\"black\">2 <\/font><font color=\"gray\">AND <\/font><font color=\"magenta\">FILE_ID <\/font><font color=\"blue\">= <\/font><font color=\"black\">1<\/font><font color=\"gray\">)\r\n\t<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<p>\nRerunning the workload against the new event session will show that the Event Session now only captures the sqlserver.file_write_completed Events based on the new criteria.\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"blue\">USE <\/font><font color=\"black\">[tempdb]\r\n\tGO\r\n\t<\/font><font color=\"blue\">IF <\/font><font color=\"magenta\">OBJECT_ID<\/font><font color=\"gray\">(<\/font><font color=\"red\">&#39;Test&#39;<\/font><font color=\"gray\">) <\/font><font color=\"blue\">IS <\/font><font color=\"gray\">NOT NULL\r\n\t&nbsp;&nbsp; <\/font><font color=\"blue\">DROP TABLE <\/font><font color=\"black\">Test\r\n\t<\/font><font color=\"blue\">CREATE TABLE <\/font><font color=\"black\">Test <\/font><font color=\"gray\">(<\/font><font color=\"black\">rowid <\/font><font color=\"blue\">INT <\/font><font color=\"#434343\">IDENTITY <\/font><font color=\"blue\">PRIMARY KEY<\/font><font color=\"gray\">, <\/font><font color=\"black\">exampledata <\/font><font color=\"blue\">VARCHAR<\/font><font color=\"gray\">(<\/font><font color=\"black\">4000<\/font><font color=\"gray\">))\r\n\t<\/font><font color=\"black\">GO\r\n\t<\/font><font color=\"blue\">INSERT INTO <\/font><font color=\"black\">Test<\/font><font color=\"gray\">(<\/font><font color=\"black\">exampledata<\/font><font color=\"gray\">) <\/font><font color=\"blue\">VALUES <\/font><font color=\"gray\">(<\/font><font color=\"magenta\">REPLICATE<\/font><font color=\"gray\">(<\/font><font color=\"red\">&#39;abcd&#39;<\/font><font color=\"gray\">, <\/font><font color=\"black\">1000<\/font><font color=\"gray\">))\r\n\t<\/font><font color=\"black\">GO 100\r\n\t<\/font><font color=\"blue\">SELECT <\/font><font color=\"magenta\">CAST<\/font><font color=\"gray\">(<\/font><font color=\"black\">target_data <\/font><font color=\"blue\">AS XML<\/font><font color=\"gray\">) \r\n\t<\/font><font color=\"blue\">FROM <\/font><font color=\"black\">sys.dm_xe_session_targets st\r\n\t<\/font><font color=\"blue\">JOIN <\/font><font color=\"black\">sys.dm_xe_sessions s <\/font><font color=\"blue\">ON <\/font><font color=\"black\">st.event_session_address <\/font><font color=\"blue\">= <\/font><font color=\"black\">s.address\r\n\t<\/font><font color=\"blue\">WHERE <\/font><font color=\"black\">s.name <\/font><font color=\"blue\">= <\/font><font color=\"red\">&#39;TrackTempdbFileWrites&#39;\r\n\t<\/font><font color=\"black\">GO<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<p>\nIf the Event Session definition proves to be to much for the ring_buffer target to maintain the data in a useable fashion, it may be preferable to instead capture the events to the asynchronous_file_target, which isn&#39;t constrained by the memory configuration of the ring_buffer target. At add the asynchronous_file_target, an ALTER EVENT SESSION statement can be used with the ADD TARGET command to add the new target to the Event Session, and a subsequent ALTER EVENT SESSION statement can be used with the DROP TARGET command to remove the ring_buffer target once its captured data has been consumed from the ring_buffer target.\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"green\">-- Add a file target\r\n\t<\/font><font color=\"blue\">ALTER <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites]\r\n\t<\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\t<\/font><font color=\"blue\">ADD <\/font><font color=\"black\">TARGET package0.asynchronous_file_target\r\n\t<\/font><font color=\"gray\">( <\/font><font color=\"blue\">SET <\/font><font color=\"black\">filename <\/font><font color=\"blue\">= <\/font><font color=\"red\">&#39;C:\\SQLBlog\\TrackTempdbFileWrites.xel&#39;<\/font><font color=\"gray\">,\r\n\t&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"black\">metadatafile <\/font><font color=\"blue\">= <\/font><font color=\"red\">&#39;C:\\SQLBlog\\TrackTempdbFileWrites.mta&#39;<\/font><font color=\"gray\">)\r\n\t<\/font><font color=\"black\">GO\r\n\t<\/font><font color=\"green\">-- Drop a ring_buffer target&nbsp;&nbsp; \r\n\t<\/font><font color=\"blue\">ALTER <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites]\r\n\t<\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\t<\/font><font color=\"blue\">DROP <\/font><font color=\"black\">TARGET package0.ring_buffer\r\n\tGO\r\n\t<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<p>\nThe only part of an Event Session that can not be modified without restarting the Event Session is the Session Options. To change a Session Option, for example the EVENT_RETENTION_MODE from ALLOW_SINGLE_EVENT_LOSS to NO_EVENT_LOSS, requires that the Event Session first be stopped and then restarted. An attempt to change a session level option for an active Event Session results in the following Error:\n<\/p>\n<blockquote><p>\n\t<em><font color=\"#ff0000\">Msg 25707, Level 16, State 1, Line 2 Event session option &quot;event_retention_mode&quot; cannot be changed while the session is running. Stop the event session before changing this session option<\/font>.<\/em>\n<\/p><\/blockquote>\n<p>\nTo change an Event Session level option, requires that the Event Session first be stopped, the option be changed with ALTER EVENT SESSION, and then the Event Session be started again.\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"green\">-- Stop the Event Seession first\r\n\t<\/font><font color=\"blue\">ALTER <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites]\r\n\t<\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\tSTATE<\/font><font color=\"blue\">=<\/font><font color=\"black\">STOP\r\n\tGO\r\n\t<\/font><font color=\"green\">-- Change Event Retention Mode\r\n\t<\/font><font color=\"blue\">ALTER <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites]\r\n\t<\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\t<\/font><font color=\"blue\">WITH <\/font><font color=\"gray\">(<\/font><font color=\"black\">EVENT_RETENTION_MODE <\/font><font color=\"blue\">= <\/font><font color=\"black\">NO_EVENT_LOSS<\/font><font color=\"gray\">)\r\n\t<\/font><font color=\"black\">GO\r\n\t<\/font><font color=\"green\">-- Start the Event Seession after the change\r\n\t<\/font><font color=\"blue\">ALTER <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites]\r\n\t<\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER\r\n\tSTATE<\/font><font color=\"blue\">=<\/font><font color=\"black\">START\r\n\tGO<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<h1>DROP EVENT SESSION<\/h1>\n<p>\nTo DROP a Event Session from the Extended Events Engine to prevents its future use, the DROP EVENT SESSION DDL command can be used. The DROP command can be used against an Active Event Session as well as against a Sessions that are simply cataloged in the Engine and not active.\n<\/p>\n<blockquote>\n<pre style=\"font-size: 12px\">\r\n\t<font color=\"blue\">DROP <\/font><font color=\"black\">EVENT SESSION [TrackTempdbFileWrites] <\/font><font color=\"blue\">ON <\/font><font color=\"black\">SERVER<\/font>\r\n\t<\/pre>\n<\/blockquote>\n<h1>What&rsquo;s next?<\/h1>\n<p>\nNow that we have how to manage Event Session in Extended Events, in the next post we&rsquo;ll look at how to find information about the Event Sessions that have been defined in a SQL Server Instance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterdays post, Querying the Extended Events Metadata, showed how to discover the objects available for use in Extended Events.&nbsp; In today&#39;s post, we&rsquo;ll take a look at the DDL Commands that are used to create and manage Event Sessions based on the objects available in the system.&nbsp; Like other objects inside of SQL Server, there [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,38,45],"tags":[],"class_list":["post-561","post","type-post","status-publish","format-standard","hentry","category-extended-events","category-sql-server-2008","category-xevent-a-day-series"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server Extended Events Managing Event Sessions - Jonathan Kehayias<\/title>\n<meta name=\"description\" content=\"Demonstrates how to managed Extended Events sessions in SQL Server 2008 and onwards using DDL commands in Transact-SQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server Extended Events Managing Event Sessions - Jonathan Kehayias\" \/>\n<meta property=\"og:description\" content=\"Demonstrates how to managed Extended Events sessions in SQL Server 2008 and onwards using DDL commands in Transact-SQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/\" \/>\n<meta property=\"og:site_name\" content=\"Jonathan Kehayias\" \/>\n<meta property=\"article:published_time\" content=\"2010-12-03T22:15:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-04-13T16:17:41+00:00\" \/>\n<meta name=\"author\" content=\"Jonathan Kehayias\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jonathan Kehayias\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/\"},\"author\":{\"name\":\"Jonathan Kehayias\",\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/#\\\/schema\\\/person\\\/01c10d94f3648654ef706d5e6305f69c\"},\"headline\":\"Extended Events Series (3 of 31) &#8211; Managing Event Sessions\",\"datePublished\":\"2010-12-03T22:15:00+00:00\",\"dateModified\":\"2017-04-13T16:17:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/\"},\"wordCount\":1504,\"commentCount\":2,\"articleSection\":[\"Extended Events\",\"SQL Server 2008\",\"XEvent a Day Series\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/\",\"url\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/\",\"name\":\"SQL Server Extended Events Managing Event Sessions - Jonathan Kehayias\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/#website\"},\"datePublished\":\"2010-12-03T22:15:00+00:00\",\"dateModified\":\"2017-04-13T16:17:41+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/#\\\/schema\\\/person\\\/01c10d94f3648654ef706d5e6305f69c\"},\"description\":\"Demonstrates how to managed Extended Events sessions in SQL Server 2008 and onwards using DDL commands in Transact-SQL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/managing-extended-events-sessions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Extended Events\",\"item\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/category\\\/extended-events\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Extended Events Series (3 of 31) &#8211; Managing Event Sessions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/\",\"name\":\"Jonathan Kehayias - The Rambling DBA\",\"description\":\"The Rambling DBA\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.sqlskills.com\\\/blogs\\\/jonathan\\\/#\\\/schema\\\/person\\\/01c10d94f3648654ef706d5e6305f69c\",\"name\":\"Jonathan Kehayias\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/86630e27f5deecc5c393ea57fc7c3b6a068949f4fd6b5309f81de5a276f12855?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/86630e27f5deecc5c393ea57fc7c3b6a068949f4fd6b5309f81de5a276f12855?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/86630e27f5deecc5c393ea57fc7c3b6a068949f4fd6b5309f81de5a276f12855?s=96&d=mm&r=g\",\"caption\":\"Jonathan Kehayias\"},\"sameAs\":[\"http:\\\/\\\/3.209.169.194\\\/blogs\\\/jonathan\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server Extended Events Managing Event Sessions - Jonathan Kehayias","description":"Demonstrates how to managed Extended Events sessions in SQL Server 2008 and onwards using DDL commands in Transact-SQL.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server Extended Events Managing Event Sessions - Jonathan Kehayias","og_description":"Demonstrates how to managed Extended Events sessions in SQL Server 2008 and onwards using DDL commands in Transact-SQL.","og_url":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/","og_site_name":"Jonathan Kehayias","article_published_time":"2010-12-03T22:15:00+00:00","article_modified_time":"2017-04-13T16:17:41+00:00","author":"Jonathan Kehayias","twitter_misc":{"Written by":"Jonathan Kehayias","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/#article","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/"},"author":{"name":"Jonathan Kehayias","@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/#\/schema\/person\/01c10d94f3648654ef706d5e6305f69c"},"headline":"Extended Events Series (3 of 31) &#8211; Managing Event Sessions","datePublished":"2010-12-03T22:15:00+00:00","dateModified":"2017-04-13T16:17:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/"},"wordCount":1504,"commentCount":2,"articleSection":["Extended Events","SQL Server 2008","XEvent a Day Series"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/","url":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/","name":"SQL Server Extended Events Managing Event Sessions - Jonathan Kehayias","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/#website"},"datePublished":"2010-12-03T22:15:00+00:00","dateModified":"2017-04-13T16:17:41+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/#\/schema\/person\/01c10d94f3648654ef706d5e6305f69c"},"description":"Demonstrates how to managed Extended Events sessions in SQL Server 2008 and onwards using DDL commands in Transact-SQL.","breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/managing-extended-events-sessions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/"},{"@type":"ListItem","position":2,"name":"Extended Events","item":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/category\/extended-events\/"},{"@type":"ListItem","position":3,"name":"Extended Events Series (3 of 31) &#8211; Managing Event Sessions"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/","name":"Jonathan Kehayias - The Rambling DBA","description":"The Rambling DBA","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/#\/schema\/person\/01c10d94f3648654ef706d5e6305f69c","name":"Jonathan Kehayias","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/86630e27f5deecc5c393ea57fc7c3b6a068949f4fd6b5309f81de5a276f12855?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/86630e27f5deecc5c393ea57fc7c3b6a068949f4fd6b5309f81de5a276f12855?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/86630e27f5deecc5c393ea57fc7c3b6a068949f4fd6b5309f81de5a276f12855?s=96&d=mm&r=g","caption":"Jonathan Kehayias"},"sameAs":["http:\/\/3.209.169.194\/blogs\/jonathan"]}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/posts\/561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/comments?post=561"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/posts\/561\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/media?parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/categories?post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/jonathan\/wp-json\/wp\/v2\/tags?post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}