<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luke Hohlin</title>
    <description>The latest articles on DEV Community by Luke Hohlin (@tctcl).</description>
    <link>https://dev.to/tctcl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4277489%2Fbabea5dd-abb7-4743-ab47-cf11e15dbb2b.png</url>
      <title>DEV Community: Luke Hohlin</title>
      <link>https://dev.to/tctcl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tctcl"/>
    <language>en</language>
    <item>
      <title>A Postgres backend for chord and pi-durable</title>
      <dc:creator>Luke Hohlin</dc:creator>
      <pubDate>Wed, 02 Dec 2026 08:15:33 +0000</pubDate>
      <link>https://dev.to/tctcl/a-postgres-backend-for-chord-and-pi-durable-5f31</link>
      <guid>https://dev.to/tctcl/a-postgres-backend-for-chord-and-pi-durable-5f31</guid>
      <description>&lt;p&gt;Cake, my web harness for pi agents, keeps its conversations on pi-durable, Earendil's durable agent runtime. pi-durable ships SQLite storage, and Cake is a server with Postgres already open for its own tables, so I wanted the durable log in the same database. &lt;code&gt;chord-postgres&lt;/code&gt; is the package that put it there: Postgres under chord's store, which gives pi-durable a &lt;code&gt;Storage&lt;/code&gt; in one call.&lt;/p&gt;

&lt;p&gt;If you just want pi-durable running on Postgres, here's all you need:&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick start
&lt;/h2&gt;

&lt;p&gt;Install it with your package manager of choice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pnpm add chord-postgres @earendil-works/chord @earendil-works/pi-durable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then hand pi-durable a Postgres storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;postgresStorage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'chord-postgres/pi-durable'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Harness&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@earendil-works/pi-durable'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;BACKGROUND_CONTEXT&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@earendil-works/chord/context'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;postgresStorage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'my-app'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;harness&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;Harness&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;owner&lt;/code&gt; is the name pi-durable's tables live under, as &lt;code&gt;&amp;lt;owner&amp;gt;:&amp;lt;table&amp;gt;&lt;/code&gt;. That's why it's capped at 38 characters: Postgres stops at 63 bytes of identifier and pi-durable's longest name is &lt;code&gt;&amp;lt;owner&amp;gt;:entries:conversation&lt;/code&gt;. The &lt;code&gt;schema&lt;/code&gt; option, &lt;code&gt;"chord"&lt;/code&gt; by default, is the Postgres schema the store lives in, and it's created when it's missing.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;harness.close(context)&lt;/code&gt; closes the storage, and the storage closes its pool. The rest of the setup is in &lt;a href="https://tctcl.de/packages/chord-postgres/docs/getting-started/" rel="noopener noreferrer"&gt;getting started&lt;/a&gt;; the package is &lt;a href="https://www.npmjs.com/package/chord-postgres" rel="noopener noreferrer"&gt;chord-postgres&lt;/a&gt; on npm.&lt;/p&gt;

&lt;p&gt;For everyone who wants to know how it works, or why it sits under chord instead of inside pi-durable, here's how I got here.&lt;/p&gt;

&lt;h2&gt;
  
  
  The storage I wrote myself
&lt;/h2&gt;

&lt;p&gt;My first version lived inside Cake: an implementation of pi-durable's &lt;code&gt;Storage&lt;/code&gt; interface with its own tables, its own commit, scan and retry logic. It passed pi-durable's storage conformance suite, all 33 cases. It was also code nobody else could use, and every change to the interface landed on me. pi-durable 1.6.1 turned &lt;code&gt;CommitResult&lt;/code&gt; into a tagged union, a result whose shape tells you which outcome it describes, and I had to follow it, because a &lt;code&gt;Storage&lt;/code&gt; implementation is written against that exact contract. That's the shitty part of this approach: you don't own the contract, you just keep paying for it.&lt;/p&gt;

&lt;p&gt;Someone had done that work before me. &lt;a href="https://github.com/j-koester/pi-durable-postgres" rel="noopener noreferrer"&gt;&lt;code&gt;@netzlabor/pi-durable-postgres&lt;/code&gt;&lt;/a&gt; by Jens Köster was the first Postgres storage for pi-durable, derived from pi-durable's own SQLite storage under MIT. It showed early that pi-durable on Postgres works at all, and its structure is easy to follow. It was written against pi-durable 1.1. pi-durable 1.2.0 removed an error type it imports, &lt;code&gt;StorageRejected&lt;/code&gt;, so on current pi-durable (1.6 and later) it doesn't load, and 1.4.0 added a &lt;code&gt;sleeping&lt;/code&gt; task status it has never heard of.&lt;/p&gt;

&lt;p&gt;Nothing wrong with the package. Working at the &lt;code&gt;Storage&lt;/code&gt; layer just means following every change to pi-durable's storage contract by hand. Others kept it alive in forks: gscode1 ported it to pi-durable 1.6.0, where it passes all 33 cases, and added fail-closed and ownership tests. tri2820 and realreus319 have forks too. I took the fail-closed latch, the refusal of a newer schema and the connection-kill test from gscode1's fork, and credited them in the README. If you pin an old pi-durable, those packages work and are a fine choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  One layer down
&lt;/h2&gt;

&lt;p&gt;I wanted the thing that keeps working as pi-durable moves, so I went one layer down. pi-durable's SQLite storage is chord's store engine, and chord's &lt;code&gt;sqliteBackend(db)&lt;/code&gt; takes any object with a handful of async SQL methods plus &lt;code&gt;transaction&lt;/code&gt; and &lt;code&gt;close&lt;/code&gt;. That's how chord's Cloudflare Durable Objects backend plugs in. Implement that interface over Postgres and pi-durable gets Postgres through its own &lt;code&gt;StoreStorage&lt;/code&gt;, with none of pi-durable's code ported.&lt;/p&gt;

&lt;p&gt;So what I have to follow instead is chord's SQL generator, which was byte-identical between chord 1.6.0 and 1.7.0. A daily Action opens an issue on every new chord or pi-durable release with the diff-and-test checklist. chord ships no Postgres backend of its own: its docs list one as an open item, and &lt;a href="https://github.com/earendil-works/pi/issues/12507" rel="noopener noreferrer"&gt;earendil-works/pi#12507&lt;/a&gt; tracks it.&lt;/p&gt;

&lt;p&gt;I wasn't sure an async network database could satisfy a contract written for a synchronous local one. It can: the interface is already async, and chord's transaction is one Postgres transaction. So the job turned out to be translation, not a redesign.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it takes to be Postgres
&lt;/h2&gt;

&lt;p&gt;chord emits SQLite SQL, so there's a dialect layer that translates the statements chord 1.7.0 actually generates. &lt;code&gt;?&lt;/code&gt; placeholders become &lt;code&gt;$1&lt;/code&gt;. &lt;code&gt;INSERT OR REPLACE&lt;/code&gt; becomes &lt;code&gt;INSERT … ON CONFLICT (pk) DO UPDATE&lt;/code&gt;, with the primary key read once from &lt;code&gt;pg_catalog&lt;/code&gt;. &lt;code&gt;IN (SELECT value FROM json_each(?))&lt;/code&gt; becomes &lt;code&gt;= ANY($n::type[])&lt;/code&gt;. &lt;code&gt;TEXT&lt;/code&gt; becomes &lt;code&gt;text COLLATE "C"&lt;/code&gt;, &lt;code&gt;INTEGER&lt;/code&gt; becomes &lt;code&gt;bigint&lt;/code&gt;, &lt;code&gt;REAL&lt;/code&gt; becomes &lt;code&gt;double precision&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;COLLATE "C"&lt;/code&gt; isn't cosmetic. chord compares text by code point and turns a &lt;code&gt;prefix()&lt;/code&gt; into a range query, so a locale collation would reorder the rows and the range would select the wrong ones.&lt;/p&gt;

&lt;p&gt;Anything the translator doesn't recognise throws &lt;code&gt;UnsupportedSqlError&lt;/code&gt; instead of being sent to Postgres. A mistranslated statement writes the wrong row and nobody notices. A refused one fails a test.&lt;/p&gt;

&lt;p&gt;Then there's the concurrency side. chord's contract wants the adapter to queue unrelated work while a transaction runs, because the store advances its in-memory sequence only after &lt;code&gt;COMMIT&lt;/code&gt;. SQLite gets that from a single connection. Here it's a queue plus an advisory lock per schema, which Postgres releases at the end of the transaction, because Postgres &lt;code&gt;BEGIN&lt;/code&gt; gives no writer lock the way &lt;code&gt;BEGIN IMMEDIATE&lt;/code&gt; does:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nf"&gt;pg_advisory_xact_lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;hashtextextended&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'chord-postgres:&amp;lt;schema&amp;gt;'&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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A lost connection latches: every later call rejects with &lt;code&gt;ConnectionLostError&lt;/code&gt; until the host closes the database and opens a new one. A commit whose connection died may or may not have committed, and a transparent reconnect could hand out an id that the lost transaction already used. In Cake the supervisor throws the storage away and opens a fresh one. There's also a small &lt;code&gt;chord_postgres_adapter&lt;/code&gt; table that records the adapter version, and a schema newer than the code is refused with &lt;code&gt;SchemaTooNewError&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proving it
&lt;/h2&gt;

&lt;p&gt;pi-durable's conformance suite through the package: 33 of 33 on pi-durable 1.7.0. CI runs the suites on Postgres 16, 17, 18 and 19.&lt;/p&gt;

&lt;p&gt;Then the statement counts, from the statement-budget test on Postgres 18 on localhost. A chord commit of one row is 4: &lt;code&gt;BEGIN&lt;/code&gt; and the lock, &lt;code&gt;INSERT … ON CONFLICT&lt;/code&gt;, the &lt;code&gt;chord_meta&lt;/code&gt; update, &lt;code&gt;COMMIT&lt;/code&gt;. Five rows is 8. A pi-durable commit of one entry is 7–8, of five entries 18. The overhead is flat: three statements per transaction plus one per row written.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shipping it, and where it stands
&lt;/h2&gt;

&lt;p&gt;Publishing was a detour. The CI token got a 403 from npm, because granular tokens that bypass 2FA are restricted for publishing. Fucking wonderful. I published 0.1.0 from my machine with &lt;code&gt;--provenance=false&lt;/code&gt; to get it out, then moved the repository to npm Trusted Publishing, so the release now comes from GitHub Actions over OIDC with provenance. Along the way a stray &lt;code&gt;0.0.0-stage&lt;/code&gt; from my local attempts had shown up on npm as &lt;code&gt;latest&lt;/code&gt; and had to be unpublished, which is a hell of a thing to find on a package page.&lt;/p&gt;

&lt;p&gt;Cake's own &lt;code&gt;Storage&lt;/code&gt; implementation is gone, with its commit, scan and retry code and their tests. The durable log sits in its own schema, &lt;code&gt;cake_durable&lt;/code&gt;, in the same database, and Cake's app tables stay in &lt;code&gt;public&lt;/code&gt;. Existing conversation data was thrown away rather than migrated, since there were no live users.&lt;/p&gt;

&lt;p&gt;The package pins what it was tested against, &lt;code&gt;~1.7.0&lt;/code&gt; for chord and pi-durable, and assumes one writer per schema. The SQL dialect is verified for chord 1.7.0, not guaranteed for whatever comes next. That's the trade I took on purpose: a chord release that emits a new construct makes the package throw instead of writing a wrong row.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>postgres</category>
      <category>typescript</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
