<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Akshya Sharma on Medium]]></title>
        <description><![CDATA[Stories by Akshya Sharma on Medium]]></description>
        <link>https://medium.com/@akshya.s?source=rss-8122ff100eed------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*vKQFYX843H6X1f2Y.jpg</url>
            <title>Stories by Akshya Sharma on Medium</title>
            <link>https://medium.com/@akshya.s?source=rss-8122ff100eed------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 07:14:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akshya.s/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[BigQuery Transactions in Sessions: Why Your Code Might Hang.]]></title>
            <link>https://medium.com/google-cloud/bigquery-transactions-in-sessions-why-your-code-might-hang-557e311e20e0?source=rss-8122ff100eed------2</link>
            <guid isPermaLink="false">https://medium.com/p/557e311e20e0</guid>
            <category><![CDATA[software-design]]></category>
            <category><![CDATA[bigquery]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[transactions]]></category>
            <category><![CDATA[data]]></category>
            <dc:creator><![CDATA[Akshya Sharma]]></dc:creator>
            <pubDate>Sun, 08 Dec 2024 07:01:23 GMT</pubDate>
            <atom:updated>2024-12-09T10:14:08.474Z</atom:updated>
            <content:encoded><![CDATA[<h3>BigQuery Transactions in sessions: Why your code might hang.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OtNJpKtxAFz44fCmpdDWwQ.png" /></figure><p>Avoid data discrepancies and unexpected behavior with proper exception handling.</p><p>BigQuery Multi-Statement Transaction allows you to perform mutating operations on one or more tables atomically. These mutations can happen inside a single query or in case of sessions across multiple queries.</p><p>Let’s understand how BQ Multi-statement transactions behave in session context and considerations around handling exceptions within the transaction.</p><p>Before we get there, let’s create few objects in BigQuery to demonstrate the concept discussed later in the article:</p><ol><li>test_data.transaction_table: A simple table on which transactions are performed.</li><li>test_data.transaction_testing: A stored procedure which implements the logic for transaction handling.</li></ol><pre>CREATE OR REPLACE PROCEDURE test_data.transaction_testing(OUT out_error_message STRING)<br><br>BEGIN<br> DECLARE v_transaction_id STRING DEFAULT GENERATE_UUID();<br>BEGIN<br> <br> BEGIN TRANSACTION;<br>   INSERT INTO test_data.transaction_table<br>    VALUES (<br>       1, &quot;Hello&quot;<br>    );<br><br>   INSERT INTO test_data.transaction_table<br>    VALUES (<br>       CAST(1/0 AS INT64), &quot;Hello&quot;<br>    );   <br>  <br>  SET out_error_message= @@error.message;<br> <br>COMMIT TRANSACTION;<br><br>EXCEPTION WHEN ERROR THEN<br>  SET out_error_message= @@error.message;<br>END;<br>END;</pre><p>All statements which are part of the stored procedure within BEGIN TRANSACTION…COMMIT_TRANSACTION block are part of multi-statement transaction.</p><p>Note we have intentionally introduced an exception in second insert statement.</p><p>Lets try running this in BQ Console:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1-v_skkIlISMMakvNtqsIg.png" /></figure><p>We notice that the transaction was rolled back even though we have not explicitly specified a ROLLBACK statement in the EXCEPTION block.</p><p>Now, let’s do a slight modification in our testing approach, lets execute the procedure in a session.</p><p>For executing the procedure in a session select session mode (More -&gt; Query settings -&gt; Use session mode)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PP6FfRsLYRpSB_SE81ey6A.png" /></figure><p>Now lets try executing the same procedure in session mode:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YXSZxmBnLii6E00MSPbung.png" /></figure><p>This time the transaction is not Rolled back.</p><p><strong>How does this behavior impacts the execution and may cause data discrepancies?</strong></p><p>Impact:</p><ul><li>Firstly, in case of executing within session as the transaction is not committed or rolled back, transaction is still active which means all <a href="https://cloud.google.com/bigquery/docs/transactions#transaction_concurrency">Transaction Concurrency limitations</a> apply.</li><li>Secondly, even in case of not executing as part of session, the ROLLBACK is the last statement which is executed. So if you have any logic in EXCEPTION BLOCK that does DML, it will also get rolled back.</li></ul><p>To understand how this impact the execution, let’s introduce one more table:</p><ul><li>test_data.transaction_status: Table captures the status of the transaction both in case of success and failure.</li></ul><p><strong><em>Note</em></strong>: This is a typical pattern found while migrating from legacy Datawarehouses to BigQuery and shown here only as an example. However, single row DMLs are considered an anti-pattern in BigQuery and need to be avoided.</p><p>Adding the transaction_status handling the procedure looks like:</p><pre>CREATE OR REPLACE PROCEDURE `test_data.transaction_testing`(OUT out_error_message STRING)<br>BEGIN<br> DECLARE v_transaction_id STRING DEFAULT GENERATE_UUID();<br>BEGIN<br> INSERT INTO test_data.transaction_status <br> VALUES (v_transaction_id, &#39;STARTED&#39;, null );<br> <br> BEGIN TRANSACTION;<br>   INSERT INTO test_data.transaction_table<br>    VALUES (<br>       1, &quot;Hello&quot;<br>    );<br><br>   INSERT INTO test_data.transaction_table<br>    VALUES (<br>       CAST(1/0 AS INT64), &quot;Hello&quot;<br>    );   <br>  <br>    UPDATE test_data.transaction_status<br>    SET status = &#39;SUCCESS&#39;<br>    WHERE transaction_id = v_transaction_id;<br><br>  SET out_error_message= @@error.message;<br> <br>COMMIT TRANSACTION;<br><br>EXCEPTION WHEN ERROR THEN<br>  UPDATE test_data.transaction_status<br>  SET status = &#39;FAILED&#39;<br>     ,status_message = @@error.message<br>  WHERE transaction_id = v_transaction_id;<br>  <br>  SET out_error_message= @@error.message;<br>END;<br>END;</pre><p>Now when we execute the procedure in session:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*85otChGDrsZP5BerrwywwQ.png" /></figure><p>The transaction was not rolled back as expected, however the UPDATE statement became part of the transaction and hence it will not reflect the correct status.</p><p>It’s not only that, now lets try updating test_data.transaction_status in another query:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rj1e0dzTVTYONkl08uPkFQ.png" /></figure><p>The statement keeps on running, seems like the script is hung. The reason this happens is because, “Conflicting DML statements that run outside of a transaction are queued to run later, subject to <a href="https://cloud.google.com/bigquery/docs/data-manipulation-language#update_delete_merge_dml_concurrency">queuing limits</a>.”</p><p>The job keeps on running till the session which created the previous transaction terminates. Unless explicitly aborted the session will be automatically terminated after 24 hours as documented <a href="https://cloud.google.com/bigquery/docs/sessions-terminating#terminate_the_current_session">here</a>.</p><p><strong>How do we solve this ?</strong></p><p>Yes, the solution is simple, add explicit ROLLBACK statement in your EXCEPTION BLOCK. However, to make sure the ROLLBACK is executed only in case when the transaction is active, we will introduce a flag to identify active transaction.</p><p>Here is the modified code with the solution:</p><pre>CREATE OR REPLACE PROCEDURE `test_data.transaction_testing`(OUT out_error_message STRING)<br>BEGIN<br> DECLARE v_transaction_id STRING DEFAULT GENERATE_UUID();<br> DECLARE v_transaction_flag BOOL DEFAULT false;<br>BEGIN<br> INSERT INTO test_data.transaction_status <br> VALUES (v_transaction_id, &#39;STARTED&#39;, null );<br> <br> BEGIN TRANSACTION;<br>   SET v_transaction_flag = true;<br>   INSERT INTO test_data.transaction_table<br>    VALUES (<br>       1, &quot;Hello&quot;<br>    );<br><br>   INSERT INTO test_data.transaction_table<br>    VALUES (<br>       CAST(1/0 AS INT64), &quot;Hello&quot;<br>    );   <br>  <br>    UPDATE test_data.transaction_status<br>    SET status = &#39;SUCCESS&#39;<br>    WHERE transaction_id = v_transaction_id;<br><br>  SET out_error_message= @@error.message;<br>  <br>  SET v_transaction_flag = false;<br>COMMIT TRANSACTION;<br><br>EXCEPTION WHEN ERROR THEN<br>  IF v_transaction_flag<br>  THEN<br>   ROLLBACK TRANSACTION;<br>  END IF;<br><br>  UPDATE test_data.transaction_status<br>  SET status = &#39;FAILED&#39;<br>     ,status_message = @@error.message<br>  WHERE transaction_id = v_transaction_id;<br>  <br>  SET out_error_message= @@error.message;<br>END;<br>END;</pre><p>Now when we execute the procedure in session:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mQyfEUfEuSNZ_NPwfcN1zA.png" /></figure><p>We notice the transaction is rolled back and if we do the update on our transaction_status table, the update does not hang:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OVTzpYiOUQnaD78VdGsB-w.png" /></figure><p>Also the status of the failed transaction is captured correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oabXLJfwoYwkLzHOgr5J2w.png" /></figure><p>In summary, this example demonstrates the importance of careful exception handling within BigQuery transactions, particularly when using sessions. By implementing this explicit rollback strategy within your stored procedures, you can ensure data integrity and prevent potential issues with hanging transactions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=557e311e20e0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/bigquery-transactions-in-sessions-why-your-code-might-hang-557e311e20e0">BigQuery Transactions in Sessions: Why Your Code Might Hang.</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>