{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "Luke Hohlin — blog",
  "home_page_url": "https://tctcl.de/blog/",
  "feed_url": "https://tctcl.de/blog/feed.json",
  "description": "Apps, packages and notes on building software, by Luke Hohlin.",
  "language": "en",
  "authors": [
    {
      "name": "Luke Hohlin",
      "url": "https://tctcl.de/"
    }
  ],
  "items": [
    {
      "id": "https://tctcl.de/blog/postgres-backend-for-chord-and-pi-durable/",
      "url": "https://tctcl.de/blog/postgres-backend-for-chord-and-pi-durable/",
      "title": "A Postgres backend for chord and pi-durable",
      "summary": "chord-postgres gives pi-durable a Postgres Storage in one call, by putting Postgres under chord's store. Setup, how it works, what it costs.",
      "content_html": "<p>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. <code>chord-postgres</code> is the package that put it there: Postgres under chord’s store, which gives pi-durable a <code>Storage</code> in one call.</p>\n<p>If you just want pi-durable running on Postgres, here’s all you need:</p>\n<h2 id=\"quick-start\">Quick start</h2>\n<p>Install it with your package manager of choice:</p>\n<div class=\"expressive-code\"><link rel=\"stylesheet\" href=\"/_astro/ec.w36nc.css\"><script type=\"module\" src=\"/_astro/ec.0vx5m.js\"></script><figure class=\"frame is-terminal not-content\"><figcaption class=\"header\"><span class=\"title\"></span><span class=\"sr-only\">Terminal window</span></figcaption><pre data-language=\"bash\"><code><div class=\"ec-line\"><div class=\"code\"><span style=\"--0:#82AAFF;--1:#3B61B0\">pnpm</span><span style=\"--0:#D6DEEB;--1:#403F53\"> </span><span style=\"--0:#ECC48D;--1:#3B61B0\">add</span><span style=\"--0:#D6DEEB;--1:#403F53\"> </span><span style=\"--0:#ECC48D;--1:#3B61B0\">chord-postgres</span><span style=\"--0:#D6DEEB;--1:#403F53\"> </span><span style=\"--0:#ECC48D;--1:#3B61B0\">@earendil-works/chord</span><span style=\"--0:#D6DEEB;--1:#403F53\"> </span><span style=\"--0:#ECC48D;--1:#3B61B0\">@earendil-works/pi-durable</span></div></div></code></pre><div class=\"copy\"><div aria-live=\"polite\"></div><button title=\"Copy to clipboard\" data-copied=\"Copied!\" data-code=\"pnpm add chord-postgres @earendil-works/chord @earendil-works/pi-durable\"><div></div></button></div></figure></div>\n<p>Then hand pi-durable a Postgres storage:</p>\n<div class=\"expressive-code\"><figure class=\"frame not-content\"><figcaption class=\"header\"></figcaption><pre data-language=\"ts\"><code><div class=\"ec-line\"><div class=\"code\"><span style=\"--0:#C792EA;--1:#8844AE\">import</span><span style=\"--0:#D6DEEB;--1:#403F53\"> { postgresStorage } </span><span style=\"--0:#C792EA;--1:#8844AE\">from</span><span style=\"--0:#D6DEEB;--1:#403F53\"> </span><span style=\"--0:#D9F5DD;--1:#111111\">'</span><span style=\"--0:#ECC48D;--1:#984E4D\">chord-postgres/pi-durable</span><span style=\"--0:#D9F5DD;--1:#111111\">'</span></div></div><div class=\"ec-line\"><div class=\"code\"><span style=\"--0:#C792EA;--1:#8844AE\">import</span><span style=\"--0:#D6DEEB;--1:#403F53\"> { Harness } </span><span style=\"--0:#C792EA;--1:#8844AE\">from</span><span style=\"--0:#D6DEEB;--1:#403F53\"> </span><span style=\"--0:#D9F5DD;--1:#111111\">'</span><span style=\"--0:#ECC48D;--1:#984E4D\">@earendil-works/pi-durable</span><span style=\"--0:#D9F5DD;--1:#111111\">'</span></div></div><div class=\"ec-line\"><div class=\"code\"><span style=\"--0:#C792EA;--1:#8844AE\">import</span><span style=\"--0:#D6DEEB;--1:#403F53\"> { BACKGROUND_CONTEXT </span><span style=\"--0:#C792EA;--1:#8844AE\">as</span><span style=\"--0:#D6DEEB;--1:#403F53\"> context } </span><span style=\"--0:#C792EA;--1:#8844AE\">from</span><span style=\"--0:#D6DEEB;--1:#403F53\"> </span><span style=\"--0:#D9F5DD;--1:#111111\">'</span><span style=\"--0:#ECC48D;--1:#984E4D\">@earendil-works/chord/context</span><span style=\"--0:#D9F5DD;--1:#111111\">'</span></div></div><div class=\"ec-line\"><div class=\"code\">\n</div></div><div class=\"ec-line\"><div class=\"code\"><span style=\"--0:#C792EA;--1:#8844AE\">const </span><span style=\"--0:#82AAFF;--1:#3B61B0\">storage</span><span style=\"--0:#C792EA;--1:#8844AE\"> = await </span><span style=\"--0:#82AAFF;--1:#3B61B0\">postgresStorage</span><span style=\"--0:#D6DEEB;--1:#403F53\">(</span><span style=\"--0:#C792EA;--1:#8844AE\">{ connection: </span><span style=\"--0:#D6DEEB;--1:#403F53\">process</span><span style=\"--0:#C792EA;--1:#8844AE\">.</span><span style=\"--0:#FAF39F;--1:#111111\">env</span><span style=\"--0:#C792EA;--1:#8844AE\">.</span><span style=\"--0:#82AAFF;--1:#3B61B0\">DATABASE_URL</span><span style=\"--0:#C792EA;--1:#8844AE\">, owner: </span><span style=\"--0:#D9F5DD;--1:#111111\">'</span><span style=\"--0:#ECC48D;--1:#984E4D\">my-app</span><span style=\"--0:#D9F5DD;--1:#111111\">'</span><span style=\"--0:#C792EA;--1:#8844AE\"> }, </span><span style=\"--0:#D6DEEB;--1:#403F53\">context)</span></div></div><div class=\"ec-line\"><div class=\"code\"><span style=\"--0:#C792EA;--1:#8844AE\">const </span><span style=\"--0:#82AAFF;--1:#3B61B0\">harness</span><span style=\"--0:#C792EA;--1:#8844AE\"> = await </span><span style=\"--0:#D6DEEB;--1:#403F53\">Harness</span><span style=\"--0:#C792EA;--1:#8844AE\">.</span><span style=\"--0:#82AAFF;--1:#3B61B0\">open</span><span style=\"--0:#D6DEEB;--1:#403F53\">(storage</span><span style=\"--0:#C792EA;--1:#8844AE\">, { </span><span style=\"--0:#D6DEEB;--1:#403F53\">registry</span><span style=\"--0:#C792EA;--1:#8844AE\">, </span><span style=\"--0:#D6DEEB;--1:#403F53\">models</span><span style=\"--0:#C792EA;--1:#8844AE\"> }, </span><span style=\"--0:#D6DEEB;--1:#403F53\">context)</span></div></div></code></pre><div class=\"copy\"><div aria-live=\"polite\"></div><button title=\"Copy to clipboard\" data-copied=\"Copied!\" data-code=\"import { postgresStorage } from &#x27;chord-postgres/pi-durable&#x27;import { Harness } from &#x27;@earendil-works/pi-durable&#x27;import { BACKGROUND_CONTEXT as context } from &#x27;@earendil-works/chord/context&#x27;const storage = await postgresStorage({ connection: process.env.DATABASE_URL, owner: &#x27;my-app&#x27; }, context)const harness = await Harness.open(storage, { registry, models }, context)\"><div></div></button></div></figure></div>\n<p><code>owner</code> is the name pi-durable’s tables live under, as <code>&lt;owner&gt;:&lt;table&gt;</code>. That’s why it’s capped at 38 characters: Postgres stops at 63 bytes of identifier and pi-durable’s longest name is <code>&lt;owner&gt;:entries:conversation</code>. The <code>schema</code> option, <code>\"chord\"</code> by default, is the Postgres schema the store lives in, and it’s created when it’s missing.</p>\n<p><code>harness.close(context)</code> closes the storage, and the storage closes its pool. The rest of the setup is in <a href=\"https://tctcl.de/packages/chord-postgres/docs/getting-started/\">getting started</a>; the package is <a href=\"https://www.npmjs.com/package/chord-postgres\">chord-postgres</a> on npm.</p>\n<p>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.</p>\n<h2 id=\"the-storage-i-wrote-myself\">The storage I wrote myself</h2>\n<p>My first version lived inside Cake: an implementation of pi-durable’s <code>Storage</code> 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 <code>CommitResult</code> into a tagged union, a result whose shape tells you which outcome it describes, and I had to follow it, because a <code>Storage</code> 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.</p>\n<p>Someone had done that work before me. <a href=\"https://github.com/j-koester/pi-durable-postgres\"><code>@netzlabor/pi-durable-postgres</code></a> 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, <code>StorageRejected</code>, so on current pi-durable (1.6 and later) it doesn’t load, and 1.4.0 added a <code>sleeping</code> task status it has never heard of.</p>\n<p>Nothing wrong with the package. Working at the <code>Storage</code> 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.</p>\n<h2 id=\"one-layer-down\">One layer down</h2>\n<p>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 <code>sqliteBackend(db)</code> takes any object with a handful of async SQL methods plus <code>transaction</code> and <code>close</code>. That’s how chord’s Cloudflare Durable Objects backend plugs in. Implement that interface over Postgres and pi-durable gets Postgres through its own <code>StoreStorage</code>, with none of pi-durable’s code ported.</p>\n<p>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 <a href=\"https://github.com/earendil-works/pi/issues/12507\">earendil-works/pi#12507</a> tracks it.</p>\n<p>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.</p>\n<h2 id=\"what-it-takes-to-be-postgres\">What it takes to be Postgres</h2>\n<p>chord emits SQLite SQL, so there’s a dialect layer that translates the statements chord 1.7.0 actually generates. <code>?</code> placeholders become <code>$1</code>. <code>INSERT OR REPLACE</code> becomes <code>INSERT … ON CONFLICT (pk) DO UPDATE</code>, with the primary key read once from <code>pg_catalog</code>. <code>IN (SELECT value FROM json_each(?))</code> becomes <code>= ANY($n::type[])</code>. <code>TEXT</code> becomes <code>text COLLATE \"C\"</code>, <code>INTEGER</code> becomes <code>bigint</code>, <code>REAL</code> becomes <code>double precision</code>.</p>\n<p><code>COLLATE \"C\"</code> isn’t cosmetic. chord compares text by code point and turns a <code>prefix()</code> into a range query, so a locale collation would reorder the rows and the range would select the wrong ones.</p>\n<p>Anything the translator doesn’t recognise throws <code>UnsupportedSqlError</code> instead of being sent to Postgres. A mistranslated statement writes the wrong row and nobody notices. A refused one fails a test.</p>\n<p>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 <code>COMMIT</code>. 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 <code>BEGIN</code> gives no writer lock the way <code>BEGIN IMMEDIATE</code> does:</p>\n<div class=\"expressive-code\"><figure class=\"frame not-content\"><figcaption class=\"header\"></figcaption><pre data-language=\"sql\"><code><div class=\"ec-line\"><div class=\"code\"><span style=\"--0:#C792EA;--1:#8844AE\">BEGIN</span><span style=\"--0:#D6DEEB;--1:#403F53\">; </span><span style=\"--0:#C792EA;--1:#8844AE\">SELECT</span><span style=\"--0:#D6DEEB;--1:#403F53\"> pg_advisory_xact_lock(hashtextextended(</span><span style=\"--0:#D9F5DD;--1:#111111\">'</span><span style=\"--0:#ECC48D;--1:#984E4D\">chord-postgres:&lt;schema&gt;</span><span style=\"--0:#D9F5DD;--1:#111111\">'</span><span style=\"--0:#D6DEEB;--1:#403F53\">, </span><span style=\"--0:#F78C6C;--1:#AA0982\">0</span><span style=\"--0:#D6DEEB;--1:#403F53\">));</span></div></div></code></pre><div class=\"copy\"><div aria-live=\"polite\"></div><button title=\"Copy to clipboard\" data-copied=\"Copied!\" data-code=\"BEGIN; SELECT pg_advisory_xact_lock(hashtextextended(&#x27;chord-postgres:<schema>&#x27;, 0));\"><div></div></button></div></figure></div>\n<p>A lost connection latches: every later call rejects with <code>ConnectionLostError</code> 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 <code>chord_postgres_adapter</code> table that records the adapter version, and a schema newer than the code is refused with <code>SchemaTooNewError</code>.</p>\n<h2 id=\"proving-it\">Proving it</h2>\n<p>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.</p>\n<p>Then the statement counts, from the statement-budget test on Postgres 18 on localhost. A chord commit of one row is 4: <code>BEGIN</code> and the lock, <code>INSERT … ON CONFLICT</code>, the <code>chord_meta</code> update, <code>COMMIT</code>. 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.</p>\n<h2 id=\"shipping-it-and-where-it-stands\">Shipping it, and where it stands</h2>\n<p>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 <code>--provenance=false</code> 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 <code>0.0.0-stage</code> from my local attempts had shown up on npm as <code>latest</code> and had to be unpublished, which is a hell of a thing to find on a package page.</p>\n<p>Cake’s own <code>Storage</code> implementation is gone, with its commit, scan and retry code and their tests. The durable log sits in its own schema, <code>cake_durable</code>, in the same database, and Cake’s app tables stay in <code>public</code>. Existing conversation data was thrown away rather than migrated, since there were no live users.</p>\n<p>The package pins what it was tested against, <code>~1.7.0</code> 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.</p>\n",
      "date_published": "2026-12-02T00:00:00.000Z",
      "date_modified": "2026-12-02T00:00:00.000Z",
      "tags": [
        "postgres",
        "pi-durable",
        "chord",
        "typescript"
      ]
    }
  ]
}