Skip to content

Commit 29c4888

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
authored
Route lag-tolerant reads to an optional Postgres read replica (#2084)
Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <[email protected]>
1 parent c81806d commit 29c4888

13 files changed

Lines changed: 2030 additions & 26 deletions

File tree

‎.env.example‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# Database (Postgres 17)
2020
# -----------------------------------------------------------------------------
2121
DATABASE_URL=postgres://buzz:buzz_dev@localhost:5432/buzz
22+
# Optional read-replica URL; unset/blank keeps all reads on the writer.
23+
# READ_DATABASE_URL=postgres://buzz:buzz_dev@localhost:5433/buzz
2224
PGHOST=localhost
2325
PGPORT=5432
2426
PGUSER=buzz

‎crates/buzz-db/src/lib.rs‎

Lines changed: 1077 additions & 17 deletions
Large diffs are not rendered by default.

‎crates/buzz-db/src/migration.rs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("../../migrations");
1414
pub async fn run_migrations(pool: &PgPool) -> Result<()> {
1515
reject_legacy_nip_rs_cardinality_ambiguity(pool).await?;
1616
MIGRATOR.run(pool).await?;
17+
// The replica-fence proof (see `replica_fence`) requires the commit-time
18+
// `created_at` floor trigger from migration 0021 — correctly shaped — on
19+
// the `events` parent and every partition. `CREATE TABLE .. PARTITION OF`
20+
// clones parent triggers, but a partition attached with `ATTACH
21+
// PARTITION` or created by an older code path would silently escape the
22+
// guard, so migration fails closed if any is missing. (The fence probe
23+
// re-runs this same check at startup on non-migrating relays.)
24+
crate::replica_fence::verify_floor_guard_catalog(pool).await?;
1725
Ok(())
1826
}
1927

@@ -549,7 +557,7 @@ mod tests {
549557
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
550558
migrations.sort_by_key(|migration| migration.version);
551559

552-
assert_eq!(migrations.len(), 20);
560+
assert_eq!(migrations.len(), 21);
553561
assert_eq!(migrations[0].version, 1);
554562
assert_eq!(&*migrations[0].description, "initial schema");
555563
assert!(migrations[0]
@@ -820,6 +828,13 @@ mod tests {
820828
.sql
821829
.as_str()
822830
.contains("CREATE TABLE join_policy_acceptances"));
831+
832+
// Replica-fence commit-time floor guard on channel-bearing events.
833+
assert_eq!(migrations[20].version, 21);
834+
assert!(migrations[20]
835+
.sql
836+
.as_str()
837+
.contains("events_created_at_floor_guard"));
823838
assert!(!migrations[0]
824839
.sql
825840
.as_str()
@@ -1066,7 +1081,7 @@ mod tests {
10661081
run_migrations(&pool)
10671082
.await
10681083
.expect("retry succeeds after operator repair");
1069-
assert_eq!(applied_versions(&pool).await.last().copied(), Some(20));
1084+
assert_eq!(applied_versions(&pool).await.last().copied(), Some(21));
10701085
}
10711086

10721087
#[tokio::test]

0 commit comments

Comments
 (0)