Operations

Repair duplicate conversation item positions in a legacy database

Before #570, concurrent item appends could give two items in one (tenant_id, conversation_id) scope the same position.

Praxis AI v0.13.0 (alpha) 3 min read
On this page

Before #570, concurrent item appends could give two items in one (tenant_id, conversation_id) scope the same position. The unique index added by #570 cannot be created while those rows exist, so store startup fails. Startup does not repair this automatically, because changing persisted ordering without a backup or while writers are active is unsafe; you run the repair yourself.

The statements below apply to the pre-#570, version 1 items table, before its (tenant_id, conversation_id, position) unique index exists, and are for historical data recovery only. They do not upgrade the database to the current schema: current releases need a new, empty schema-v5 store, as described in the recreate-only upgrade policy. Index definitions have also changed, and IF NOT EXISTS does not replace an old index definition.

  1. Stop every gateway instance writing to this database and take a backup.

  2. Substitute the configured items table name for <items> throughout the chosen backend’s SQL. Inspect affected conversations:

    SELECT tenant_id, conversation_id, position, COUNT(*) AS copies
    FROM <items>
    GROUP BY tenant_id, conversation_id, position
    HAVING COUNT(*) > 1;
    
  3. Run the backend-specific transaction below. It keeps every item and, only in conversations with duplicates, renumbers positions 1, 2, … in the existing (position, item_id) order. The conversation message cache is built in that same order, so its item order does not change. If any statement fails, roll back and investigate before starting the gateway.

PostgreSQL

BEGIN;
LOCK TABLE <items> IN ACCESS EXCLUSIVE MODE;

WITH duplicate_conversations AS (
    SELECT tenant_id, conversation_id
    FROM <items>
    GROUP BY tenant_id, conversation_id
    HAVING COUNT(*) > COUNT(DISTINCT position)
), ranked AS (
    SELECT i.item_id, i.tenant_id, i.conversation_id,
           ROW_NUMBER() OVER (
               PARTITION BY i.tenant_id, i.conversation_id
               ORDER BY i.position, i.item_id
           ) AS next_position
    FROM <items> AS i
    JOIN duplicate_conversations AS d
      ON d.tenant_id = i.tenant_id
     AND d.conversation_id = i.conversation_id
)
UPDATE <items> AS i
SET position = ranked.next_position
FROM ranked
WHERE i.item_id = ranked.item_id
  AND i.tenant_id = ranked.tenant_id
  AND i.conversation_id = ranked.conversation_id
  AND i.position IS DISTINCT FROM ranked.next_position;

CREATE UNIQUE INDEX IF NOT EXISTS idx_<items>_position
    ON <items>(tenant_id, conversation_id, position);
COMMIT;

SQLite

Requires SQLite 3.35 or later. MATERIALIZED fixes the ranking snapshot before the update changes any positions.

BEGIN IMMEDIATE;

WITH duplicate_conversations AS (
    SELECT tenant_id, conversation_id
    FROM <items>
    GROUP BY tenant_id, conversation_id
    HAVING COUNT(*) > COUNT(DISTINCT position)
), ranked AS MATERIALIZED (
    SELECT i.item_id, i.tenant_id, i.conversation_id,
           ROW_NUMBER() OVER (
               PARTITION BY i.tenant_id, i.conversation_id
               ORDER BY i.position, i.item_id
           ) AS next_position
    FROM <items> AS i
    JOIN duplicate_conversations AS d
      ON d.tenant_id = i.tenant_id
     AND d.conversation_id = i.conversation_id
)
UPDATE <items> AS i
SET position = (
    SELECT r.next_position FROM ranked AS r
    WHERE r.item_id = i.item_id
      AND r.tenant_id = i.tenant_id
      AND r.conversation_id = i.conversation_id
)
WHERE EXISTS (
    SELECT 1 FROM ranked AS r
    WHERE r.item_id = i.item_id
      AND r.tenant_id = i.tenant_id
      AND r.conversation_id = i.conversation_id
      AND r.next_position <> i.position
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_<items>_position
    ON <items>(tenant_id, conversation_id, position);
COMMIT;

Run the inspection query again; it must return no rows. Check that the item count still matches the backup before using the recovered data. Do not run a current release against the repaired database; provision a new store as described in schema migration.