Home
%3CLINGO-SUB%20id%3D%22lingo-sub-745344%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%2398%3A%20Is%20possible%20to%20create%20an%20extended%20event%20on%20the%20server%20in%20Azure%20Managed%20Instance%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-745344%22%20slang%3D%22en-US%22%3E%3CP%3E1)%20The%20first%20point%20that%20we%20need%20is%20to%20create%20the%20credential%2C%20because%20the%20extended%20file%20will%20create%20in%20a%26nbsp%3Bblob%20storage.%3C%2FP%3E%0A%3CPRE%3ECREATE%20CREDENTIAL%20%5Bhttps%3A%2F%2Fmyblogstorage.blob.core.windows.net%2Fmyfolder%5D%0AWITH%20IDENTITY%3D'SHARED%20ACCESS%20SIGNATURE'%2C%0ASECRET%20%3D%20'sv%3D2018-03-28%26amp%3Bss%3Dbfqt%26amp%3Bsrt%3Dsco%26amp%3Bsp%3Drwdlacup%26amp%3Bse%3D2019-07-23T23%3A29%3A33Z%26amp%3Bst%3D2019-07-09T15%3A29%3A33Z%26amp%3Bspr%3Dhttps%26amp%3Bsig%3D...%25%253D'%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2)%20The%20second%20point%20is%20to%20create%20the%20extended%20event%2C%20in%20this%20case%2C%20as%20an%20example%2C%20I'm%20going%20to%20define%20an%20extended%20event%20for%20deadlocks%20for%20all%20databases%20at%20server%20level.%3C%2FP%3E%0A%3CPRE%3ECREATE%20EVENT%20SESSION%20%5BdeadlockJM%5D%20ON%20SERVER%20%0AADD%20EVENT%20sqlserver.lock_deadlock(%0A%20%20%20%20ACTION(sqlserver.sql_text%2Csqlserver.client_app_name%2Csqlserver.client_connection_id%2Csqlserver.client_hostname%2Csqlserver.database_id%2Csqlserver.database_name%2Csqlserver.session_id%2Csqlserver.username))%2C%0AADD%20EVENT%20sqlserver.lock_deadlock_chain(%0A%20%20%20%20ACTION(sqlserver.sql_text%2Csqlserver.client_app_name%2Csqlserver.client_connection_id%2Csqlserver.client_hostname%2Csqlserver.database_id%2Csqlserver.database_name%2Csqlserver.session_id%2Csqlserver.username))%0AADD%20TARGET%20package0.asynchronous_file_target(%0ASET%20filename%3D'https%3A%2F%2Fmyblobstorage.blob.core.windows.net%2Fmyfolder%2Fdeadlock.xel')%0AWITH%20(MAX_MEMORY%3D4096%20KB%2CEVENT_RETENTION_MODE%3DALLOW_SINGLE_EVENT_LOSS%2CMAX_DISPATCH_LATENCY%3D30%20SECONDS%2CMAX_EVENT_SIZE%3D0%20KB%2CMEMORY_PARTITION_MODE%3DNONE%2CTRACK_CAUSALITY%3DON%2CSTARTUP_STATE%3DOFF)%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E3)%20The%20third%20one%2C%20is%20to%20enable%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EALTER%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EEVENT%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3ESESSION%3C%2FFONT%3E%3CFONT%20face%3D%22Consolas%22%20size%3D%222%22%3E%5BdeadlockJM%5D%20%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EON%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3Eserver%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3ESTATE%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E%3D%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3ESTART%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E4)%20Try%20to%20reproduce%20the%20issue%20using%20these%20two%20sessions%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E4.1)%20Create%20the%20tables%20and%20add%20some%20rows.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3ECREATE%20TABLE%20%5Bdbo%5D.%5BPerson%5D(%0A%5BId%5D%20%5Bint%5D%20IDENTITY(1%2C1)%20NOT%20NULL%2C%0A%5BFirstName%5D%20%5Bvarchar%5D(20)%20NOT%20NULL%2C%0A%5BBusinessEntityID%5D%20%5Bint%5D%20NOT%20NULL%2C%0ACONSTRAINT%20%5BPK_Person%5D%20PRIMARY%20KEY%20CLUSTERED%0A(%0A%5BId%5D%20ASC%0A))%0A%0ACREATE%20TABLE%20%5Bdbo%5D.%5BPersonPhone%5D(%0A%5BPhoneNumber%5D%20%5Bvarchar%5D(50)%20NOT%20NULL%2C%0A%5BBusinessEntityID%5D%20%5Bint%5D%20NOT%20NULL%2C%0ACONSTRAINT%20%5BPK_PersonPhone%5D%20PRIMARY%20KEY%20CLUSTERED%0A(%0A%5BPhoneNumber%5D%20ASC%0A))%0A%0Ainsert%20into%20%5BPersonPhone%5D%20values%20(%20'999-555-1212'%2C1)%0Ainsert%20into%20%5BPerson%5D%20values(%20'Chris'%2C1)%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E4.2)%20Open%20the%20SQL%20SERVER%20Management%20Studio%20and%20open%20two%20queries.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E--%20Execute%20this%20query%20using%20a%20query%20window%20for%20Deadlock%20Thread%201%0ABEGIN%20TRANSACTION%0AUPDATE%20%5BPersonPhone%5D%20SET%20PhoneNumber%20%3D%20'999-555-1212'%20WHERE%20%5BBusinessEntityID%5D%20%3D%201%0AWAITFOR%20DELAY%20'00%3A00%3A50'%0AUPDATE%20%5BPerson%5D%20SET%20%5BFirstName%5D%20%3D%20'Chris'%20WHERE%20%5BBusinessEntityID%5D%20%3D%201%0AROLLBACK%20TRANSACTION%0A%3C%2FPRE%3E%0A%3CPRE%3E--%20Very%20quickly%20execute%20this%20query%20using%20a%20query%20window%20for%20Deadlock%20Thread%202%0A--%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0ABEGIN%20TRANSACTION%0AUPDATE%20%5BPerson%5D%20SET%20%5BFirstName%5D%20%3D%20'Chris'%20WHERE%20%5BBusinessEntityID%5D%20%3D%201%0AUPDATE%20%5BPersonPhone%5D%20SET%20PhoneNumber%20%3D%20'999-555-1212'%20WHERE%20%5BBusinessEntityID%5D%20%3D%201%0AWAITFOR%20DELAY%20'00%3A00%3A10'%0AROLLBACK%20TRANSACTION%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E5)%20Once%20the%20deadlock%20has%20been%20generated%2C%20when%20I%20tried%20to%20download%20from%20Azure%20Portal%20I%20found%20that%20the%20process%20took%20too%20much%20time%20and%20I%20was%20not%20able%20to%2C%20just%20only%20running%20the%20following%20TSQL%3A%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EALTER%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EEVENT%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3ESESSION%3C%2FFONT%3E%3CFONT%20face%3D%22Consolas%22%20size%3D%222%22%3E%5BdeadlockJM%5D%20%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EON%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3Eserver%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3ESTATE%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E%3D%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3ESTOP%3C%2FFONT%3EI%20was%20able%20to.%20I%20found%20that%20the%20size%20of%20the%20file%20initialy%20was%201%20Gib%20but%20after%20stopping%20got%20the%20expected%20size%20of%20the%20information%20required%20in%20the%20definition%20of%20this%20extended%20event.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E6)%20Just%20right%20now%2C%20using%20this%20extended%20event%20you%20could%20see%20the%20deadlock.%20Also%2C%20you%20could%20see%20this%20extended%20event%20is%20you%20are%20using%20the%20latest%20version%20of%20SSMS%2018.1%20under%20Management%20option%20of%20the%20instance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnjoy!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-745344%22%20slang%3D%22en-US%22%3E%3CP%3ENowadays%2C%20you%20know%20that%20we%20could%20create%20only%20extended%20event%20at%20database%20level%20in%20Azure%20SQL%20Database.%20But%2C%20with%20this%20new%20model%20Azure%20SQL%20Managed%20Instance%20we%20have%20the%20option%20among%20others%20to%20create%20extended%20events%20in%20Azure%20SQL%20Managed%20Instance.%20This%20is%20my%20lesson%20learned%20doing%20this.%3C%2FP%3E%3C%2FLINGO-TEASER%3E

1) The first point that we need is to create the credential, because the extended file will create in a blob storage.

CREATE CREDENTIAL [https://myblogstorage.blob.core.windows.net/myfolder]
WITH IDENTITY='SHARED ACCESS SIGNATURE',
SECRET = 'sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-07-23T23:29:33Z&st=2019-07-09T15:29:33Z&spr=https&sig=...%%3D'

 

2) The second point is to create the extended event, in this case, as an example, I'm going to define an extended event for deadlocks for all databases at server level.

CREATE EVENT SESSION [deadlockJM] ON SERVER 
ADD EVENT sqlserver.lock_deadlock(
    ACTION(sqlserver.sql_text,sqlserver.client_app_name,sqlserver.client_connection_id,sqlserver.client_hostname,sqlserver.database_id,sqlserver.database_name,sqlserver.session_id,sqlserver.username)),
ADD EVENT sqlserver.lock_deadlock_chain(
    ACTION(sqlserver.sql_text,sqlserver.client_app_name,sqlserver.client_connection_id,sqlserver.client_hostname,sqlserver.database_id,sqlserver.database_name,sqlserver.session_id,sqlserver.username))
ADD TARGET package0.asynchronous_file_target(
SET filename='https://myblobstorage.blob.core.windows.net/myfolder/deadlock.xel')
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=ON,STARTUP_STATE=OFF)

 

3) The third one, is to enable ALTER EVENT SESSION [deadlockJM] ON server STATE = START;

4) Try to reproduce the issue using these two sessions:

  • 4.1) Create the tables and add some rows.
CREATE TABLE [dbo].[Person](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](20) NOT NULL,
[BusinessEntityID] [int] NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
(
[Id] ASC
))

CREATE TABLE [dbo].[PersonPhone](
[PhoneNumber] [varchar](50) NOT NULL,
[BusinessEntityID] [int] NOT NULL,
CONSTRAINT [PK_PersonPhone] PRIMARY KEY CLUSTERED
(
[PhoneNumber] ASC
))

insert into [PersonPhone] values ( '999-555-1212',1)
insert into [Person] values( 'Chris',1)

 

  • 4.2) Open the SQL SERVER Management Studio and open two queries.

 

-- Execute this query using a query window for Deadlock Thread 1
BEGIN TRANSACTION
UPDATE [PersonPhone] SET PhoneNumber = '999-555-1212' WHERE [BusinessEntityID] = 1
WAITFOR DELAY '00:00:50'
UPDATE [Person] SET [FirstName] = 'Chris' WHERE [BusinessEntityID] = 1
ROLLBACK TRANSACTION
-- Very quickly execute this query using a query window for Deadlock Thread 2
-- ====================================
BEGIN TRANSACTION
UPDATE [Person] SET [FirstName] = 'Chris' WHERE [BusinessEntityID] = 1
UPDATE [PersonPhone] SET PhoneNumber = '999-555-1212' WHERE [BusinessEntityID] = 1
WAITFOR DELAY '00:00:10'
ROLLBACK TRANSACTION

 

5) Once the deadlock has been generated, when I tried to download from Azure Portal I found that the process took too much time and I was not able to, just only running the following TSQL:

ALTER EVENT SESSION [deadlockJM] ON server STATE = STOP I was able to. I found that the size of the file initialy was 1 Gib but after stopping got the expected size of the information required in the definition of this extended event.

 

6) Just right now, using this extended event you could see the deadlock. Also, you could see this extended event is you are using the latest version of SSMS 18.1 under Management option of the instance.

 

Enjoy!