ADR-106: Database-Owned Community Member Count
ADR-106: Database-Owned Community Member Count
Date: 2026-11-19 Status: Implemented (Sprint 144, v11.117.0; deployed 2026-11-23) Sprint: 144 (BUG-103, with BUG-104 and BUG-105 on the same staff-write surface) Deciders: Maintainer (trigger ownership, the column guard, the recount write and this ADR number, allocated 2026-11-19); Claude (implementation) Related: ADR-102 (authorization reads live membership), ADR-062 (the duplicate-create join), ADR-018 (fission/fusion), ADR-087 (init.sql is generated), BUG-103, BUG-106
Deployment evidence: PR #341, squash
71773476; CI/CD 45066293676, attempt 2
passed 14/14 jobs. The deploy log records 20261119-community-member-count-trigger.sql applied with
COMMIT (one migration) and all nine service health checks passing on 2026-11-23. This record uses
the existing deploy log; no new demo database read or write was performed.
Context
communities.communities.current_members is a cache of the number of active communities.members rows. People see
it: the community hero shows "N neighbours · room for M more" and disables Join when the community is full. Join
also reads it as the capacity check.
Five code paths in two services adjusted it by hand:
- join (
+1); - staff direct-add (
+1); - DELETE (
−1); - the duplicate-create join (
+1); - social-graph's invitation accept (
+1, even when itsON CONFLICT DO NOTHINGinsert was a no-op).
Staff approval (PUT … {status:'active'}) never counted at all (BUG-103). Two migrations had already recounted
the column (Sprint 86, Sprint 97), and the drift kept coming back because every writer had to remember the
arithmetic.
Capacity was not atomic either. Join read the count outside the transaction that wrote the member row, so two concurrent joins could both pass, and approval ignored capacity entirely. Community-service's CONTEXT claimed that capacity "cannot be bypassed in code (database constraint)". No such constraint existed.
Decision
The database owns the column. Application code never writes it.
1. A sync trigger is the only writer
communities.sync_current_members() runs on communities.members:
AFTER INSERT OR DELETE;AFTER UPDATE OF status, community_id, with aWHENclause that skips a row whose status and community did not change. A role-only update (a governance grant, a staff role change) does not fire it.
It applies the row's active-delta, (NEW is active ? +1 : 0) − (OLD is active ? 1 : 0), to that row's one
community. A NULL status is not active. It never clamps: a negative count would expose a bug instead of hiding it.
Membership rows never move between communities. The function raises check_violation on a community_id
change. No writer does that today (fission and fusion insert new rows), so every row firing touches at most one
community row. One statement that changes member rows in several communities still locks those community rows in
the order it scans them: for example a user deletion cascading through members_user_id_fkey. Two such statements
running concurrently can deadlock, and PostgreSQL then aborts one of them. No product path issues such a statement.
The only multi-community member deletion is the simulation's curated reset, and it runs as one transaction.
2. A column guard ignores every other write
communities.guard_current_members() is a BEFORE INSERT OR UPDATE OF current_members trigger on
communities.communities:
- on INSERT, it forces
NEW.current_members := 0, which is always true for a community with no member rows yet; - on UPDATE, it keeps
OLD.current_membersunless the transaction-local settingkarmyq.member_count_syncis'on'.
The guard exists because of the deploy window. scripts/deploy.sh applies migrations (:154) before it restarts
the services (:225), and a failed image build (:208-219) or a rollback leaves old binaries serving with the
trigger installed. Without the guard, an old join would do the trigger's +1 and its own +1. With it, every old
±1 is a no-op and the count stays exact whichever binaries are running.
3. Flag discipline
The sync function and the recount function set karmyq.member_count_sync to 'on' only around their own
UPDATE, and restore the prior value before returning. Leaving it on would let a later application write in the
same transaction (old join code's +1 after its member INSERT) through the guard. A real-database test proves that
write is ignored.
4. Recount, and the only repair path
communities.recount_current_members(p_community_id uuid DEFAULT NULL) takes SHARE ROW EXCLUSIVE on
communities.members, which fences member writers for the rest of the caller's transaction. It then sets every
drifted count, or the named community's count, to its active-row count, and returns the number of rows it changed.
The migration runs it once. This is the third repair and the one authorized demo write; a rerun changes no row.
Operators repair only through this function. A direct UPDATE of the column is ignored by the guard.
5. Capacity is re-checked after the write
services/community-service/src/database/memberCapacity.ts exports assertWithinCapacity(q, communityId). Every
admitting path runs it after its member write, on the same withTransaction client:
- join to
active(including aleftrejoin); - staff add (retired in Sprint 147; see the amendment below);
- staff PUT approve;
- the duplicate-create join.
It throws CommunityFullError when current_members > max_members, so withTransaction rolls the member write
and the trigger's increment back together, and the route answers 400 COMMUNITY_FULL. No new lock is needed. The
trigger's UPDATE holds the community row lock until commit, so concurrent admitters serialize on it, and under
READ COMMITTED the second one re-reads the first one's committed count. Lock order stays member row(s) → that one
community row everywhere. The pre-write "is it full?" read stays as a friendly early refusal; the post-check is
authoritative.
The migration
infrastructure/postgres/migrations/20261119-community-member-count-trigger.sql has no top-level
BEGIN/COMMIT/LOCK, because the two appliers disagree on transaction wrapping. The functions are top-level
CREATE OR REPLACE. Trigger installation and the recount are one DO block that sets lock_timeout to 10 s and
takes ACCESS EXCLUSIVE on members, then communities, up front. Taking the weaker lock first and upgrading for
DROP TRIGGER would deadlock against an old-binary DELETE or PUT that holds FOR UPDATE row locks and then asks
for ROW EXCLUSIVE (found by the migration-validator review). A busy table fails the migration, and the deploy
rolls back, instead of stalling it.
Alternatives rejected
- Fix each writer in the application. That was tried twice (Sprints 86 and 97). Five writers in two services, plus every future one and every old binary, would each have to stay correct.
- Capacity inside the trigger. It would make fusion fail where it succeeds today (
fusionService.tshas no capacity check, so a merged community can exceedmax_members). That is a behavior change nobody decided. - Reorder
deploy.shto restart before migrating. It only shrinks the window. A failed build and a rollback still leave old code with new schema. - Accept the window. The count is user-visible and gates Join. A week of double counting after a failed build is a real bug.
- A CHECK constraint (
current_members <= max_members). It does not keep the count true, it fails fusion, and a drifted count would turn into errors on unrelated writes.
Consequences
- Fixtures cannot set counts. A test that needs a count, or a full community, creates the active rows or lowers
max_members. The one deliberate corruption (the Sprint 134 L2 health test, which proves that health metrics read live rows rather than the cache) writes through the flag on one client in one transaction. - Operators repair only through
communities.recount_current_members(). Measuring demo drift (scripts/audit-demo-data.sqlquery 1) is a separate, authorized read. - Fission and fusion keep their recompute statements (D7). They do not set the flag, so the guard turns them into no-ops; the trigger has already produced the same value. Removing them, and their two regression tests, is deferred.
- The flag is a correctness fence, not a privilege boundary. Any session that can run SQL can set it, and that session could already write any column.
- The triggers run as the invoking role (no
SECURITY DEFINER). That is the same exposure as the applicationUPDATEthey replace, and every service shares one database role. If a future role is subject to RLS oncommunities.communities, thecommunity_isolationpolicy could silently filter the trigger'sUPDATEand leave a count stale; such a role needs a policy that admits the sync, or the function needs a reviewedSECURITY DEFINER. - Staff direct-add stays a documented consent exception (decision 10). It still activates a user directly and is admin-only and API-only. The consent gap is BUG-107. (Retired in Sprint 147: the route answers 410; see the amendment below.)
- BUG-106 is out of scope. Invitation accept loses only its manual increment. It still ignores capacity and
silently no-ops on an existing row. (Sprint 145 fixed BUG-106, deployed in v11.118.0 on 2026-11-24. It
uses this ADR's trigger and the same post-write capacity check, on social-graph's own transaction client; see the
social-graph
CONTEXT.md.) - A static gate (
tests/regression/sprint-144-no-member-count-deltas.test.ts) keeps self-referential arithmetic on the column out ofservices/*/src,apps/*/srcandpackages/*/src. With the guard such arithmetic would already be a no-op, so the gate is hygiene: it stops dead code from misleading the next reader.
The rule
Never write communities.communities.current_members. Insert, update or delete communities.members rows; the
database counts them. Read the count for display and for the early refusal, and run assertWithinCapacity after any
write that admits someone.
Amendment (Sprint 147, 2026-12-01): decision 10 retired — staff direct-add is gone (BUG-107)
Status: Accepted · Decision: maintainer, 2026-12-01: "retire direct-add with 410; migrate fixtures and race coverage to supported admission paths"
Decision 10 kept staff direct-add (POST /communities/:communityId/members) as a documented consent exception:
an admin could activate any user with no action from that person. That exception is withdrawn. The route stays
mounted behind authentication as a tombstone. Every authenticated UUID caller, admin or not, with any body or
community path, gets 410 MEMBER_DIRECT_ADD_RETIRED. The handler runs no SQL, opens no transaction and
publishes no event. A missing or non-UUID caller is 401. People now enter only through their own action:
- a join (public), or a join request that staff approve (private);
- accepting a member's invitation;
- a duplicate-create join of an existing public community.
Capacity paths. Decision 5's list of admitting paths loses "staff add". The paths that run
assertWithinCapacity (or, for acceptance, the same post-write check on social-graph's client) after their member
write are: join to active (including a left rejoin), staff PUT approve, the duplicate-create join and
invitation acceptance. The trigger, the column guard, the recount function and the lock order are unchanged.
The unused web-client wrapper and the dormant shared API-client method for the route are removed. The test
fixtures now enroll people by real invitation acceptance (tests/fixtures/index.ts CommunityFactory.addMember).
The Sprint 145 last-slot races drop the staff-add pair. Each remaining supported pair now runs both winner orders
deterministically (tests/helpers/admissionOrder.ts). The original decision text above stays as the historical
record.