<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DeadLock &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/deadlock/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 16 Jan 2018 11:04:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>Greenplum: Script to find blocked and deadlock by other blocking queries</title>
		<link>https://www.dbrnd.com/2018/02/greenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks/</link>
					<comments>https://www.dbrnd.com/2018/02/greenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 02 Feb 2018 16:16:41 +0000</pubDate>
				<category><![CDATA[Greenplum]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[Greenplum Administrator]]></category>
		<category><![CDATA[Greenplum Error]]></category>
		<category><![CDATA[Greenplum Monitoring]]></category>
		<category><![CDATA[Greenplum Performance Tuning]]></category>
		<category><![CDATA[Greenplum Programming]]></category>
		<category><![CDATA[Greenplum Query]]></category>
		<category><![CDATA[Greenplum Tips and Tricks]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[long running queries]]></category>
		<category><![CDATA[PostgreSQL 8.2]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4394</guid>

					<description><![CDATA[In this post, I am sharing a script to find a list of blocked and deadlock queries by other blocking queries of Greenplum Database Server. You can visit below article to know more about, what is a lock, deadlock, block and timeout? Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System? [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F02%2Fgreenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks%2F&amp;linkname=Greenplum%3A%20Script%20to%20find%20blocked%20and%20deadlock%20by%20other%20blocking%20queries" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F02%2Fgreenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks%2F&amp;linkname=Greenplum%3A%20Script%20to%20find%20blocked%20and%20deadlock%20by%20other%20blocking%20queries" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F02%2Fgreenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks%2F&amp;linkname=Greenplum%3A%20Script%20to%20find%20blocked%20and%20deadlock%20by%20other%20blocking%20queries" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F02%2Fgreenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks%2F&amp;linkname=Greenplum%3A%20Script%20to%20find%20blocked%20and%20deadlock%20by%20other%20blocking%20queries" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F02%2Fgreenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks%2F&amp;linkname=Greenplum%3A%20Script%20to%20find%20blocked%20and%20deadlock%20by%20other%20blocking%20queries" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F02%2Fgreenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks%2F&amp;linkname=Greenplum%3A%20Script%20to%20find%20blocked%20and%20deadlock%20by%20other%20blocking%20queries" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F02%2Fgreenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks%2F&#038;title=Greenplum%3A%20Script%20to%20find%20blocked%20and%20deadlock%20by%20other%20blocking%20queries" data-a2a-url="https://www.dbrnd.com/2018/02/greenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks/" data-a2a-title="Greenplum: Script to find blocked and deadlock by other blocking queries"></a></p><p>In this post, I am sharing a script to find a list of blocked and deadlock queries by other blocking queries of Greenplum Database Server. </p>
<p>You can visit below article to know more about, what is a lock, deadlock, block and timeout?</p>
<blockquote class="wp-embedded-content" data-secret="CZy0OcEdgM"><p><a href="https://www.dbrnd.com/2016/04/database-theory-what-is-timeout-lock-block-and-deadlock-in-a-database-system/">Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System?</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/04/database-theory-what-is-timeout-lock-block-and-deadlock-in-a-database-system/embed/#?secret=CZy0OcEdgM" data-secret="CZy0OcEdgM" width="600" height="338" title="&#8220;Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System?&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>If two queries are trying to get the same resource, both are depended on each other and generates block state first and then deadlock.</p>
<blockquote><p>In the below script, you can find a list of blocked queries and blocker queries with a value of time age.<br />
You can find a process_id of blocker query which we can kill for avoiding the deadlock situation.</p></blockquote>
<pre class="crayon-plain-tag">SELECT
	kl.pid as blocking_pid
	,ka.usename as blocking_user
	,ka.current_query as blocking_query
	,bl.pid as blocked_pid
	,a.usename as blocked_user
	,a.current_query as blocked_query
	,to_char(age(now(), a.query_start),'HH24h:MIm:SSs') as age
FROM pg_catalog.pg_locks bl
JOIN pg_catalog.pg_stat_activity a
	ON bl.pid = a.procpid
JOIN pg_catalog.pg_locks kl
	ON bl.locktype = kl.locktype
	and bl.database is not distinct from kl.database
	and bl.relation is not distinct from kl.relation
	and bl.page is not distinct from kl.page
	and bl.tuple is not distinct from kl.tuple
	and bl.transactionid is not distinct from kl.transactionid
	and bl.classid is not distinct from kl.classid
	and bl.objid is not distinct from kl.objid
	and bl.objsubid is not distinct from kl.objsubid
	and bl.pid &lt;&gt; kl.pid
JOIN pg_catalog.pg_stat_activity ka
	ON kl.pid = ka.procpid
WHERE kl.granted and not bl.granted
ORDER BY a.query_start;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2018/02/greenplum-script-to-find-blocked-and-deadlock-by-other-blocking-queries-pg_locks/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Set wait time for Deadlock Detection (deadlock_timeout)</title>
		<link>https://www.dbrnd.com/2017/12/postgresql-set-wait-time-for-deadlock-detection-deadlock_timeout/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 26 Dec 2017 16:16:59 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[deadlock_timeout]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6859</guid>

					<description><![CDATA[In this post, I am sharing an important parameter which we can use for setting a wait time before deadlock occurs in PostgreSQL. The parameter is deadlock_timeout (integer). The default is one second which is very less time. Before any deadlock, there is a blocked transaction situation. In most of the cases, we can avoid [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-set-wait-time-for-deadlock-detection-deadlock_timeout%2F&amp;linkname=PostgreSQL%3A%20Set%20wait%20time%20for%20Deadlock%20Detection%20%28deadlock_timeout%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-set-wait-time-for-deadlock-detection-deadlock_timeout%2F&amp;linkname=PostgreSQL%3A%20Set%20wait%20time%20for%20Deadlock%20Detection%20%28deadlock_timeout%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-set-wait-time-for-deadlock-detection-deadlock_timeout%2F&amp;linkname=PostgreSQL%3A%20Set%20wait%20time%20for%20Deadlock%20Detection%20%28deadlock_timeout%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-set-wait-time-for-deadlock-detection-deadlock_timeout%2F&amp;linkname=PostgreSQL%3A%20Set%20wait%20time%20for%20Deadlock%20Detection%20%28deadlock_timeout%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-set-wait-time-for-deadlock-detection-deadlock_timeout%2F&amp;linkname=PostgreSQL%3A%20Set%20wait%20time%20for%20Deadlock%20Detection%20%28deadlock_timeout%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-set-wait-time-for-deadlock-detection-deadlock_timeout%2F&amp;linkname=PostgreSQL%3A%20Set%20wait%20time%20for%20Deadlock%20Detection%20%28deadlock_timeout%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-set-wait-time-for-deadlock-detection-deadlock_timeout%2F&#038;title=PostgreSQL%3A%20Set%20wait%20time%20for%20Deadlock%20Detection%20%28deadlock_timeout%29" data-a2a-url="https://www.dbrnd.com/2017/12/postgresql-set-wait-time-for-deadlock-detection-deadlock_timeout/" data-a2a-title="PostgreSQL: Set wait time for Deadlock Detection (deadlock_timeout)"></a></p><p>In this post, I am sharing an important parameter which we can use for setting a wait time before deadlock occurs in PostgreSQL. </p>
<p>The parameter is <strong>deadlock_timeout (integer)</strong>. The default is one second which is very less time. </p>
<blockquote><p>Before any deadlock, there is a blocked transaction situation. In most of the cases, we can avoid the deadlock by checking or solving the blocking situation. So in this situation, database engine has to wait before generating the deadlock.</p></blockquote>
<p>Using deadlock_timeout parameter, we can set the wait time for deadlock situation.</p>
<p>Increasing this value reduces the amount of time wasted in needless deadlock checks, but slows down reporting of real deadlock errors.</p>
<p>Change the value of the deadlock_timeout parameter in postgresql.conf file.</p>
<blockquote class="wp-embedded-content" data-secret="2lrVZZar15"><p><a href="https://www.dbrnd.com/2016/09/postgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block/">PostgreSQL: Script to find information about the Locks held by Open Transactions (pg_locks)</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/09/postgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block/embed/#?secret=2lrVZZar15" data-secret="2lrVZZar15" width="600" height="338" title="&#8220;PostgreSQL: Script to find information about the Locks held by Open Transactions (pg_locks)&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL 10: Trace the Status of Transaction (Commit, In Progress, Rollback, Aborted)</title>
		<link>https://www.dbrnd.com/2017/12/postgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 10 Dec 2017 16:16:31 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[COMMIT]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL 10]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[ROLLBACK]]></category>
		<category><![CDATA[transaction]]></category>
		<category><![CDATA[txid_current]]></category>
		<category><![CDATA[txid_status]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6788</guid>

					<description><![CDATA[PostgreSQL 10 introduced many useful features that we can use in day to day work. Now, we can trace the status of the running transaction in PostgreSQL 10. If we have transaction IDs of our sessions and suddenly database crashed or shutdown, we can check the status of those transactions. We can check the status [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current%2F&amp;linkname=PostgreSQL%2010%3A%20Trace%20the%20Status%20of%20Transaction%20%28Commit%2C%20In%20Progress%2C%20Rollback%2C%20Aborted%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current%2F&amp;linkname=PostgreSQL%2010%3A%20Trace%20the%20Status%20of%20Transaction%20%28Commit%2C%20In%20Progress%2C%20Rollback%2C%20Aborted%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current%2F&amp;linkname=PostgreSQL%2010%3A%20Trace%20the%20Status%20of%20Transaction%20%28Commit%2C%20In%20Progress%2C%20Rollback%2C%20Aborted%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current%2F&amp;linkname=PostgreSQL%2010%3A%20Trace%20the%20Status%20of%20Transaction%20%28Commit%2C%20In%20Progress%2C%20Rollback%2C%20Aborted%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current%2F&amp;linkname=PostgreSQL%2010%3A%20Trace%20the%20Status%20of%20Transaction%20%28Commit%2C%20In%20Progress%2C%20Rollback%2C%20Aborted%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current%2F&amp;linkname=PostgreSQL%2010%3A%20Trace%20the%20Status%20of%20Transaction%20%28Commit%2C%20In%20Progress%2C%20Rollback%2C%20Aborted%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current%2F&#038;title=PostgreSQL%2010%3A%20Trace%20the%20Status%20of%20Transaction%20%28Commit%2C%20In%20Progress%2C%20Rollback%2C%20Aborted%29" data-a2a-url="https://www.dbrnd.com/2017/12/postgresql-10-trace-the-status-of-transaction-commit-in-progress-rollback-aborted-txid_status-txid_current/" data-a2a-title="PostgreSQL 10: Trace the Status of Transaction (Commit, In Progress, Rollback, Aborted)"></a></p><p>PostgreSQL 10 introduced many useful features that we can use in day to day work. </p>
<p>Now, we can trace the status of the running transaction in PostgreSQL 10. If we have transaction IDs of our sessions and suddenly database crashed or shutdown, we can check the status of those transactions.</p>
<blockquote><p>We can check the status like Committed, In Progress, Rollback or Aborted. This facility will help a lot for taking a necessary decision on crashed transactions. </p></blockquote>
<p><strong>PostgreSQL 10 introduced two functions:</strong></p>
<blockquote><p>txid_current() &#8211; will return the current transaction id of a session<br />
txid_status() &#8211;  will return the status of transaction</p></blockquote>
<p>Please check the below sample:</p>
<p><strong>Create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_stud
(Rno INT, Name Character Varying);</pre> </p>
<p><strong>Open a transaction, and insert a record:</strong></p>
<pre class="crayon-plain-tag">BEGIN TRANSACTION;

INSERT INTO tbl_stud
VALUES (1,'Anvesh');</pre> </p>
<p><strong>Check the current transaction id:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM txid_current();

txid_current
------------
    587</pre> </p>
<p><strong>Check the status of above transaction id:</strong><br />
It is showing &#8220;in progress&#8221; because I didn&#8217;t commit the transaction yet.</p>
<pre class="crayon-plain-tag">SELECT txid_status(BIGINT '587');

txid_status
-----------
in progress</pre> </p>
<p><strong>Now, execute the COMMIT:</strong></p>
<pre class="crayon-plain-tag">COMMIT TRANSACTION;</pre> </p>
<p><strong>Now, check the status again:</strong></p>
<pre class="crayon-plain-tag">SELECT txid_status(BIGINT '587');

txid_status
-----------
committed</pre> </p>
<p><strong>You can also return transaction id, with the insert statement: (Using RETURNING)</strong></p>
<pre class="crayon-plain-tag">SELECT txid_status(BIGINT '587');

txid_status
-----------
committed</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server: Script to find Open Transaction to avoid Deadlock</title>
		<link>https://www.dbrnd.com/2017/05/sql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 22 May 2017 16:16:12 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[open transaction]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5281</guid>

					<description><![CDATA[In this post, I am sharing a T-SQL script to find out a list of open transactions in SQL Server. If application developers are not closed database transactions correctly, there are a chance for deadlock/lock in the database. All application/database developers should make sure that all transactions are closed which are opened earlier. DBA can [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fsql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Open%20Transaction%20to%20avoid%20Deadlock" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fsql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Open%20Transaction%20to%20avoid%20Deadlock" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fsql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Open%20Transaction%20to%20avoid%20Deadlock" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fsql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Open%20Transaction%20to%20avoid%20Deadlock" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fsql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Open%20Transaction%20to%20avoid%20Deadlock" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fsql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Open%20Transaction%20to%20avoid%20Deadlock" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fsql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions%2F&#038;title=SQL%20Server%3A%20Script%20to%20find%20Open%20Transaction%20to%20avoid%20Deadlock" data-a2a-url="https://www.dbrnd.com/2017/05/sql-server-script-to-find-open-transaction-to-avoid-deadlock-dm_tran_database_transactions/" data-a2a-title="SQL Server: Script to find Open Transaction to avoid Deadlock"></a></p><p>In this post, I am sharing a T-SQL script to find out a list of open transactions in SQL Server. If application developers are not closed database transactions correctly, there are a chance for deadlock/lock in the database.</p>
<p>All application/database developers should make sure that all transactions are closed which are opened earlier.<br />
DBA can use below script to find out a list of Open Transaction in SQL Server:</p>
<pre class="crayon-plain-tag">SELECT
	[tst].[session_id] AS SessionID
	,[des].[login_name] AS LoginName
	,DB_NAME (tdt.database_id) AS DatabaseName
	,[tdt].[database_transaction_begin_time] AS TransactionBeginTime
	,[tdt].[database_transaction_log_bytes_used] AS LogBytesUsed    
	,[mrsh].text AS QueryText
	,[ph].[query_plan] AS QueryPlan
FROM sys.dm_tran_database_transactions [tdt]
JOIN sys.dm_tran_session_transactions [tst]
	ON [tst].[transaction_id] = [tdt].[transaction_id]
JOIN sys.[dm_exec_sessions] [des]
	ON [des].[session_id] = [tst].[session_id]
JOIN sys.dm_exec_connections [dec]
	ON [dec].[session_id] = [tst].[session_id]
LEFT OUTER JOIN sys.dm_exec_requests [der]
	ON [der].[session_id] = [tst].[session_id]
CROSS APPLY sys.dm_exec_sql_text ([dec].[most_recent_sql_handle]) AS [mrsh]
OUTER APPLY sys.dm_exec_query_plan ([der].[plan_handle]) AS [ph]
ORDER BY [TransactionBeginTime] ASC
GO</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server: When records lock, use %%lockres%% to find the page information of records</title>
		<link>https://www.dbrnd.com/2017/03/sql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 04 Mar 2017 16:16:35 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[%%lockres%%]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[data page]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[dm_tran_locks]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4689</guid>

					<description><![CDATA[I shared a couple of articles on SQL Server database locking and how to detect a deadlock. Please access few of them. Different ways to detect a deadlock What happens after the Deadlock? I recently found an undocumented function %%lockres%% which we can use to locate the page information of records especially for those records [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records%2F&amp;linkname=SQL%20Server%3A%20When%20records%20lock%2C%20use%20%25%25lockres%25%25%20to%20find%20the%20page%20information%20of%20records" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records%2F&amp;linkname=SQL%20Server%3A%20When%20records%20lock%2C%20use%20%25%25lockres%25%25%20to%20find%20the%20page%20information%20of%20records" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records%2F&amp;linkname=SQL%20Server%3A%20When%20records%20lock%2C%20use%20%25%25lockres%25%25%20to%20find%20the%20page%20information%20of%20records" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records%2F&amp;linkname=SQL%20Server%3A%20When%20records%20lock%2C%20use%20%25%25lockres%25%25%20to%20find%20the%20page%20information%20of%20records" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records%2F&amp;linkname=SQL%20Server%3A%20When%20records%20lock%2C%20use%20%25%25lockres%25%25%20to%20find%20the%20page%20information%20of%20records" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records%2F&amp;linkname=SQL%20Server%3A%20When%20records%20lock%2C%20use%20%25%25lockres%25%25%20to%20find%20the%20page%20information%20of%20records" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records%2F&#038;title=SQL%20Server%3A%20When%20records%20lock%2C%20use%20%25%25lockres%25%25%20to%20find%20the%20page%20information%20of%20records" data-a2a-url="https://www.dbrnd.com/2017/03/sql-server-when-records-lock-use-lockres-to-find-the-page-information-of-records/" data-a2a-title="SQL Server: When records lock, use %%lockres%% to find the page information of records"></a></p><p>I shared a couple of articles on SQL Server database locking and how to detect a deadlock. Please access few of them.</p>
<p><strong><a href="https://www.dbrnd.com/2016/04/sql-server-8-different-ways-to-detect-a-deadlock-in-a-database/" target="_blank">Different ways to detect a deadlock</a></strong><br />
<a href="https://www.dbrnd.com/2016/04/sql-server-what-happens-after-the-deadlock-arise/" target="_blank"><strong>What happens after the Deadlock?</strong></a></p>
<blockquote><p>I recently found an undocumented function <strong>%%lockres%%</strong> which we can use to locate the page information of records especially for those records which hold a Lock.</p></blockquote>
<blockquote><p>It returns a hash value same as like column &#8220;resource_description&#8221; of table sys.dm_tran_locks, but the good thing is you can use it in your SELECT statement and can correlate the column resource_description of table sys.dm_tran_locks.</p></blockquote>
<p>By this way, you can get the exact record, and it&#8217;s page info which currently held by a Lock.</p>
<p>Let me demonstrate this,<br />
<strong>Prerequisites:</strong><br />
Use below article and generate a sample deadlock situation in your database server.</p>
<blockquote class="wp-embedded-content" data-secret="FLnip7cgnO"><p><a href="https://www.dbrnd.com/2016/04/sql-server-t-sql-script-to-generate-a-deadlock-in-a-database/">SQL Server: T-SQL script to generate a DeadLock in a Database</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/04/sql-server-t-sql-script-to-generate-a-deadlock-in-a-database/embed/#?secret=FLnip7cgnO" data-secret="FLnip7cgnO" width="600" height="338" title="&#8220;SQL Server: T-SQL script to generate a DeadLock in a Database&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>Check the result of below query and note down the result of column &#8220;resource_description&#8221;:</strong></p><pre class="crayon-plain-tag">select *from sys.dm_tran_locks</pre><p>
<img loading="lazy" decoding="async" class="alignnone wp-image-4696 size-full" title="SQL Server Deadlock Result" src="https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerDeadlockResult.png" alt="SQL Server Deadlock Result" width="628" height="272" srcset="https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerDeadlockResult.png 628w, https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerDeadlockResult-300x130.png 300w, https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerDeadlockResult-150x65.png 150w" sizes="auto, (max-width: 628px) 100vw, 628px" /></p>
<p><strong>Use %%lockres%% and SELECT both the tables:</strong><br />
You can easily correlate the result of sys.dm_tran_locks column resource_description and the result of %%lockres%%. You can find an exact record which held by a Lock.</p><pre class="crayon-plain-tag">SELECT %%lockres%% AS RecordPageInfo,* 
FROM tbl_SampleTable_A

SELECT %%lockres%% AS RecordPageInfo,* 
FROM tbl_SampleTable_B</pre><p>
<img loading="lazy" decoding="async" class="alignnone wp-image-4695 size-full" title="SQL Server Deadlock Record Page Info" src="https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerDeadlockRecordPageInfo.png" alt="SQL Server Deadlock Record Page Info" width="328" height="643" srcset="https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerDeadlockRecordPageInfo.png 328w, https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerDeadlockRecordPageInfo-153x300.png 153w, https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerDeadlockRecordPageInfo-150x294.png 150w" sizes="auto, (max-width: 328px) 100vw, 328px" /></p>
<p>After this exercise, Please COMMIT all your open transactions.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server 2016: Script to find last statement executed by a SPID using sys.dm_exec_input_buffer</title>
		<link>https://www.dbrnd.com/2017/01/sql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 14 Jan 2017 20:17:47 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[block transaction]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[DBCC INPUTBUFFER]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[sys.dm_exec_input_buffer]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4312</guid>

					<description><![CDATA[In the previous post, I have shared important details about SQL Server DBCC INPUTBUFFER and how we can use it to find a lead blocker transaction. SQL Server: DBCC INPUTBUFFER to find the last statement executed by a SPID SQL Server 2016 introduced sys.dm_exec_input_buffer which is a extended version of DBCC INPUTBUFFER. It is also [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer%2F&amp;linkname=SQL%20Server%202016%3A%20Script%20to%20find%20last%20statement%20executed%20by%20a%20SPID%20using%20sys.dm_exec_input_buffer" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer%2F&amp;linkname=SQL%20Server%202016%3A%20Script%20to%20find%20last%20statement%20executed%20by%20a%20SPID%20using%20sys.dm_exec_input_buffer" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer%2F&amp;linkname=SQL%20Server%202016%3A%20Script%20to%20find%20last%20statement%20executed%20by%20a%20SPID%20using%20sys.dm_exec_input_buffer" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer%2F&amp;linkname=SQL%20Server%202016%3A%20Script%20to%20find%20last%20statement%20executed%20by%20a%20SPID%20using%20sys.dm_exec_input_buffer" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer%2F&amp;linkname=SQL%20Server%202016%3A%20Script%20to%20find%20last%20statement%20executed%20by%20a%20SPID%20using%20sys.dm_exec_input_buffer" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer%2F&amp;linkname=SQL%20Server%202016%3A%20Script%20to%20find%20last%20statement%20executed%20by%20a%20SPID%20using%20sys.dm_exec_input_buffer" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer%2F&#038;title=SQL%20Server%202016%3A%20Script%20to%20find%20last%20statement%20executed%20by%20a%20SPID%20using%20sys.dm_exec_input_buffer" data-a2a-url="https://www.dbrnd.com/2017/01/sql-server-2016-script-to-find-last-statement-executed-by-a-spid-using-sys-dm_exec_input_buffer/" data-a2a-title="SQL Server 2016: Script to find last statement executed by a SPID using sys.dm_exec_input_buffer"></a></p><p>In the previous post, I have shared important details about SQL Server DBCC INPUTBUFFER and how we can use it to find a lead blocker transaction.</p>
<blockquote class="wp-embedded-content" data-secret="Iub70eY05J"><p><a href="https://www.dbrnd.com/2017/01/sql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock/">SQL Server: DBCC INPUTBUFFER to find the last statement executed by a SPID</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2017/01/sql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock/embed/#?secret=Iub70eY05J" data-secret="Iub70eY05J" width="600" height="338" title="&#8220;SQL Server: DBCC INPUTBUFFER to find the last statement executed by a SPID&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>SQL Server 2016 introduced sys.dm_exec_input_buffer which is a extended version of DBCC INPUTBUFFER.<br />
It is also serving the same service like DBCC INPUTBUFFER. </p>
<p>You can build your full query using this new system dmv.</p>
<p>Prepared a script to find the last statement executed by SPID. </p>
<pre class="crayon-plain-tag">SELECT
	der.session_id
	,ib.event_info AS QueryText
FROM sys.dm_exec_requests AS der
JOIN sys.dm_exec_sessions AS des 
	ON des.session_id = der.session_id
CROSS APPLY sys.dm_exec_input_buffer(der.session_id, der.request_id) AS ib
WHERE
	des.is_user_process = 1
GO</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server: DBCC INPUTBUFFER to find the last statement executed by a SPID</title>
		<link>https://www.dbrnd.com/2017/01/sql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 13 Jan 2017 19:55:43 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[block transaction]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[DBCC INPUTBUFFER]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[SPID]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4311</guid>

					<description><![CDATA[Are you SQL Server DBA? Have you ever faced deadlock situations? What are your steps to remove a deadlock? Do you know, how to get the last statement executed by lead blocker transaction? If you are a SQL DBA, you must know about this. You can access few related articles: SQL Server: Database Lock, Deadlock, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock%2F&amp;linkname=SQL%20Server%3A%20DBCC%20INPUTBUFFER%20to%20find%20the%20last%20statement%20executed%20by%20a%20SPID" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock%2F&amp;linkname=SQL%20Server%3A%20DBCC%20INPUTBUFFER%20to%20find%20the%20last%20statement%20executed%20by%20a%20SPID" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock%2F&amp;linkname=SQL%20Server%3A%20DBCC%20INPUTBUFFER%20to%20find%20the%20last%20statement%20executed%20by%20a%20SPID" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock%2F&amp;linkname=SQL%20Server%3A%20DBCC%20INPUTBUFFER%20to%20find%20the%20last%20statement%20executed%20by%20a%20SPID" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock%2F&amp;linkname=SQL%20Server%3A%20DBCC%20INPUTBUFFER%20to%20find%20the%20last%20statement%20executed%20by%20a%20SPID" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock%2F&amp;linkname=SQL%20Server%3A%20DBCC%20INPUTBUFFER%20to%20find%20the%20last%20statement%20executed%20by%20a%20SPID" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F01%2Fsql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock%2F&#038;title=SQL%20Server%3A%20DBCC%20INPUTBUFFER%20to%20find%20the%20last%20statement%20executed%20by%20a%20SPID" data-a2a-url="https://www.dbrnd.com/2017/01/sql-server-dbcc-inputbuffer-to-find-the-last-statement-executed-by-a-spid-bloc-deadlock/" data-a2a-title="SQL Server: DBCC INPUTBUFFER to find the last statement executed by a SPID"></a></p><p style="text-align: center;">Are you SQL Server DBA?<br />
Have you ever faced deadlock situations?<br />
What are your steps to remove a deadlock?<br />
Do you know, how to get the last statement executed by lead blocker transaction?</p>
<p>If you are a SQL DBA, you must know about this.<br />
You can access few related articles:</p>
<blockquote data-secret="xPkb2B15YJ" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2016/06/sql-server-database-lock-deadlock-block-and-isolation-interview-questions-and-answers-day-1/">SQL Server: Database Lock, Deadlock, Block and Isolation Interview Questions and   Answers (Day-1)</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/06/sql-server-database-lock-deadlock-block-and-isolation-interview-questions-and-answers-day-1/embed/#?secret=xPkb2B15YJ" data-secret="xPkb2B15YJ" width="600" height="338" title="&#8220;SQL Server: Database Lock, Deadlock, Block and Isolation Interview Questions and   Answers (Day-1)&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote data-secret="7puD3wlDCb" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2016/04/sql-server-8-different-ways-to-detect-a-deadlock-in-a-database/">SQL Server: 8 different ways to Detect a DeadLock in a Database</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/04/sql-server-8-different-ways-to-detect-a-deadlock-in-a-database/embed/#?secret=7puD3wlDCb" data-secret="7puD3wlDCb" width="600" height="338" title="&#8220;SQL Server: 8 different ways to Detect a DeadLock in a Database&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>DBCC INPUTBUFFER:</strong><br />
Using this DBCC command, you can get the last statement executed by a particular SPID which is playing a lead blocker role. </p>
<p>Please do not blindly kill the connections by SPID. You must know the root cause of deadlock and its previous lead blocker transaction. </p>
<p>Prepared a one example demonstration on this.</p>
<p><strong>Create a table with sample data:</strong><br />
<pre class="crayon-plain-tag">CREATE TABLE tbl_Students 
(
	StudID INT
	,StudName VARCHAR(20)
)
GO

INSERT INTO tbl_Students
VALUES 
(1,'Anvesh'),(2,'Neevan')
,(3,'Jeeny'),(4,'Roy');</pre>
<strong>Open a transaction and UPDATE one record:</strong><br />
<pre class="crayon-plain-tag">BEGIN TRANSACTION 
UPDATE tbl_Students SET StudName = 'Meera' 
WHERE StudID = 1
GO</pre>
<strong>Open a new session, UPDATE a same record:</strong><br />
<pre class="crayon-plain-tag">UPDATE tbl_Students SET StudName = 'Anvesh' 
WHERE StudID = 1
GO</pre>
<strong>Execute SP_WHO2 and find SPID of lead blocker transaction:</strong><br />
In my enviorment, I have found spid is: 151. You can find block by spid in BlkBy column<br />
<pre class="crayon-plain-tag">SP_WHO2</pre>
<strong>Execute DBCC INPUTBUFFER to find out last statement executed by SPID:</strong><br />
<pre class="crayon-plain-tag">DBCC INPUTBUFFER (151)</pre>
<strong>Find out lead blocker query:</strong><br />
<pre class="crayon-plain-tag">EventType      Parameters EventInfo
-------------- ---------- --------------------
Language Event 0          BEGIN TRANSACTION 
UPDATE tbl_Students SET StudName = 'Meera' 
WHERE StudID = 1</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server: Script to find waiting Queries which are block by other running Queries</title>
		<link>https://www.dbrnd.com/2016/12/sql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction/</link>
					<comments>https://www.dbrnd.com/2016/12/sql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 09 Dec 2016 19:51:12 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[block transaction]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[long running transaction]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[waiting query]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4020</guid>

					<description><![CDATA[In this post, I am sharing one script to find a list of queries which are blocked by other running queries of SQL Server. DBAs are always seeking for long running queries, most resources consumed query. Using this script DBA can find list of waiting queries which are not under any type of execution. DBAs [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fsql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20waiting%20Queries%20which%20are%20block%20by%20other%20running%20Queries" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fsql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20waiting%20Queries%20which%20are%20block%20by%20other%20running%20Queries" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fsql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20waiting%20Queries%20which%20are%20block%20by%20other%20running%20Queries" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fsql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20waiting%20Queries%20which%20are%20block%20by%20other%20running%20Queries" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fsql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20waiting%20Queries%20which%20are%20block%20by%20other%20running%20Queries" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fsql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20waiting%20Queries%20which%20are%20block%20by%20other%20running%20Queries" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fsql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction%2F&#038;title=SQL%20Server%3A%20Script%20to%20find%20waiting%20Queries%20which%20are%20block%20by%20other%20running%20Queries" data-a2a-url="https://www.dbrnd.com/2016/12/sql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction/" data-a2a-title="SQL Server: Script to find waiting Queries which are block by other running Queries"></a></p><p>In this post, I am sharing one script to find a list of queries which are blocked by other running queries of SQL Server. </p>
<blockquote><p>DBAs are always seeking for long running queries, most resources consumed query. Using this script DBA can find list of waiting queries which are not under any type of execution. </p>
<p>DBAs can use this query to prevent the deadlock also because long running block transactions can occur a deadlock situation.</p></blockquote>
<pre class="crayon-plain-tag">SELECT
	dowt.session_id
	,dowt.wait_duration_ms
	,dowt.wait_type
	,dowt.blocking_session_id
	,dese.host_name as HostName
	,der.command
	,der.percent_complete
	,der.cpu_time
	,der.total_elapsed_time
	,der.reads
	,der.writes
	,der.logical_reads
	,der.row_count
	,dest.text AS QueryText
	,dest.dbid AS DatabaseID
	,deqp.query_plan
	,der.plan_handle
FROM sys.dm_os_waiting_tasks as dowt
INNER JOIN sys.dm_exec_sessions as dese
	ON dowt.session_id = dese.session_id
INNER JOIN sys.dm_exec_requests as der
	ON dese.session_id = der.session_id
CROSS APPLY sys.dm_exec_sql_text(der.plan_handle) as dest
CROSS APPLY sys.dm_exec_query_plan(der.plan_handle) as deqp
WHERE dowt.session_id &gt; 50</pre> </p>
<p><strong>Here, You can also access few related articles:</strong></p>
<blockquote class="wp-embedded-content" data-secret="pPED8T570n"><p><a href="https://www.dbrnd.com/2016/11/sql-server-script-to-find-top-20-most-used-or-executed-sql-queries-dm_exec_query_stats/">SQL Server: Script to find TOP 20 most used or executed SQL Queries</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/11/sql-server-script-to-find-top-20-most-used-or-executed-sql-queries-dm_exec_query_stats/embed/#?secret=pPED8T570n" data-secret="pPED8T570n" width="600" height="338" title="&#8220;SQL Server: Script to find TOP 20 most used or executed SQL Queries&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="8jqWygALgw"><p><a href="https://www.dbrnd.com/2016/11/sql-server-script-to-find-top-20-running-query-which-are-utilizing-more-cpu/">SQL Server: Script to find top 20 running query, which are utilizing more CPU</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/11/sql-server-script-to-find-top-20-running-query-which-are-utilizing-more-cpu/embed/#?secret=8jqWygALgw" data-secret="8jqWygALgw" width="600" height="338" title="&#8220;SQL Server: Script to find top 20 running query, which are utilizing more CPU&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="o8e33uMZIe"><p><a href="https://www.dbrnd.com/2016/04/sql-server-8-different-ways-to-detect-a-deadlock-in-a-database/">SQL Server: 8 different ways to Detect a DeadLock in a Database</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/04/sql-server-8-different-ways-to-detect-a-deadlock-in-a-database/embed/#?secret=o8e33uMZIe" data-secret="o8e33uMZIe" width="600" height="338" title="&#8220;SQL Server: 8 different ways to Detect a DeadLock in a Database&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/12/sql-server-script-to-find-waiting-queries-which-are-block-by-other-running-queries-find-block-transaction/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: How to generate a DeadLock in a Database (ERROR:  deadlock detected)</title>
		<link>https://www.dbrnd.com/2016/11/postgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 19 Nov 2016 12:13:52 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[BEGIN]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[COMMIT]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[generate deadlock]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[transaction]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3873</guid>

					<description><![CDATA[The DeadLock in a database system is a very common thing and all Database Administrator responsible to detect and manage a DeadLock situation. In this post, I am sharing one small demonstration t to generate a DeadLock situation in a PostgreSQL. Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System? Most [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F11%2Fpostgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected%2F&amp;linkname=PostgreSQL%3A%20How%20to%20generate%20a%20DeadLock%20in%20a%20Database%20%28ERROR%3A%20%20deadlock%20detected%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F11%2Fpostgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected%2F&amp;linkname=PostgreSQL%3A%20How%20to%20generate%20a%20DeadLock%20in%20a%20Database%20%28ERROR%3A%20%20deadlock%20detected%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F11%2Fpostgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected%2F&amp;linkname=PostgreSQL%3A%20How%20to%20generate%20a%20DeadLock%20in%20a%20Database%20%28ERROR%3A%20%20deadlock%20detected%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F11%2Fpostgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected%2F&amp;linkname=PostgreSQL%3A%20How%20to%20generate%20a%20DeadLock%20in%20a%20Database%20%28ERROR%3A%20%20deadlock%20detected%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F11%2Fpostgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected%2F&amp;linkname=PostgreSQL%3A%20How%20to%20generate%20a%20DeadLock%20in%20a%20Database%20%28ERROR%3A%20%20deadlock%20detected%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F11%2Fpostgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected%2F&amp;linkname=PostgreSQL%3A%20How%20to%20generate%20a%20DeadLock%20in%20a%20Database%20%28ERROR%3A%20%20deadlock%20detected%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F11%2Fpostgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected%2F&#038;title=PostgreSQL%3A%20How%20to%20generate%20a%20DeadLock%20in%20a%20Database%20%28ERROR%3A%20%20deadlock%20detected%29" data-a2a-url="https://www.dbrnd.com/2016/11/postgresql-how-to-generate-a-deadlock-in-a-database-error-deadlock-detected/" data-a2a-title="PostgreSQL: How to generate a DeadLock in a Database (ERROR:  deadlock detected)"></a></p><p>The DeadLock in a database system is a very common thing and all Database Administrator responsible to detect and manage a DeadLock situation.</p>
<p>In this post, I am sharing one small demonstration t to generate a DeadLock situation in a PostgreSQL. </p>
<blockquote data-secret="ZVivaTqnPl" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2016/04/database-theory-what-is-timeout-lock-block-and-deadlock-in-a-database-system/">Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System?</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/04/database-theory-what-is-timeout-lock-block-and-deadlock-in-a-database-system/embed/#?secret=ZVivaTqnPl" data-secret="ZVivaTqnPl" width="600" height="338" title="&#8220;Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System?&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote><p>Most of the time, We are facing DeadLock problem in our Production Database Server.<br />
But What happen, when Database Administrator wants to generate DeadLock situation and wants to create a script for detecting a DeadLock.</p></blockquote>
<p><strong>First create two sample tables:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE ABC (ID INT PRIMARY KEY);
CREATE TABLE XYZ (ID INT PRIMARY KEY);</pre> </p>
<p><strong>Open Session &#8211; 1 and execute below code:</strong></p>
<pre class="crayon-plain-tag">BEGIN;
INSERT INTO ABC VALUES (1);</pre> </p>
<p><strong>Open Session &#8211; 2 and execute below code:</strong></p>
<pre class="crayon-plain-tag">BEGIN;
INSERT INTO XYZ VALUES (1);
INSERT INTO ABC VALUES (1);</pre> </p>
<p><strong>Again open Session &#8211; 1 and execute below Insert Statement:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO XYZ VALUES (1);</pre> </p>
<p><strong>You will get a below deadlock message:</strong></p>
<pre class="crayon-plain-tag">ERROR:  deadlock detected
DETAIL:  Process 5944 waits for ShareLock on transaction 892; blocked by process 4884.
Process 4884 waits for ShareLock on transaction 891; blocked by process 5944.
HINT:  See server log for query details.
CONTEXT:  while inserting index tuple (0,2) in relation "xyz_pkey"</pre> </p>
<h2>Please visit other related articles:</h2>
<blockquote data-secret="75LewbEKVb" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2016/08/postgresql-9-5-using-for-update-skip-locked-option-select-only-committed-records-mvcc-lock-with-nowait-thread/">PostgreSQL 9.5: Using FOR UPDATE SKIP LOCKED Option SELECT only Committed Records</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/08/postgresql-9-5-using-for-update-skip-locked-option-select-only-committed-records-mvcc-lock-with-nowait-thread/embed/#?secret=75LewbEKVb" data-secret="75LewbEKVb" width="600" height="338" title="&#8220;PostgreSQL 9.5: Using FOR UPDATE SKIP LOCKED Option SELECT only Committed Records&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote data-secret="MqeJj9B5Nt" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2016/09/postgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block/">PostgreSQL: Script to find information about the Locks held by Open Transactions (pg_locks)</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/09/postgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block/embed/#?secret=MqeJj9B5Nt" data-secret="MqeJj9B5Nt" width="600" height="338" title="&#8220;PostgreSQL: Script to find information about the Locks held by Open Transactions (pg_locks)&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote data-secret="jgqH9JxLjm" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2015/09/script-to-find-sessions-that-are-blocking-other-sessions-in-postgresql/">Script to find sessions that are blocking other sessions in PostgreSQL</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/09/script-to-find-sessions-that-are-blocking-other-sessions-in-postgresql/embed/#?secret=jgqH9JxLjm" data-secret="jgqH9JxLjm" width="600" height="338" title="&#8220;Script to find sessions that are blocking other sessions in PostgreSQL&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL: Script to find information about the Locks held by Open Transactions (pg_locks)</title>
		<link>https://www.dbrnd.com/2016/09/postgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 06 Sep 2016 19:23:52 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[DeadLock]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[pg_class]]></category>
		<category><![CDATA[pg_locks]]></category>
		<category><![CDATA[pg_stat_activity]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3582</guid>

					<description><![CDATA[In this post, I am sharing one script to find information about the Locks which are held by Open Transactions of PostgreSQL Database Server. Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System? PostgreSQL view pg_locks contains the main information about the all types of lock of Database Server. It contains [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20information%20about%20the%20Locks%20held%20by%20Open%20Transactions%20%28pg_locks%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20information%20about%20the%20Locks%20held%20by%20Open%20Transactions%20%28pg_locks%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20information%20about%20the%20Locks%20held%20by%20Open%20Transactions%20%28pg_locks%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20information%20about%20the%20Locks%20held%20by%20Open%20Transactions%20%28pg_locks%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20information%20about%20the%20Locks%20held%20by%20Open%20Transactions%20%28pg_locks%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20information%20about%20the%20Locks%20held%20by%20Open%20Transactions%20%28pg_locks%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F09%2Fpostgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block%2F&#038;title=PostgreSQL%3A%20Script%20to%20find%20information%20about%20the%20Locks%20held%20by%20Open%20Transactions%20%28pg_locks%29" data-a2a-url="https://www.dbrnd.com/2016/09/postgresql-script-to-find-information-about-the-locks-held-by-open-transactions-pg_locks-deadlock-lock-block/" data-a2a-title="PostgreSQL: Script to find information about the Locks held by Open Transactions (pg_locks)"></a></p><p>In this post, I am sharing one script to find information about the Locks which are held by Open Transactions of PostgreSQL Database Server.</p>
<blockquote data-secret="ECraasdyx3" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2016/04/database-theory-what-is-timeout-lock-block-and-deadlock-in-a-database-system/">Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System?</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/04/database-theory-what-is-timeout-lock-block-and-deadlock-in-a-database-system/embed/#?secret=ECraasdyx3" data-secret="ECraasdyx3" width="600" height="338" title="&#8220;Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System?&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote><p>PostgreSQL view pg_locks contains the main information about the all types of lock of Database Server.<br />
It contains one row per active lockable object.</p></blockquote>
<p>This script is very helpful to all PostgreSQL Database Administrators.</p>
<p><strong>Below is a script:<br />
</strong><br />
<pre class="crayon-plain-tag">SELECT 
	pl.pid AS ProcessID
	,psa.datname AS DatabaseName
	,psa.usename AS UserName
	,psa.application_name AS ApplicationName
	,ps.relname AS ObjectName
	,psa.query_start AS QueryStartTime
	,psa.state AS QueryState
	,psa.query AS SQLQuery
	,pl.locktype
	,pl.tuple AS TupleNumber
	,pl.mode AS LockMode
	,pl.granted -- True if lock is held, false if lock is awaited
FROM pg_locks AS pl 
LEFT JOIN pg_stat_activity AS psa
	ON pl.pid = psa.pid
LEFT JOIN pg_class AS ps
	ON pl.relation = ps.oid</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
