Modeling a two-signature record in Postgres: one person marks, the other confirms

2026-12-05来源 dev.toEnglish

I'm building Commitment, a small Android app (iOS is in review) for counts that two people share: milk deliveries, a maid's attendance, money lent to a friend, a gym streak. The whole product is one rule: an entry only counts once the other person confirms it.

That rule turned out to be a nice little data-modeling problem. Here is how it ended up in Postgres on Supabase, with the actual constraints from the migration.

The mark table

Every "this happened" is a row in marks, with a small state machine in a check constraint:

create table public.marks ( id uuid primary key default gen_random_uuid(), commitment_id uuid not null references public.commitments (id) on delete cascade, day date not null, marked_by uuid not null references public.profiles (id) on delete cascade, marked_at timestamptz not null default now(), status text not null default 'waiting' check (status in ('waiting', 'confirmed', 'rejected', 'expired')), responded_by uuid references public.profiles (id) on delete set null, responded_at timestamptz, check (responded_by is null or responded_by <> marked_by) ); 

The last line does a lot of work. You cannot confirm your own mark. No app logic needs to remember that, because the database refuses it.

One live mark per day

Two people tapping "mark today" at the same moment shouldn't produce two entries. A partial unique index handles it:

create unique index marks_one_live_per_day on public.marks (commitment_id, day) where status in ('waiting', 'confirmed'); 

Rejected and expired marks don't block the day, so after a "no" you can mark again with the right amount. Waiting and confirmed ones do block it.

Expiry without a cron job

Requests that sit unanswered shouldn't pile up into a month of claims someone confirms blindly on the 30th. Waiting marks expire after 48 hours. I don't run a scheduler for this. The functions that work on a commitment's marks call this first:

create or replace function public.expire_marks(cid uuid) returns void language sql security definer set search_path = public as $$ update marks set status = 'expired' where commitment_id = cid and status = 'waiting' and marked_at < now() - interval '48 hours' $$; 

Lazy expiry keeps the state correct whenever anyone touches it, which is the only time it matters.

Undo, but only for your own waiting mark

Mistakes happen ("I tapped 2 litres, meant 1"). The person who made a mark can withdraw it while it is still waiting, and not after:

delete from marks m using commitments c where m.id = p_mark and m.marked_by = auth.uid() and m.status = 'waiting' and c.id = m.commitment_id and c.status = 'active'; if not found then raise exception 'can only undo your own waiting mark'; end if; 

Once confirmed, an entry is locked for both people. That's the point of the whole thing.

Everything reads from the confirmed set

Streaks, the progress ring and the month-end bill never count waiting rows. The bill is a sum over status = 'confirmed', so "what do I owe" and "what did we agree on" are the same query.

Writes go through RPCs, not table grants

Clients can't insert or update marks directly. All state changes go through security definer functions (mark_day, respond_mark, undo_mark) that check auth.uid() against the commitment's two members. Row-level security walls each pair's data off for reads. This keeps the state machine in one place instead of spread across the React Native client.

What I'd do differently

  • I added strict to the respond function late, after realising a null decision was being read as "no". Make nulls explicit from day one.
  • Freeze history when a commitment ends. I added that in a later migration. It should have been in the first one.

If you've modeled something similar (two-party approvals, dual sign-off), I'd like to hear how you handled expiry. The app is free with no ads: Google Play, and the site is commitment.live.

原文:https://www.a8q9.cn/en/article/modeling-a-two-signature-record-in-postgres-one-person-marks-the-other-confirms-b3c67e.html

相关文章