Second section of #153's change. Migration 00035 adds two trigger functions and two triggers on core: - On a plan ladder tier insert, or an update of its product, the tier's product is read FOR SHARE and the row is refused with chk_plan_ladder_tiers_product_has_set when the product has no entitlement set. - On an update that clears a product's entitlement_set_id (from a set to NULL; a product already without one is left alone), the update is refused with chk_products_tier_keeps_set when any ladder tier names the product. The product row taken FOR SHARE by the tier trigger is a new lock; a tier add and a set clear on one product cannot both commit. That holds under READ COMMITTED with the trigger functions VOLATILE, which the migration header and docs/database-locks.md now state; the product edit's write runs at the server's default isolation today, and item 4.6 opens it explicitly READ COMMITTED in section 4. docs/database-locks.md gains the product row, the tier add's order and the second FOR SHARE exception; docs/models/product-catalog.md invariant 13 and docs/models/identity-organization-workspace.md invariant 7 state the rule, with the set id that names no set (#99) as the case it does not cover; docs/models/plan-ladders-transitions.md names it on the tiers row. Tests (internal/db/tier_products_keep_a_set_test.go): TestATierOfAProductWithNoEntitlementSetIsRefused, TestClearingTheSetOfATierProductIsRefused, TestATierAddAndAClearedSetCannotBothCommit (both orders), TestTierProductsKeepASetMigrationUpDownUp. Race run (2.3): `go test -race -p 1 -count=1 -run '^TestATierAddAndAClearedSetCannotBothCommit$' ./internal/db` after test/reset-test-db.sh: ok (1.126 s). The review's blocker: T2 renamed a product whose set was already empty with SET name alone, which the trigger's UPDATE OF entitlement_set_id never sees, so the break on the trigger's WHEN passed. The statement now names the column too, and the break is caught. Breaks (scripts/mutation-check, command records on the migration, each preceded by a test-database reset; 14 of 14 CAUGHT): - tier check never raises (T1) - tier check never raises (T3) - tier check raises for every product - tier trigger on inserts only - tier check constraint name changed - tier lock removed (set clear returns) - tier lock removed (tier insert returns) - WHEN refuses a replacement - WHEN refuses the state - product check ignores the tiers - product trigger not created (T2) - product trigger not created (T3) - product check constraint name changed - Down leaves the product trigger and function The migration adds no relation or column, so scripts/db-reader-role.sql needs no re-run. make lint passes; openspec validate --strict passes. The suite on reset databases: 4699 pass, 14 skip; two failures, both outside this section: TestEveryRegisteredFormWasCaptured read the capture list from before the previous commit's capture and passes after it, and TestPlanLaddersWalkthrough timed out once on its create toast (#222) and passed twice alone. make screens: no change from this section.
38 KiB
title, audience, summary
| title | audience | summary | |
|---|---|---|---|
| Database Locks |
|
Every advisory lock and serializing row lock the console takes, how each key is built, and the order each path takes them in. |
Database Locks
This page describes the locks as the code takes them today. It records what each lock serializes, who takes it, and the order each path takes several. It does not describe a target design. One piece of planned work changes the page: #186 gives every advisory key one family prefix and writes one lock order. Whoever lands it edits this page in the same change.
The code names functions and sqlc queries; so does this page. Line numbers are left out because they move.
Every advisory lock is transaction-scoped (pg_advisory_xact_lock), so
Postgres releases it at commit or rollback. No code takes a session-level
advisory lock, LOCK TABLE, or an explicit FOR SHARE, except that a plan
act's listing batch takes an explicit FOR KEY SHARE on the pool rows it
lists (see the pool row below) and that the trigger on a tier insert takes the
product row FOR SHARE (see the product row below).
The locks
Advisory locks
| Lock | Key | Mode | Taken by | Serializes |
|---|---|---|---|---|
| Materialization rendezvous | hashtextextended('entitlement_materialization', 0) |
shared for a transaction that materializes a pool; exclusive for a rule change, for a change to a ladder's tiers (tier add, tier delete, tier removal, the reorder's renumbering) and for a default change's T0 | the entitlements.Materializing and entitlements.RuleChange transaction kinds (BeginMaterializing, BeginRuleChange), as the transaction's first statement |
A rule change or a tier change against every materializing transaction: each one finishes before the change or starts after it, so a conferral reads one set of rules and one order of each ladder. Materializing transactions do not wait on each other here. |
| Subscription | hashtext(<Stripe subscription id>)::bigint |
exclusive | fulfillment.ReconcileSubscription |
Two reconciles of one subscription (the checkout return and a webhook), so they converge instead of racing the exclusion constraint on subscription periods. |
| Invoice | hashtext(<Stripe invoice id>)::bigint |
exclusive | invoiceReconcile.converge in internal/integrations/stripe/workflows |
Two reconciles of one invoice (two events, or an event and the sweep), so the second finds what the first wrote. |
| Product sync | hashtext(<product id>)::bigint |
exclusive | catalogsync.Sync in internal/integrations/stripe/catalogsync |
Two sync clicks for one product, so the mapping writes and the outbox enqueue happen once. |
| FedWiki workspace | hashtext(<workspace id>)::bigint |
exclusive | withWorkspaceTx in internal/integrations/fedwiki/web, for the status-change and swap handlers |
One workspace's rotations of its active site, so two tabs cannot interleave the cooldown check and stamp or the choice of the site to park. |
| Lifecycle instance | hashtextextended('lifecycle:' || <instance id>, 0) |
exclusive | the LockLifecycleInstance query, run by integration.RecordLifecycleRequest, RecordFirstLifecycleRequest and the FedWiki activity helper finishAllWith |
One instance's requests: the latest-generation check and the insert are one step, and an outcome is recorded against a request that is still accepted. |
| Desired-state address | hashtextextended('desired-state:' || <connection id> || ':' || <recipient kind> || ':' || <recipient id>, 0) |
exclusive | the LockDesiredStateAddress query, run by lockAddress in internal/workflows/desiredstate |
Every write to the content or generation of the record at one (connection, recipient) address, and to its projection, including an address that has no record yet. |
| Domains registry | the constant registryAllocationLock (the ASCII bytes of "domains") |
exclusive | domains.Registry.WithLock |
Every mutating allocation in the registry, one lock for the whole namespace, because the disjointness rules span rows no database constraint can compare. |
Row locks
| Lock | Row | Mode | Taken by | Serializes |
|---|---|---|---|---|
| Pool row | core.resource_pools row of one pool |
FOR UPDATE; FOR KEY SHARE in a plan act's listing batch |
the functions core.confer, core.end_conferral, core.align_conferral_shape, core.update_conferral_bounds and core.settle_obligation; LockPools (through the LockResourcePool query), called by RevokeGrantTx, ManageGrantTx, ResumeReplacedGrantsTx, settlePool, reconcileItems, reconcileSuspended and reconcileEnded (the subscription reconcile's active, suspended and ended branches), runPlanAct (AddTier, RemoveTier and EnactObligation, a default change's move), and runPlanAct for a reorder's move (EnactObligation); LockPoolOfGrant (through the LockGrantPool query), called by ExpireGrantTx and ExtendWaitingGrantTx; and core.write_plan_act_obligations, which takes the pool rows of one listing batch FOR KEY SHARE |
Everything that changes a pool's positions or entitlements: conferral, endings, a subscription's suspension and resume, grant acts, tier removal, a default change's move, rule-change settlement and grant resumption. A listing batch's key-share lock conflicts with FOR UPDATE, so a batch and a move or a conferral on one of its pools wait for each other, and two batches of one act share the rows. |
| Entitlement set row and rule row | core.entitlement_sets row and core.entitlement_set_rules row |
FOR UPDATE |
core.commit_rule_change (the set row also by the LockEntitlementSet query, once per batch) |
A rule change against a writer that changes the set or the rule without holding the rendezvous. |
| Change obligation row | core.entitlement_set_change_obligations row of one (change, pool) |
FOR UPDATE |
core.settle_obligation |
Two passes settling one owed pool, so each effect is written once. |
| Plan act obligation row | core.plan_act_obligations row of one (act, pool) |
FOR UPDATE by a move, a listing batch's supersession and Retry's requeue; UPDATE by the failure recorder |
the LockPlanActObligation query (a move, after the pool row), which calls core.lock_plan_act_obligation; the function takes the row FOR UPDATE, core.write_plan_act_obligations (the open obligations of earlier acts on the batch's pools), core.requeue_plan_act_obligations (the retried act's failed obligations of the types it still owns) and core.record_plan_act_obligation_failure |
Two passes settling one obligation, and a settlement against a supersession or a requeue. A batch and a Retry take several rows in ascending pool_id order and touch disjoint rows: a batch supersedes the open rows of earlier acts on the types its act owns, a Retry requeues the failed rows of the types its act still owns, and both hold the shared rendezvous, so no default change can move the ownership while either runs. |
| Product row | core.products row of one product |
FOR SHARE by the tier trigger; FOR NO KEY UPDATE by the product edit's UPDATE |
core.refuse_tier_product_without_set, the trigger on every insert into core.plan_ladder_tiers and every change of a tier's product; UpdateProduct |
A tier insert against a clear of the product's set, so the two cannot both commit at READ COMMITTED, the isolation both writers run at (product-catalog invariant 13). The writer holds UPDATE on the table, so the trigger takes the lock with the caller's rights. The tier add takes the row after its pool rows; the product edit takes it alone. |
| Org type row | core.org_types row of one type |
FOR NO KEY UPDATE |
the LockOrgType query, run by the default change's T0 |
The T0 against the demonstration seed and an edit of the type's other columns, which write the row without the rendezvous. Every other writer of the default already waits on the exclusive rendezvous. FOR NO KEY UPDATE does not conflict with the FOR KEY SHARE an organization insert takes on its type's row. |
| Grant lineage head | the core.grants row no other grant extends |
FOR UPDATE OF g |
the GetGrantLineageHeadForUpdate query, run by RevokeGrantTx, ExtendWaitingGrantTx and ManageGrantTx |
Acts on one lineage, so an extension that committed while another act waited is visible to it. |
| Usage row | core.numeric_entitlement_usage row of one (pool, resource key) |
FOR UPDATE |
the LockNumericUsage query, run by usage.Recount and by the slot gate, after the site rows the transaction writes and before the request rows it finishes (the hold rows have no place in the order, see rule 5); the count is a statement of its own after the lock |
Two recounts of one pool, so each count reads every change committed before its lock was granted; two slot gates for one pool, so the second counts the first's hold and two creates cannot both take the last slot. |
| FedWiki site row | fedwiki.sites row of one domain or one site id |
FOR UPDATE |
the LockSiteStatusByDomain query, run by sweepLocally, RecordSiteStatusActivity, RecordSwapActivity and the sync's status write (writeStatus), and the LockSiteStatusByID query, run by the slot gate for an activation |
Paths that move one site (a member's request, the downgrade sweep and the farm sync), so each writes the status it finds under the lock and the recount that follows counts the row as it ended; the sync's reading of the site's requests, which therefore reflects every request that committed before the lock was granted; and the gate's reading of an activation whose row the sweep already made active. |
| Desired-state record | core.desired_state_records row |
FOR UPDATE |
the LockDesiredStateRecord query (after the address lock); StampDesiredStateRecordsDispatched, which locks the records in identifier order |
The record's content and generation within one recompute; the dispatch stamp against another stamp. |
| Desired-state notice | core.desired_state_outbox rows |
FOR UPDATE SKIP LOCKED |
the ClaimDesiredStateOutbox query |
Two relay passes, so each claims different rows; a pass skips rows another still holds. Triggers write the rows through core.insert_desired_state_notice inside the transaction that changed the source, and they take no lock beyond the insert. |
| Resource-key row | core.resource_keys row of one key |
the update lock of SetResourceKeyProvider |
integration.RegisterProviders at boot, stamping keys in byte order |
A stamp that changes a key's provider against an open entitlement insert of that key, which holds the row's FOR KEY SHARE through the foreign key. The materializer writes a pool's keys in byte order too, so the two wait in one direction. |
How advisory keys are built
Postgres takes one 64-bit key for an advisory lock, so every lock above maps its identity onto a bigint. The code builds those keys four ways.
hashtextextendedof a fixed name. The rendezvous. One key for the whole database.hashtextextendedof a prefixed string. The lifecycle instance (lifecycle:and the instance id) and the desired-state address (desired-state:, the connection, the recipient kind and the recipient id). The prefix keeps the two families from sharing keys with each other.hashtextof a bare id, widened to bigint. The subscription, the invoice, the product sync and the FedWiki workspace.hashtextreturns 32 bits, so these four families share one 32-bit key space with no prefix between them; a collision serializes two unrelated objects briefly and cannot lose a write. The ids differ in shape (Stripe ids against UUIDs), which makes a cross-family collision unlikely, not impossible.- A literal constant. The domains registry. Its value is larger than any 32-bit hash, so it cannot collide with family 3.
The order each path takes locks
Each path below lists its locks in the order it takes them. "Rendezvous"
means the shared lock a transaction of the entitlements.Materializing kind
(BeginMaterializing) takes as its first statement; the entitlements.RuleChange
kind takes the exclusive one. Those two kinds are what set
SET LOCAL lock_timeout = '5s', which bounds every later lock wait in the
transaction. A plain transaction sets no timeout, so its lock waits last until
its context ends. A wait past the bound surfaces as SQLSTATE 55P03. The rule
change, tier add, tier delete, tier removal, tier reorder and default change
render it as the refusal for a change in progress; the other paths show their
own failure copy. Materializing
transactions run at READ COMMITTED.
Rule change and drain
- Commit (
RuleChangeBatchCommit): the exclusive rendezvous (BeginRuleChange), then the set row (LockEntitlementSet, once for the batch), then, for each delta in resource-key order, the set row again (the same lock) and the rule row insidecore.commit_rule_change. At or below the sync cap the same transaction then takes each carrying pool in ascendingpool_idorder (ListPoolsCarryingSet):settlePoollocks the pool row throughLockPools, one pool per call, then the obligation row and the pool row again insidecore.settle_obligation, once per act. - Drain (
DrainChange, called by the commit's own drain above the cap and by the poller's activity): one short transaction per owed pool, opened throughBeginMaterializing. It takes the rendezvous, the pool row (settlePool, throughLockPools), then the obligation row of each owed act in the batch's order. A failed pool rolls back, andrecordObligationFailurerecords the attempt in a second transaction, also opened asMaterializing: the rendezvous, then, throughMarkObligationFailedandcore.settle_obligation, the obligation row of each owed act and after it the pool row. That is the reverse of the drain's pool-then-obligation order (#196).
Grant acts
All of these run in a transaction from BeginMaterializing: the operator
handlers, ExpireGrantActivity and the demo seed.
- Issue or confer a grant (
ConferGrantTx, whichConferGrant, provisioning andReapplyDefaultsForPoolcall): the rendezvous, the grant insert, then the pool row insidecore.confer, then materialization. - Revoke (
RevokeGrantTx): the rendezvous, the organization's default pool row (LockPools), the lineage head row, thencore.end_conferral(its pool locks are already held), resumption (the pool row again), the floor restoration (core.confer) and materialization. - Expire (
ExpireGrantTx): the rendezvous, then the pool row throughLockPoolOfGrant, then the grant update. It does not take the lineage head row. A grant that names no organization has no pool, so the lock, resumption and materialization are skipped. - Extend a waiting grant (
ExtendWaitingGrantTx): the rendezvous, the pool row (LockPoolOfGrant), the lineage head row. - Manage (
ManageGrantTx): the rendezvous, the pool row (LockPools), the lineage head row, then eitherExtendGrantTx(inside itcore.conferandcore.end_conferral, whose pool lock is already held) orExtendWaitingGrantTx(the same pool row again). - Resume replaced grants (
ResumeReplacedGrantsTx): called inside the transactions above and in the subscription reconcile. It takes the pool row first and reads the resumable grants only after the lock is held, so an expiry, revocation, mark change or extension is either wholly visible to it or waits for it. Each resumed grant callscore.confer.
Subscription reconcile
ReconcileSubscription reads the subscription from Stripe before it opens a
transaction. The transaction then takes the rendezvous, then the subscription
advisory lock, then the rest by branch.
- Active:
reconcileItemstakes the pool row (LockPools) after the subscription lock and beforecore.sync_source_status('active'). It then mirrors the items, reads the live provisions, then the shapes of the desired products and the subscription's displacements, all under the pool row, and confers a missing product unless another plan took one of its ladders from the subscription and still holds it (core.confer), changes a quantity (core.update_conferral_bounds) and ends a surplus provision (core.end_conferral); each of those functions takes the row the transaction already holds. Materialization follows.convergeScheduledChangesthen writes the scheduled-change ledger. A spent Stripe subscription schedule it finds is released after the commit (releaseSpentSchedule), with no lock held. When the release succeeds and the schedule held a downgrade row still scheduled,supersedeReleasedDowngradesopens a second transaction, takes the subscription advisory lock and supersedes those rows; a released schedule that held none opens no second transaction. It takes no rendezvous and no pool row: it writes only scheduled-change rows. - Ended:
core.end_conferralby subscription (the pool row of each provision it ends), then, only when something ended, resumption (the pool row), then the pool row again (LockPools, which is the first take when nothing ended), then the floor restoration (ReapplyDefaultsIfVacant, which confers throughcore.confer), then materialization. - Suspended:
reconcileSuspendedtakes the pool row (LockPools) beforecore.sync_source_status('suspended'), then materializes when it suspended a provision.
No path takes a pool row and then a subscription's advisory lock, so the order cannot wait in a cycle with a grant act, which takes the pool row and never a subscription's lock.
Tier add, tier removal, default change and reorder
The tier add, the tier removal, the tier delete and the reorder's
renumbering run in transactions from BeginRuleChange, which takes the
exclusive rendezvous: each changes a ladder's tiers, and core.confer reads a
ladder's ranks and a product's shape in separate statements, so no conferral
may run while one of them commits (#213). The default change's T0 takes the
exclusive rendezvous too: a move reads the type's default and its rank 0, and a
default saved between those reads would mix two catalogs. What follows T0 runs
in transactions that take the shared rendezvous: Retry's own and the drain's
listing, moves and last probe come from BeginMaterializing; the listing, the
moves and the last probe inside the request that made the act come from
MaterializingWithin, which sets the request's shorter lock timeout and its
transaction timeout before the rendezvous statement.
-
Tier add (
entitlements.AddTier): the exclusive rendezvous, then the reads that decide the add, inside the transaction: the product (a product no row names, or one not published, refuses the add) and the count of the organizations holding a live provision of it (aboveplanActHolderCapthe add is refused), then the pool row of every pool holding a live provision of the product throughLockPools, which sorts and deduplicates them, so they lock in ascendingpool_idorder. The provisions are read again under the locks, then the ladder's name, the act row and its detail row (core.record_plan_act), the tier insert (its trigger takes the product rowFOR SHAREand holds it to the commit), and the collision read (ListTierAddCollisions), which refuses a pool that already holds a live position on the ladder or holds two live provisions of the product before any alignment. Then, for each pool in the same order,core.align_conferral_shapefor each of its provisions in creation order and one materialization. It runs throughrunPlanAct. -
Tier removal (
entitlements.RemoveTier): the exclusive rendezvous, then the reads that decide the removal, all inside the transaction and before any pool row: the count of the organizations holding a live position on the tier (CountLiveTierHolders), the ladder's tiers and the org types (ListOrgTypes), from whichTierRemovalRefusaldecides (aboveplanActHolderCapthe removal is refused; the only tier of a ladder an org type defaults to is refused under either disposition), the names of the ladder and the product, and the tier's holders. A failed read refuses. Then the pool row of every holder throughLockPools, which sorts and deduplicates them, so they lock in ascendingpool_idorder, and the holders are read again under the locks. Then the act row and its detail row (core.record_plan_act), the tier delete and the renumbering. Then, for each pool in the same order, the incumbents end (core.end_conferral), the remaining positions align (core.align_conferral_shape), the floor is restored when the disposition is migrate, with the act's id on the grants it creates, and the pool materializes. It runs throughrunPlanAct. -
Tier delete without holders (
entitlements.DeleteTier): the exclusive rendezvous, then the reads that decide the delete, all inside the transaction: the count of the tier's live holders (positions that areactiveorsuspended), the ladder's tiers and the org types (ListOrgTypes), from whichLastTierRefusaldecides. A failed read refuses the delete. Then the names of the ladder and the product, the act row and its detail row (core.record_plan_act), the tier delete and the renumbering. It takes no pool lock: a tier with no live holder has no pool to reconcile, and the guard holds because the exclusive rendezvous keeps any conferral from placing a position on the tier between the count and the delete. The delete runs throughrunPlanAct. -
Default change, T0 (
entitlements.ChangeOrgTypeDefault, throughrunPlanAct): the exclusive rendezvous, then the org type's row (LockOrgType,FOR NO KEY UPDATE), the reads (the candidate's rank 0 and the names), the type's update (the row it already holds),core.record_plan_act(the act, its detail row and the reached type, with one index probe for the type's newest organization) and, when the operator chose no disposition, the gate (core.outgoing_default_exists, index probes). It takes no pool row and no obligation row, so its hold does not grow with the number of organizations. The request's deadline covers the two waits it can make, the rendezvous and the type's row. -
Reorder (
entitlements.ReorderTiers, throughrunPlanAct): T0 takes the exclusive rendezvous and no pool or obligation row. Inside it: the ladder's tiers are listed and the submitted order reconciled against them, the names read, the ranks renumbered (RenumberTiers), the act recorded (core.record_plan_act, which reads the org types that default to the ladder and records them as the types the act reaches, only when rank 0 changed), and, when no disposition was chosen and the act reaches a type, the gate (core.outgoing_default_exists), which refuses the whole T0. Then the listing batches and the moves run as for a default change: the shared rendezvous,core.write_plan_act_obligations, andEnactObligationfor each organization, which reads the type's current default and its rank 0 when it runs, not the rank 0 the reorder saved. A reorder that keeps rank 0 reaches no type and writes no obligation. -
The listing (
ListPlanActBatch, after T0): the shared rendezvous with the lock timeout and transaction timeout the caller gives (the request's shorter ones, or the drain's 5 s), thencore.write_plan_act_obligations: the batch's pool rowsFOR KEY SHAREin ascendingpool_idorder, the open obligations of earlier acts on those poolsFOR UPDATEin the same order and their update tosuperseded, and the inserts in (org type, organization id) order. A deadlock between two batches of one act is benign: the next pass lists the batch again. -
One organization's move (
EnactObligation, throughrunPlanAct): the shared rendezvous, the pool row (LockPools), then the obligation row (FOR UPDATE, taken bycore.lock_plan_act_obligation, which theLockPlanActObligationquery calls; the function reads the type's owner in the same statement; a move skips an obligation that is not pending or whose act no longer owns the type), then the reads (the type's default and the pool's positions) and the bucket's work (ReapplyDefaultsIfVacant,ConferGrantTxfor a grandfathered incumbent, orcore.end_conferral,ReapplyDefaultsIfVacantand materialization for a migration), which take the same pool row again, andcore.settle_plan_act_obligation. A failure on one organization leaves the others alone. -
Recording a failed move (
core.record_plan_act_obligation_failure): a plain transaction that updates the one obligation row, and only while it is stillpending. It takes no rendezvous and no pool row, so it cannot wait in a cycle with a move. -
The act's last probe (
DrainPlanAct, once the act is done and settled any obligation as migrated or grandfathered): the shared rendezvous, thencore.outgoing_default_exists, so the walk over the index entries the moves ended happens outside the next T0's exclusive hold. -
Retry (
entitlements.RetryPlanAct): the shared rendezvous,core.record_plan_act(the Retry act and its detail row), thencore.requeue_plan_act_obligations, which locks the retried act's failed obligations of the types it still ownsFOR UPDATEin ascendingpool_idorder and updates them. A Retry that requeues nothing rolls back.No two of these wait on each other in a cycle. T0 holds the exclusive rendezvous and takes no pool or obligation row, so it waits only for the rendezvous and the type's row. Every path that takes both a pool row and an obligation row takes the pool row first, and a listing batch takes its pool rows
FOR KEY SHARE, which conflicts with a move'sFOR UPDATE, before it touches any obligation. The two writers of several obligation rows, a batch's supersession and a Retry's requeue, take them in ascending order and touch disjoint rows.
Invoice reconcile
invoiceReconcile.converge opens a plain transaction, with no rendezvous, and
takes the invoice advisory lock. It writes invoice, line and payment rows
under that lock and touches no pool.
Product sync
catalogsync.Sync opens a plain transaction, takes the product advisory
lock, re-reads the price and product mappings, writes the pending mappings
and enqueues the outbox rows. It takes no pool lock and no rendezvous. A sync
that commits nothing returns with its transaction open, and
SyncProductToStripe rolls it back after it has rendered its answer, so the
lock is held until then.
FedWiki at the handler
withWorkspaceTx takes the workspace advisory lock, then runs the handler's
statements. A swap (swapActiveSite) and a rotation with a cooldown
(changeStatus) then record a lifecycle request, which takes the lifecycle
instance lock of the activated site. A swap records a second request for the
parked site, which takes that instance's lock after the first. The swap policy
row is upserted last. The handler takes no usage-row lock and no site-row
lock. A status change without a cooldown skips the workspace lock and records
its request in a transaction of its own, taking only the instance lock.
At the slot gate
HoldSiteSlotActivity runs usage.Hold in one transaction at READ COMMITTED.
It takes the request's lifecycle instance lock, then, for an activation, the
site row by id (LockSiteStatusByID), then reads the pool and its limit
without a lock, then the usage row (LockNumericUsage), then counts in a
statement of its own (CountSlotsByPool), then inserts the request's hold,
then recounts. A hold row is inserted only by its request's own workflow, and
deleted by that workflow, by a status reconcile that settles the request, or
by settle's finish, each under the request's instance lock, so no two
transactions wait on one hold row. The gate takes no workspace advisory lock.
When recording an outcome
The workflow activities that finish a lifecycle request, and settle, run
finishAllWith, one transaction at READ COMMITTED: the lifecycle instance
locks, in the order the handler recorded the requests (the activated site
first, then the parked site); then the activity's own site-row writes; then
the requests' holds are deleted (DeleteSlotHolds); then the usage row
(usage.Recount: LockNumericUsage, the count, the write); then the swap's
guard; then the request rows (FinishLifecycleRequest).
RecordCreatedSiteActivity: the site row insert.RecordSiteStatusActivity: the site row (LockSiteStatusByDomain).RecordSwapActivity: the site row of the parked site, then the site row of the activated site; the guard reads the pool's limit after the recount.RecordSiteDeletedActivityandFinishLifecycleRequestsActivity(every declined or failed outcome): no site row.DeleteFedWikiSiteActivitydeletes the site row and recounts in a transaction of its own, before the record step, which recounts again.
A pool with no usage row takes no recount lock and writes no count; the transaction commits its other writes.
So a transaction takes its site rows first, the usage row after them, and the request rows last. Its hold rows fall anywhere in that order: only the holder of their request's instance lock writes them, so no other transaction waits on one. The slot gate inserts its hold after the usage row's lock and count.
In the downgrade sweep and at boot
- Park (
parkSite): the farm call first, with no lock held, then one transaction (sweepLocally, at READ COMMITTED) that takes the site row, writes the status and the mark, and recounts the pool. - Reactivate (
reactivateSite): the farm call first, with no slot taken, then the same transaction: the site row, the status, the recount, and the pool's limit read in the same transaction; a figure above the limit rolls the transaction back. - Recount at boot (
usage.RecountAll, run by the domains reconciliation, and at the end of each sync pass): one transaction per pool that takes the usage row withLockNumericUsageand counts, then writes the figure, with no other lock. - Settle (
SettleOpenRequests): a closed activation's hold is read (SlotHoldExists) with no lock. A request that holds a slot is handed to the status reconcile, which settle starts without finishing it, so settle takes no lock and leaves the hold in place. Every other request it finishes goes throughfinishAllWith, as above. - Status reconcile (
ReconcileFedWikiSiteStatusWorkflow): reads the request, the site row and the farm with no lock held. Its outcome ends infinishAllWith, as above:RecordSiteStatusActivitywhen the farm holds the site in the requested status,FinishLifecycleRequestsActivityotherwise. When the site row is gone it ends throughFinishLifecycleRequestsActivitywithout reading the farm. Each removes the hold and recounts. - Provider registration (
RegisterProviders): the resource-key rows in byte order. - Domains reconciliation at boot (
reconcile_domains.goin the FedWiki package): oneRegistry.WithLocktransaction per orphaned placement or adopted site.
In the farm sync
SyncSitesToDBActivity reads its sets with no lock held (ListAllSites,
ListDomainsWithOpenRequests, and ListDomainsWithRequestsUpdatedSince when
the input carries the moment the farm list was taken), then writes one site at
a time. No transaction holds more than one site row, and none takes a
lifecycle instance lock or a workspace lock.
- A farm site with no row (
insert): one transaction at READ COMMITTED that inserts the row (InsertSiteFromFarm,ON CONFLICT DO NOTHING, so a row a create recorded after the pass read its rows is left alone) and then recounts the pool. - A listed status equal to the row's: one statement in autocommit
(
SetSiteObservedByDomain) that writes the observed storage and last-modified time. It holds the row's lock for that statement only, writes no status and takes no usage row. - A listed status different from the row's (
writeStatus): one transaction at READ COMMITTED that takes the site row (LockSiteStatusByDomain), then reads whether a request on the site is still accepted (SiteHasOpenRequest) and whether one was updated at or after the moment the entry was read (SiteHasRequestUpdatedSince), and commits without a write when either answers yes. Otherwise it writes the status and recounts the pool. A record step that writes a status takes the same site row, so a request either committed before the sync's lock, and the two reads see it, or takes the row after the sync's commit and writes its own status last. - A farm-absent row of the System workspace: one transaction at READ COMMITTED that deletes the row and recounts the pool. The name is released after the commit.
- The end of the pass (
usage.RecountAll): one transaction per pool, as at boot.
A pool with no usage row, the System tenant's, takes no recount lock and writes no count; the insert, the status write and the delete commit.
Lifecycle intake
RecordLifecycleRequest takes the instance lock first, then reads the latest
generation, then inserts. RecordFirstLifecycleRequest takes the same lock
once and reads the latest generation once: it refuses a later generation of an
instance that already holds a request, then decides as
RecordLifecycleRequest does. Called with a *sql.DB, each opens its own
transaction; called with a transaction, each uses the caller's, which is how
the handler records under the workspace lock.
DeleteUnstartedLifecycleRequest, which removes a request whose workflow never
started, takes no instance lock, so a repeat that races it may be answered from
either side of the delete.
Desired-state relay
- Claim (
ProcessDesiredStateOutbox):ClaimDesiredStateOutboxis one statement that locks the pending rowsSKIP LOCKEDand leases them. The relay never takes the rendezvous and never locks a source row. - One recompute (
recomputeAddress, and the same sequence inreadBackItemandwritePushAnswer): one READ COMMITTED transaction that takes the address advisory lock, then the record row (LockDesiredStateRecord), then computes the content in a new statement, then updates or inserts the record. Every transaction that writes a record's content or generation, or a projection, takes the address lock first. - Dispatch stamp (
StampDesiredStateRecordsDispatched): one statement that locks the records in identifier order before it writes. It takes no address lock.
Registry
domains.Registry.WithLock opens a plain transaction and takes the registry
lock before anything else. Every composite operation, including the FedWiki
create, release and sync projections, runs on the *Tx it passes, so the lock
is taken once. The comment on registryAllocationLock states the rule for a
transaction that would touch both the registry and a usage row: registry lock
first. No transaction does both today: the slot gate and the recounts are
workflow activities that run their own transactions, apart from the
transaction that allocates or releases the name.
Rules every path keeps
- A transaction that materializes a pool takes the shared rendezvous as its
first statement, through
BeginMaterializingor, inside the request that made a plan act,MaterializingWithin; a plan act's listing batch, a move, the last probe and a Retry are such transactions. A rule change, an operator's tier change (the tier add, the tier delete, the tier removal, the reorder's renumbering) and a default change's T0 take the exclusive one throughBeginRuleChange, and may materialize under it; the demonstration seed adds its ladder's tiers on the plain handle, outside the rendezvous.core.assert_rendezvousraisesmaterialization_rendezvous_missingwhen a function that materializes or settles runs without it. - The pool row is locked before positions or entitlements change. The five conferral and settlement functions lock the row themselves, so a caller that goes straight to one of them still holds it.
- A transaction that locks several pools takes them in ascending
pool_idorder, throughLockPools. The rule change, the tier add and the tier removal do. A plan act's listing batch locks its pools for key-share in the same order, in SQL (ORDER BY pool_id FOR KEY SHARE). The deferred drain and a plan act's move lock one pool per transaction. - The grant lineage head row is locked after the pool row, never before.
- A FedWiki transaction takes its locks in this order, skipping those it does not need: the lifecycle instance locks, the site rows, the usage row, then the lifecycle request rows. Hold rows have no place in the order, because only the holder of their request's instance lock writes them. The count under the usage row is a statement of its own after the lock, so it reads every commit that preceded the lock's grant.
- Lifecycle instance locks of one transaction are taken in the order the requests were recorded.
- A desired-state write takes the address lock, then the record row, then computes content in a new statement.
- Every advisory lock belongs to its transaction, so a commit or a rollback releases it, and an error never leaves one behind.
- A row lock on a table where the writer holds
UPDATEon no column is taken inside aSECURITY DEFINERfunction owned bycore_owner, because Postgres requiresUPDATEon at least one column forFOR UPDATE,FOR NO KEY UPDATE,FOR SHAREandFOR KEY SHARE, and the runtime user holds only whatconsole_runtimeholds. A move takes its lock oncore.plan_act_obligationsthis way, throughcore.lock_plan_act_obligation.
Where the paths differ
- The plan-ending paths take the pool lock at different points (#180).
Revocation and expiry lock the pool before they write. The subscription
reconcile's ended branch ends positions first, so the pool row is taken
inside
core.end_conferral, and takes it again for resumption and before the floor restoration. Each path then runs resumption, the floor restoration and materialization in the same order. - Advisory keys are built four ways (#186). The keys follow the four conventions above.
- Recording a failed drain locks the obligation row before the pool row (#196), where the drain and the commit take the pool first. The plan acts' failure recorder takes the obligation row alone, in a plain transaction, and no pool row, so it has no such order.