<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    
    <title>Magnus Hagander's PostgreSQL blog (Entries tagged as sql)</title>
    <link>http://blog.hagander.net/</link>
    <description></description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.6.2 - http://www.s9y.org/</generator>
    
    

<item>
    <title>Finding gaps in partitioned sequences</title>
    <link>http://blog.hagander.net/archives/203-Finding-gaps-in-partitioned-sequences.html</link>
            <category>PostgreSQL</category>
    
    <comments>http://blog.hagander.net/archives/203-Finding-gaps-in-partitioned-sequences.html#comments</comments>
    <wfw:comment>http://blog.hagander.net/wfwcomment.php?cid=203</wfw:comment>

    <slash:comments>4</slash:comments>
    <wfw:commentRss>http://blog.hagander.net/rss.php?version=2.0&amp;type=comments&amp;cid=203</wfw:commentRss>
    

    <author>nospam@example.com (Magnus Hagander)</author>
    <content:encoded>
    &lt;p&gt;There are an almost unlimited number of articles on the web about how to find gaps in sequences in SQL. And it doesn&#039;t have to be very hard. Doing it in a &quot;partitioned sequence&quot; makes it a bit harder, but still not very hard. But when I turned to a window aggregate to do that, I was immediately told &quot;hey, that&#039;s a good example of a window aggregate to solve your daily chores, you should blog about that&quot;. So here we go - yet another example of finding a gap in a sequence using SQL.&lt;/p&gt;

&lt;p&gt;I have a database that is very simply structured - it&#039;s got a primary key made out of &lt;i&gt;(groupid, year, month, seq)&lt;/i&gt;, all integers. On top of that it has a couple of largish text fields and an fti field for full text search. (Initiated people will know right away which database this is). The sequence in the seq column resets to zero for each combination of &lt;i&gt;(groupid, year, month)&lt;/i&gt;. And I wanted to find out where there were gaps in it, and how big they were, to debug the tool that wrote the data into the database. This is really easy with a window aggregate:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&lt;div class=&quot;geshi&quot; style=&quot;text-align: left&quot;&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; groupid,&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; year,&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; month,&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; seq, &lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; seq-lag&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;seq,&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; OVER &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;PARTITION &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;BY&lt;/span&gt; groupid, year, month &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;BY&lt;/span&gt; seq&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AS&lt;/span&gt; gap &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; mytable&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AS&lt;/span&gt; t&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;WHERE&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;NOT&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;t.gap=&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;BY&lt;/span&gt; groupid, year, month, seq&lt;br /&gt;&amp;#160;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One advantage to using a window aggregate for this is that we actually get the whole row back, and not just the primary key - so it&#039;s easy enough to include all the data you need to figure something out.&lt;/p&gt;

&lt;p&gt;What about performance? I don&#039;t really have a big database to test this on, so I can&#039;t say for sure. It&#039;s going to be a sequential scan, since I look at the &lt;i&gt;whole&lt;/i&gt; table,and not just parts of it. It takes about 4 seconds to run over a table of about a million rows, 2.7Gb, on a modest VM with no actual I/O capacity to speak of and a very limited amount of memory, returning about 100 rows. It&#039;s certainly by far fast enough for me in this case.&lt;/p&gt;

&lt;p&gt;And as a bonus, it found me two bugs in the loading script and at least one bug in somebody elses code that I&#039;m now waiting on to get fixed...&lt;/p&gt;

 
    </content:encoded>

    <pubDate>Fri, 27 Jan 2012 16:53:52 +0000</pubDate>
    <guid isPermaLink="false">http://blog.hagander.net/archives/203-guid.html</guid>
    <category>postgresql</category>
<category>sql</category>
<category>window aggregates</category>

</item>
<item>
    <title>Getting a range of entries centered around a point</title>
    <link>http://blog.hagander.net/archives/147-Getting-a-range-of-entries-centered-around-a-point.html</link>
            <category>PostgreSQL</category>
    
    <comments>http://blog.hagander.net/archives/147-Getting-a-range-of-entries-centered-around-a-point.html#comments</comments>
    <wfw:comment>http://blog.hagander.net/wfwcomment.php?cid=147</wfw:comment>

    <slash:comments>4</slash:comments>
    <wfw:commentRss>http://blog.hagander.net/rss.php?version=2.0&amp;type=comments&amp;cid=147</wfw:commentRss>
    

    <author>nospam@example.com (Magnus Hagander)</author>
    <content:encoded>
    &lt;p&gt;I had a question yesterday on an internal IRC channel from one of my colleagues in Norway about a SQL query that would &quot;for a given id value, return the 50 rows centered around the row with this id&quot;, where the id column can contain gaps (either because they were inserted with gaps, or because there are further &lt;strong&gt;WHERE&lt;/strong&gt; restrictions in the query).&lt;/p&gt;

&lt;p&gt;I came up with a reasonably working solution fairly quickly, but I made one mistake. For fun, I asked around a number of my PostgreSQL contacts on IM and IRC for their solutions, and it turns out that almost everybody made the exact same mistake at first. I&#039;m pretty sure all of them, like me, would&#039;ve found and fixed that issue within seconds if they were in front of a psql console. But I figured that was a good excuse to write a blog post about it.&lt;/p&gt;

&lt;p&gt;The solution itself becomes pretty simple if you rephrase the problem as &quot;for a given id value, return the 25 rows preceding and the 25 rows following the row with this id&quot;. That pretty much spells a &lt;strong&gt;UNION&lt;/strong&gt; query. Thus, the solution to the problem is:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&lt;div class=&quot;geshi&quot; style=&quot;text-align: left&quot;&gt;&lt;br /&gt;&amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; id,field1,field2 &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; mytable &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;WHERE&lt;/span&gt; id &amp;amp;gt;= &lt;span style=&quot;color: #cc66cc;&quot;&gt;123456&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;BY&lt;/span&gt; id &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;LIMIT&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;26&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AS&lt;/span&gt; a&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ALL&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; id,field1,field2 &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; mytable &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;WHERE&lt;/span&gt; id &amp;amp;lt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;123456&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;BY&lt;/span&gt; id &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;DESC&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;LIMIT&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;25&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AS&lt;/span&gt; b&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;BY&lt;/span&gt; id;&lt;br /&gt;&amp;#160;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The mistake everybody made? Forgetting that you need a subselect in order to use &lt;strong&gt;LIMIT&lt;/strong&gt;. Without subselects, you can&#039;t put &lt;strong&gt;ORDER BY&lt;/strong&gt; or &lt;strong&gt;LIMIT&lt;/strong&gt; inside the two separate parts of the query, only at the outer end of it. But we specifically need to apply the &lt;strong&gt;LIMIT&lt;/strong&gt; individually, and the &lt;strong&gt;ORDER BY&lt;/strong&gt; needs to be different for the two parts.&lt;/p&gt;

&lt;p&gt;Another question I got around this was, why use &lt;strong&gt;UNION ALL&lt;/strong&gt;. We know, after all, that there are no overlapping rows so the result should be the same as for &lt;strong&gt;UNION&lt;/strong&gt;. And this is exactly the reason why &lt;strong&gt;UNION ALL&lt;/strong&gt; should be used, rather than a plain &lt;strong&gt;UNION&lt;/strong&gt;. &lt;i&gt;We&lt;/i&gt; know it - the database doesn&#039;t. A &lt;strong&gt;UNION&lt;/strong&gt; query will generate a plan that requires an extra &lt;i&gt;unique&lt;/i&gt; node at the top, to make sure that there are no overlapping rows. So the tip here is - &lt;i&gt;always&lt;/i&gt; use &lt;strong&gt;UNION ALL&lt;/strong&gt; rather than &lt;strong&gt;UNION&lt;/strong&gt; whenever you &lt;i&gt;know&lt;/i&gt; that the results are not overlapping.&lt;/p&gt;

&lt;p&gt;All things considered, this query produces a pretty quick plan even for large datasets, since it allows us to do two independent index scans, one backwards. Since there are &lt;strong&gt;LIMIT&lt;/strong&gt; nodes on the scans, they will stop running as soon as they have produced the required number of rows, which is going to be very small compared to the size of the table. This is the query plan I got on my test data:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&lt;div class=&quot;geshi&quot; style=&quot;text-align: left&quot;&gt;&lt;br /&gt;&amp;#160;Sort&amp;#160; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;cost=&lt;span style=&quot;color: #cc66cc;&quot;&gt;54&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;60&lt;/span&gt;..&lt;span style=&quot;color: #cc66cc;&quot;&gt;54&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;73&lt;/span&gt; rows=&lt;span style=&quot;color: #cc66cc;&quot;&gt;51&lt;/span&gt; width=&lt;span style=&quot;color: #cc66cc;&quot;&gt;86&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160;Sort &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;KEY&lt;/span&gt;: id&lt;br /&gt;&amp;#160; &amp;#160;-&amp;amp;gt;&amp;#160; Append&amp;#160; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;cost=&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;00&lt;/span&gt;..&lt;span style=&quot;color: #cc66cc;&quot;&gt;53&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;15&lt;/span&gt; rows=&lt;span style=&quot;color: #cc66cc;&quot;&gt;51&lt;/span&gt; width=&lt;span style=&quot;color: #cc66cc;&quot;&gt;86&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;-&amp;amp;gt;&amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;LIMIT&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;cost=&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;00&lt;/span&gt;..&lt;span style=&quot;color: #cc66cc;&quot;&gt;35&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;09&lt;/span&gt; rows=&lt;span style=&quot;color: #cc66cc;&quot;&gt;26&lt;/span&gt; width=&lt;span style=&quot;color: #cc66cc;&quot;&gt;51&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;-&amp;amp;gt;&amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INDEX&lt;/span&gt; Scan &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;USING&lt;/span&gt; mytable_pk &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ON&lt;/span&gt; mytable&amp;#160; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;cost=&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;00&lt;/span&gt;..&lt;span style=&quot;color: #cc66cc;&quot;&gt;55425&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;06&lt;/span&gt; rows=&lt;span style=&quot;color: #cc66cc;&quot;&gt;41062&lt;/span&gt; width=&lt;span style=&quot;color: #cc66cc;&quot;&gt;51&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INDEX&lt;/span&gt; Cond: &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;id &amp;amp;gt;= &lt;span style=&quot;color: #cc66cc;&quot;&gt;100000&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;-&amp;amp;gt;&amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;LIMIT&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;cost=&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;00&lt;/span&gt;..&lt;span style=&quot;color: #cc66cc;&quot;&gt;17&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;04&lt;/span&gt; rows=&lt;span style=&quot;color: #cc66cc;&quot;&gt;25&lt;/span&gt; width=&lt;span style=&quot;color: #cc66cc;&quot;&gt;51&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;-&amp;amp;gt;&amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INDEX&lt;/span&gt; Scan Backward &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;USING&lt;/span&gt; mytable_pk &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ON&lt;/span&gt; mytable&amp;#160; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;cost=&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;00&lt;/span&gt;..&lt;span style=&quot;color: #cc66cc;&quot;&gt;56090&lt;/span&gt;.&lt;span style=&quot;color: #cc66cc;&quot;&gt;47&lt;/span&gt; rows=&lt;span style=&quot;color: #cc66cc;&quot;&gt;82306&lt;/span&gt; width=&lt;span style=&quot;color: #cc66cc;&quot;&gt;51&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INDEX&lt;/span&gt; Cond: &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;id &amp;amp;lt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;100000&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And yes, the final &lt;strong&gt;ORDER BY&lt;/strong&gt; is still needed if we want the total result to come out in the correct order. With the default query plan, it will come out in the wrong order after the &lt;i&gt;append&lt;/i&gt; node. But it&#039;s important to remember that by the specification the database is free to return the rows in &lt;i&gt;any order it chooses&lt;/i&gt; unless there is an explicit &lt;strong&gt;ORDER BY&lt;/strong&gt; in the query. The rows may otherwise be returned in a completely different order between different runs, depending on the size/width of the table and other parameters.&lt;/p&gt;

 
    </content:encoded>

    <pubDate>Fri, 05 Jun 2009 13:26:00 +0000</pubDate>
    <guid isPermaLink="false">http://blog.hagander.net/archives/147-guid.html</guid>
    <category>limit</category>
<category>postgresql</category>
<category>sql</category>
<category>union</category>

</item>

</channel>
</rss>