Skip to content

Commit f7bbfa1

Browse files
gautamg795Convex, Inc.
authored andcommitted
run INIT_SQL in a txn per statement (#38756)
GitOrigin-RevId: f6ba7d6936f038e7e904c046069100d627e00764
1 parent 3e57da5 commit f7bbfa1

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

crates/postgres/src/lib.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ impl PostgresPersistence {
241241
}
242242
client
243243
.with_retry(async |client| {
244-
client.batch_execute(INIT_SQL).await?;
245-
client.batch_execute(INIT_LEASE_SQL).await?;
244+
for stmt in INIT_SQL {
245+
client.batch_execute(stmt).await?;
246+
}
246247
Ok(())
247248
})
248249
.await?;
@@ -1552,7 +1553,8 @@ const CREATE_SCHEMA_SQL: &str = r"CREATE SCHEMA IF NOT EXISTS @db_name;";
15521553
// This runs (currently) every time a PostgresPersistence is created, so it
15531554
// needs to not only be idempotent but not to affect any already-resident data.
15541555
// IF NOT EXISTS and ON CONFLICT are helpful.
1555-
const INIT_SQL: &str = r#"
1556+
const INIT_SQL: &[&str] = &[
1557+
r#"
15561558
CREATE TABLE IF NOT EXISTS @db_name.documents (
15571559
id BYTEA NOT NULL,
15581560
ts BIGINT NOT NULL,
@@ -1566,18 +1568,20 @@ const INIT_SQL: &str = r#"
15661568
15671569
PRIMARY KEY (ts, table_id, id)
15681570
);
1571+
"#,
1572+
r#"
15691573
CREATE INDEX IF NOT EXISTS documents_by_table_and_id ON @db_name.documents (
15701574
table_id, id, ts
15711575
);
15721576
CREATE INDEX IF NOT EXISTS documents_by_table_ts_and_id ON @db_name.documents (
15731577
table_id, ts, id
15741578
);
1575-
1579+
"#,
1580+
r#"
15761581
CREATE TABLE IF NOT EXISTS @db_name.indexes (
15771582
/* ids should be serialized as bytes but we keep it compatible with documents */
15781583
index_id BYTEA NOT NULL,
15791584
ts BIGINT NOT NULL,
1580-
15811585
/*
15821586
Postgres maximum primary key length is 2730 bytes, which
15831587
is why we split up the key. The first 2500 bytes are stored in key_prefix,
@@ -1600,6 +1604,8 @@ const INIT_SQL: &str = r#"
16001604
document_id BYTEA NULL,
16011605
PRIMARY KEY (index_id, key_prefix, key_sha256, ts)
16021606
);
1607+
"#,
1608+
r#"
16031609
/* This index with `ts DESC` enables our "loose index scan" queries
16041610
* (i.e. `DISTINCT ON`) to run in both directions, complementing the
16051611
* primary key's ts ASC ordering */
@@ -1609,27 +1615,33 @@ const INIT_SQL: &str = r#"
16091615
key_sha256,
16101616
ts DESC
16111617
);
1612-
1618+
"#,
1619+
r#"
16131620
CREATE TABLE IF NOT EXISTS @db_name.leases (
16141621
id BIGINT NOT NULL,
16151622
ts BIGINT NOT NULL,
16161623
16171624
PRIMARY KEY (id)
16181625
);
1626+
"#,
1627+
r#"
16191628
CREATE TABLE IF NOT EXISTS @db_name.read_only (
16201629
id BIGINT NOT NULL,
16211630
16221631
PRIMARY KEY (id)
16231632
);
1633+
"#,
1634+
r#"
16241635
CREATE TABLE IF NOT EXISTS @db_name.persistence_globals (
16251636
key TEXT NOT NULL,
16261637
json_value BYTEA NOT NULL,
16271638
PRIMARY KEY (key)
1628-
);"#;
1629-
// We also run this on every initialization, but separate it from INIT_SQL to
1630-
// keep DDL and DML separate.
1631-
const INIT_LEASE_SQL: &str =
1632-
"INSERT INTO @db_name.leases (id, ts) VALUES (1, 0) ON CONFLICT DO NOTHING;";
1639+
);
1640+
"#,
1641+
r#"
1642+
INSERT INTO @db_name.leases (id, ts) VALUES (1, 0) ON CONFLICT DO NOTHING;
1643+
"#,
1644+
];
16331645
const TABLES: &[&str] = &[
16341646
"documents",
16351647
"indexes",

0 commit comments

Comments
 (0)