---
title: "Crash-safe approval in PostgreSQL"
category: "code"
id: abf288c3-012a-4b2a-bb1a-6975716fa26a
canonical: https://solvedlist.com/solutions/crash-safe-approval-in-postgresql
---

# Crash-safe approval in PostgreSQL

> To ensure crash-safe approval and indexing in PostgreSQL, store the approval and an open index job in one transaction. Use a worker to claim and process jobs, ensuring atomicity and preventing duplicates.

## Question

How to make approval and indexing crash-safe in PostgreSQL to prevent invisible entries or duplicate points after a restart?

## Solution

Problem: the approval commits, then the index write fails or the process dies. The entry stays invisible, or a retry duplicates the point.

Fix: store the approval and an open index job in one transaction; a restarted worker finishes the job.

```sql
BEGIN;
UPDATE entry_revisions SET review_state = 'approved'
 WHERE entry_id = $1 AND id = $2 AND review_state = 'pending'
 RETURNING id;            -- must return exactly the wanted id, else ROLLBACK
UPDATE entries SET approved_revision_id = $2 WHERE id = $1;
INSERT INTO index_jobs (entry_id, revision_id) VALUES ($1, $2);
COMMIT;
```

Matching on the revision id, not just the entry, matters: with a revoked target and a pending sibling revision, a filter on `entry_id` alone releases the wrong revision and then points the entry at the revoked one.

Worker: claim one job with `WHERE done_at IS NULL ORDER BY created_at LIMIT 1 FOR UPDATE SKIP LOCKED`, set `claimed_at`/`claimed_by` in that transaction, commit the claim, then upsert the point under the entry id with `?wait=true` and require `status=completed`. Mark the job done with a compare-and-set on `claimed_by` only after the write is confirmed. This example assumes at most one active worker. An unfinished job remains eligible immediately after the claim commits. Replaying the same upsert under the stable point ID replaces that point.

## Query directly via MCP

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}
```

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
