<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>databases on Saurabh Kumar</title><link>https://saurabh-kumar.com/tags/databases/</link><description>Recent content in databases on Saurabh Kumar</description><generator>Hugo</generator><language>en-US</language><copyright>Copyright © 2025, Saurabh Kumar.</copyright><lastBuildDate>Wed, 30 Sep 2026 12:00:00 +0000</lastBuildDate><atom:link href="https://saurabh-kumar.com/tags/databases/index.xml" rel="self" type="application/rss+xml"/><item><title>django-ox</title><link>https://saurabh-kumar.com/til/1790769600/</link><pubDate>Wed, 30 Sep 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1790769600/</guid><description>&lt;p&gt;&lt;a href="https://oxpull.com/django-ox/" rel="noopener external"&gt;django-ox&lt;/a&gt; makes the database the Django task queue: transactional enqueue inside &lt;code&gt;atomic()&lt;/code&gt;, no Redis/RabbitMQ, retries, worker-death recovery, recurring schedules, admin and metrics. Adoption read: pilot it for ordinary Django jobs; keep Celery for broker-scale workflows.&lt;/p&gt;&#10;</description></item><item><title>jetify-com/typeid</title><link>https://saurabh-kumar.com/til/1790424000/</link><pubDate>Sat, 26 Sep 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1790424000/</guid><description>&lt;p&gt;&lt;a href="https://github.com/jetify-com/typeid" rel="noopener external"&gt;jetify-com/typeid&lt;/a&gt; is a spec for Stripe-style IDs: &lt;code&gt;user_01h2xcejqtf2nbrexx3vqjhp41&lt;/code&gt; is a snake_case type prefix, &lt;code&gt;_&lt;/code&gt;, then a UUIDv7 in 26 chars of Crockford base32 (hex UUIDs take 36). Time-ordered, so B-tree inserts stay local, unlike UUIDv4. Double-click selects the whole ID, and a serializer expecting &lt;code&gt;user_&lt;/code&gt; rejects a pasted &lt;code&gt;post_&lt;/code&gt; ID. Strip the prefix and you get a valid UUIDv7 back, so the database never has to know. Cheap way to adopt: keep the column a native 16-byte &lt;code&gt;uuid&lt;/code&gt; (&lt;code&gt;DEFAULT uuidv7()&lt;/code&gt; on Postgres 18, &lt;code&gt;uuid.uuid7()&lt;/code&gt; on Python 3.14) and encode/decode only at the API edge, e.g. a DRF serializer field on top of &lt;a href="https://github.com/akhundMurad/typeid-python" rel="noopener external"&gt;typeid-python&lt;/a&gt;. If PKs are already &lt;code&gt;uuid&lt;/code&gt;, no schema change, and IDs explain themselves in logs and support tickets.&lt;/p&gt;&#10;</description></item><item><title>Lobste.rs moved to SQLite</title><link>https://saurabh-kumar.com/til/1784376000/</link><pubDate>Sat, 18 Jul 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1784376000/</guid><description>&lt;p&gt;&lt;a href="https://lobste.rs/s/ko1ji1" rel="noopener external"&gt;Lobste.rs moved to SQLite&lt;/a&gt;, and the &lt;a href="https://news.ycombinator.com/item?id=48899847" rel="noopener external"&gt;HN thread&lt;/a&gt; had sharper lessons than the post itself:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;WAL checkpoints only run during &amp;ldquo;reader gaps.&amp;rdquo; Keep reads/writes continuously overlapping and the WAL file grows unbounded, a DoS vector kgeist reproduced with short-lived reads/writes, not just long-lived readers as the docs imply.&lt;/li&gt;&#10;&lt;li&gt;Writes are fully serialized, one fsync each, yet app-layer batching still gets 100k+ writes/sec.&lt;/li&gt;&#10;&lt;li&gt;Plain rsync on a live .db file can silently corrupt it since it doesn&amp;rsquo;t understand SQLite transactions. Use &lt;a href="https://sqlite.org/rsync.html" rel="noopener external"&gt;sqlite3_rsync&lt;/a&gt; instead.&lt;/li&gt;&#10;&lt;li&gt;The real weak point is schema migrations: treat SQLite like a KV store, or reach for CQRS, to keep the churn low.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>PostgreSQL UNLOGGED tables as a cache</title><link>https://saurabh-kumar.com/til/1782648000/</link><pubDate>Sun, 28 Jun 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1782648000/</guid><description>&lt;p&gt;PostgreSQL &lt;code&gt;UNLOGGED&lt;/code&gt; tables skip the write-ahead log, making writes considerably faster than ordinary tables. Trade-off: they are truncated after a crash or unclean shutdown and are not replicated to standbys. &lt;a href="https://github.com/hynek/psycache" rel="noopener external"&gt;hynek/psycache&lt;/a&gt; exploits this as a PostgreSQL-native cache replacing Redis — crash truncation is acceptable since it&amp;rsquo;s a cache anyway, and you reuse existing Postgres connections.&#10;Interesting idea: port this to Django as a custom &lt;code&gt;BaseCache&lt;/code&gt; backend — swap &lt;code&gt;DatabaseCache&lt;/code&gt;&amp;rsquo;s regular table for &lt;code&gt;CREATE UNLOGGED TABLE&lt;/code&gt;, reuse Django&amp;rsquo;s existing DB connections, and skip Redis entirely. ~200 lines.&lt;/p&gt;&#10;</description></item><item><title>Django ORM: From sync_to_async Threads to Native psycopg3</title><link>https://saurabh-kumar.com/articles/2026/06/django-orm-from-sync_to_async-threads-to-native-psycopg3/</link><pubDate>Sat, 13 Jun 2026 00:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/articles/2026/06/django-orm-from-sync_to_async-threads-to-native-psycopg3/</guid><description>&lt;p&gt;Django shipped async ORM methods back in 3.1. The &lt;code&gt;a&lt;/code&gt;-prefixed methods, including&#10;&lt;code&gt;aget()&lt;/code&gt;, &lt;code&gt;afilter()&lt;/code&gt;, and &lt;code&gt;acreate()&lt;/code&gt;, let calling code use coroutines, but the&#10;database work still blocked an OS thread. I want to explain that distinction&#10;and how the execution path changed.&lt;/p&gt;&#10;&lt;p&gt;Django 6.0 fixed it for real in December 2025. I want to walk through the path&#10;from &amp;ldquo;async via threads&amp;rdquo; to &amp;ldquo;native async via psycopg3,&amp;rdquo; because it is not&#10;a story about a feature landing. It is a story about three constraints that&#10;boxed Django in for five years: &lt;code&gt;psycopg2&lt;/code&gt; blocks, connection state lived in&#10;thread-locals, and the entire ORM assumed DB-API2. Once you see those three, every&#10;design decision Django made falls out of them almost mechanically.&lt;/p&gt;&#10;&lt;h2 id="the-thread-based-async-path"&gt;The Thread-Based Async Path&lt;/h2&gt;&#10;&lt;p&gt;When async views arrived in 3.1, the ORM underneath them was entirely synchronous.&#10;&lt;code&gt;psycopg2&lt;/code&gt; — the default PostgreSQL driver for most of Django&amp;rsquo;s life — wraps&#10;&lt;code&gt;libpq&lt;/code&gt;, a C library with no async I/O surface at all. There is no flag you set to&#10;make &lt;code&gt;psycopg2&lt;/code&gt; async. You either replace the driver or you find a workaround.&lt;/p&gt;&#10;&lt;p&gt;Django found the workaround: &lt;code&gt;asgiref.sync.sync_to_async&lt;/code&gt;.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Simplified — what aget() actually did under the hood in Django 5.x&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;asgiref.sync&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sync_to_async&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;aget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;sync_to_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread_sensitive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;sync_to_async&lt;/code&gt; pushes the callable into a &lt;code&gt;ThreadPoolExecutor&lt;/code&gt;. The event loop is&#10;not blocked, because the coroutine awaits the thread&amp;rsquo;s future. But a real OS thread&#10;is blocked, sitting on a TCP socket waiting for Postgres to answer. You moved the&#10;blocking, you did not remove it.&lt;/p&gt;&#10;&lt;p&gt;The &lt;code&gt;thread_sensitive=True&lt;/code&gt; flag is the part that gives the game away. Django&amp;rsquo;s&#10;database connections lived in thread-locals:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# django/db/backends/base/base.py (pre-6.0)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;threading&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;_thread_local&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without &lt;code&gt;thread_sensitive=True&lt;/code&gt;, two consecutive &lt;code&gt;sync_to_async&lt;/code&gt; calls from the same&#10;request might land on different threads, see different thread-locals, and tear your&#10;transaction state in half. The flag pins every call within the same coroutine&#10;context to one dedicated thread, so the connection still looks like a single&#10;connection per request. This preserves the connection’s thread affinity.&lt;/p&gt;&#10;&lt;p&gt;Put it together and the cost is obvious. Five hundred concurrent async requests, each&#10;touching the database, need five hundred threads. That is the thread-per-connection&#10;model, even though the calling code uses coroutines.&lt;/p&gt;&#10;&lt;h2 id="why-psycopg2-was-a-dead-end"&gt;Why psycopg2 Was a Dead End&lt;/h2&gt;&#10;&lt;p&gt;You cannot retrofit async I/O onto &lt;code&gt;psycopg2&lt;/code&gt;. It calls into &lt;code&gt;libpq&lt;/code&gt; synchronously&#10;and blocks the calling thread until the query returns. The only way to run it in an&#10;async context is exactly what Django did: park it in a thread and await that thread.&#10;There was never a clever escape here.&lt;/p&gt;&#10;&lt;p&gt;The obvious alternative was &lt;code&gt;asyncpg&lt;/code&gt;, a fully async PostgreSQL driver that beats&#10;psycopg3 on raw throughput. Django ruled it out, and the reasons are worth their own&#10;section further down. The short version is that &lt;code&gt;asyncpg&lt;/code&gt; does not implement DB-API2&#10;(PEP 249)&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;, and Django&amp;rsquo;s ORM is built on DB-API2 from the ground up — cursor&#10;objects, &lt;code&gt;execute()&lt;/code&gt;, &lt;code&gt;fetchone()&lt;/code&gt;, &lt;code&gt;fetchall()&lt;/code&gt;. You do not bolt a different&#10;contract onto twenty years of ORM and call it a driver swap.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;psycopg3&lt;/code&gt; is the answer that fits, because it does not force that choice.&lt;/p&gt;&#10;&lt;h2 id="psycopg3-does-async-and-db-api2-at-the-same-time"&gt;psycopg3 Does Async And DB-API2 At The Same Time&lt;/h2&gt;&#10;&lt;p&gt;&lt;code&gt;psycopg3&lt;/code&gt; — the package is just &lt;code&gt;psycopg&lt;/code&gt;, not &lt;code&gt;psycopg3&lt;/code&gt; — was redesigned to support&#10;both synchronous and asynchronous execution through one shared core&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;. The same query&#10;compilation logic runs both paths. What changes is the connection and cursor type you&#10;hand it.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# psycopg3 — synchronous path&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;SELECT * FROM posts WHERE id = &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# psycopg3 — async path (same SQL, same parameters, different objects)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsyncConnection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;aconn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;aconn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;SELECT * FROM posts WHERE id = &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;AsyncConnection&lt;/code&gt; and &lt;code&gt;AsyncCursor&lt;/code&gt; use &lt;code&gt;asyncio&lt;/code&gt; directly. The &lt;code&gt;await cur.execute(...)&lt;/code&gt;&#10;suspends the coroutine and releases the event loop while the network I/O happens. No&#10;thread sits idle on a socket. That is the whole difference, and it is the difference&#10;that matters.&lt;/p&gt;&#10;&lt;p&gt;Django 6.0 made psycopg3 the recommended PostgreSQL driver and wired&#10;&lt;code&gt;AsyncConnection&lt;/code&gt; and &lt;code&gt;AsyncCursor&lt;/code&gt; into the backend.&lt;/p&gt;&#10;&lt;h2 id="what-django-60-actually-changed"&gt;What Django 6.0 Actually Changed&lt;/h2&gt;&#10;&lt;p&gt;Three refactors enabled the switch. None of them are glamorous, and that is the point.&lt;/p&gt;&#10;&lt;h3 id="1-contextvar-replaces-thread-locals"&gt;1. ContextVar Replaces Thread-Locals&lt;/h3&gt;&#10;&lt;p&gt;Thread-locals are indexed by thread ID. Coroutines do not have thread IDs of their&#10;own — they share threads and switch between each other at &lt;code&gt;await&lt;/code&gt; points. So&#10;thread-local connection storage is not merely awkward under async, it is structurally&#10;wrong. Think of a thread-local as a coat hook bolted to a single chair: it works only&#10;as long as one person stays in that chair the whole time. Coroutines stand up and swap&#10;chairs constantly.&lt;/p&gt;&#10;&lt;p&gt;The fix is &lt;code&gt;contextvars.ContextVar&lt;/code&gt;, which arrived in Python 3.7. A &lt;code&gt;ContextVar&lt;/code&gt; is&#10;scoped to the current execution context, and each asyncio &lt;code&gt;Task&lt;/code&gt; gets its own context,&#10;copied from its parent when it is created. Django 6.0 moved connection storage off&#10;&lt;code&gt;threading.local()&lt;/code&gt; and onto a &lt;code&gt;ContextVar&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# django/db/backends/base/base.py (Django 6.0)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;contextvars&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ContextVar&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;_connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ContextVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ContextVar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;django_db_connection&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Two concurrent requests, both awaiting a query, each live in their own asyncio Task,&#10;each with their own context copy, each seeing their own connection. No locking, no&#10;thread affinity, no &lt;code&gt;thread_sensitive&lt;/code&gt; gymnastics. The coat hook now travels with the&#10;person.&lt;/p&gt;&#10;&lt;h3 id="2-databasewrapper-gets-an-async-connection-path"&gt;2. DatabaseWrapper Gets an Async Connection Path&lt;/h3&gt;&#10;&lt;p&gt;&lt;code&gt;DatabaseWrapper&lt;/code&gt; is the Django object that represents a database connection. Before&#10;6.0 it exposed one path, &lt;code&gt;self.connection&lt;/code&gt;, a synchronous psycopg2 connection. After&#10;6.0 it also exposes &lt;code&gt;async_connection()&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Conceptual — the async context manager on DatabaseWrapper&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nd"&gt;@asynccontextmanager&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsyncConnection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_connection_params&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;_connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Django&amp;rsquo;s connection-handling code detects whether it is running inside an asyncio event&#10;loop and routes to the right path. You do not pick the path. The runtime does.&lt;/p&gt;&#10;&lt;h3 id="3-sqlcompiler-gets-async_execute"&gt;3. SQLCompiler Gets async_execute&lt;/h3&gt;&#10;&lt;p&gt;The query compiler, &lt;code&gt;SQLCompiler&lt;/code&gt; and its subclasses, is where Django turns a &lt;code&gt;QuerySet&lt;/code&gt;&#10;into SQL and sends it to the database. Before 6.0 there was one path, &lt;code&gt;execute_sql()&lt;/code&gt;.&#10;Django 6.0 adds &lt;code&gt;async_execute_sql()&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_execute_sql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;MULTI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunked_fetch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;GET_ITERATOR_CHUNK_SIZE&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_connection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;as_sql&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result_type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;MULTI&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;aget()&lt;/code&gt;, &lt;code&gt;afilter()&lt;/code&gt;, &lt;code&gt;aiterator()&lt;/code&gt; and the rest now route through &lt;code&gt;async_execute_sql()&lt;/code&gt;&#10;instead of wrapping &lt;code&gt;execute_sql()&lt;/code&gt; in &lt;code&gt;sync_to_async&lt;/code&gt;. The thread pool drops out of the&#10;picture entirely.&lt;/p&gt;&#10;&lt;h2 id="the-new-execution-stack"&gt;The New Execution Stack&lt;/h2&gt;&#10;&lt;p&gt;Here is how &lt;code&gt;await Post.objects.aget(id=1)&lt;/code&gt; flows through Django 6.0:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;await Post.objects.aget(id=1)&#10; └── QuerySet.aget()&#10; └── SQLCompiler.async_execute_sql()&#10; └── DatabaseWrapper.async_connection() # ContextVar-scoped&#10; └── psycopg.AsyncConnection&#10; └── AsyncCursor.execute()&#10; └── asyncio TCP socket write&#10; └── await socket read ← event loop released here&#10; └── row data returned&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And here is the same call under Django 5.x:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;await Post.objects.aget(id=1)&#10; └── QuerySet.aget()&#10; └── sync_to_async(thread_sensitive=True)(QuerySet.get)&#10; └── ThreadPoolExecutor — OS thread blocked&#10; └── SQLCompiler.execute_sql()&#10; └── psycopg2 connection (thread-local)&#10; └── libpq blocking TCP call&#10; └── thread wakes, result returned&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The event loop is no longer handing work off to a thread. It suspends the coroutine,&#10;does other work, and resumes when the socket is readable. That is the entire reform,&#10;and it is enough.&lt;/p&gt;&#10;&lt;h2 id="what-this-means-for-concurrency"&gt;What This Means For Concurrency&lt;/h2&gt;&#10;&lt;p&gt;The practical payoff is in connection count. With the old thread approach, five hundred&#10;concurrent async requests meant up to five hundred threads and up to five hundred&#10;Postgres connections. Threads are cheap but not free — each costs roughly 8MB of stack&#10;by default, plus scheduler overhead.&lt;/p&gt;&#10;&lt;p&gt;With native async, those same five hundred requests can be multiplexed over a&#10;connection pool sized to what your database can actually handle, typically 10 to 50&#10;connections, with the event loop running the wait queue. You use far fewer threads,&#10;one or a handful for the event loop workers, and far fewer database connections.&lt;/p&gt;&#10;&lt;p&gt;Django 6.0 also ships native connection pooling for the async path&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;, built on&#10;&lt;code&gt;psycopg_pool.AsyncConnectionPool&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# settings.py — async pooling in Django 6.0&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;default&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ENGINE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;django.db.backends.postgresql&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;OPTIONS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;pool&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;min_size&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_size&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;CONN_MAX_AGE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is the same &lt;code&gt;pool&lt;/code&gt; option I covered in &lt;a href="https://saurabh-kumar.com/articles/2025/06/django-native-postgresql-connection-pooling/"&gt;Django Native PostgreSQL Connection Pooling&lt;/a&gt;, with one&#10;difference that matters. In that post I warned against native pooling under ASGI —&#10;Django&amp;rsquo;s own docs recommended PgBouncer instead — because the sync pool and the event&#10;loop did not cooperate. Django 6.0&amp;rsquo;s &lt;code&gt;AsyncConnectionPool&lt;/code&gt; is what closes that gap. The&#10;pool itself is now async-aware, so the ASGI caveat no longer applies on the native&#10;psycopg3 path. I was right to warn you then. You can stop worrying about it now.&lt;/p&gt;&#10;&lt;h2 id="is-sync_to_async-gone"&gt;Is sync_to_async Gone?&lt;/h2&gt;&#10;&lt;p&gt;No, and it should not be. It is the fallback for drivers that cannot go native async:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Driver&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Async path&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;psycopg3 (PostgreSQL)&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Native &lt;code&gt;AsyncCursor&lt;/code&gt; — no threads&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;psycopg2 (PostgreSQL)&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;sync_to_async&lt;/code&gt; thread wrap — still blocked&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;mysqlclient / MySQL&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;sync_to_async&lt;/code&gt; thread wrap&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;sqlite3&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;sync_to_async&lt;/code&gt; thread wrap&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;Run Django 6.0 against &lt;code&gt;psycopg2&lt;/code&gt; or MySQL in an async context and Django still falls&#10;back to the thread pool. The native path only exists when psycopg3 is installed and&#10;configured. The thread trick did not become wrong. It became the path of last resort.&lt;/p&gt;&#10;&lt;p&gt;The way forward for other databases is psycopg3-equivalent async drivers. MySQL has&#10;&lt;code&gt;aiomysql&lt;/code&gt;, SQLite has &lt;code&gt;aiosqlite&lt;/code&gt;. Django contributions for those backends are in&#10;progress but not stable in 6.0, so for now PostgreSQL is the database that gets the&#10;real thing.&lt;/p&gt;&#10;&lt;h2 id="asyncpg-is-still-faster-and-still-not-supported"&gt;asyncpg Is Still Faster, And Still Not Supported&lt;/h2&gt;&#10;&lt;p&gt;Benchmark &lt;code&gt;asyncpg&lt;/code&gt; against psycopg3 on raw async throughput and asyncpg wins, often&#10;by 20 to 40 percent on bulk read workloads. Django will not adopt it. The reasons are&#10;structural, not political, and I think Django is right.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;No DB-API2.&lt;/strong&gt; asyncpg uses positional &lt;code&gt;$1, $2&lt;/code&gt; parameters, not &lt;code&gt;%s&lt;/code&gt;. The entire&#10;ORM query compiler assumes &lt;code&gt;%s&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;No synchronous fallback.&lt;/strong&gt; Management commands, migrations, and sync views would&#10;need a whole separate code path.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;A different transaction model.&lt;/strong&gt; asyncpg handles transactions differently from the&#10;DB-API2 autocommit and explicit model Django manages internally.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Using asyncpg would require changes to the ORM, not just a driver swap.&#10;A driver benchmark alone does not measure that work. If you genuinely need asyncpg throughput with a Django-like&#10;ORM, look at SQLAlchemy 2.0 with its async session support and asyncpg backend, running&#10;under Starlette or FastAPI. For Django, psycopg3 is the correct answer.&lt;/p&gt;&#10;&lt;h2 id="upgrading"&gt;Upgrading&lt;/h2&gt;&#10;&lt;p&gt;To get the native async path today:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;pip install &lt;span class="s2"&gt;&amp;#34;psycopg[binary,pool]&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# settings.py&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;default&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ENGINE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;django.db.backends.postgresql&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;NAME&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;mydb&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;USER&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;myuser&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PASSWORD&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;mypassword&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;HOST&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;localhost&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;CONN_MAX_AGE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;OPTIONS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;pool&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# or dict with min_size/max_size&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you are on Django 5.2 LTS with psycopg3, you already get the synchronous psycopg3&#10;path and connection pooling. The native async query path needs Django 6.0. You can&#10;upgrade the driver without upgrading Django and still get the pooling benefits in sync&#10;contexts, which is a fine thing to do while you wait.&lt;/p&gt;&#10;&lt;h2 id="why-it-took-five-years"&gt;Why It Took Five Years&lt;/h2&gt;&#10;&lt;p&gt;Django&amp;rsquo;s async ORM story ran from &amp;ldquo;async with threads&amp;rdquo; in 3.1 in 2020 to &amp;ldquo;native&#10;async with psycopg3&amp;rdquo; in 6.0 in 2025. Five years sounds slow until you look at what had&#10;to be true first:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;code&gt;psycopg3&lt;/code&gt; had to be stable enough to depend on, which it reached around 2022 to 2023.&lt;/li&gt;&#10;&lt;li&gt;Thread-local connection state was woven through the codebase, and the ContextVar&#10;migration touched hundreds of files.&lt;/li&gt;&#10;&lt;li&gt;The Django project is conservative about breaking changes, especially in database&#10;handling, where bugs are not cosmetic.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The result is correct and maintainable rather than the absolute fastest thing money can&#10;buy, and I do not think that was the wrong trade. For most Django applications — web&#10;services handling thousands of concurrent HTTP requests — native async with psycopg3 is&#10;already more than fast enough, and it integrates cleanly with everything else in the&#10;ecosystem.&lt;/p&gt;&#10;&lt;h2 id="related-reading"&gt;Related reading&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://saurabh-kumar.com/articles/2025/06/django-native-postgresql-connection-pooling/"&gt;Django Native PostgreSQL Connection Pooling&lt;/a&gt; — the sync side of the same story: configuring the psycopg3 pool, sizing &lt;code&gt;min_size&lt;/code&gt;/&lt;code&gt;max_size&lt;/code&gt;, and avoiding the &lt;code&gt;CONN_MAX_AGE&lt;/code&gt; foot-gun.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="footnotes" role="doc-endnotes"&gt;&#10;&lt;hr&gt;&#10;&lt;ol&gt;&#10;&lt;li id="fn:1"&gt;&#10;&lt;p&gt;&lt;a href="https://peps.python.org/pep-0249/" rel="noopener external"&gt;PEP 249 — Python Database API Specification v2.0&lt;/a&gt; — The DB-API2 spec that Django&amp;rsquo;s ORM is built on top of; asyncpg deliberately does not implement it.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:2"&gt;&#10;&lt;p&gt;&lt;a href="https://www.psycopg.org/psycopg3/docs/advanced/async.html" rel="noopener external"&gt;psycopg3 — Async operations&lt;/a&gt; — Official psycopg documentation on the async connection and cursor API.&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:3"&gt;&#10;&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/6.0/releases/6.0/" rel="noopener external"&gt;Django 6.0 Release Notes&lt;/a&gt; — Full changelog including async ORM changes, background tasks, and template partials.&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>pageindex</title><link>https://saurabh-kumar.com/til/1774353600/</link><pubDate>Tue, 24 Mar 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1774353600/</guid><description>&lt;p&gt;&lt;a href="https://github.com/VectifyAI/PageIndex/" rel="noopener external"&gt;pageindex&lt;/a&gt; generates a semantic tree-like json index of a lengthy document to allow for reasoning based RAG without the need for vectordb.&lt;/p&gt;&#10;</description></item><item><title>slingdata-io/sling-cli</title><link>https://saurabh-kumar.com/til/1771502400/</link><pubDate>Thu, 19 Feb 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1771502400/</guid><description>&lt;p&gt;&lt;a href="https://github.com/slingdata-io/sling-cli" rel="noopener external"&gt;slingdata-io/sling-cli&lt;/a&gt; is a promising tool move/sync data between databases and files, esp. helpful for local testing, ci/cd while able to do stage/sql based transformations.&lt;/p&gt;&#10;</description></item><item><title>alibaba/zvec</title><link>https://saurabh-kumar.com/til/1771156802/</link><pubDate>Sun, 15 Feb 2026 12:00:02 +0000</pubDate><guid>https://saurabh-kumar.com/til/1771156802/</guid><description>&lt;p&gt;&lt;a href="https://github.com/alibaba/zvec" rel="noopener external"&gt;alibaba/zvec&lt;/a&gt; by alibaba is an&#10;embedded vector database supports both spare and dense vectors, along with&#10;structured data. It can be considered the sqlite of vector databases.&lt;/p&gt;&#10;</description></item><item><title>zoocache</title><link>https://saurabh-kumar.com/til/1770984000/</link><pubDate>Fri, 13 Feb 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1770984000/</guid><description>&lt;p&gt;&lt;a href="https://github.com/albertobadia/zoocache" rel="noopener external"&gt;zoocache&lt;/a&gt; is a sematic dependency based cache manager, that support in-memory, LMDB or redis backends. Integration with &lt;a href="https://github.com/albertobadia/zoocache/blob/main/docs/django_user_guide.md" rel="noopener external"&gt;Django&lt;/a&gt; looks interesting.&lt;/p&gt;&#10;</description></item><item><title>Vortex</title><link>https://saurabh-kumar.com/til/1770292801/</link><pubDate>Thu, 05 Feb 2026 12:00:01 +0000</pubDate><guid>https://saurabh-kumar.com/til/1770292801/</guid><description>&lt;p&gt;&lt;a href="https://vortex.dev/" rel="noopener external"&gt;Vortex&lt;/a&gt; a newer format is supported by &lt;a href="https://duckdb.org/2026/01/23/duckdb-vortex-extension" rel="noopener external"&gt;duckdb&lt;/a&gt; promises to be faster for random access and has zero-copy metadata, with better compression. While the support across the board is limited but worth considering for LLM/RAG based uses over parquet.&lt;/p&gt;&#10;</description></item><item><title>Designing Data-Intensive Applications</title><link>https://saurabh-kumar.com/til/1770292800/</link><pubDate>Thu, 05 Feb 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1770292800/</guid><description>&lt;p&gt;&lt;strong&gt;Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems&lt;/strong&gt; by Martin Kleppmann manages to be both rigorous technical manual and something approaching a philosophical treatise on the nature of truth, consistency, and trust in distributed systems. [Claude][https://claude.ai/chat/0f58f2f6-bd56-41a1-a785-8267afa5a3d1]&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Foundation. He starts by asking the questions &lt;em&gt;&amp;ldquo;What do we actually want in our data systems&amp;rdquo;&lt;/em&gt;, answers them as - reliability, scalability and maintainability.&#10;&lt;ul&gt;&#10;&lt;li&gt;Reliability. The question isn&amp;rsquo;t whether failure will happen, it will, but the whether the systems can survive them.&lt;/li&gt;&#10;&lt;li&gt;Scalability. It&amp;rsquo;s not a binary question of whether a system is &amp;ldquo;scalable&amp;rdquo; or &amp;ldquo;not scalable&amp;rdquo;. Ask - &amp;ldquo;What happens when specific load parameter increase?&amp;rdquo;&lt;/li&gt;&#10;&lt;li&gt;Maintainability - most underappreciated of the three, he argues the majority of cost isn&amp;rsquo;t in initial development but in ongoing maintainence.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;The data model wars - the skill isn&amp;rsquo;t in choosing the &amp;ldquo;best&amp;rdquo; database but in understanding which tradeoffs matter for your specific problem.&lt;/li&gt;&#10;&lt;li&gt;Storage engines. Two major approaches to read &amp;amp; write data from disk, neither is universally better.&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Log-structured storage&lt;/strong&gt; (like LSM-trees) optimizes for writes, every write is appended and the merged/compacted later.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Page-oriented storage&lt;/strong&gt; (like B-trees) optimizes for reads, data is stored in fixed-sized block, which then get updated in-place, more like filing cabinet with each document has a designated slot.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;Instead of asking &amp;ldquo;how do I build this?&amp;rdquo; ask &amp;ldquo;what does it mean for this to work correctly?&amp;rdquo;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>etcd, a distributed key-value store</title><link>https://saurabh-kumar.com/til/1768219202/</link><pubDate>Mon, 12 Jan 2026 12:00:02 +0000</pubDate><guid>https://saurabh-kumar.com/til/1768219202/</guid><description>&lt;p&gt;Distributed Key-value store &amp;ndash; &lt;a href="https://etcd.io/" rel="noopener external"&gt;https://etcd.io/&lt;/a&gt;&lt;/p&gt;&#10;</description></item><item><title>Redis’s License Change and the Valkey Fork</title><link>https://saurabh-kumar.com/articles/2026/01/rediss-license-change-and-the-valkey-fork/</link><pubDate>Mon, 12 Jan 2026 00:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/articles/2026/01/rediss-license-change-and-the-valkey-fork/</guid><description>&lt;h2 id="the-license-change"&gt;The License Change&lt;/h2&gt;&#10;&lt;p&gt;On March 20, 2024, Redis Inc. announced a switch from the BSD 3-clause license to dual source-available licensing: RSALv2 and SSPLv1. The new licensing takes effect with Redis 7.4 and restricts how the code can be offered as a service.&lt;/p&gt;&#10;&lt;p&gt;I see this as a loss for the open-source community, even if Redis Inc. considers it a business necessity.&lt;/p&gt;&#10;&lt;h2 id="the-thing-about-open-source"&gt;The Thing About Open Source&lt;/h2&gt;&#10;&lt;p&gt;Redis was created by Salvatore Sanfilippo in 2009, known as &amp;ldquo;antirez&amp;rdquo; in the community. He built Redis to solve a simple problem: web applications were slow because databases couldn&amp;rsquo;t keep up. What if, instead of storing data on disk, you kept everything in memory? What if you made it blazingly fast?&lt;/p&gt;&#10;&lt;p&gt;Redis became legendary. By 2024, it was everywhere—caching layers for social media platforms, session stores for e-commerce sites, real-time analytics for financial systems. Developers loved it. Not just because it was fast, but because it was &lt;em&gt;theirs&lt;/em&gt;. They could use it, modify it, deploy it however they wanted.&lt;/p&gt;&#10;&lt;p&gt;Then Redis Inc. changed the rules.&lt;/p&gt;&#10;&lt;h2 id="the-rebellion"&gt;The Rebellion&lt;/h2&gt;&#10;&lt;p&gt;Kyle Davis, a longtime Redis contributor, said something interesting after the split: &amp;ldquo;From this point forward, Redis and Valkey are two different pieces of software.&amp;rdquo; Not just different licenses—different software.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Just eight days later, on March 28, 2024&lt;/strong&gt;, something remarkable happened. The Linux Foundation announced Valkey, a fork starting from Redis 7.2.4, the last truly open version. But this wasn&amp;rsquo;t some ragtag group of angry developers. This was AWS, Google Cloud, Oracle, Ericsson, and Snap Inc.—companies with deep pockets and deeper technical expertise.&lt;/p&gt;&#10;&lt;p&gt;They weren&amp;rsquo;t just copying Redis and giving it a new name. They were going to &lt;em&gt;improve&lt;/em&gt; it.&lt;/p&gt;&#10;&lt;h2 id="the-performance-revolution"&gt;The Performance Revolution&lt;/h2&gt;&#10;&lt;p&gt;Here&amp;rsquo;s where the story gets interesting, because it stops being about philosophy and starts being about engineering.&lt;/p&gt;&#10;&lt;p&gt;Redis, for all its speed, had a design constraint: it was single-threaded. Salvatore made this choice deliberately. Single-threading avoids all the complexity of concurrent programming—no locks, no race conditions, no debugging nightmares. For years, this was fine. Processors were getting faster, and Redis was fast enough.&lt;/p&gt;&#10;&lt;p&gt;But by 2024, we&amp;rsquo;re not making processors faster anymore—we&amp;rsquo;re making them wider. Your laptop has eight cores, maybe sixteen. Your cloud server has dozens. And Redis, with its elegant single-threaded design, was using&amp;hellip; one.&lt;/p&gt;&#10;&lt;p&gt;The Valkey team saw their opportunity.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Valkey 8.0, released on September 15, 2024&lt;/strong&gt;, introduced enhanced multi-threaded I/O. Not for the core data operations—those stayed single-threaded to preserve Redis&amp;rsquo;s simplicity and safety—but for network operations, for handling connections, for moving data in and out.&lt;/p&gt;&#10;&lt;p&gt;The results were dramatic. In benchmarks on AWS hardware, Valkey 8.0 hit 1.19 million requests per second. That&amp;rsquo;s 230% more than Valkey 7.2. That&amp;rsquo;s the kind of improvement that makes CTOs take notice.&lt;/p&gt;&#10;&lt;p&gt;But the engineers didn&amp;rsquo;t stop there.&lt;/p&gt;&#10;&lt;h2 id="the-memory-game"&gt;The Memory Game&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;Valkey 8.1, released on March 31, 2025&lt;/strong&gt;, arrived with something even more impressive: a complete redesign of how data is stored in memory.&lt;/p&gt;&#10;&lt;p&gt;The team rebuilt the hash table—the fundamental data structure at the heart of any key-value store—using modern algorithms inspired by Google&amp;rsquo;s &amp;ldquo;Swiss Tables&amp;rdquo; design. Every key-value pair now uses about 20-30 bytes less memory. That might not sound like much, but scale it up.&lt;/p&gt;&#10;&lt;p&gt;When researchers tested Valkey 8.1 against Redis 8.2 with 50 million entries, Valkey consumed 3.77 GB while Redis used 4.83 GB. That&amp;rsquo;s a 28% reduction. For a company running terabytes of cached data across hundreds of servers, that&amp;rsquo;s not just impressive—it&amp;rsquo;s millions of dollars in annual savings.&lt;/p&gt;&#10;&lt;h2 id="the-feature-race"&gt;The Feature Race&lt;/h2&gt;&#10;&lt;p&gt;Of course, efficiency isn&amp;rsquo;t everything. Redis Inc. didn&amp;rsquo;t just sit back and watch. &lt;strong&gt;In May 2025, Redis released version 8.0&lt;/strong&gt;, bundling their enterprise features—JSON support, vector search for AI applications, time series databases—directly into the core product. These are sophisticated capabilities that took years to develop.&lt;/p&gt;&#10;&lt;p&gt;Valkey&amp;rsquo;s roadmap is playing catch-up here. Version 8.1 added JSON and Bloom filters. Vector search support became available through the valkey-search module. Time series support is planned. But there&amp;rsquo;s still a gap.&lt;/p&gt;&#10;&lt;p&gt;This is the classic innovator&amp;rsquo;s dilemma: Redis has more features &lt;em&gt;today&lt;/em&gt;. Valkey has better fundamentals and a trajectory that suggests they&amp;rsquo;ll have those features &lt;em&gt;tomorrow&lt;/em&gt;—and they&amp;rsquo;ll run faster when they do.&lt;/p&gt;&#10;&lt;h2 id="the-cloud-complication"&gt;The Cloud Complication&lt;/h2&gt;&#10;&lt;p&gt;Here&amp;rsquo;s where it gets messy. When you go to AWS and spin up what was formerly called &amp;ldquo;Redis,&amp;rdquo; you might now get Valkey. Same with Google Cloud&amp;rsquo;s Memorystore.&lt;/p&gt;&#10;&lt;p&gt;These managed services have their own quirks and limitations, shaped by what the cloud providers choose to support. The result? More fragmentation.&lt;/p&gt;&#10;&lt;p&gt;Managed Redis-compatible services now include both projects, with different feature sets and provider support.&lt;/p&gt;&#10;&lt;h2 id="the-choice"&gt;The Choice&lt;/h2&gt;&#10;&lt;p&gt;So where does this leave developers in 2026?&lt;/p&gt;&#10;&lt;p&gt;If you&amp;rsquo;re starting a new project, Valkey offers true open-source freedom, better performance on multi-core systems, and superior memory efficiency. You sacrifice some advanced features, but its BSD license permits use, modification, and redistribution under its terms.&lt;/p&gt;&#10;&lt;p&gt;If you need those advanced features &lt;em&gt;right now&lt;/em&gt;—vector search for AI applications, sophisticated time series analysis, enterprise support—Redis has them. But you&amp;rsquo;re accepting the terms Redis Inc. sets, and those terms have already changed once.&lt;/p&gt;&#10;&lt;p&gt;For existing Redis deployments, the calculation is harder. Migration costs are real. But so is the risk of depending on a vendor that rewrote the rules mid-game.&lt;/p&gt;&#10;&lt;h2 id="my-take"&gt;My Take&lt;/h2&gt;&#10;&lt;p&gt;Valkey started with an established codebase and backing from major companies, including AWS, Google Cloud, Oracle, Ericsson, and Snap Inc. Its performance and memory work give developers reasons to evaluate it beyond the license.&lt;/p&gt;&#10;&lt;p&gt;I prefer Valkey’s permissive licensing and foundation governance. To me, the fork shows why the ability to keep developing an existing open-source codebase matters when a vendor changes terms. Both projects continue to evolve, and the choice still depends on features, migration costs, and workload measurements.&lt;/p&gt;&#10;</description></item><item><title>github recommendor</title><link>https://saurabh-kumar.com/til/1768132800/</link><pubDate>Sun, 11 Jan 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1768132800/</guid><description>&lt;p&gt;Puzer published a &lt;a href="https://puzer.github.io/github_recommender/" rel="noopener external"&gt;github recommendor&lt;/a&gt; that uses semantic embedding from user&amp;rsquo;s github stars all client side, I found some great recommendations which I plan to use:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/pamburus/hl" rel="noopener external"&gt;pamburus/hl&lt;/a&gt;: A fast and powerful log viewer and processor that converts JSON logs or logfmt logs into a clear human-readable format. (⭐2657)&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/samwho/spacer" rel="noopener external"&gt;samwho/spacer&lt;/a&gt;: CLI tool to insert spacers when command output stops (⭐1663)&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/darrenburns/posting" rel="noopener external"&gt;darrenburns/posting&lt;/a&gt;: The modern API client that lives in your terminal. (⭐11134)&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/plutov/oq" rel="noopener external"&gt;plutov/oq&lt;/a&gt;: Terminal OpenAPI Spec viewer (⭐943)&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/wey-gu/py-pglite" rel="noopener external"&gt;wey-gu/py-pglite&lt;/a&gt;: PGlite wrapper in Python for testing. Test your app with Postgres just as lite as SQLite. (⭐577)&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>Django Postgres Migration Tools</title><link>https://saurabh-kumar.com/til/1767787200/</link><pubDate>Wed, 07 Jan 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1767787200/</guid><description>&lt;p&gt;&amp;ldquo;&lt;a href="https://github.com/kraken-tech/django-pg-migration-tools?tab=readme-ov-file" rel="noopener external"&gt;Django Postgres Migration Tools&lt;/a&gt; - add-on for safer and more scalable migrations in django.&lt;/p&gt;&#10;</description></item><item><title>Thoughtworks - Technology Radar vol 33</title><link>https://saurabh-kumar.com/til/1766404802/</link><pubDate>Mon, 22 Dec 2025 12:00:02 +0000</pubDate><guid>https://saurabh-kumar.com/til/1766404802/</guid><description>&lt;p&gt;Notes from &lt;a href="https://www.thoughtworks.com/content/dam/thoughtworks/documents/radar/2025/11/tr_technology_radar_vol_33_en.pdf" rel="noopener external"&gt;Thoughtworks - Technology Radar vol 33&lt;/a&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;text-to-sql solutions aren&amp;rsquo;t working as expected&lt;/li&gt;&#10;&lt;li&gt;pnpm, langGraph, and pydantic recommended for adoption&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>VectorChord</title><link>https://saurabh-kumar.com/til/1766145600/</link><pubDate>Fri, 19 Dec 2025 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1766145600/</guid><description>&lt;p&gt;&lt;a href="https://github.com/tensorchord/VectorChord/" rel="noopener external"&gt;VectorChord&lt;/a&gt; is a faster pgvector alternative.&lt;/p&gt;&#10;</description></item><item><title>BM25 Ranking in PostgreSQL with pg_textsearch</title><link>https://saurabh-kumar.com/articles/2025/12/bm25-ranking-in-postgresql-with-pg-textsearch/</link><pubDate>Fri, 19 Dec 2025 00:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/articles/2025/12/bm25-ranking-in-postgresql-with-pg-textsearch/</guid><description>&lt;p&gt;Today I learned about &lt;strong&gt;pg_textsearch&lt;/strong&gt;, a new PostgreSQL extension by Timescale that brings BM25 relevance-ranked full-text search to Postgres.&lt;/p&gt;&#10;&lt;h2 id="what-is-bm25"&gt;What is BM25?&lt;/h2&gt;&#10;&lt;p&gt;BM25 (Best Matching 25) is the industry-standard ranking algorithm used by search engines like Elasticsearch and Lucene. Unlike simple term frequency counting, BM25 provides smarter relevance scoring through:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Term Frequency Saturation&lt;/strong&gt;: Mentioning a word 10 times isn&amp;rsquo;t 10x better than once—diminishing returns kick in&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Inverse Document Frequency (IDF)&lt;/strong&gt;: Rare terms get higher weight than common ones&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Document Length Normalization&lt;/strong&gt;: Short and long documents are compared fairly&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The formula uses two tunable parameters:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;k1&lt;/code&gt; (default 1.2): Controls term frequency saturation&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;b&lt;/code&gt; (default 0.75): Controls length normalization (0 = none, 1 = full)&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="basic-usage"&gt;Basic Usage&lt;/h2&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;-- Create a BM25 index&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;INDEX&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;docs_idx&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;USING&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bm25&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;WITH&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text_config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;english&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;-- Search with the &amp;lt;@&amp;gt; operator (returns negative scores—lower is better)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;@&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;database search&amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;BY&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;@&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;database search&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;LIMIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="pg_textsearch-vs-postgresql-built-in-fts"&gt;pg_textsearch vs PostgreSQL Built-in FTS&lt;/h2&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Aspect&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;PostgreSQL FTS&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;pg_textsearch (BM25)&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;strong&gt;Ranking&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Basic term frequency&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Probabilistic with IDF&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;strong&gt;Operator&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;@@&lt;/code&gt; with &lt;code&gt;tsquery&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;&amp;lt;@&amp;gt;&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;strong&gt;Boolean queries&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Rich (&lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt;, &lt;code&gt;!&lt;/code&gt;, &lt;code&gt;&amp;lt;-&amp;gt;&lt;/code&gt;)&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Simple (implicit AND)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;strong&gt;Phrase search&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Yes&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;No&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;strong&gt;Highlighting&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;ts_headline()&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;No&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;strong&gt;Relevance quality&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Basic&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Industry-standard&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;strong&gt;Maturity&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Battle-tested&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Prerelease (v0.1.x)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;h2 id="when-to-use-each"&gt;When to Use Each&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;Use PostgreSQL FTS when you need:&lt;/strong&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Complex boolean queries (&lt;code&gt;(postgresql | mysql) &amp;amp; !oracle&lt;/code&gt;)&lt;/li&gt;&#10;&lt;li&gt;Phrase search (&lt;code&gt;'full text search'&lt;/code&gt;)&lt;/li&gt;&#10;&lt;li&gt;Highlighting/snippets&lt;/li&gt;&#10;&lt;li&gt;Prefix matching (&lt;code&gt;data:*&lt;/code&gt;)&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;strong&gt;Use pg_textsearch when you need:&lt;/strong&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;High-quality relevance ranking (like Google/Elasticsearch)&lt;/li&gt;&#10;&lt;li&gt;Simple search box UX (type words, get ranked results)&lt;/li&gt;&#10;&lt;li&gt;Fair comparison across varying document lengths&lt;/li&gt;&#10;&lt;li&gt;Proper rare-term weighting&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="hybrid-approach"&gt;Hybrid Approach&lt;/h2&gt;&#10;&lt;p&gt;You can combine both—use FTS for filtering and BM25 for ranking:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;@&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;database performance&amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;articles&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;body_tsv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;@@&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;to_tsquery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;english&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;database &amp;amp; !mysql&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;-- FTS filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;BY&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;@&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;database performance&amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;-- BM25 ranking&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;LIMIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="taking-step-further-with-semantic-search"&gt;Taking step further with Semantic Search&lt;/h2&gt;&#10;&lt;p&gt;Pure vector search might miss exact matches; pure keyword search misses synonyms. Hybrid gets the best of both — a search for &amp;ldquo;ML algorithms&amp;rdquo; would find documents about &amp;ldquo;machine learning techniques&amp;rdquo; (semantic) and those with the exact phrase (keyword).&lt;/p&gt;&#10;&lt;p&gt;This &lt;a href="https://www.tigerdata.com/blog/you-dont-need-elasticsearch-bm25-is-now-in-postgres" rel="noopener external"&gt;article&lt;/a&gt; outlines with code sample how to combine BM25 with Vector Search in PostgreSQL.&lt;/p&gt;&#10;&lt;h2 id="key-takeaways"&gt;Key Takeaways&lt;/h2&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;BM25 is what makes modern search engines feel &amp;ldquo;smart&amp;rdquo;—it&amp;rsquo;s not just counting words&lt;/li&gt;&#10;&lt;li&gt;pg_textsearch brings this to Postgres without needing Elasticsearch&lt;/li&gt;&#10;&lt;li&gt;The &lt;code&gt;&amp;lt;@&amp;gt;&lt;/code&gt; operator returns negative scores (more negative = more relevant)&lt;/li&gt;&#10;&lt;li&gt;For production search, consider the hybrid approach: FTS for filtering, BM25 for ranking&lt;/li&gt;&#10;&lt;li&gt;The extension is still prerelease—not yet recommended for production&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="resources"&gt;Resources&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/timescale/pg_textsearch" rel="noopener external"&gt;pg_textsearch on GitHub&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/textsearch.html" rel="noopener external"&gt;PostgreSQL Full Text Search Docs&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://www.tigerdata.com/blog/you-dont-need-elasticsearch-bm25-is-now-in-postgres" rel="noopener external"&gt;Hybrid search using BM25 and pgVector&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>Django Native PostgreSQL Connection Pooling</title><link>https://saurabh-kumar.com/articles/2025/06/django-native-postgresql-connection-pooling/</link><pubDate>Wed, 18 Jun 2025 00:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/articles/2025/06/django-native-postgresql-connection-pooling/</guid><description>&lt;p&gt;Opening PostgreSQL connections adds overhead. Django 5.1 (released August 7, 2024) added native connection pooling&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;, which lets an application reuse connections. Measure connection overhead and response times before and after enabling it; the benefit depends on your workload.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;If you&amp;rsquo;re on Django 6+ and running async/ASGI, also see the async pool notes in &lt;a href="https://saurabh-kumar.com/articles/2026/06/django-orm-from-sync_to_async-threads-to-native-psycopg3/"&gt;How Django&amp;rsquo;s ORM Went From&#10;sync_to_async Threads to Native&#10;psycopg3&lt;/a&gt;.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 id="native-pooling-vs-external-pooling"&gt;Native Pooling vs External Pooling&lt;/h2&gt;&#10;&lt;p&gt;PgBouncer runs as a separate pooling server. Django’s native pooling runs in the application process, so it does not require a separate server. It does require psycopg’s pool package. Third-party Django pooling packages and manual connection management are other options, with their own maintenance and configuration needs.&lt;/p&gt;&#10;&lt;h2 id="configure-native-pooling"&gt;Configure Native Pooling&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;Requirements&lt;/strong&gt;: Django 5.1+ and PostgreSQL (this feature does not work with&#10;psycopg2)&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#10;&lt;p&gt;Install the correct package:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;pip install &lt;span class="s2"&gt;&amp;#34;psycopg[binary,pool]&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Critical&lt;/strong&gt;: The package is called &lt;code&gt;psycopg&lt;/code&gt; (not &lt;code&gt;psycopg3&lt;/code&gt;). The pool&#10;functionality requires a separate package that this command installs&#10;automatically&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;p&gt;Add these lines to your &lt;code&gt;settings.py&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;default&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ENGINE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;django.db.backends.postgresql&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;NAME&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_name&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;USER&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_user&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PASSWORD&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_password&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;HOST&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_host&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PORT&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;5432&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;CONN_MAX_AGE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Required - pooling fails without this&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;OPTIONS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;pool&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Deploy this change. Your app now reuses connections instead of creating new ones&#10;for every request.&lt;/p&gt;&#10;&lt;h2 id="prevent-the-configuration-error-that-breaks-production"&gt;Prevent the Configuration Error That Breaks Production&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;Must set &lt;code&gt;CONN_MAX_AGE = 0&lt;/code&gt;&lt;/strong&gt;. Without this, Django throws&#10;&lt;code&gt;ImproperlyConfigured: Pooling doesn't support persistent connections&lt;/code&gt; and your&#10;app crashes&lt;sup id="fnref:4"&gt;&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref"&gt;4&lt;/a&gt;&lt;/sup&gt;. The pool manages connection lifetimes, making Django&amp;rsquo;s&#10;persistence redundant.&lt;/p&gt;&#10;&lt;h2 id="scale-for-high-traffic-production"&gt;Scale for High-Traffic Production&lt;/h2&gt;&#10;&lt;p&gt;Replace &lt;code&gt;&amp;quot;pool&amp;quot;: True&lt;/code&gt; with specific parameters for production loads:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;default&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ENGINE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;django.db.backends.postgresql&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;NAME&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_name&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;USER&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_user&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PASSWORD&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_password&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;HOST&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_host&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PORT&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;5432&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;CONN_MAX_AGE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;OPTIONS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;pool&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;min_size&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Keeps connections warm&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_size&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Handles traffic spikes&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;timeout&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Fails fast under extreme load&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_lifetime&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# 30 minutes maximum connection age&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_idle&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Close idle connections after 5 minutes&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Start with &lt;code&gt;max_size&lt;/code&gt; equal to expected concurrent users divided by 10. Scale up&#10;if you see timeout errors in logs.&lt;/p&gt;&#10;&lt;h2 id="monitor-your-performance-gains"&gt;Monitor Your Performance Gains&lt;/h2&gt;&#10;&lt;p&gt;Enable pool monitoring to track your improvements:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# In settings.py&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;logging&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;psycopg.pool&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Watch pool utilization, connection wait times, timeout errors, and response times. Record median and 95th percentile latency before and after the change under comparable load. Use those measurements to size the pool and judge whether pooling helps your application.&lt;/p&gt;&#10;&lt;h2 id="critical-fix-for-threading-and-celery-applications"&gt;Critical Fix for Threading and Celery Applications&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;Essential for background processing&lt;/strong&gt;: This isn&amp;rsquo;t a pooling bug — it&amp;rsquo;s expected&#10;Django behavior that pooling simply makes painful. Any connection opened outside&#10;Django&amp;rsquo;s request-response cycle (for example, in a thread you spawn yourself)&#10;stays open until you explicitly close it. Without a pool you might not notice;&#10;with one, those un-returned connections exhaust &lt;code&gt;max_size&lt;/code&gt; and you start seeing&#10;&lt;code&gt;PoolTimeout&lt;/code&gt; errors&lt;sup id="fnref:5"&gt;&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref"&gt;5&lt;/a&gt;&lt;/sup&gt;. Add explicit cleanup:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;threading&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Thread&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DataProcessingThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;# Your database operations here&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_seen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;# Critical: returns the connection to the pool&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For code that may touch multiple database aliases, Django&amp;rsquo;s documented helper&#10;&lt;code&gt;django.db.close_old_connections()&lt;/code&gt; is the more robust choice — it closes all&#10;old or unusable connections across every configured database:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;close_old_connections&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DataProcessingThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_seen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;close_old_connections&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without this cleanup, each background thread permanently holds a connection from&#10;your pool.&lt;/p&gt;&#10;&lt;h2 id="deployment-scenarios-that-require-different-approaches"&gt;Deployment Scenarios That Require Different Approaches&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;ASGI applications&lt;/strong&gt;: On Django 5.1–5.2, Django&amp;rsquo;s documentation recommends&#10;against native pooling with ASGI — use PgBouncer or similar external poolers&#10;instead. This changed in Django 6.0, which ships an async-aware&#10;&lt;code&gt;AsyncConnectionPool&lt;/code&gt; that works correctly under ASGI. I cover how that fits&#10;together with the truly-async ORM in &lt;a href="https://saurabh-kumar.com/articles/2026/06/django-orm-from-sync_to_async-threads-to-native-psycopg3/"&gt;How Django&amp;rsquo;s ORM Went From sync_to_async&#10;Threads to Native psycopg3&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Serverless deployments&lt;/strong&gt;: Connection pools don&amp;rsquo;t persist across serverless&#10;invocations. Skip this optimization for Lambda/Cloud Functions.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Multi-tenant applications&lt;/strong&gt;: Each tenant database needs its own pool&#10;configuration in &lt;code&gt;DATABASES&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="when-to-evaluate-pooling"&gt;When to Evaluate Pooling&lt;/h2&gt;&#10;&lt;p&gt;Evaluate pooling when connection establishment is a measurable part of request latency, concurrency is growing, or the application is approaching PostgreSQL connection limits. Compare measurements before and after rather than assuming a fixed latency or cost saving.&lt;/p&gt;&#10;&lt;div class="footnotes" role="doc-endnotes"&gt;&#10;&lt;hr&gt;&#10;&lt;ol&gt;&#10;&lt;li id="fn:1"&gt;&#10;&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/5.1/releases/5.1/" rel="noopener external"&gt;Django 5.1 Release Notes&lt;/a&gt; - Official Django documentation announcing native connection pooling support&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:2"&gt;&#10;&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/5.2/ref/databases/" rel="noopener external"&gt;Django Database Configuration&lt;/a&gt; - Complete guide to Django database settings including pooling requirements&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:3"&gt;&#10;&lt;p&gt;&lt;a href="https://www.psycopg.org/psycopg3/docs/basic/install.html" rel="noopener external"&gt;psycopg Installation Guide&lt;/a&gt; - Official psycopg documentation for package installation and pool dependencies&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:4"&gt;&#10;&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/78879329/django-improperlyconfigured-pooling-doesnt-support-persistent-connections" rel="noopener external"&gt;Stack Overflow: Django Pooling Configuration Error&lt;/a&gt; - Community discussion of the CONN_MAX_AGE requirement&amp;#160;&lt;a href="#fnref:4" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:5"&gt;&#10;&lt;p&gt;&lt;a href="https://code.djangoproject.com/ticket/35672" rel="noopener external"&gt;Django Ticket #35672&lt;/a&gt; - Closed as &lt;em&gt;invalid&lt;/em&gt;: connections opened in threads must be returned manually. See also the &lt;a href="https://docs.djangoproject.com/en/stable/ref/databases/#caveats" rel="noopener external"&gt;Databases → Caveats&lt;/a&gt; docs, which recommend &lt;code&gt;django.db.close_old_connections()&lt;/code&gt; for connections created outside the request-response cycle.&amp;#160;&lt;a href="#fnref:5" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;/div&gt;&#10;</description></item></channel></rss>