Skip to content

Implement Regional Sequence Generators for User Table#241

Open
Suhasrv2403 wants to merge 5 commits into
saayam-for-all:testfrom
Suhasrv2403:suhas_issue_238
Open

Implement Regional Sequence Generators for User Table#241
Suhasrv2403 wants to merge 5 commits into
saayam-for-all:testfrom
Suhasrv2403:suhas_issue_238

Conversation

@Suhasrv2403

Copy link
Copy Markdown

Summary

This PR implements the multi-region sequence generator changes required to support GDPR-compliant user data residency across Virginia, Ireland, and Stockholm. It introduces a new user_id format, an is_eu flag, and disaster recovery sequence ranges for all three regions.


Problem

The existing generate_sid() function had the following issues:

  • Used INT instead of BIGINT, limiting scale
  • Old 3-segment SID-00-XXX-XXX-XXX format had no region identity
  • No is_eu awareness — EU and non-EU users were treated identically
  • No disaster recovery sequence ranges — ID collisions would occur during regional failover
  • Sequence was unqualified and unbound (NO MINVALUE, NO MAXVALUE)

Approach

New ID format:
SID-001-XXX-XXX-XXX-XXX → non-EU users
SID-002-XXX-XXX-XXX-XXX → EU users

Sequence range partitioning:

Region Role Prefix Range Direction
Virginia Normal non-EU SID-001 1 → 1T Ascending
Ireland DR Virginia SID-001 1T → 1 Descending
Ireland Normal EU SID-002 2T → 4T Ascending
Stockholm DR Ireland SID-002 4T → 2T Descending
Virginia DR EU both SID-002 5T → 4T Descending

Why directional sequences for DR?

  • Normal operation counts ascending from the bottom of its range
  • DR operation counts descending from the top of its range
  • The two can never collide within a realistic DR window
  • On recovery and re-sync, origin of any ID is traceable by whether it is high or low within its range

is_eu flag:

  • Added to all three regions' users tables
  • Virginia defaults FALSE, Ireland and Stockholm default TRUE
  • Virginia and Stockholm are each dedicated to one role — no branching needed
  • Ireland branches on is_eu to pick between its two sequences

Files Changed

File Purpose
migrate_virginia_users.sql One-time migration for existing Virginia rows
migrate_ireland_users.sql One-time migration for existing Ireland rows
migrate_stockholm_users.sql One-time migration for existing Stockholm rows
ddl_virginia_users.sql Clean schema definition for Virginia
ddl_ireland_users.sql Clean schema definition for Ireland
ddl_stockholm_users.sql Clean schema definition for Stockholm
test_virginia_generate_sid.sql pgTAP tests for Virginia
test_ireland_generate_sid.sql pgTAP tests for Ireland
test_stockholm_generate_sid.sql pgTAP tests for Stockholm

Migration Order

  1. Run migration scripts first — these create sequences and backfill existing rows
  2. Run DDL scripts second — these define the clean schema for new deployments
  3. Run tests third to verify all regions generate correct IDs

Closes #238

@Suhasrv2403 Suhasrv2403 self-assigned this Jun 25, 2026
…ct approach, SID-EU prefix, Stockholm DDL added
@Suhasrv2403
Suhasrv2403 changed the base branch from main to test July 2, 2026 17:58
@Suhasrv2403

Copy link
Copy Markdown
Author

Updates Based on Review Comments

Changes Made

1. Ireland EU prefix corrected
Fixed Ireland EU users to use SID-EU- prefix instead of SID-002-. This was missed in the original submission.

2. Updated sequence ranges
Moved from trillion-scale ranges to billion-scale ranges as discussed:

Region Role Prefix Range
Virginia Normal non-EU SID-001 1 → 20B
Ireland DR Virginia SID-001 20B → 40B
Stockholm DR Virginia SID-001 40B → 60B
Ireland Normal EU SID-EU 1 → 20B
Stockholm DR Ireland SID-EU 20B → 40B

3. Math extract approach adopted
Region identity is derived from the sequence value itself — dividing by 20B naturally produces the region segment (000, 001, 002). Same function works identically across all regions with no hardcoding needed.

4. Stockholm DDL added
Stockholm was missing from the EU DDL file. Added at the end of ddl_eu_users.sql using Ireland as reference, including table definition, sequences, generate_sid() function and trigger.

5. Migration scripts removed
Removed as discussed — scope is DDL and function changes only.


Files Changed

File Change
ddl_virginia_users.sql Updated sequence range, function, is_eu column
ddl_eu_users.sql Updated Ireland sequence, function, SID-EU prefix, Stockholm added at end
test_virginia_generate_sid.sql Unchanged — still valid
test_ireland_generate_sid.sql Unchanged — still valid
test_stockholm_generate_sid.sql Unchanged — still valid

Closes #238

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Regional Sequence Generators for User Table

2 participants