Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dfb4908
Document configurable PostgreSQL storage design
mmpg-x86 Oct 14, 2026
953c8d2
Initialize pseudoterminals in display tests
mmpg-x86 Oct 15, 2026
ebb23b6
Parse opt-in PostgreSQL storage modes
mmpg-x86 Oct 15, 2026
8cc1137
Add PostgreSQL catalog migration foundation
mmpg-x86 Oct 15, 2026
282da5e
Support PostgreSQL for daemon storage
mmpg-x86 Oct 15, 2026
5ff2a61
Synchronize shared bug catalogs through PostgreSQL
mmpg-x86 Oct 15, 2026
0d7e948
Keep catalog merge bases until local changes are acknowledged
mmpg-x86 Oct 15, 2026
f0ab873
Keep imported discovery links scoped to their producer
mmpg-x86 Oct 15, 2026
1b9ef39
Honor the selected storage backend in database clients
mmpg-x86 Oct 15, 2026
dec5920
Provide OpenSSL dependencies for Linux Nix builds
mmpg-x86 Oct 15, 2026
9d63568
Run PostgreSQL integration coverage in CI
mmpg-x86 Oct 15, 2026
f74127a
Preserve ASCII subsystem scope matching on both backends
mmpg-x86 Oct 15, 2026
1256de8
Redact foreign duplicate history in scoped bug views
mmpg-x86 Oct 15, 2026
0a1ddeb
Bound pending catalog lookups to the requested bug views
mmpg-x86 Oct 15, 2026
67b4b3d
Preserve shared duplicate relationships during concurrent triage
mmpg-x86 Oct 15, 2026
b67bc94
Preserve analysis outcomes across shared catalog outages
mmpg-x86 Oct 15, 2026
44b0051
Recover ordinary PostgreSQL sessions after connection loss
mmpg-x86 Oct 15, 2026
ba8ef01
Keep the libSQL covering index for review statistics
mmpg-x86 Oct 15, 2026
3d64a2d
Exercise PostgreSQL adapter boundaries with deterministic fixtures
mmpg-x86 Oct 15, 2026
4447f1f
Describe PostgreSQL deployment and execution boundaries
mmpg-x86 Oct 15, 2026
56b079b
Keep shared publication scoped to the configured project
mmpg-x86 Oct 15, 2026
f5f686c
Scope PostgreSQL coordination locks to the configured schema
mmpg-x86 Oct 15, 2026
592d15d
Version shared mirror tracking and exclude local bookkeeping
mmpg-x86 Oct 15, 2026
44eb292
Reuse the shared catalog connection and TLS configuration
mmpg-x86 Oct 15, 2026
35b5c79
Discard PostgreSQL sessions after interrupted operations
mmpg-x86 Oct 15, 2026
672dbce
Classify transient catalog failures before refunding attempts
mmpg-x86 Oct 15, 2026
085e67a
Preserve legacy bug identifiers in project catalog namespaces
mmpg-x86 Oct 15, 2026
e875af3
Bound shared catalog synchronization with durable scheduling
mmpg-x86 Oct 15, 2026
490c9dc
Preserve original metadata when importing shared bug history
mmpg-x86 Oct 15, 2026
13fd37c
Match libSQL bug triage behavior on PostgreSQL
mmpg-x86 Oct 15, 2026
66e32d9
Verify the shared catalog readiness contract
mmpg-x86 Oct 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Scope PostgreSQL coordination locks to the configured schema
Use separate advisory lock classes with the server hash of the schema
name. Keep writers and canonical decisions coordinated within their
schema while allowing independent deployments to make progress.

Exercise both lock classes across separate schemas. Identify the
second mirror by its application name when observing its lock wait,
so a concurrent test cannot satisfy that assertion.

Signed-off-by: Maciej Grochowski <maciej.grochowski@protonmail.com>
  • Loading branch information
mmpg-x86 committed Oct 15, 2026
commit f5f686cabf5b8c66fd573bb43566ef2ab148bbdb
4 changes: 2 additions & 2 deletions designs/DESIGN_POSTGRESQL_STORAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ Canonical bug deduplication also takes a PostgreSQL session lock across
the candidate check and final write. Keep a single database
transaction for operations that currently depend on SQLite atomicity.

The first implementation serializes reads and writes with a database-wide
advisory lock. Explicit transactions use dedicated connections; ordinary
The first implementation serializes reads and writes with an advisory lock for
the configured schema. Explicit transactions use dedicated connections; ordinary
sessions reconnect before a later operation after connection loss, without
replaying failed writes. Transaction and deduplication sessions retain their
connection identity and fail when that session is lost. Query results are
Expand Down
6 changes: 3 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ enabled = true
Set the PostgreSQL URL through deployment secrets in either configuration.
An existing libSQL database is not automatically moved when selecting full
PostgreSQL storage. Full mode creates an independent database schema.
The initial backend serializes database operations with a database-wide
advisory lock and uses dedicated sessions for explicit transactions. Select it
with that execution model in mind when sizing a deployment.
The initial backend serializes database operations with an advisory lock for
the configured schema and uses dedicated sessions for explicit transactions.
Select it with that execution model in mind when sizing a deployment.

In shared catalog mode, each local database has a stable instance identity
and a local bug mirror. Changes enter a durable outbox in the same transaction
Expand Down
33 changes: 20 additions & 13 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::Mutex;
use tokio_postgres::types::{FromSql, IsNull, ToSql, Type};

const WRITE_LOCK: i64 = 0x53415348494b4f;
const WRITE_LOCK: i32 = 0x53415348;

#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down Expand Up @@ -115,7 +115,10 @@ impl Connection {
.client
.lock()
.await
.query_one("SELECT pg_advisory_lock($1)", &[&(WRITE_LOCK + 1)])
.query_one(
"SELECT pg_catalog.pg_advisory_lock(pg_catalog.hashtext($1),$2)",
&[&connection.config.schema, &(WRITE_LOCK + 1)],
)
.await?;
}
Ok(Some(conn))
Expand Down Expand Up @@ -150,8 +153,7 @@ impl Connection {
let schema = format!("\"{}\"", config.schema);
if !transaction {
let tx = client.transaction().await?;
tx.query_one("SELECT pg_advisory_xact_lock($1)", &[&WRITE_LOCK])
.await?;
pg_write_lock(&tx, &config.schema).await?;
let exists: bool = tx
.query_one(
"SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname=$1)",
Expand All @@ -169,9 +171,7 @@ impl Connection {
.await?;
if transaction {
client.batch_execute("BEGIN").await?;
client
.query_one("SELECT pg_advisory_xact_lock($1)", &[&WRITE_LOCK])
.await?;
pg_write_lock(&client, &config.schema).await?;
}
Ok(Self(Backend::Postgres(Arc::new(PgConnection {
client: Mutex::new(client),
Expand All @@ -192,8 +192,7 @@ impl Connection {
pg_query(&*client, &sql, params).await?
} else {
let tx = client.transaction().await?;
tx.query_one("SELECT pg_advisory_xact_lock($1)", &[&WRITE_LOCK])
.await?;
pg_write_lock(&tx, &conn.config.schema).await?;
let values = pg_query(&tx, &sql, params).await?;
tx.commit().await?;
values
Expand Down Expand Up @@ -226,8 +225,7 @@ impl Connection {
pg_execute(&*client, &sql, params).await
} else {
let tx = client.transaction().await?;
tx.query_one("SELECT pg_advisory_xact_lock($1)", &[&WRITE_LOCK])
.await?;
pg_write_lock(&tx, &conn.config.schema).await?;
let count = pg_execute(&tx, &sql, params).await?;
tx.commit().await?;
Ok(count)
Expand All @@ -247,8 +245,7 @@ impl Connection {
client.batch_execute(sql).await?;
} else {
let tx = client.transaction().await?;
tx.query_one("SELECT pg_advisory_xact_lock($1)", &[&WRITE_LOCK])
.await?;
pg_write_lock(&tx, &conn.config.schema).await?;
tx.batch_execute(sql).await?;
tx.commit().await?;
}
Expand Down Expand Up @@ -486,6 +483,16 @@ async fn pg_query(
.collect()
}

async fn pg_write_lock(client: &impl tokio_postgres::GenericClient, schema: &str) -> Result<()> {
client
.query_one(
"SELECT pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtext($1),$2)",
&[&schema, &WRITE_LOCK],
)
.await?;
Ok(())
}

async fn pg_execute(
client: &impl tokio_postgres::GenericClient,
sql: &str,
Expand Down
45 changes: 41 additions & 4 deletions tests/integration/postgres_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,46 @@ async fn postgres_daemon_storage_roundtrip() -> Result<()> {
let db = Database::configured(&configured).await?;
let result = std::panic::AssertUnwindSafe(async {
exercise_daemon(&db).await?;
exercise_connection_recovery(&db, &configured.storage).await
exercise_connection_recovery(&db, &configured.storage).await?;
exercise_schema_isolation(&db, &configured.storage).await
})
.catch_unwind()
.await;
db.execute_batch(&format!("DROP SCHEMA \"{schema}\" CASCADE"))
.await?;
db.execute_batch(&format!(
"DROP SCHEMA IF EXISTS \"{schema}_independent\" CASCADE;
DROP SCHEMA \"{schema}\" CASCADE"
))
.await?;
match result {
Ok(result) => result,
Err(panic) => std::panic::resume_unwind(panic),
}
}

async fn exercise_schema_isolation(db: &Database, settings: &StorageSettings) -> Result<()> {
let mut settings = settings.clone();
settings.postgres_schema.push_str("_independent");
let independent = Database::postgres(&settings).await?;
let tx = db.begin_transaction().await?;
tokio::time::timeout(
std::time::Duration::from_secs(10),
independent.conn.query("SELECT 1", ()),
)
.await
.context("a transaction blocked an independent schema")??;
tx.rollback().await?;
let guard = db.lock_bug_dedup().await?;
let independent_guard = tokio::time::timeout(
std::time::Duration::from_secs(10),
independent.lock_bug_dedup(),
)
.await
.context("deduplication blocked an independent schema")??;
drop(independent_guard);
drop(guard);
Ok(())
}

async fn postgres_control(settings: &StorageSettings) -> Result<tokio_postgres::Client> {
let mut config: tokio_postgres::Config = settings.postgres_url.as_ref().unwrap().parse()?;
config.ssl_mode(tokio_postgres::config::SslMode::Require);
Expand Down Expand Up @@ -320,6 +348,12 @@ async fn postgres_shared_catalog_between_instances() -> Result<()> {
.ensure_project_stamp(sashiko::project::ProjectId::Sashiko)
.await?;
let mut second_settings = settings.clone();
let mut url = reqwest::Url::parse(second_settings.storage.postgres_url.as_deref().unwrap())?;
url.query_pairs_mut().append_pair(
"application_name",
&format!("{}_mirror", settings.storage.postgres_schema),
);
second_settings.storage.postgres_url = Some(url.into());
second_settings.database.url = directory
.path()
.join("second.db")
Expand Down Expand Up @@ -482,7 +516,8 @@ async fn assert_shared_lock_exclusion(
let guard = first.lock_bug_dedup().await?.context("held dedup guard")?;
let pid = backend_pid(&guard).await?;
let control = postgres_control(settings).await?;
let params: [&(dyn tokio_postgres::types::ToSql + Sync); 1] = [&pid];
let second_name = format!("{}_mirror", settings.postgres_schema);
let params: [&(dyn tokio_postgres::types::ToSql + Sync); 2] = [&pid, &second_name];
let waiter = second.lock_bug_dedup();
tokio::pin!(waiter);
tokio::time::timeout(std::time::Duration::from_secs(10), async {
Expand All @@ -497,7 +532,9 @@ async fn assert_shared_lock_exclusion(
SELECT 1 FROM pg_catalog.pg_locks held
JOIN pg_catalog.pg_locks waiting
USING (locktype,database,classid,objid,objsubid)
JOIN pg_catalog.pg_stat_activity client ON client.pid=waiting.pid
WHERE held.pid=$1 AND held.locktype='advisory'
AND client.application_name=$2
AND held.granted AND NOT waiting.granted)", &params,
) => {
if row?.get::<_, bool>(0) {
Expand Down