Add the workforce unit store: tables, models, read API and seed
What does this MR do and why?
Adds the workforce unit store for the Workforce Hierarchy. GitLab can already hold a tenant's people (!261107 (merged)). This MR adds the tree of workforce units, such as supervisory organizations, and the memberships of people in them.
It can answer which units sit under a unit, which people belong to that part of the organization, and which units a person belongs to. Nothing calls these reads yet, so users see no change. The sync and the dashboard filter build on them in follow-up issues.
What is in this MR:
- Table
workforce_units: external identifier, name, type, external parent identifier, external manager identifier, and atraversal_idsancestry path of unit ids. Units form one tree per type. The type is free-form text and cannot be changed once the unit exists. External identifiers are unique per tenant and unit type. - Table
workforce_unit_memberships: links a person to a unit. The unit's type is copied onto the membership, so a unique index on(workforce_person_id, unit_type)enforces one unit per type for each person. A foreign key on(workforce_unit_id, unit_type)keeps the copied type equal to the unit's. - Both tables use the same sharding key as
workforce_people:group_idororganization_id, enforced byCHECK (num_nonnulls(group_id, organization_id) = 1). Thegitlab_schemaisgitlab_main_org. - Models
Workforce::UnitandWorkforce::UnitMembership, and the scopesUnit.of_type,Unit.roots,Unit.subtree_of,Unit.for_person,UnitMembership.in_unit_subtreeandPerson.in_unit_subtree. Every scope adds the hierarchy filter itself. Workforce::Hierarchy.delete_for_tenantnow removes memberships, then people, then units, in batches of 1,000.- The development seed builds a supervisory unit tree, one unit per five people (10,000 units at 50,000 people), each managed by a seeded person, with a membership for 95% of people.
- A benchmark spec,
ee/spec/models/workforce/unit_subtree_benchmark_spec.rb, which runs only withBENCHMARK=1.
Out of scope, as stated in the issue: the sync, unit defects, unit types other than supervisory organizations, and any UI.
Decisions reviewers should know
-
A membership that links a person and a unit of different hierarchies is rejected by a model validation only.
Why
The foreign keys do not prevent it. A database guarantee needs composite foreign keys that include
workforce_hierarchy_id, which the draft SQL of the issue does not have. The sync writes with bulk upserts and builds both sides from one tenant's snapshot. The unit type is different: the database does enforce it, see decision 10. -
The model does not maintain
traversal_ids. The sync writes the paths, as the development seed does. The factory writes a valid path so specs can build trees. -
Unit.subtree_offilters by the unit's type as well as by its path.Why
A parent should always have the type of its children, but nothing in the database enforces that. The type filter keeps a unit of another type out of the subtree even if its path runs through it. The read still uses the
(workforce_hierarchy_id, traversal_ids)index, with the type as a filter. Specs plant a unit of another type inside a subtree. -
Both tables are
no_work_neededinconfig/organizations/transfer_support.yml, as the issue and https://gitlab.com/gitlab-org/gitlab/-/work_items/638003 ask. Neither holds a user reference. On GitLab.com every row is keyed by the root group'sgroup_id, withorganization_idNULL, so the rows follow the group when it moves to another organization and no write is needed. Tenant Scale: please confirm, because the file header says new tables must usesupported. -
delete_for_tenantdeletes memberships first, so deleting people and units cascades to nothing and no statement removes more than one batch of rows. The cascading foreign keys stay as a backstop. -
!269887 (merged), which stops using the people tree on
workforce_people, merged first. This MR is rebased on it: the development seed creates a flat list of people, and only the units form a tree. -
index_workforce_units_on_hierarchy_id_type_and_parent_xidon(workforce_hierarchy_id, unit_type, parent_external_xid)serves the children-of-a-unit lookup. The draft SQL of the issue has it withoutunit_type. The type was added in database review: external ids can repeat across types (decision 8), so the lookup has to filter by type. No scope in this MR reads children yet. The expected first caller is the unit sync in https://gitlab.com/gitlab-org/gitlab/-/work_items/638004, which has to find a unit's children when the unit moves to a new parent.Unit.rootsuses the index too, with the type as an index condition, in the plan below. -
The unique index on
workforce_unitsis(workforce_hierarchy_id, unit_type, external_xid). The draft SQL of the issue has(workforce_hierarchy_id, external_xid). The change is agreed with the epic author.Why
An HR source can number each kind of organization separately, so a supervisory organization and a cost center of one tenant could share an external id. A tenant-wide unique index would reject the second unit, or let an upsert overwrite the first. Phase 1 syncs one type only, so this matters for adding types later without a store change.
The new index starts with
(workforce_hierarchy_id, unit_type), so it also serves reads of one type. The separate index on those two columns from the draft SQL is therefore left out. -
index_workforce_units_on_hierarchy_id_type_and_idon(workforce_hierarchy_id, unit_type, id)is not in the draft SQL of the issue. Its consumer is the units connection in !271811 (merged), which pages the units of one hierarchy and type ordered by id. No scope in this MR uses it. With the index, the first page for a second tenant takes 0.05 ms, down from 1.9 ms: https://explain.depesz.com/s/wEjV.Why
Without it the page walks the primary key and filters out other tenants' rows, so its cost grows with the units of every tenant. The plans are in !271811 (merged). Ordering by
external_xidwould reuse the unique index, but keyset cursors would then carry external identifiers, which that API does not expose. -
The database keeps the type copied onto a membership equal to the unit's type. This was added in database review and is not in the draft SQL of the issue.
Why and how
The one-unit-per-type guarantee rests on the copied type being right. The model copies it in a callback, but the sync will write with bulk upserts, which skip callbacks.
- The foreign key from memberships to units is on
(workforce_unit_id, unit_type)and referencesworkforce_units (id, unit_type),ON DELETE CASCADE. It replaces the single-column foreign key onworkforce_unit_id. workforce_unitsgets a unique index on(id, unit_type)as the target of that foreign key.- On
workforce_unit_memberships, the index onworkforce_unit_idbecomes(workforce_unit_id, unit_type), so it covers both foreign key columns. The number of indexes on that table is unchanged.
- The foreign key from memberships to units is on
References
- Issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/638005
- Epic, Phase 1: https://gitlab.com/groups/gitlab-org/-/work_items/23625
- Parent initiative: https://gitlab.com/groups/gitlab-org/-/work_items/23251
- People store this builds on: !261107 (merged)
- Blocked by this MR, unit sync: https://gitlab.com/gitlab-org/gitlab/-/work_items/638004
- Organization transfer support: https://gitlab.com/gitlab-org/gitlab/-/work_items/638003
Screenshots or screen recordings
Not applicable: no UI change.
How to set up and validate locally
-
Run the migrations:
bundle exec rails db:migrate -
Seed a 50,000-person hierarchy with 10,000 units. It attaches to the first root group, or set
GROUP_ID=<root group id>.FILTER=99_workforce_hierarchy COUNT=50000 bundle exec rake db:seed_fu -
In the Rails console, run the reads:
h = Workforce::Hierarchy.last root = Workforce::Unit.roots(h, 'supervisory').first Workforce::Unit.subtree_of(root).count # => 10000 Workforce::Person.in_unit_subtree(root).count # => 47500 unit = h.units.where('array_length(traversal_ids, 1) = 4').first Workforce::Unit.subtree_of(unit).count Workforce::Person.in_unit_subtree(unit).count Workforce::Unit.for_person(unit.memberships.first.person) -
Check that the plan uses the traversal index:
Workforce::Unit.subtree_of(unit).explainExpect
Index Scan using index_workforce_units_on_hierarchy_id_and_traversal_ids.
Database queries
The SQL for every scope this MR adds, with local EXPLAIN (ANALYZE, BUFFERS) plans. No request path calls these scopes yet.
- Data: local GDK,
COUNT=50000seed, so one tenant with 50,000 people, 10,000 units and 47,500 memberships. The tables wereANALYZEd first. - Seq scans below are expected locally. With one tenant, every row matches
workforce_hierarchy_id, so the planner reads the table for whole-tenant reads. - Why local plans, not Database Lab: both tables are new and production has no rows for this feature, so a clone would hold the same synthetic data as the local seed.
Workforce::Unit.in_hierarchy(hierarchy) and Workforce::UnitMembership.in_hierarchy(hierarchy): the tenant filter every other read builds on. Not called on their own except by delete_for_tenant and the development seed, both shown further down.
Workforce::Unit.of_type(hierarchy, type)
SELECT "workforce_units".* FROM "workforce_units" WHERE "workforce_units"."workforce_hierarchy_id" = 6 AND "workforce_units"."unit_type" = 'supervisory'Plan
Seq Scan on workforce_units (cost=0.00..701.00 rows=10000 width=172) (actual time=0.004..0.494 rows=10000 loops=1)
Filter: ((workforce_hierarchy_id = 6) AND (unit_type = 'supervisory'::text))
Buffers: shared hit=551
Planning Time: 0.045 ms
Execution Time: 0.675 msWorkforce::Unit.roots(hierarchy, type)
SELECT "workforce_units".* FROM "workforce_units" WHERE "workforce_units"."workforce_hierarchy_id" = 6 AND "workforce_units"."unit_type" = 'supervisory' AND "workforce_units"."parent_external_xid" IS NULLPlan
Index Scan using index_workforce_units_on_hierarchy_id_type_and_parent_xid on workforce_units (cost=0.29..2.31 rows=1 width=172) (actual time=0.001..0.002 rows=1 loops=1)
Index Cond: ((workforce_hierarchy_id = 6) AND (unit_type = 'supervisory'::text) AND (parent_external_xid IS NULL))
Buffers: shared hit=3
Planning Time: 0.015 ms
Execution Time: 0.004 msWorkforce::Unit.subtree_of(unit): a half-open range on traversal_ids, filtered by the unit's type. Shown for a depth-4 unit with 518 units in its subtree.
SELECT "workforce_units".* FROM "workforce_units" WHERE "workforce_units"."workforce_hierarchy_id" = 6 AND "workforce_units"."unit_type" = 'supervisory' AND (workforce_units.traversal_ids >= '{2001,2002,2006,2017}' AND '{2001,2002,2006,2018}' > workforce_units.traversal_ids)Plan
Index Scan using index_workforce_units_on_hierarchy_id_and_traversal_ids on workforce_units (cost=0.41..197.14 rows=499 width=172) (actual time=0.004..0.048 rows=518 loops=1)
Index Cond: ((workforce_hierarchy_id = 6) AND (traversal_ids >= '{2001,2002,2006,2017}'::bigint[]) AND (traversal_ids < '{2001,2002,2006,2018}'::bigint[]))
Filter: (unit_type = 'supervisory'::text)
Buffers: shared hit=167
Planning Time: 0.027 ms
Execution Time: 0.059 msWorkforce::Unit.for_person(person)
SELECT "workforce_units".* FROM "workforce_units" WHERE "workforce_units"."workforce_hierarchy_id" = 6 AND "workforce_units"."id" IN (SELECT "workforce_unit_memberships"."workforce_unit_id" FROM "workforce_unit_memberships" WHERE "workforce_unit_memberships"."workforce_hierarchy_id" = 6 AND "workforce_unit_memberships"."workforce_person_id" = 640341)Plan
Nested Loop (cost=2.72..4.75 rows=1 width=172) (actual time=0.004..0.004 rows=1 loops=1)
Buffers: shared hit=7
-> HashAggregate (cost=2.44..2.45 rows=1 width=8) (actual time=0.003..0.003 rows=1 loops=1)
Group Key: workforce_unit_memberships.workforce_unit_id
Batches: 1 Memory Usage: 24kB
Buffers: shared hit=4
-> Index Scan using index_workforce_unit_memberships_on_person_id_and_unit_type on workforce_unit_memberships (cost=0.41..2.44 rows=1 width=8) (actual time=0.002..0.002 rows=1 loops=1)
Index Cond: (workforce_person_id = 640341)
Filter: (workforce_hierarchy_id = 6)
Buffers: shared hit=4
-> Index Scan using index_workforce_units_on_id_and_unit_type on workforce_units (cost=0.29..2.31 rows=1 width=172) (actual time=0.001..0.001 rows=1 loops=1)
Index Cond: (id = workforce_unit_memberships.workforce_unit_id)
Filter: (workforce_hierarchy_id = 6)
Buffers: shared hit=3
Planning:
Buffers: shared hit=14
Planning Time: 0.066 ms
Execution Time: 0.010 msWorkforce::UnitMembership.in_unit_subtree(unit): the memberships of a unit and of all its descendant units. Shown for the same depth-4 unit, with 2,590 memberships.
SELECT "workforce_unit_memberships".* FROM "workforce_unit_memberships" WHERE "workforce_unit_memberships"."workforce_hierarchy_id" = 6 AND "workforce_unit_memberships"."workforce_unit_id" IN (SELECT "workforce_units"."id" FROM "workforce_units" WHERE "workforce_units"."workforce_hierarchy_id" = 6 AND "workforce_units"."unit_type" = 'supervisory' AND (workforce_units.traversal_ids >= '{2001,2002,2006,2017}' AND '{2001,2002,2006,2018}' > workforce_units.traversal_ids))Plan
Nested Loop (cost=0.70..1295.53 rows=2369 width=76) (actual time=0.007..0.541 rows=2590 loops=1)
Buffers: shared hit=3793
-> Index Scan using index_workforce_units_on_hierarchy_id_and_traversal_ids on workforce_units (cost=0.41..197.14 rows=499 width=8) (actual time=0.005..0.063 rows=518 loops=1)
Index Cond: ((workforce_hierarchy_id = 6) AND (traversal_ids >= '{2001,2002,2006,2017}'::bigint[]) AND (traversal_ids < '{2001,2002,2006,2018}'::bigint[]))
Filter: (unit_type = 'supervisory'::text)
Buffers: shared hit=167
-> Index Scan using index_workforce_unit_memberships_on_unit_id_and_unit_type on workforce_unit_memberships (cost=0.29..2.15 rows=5 width=76) (actual time=0.000..0.001 rows=5 loops=518)
Index Cond: (workforce_unit_id = workforce_units.id)
Filter: (workforce_hierarchy_id = 6)
Buffers: shared hit=3626
Planning:
Buffers: shared hit=12
Planning Time: 0.113 ms
Execution Time: 0.591 msWorkforce::Person.in_unit_subtree(unit): the members of a unit and of all its descendant units. It nests Workforce::UnitMembership.in_unit_subtree(unit). Shown for the same depth-4 unit, with 2,590 people.
SELECT "workforce_people"."id", "workforce_people"."workforce_hierarchy_id", "workforce_people"."user_id", "workforce_people"."group_id", "workforce_people"."organization_id", "workforce_people"."created_at", "workforce_people"."updated_at", "workforce_people"."external_xid", "workforce_people"."full_name" FROM "workforce_people" WHERE "workforce_people"."workforce_hierarchy_id" = 6 AND "workforce_people"."id" IN (SELECT "workforce_unit_memberships"."workforce_person_id" FROM "workforce_unit_memberships" WHERE "workforce_unit_memberships"."workforce_hierarchy_id" = 6 AND "workforce_unit_memberships"."workforce_unit_id" IN (SELECT "workforce_units"."id" FROM "workforce_units" WHERE "workforce_units"."workforce_hierarchy_id" = 6 AND "workforce_units"."unit_type" = 'supervisory' AND (workforce_units.traversal_ids >= '{2001,2002,2006,2017}' AND '{2001,2002,2006,2018}' > workforce_units.traversal_ids)))Plan
Nested Loop (cost=1301.75..2152.29 rows=2369 width=81) (actual time=0.651..1.853 rows=2590 loops=1)
Buffers: shared hit=11563
-> HashAggregate (cost=1301.46..1325.15 rows=2369 width=8) (actual time=0.648..0.724 rows=2590 loops=1)
Group Key: workforce_unit_memberships.workforce_person_id
Batches: 1 Memory Usage: 241kB
Buffers: shared hit=3793
-> Nested Loop (cost=0.70..1295.53 rows=2369 width=8) (actual time=0.006..0.528 rows=2590 loops=1)
Buffers: shared hit=3793
-> Index Scan using index_workforce_units_on_hierarchy_id_and_traversal_ids on workforce_units (cost=0.41..197.14 rows=499 width=8) (actual time=0.005..0.064 rows=518 loops=1)
Index Cond: ((workforce_hierarchy_id = 6) AND (traversal_ids >= '{2001,2002,2006,2017}'::bigint[]) AND (traversal_ids < '{2001,2002,2006,2018}'::bigint[]))
Filter: (unit_type = 'supervisory'::text)
Buffers: shared hit=167
-> Index Scan using index_workforce_unit_memberships_on_unit_id_and_unit_type on workforce_unit_memberships (cost=0.29..2.15 rows=5 width=16) (actual time=0.000..0.001 rows=5 loops=518)
Index Cond: (workforce_unit_id = workforce_units.id)
Filter: (workforce_hierarchy_id = 6)
Buffers: shared hit=3626
-> Index Scan using workforce_people_pkey on workforce_people (cost=0.29..0.35 rows=1 width=81) (actual time=0.000..0.000 rows=1 loops=2590)
Index Cond: (id = workforce_unit_memberships.workforce_person_id)
Filter: (workforce_hierarchy_id = 6)
Buffers: shared hit=7770
Planning:
Buffers: shared hit=21
Planning Time: 0.142 ms
Execution Time: 1.911 msThe same read for the root unit returns 47,500 people in 14.6 ms through hash joins over sequential scans, which is the cheaper plan when the subtree is the whole tenant.
Group and organization deletion: Workforce::Hierarchy.delete_for_tenant runs the three statements below, in this order, each repeated until a batch deletes fewer than 1,000 rows. The plans show one batch per table, taken in that order in a rolled-back transaction. The people and units deletes fire the foreign key triggers towards workforce_unit_memberships (fk_2459d6a5fd and fk_workforce_unit_memberships_unit_id_and_unit_type), which find nothing to cascade to because the memberships are already gone.
DELETE FROM "workforce_unit_memberships" WHERE ("workforce_unit_memberships"."id") IN (SELECT "workforce_unit_memberships"."id" FROM "workforce_unit_memberships" WHERE "workforce_unit_memberships"."workforce_hierarchy_id" = 6 LIMIT 1000)Plan
Delete on workforce_unit_memberships (cost=41.29..1104.81 rows=0 width=0) (actual time=1.035..1.035 rows=0 loops=1)
Buffers: shared hit=4152 read=3 dirtied=15
-> Nested Loop (cost=41.29..1104.81 rows=1000 width=38) (actual time=0.309..0.830 rows=1000 loops=1)
Buffers: shared hit=3138 read=3
-> HashAggregate (cost=41.00..51.00 rows=1000 width=40) (actual time=0.217..0.252 rows=1000 loops=1)
Group Key: "ANY_subquery".id
Batches: 1 Memory Usage: 193kB
Buffers: shared hit=141
-> Subquery Scan on "ANY_subquery" (cost=0.00..38.50 rows=1000 width=40) (actual time=0.029..0.157 rows=1000 loops=1)
Buffers: shared hit=141
-> Limit (cost=0.00..28.50 rows=1000 width=8) (actual time=0.026..0.105 rows=1000 loops=1)
Buffers: shared hit=141
-> Seq Scan on workforce_unit_memberships workforce_unit_memberships_1 (cost=0.00..1353.75 rows=47500 width=8) (actual time=0.025..0.070 rows=1000 loops=1)
Filter: (workforce_hierarchy_id = 6)
Buffers: shared hit=141
-> Index Scan using workforce_unit_memberships_pkey on workforce_unit_memberships (cost=0.29..1.07 rows=1 width=14) (actual time=0.001..0.001 rows=1 loops=1000)
Index Cond: (id = "ANY_subquery".id)
Buffers: shared hit=2997 read=3
Planning:
Buffers: shared hit=3 read=3
Planning Time: 0.460 ms
Execution Time: 1.647 msDELETE FROM "workforce_people" WHERE ("workforce_people"."id") IN (SELECT "workforce_people"."id" FROM "workforce_people" WHERE "workforce_people"."workforce_hierarchy_id" = 6 LIMIT 1000)Plan
Delete on workforce_people (cost=52.16..1552.54 rows=0 width=0) (actual time=3.607..3.607 rows=0 loops=1)
Buffers: shared hit=3034 read=3 dirtied=17
-> Hash Semi Join (cost=52.16..1552.54 rows=1000 width=38) (actual time=0.174..2.958 rows=1000 loops=1)
Hash Cond: (workforce_people.id = "ANY_subquery".id)
Buffers: shared hit=1017
-> Seq Scan on workforce_people (cost=0.00..1358.00 rows=50000 width=14) (actual time=0.001..1.583 rows=50000 loops=1)
Buffers: shared hit=858
-> Hash (cost=39.66..39.66 rows=1000 width=40) (actual time=0.171..0.172 rows=1000 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 79kB
Buffers: shared hit=159
-> Subquery Scan on "ANY_subquery" (cost=0.00..39.66 rows=1000 width=40) (actual time=0.002..0.138 rows=1000 loops=1)
Buffers: shared hit=159
-> Limit (cost=0.00..29.66 rows=1000 width=8) (actual time=0.001..0.093 rows=1000 loops=1)
Buffers: shared hit=159
-> Seq Scan on workforce_people workforce_people_1 (cost=0.00..1483.00 rows=50000 width=8) (actual time=0.001..0.062 rows=1000 loops=1)
Filter: (workforce_hierarchy_id = 6)
Buffers: shared hit=159
Planning:
Buffers: shared hit=5 read=1
Planning Time: 0.219 ms
Trigger for constraint fk_2459d6a5fd: time=1.397 calls=1000
Execution Time: 5.038 msDELETE FROM "workforce_units" WHERE ("workforce_units"."id") IN (SELECT "workforce_units"."id" FROM "workforce_units" WHERE "workforce_units"."workforce_hierarchy_id" = 6 LIMIT 1000)Plan
Delete on workforce_units (cost=54.78..743.16 rows=0 width=0) (actual time=2.177..2.177 rows=0 loops=1)
Buffers: shared hit=2581 read=14 dirtied=27
-> Hash Semi Join (cost=54.78..743.16 rows=1000 width=38) (actual time=1.004..1.878 rows=1000 loops=1)
Hash Cond: (workforce_units.id = "ANY_subquery".id)
Buffers: shared hit=552 read=14
-> Seq Scan on workforce_units (cost=0.00..651.00 rows=10000 width=14) (actual time=0.002..0.607 rows=10000 loops=1)
Buffers: shared hit=551
-> Hash (cost=42.28..42.28 rows=1000 width=40) (actual time=1.001..1.001 rows=1000 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 79kB
Buffers: shared hit=1 read=14
-> Subquery Scan on "ANY_subquery" (cost=0.29..42.28 rows=1000 width=40) (actual time=0.273..0.963 rows=1000 loops=1)
Buffers: shared hit=1 read=14
-> Limit (cost=0.29..32.28 rows=1000 width=8) (actual time=0.272..0.916 rows=1000 loops=1)
Buffers: shared hit=1 read=14
-> Index Only Scan using index_workforce_units_on_hierarchy_id_type_and_id on workforce_units workforce_units_1 (cost=0.29..320.29 rows=10000 width=8) (actual time=0.271..0.886 rows=1000 loops=1)
Index Cond: (workforce_hierarchy_id = 6)
Heap Fetches: 0
Buffers: shared hit=1 read=14
Planning:
Buffers: shared hit=6
Planning Time: 0.061 ms
Trigger for constraint fk_workforce_unit_memberships_unit_id_and_unit_type: time=2.977 calls=1000
Execution Time: 5.187 msDevelopment seed only: 99_workforce_hierarchy.rb clears the seeded tenant before re-seeding. It never runs in production.
DELETE FROM "workforce_unit_memberships" WHERE "workforce_unit_memberships"."workforce_hierarchy_id" = 6
DELETE FROM "workforce_people" WHERE "workforce_people"."workforce_hierarchy_id" = 6
DELETE FROM "workforce_units" WHERE "workforce_units"."workforce_hierarchy_id" = 6Migrations
Run locally on the final commit. transaction_open? and quote_column_name lines are removed for length.
Up: bundle exec rails db:migrate
main: == 20261111095205 CreateWorkforceUnits: migrating =============================
main: -- create_table(:workforce_units)
main: -> 0.0823s
main: == 20261111095205 CreateWorkforceUnits: migrated (0.0883s) ====================
main: == 20261111095206 CreateWorkforceUnitMemberships: migrating ===================
main: -- create_table(:workforce_unit_memberships)
main: -> 0.0122s
main: == 20261111095206 CreateWorkforceUnitMemberships: migrated (0.0166s) ==========
main: == 20261111095207 AddFksToWorkforceUnits: migrating ===========================
main: -- execute("LOCK TABLE workforce_hierarchies, workforce_units IN SHARE ROW EXCLUSIVE MODE")
main: -> 0.0005s
main: -- execute("ALTER TABLE workforce_units ADD CONSTRAINT fk_887d24b73a FOREIGN KEY (workforce_hierarchy_id) REFERENCES workforce_hierarchies (id) ON DELETE CASCADE NOT VALID;")
main: -> 0.0012s
main: -- execute("ALTER TABLE workforce_units VALIDATE CONSTRAINT fk_887d24b73a;")
main: -> 0.0024s
main: -- execute("LOCK TABLE namespaces, workforce_units IN SHARE ROW EXCLUSIVE MODE")
main: -> 0.0004s
main: -- execute("ALTER TABLE workforce_units ADD CONSTRAINT fk_0bbe82c17a FOREIGN KEY (group_id) REFERENCES namespaces (id) ON DELETE CASCADE NOT VALID;")
main: -> 0.0010s
main: -- execute("ALTER TABLE workforce_units VALIDATE CONSTRAINT fk_0bbe82c17a;")
main: -> 0.0058s
main: -- execute("LOCK TABLE organizations, workforce_units IN SHARE ROW EXCLUSIVE MODE")
main: -> 0.0004s
main: -- execute("ALTER TABLE workforce_units ADD CONSTRAINT fk_cbfa0429c2 FOREIGN KEY (organization_id) REFERENCES organizations (id) ON DELETE CASCADE NOT VALID;")
main: -> 0.0012s
main: -- execute("ALTER TABLE workforce_units VALIDATE CONSTRAINT fk_cbfa0429c2;")
main: -> 0.0024s
main: == 20261111095207 AddFksToWorkforceUnits: migrated (0.0670s) ==================
main: == 20261111095208 AddFksToWorkforceUnitMemberships: migrating =================
main: -- execute("LOCK TABLE workforce_hierarchies, workforce_unit_memberships IN SHARE ROW EXCLUSIVE MODE")
main: -> 0.0004s
main: -- execute("ALTER TABLE workforce_unit_memberships ADD CONSTRAINT fk_477cabfad2 FOREIGN KEY (workforce_hierarchy_id) REFERENCES workforce_hierarchies (id) ON DELETE CASCADE NOT VALID;")
main: -> 0.0007s
main: -- execute("ALTER TABLE workforce_unit_memberships VALIDATE CONSTRAINT fk_477cabfad2;")
main: -> 0.0013s
main: -- execute("LOCK TABLE workforce_people, workforce_unit_memberships IN SHARE ROW EXCLUSIVE MODE")
main: -> 0.0005s
main: -- execute("ALTER TABLE workforce_unit_memberships ADD CONSTRAINT fk_2459d6a5fd FOREIGN KEY (workforce_person_id) REFERENCES workforce_people (id) ON DELETE CASCADE NOT VALID;")
main: -> 0.0007s
main: -- execute("ALTER TABLE workforce_unit_memberships VALIDATE CONSTRAINT fk_2459d6a5fd;")
main: -> 0.0023s
main: -- execute("LOCK TABLE workforce_units, workforce_unit_memberships IN SHARE ROW EXCLUSIVE MODE")
main: -> 0.0005s
main: -- execute("ALTER TABLE workforce_unit_memberships ADD CONSTRAINT fk_workforce_unit_memberships_unit_id_and_unit_type FOREIGN KEY (workforce_unit_id, unit_type) REFERENCES workforce_units (id, unit_type) ON DELETE CASCADE NOT VALID;")
main: -> 0.0007s
main: -- execute("ALTER TABLE workforce_unit_memberships VALIDATE CONSTRAINT fk_workforce_unit_memberships_unit_id_and_unit_type;")
main: -> 0.0009s
main: -- execute("LOCK TABLE namespaces, workforce_unit_memberships IN SHARE ROW EXCLUSIVE MODE")
main: -> 0.0004s
main: -- execute("ALTER TABLE workforce_unit_memberships ADD CONSTRAINT fk_5cb4fb06d0 FOREIGN KEY (group_id) REFERENCES namespaces (id) ON DELETE CASCADE NOT VALID;")
main: -> 0.0009s
main: -- execute("ALTER TABLE workforce_unit_memberships VALIDATE CONSTRAINT fk_5cb4fb06d0;")
main: -> 0.0010s
main: -- execute("LOCK TABLE organizations, workforce_unit_memberships IN SHARE ROW EXCLUSIVE MODE")
main: -> 0.0004s
main: -- execute("ALTER TABLE workforce_unit_memberships ADD CONSTRAINT fk_1d185037d5 FOREIGN KEY (organization_id) REFERENCES organizations (id) ON DELETE CASCADE NOT VALID;")
main: -> 0.0008s
main: -- execute("ALTER TABLE workforce_unit_memberships VALIDATE CONSTRAINT fk_1d185037d5;")
main: -> 0.0008s
main: == 20261111095208 AddFksToWorkforceUnitMemberships: migrated (0.1032s) ========Down: the four migrations, newest first
Run as bundle exec rails db:migrate:down:main VERSION=<version> for each of the four, which is what db:rollback STEP=4 does on a database with no newer migrations. My local database has newer migrations from other branches.
main: == 20261111095208 AddFksToWorkforceUnitMemberships: reverting =================
main: -- execute("LOCK TABLE workforce_units, workforce_unit_memberships IN ACCESS EXCLUSIVE MODE")
main: -> 0.0004s
main: -- remove_foreign_key(:workforce_unit_memberships, :workforce_units, {name: "fk_workforce_unit_memberships_unit_id_and_unit_type"})
main: -> 0.0026s
main: -- remove_foreign_key(:workforce_unit_memberships, {column: :workforce_hierarchy_id})
main: -> 0.0018s
main: -- remove_foreign_key(:workforce_unit_memberships, {column: :workforce_person_id})
main: -> 0.0015s
main: -- remove_foreign_key(:workforce_unit_memberships, {column: :group_id})
main: -> 0.0018s
main: -- remove_foreign_key(:workforce_unit_memberships, {column: :organization_id})
main: -> 0.0015s
main: == 20261111095208 AddFksToWorkforceUnitMemberships: reverted (0.0894s) ========
main: == 20261111095207 AddFksToWorkforceUnits: reverting ===========================
main: -- remove_foreign_key(:workforce_units, {column: :workforce_hierarchy_id})
main: -> 0.0023s
main: -- remove_foreign_key(:workforce_units, {column: :group_id})
main: -> 0.0025s
main: -- remove_foreign_key(:workforce_units, {column: :organization_id})
main: -> 0.0020s
main: == 20261111095207 AddFksToWorkforceUnits: reverted (0.0940s) ==================
main: == 20261111095206 CreateWorkforceUnitMemberships: reverting ===================
main: -- drop_table(:workforce_unit_memberships)
main: -> 0.0626s
main: == 20261111095206 CreateWorkforceUnitMemberships: reverted (0.0734s) ==========
main: == 20261111095205 CreateWorkforceUnits: reverting =============================
main: -- drop_table(:workforce_units)
main: -> 0.0544s
main: == 20261111095205 CreateWorkforceUnits: reverted (0.0631s) ====================Expected table growth
Both tables are table_size: small (under 10 GB, indexes included).
- Per tenant, upper bound: 50,000 people, 10,000 supervisory organizations, and at most one membership per person. Phase 1 syncs one unit type, so that is at most 10,000
workforce_unitsrows and 50,000workforce_unit_membershipsrows. - Measured locally at that scale, indexes included:
workforce_unitsis 12 MB for 10,000 rows (about 1.2 kB per row).workforce_unit_membershipsis 12 MB for 47,500 rows (about 260 bytes per row). - Headroom: each table stays under 10 GB up to roughly 800 tenants of the maximum size.
- Writes: nothing writes to these tables in this MR. The sync in https://gitlab.com/gitlab-org/gitlab/-/work_items/638004 will be the only writer.
Evidence for the acceptance criteria
Collected locally on GDK, on the branch rebased on master of 2026-11-20.
| Acceptance criterion | Status | Proof |
|---|---|---|
| Resolving any unit's subtree, and the people in it, takes under 100 ms for 10,000 units and 50,000 people, verified by a benchmark spec | Met | Slowest read is all 50,000 people under the root, 16.5 ms |
| The database rejects a second membership of the same type for one person | Met | Unique index index_workforce_unit_memberships_on_person_id_and_unit_type, and a foreign key that keeps the stored type equal to the unit's; specs in unit_membership_spec.rb |
| A unit subtree never contains units of another type | Met | subtree_of filters by type; specs in unit_spec.rb and person_spec.rb plant a unit of another type inside a subtree |
| A spec proves unit and membership reads never return another tenant's data | Met | "tenant isolation" specs in unit_spec.rb and person_spec.rb, with a path planted inside the other tenant's subtree and a membership pointing at another tenant's person |
| Deleting a tenant's hierarchy removes its units and memberships, with no statement removing more than 5,000 rows | Met | .delete_for_tenant specs in hierarchy_spec.rb, which check the order and the LIMIT of the real DELETE statements; batches of 1,000, memberships first |
| A spec proves the store accepts a second unit type | Met | Two types in one hierarchy in unit_spec.rb, including two units of different types with the same external id; a second-type membership in unit_membership_spec.rb |
| The development seed builds a valid supervisory tree with memberships | Met | 50,000 people seeded in 4.5 s; tree checks all 0; two runs give the same tree |
| Deleting a matched GitLab user keeps the person and their memberships | Met | Memberships have no user column: "attribute minimisation" spec in unit_membership_spec.rb. The person is unmatched, not deleted: migrate_records_to_ghost_user_service_spec.rb |
| Database review has approved the sharding keys | Pending | Needs database review and Tenant Scale |
Benchmark spec output
BENCHMARK=1 bundle exec rspec ee/spec/models/workforce/unit_subtree_benchmark_spec.rbdepth 0: 10000 units in 3.1 ms, 50000 people in 16.5 ms
depth 1: 2801 units in 1.5 ms, 14005 people in 9.3 ms
depth 2: 400 units in 0.8 ms, 2000 people in 2.3 ms
depth 3: 57 units in 0.6 ms, 285 people in 1.1 ms
depth 4: 8 units in 0.6 ms, 40 people in 1.0 ms
depth 5: 1 units in 0.5 ms, 5 people in 0.8 msSeed validation output
people: 50000, units: 10000, memberships: 47500
roots: 1
levels: {1 => 1, 2 => 4, 3 => 11, 4 => 74, 5 => 524, 6 => 3730, 7 => 5656}
path not ending in self: 0
unresolved parents: 0
unresolved managers: 0
people with >1 membership: 0MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.