# Reversal runs twice when two requests race

> The idempotency guard read "already reversed?" and then inserted. Two callers could both pass the check before either wrote.

Web page: https://davidewins.com/errors/idempotency-read-then-write-race

Supabase. Likely fix, 27 September 2026.

Versions: Postgres RPC

There is no error message for this one, only the symptom in the title.

## Cause

The idempotency guard read "already reversed?" and then inserted. Two callers could both pass the check before either wrote.

## Fix

Move check and insert into one database function that holds the same per-user advisory lock as the spend path (`pg_advisory_xact_lock`). A unique index only works if there is exactly one valid row per key.

## More Supabase errors

- [Supabase .single() fails with PGRST116 for a row you know exists](https://davidewins.com/errors/postgrest-pgrst116-rls-hides-row)
- [PostgREST 500: canceling statement due to statement timeout on a jsonb filter](https://davidewins.com/errors/postgrest-jsonb-filter-statement-timeout)

[Search all errors](https://davidewins.com/errors)
