<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>SQL on Nalar</title>
    <link>https://nalar.dev/tags/sql/</link>
    <description>Recent content in SQL on Nalar</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 01 Sep 2026 00:00:00 +0700</lastBuildDate>
    <atom:link href="https://nalar.dev/tags/sql/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Keyset Pagination for Stable and Efficient Database Queries</title>
      <link>https://nalar.dev/keyset-pagination-for-stable-database-queries/</link>
      <pubDate>Tue, 01 Sep 2026 00:00:00 +0700</pubDate>
      <guid>https://nalar.dev/keyset-pagination-for-stable-database-queries/</guid>
      <description>&lt;p&gt;Pagination looks straightforward with &lt;code&gt;LIMIT&lt;/code&gt; and &lt;code&gt;OFFSET&lt;/code&gt;, but deep offsets become increasingly expensive and can produce unstable results when rows are inserted or deleted between requests.&lt;/p&gt;&#xA;&lt;p&gt;Keyset pagination, also called seek pagination, uses the last seen sort key as the starting point for the next query.&lt;/p&gt;&#xA;&lt;h2 id=&#34;why-offset-degrades&#34;&gt;Why OFFSET degrades&lt;/h2&gt;&#xA;&lt;p&gt;A typical query is:&lt;/p&gt;&#xA;&lt;div &#xA;    x-data=&#34;{&#xA;        code: $el.querySelector(&#39;code&#39;).innerText&#xA;    }&#34;&#xA;    class=&#34;relative my-4 rounded-lg group&#34;&#xA;&gt;&#xA;    &lt;button&#xA;        @click=&#34;navigator.clipboard.writeText(code); $el.innerText = &#39;Copied!&#39;; setTimeout(() =&gt; $el.innerText = &#39;Copy&#39;, 2000)&#34;&#xA;        class=&#34;absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity&#34;&#xA;    &gt;&#xA;        Copy&#xA;    &lt;/button&gt;&#xA;    &#xA;    &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;SELECT&lt;/span&gt; id, created_at, title&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;FROM&lt;/span&gt; posts&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;ORDER&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;BY&lt;/span&gt; created_at &lt;span style=&#34;color:#66d9ef&#34;&gt;DESC&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;LIMIT&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;50&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;OFFSET&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;100000&lt;/span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#xA;&lt;/div&gt;&#xA;&lt;p&gt;The database still has to find and skip preceding rows before returning the page. There is also a correctness problem: if a new row is inserted at the front between requests, offsets shift and a user may see a duplicate or miss an item.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Keyset Pagination in SQL for Fast, Stable APIs</title>
      <link>https://nalar.dev/keyset-pagination-sql/</link>
      <pubDate>Tue, 01 Sep 2026 00:00:00 +0700</pubDate>
      <guid>https://nalar.dev/keyset-pagination-sql/</guid>
      <description>&lt;p&gt;Pagination looks simple until a table becomes large or new rows are inserted while a client is paging through results. &lt;code&gt;LIMIT&lt;/code&gt; and &lt;code&gt;OFFSET&lt;/code&gt; are easy to understand, but deep offsets can become expensive and changing data can make rows appear twice or disappear between requests.&lt;/p&gt;&#xA;&lt;p&gt;Keyset pagination, also called seek pagination, avoids those problems by asking for rows after a known position instead of asking the database to skip a number of rows.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Safe Database Migrations with the Expand-and-Contract Pattern</title>
      <link>https://nalar.dev/safe-database-migrations-expand-contract/</link>
      <pubDate>Tue, 01 Sep 2026 00:00:00 +0700</pubDate>
      <guid>https://nalar.dev/safe-database-migrations-expand-contract/</guid>
      <description>&lt;p&gt;A schema migration can be syntactically correct and still cause an outage. Production deployments often run old and new application versions at the same time, background workers may lag behind, and a large table can turn a simple-looking DDL statement into a long lock.&lt;/p&gt;&#xA;&lt;p&gt;The expand-and-contract pattern reduces those risks by splitting an incompatible change into compatible stages.&lt;/p&gt;&#xA;&lt;h2 id=&#34;why-one-step-schema-changes-are-risky&#34;&gt;Why one-step schema changes are risky&lt;/h2&gt;&#xA;&lt;p&gt;Suppose an application wants to rename &lt;code&gt;users.full_name&lt;/code&gt; to &lt;code&gt;users.display_name&lt;/code&gt;.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
