<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Sql-Keywords on despatches</title><link>https://icle.es/tags/sql-keywords/</link><description>Recent content in Sql-Keywords on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Wed, 18 Mar 2026 15:13:17 +0000</lastBuildDate><atom:link href="https://icle.es/tags/sql-keywords/index.xml" rel="self" type="application/rss+xml"/><item><title>Tracking progress of an update statement</title><link>https://icle.es/2011/11/02/tracking-progress-of-an-update-statement-1101/</link><pubDate>Wed, 02 Nov 2011 19:59:02 +0000</pubDate><guid>https://icle.es/2011/11/02/tracking-progress-of-an-update-statement-1101/</guid><description>&lt;p>Sometimes there is a need to execute a long running update statement. This
update statement might be modifying millions of rows as was the case when we
went hunting for a way to track the progress of the update. Hunting around took
us to &lt;a href="http://archives.postgresql.org/pgsql-admin/2002-07/msg00286.php">http://archives.postgresql.org/pgsql-admin/2002-07/msg00286.php&lt;/a> In our
particular case, we are using postgresql but this should work with any database
server that provides sequences. Our original sql was of the form:&lt;/p>
```sql
update only table1 t1
set amount = t2.price
from table2 t2
where t1.id = t2.id;
```
&lt;p>There is of course now way of figuring out how many rows had been updated
already. The first step was to create a sequence&lt;/p>
```sql
CREATE TEMPORARY SEQUENCE seq_progress START 1;
```</description><content:encoded><![CDATA[<p>Sometimes there is a need to execute a long running update statement. This
update statement might be modifying millions of rows as was the case when we
went hunting for a way to track the progress of the update. Hunting around took
us to <a href="http://archives.postgresql.org/pgsql-admin/2002-07/msg00286.php">http://archives.postgresql.org/pgsql-admin/2002-07/msg00286.php</a> In our
particular case, we are using postgresql but this should work with any database
server that provides sequences. Our original sql was of the form:</p>
```sql
update only table1 t1
set amount = t2.price
from table2 t2
where t1.id = t2.id;
```
<p>There is of course now way of figuring out how many rows had been updated
already. The first step was to create a sequence</p>
```sql
CREATE TEMPORARY SEQUENCE seq_progress START 1;
```
<p>We can then use this sequence in the update statement to ensure that each row
updated also increments the sequence</p>
```sql
update only table1 t1
set amount = t2.price
from table2 t2
where nextval('seq_progress') != 0
and t1.id = t2.id;
```
<p>Once the query is running, you can open another connection to the database. To
get an indication of how far it has got, you can just run the following</p>
```sql
select nextval('seq_progress');
```
<p>Bear in mind that this will also increment it by 1 but if you have millions of
rows which is really the only case in which this would be useful, a few
additional increments is hardly going to make a difference.</p>
<p>Good luck and have fun!</p>]]></content:encoded></item></channel></rss>