<?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: Roni Yusuf</title>
    <description>The latest articles on DEV Community by Roni Yusuf (@rymanalu).</description>
    <link>https://dev.to/rymanalu</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%2F4172735%2F7c1be90b-dfcb-4d29-a6d4-0d6ec6f7897d.jpg</url>
      <title>DEV Community: Roni Yusuf</title>
      <link>https://dev.to/rymanalu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rymanalu"/>
    <language>en</language>
    <item>
      <title>Three silent failures in Postgres row-level security</title>
      <dc:creator>Roni Yusuf</dc:creator>
      <pubDate>Tue, 17 Nov 2026 14:10:22 +0000</pubDate>
      <link>https://dev.to/rymanalu/three-silent-failures-in-postgres-row-level-security-55i5</link>
      <guid>https://dev.to/rymanalu/three-silent-failures-in-postgres-row-level-security-55i5</guid>
      <description>&lt;p&gt;I checked a multi-tenant row-level security design on PostgreSQL 16.15 before building it, and three kinds of mistake produced no error.&lt;/p&gt;

&lt;p&gt;Row-level security fails closed, and closed is quiet. A policy's &lt;code&gt;WITH CHECK&lt;/code&gt; rejects a bad new row loudly, with error 42501. But &lt;code&gt;USING&lt;/code&gt; only filters, so a mistake on that side looks like an empty result, never a failure. Each trap below is a case where "nothing matched" hides a mistake.&lt;/p&gt;

&lt;p&gt;None of the three is a defect. Each is documented or was reported upstream. What follows is how each one looks, why it stays quiet, and what catches it.&lt;/p&gt;

&lt;p&gt;Every behavior here was measured on PostgreSQL 16.15, as a role that is neither a superuser nor &lt;code&gt;BYPASSRLS&lt;/code&gt;. The SQL is in the post, so you can run it yourself, and I link the documentation wherever it covers a behavior.&lt;/p&gt;

&lt;p&gt;The design is for the hosted version of lontar, a memory store for AI agents on Postgres. lontar's local build runs as a Claude Code plugin and has no row-level security, and the hosted version doesn't exist yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;You need a scratch database and a role that can create roles. I used a database called &lt;code&gt;rls_demo&lt;/code&gt; and a role with &lt;code&gt;CREATEROLE&lt;/code&gt; that is not a superuser. The SQL below makes a table, &lt;code&gt;item&lt;/code&gt;, with one policy that keys on a session setting, &lt;code&gt;app.org_id&lt;/code&gt;, and puts one row in it for each of two organizations.&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="c1"&gt;-- Block 0: a non-owner role, one table, one policy, one row per organization.&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;rls_app&lt;/span&gt; &lt;span class="no"&gt;NOLOGIN&lt;/span&gt; &lt;span class="no"&gt;NOSUPERUSER&lt;/span&gt; &lt;span class="no"&gt;NOBYPASSRLS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="n"&gt;rls_app&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="no"&gt;CURRENT_USER&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;              &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nf"&gt;gen_random_uuid&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;span class="n"&gt;organization_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;key&lt;/span&gt;             &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;body&lt;/span&gt;            &lt;span class="kt"&gt;text&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="no"&gt;ENABLE&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;SECURITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="no"&gt;FORCE&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;SECURITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;POLICY&lt;/span&gt; &lt;span class="n"&gt;org_isolation&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;
  &lt;span class="k"&gt;USING&lt;/span&gt;      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;organization_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;organization_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;INSERT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;rls_app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;rls_app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'11111111-1111-1111-1111-111111111111'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'aaaaaaaa-0000-0000-0000-000000000001'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'a-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'mine'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'22222222-2222-2222-2222-222222222222'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'bbbbbbbb-0000-0000-0000-000000000001'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'theirs'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;RESET&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;rolname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rolsuper&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rolbypassrls&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_roles&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;rolname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'rls_app'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details matter. &lt;code&gt;FORCE ROW LEVEL SECURITY&lt;/code&gt; is there because a table's owner bypasses its own policies without it. The last query prints the flags of &lt;code&gt;rls_app&lt;/code&gt;, the role most later blocks run as, because &lt;a href="https://www.postgresql.org/docs/16/ddl-rowsecurity.html" rel="noopener noreferrer"&gt;the documentation&lt;/a&gt; says "Superusers and roles with the &lt;code&gt;BYPASSRLS&lt;/code&gt; attribute always bypass the row security system when accessing a table." A test run as one of them shows every row and proves nothing. And &lt;code&gt;GRANT rls_app TO CURRENT_USER&lt;/code&gt; is there because, on 16.15, a role that creates another role gets &lt;code&gt;ADMIN OPTION&lt;/code&gt; on it but not the right to switch to it. Without that line, &lt;code&gt;SET ROLE rls_app&lt;/code&gt; fails with &lt;code&gt;permission denied to set role "rls_app"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The policy has two halves, and the traps live in the difference between them. &lt;a href="https://www.postgresql.org/docs/16/sql-createpolicy.html" rel="noopener noreferrer"&gt;Rows that fail &lt;code&gt;USING&lt;/code&gt;&lt;/a&gt; are not an error: "Typically, such rows are silently suppressed; no error is reported". &lt;code&gt;WITH CHECK&lt;/code&gt; is the loud half: when it is false or null, "an error occurs".&lt;/p&gt;

&lt;p&gt;Run each later SQL block in its own fresh &lt;code&gt;psql&lt;/code&gt; session. The shell blocks connect through the standard &lt;code&gt;PG*&lt;/code&gt; environment variables, so set &lt;code&gt;PGDATABASE=rls_demo&lt;/code&gt;, plus your usual &lt;code&gt;PGHOST&lt;/code&gt; and &lt;code&gt;PGUSER&lt;/code&gt;, before running them. Roles belong to the whole cluster, not to one database, so to start over, drop the database and the roles too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 1: two kinds of unset
&lt;/h2&gt;

&lt;p&gt;The loud case comes first, because it is the exception. In a session that has already touched the setting, the plain &lt;code&gt;::uuid&lt;/code&gt; cast of an unset organization fails with &lt;code&gt;ERROR:  22P02: invalid input syntax for type uuid: ""&lt;/code&gt;. In a fresh session, the same cast returns NULL and no error, so a test that opens a new connection never sees it. The reason is that "no organization set" has two spellings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.postgresql.org/docs/16/functions-admin.html" rel="noopener noreferrer"&gt;The documentation&lt;/a&gt; says that &lt;code&gt;current_setting&lt;/code&gt; with &lt;code&gt;missing_ok&lt;/code&gt; true returns NULL when there is no such setting, and that is what a fresh session reads. But once anything in the session has touched the setting, it reads as an empty string. For &lt;code&gt;set_config&lt;/code&gt; with &lt;code&gt;is_local&lt;/code&gt; true, the same page says "the new value will only apply during the current transaction." Neither entry says what the setting reads afterward.&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="c1"&gt;-- Block 1: run in a fresh psql session.&lt;/span&gt;
\&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="no"&gt;VERBOSITY&lt;/span&gt; &lt;span class="n"&gt;verbose&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;rls_app&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;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;fresh_session_is_null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'11111111-1111-1111-1111-111111111111'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&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;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;now_empty_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;is_null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- the guard and the fallback both pass quietly&lt;/span&gt;
&lt;span class="k"&gt;DO&lt;/span&gt; $$
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
  &lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
    &lt;span class="no"&gt;RAISE&lt;/span&gt; &lt;span class="no"&gt;EXCEPTION&lt;/span&gt; &lt;span class="s1"&gt;'no organization set'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;RAISE&lt;/span&gt; &lt;span class="no"&gt;NOTICE&lt;/span&gt; &lt;span class="s1"&gt;'GUARD PASSED: org value is [%]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt; $$&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COALESCE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'FALLBACK'&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;coalesced&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- the plain cast is the loud one&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- the fix: NULLIF turns both spellings into NULL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&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="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;nullif_is_null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The block uses a committed &lt;code&gt;set_config(…, true)&lt;/code&gt;. I measured the same empty string after a rolled-back one, after &lt;code&gt;SET LOCAL&lt;/code&gt; in a committed transaction, and after &lt;code&gt;SET&lt;/code&gt; followed by &lt;code&gt;RESET&lt;/code&gt;, so this is not only about transactions.&lt;/p&gt;

&lt;p&gt;The quiet cases are the ones that matter. The &lt;code&gt;IS NULL&lt;/code&gt; guard does not raise: it prints &lt;code&gt;GUARD PASSED: org value is []&lt;/code&gt;, because the value is an empty string and not NULL. A &lt;code&gt;COALESCE&lt;/code&gt; with a fallback returns the empty string, and its fallback never applies. Both are ordinary ways to write "stop if no organization is set", and both pass without a sound.&lt;/p&gt;

&lt;p&gt;This is known upstream. &lt;a href="https://www.postgresql.org/message-id/15646-0d3caee9f6266f60%40postgresql.org" rel="noopener noreferrer"&gt;It was reported in 2019, against 10.4&lt;/a&gt;: after a rolled-back transaction, a setting read as an empty string where it had been NULL. In &lt;a href="https://www.postgresql.org/message-id/392925.1673397546%40sss.pgh.pa.us" rel="noopener noreferrer"&gt;a 2023 thread on the same list&lt;/a&gt;, Tom Lane wrote: "The GUC infrastructure doesn't recognize null as a value, so you are making a distinction that the system doesn't."&lt;/p&gt;

&lt;p&gt;The fix is to treat the two spellings as one. Wrap every read of the setting in &lt;code&gt;NULLIF(current_setting('app.org_id', true), '')&lt;/code&gt;, including the column default, as the setup does. The last query in the block prints &lt;code&gt;t&lt;/code&gt;: the wrapped read is NULL in the touched session too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 2: an empty backup
&lt;/h2&gt;

&lt;p&gt;A backup is where "nothing matched" costs the most, and &lt;a href="https://www.postgresql.org/docs/16/ddl-rowsecurity.html" rel="noopener noreferrer"&gt;the documentation&lt;/a&gt; says so: "it could be disastrous if row security silently caused some rows to be omitted from the backup". By default, &lt;code&gt;pg_dump&lt;/code&gt; turns &lt;code&gt;row_security&lt;/code&gt; off, and &lt;a href="https://www.postgresql.org/docs/16/app-pgdump.html" rel="noopener noreferrer"&gt;its documentation&lt;/a&gt; says "If the user does not have sufficient privileges to bypass row security, then an error is thrown." That is the loud default. &lt;code&gt;pg_dump&lt;/code&gt; is a separate process, so the &lt;code&gt;SET ROLE&lt;/code&gt; from the setup does not reach it, and &lt;code&gt;--role&lt;/code&gt; issues one after it connects. I use &lt;code&gt;--inserts&lt;/code&gt; because the same page says that with &lt;code&gt;--enable-row-security&lt;/code&gt; "you probably also want the dump be in &lt;code&gt;INSERT&lt;/code&gt; format, as the &lt;code&gt;COPY FROM&lt;/code&gt; during restore does not support row security."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Block 2a: the default dump fails loudly; the usual fix succeeds with nothing in it.&lt;/span&gt;
&lt;span class="nv"&gt;DUMP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"pg_dump --role=rls_app --data-only --table=item --inserts"&lt;/span&gt;
&lt;span class="nv"&gt;$DUMP&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"exit $?"&lt;/span&gt;
&lt;span class="nv"&gt;$DUMP&lt;/span&gt; --enable-row-security &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; dump.sql&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"exit $?"&lt;/span&gt;
grep -c &lt;span class="s1"&gt;'^INSERT'&lt;/span&gt; dump.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first dump prints &lt;code&gt;pg_dump: error: query failed: ERROR:  query would be affected by row-level security policy for table "item"&lt;/code&gt; and exits 1. The obvious fix, &lt;code&gt;--enable-row-security&lt;/code&gt;, exits 0 and prints nothing to stderr. The file has no &lt;code&gt;INSERT&lt;/code&gt; lines, because the flag dumps "the parts of the contents of the table that they have access to", and with no organization set, &lt;code&gt;rls_app&lt;/code&gt; can see none.&lt;/p&gt;

&lt;p&gt;I measured the same with a custom-format dump: &lt;code&gt;pg_dump&lt;/code&gt; exits 0, &lt;code&gt;pg_restore --list&lt;/code&gt; exits 0 and lists the table's data, and &lt;code&gt;pg_restore --data-only&lt;/code&gt; exits 0 with nothing on stderr and restores zero rows. Every exit code says success while the backup is empty. I measured this as a role that does not own the table, as the blocks do, and as the table's owner before any backup policy existed. Neither was a superuser or &lt;code&gt;BYPASSRLS&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fix is a policy for the backup role. This block creates a role that cannot bypass row-level security, grants it to your own role so that &lt;code&gt;--role&lt;/code&gt; can switch to it, gives it &lt;code&gt;SELECT&lt;/code&gt;, and adds a &lt;code&gt;FOR SELECT … USING (true)&lt;/code&gt; policy that applies only to it.&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="c1"&gt;-- Block 2b: the fix is a policy for the backup role, not a flag.&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;rls_backup&lt;/span&gt; &lt;span class="no"&gt;NOLOGIN&lt;/span&gt; &lt;span class="no"&gt;NOSUPERUSER&lt;/span&gt; &lt;span class="no"&gt;NOBYPASSRLS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="n"&gt;rls_backup&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="no"&gt;CURRENT_USER&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;rls_backup&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;POLICY&lt;/span&gt; &lt;span class="n"&gt;backup_read&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;rls_backup&lt;/span&gt; &lt;span class="k"&gt;USING&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the flag has something to dump. The last block dumps as that role and checks the result against a count that does not pass through row-level security.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Block 2c: dump as the backup role, then compare with a count that does not pass through RLS.&lt;/span&gt;
pg_dump --role=rls_backup --data-only --table=item --inserts --enable-row-security &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; dump.sql&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"exit $?"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"dumped: $(grep -c '^INSERT' dump.sql)"&lt;/span&gt;
psql -At -c &lt;span class="s2"&gt;"SELECT n_live_tup FROM pg_stat_user_tables WHERE relname = 'item'"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It prints &lt;code&gt;exit 0&lt;/code&gt;, &lt;code&gt;dumped: 2&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;. The exit code alone can't tell you the backup is complete, so the check compares the number of &lt;code&gt;INSERT&lt;/code&gt; lines with &lt;code&gt;n_live_tup&lt;/code&gt;. A &lt;code&gt;count(*)&lt;/code&gt; taken as the backup role would be the wrong comparison: with the policy missing, it reads 0 beside a dump of 0 rows, and the check would agree with the failure it exists to catch. The statistics are not filtered by row-level security. But &lt;code&gt;n_live_tup&lt;/code&gt; is an estimate that lags behind recent writes (in the session that did the inserts it read 0 right after them), so keep this check for tables where an estimate is fair, and run it once the writes have settled.&lt;/p&gt;

&lt;p&gt;To see the check work, start over on a fresh database and run everything as before, but leave the &lt;code&gt;CREATE POLICY&lt;/code&gt; line out of the policy block. The last block then prints &lt;code&gt;exit 0&lt;/code&gt;, &lt;code&gt;dumped: 0&lt;/code&gt;, and then &lt;code&gt;2&lt;/code&gt;. The mismatch is the whole point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 3: an update that found nothing
&lt;/h2&gt;

&lt;p&gt;The last trap is an answer, not a failure. In the block below, organization 1 tries to change and then delete organization 2's row by id. Both statements succeed with no error: &lt;code&gt;UPDATE 0&lt;/code&gt; and &lt;code&gt;DELETE 0&lt;/code&gt;. That is the same answer organization 1 gets for an id that does not exist, and &lt;code&gt;UPDATE … RETURNING id&lt;/code&gt; returns &lt;code&gt;(0 rows)&lt;/code&gt;. Reading organization 2's data afterward shows the row untouched, still &lt;code&gt;theirs&lt;/code&gt;.&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="c1"&gt;-- Block 3: run in a fresh psql session.&lt;/span&gt;
\&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="no"&gt;VERBOSITY&lt;/span&gt; &lt;span class="n"&gt;verbose&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;rls_app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- organization 1 acts&lt;/span&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;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'11111111-1111-1111-1111-111111111111'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'edited'&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'aaaaaaaa-0000-0000-0000-000000000001'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'edited'&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'bbbbbbbb-0000-0000-0000-000000000001'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'bbbbbbbb-0000-0000-0000-000000000001'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'edited'&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'cccccccc-0000-0000-0000-000000000001'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'edited'&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'bbbbbbbb-0000-0000-0000-000000000001'&lt;/span&gt; &lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- the loud one, in its own transaction&lt;/span&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;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'11111111-1111-1111-1111-111111111111'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;organization_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'22222222-2222-2222-2222-222222222222'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'planted'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ROLLBACK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- the other organization's row is untouched&lt;/span&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;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'22222222-2222-2222-2222-222222222222'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- aside: ON CONFLICT DO NOTHING against a row organization 1 cannot see&lt;/span&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;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'11111111-1111-1111-1111-111111111111'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'b-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'mine too'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'b-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'mine too'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ROLLBACK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.org_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'11111111-1111-1111-1111-111111111111'&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="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'b-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'mine too'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ROLLBACK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is row-level security working: it never reveals that organization 2's row exists. The mistake lives in the application, in code that treats "no error" as "it worked". An update that changed nothing looks exactly like one that changed a row, and a cross-organization mix-up shows up as nothing at all.&lt;/p&gt;

&lt;p&gt;Compare the insert. A row stamped with another organization's id fails loudly: &lt;code&gt;ERROR:  42501: new row violates row-level security policy for table "item"&lt;/code&gt;. That is the &lt;code&gt;WITH CHECK&lt;/code&gt; half from the setup. The fix for the quiet half is to treat an affected-row count of 0 as an error wherever a row was expected. The count can't tell "not yours" from "not there", and by design it shouldn't.&lt;/p&gt;

&lt;p&gt;You will probably ask about &lt;code&gt;INSERT … ON CONFLICT DO NOTHING&lt;/code&gt;. &lt;a href="https://www.postgresql.org/docs/16/sql-createpolicy.html" rel="noopener noreferrer"&gt;The &lt;code&gt;CREATE POLICY&lt;/code&gt; documentation&lt;/a&gt; says that with an arbiter, the rows proposed for insertion are checked against the &lt;code&gt;SELECT&lt;/code&gt; policies, and "the &lt;code&gt;INSERT&lt;/code&gt; is &lt;em&gt;never&lt;/em&gt; silently avoided". That is about the row you propose. I measured a different case, where the conflicting row belongs to another organization and is invisible to the writer. An &lt;code&gt;ON CONFLICT … DO NOTHING&lt;/code&gt; whose conflicting row is invisible to the writer returns &lt;code&gt;INSERT 0 0&lt;/code&gt;, with no error, and the other organization's row is unchanged. The plain &lt;code&gt;INSERT&lt;/code&gt; with the same key fails with &lt;code&gt;23505&lt;/code&gt; (&lt;code&gt;duplicate key value violates unique constraint "item_key_key"&lt;/code&gt;), which tells organization 1 that the key exists. Both need a unique key that spans organizations, as &lt;code&gt;key&lt;/code&gt; does in this table.&lt;/p&gt;

&lt;h2&gt;
  
  
  What catches all three
&lt;/h2&gt;

&lt;p&gt;Four checks catch all three traps, and none takes more than a few lines.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Normalize the setting. Wrap every read of it in &lt;code&gt;NULLIF(current_setting('app.org_id', true), '')&lt;/code&gt;, including the column default, so an empty string and NULL mean the same thing.&lt;/li&gt;
&lt;li&gt;Give every backup role a read policy, and compare the dump's row count with a count that does not pass through row-level security. An exit code says the dump finished, not that it holds rows.&lt;/li&gt;
&lt;li&gt;Check affected-row counts. Where a row was expected, &lt;code&gt;UPDATE 0&lt;/code&gt; or &lt;code&gt;DELETE 0&lt;/code&gt; is an error.&lt;/li&gt;
&lt;li&gt;Run your RLS tests as a role that is not the table's owner, not a superuser and not &lt;code&gt;BYPASSRLS&lt;/code&gt;, and assert those flags in the test. A test run as a superuser or a &lt;code&gt;BYPASSRLS&lt;/code&gt; role sees every row, and so does one run as the owner of a table without &lt;code&gt;FORCE&lt;/code&gt;. It passes without testing anything.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of this is built in lontar, because the hosted version doesn't exist yet. Its design has a named test for a pooled connection reused after another organization's transaction, one for a cross-organization revise failing exactly like a missing id, one that the test suite's application role is not the owner, a superuser or able to bypass RLS, and a suite test that fails any organization-owned table without a backup policy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this stands
&lt;/h2&gt;

&lt;p&gt;The local build runs today as a Claude Code plugin, for one user: me. It has no row-level security. The repository is private for now, and it will be open source (AGPL-3.0). The hosted version, which this design is for, doesn't exist yet. None of what's above has run in production: I measured it on a scratch database while checking the design.&lt;/p&gt;

&lt;p&gt;Whether the hosted version gets built depends on a waitlist: if 280 people confirm a signup at &lt;a href="https://lontar.dev/?utm_source=devto" rel="noopener noreferrer"&gt;lontar.dev&lt;/a&gt; before its closing date, I build it, and if not, lontar stays a personal tool and the list is deleted. The page has the date and the details.&lt;/p&gt;

&lt;p&gt;If you have hit other quiet row-level security failures, ones where "nothing matched" hid a mistake, I would like to compare notes: what you saw, why it stayed quiet, and what caught it.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>sql</category>
      <category>database</category>
      <category>security</category>
    </item>
    <item>
      <title>I set a go/no-go threshold before building the paid version of my side project</title>
      <dc:creator>Roni Yusuf</dc:creator>
      <pubDate>Tue, 10 Nov 2026 13:04:55 +0000</pubDate>
      <link>https://dev.to/rymanalu/i-set-a-gono-go-threshold-before-building-the-paid-version-of-my-side-project-1j9</link>
      <guid>https://dev.to/rymanalu/i-set-a-gono-go-threshold-before-building-the-paid-version-of-my-side-project-1j9</guid>
      <description>&lt;p&gt;I built a memory store for AI agents: a Postgres database behind six MCP tools. It runs today as a local Claude Code plugin. &lt;a href="https://lontar.dev/posts/one-memory-across-ai-agents" rel="noopener noreferrer"&gt;The first post&lt;/a&gt; is about what it takes to share one memory across agents.&lt;/p&gt;

&lt;p&gt;The paid, hosted version doesn't exist yet. Before writing any of it, I put up a waitlist page as a gate. I wrote down the number that decides whether I build it, 280 people confirming their email inside 28 days, and what each possible verdict makes me do, including stopping.&lt;/p&gt;

&lt;p&gt;On day 28 the gate read &lt;code&gt;TOO FEW VISITORS&lt;/code&gt;. Too few people had seen the page to reach 280, and the rules I wrote down answer that with one more round through different channels. This post is how I set the numbers, how a script turns them into a verdict, and what each verdict makes me do.&lt;/p&gt;

&lt;p&gt;Every number below is computed from inputs I give, so you can redo it, and figures from outside link to their source.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a gate before the build
&lt;/h2&gt;

&lt;p&gt;The checks on whether anyone wants the hosted version, a checkpoint at month 6 and a final decision at month 12, only fire after launch. Launch itself is at least 12 weeks of building away, so the earliest either could tell me no is six months after launch, with all of that building already spent.&lt;/p&gt;

&lt;p&gt;I moved one of the two questions ahead of the build: does a pitch for the hosted version make people raise a hand? A waitlist page can test that for the cost of a page and a few posts.&lt;/p&gt;

&lt;p&gt;It cannot answer the other question, whether people who try the product pay for it. That needs a working product, and the month-6 checkpoint still decides it. A confirmed email is a weaker signal than a payment. The gate can tell me that nobody raises a hand. It can't tell me that nobody pays.&lt;/p&gt;

&lt;h2&gt;
  
  
  What counts as a raised hand
&lt;/h2&gt;

&lt;p&gt;The page shows the price before the form: $10 a month or $100 a year, with a 14-day free trial and no card. Someone who raises a hand without knowing the price tells me less.&lt;/p&gt;

&lt;p&gt;Only confirmed signups count. Entering an address sends one email with a confirm button, and the signup counts once you click it. It is a button and not a plain link because mail scanners and link previews fetch URLs on their own. A plain link would let them confirm signups nobody agreed to, and that would inflate the one number that decides whether I build. A test pins this: opening the confirm link renders the button page and leaves the signup unconfirmed. The count itself only includes signups made after the gate opened, and a signup that its owner deletes stops counting.&lt;/p&gt;

&lt;p&gt;The page is framed as build-if: "If 280 people confirm, this gets built," with a closing date. It shows no running count. A live counter would let visitors' reactions move the rate I am trying to measure, so the threshold is public and the total is not.&lt;/p&gt;

&lt;p&gt;The gate runs for a window of 28 days, and the page has the date. That date can move, so the page never says "final" or counts days down, and its wording names the date and nothing else about time. Moving it is a one-token edit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where 280 and 1.48% come from
&lt;/h2&gt;

&lt;p&gt;The gate rests on two numbers. The first is a floor on the share of visitors who confirm. I took &lt;a href="https://unbounce.com/landing-pages/whats-a-good-conversion-rate/" rel="noopener noreferrer"&gt;Unbounce's median landing-page conversion rate for SaaS, 3.8%&lt;/a&gt;, and multiplied it by &lt;a href="https://mailchimp.com/en-ca/resources/why-single-opt-in-and-an-update-for-our-eu-customers/" rel="noopener noreferrer"&gt;Mailchimp's report&lt;/a&gt; that 61% of people who start a double opt-in don't finish it, which leaves 39%. The product is 1.48%. It is a rough floor: Unbounce's data mixes click and form conversions, and neither figure is specific to waitlists. A page at or above it hasn't failed; it just hasn't been seen enough.&lt;/p&gt;

&lt;p&gt;At 1.48%, 280 confirmed signups needs about 19,000 visitors, or about 675 a day for 28 days. My design spec says 18,893, because it used the unrounded 1.482%.&lt;/p&gt;

&lt;p&gt;The second number, 280, is what I would need to see. At 5% to 10% of a list becoming paying customers, 280 signups is 14 to 28 customers. Those rates come from &lt;a href="https://www.lennysnewsletter.com/p/what-is-good-waitlist-conversion" rel="noopener noreferrer"&gt;Lenny Rachitsky's 2022 piece on waitlist conversion&lt;/a&gt;. He asked founders "over Twitter, email, and IRL" and wrote that conversion averages "around 20% if you get people off the waitlist in under a month and falling below 10% if you wait over three months." That is an anecdote, not a study. The wait on my list would be close to that line or past it, so I used the lower range. 28 is my month-6 number for continuing.&lt;/p&gt;

&lt;p&gt;A rate measured on few visitors is noisy, so the script also reads a 95% Wilson interval. A rate of 1.48% seen across 1,000 visitors has an interval of 0.90% to 2.44%, which straddles the floor. Zero signups put the whole interval below the floor only at 256 visitors. At 255 the script still can't read it.&lt;/p&gt;

&lt;p&gt;I fixed these numbers before the data. The design spec holding them was committed September 14, and the constants went into code September 17. That is my record, and you can't check it, because both repositories are private. What you can check: &lt;a href="https://dev.to/rymanalu/one-memory-across-ai-agents-what-it-actually-takes-347l"&gt;post 1 on dev.to&lt;/a&gt;, published October 9, says "if 280 people confirm", and so does the Internet Archive's &lt;a href="https://web.archive.org/web/20261010022219/https://lontar.dev/posts/one-memory-across-ai-agents" rel="noopener noreferrer"&gt;snapshot of the same post from October 10&lt;/a&gt;. dev.to lists a later edit date on post 1, but the snapshot doesn't depend on it.&lt;/p&gt;

&lt;p&gt;One rule is mine, enforced by hand and not by code: changing a threshold after the window opens needs a written reason in the results document where I record each daily readout. It holds no such entry, and again that is my record.&lt;/p&gt;

&lt;h2&gt;
  
  
  A script reads the result
&lt;/h2&gt;

&lt;p&gt;A script reads the database every day, starting on day one, so an early &lt;code&gt;GO&lt;/code&gt; counts. It computes S, the confirmed signups; V, the visitors; r = S ÷ V; and the 95% Wilson interval for r. Then it checks these conditions in order and returns at the first one that holds.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Order&lt;/th&gt;
&lt;th&gt;Condition&lt;/th&gt;
&lt;th&gt;Verdict&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;S ≥ 280&lt;/td&gt;
&lt;td&gt;&lt;code&gt;GO&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;the follow-up round has closed&lt;/td&gt;
&lt;td&gt;&lt;code&gt;STOP&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;V = 0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UNREADABLE&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;r ≥ 1.48%&lt;/td&gt;
&lt;td&gt;&lt;code&gt;TOO FEW VISITORS&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;the interval's upper bound is below 1.48%&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PITCH FAILS&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;anything else&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UNREADABLE&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two choices in that order are deliberate. &lt;code&gt;GO&lt;/code&gt; depends on S alone, and a test pins that 280 returns it whatever the visitor count. The two "no" readings are asymmetric: &lt;code&gt;TOO FEW VISITORS&lt;/code&gt; reads the point estimate, so a rate at or above the floor, even exactly at it, says the pitch hasn't failed and the page needs more visitors. &lt;code&gt;PITCH FAILS&lt;/code&gt; needs the whole interval below the floor, so a bad number from a small sample can't condemn the pitch. When the evidence is thin, the script says &lt;code&gt;UNREADABLE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These rows are hypothetical inputs run through the same &lt;code&gt;verdict()&lt;/code&gt; function. None is a real reading.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;S&lt;/th&gt;
&lt;th&gt;V&lt;/th&gt;
&lt;th&gt;r&lt;/th&gt;
&lt;th&gt;95% interval&lt;/th&gt;
&lt;th&gt;Round closed&lt;/th&gt;
&lt;th&gt;Verdict&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;300&lt;/td&gt;
&lt;td&gt;4,000&lt;/td&gt;
&lt;td&gt;7.50%&lt;/td&gt;
&lt;td&gt;6.72–8.36%&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;code&gt;GO&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;1,000&lt;/td&gt;
&lt;td&gt;4.00%&lt;/td&gt;
&lt;td&gt;2.95–5.40%&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;code&gt;TOO FEW VISITORS&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;td&gt;3.33%&lt;/td&gt;
&lt;td&gt;0.92–11.36%&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;code&gt;TOO FEW VISITORS&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;600&lt;/td&gt;
&lt;td&gt;0.33%&lt;/td&gt;
&lt;td&gt;0.09–1.21%&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PITCH FAILS&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt;0.50%&lt;/td&gt;
&lt;td&gt;0.09–2.78%&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UNREADABLE&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;150&lt;/td&gt;
&lt;td&gt;6,000&lt;/td&gt;
&lt;td&gt;2.50%&lt;/td&gt;
&lt;td&gt;2.13–2.93%&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;STOP&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The third and fourth rows show the asymmetry. Two signups from 60 visitors is above the floor, so the pitch gets the benefit of the doubt even though the interval runs from 0.92% to 11.36%. Two from 600 is 0.33% with the whole interval under 1.48%, so the pitch fails.&lt;/p&gt;

&lt;p&gt;V is the sum, over days, of distinct visitors to the front page. A visit to the post page is recorded but never counted, and tests pin both rules. A visitor who returns on another day counts twice, which lowers r. That can mislabel a no as &lt;code&gt;PITCH FAILS&lt;/code&gt; when it should be &lt;code&gt;TOO FEW VISITORS&lt;/code&gt;, but it can never turn a &lt;code&gt;GO&lt;/code&gt; into a no, because &lt;code&gt;GO&lt;/code&gt; is decided on S alone. I documented this and left it unfixed: counting people across days would need a cookie and consent, and would contradict the page's privacy claims.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;STOP&lt;/code&gt; is the one reading I set by hand. It depends on a constant in the readout script that I flip when the follow-up round ends. The verdict logic has tests. The readout script that calls it, and holds that constant, has none.&lt;/p&gt;

&lt;h2&gt;
  
  
  What each outcome triggers
&lt;/h2&gt;

&lt;p&gt;Every reading has an action, and the table below was in the same design spec as the numbers, before the window opened.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Verdict&lt;/th&gt;
&lt;th&gt;What I do&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GO&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;I start building the following Monday. Every confirmed signup gets an email, and the page changes from "build-if" to "being built", with the form still open.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TOO FEW VISITORS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One follow-up round of four weeks, through different channels.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PITCH FAILS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One follow-up round of four weeks with a different pitch, aimed at independents who work for several clients.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;UNREADABLE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One extension of two weeks. The page's date moves, which is why its wording names the date and nothing else about time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;STOP&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reached when S is still below 280 at the end of the follow-up round. Every confirmed signup is emailed that it won't be built, the list is deleted 30 days later, and lontar stays a personal tool.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;STOP&lt;/code&gt; email says that and nothing more. It names no alternative and recommends no other product.&lt;/p&gt;

&lt;p&gt;S carries over between the first window and a round, so a round adds to the signups already counted and doesn't restart the count at zero.&lt;/p&gt;

&lt;p&gt;Three limits keep the gate from running on. There is at most one follow-up round, and a two-week extension counts as that round. The whole gate ends no later than eight weeks after the first post. Re-opening a &lt;code&gt;STOP&lt;/code&gt; needs new evidence. These are process rules that I follow by hand, and no code enforces them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Day 28, and the round it triggered
&lt;/h2&gt;

&lt;p&gt;On day 28 the script returned &lt;code&gt;TOO FEW VISITORS&lt;/code&gt;. That word says two things: the share of visitors who confirmed was at or above the 1.48% floor, and not enough people saw the page for the count to reach 280. It does not say the pitch is proven. The word reads the point estimate, and a rate above the floor with few visitors behind it still has a wide interval around it. What it does say is that nothing so far shows the pitch failing, so the page needs more visitors, not a different pitch.&lt;/p&gt;

&lt;p&gt;I'm leaving out the counts behind that reading while the round runs. A running total would let reactions move the rate being measured, which is also why the page has no counter.&lt;/p&gt;

&lt;p&gt;The action for that word is the one in the table above: one follow-up round of four weeks, through different channels. The page's date has moved to the end of the round, and the page has the date. This post is part of the round. It is about a different subject than my earlier posts, and it goes to the communities whose rules allow it, and to dev.to and LinkedIn as before.&lt;/p&gt;

&lt;p&gt;Inside the round only the count matters. After the round the script can return only &lt;code&gt;GO&lt;/code&gt; or &lt;code&gt;STOP&lt;/code&gt;, and which one depends on whether S, carried over from the first window, has reached 280. Visitors who don't sign up change the reported rate, but they can't change the outcome.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this stands
&lt;/h2&gt;

&lt;p&gt;The local build runs today as a Claude Code plugin, for one user: me. The repository is private for now, and it will be open source (AGPL-3.0). The hosted version doesn't exist yet. I haven't written any of it.&lt;/p&gt;

&lt;p&gt;Whether it gets built depends on the gate above. If 280 people confirm a signup at &lt;a href="https://lontar.dev/?utm_source=devto" rel="noopener noreferrer"&gt;lontar.dev&lt;/a&gt;, I build it. If the round ends short, lontar stays a personal tool and the list is deleted. The page has the date and the details. If you build with several coding agents and want one memory across them, you are who the page is asking.&lt;/p&gt;

&lt;p&gt;If you take one thing from this post, it isn't lontar. Write the number down, and what each reading makes you do, before there is any data, and keep a copy somewhere dated that others can check. A threshold chosen after you have seen the traffic can be fitted to it. If you have run a gate of your own, I would like to compare notes: what you set, how you read it, and what you would change.&lt;/p&gt;

</description>
      <category>sideprojects</category>
      <category>startup</category>
      <category>saas</category>
      <category>statistics</category>
    </item>
    <item>
      <title>Corrected vs. ended: two ways a fact stops being true</title>
      <dc:creator>Roni Yusuf</dc:creator>
      <pubDate>Thu, 15 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/rymanalu/corrected-vs-ended-two-ways-a-fact-stops-being-true-1g0h</link>
      <guid>https://dev.to/rymanalu/corrected-vs-ended-two-ways-a-fact-stops-being-true-1g0h</guid>
      <description>&lt;p&gt;A fact can stop being true in two different ways. The world can change: the &lt;code&gt;acme-api&lt;/code&gt; project deployed from &lt;code&gt;main&lt;/code&gt;, and now it deploys from &lt;code&gt;trunk&lt;/code&gt;. Or the fact was never true: I wrote down that it runs on Postgres 15, and it runs on 16. Both facts have to stop being returned, but they are different events. The first was true for a while. The second never was, and the record that I once believed it is worth keeping.&lt;/p&gt;

&lt;p&gt;A common schema folds both into one &lt;code&gt;UPDATE&lt;/code&gt;, or one &lt;code&gt;valid_to&lt;/code&gt; column, and loses which. That loss gets expensive when the writer is a coding agent, because the writer is often wrong, and it retries.&lt;/p&gt;

&lt;p&gt;lontar is the memory store I built for that case: a Postgres database behind six MCP tools, run locally as a Claude Code plugin. &lt;a href="https://lontar.dev/posts/one-memory-across-ai-agents" rel="noopener noreferrer"&gt;The first post&lt;/a&gt; covers what it takes to share one memory across agents. This one is about the two ways a fact in it can stop being true, and the rules that keep them apart.&lt;/p&gt;

&lt;p&gt;Everything I describe about lontar is in the code today, and where a test pins a rule, I name the test. Not every rule has one, so I don't claim they all do. Anything that isn't built is marked &lt;strong&gt;designed&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two timelines
&lt;/h2&gt;

&lt;p&gt;Every fact row carries two intervals, one on the world axis and one on the belief axis. &lt;code&gt;valid_from&lt;/code&gt; and &lt;code&gt;valid_to&lt;/code&gt; say when the statement was true in the world. &lt;code&gt;recorded_at&lt;/code&gt; and &lt;code&gt;invalidated_at&lt;/code&gt; say when the store held it as something it believed. A fact stops being true by changing one end of one of them, and which end is the whole difference between ended and corrected.&lt;/p&gt;

&lt;p&gt;Both intervals are half-open: &lt;code&gt;valid_from&lt;/code&gt; and &lt;code&gt;recorded_at&lt;/code&gt; are inclusive, &lt;code&gt;valid_to&lt;/code&gt; and &lt;code&gt;invalidated_at&lt;/code&gt; exclusive. A fact valid until April 14 is gone at the instant of April 14. The queries exist twice, as SQL and as a pure function, and a contract test, "the SQL predicates agree with domain/temporal.ts on every boundary", drives both over query points that land exactly on each boundary.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;valid_from&lt;/code&gt; has no database default. The application fills it in when the caller omits it, so an insert that forgets it fails instead of quietly collapsing the model into one timeline.&lt;/p&gt;

&lt;p&gt;Here is the running example, invented, for a project called &lt;code&gt;acme-api&lt;/code&gt;. On February 2 an agent records "deploys from main" and "runs on Postgres 15". On April 14 the team moves to trunk, so the first fact is ended and "deploys from trunk" starts that day. On June 9 the team finds the database was on 16 all along, so the second fact is corrected, and "runs on Postgres 16" replaces it with the same window. What &lt;code&gt;recall_facts&lt;/code&gt; returns:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Called with&lt;/th&gt;
&lt;th&gt;Returns&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;nothing&lt;/td&gt;
&lt;td&gt;deploys from trunk; runs on Postgres 16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;as_of&lt;/code&gt; March 1&lt;/td&gt;
&lt;td&gt;deploys from main; runs on Postgres 16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;as_believed&lt;/code&gt; May 1&lt;/td&gt;
&lt;td&gt;deploys from main; deploys from trunk; runs on Postgres 15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;as_of&lt;/code&gt; May 1 and &lt;code&gt;as_believed&lt;/code&gt; May 1&lt;/td&gt;
&lt;td&gt;deploys from trunk; runs on Postgres 15&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two rows are worth reading slowly. "Runs on Postgres 15" is missing from the &lt;code&gt;as_of&lt;/code&gt; March 1 row because it was never true on March 1 or any other date; it comes back only when you ask what was believed, which the "point-in-time belief query includes since-corrected facts" test pins. And &lt;code&gt;as_believed&lt;/code&gt; alone ignores the world axis, which is why it still lists "deploys from main": pass both parameters to ask what was believed and true on the same day. A test named "as_of and as_believed together apply BOTH axes" pins the last row of the table.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is old, and Postgres doesn't do it for you
&lt;/h2&gt;

&lt;p&gt;None of this is new. The temporal-database community's &lt;a href="https://infolab.usc.edu/csci599/Fall2001/paper/glossary.pdf" rel="noopener noreferrer"&gt;consensus glossary&lt;/a&gt; (the 1998 version) defines both timelines: valid time is "usually supplied by the user", transaction time is "system-generated and -supplied", and past transaction times "cannot be changed". Snodgrass's &lt;a href="https://rts.cs.arizona.edu/tdbbook.pdf" rel="noopener noreferrer"&gt;book on time-oriented SQL&lt;/a&gt; (Morgan Kaufmann, ©2000, freely available from the author's site) describes the split this post is about: archival rows "originate from two separate processes", an end time that later "becomes known", and the case "when erroneous data was discovered and corrected; the data in error is retained" (p. 338). Fowler's &lt;a href="https://martinfowler.com/articles/bitemporal-history.html" rel="noopener noreferrer"&gt;2021 article&lt;/a&gt; calls the two timelines actual history and record history, and says "record history itself is append only".&lt;/p&gt;

&lt;p&gt;SQL:2011 standardized the pieces, as &lt;a href="https://sigmodrecord.org/publications/sigmodRecord/1209/pdfs/07.industry.kulkarni.pdf" rel="noopener noreferrer"&gt;Kulkarni and Michels describe&lt;/a&gt;: system-versioned tables for transaction time and application-time period tables for valid time. It has no term for a table that is both; "bitemporal tables" is the paper's word, not the standard's.&lt;/p&gt;

&lt;p&gt;PostgreSQL 16, which lontar runs on, has neither table type: &lt;a href="https://www.postgresql.org/docs/16/unsupported-features-sql-standard.html" rel="noopener noreferrer"&gt;T180 and T181 are on its unsupported-features list&lt;/a&gt;. PostgreSQL 18 added &lt;code&gt;WITHOUT OVERLAPS&lt;/code&gt; for primary keys and unique constraints and &lt;code&gt;PERIOD&lt;/code&gt; for foreign keys (&lt;a href="https://www.postgresql.org/docs/18/release-18.html" rel="noopener noreferrer"&gt;release notes&lt;/a&gt;), not system-versioned tables. The machinery is yours to build.&lt;/p&gt;

&lt;p&gt;What lontar adds is small. A required discriminator: every revision says &lt;code&gt;ended&lt;/code&gt; or &lt;code&gt;corrected&lt;/code&gt;. A correction's successor inherits both bounds of the original's window, which is my design choice; I haven't found it stated as a rule anywhere. And guards for a writer that retries, which the next three sections cover.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ended
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ended&lt;/code&gt; is the ordinary case: the fact was true, and the world moved on. The revision sets the fact's &lt;code&gt;valid_to&lt;/code&gt; to &lt;code&gt;at&lt;/code&gt;, which is now if the caller leaves it out, and a replacement, if there is one, becomes a new row that starts at &lt;code&gt;at&lt;/code&gt; and has no end. In the running example, "deploys from main" gets a &lt;code&gt;valid_to&lt;/code&gt; of April 14, and "deploys from trunk" starts the same day.&lt;/p&gt;

&lt;p&gt;Two guards sit in front of that write.&lt;/p&gt;

&lt;p&gt;The first is range. &lt;code&gt;at&lt;/code&gt; comes from a model, so nothing upstream stops it from preceding the fact's own &lt;code&gt;valid_from&lt;/code&gt;. The database &lt;code&gt;CHECK&lt;/code&gt; would catch that, and what the model would get back is an opaque SQL error it can't act on. So the application checks first and returns a typed &lt;code&gt;InvalidTemporalRange&lt;/code&gt; saying &lt;code&gt;valid_to&lt;/code&gt; precedes &lt;code&gt;valid_from&lt;/code&gt;, and the transaction rolls back. The test "refuses to end a fact before it began, with the typed range error" pins it. Ending a fact at exactly its &lt;code&gt;valid_from&lt;/code&gt; is allowed.&lt;/p&gt;

&lt;p&gt;The second is state. A fact that has already ended can't end again: the call fails with &lt;code&gt;FactAlreadyRetired&lt;/code&gt;, whose message ends "re-read current state before revising". It is a different error from "not found" on purpose, because "not found" would send the model hunting for another id when it should re-read. The test "refuses to end a fact that has already ended" pins it.&lt;/p&gt;

&lt;p&gt;The second guard exists because of retries. A model keeps ids it saw in an earlier recall, and &lt;code&gt;revise_fact&lt;/code&gt; is annotated &lt;code&gt;idempotentHint: false&lt;/code&gt;, so a retried call can land on a fact that is already retired. Without the guard, the &lt;code&gt;UPDATE&lt;/code&gt; runs again and a second successor is inserted. The store then holds two current "deploys from trunk" rows, or two contradictory ones if the retry worded the replacement differently, and the next session's recall hands both to the model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Corrected, and the window the successor inherits
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;corrected&lt;/code&gt; is the other half, and the tool's own wording for it is "the fact was NEVER true and we were wrong". The revision sets &lt;code&gt;invalidated_at&lt;/code&gt; to &lt;code&gt;at&lt;/code&gt;, which is the belief axis, and leaves &lt;code&gt;valid_to&lt;/code&gt; alone. A replacement, if there is one, is inserted in the same transaction.&lt;/p&gt;

&lt;p&gt;A corrected fact is terminal. Any later revision, of either reason, fails with &lt;code&gt;FactAlreadyRetired&lt;/code&gt; and &lt;code&gt;retiredBy: "corrected"&lt;/code&gt;. The test "a corrected fact is terminal; any later revision fails FactAlreadyRetired" pins it. The guard has to be separate from the one for ended facts, because a corrected fact keeps an empty &lt;code&gt;valid_to&lt;/code&gt;: an &lt;code&gt;ended&lt;/code&gt; revision would walk straight past a &lt;code&gt;valid_to&lt;/code&gt; check, and either reason would insert a second successor beside the first.&lt;/p&gt;

&lt;p&gt;The successor inherits both bounds of the original's window, &lt;code&gt;valid_from&lt;/code&gt; and &lt;code&gt;valid_to&lt;/code&gt;, plus its subject, and &lt;code&gt;supersedes&lt;/code&gt; points back at the original. Both bounds, because of a bug I wrote. My first version carried only &lt;code&gt;valid_from&lt;/code&gt;. Take "deploys from main", ended April 14, and suppose someone corrects its wording to "deploys from the main branch". With only &lt;code&gt;valid_from&lt;/code&gt; carried, the new row has no &lt;code&gt;valid_to&lt;/code&gt;, and an empty &lt;code&gt;valid_to&lt;/code&gt; is exactly what the current filter matches, so the dead branch comes back as current beside "deploys from trunk".&lt;/p&gt;

&lt;p&gt;The suite didn't catch it, and the reason is the instructive part. Every &lt;code&gt;corrected&lt;/code&gt; test started from a fact with no end, so the buggy value and the expected value were both empty. Review caught it before the code first merged. The test "'corrected' carries the original's valid_to to the successor" now starts from an ended fact.&lt;/p&gt;

&lt;p&gt;Correcting an ended fact is legal on purpose. Fixing the wording of something that has already ended is ordinary, and the successor carries the end forward instead of reopening it.&lt;/p&gt;

&lt;p&gt;Because a correction lives on the belief axis, the wrong fact isn't deleted. &lt;code&gt;as_of&lt;/code&gt; never returns it, and &lt;code&gt;as_believed&lt;/code&gt; does, which the remember skill calls "how the record of having been wrong is preserved". In the table under "Two timelines", "runs on Postgres 15" is that row: absent for every world date, present when you ask what was believed on May 1.&lt;/p&gt;

&lt;p&gt;Without terminality, a model that retries a correction, or tries to end a fact it has just corrected, would leave two replacements both current, "runs on Postgres 16" and a second take on it, and two rows that each claim to have replaced the same fact.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making the distinction impossible to skip
&lt;/h2&gt;

&lt;p&gt;A rule is only as good as the paths around it, so the paths are closed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;revise_fact&lt;/code&gt; is the only way to retire a fact. &lt;code&gt;remember_fact&lt;/code&gt; inserts and nothing else, and it has no &lt;code&gt;supersedes&lt;/code&gt; parameter, because one would let a caller retire a fact without saying which way, and that is the one distinction the whole model exists to preserve. On &lt;code&gt;revise_fact&lt;/code&gt; the reason is required, and the wire schema accepts exactly &lt;code&gt;corrected&lt;/code&gt; or &lt;code&gt;ended&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Requiring a reason doesn't make a model choose well, so the remember skill gives it a test question: "if someone asked what was true last month, should this fact be in the answer?" Yes means ended, no means corrected. It also has a rule for when the test doesn't settle it: "When you cannot tell which applies, ask. Never guess."&lt;/p&gt;

&lt;p&gt;The retirement and the successor's insert happen in one transaction. The test "revise_fact with a replacement retires and inserts in ONE transaction" breaks the insert with a 501-character replacement and checks that the original is still current, so the retirement rolled back with it.&lt;/p&gt;

&lt;p&gt;Two sessions can race. Each Claude Code session runs its own MCP server against the same database, and the target row is read with &lt;code&gt;SELECT … FOR UPDATE&lt;/code&gt;. Without that lock, both revisions would pass the guards and both would insert a successor. The test "concurrent revisions of one fact leave exactly one successor" holds the row, parks both revisions behind it, and expects one &lt;code&gt;accepted&lt;/code&gt; and one &lt;code&gt;FactAlreadyRetired&lt;/code&gt;. With the lock removed, it fails with both accepted.&lt;/p&gt;

&lt;p&gt;Spaces get the same treatment. The lookup is scoped to the session's write space, so a work session can't even see a personal fact to revise ("a fact cannot supersede a fact in another space"), and a composite foreign key from &lt;code&gt;(supersedes, space_id)&lt;/code&gt; to &lt;code&gt;(id, space_id)&lt;/code&gt; rejects it in the schema ("refuses a fact superseding a fact in another space", a raw insert).&lt;/p&gt;

&lt;p&gt;For a retrying writer, that adds up to one thing: a duplicate, late or crossed revision ends as a typed refusal, and the store never holds two answers to what is current.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it bends and leaves out
&lt;/h2&gt;

&lt;p&gt;This is where the model bends, and what it leaves out.&lt;/p&gt;

&lt;p&gt;"Current" means the end of the chain, not "true now". The filter is &lt;code&gt;invalidated_at IS NULL AND valid_to IS NULL&lt;/code&gt;, with no comparison against &lt;code&gt;valid_from&lt;/code&gt;, so a fact dated for next month counts as current today.&lt;/p&gt;

&lt;p&gt;The correction time is the caller's. SQL:2011 makes system-time columns &lt;code&gt;GENERATED ALWAYS&lt;/code&gt;, and users "are not allowed to assign or change" them. Here the database assigns &lt;code&gt;recorded_at&lt;/code&gt;, but &lt;code&gt;invalidated_at&lt;/code&gt; is whatever &lt;code&gt;at&lt;/code&gt; the caller passes, and the test "does not range-check a 'corrected' revision" pins that nothing checks it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ended&lt;/code&gt; is an in-place update. In Snodgrass's first process the previous row moves to the archival store when an end time becomes known. Mine overwrites &lt;code&gt;valid_to&lt;/code&gt;, so only corrections leave a trace on the belief axis. Ask what the store believed on March 1 about May 1, and "deploys from main" is missing, because its end is now written into the only row it has. No test pins this, or the future-dated case above.&lt;/p&gt;

&lt;p&gt;There are no period keys and no overlap checks. Nothing stops two current facts about one subject from disagreeing, and PostgreSQL 16 has no &lt;code&gt;WITHOUT OVERLAPS&lt;/code&gt; to ask for; 18 added it for keys. Keeping facts from colliding is the writer's job.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this stands
&lt;/h2&gt;

&lt;p&gt;Everything above is in the code today, and it runs for one user: me. It is a stdio MCP server over a Postgres I run myself, installed as a Claude Code plugin. The repository is private for now. It will be open source (AGPL-3.0), whether or not a hosted version gets built.&lt;/p&gt;

&lt;p&gt;The hosted version does not exist. Its plan is the same six tools with today's behavior, so these rules would carry over (&lt;strong&gt;designed&lt;/strong&gt;). Whether it gets built depends on a waitlist: if 280 people confirm a signup at &lt;a href="https://lontar.dev/?utm_source=devto" rel="noopener noreferrer"&gt;lontar.dev&lt;/a&gt; before the window closes, I build it, and if not, lontar stays a personal tool and the list is deleted. The page has the date and the details.&lt;/p&gt;

&lt;p&gt;If you take one thing from this post, it isn't lontar. Treat the two ways a fact stops being true as two operations, and make the writer say which one. A writer that is often wrong and retries will take the easiest path, so the easiest path has to keep the difference. If you've modeled this another way, or found a case where ended and corrected aren't enough, I would like to compare notes.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>ai</category>
      <category>mcp</category>
    </item>
    <item>
      <title>One memory across AI agents: what it actually takes</title>
      <dc:creator>Roni Yusuf</dc:creator>
      <pubDate>Fri, 09 Oct 2026 07:21:12 +0000</pubDate>
      <link>https://dev.to/rymanalu/one-memory-across-ai-agents-what-it-actually-takes-347l</link>
      <guid>https://dev.to/rymanalu/one-memory-across-ai-agents-what-it-actually-takes-347l</guid>
      <description>&lt;p&gt;The memory built into a coding agent lives inside that vendor's tools. What I tell one agent stays with that agent, and the next one I open in the same project starts from nothing. A checked-in instruction file gets part of the way, since any agent can read the repository, but it belongs to the repository: it can't hold what spans projects, or what I wouldn't commit.&lt;/p&gt;

&lt;p&gt;So I built a memory that sits outside the agents: a Postgres store behind six MCP tools, &lt;code&gt;remember_fact&lt;/code&gt;, &lt;code&gt;remember_note&lt;/code&gt;, &lt;code&gt;recall_facts&lt;/code&gt;, &lt;code&gt;search_notes&lt;/code&gt;, &lt;code&gt;revise_fact&lt;/code&gt; and &lt;code&gt;forget&lt;/code&gt;. It holds facts, which record both when something was true and when I believed it, and notes, which are searchable text. Today it runs locally, for one user, as a Claude Code plugin.&lt;/p&gt;

&lt;p&gt;The tools were the easy part. The work was in four requirements around them, and the first three fail silently when you get them wrong.&lt;/p&gt;

&lt;p&gt;I'm writing them down because I'm deciding whether to build a hosted version, &lt;a href="https://lontar.dev/?utm_source=devto" rel="noopener noreferrer"&gt;lontar.dev&lt;/a&gt;, and because they hold no matter who builds it.&lt;/p&gt;

&lt;p&gt;Each claim below says how I know it: &lt;strong&gt;built&lt;/strong&gt; (in the code today), &lt;strong&gt;measured&lt;/strong&gt; (by me, with the Claude Code version named where it matters), &lt;strong&gt;found&lt;/strong&gt; (in Claude Code's shipped binary), &lt;strong&gt;documented&lt;/strong&gt; (in a vendor's documentation, untested unless also marked measured) or &lt;strong&gt;designed&lt;/strong&gt; (written down, not built).&lt;/p&gt;

&lt;h2&gt;
  
  
  The memory must know where you are, and the model must not be the one to say
&lt;/h2&gt;

&lt;p&gt;Most of what I want an agent to remember belongs to one context. A client's deployment quirks belong with that client's repositories; how I like commit messages written belongs everywhere. So the store is divided into spaces, and the first design question was who picks the space for each read and write.&lt;/p&gt;

&lt;p&gt;The obvious answer is a &lt;code&gt;space&lt;/code&gt; parameter on every tool, and I didn't add one. A parameter the model fills in is a parameter any text in its context can argue for: a README, a web page, another tool's output. lontar's tools can't name a space, so a model can't ask for another one. &lt;strong&gt;Built.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead the server decides once, at startup, from where it was started. Claude Code starts each session's MCP server in that session's working directory (&lt;strong&gt;measured&lt;/strong&gt; on 2.1.286, and on 2.1.263 before it), and a config file maps directories to spaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="py"&gt;default_space&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"personal"&lt;/span&gt;

&lt;span class="nn"&gt;[[space]]&lt;/span&gt;
&lt;span class="py"&gt;id&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"personal"&lt;/span&gt;
&lt;span class="py"&gt;paths&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"~/dotfiles"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nn"&gt;[[space]]&lt;/span&gt;
&lt;span class="py"&gt;id&lt;/span&gt;         &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"work"&lt;/span&gt;
&lt;span class="py"&gt;paths&lt;/span&gt;      &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"~/codes/acme-*"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="py"&gt;reads_from&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"personal"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resolution runs in a fixed order: the &lt;code&gt;LONTAR_SPACE&lt;/code&gt; environment variable if it is set, then the longest path in the file that matches the working directory, then &lt;code&gt;default_space&lt;/code&gt;, which must be named explicitly. A path matches the directory itself or anything beneath it, and a trailing &lt;code&gt;*&lt;/code&gt; matches by plain prefix, so &lt;code&gt;~/codes/acme-*&lt;/code&gt; covers &lt;code&gt;~/codes/acme-api&lt;/code&gt; and &lt;code&gt;~/codes/acme-web&lt;/code&gt;. &lt;code&gt;reads_from&lt;/code&gt; widens reads in one direction only: here, work sessions see personal facts and personal sessions never see work ones. Every write, revision and deletion goes to the session's own space. &lt;strong&gt;Built.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Two spaces claiming the same directory at the same depth is an error, not a guess: &lt;code&gt;matches "work" and "personal" equally; make one path more specific&lt;/code&gt;. Picking one silently would hide the loser's memories and send every write to the winner. &lt;strong&gt;Built.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The failure that stays silent is the boring one. Rename or move a directory and its entry stops matching. Nothing errors; the session falls through to &lt;code&gt;default_space&lt;/code&gt;. If the default happens to be the space that directory belonged to, everything looks fine for a long time while the config names a path that no longer exists. If it isn't, the agent quietly reads and writes the wrong space. lontar doesn't check configured paths against the disk yet, so the fix today is discipline: update the paths when you rename. &lt;strong&gt;Built.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The memory must be there before the first prompt
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;recall_facts&lt;/code&gt; is a tool, and a tool is something the model decides to call. At the start of a session it has the least reason to, because it doesn't yet know what it's missing. So in Claude Code the current facts arrive before the first prompt, through a SessionStart hook: no tool call, and nothing for the model to decide. &lt;strong&gt;Built.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The hook runs &lt;code&gt;lontar recall&lt;/code&gt; in the directory named in the hook's input, not in its own working directory, and that command resolves the space through the same code as the server, so the boundary between spaces lives in one place. It fires on &lt;code&gt;startup&lt;/code&gt; and &lt;code&gt;clear&lt;/code&gt; only; after &lt;code&gt;compact&lt;/code&gt;, a wall of facts mid-session is interruption, not context. It also blocks session start, so it has a budget of 800 ms and a hard timeout of 5 seconds. On my machine it takes 656 to 799 ms over five runs (&lt;strong&gt;measured&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Every failure exits 0 and prints nothing: Postgres down, &lt;code&gt;node&lt;/code&gt; or &lt;code&gt;jq&lt;/code&gt; missing, no config, timeout. The hook runs at the start of every session in every space. A memory that sometimes forgets is an annoyance; one that blocks unrelated work gets uninstalled. The price is in the hook's own comment: when it is broken, a session simply has no memory, which looks exactly like an empty space. &lt;strong&gt;Built.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The output shape is where that price comes due. Claude Code reads the context from a nested field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"hookSpecificOutput"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"hookEventName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"SessionStart"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"additionalContext"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"…"&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I tested three shapes on Claude Code 2.1.286, each in a fresh headless session with a marker string (&lt;strong&gt;measured&lt;/strong&gt;). The nested shape arrived, labeled as hook context. Plain text on stdout also arrived, as a hook success message. A flat &lt;code&gt;{"additionalContext": "…"}&lt;/code&gt; is valid JSON, the hook exits 0, and the model saw nothing. Pair that with a hook that is silent by design, and you have a memory that never worked and never once complained.&lt;/p&gt;

&lt;p&gt;The plugin as a whole can fail the same way. Some time after I moved the repository, the plugin stopped loading, because its marketplace entry still pointed at the old path, and sessions started with no memory and no memory tools. Nothing said so. I found it while checking something else.&lt;/p&gt;

&lt;h2&gt;
  
  
  It has to fit
&lt;/h2&gt;

&lt;p&gt;A hook can't inject unlimited context. Claude Code's hooks documentation states the cap: a hook's &lt;code&gt;additionalContext&lt;/code&gt;, and its plain stdout, are each limited to 10,000 characters. I measured it before I found that sentence, with a hook that injects exactly N characters of numbered 50-character lines, one fresh headless session per size, and the model asked for the first and last line it could see:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Injected characters&lt;/th&gt;
&lt;th&gt;What the model saw&lt;/th&gt;
&lt;th&gt;Claude Code&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10,000&lt;/td&gt;
&lt;td&gt;all 200 lines&lt;/td&gt;
&lt;td&gt;2.1.268, 2.1.285, 2.1.286&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10,001&lt;/td&gt;
&lt;td&gt;the first 40 lines, then "Output too large (9.8KB). Full output saved to: …"&lt;/td&gt;
&lt;td&gt;2.1.285, 2.1.286&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10,050 to 19,999 (six sizes)&lt;/td&gt;
&lt;td&gt;the same 40-line preview&lt;/td&gt;
&lt;td&gt;2.1.285&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The documentation and the measurement agree to the character (&lt;strong&gt;documented&lt;/strong&gt;, &lt;strong&gt;measured&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;What happens above the cap is the part worth reading twice. The output goes to a file, and the model gets the path and a preview of the first 2,000 characters. In the documentation's words, "Claude Code doesn't ask Claude to read the file." When I checked on 2.1.266 to 2.1.268, injected context didn't show in the transcript view either, so the person at the keyboard saw nothing. Past 10,000 characters, whether the rest of the memory reaches the model is the model's decision again, which is exactly what the hook was there to take away.&lt;/p&gt;

&lt;p&gt;A memory store reaches the cap sooner than its own limits suggest. &lt;code&gt;recall_facts&lt;/code&gt; stops at 200 facts or about 10,000 tokens, but the token count is estimated from the statements alone, and each fact renders with about 97 more characters of metadata: a subject, an id, a date and a source. At 150-character statements, 40 facts fit in 10,000 characters, a full recall is about 49,000, and the preview keeps the newest eight. The line saying the recall was capped comes last, so it is the first thing cut. That is arithmetic on the renderer, not something I have hit yet.&lt;/p&gt;

&lt;p&gt;There is a third number. The Claude Code binary carries a table of limits, &lt;code&gt;reason:2000, stopReason:2000, systemMessage:4000, additionalContext:8000, permissionDecisionReason:2000&lt;/code&gt;, and the &lt;code&gt;additionalContext:8000&lt;/code&gt; entry is there in 2.1.268 and still in 2.1.286 (&lt;strong&gt;found&lt;/strong&gt;). A 10,000-character injection arrived whole, so it isn't the SessionStart cutoff, and I don't know what it governs.&lt;/p&gt;

&lt;p&gt;The hosted design caps the injection at 8,000 characters, under both numbers, rendered on the server. Facts about the current workspace come first, then facts with no subject, then the rest newest first, and when anything is cut the injection ends with a line inside the budget: "N more facts in lontar; call recall_facts to see them" (&lt;strong&gt;designed&lt;/strong&gt;). The local hook has no cap of its own (&lt;strong&gt;built&lt;/strong&gt;). A space that outgrows 10,000 characters keeps working, quietly, as a 2,000-character preview of its newest facts.&lt;/p&gt;

&lt;h2&gt;
  
  
  What another agent must supply
&lt;/h2&gt;

&lt;p&gt;The six tools are plain MCP. Their schemas and behavior don't depend on the client, with one exception: the server tags every write &lt;code&gt;source=claude-code&lt;/code&gt;, a constant a second client would have to replace with its own name (&lt;strong&gt;built&lt;/strong&gt;). Everything else that changes from one agent to the next comes down to two questions: how does the agent tell the memory where you are, and can it load memory before the first prompt?&lt;/p&gt;

&lt;p&gt;Cursor is my example of a second agent. Everything below comes from its documentation as I read it on October 1, 2026, and none of it is tested (&lt;strong&gt;documented&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;The workspace signal exists in two forms. Cursor resolves &lt;code&gt;${workspaceFolder}&lt;/code&gt; inside an MCP server's &lt;code&gt;command&lt;/code&gt;, &lt;code&gt;args&lt;/code&gt;, &lt;code&gt;env&lt;/code&gt;, &lt;code&gt;url&lt;/code&gt; and &lt;code&gt;headers&lt;/code&gt;, so a config can hand the server its workspace explicitly. And it lists MCP &lt;code&gt;roots&lt;/code&gt;, the protocol's own way for a client to report its workspace folders, as supported. What the docs don't say is which directory a stdio server starts in. The local build resolves its space from exactly that, so if it isn't the project, every session lands in &lt;code&gt;default_space&lt;/code&gt;, the same silent failure as a renamed directory, arriving from a different direction.&lt;/p&gt;

&lt;p&gt;Session-start loading exists too, with a caveat. Cursor's &lt;code&gt;sessionStart&lt;/code&gt; hook can return an &lt;code&gt;additional_context&lt;/code&gt; field, documented as "Additional context to add to the conversation's initial system context." The same page says the hook "runs as fire-and-forget; the agent loop does not wait for or enforce a blocking response." Whether a recall that takes most of a second lands before the first prompt is the first thing I would measure.&lt;/p&gt;

&lt;p&gt;Built-in memory needs none of this, because it never leaves the product it is built into. The vendor decides where it is stored, what it is scoped to and when it is written, and the next tool you open doesn't see it. Support for agents beyond Claude Code is planned, with tested setup guides at launch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this stands
&lt;/h2&gt;

&lt;p&gt;Everything marked &lt;strong&gt;built&lt;/strong&gt; above runs today, for one user: me. It is a stdio MCP server over a Postgres I run myself, installed as a Claude Code plugin, and the repository is private for now. It will be open source (AGPL-3.0), whether or not the hosted version gets built.&lt;/p&gt;

&lt;p&gt;The hosted version does not exist yet. It would be the same six tools over HTTP with a sign-in, and the 8,000-character session-start design above. Whether it gets built depends on a waitlist: if 280 people confirm a signup at &lt;a href="https://lontar.dev/?utm_source=devto" rel="noopener noreferrer"&gt;lontar.dev&lt;/a&gt; before the window closes, I build it, and if not, lontar stays a personal tool and the list is deleted. The page has the date and the details.&lt;/p&gt;

&lt;p&gt;Whichever way that goes, the requirements stand. A memory shared across agents has to know where you are without the model choosing, arrive before the first prompt, fit in what the agent will actually show the model, and get both of those signals from every agent it supports. If you are building any of this, I would like to compare notes.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>mcp</category>
      <category>postgres</category>
      <category>claude</category>
    </item>
  </channel>
</rss>
