Skip to content

Commit 6ab73d0

Browse files
gautamg795Convex, Inc.
authored andcommitted
conditionally create table in postgres (#38785)
GitOrigin-RevId: 3b33c3a578ad14292395876dbbe57c588f0dff22
1 parent 04a6726 commit 6ab73d0

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

crates/postgres/src/lib.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,8 +1567,14 @@ const CREATE_SCHEMA_SQL: &str = r"CREATE SCHEMA IF NOT EXISTS @db_name;";
15671567
// This runs (currently) every time a PostgresPersistence is created, so it
15681568
// needs to not only be idempotent but not to affect any already-resident data.
15691569
// IF NOT EXISTS and ON CONFLICT are helpful.
1570+
// Despite the idempotence of IF NOT EXISTS, we still use a conditional check to
1571+
// see if we can avoid running that statement, as it acquires an `ACCESS
1572+
// EXCLUSIVE` lock across the database.
15701573
const INIT_SQL: &[&str] = &[
15711574
r#"
1575+
DO $$
1576+
BEGIN
1577+
IF to_regclass('@db_name.documents') IS NULL THEN
15721578
CREATE TABLE IF NOT EXISTS @db_name.documents (
15731579
id BYTEA NOT NULL,
15741580
ts BIGINT NOT NULL,
@@ -1582,16 +1588,28 @@ const INIT_SQL: &[&str] = &[
15821588
15831589
PRIMARY KEY (ts, table_id, id)
15841590
);
1591+
END IF;
1592+
END $$;
15851593
"#,
15861594
r#"
1595+
DO $$
1596+
BEGIN
1597+
IF to_regclass('@db_name.documents_by_table_and_id') IS NULL THEN
15871598
CREATE INDEX IF NOT EXISTS documents_by_table_and_id ON @db_name.documents (
15881599
table_id, id, ts
15891600
);
1601+
END IF;
1602+
IF to_regclass('@db_name.documents_by_table_ts_and_id') IS NULL THEN
15901603
CREATE INDEX IF NOT EXISTS documents_by_table_ts_and_id ON @db_name.documents (
15911604
table_id, ts, id
15921605
);
1606+
END IF;
1607+
END $$;
15931608
"#,
15941609
r#"
1610+
DO $$
1611+
BEGIN
1612+
IF to_regclass('@db_name.indexes') IS NULL THEN
15951613
CREATE TABLE IF NOT EXISTS @db_name.indexes (
15961614
/* ids should be serialized as bytes but we keep it compatible with documents */
15971615
index_id BYTEA NOT NULL,
@@ -1618,40 +1636,62 @@ const INIT_SQL: &[&str] = &[
16181636
document_id BYTEA NULL,
16191637
PRIMARY KEY (index_id, key_prefix, key_sha256, ts)
16201638
);
1639+
END IF;
1640+
END $$;
16211641
"#,
16221642
r#"
1623-
/* This index with `ts DESC` enables our "loose index scan" queries
1624-
* (i.e. `DISTINCT ON`) to run in both directions, complementing the
1625-
* primary key's ts ASC ordering */
1643+
DO $$
1644+
BEGIN
1645+
/* This index with `ts DESC` enables our "loose index scan" queries
1646+
* (i.e. `DISTINCT ON`) to run in both directions, complementing the
1647+
* primary key's ts ASC ordering */
1648+
IF to_regclass('@db_name.indexes_by_index_id_key_prefix_key_sha256_ts') IS NULL THEN
16261649
CREATE UNIQUE INDEX IF NOT EXISTS indexes_by_index_id_key_prefix_key_sha256_ts ON @db_name.indexes (
16271650
index_id,
16281651
key_prefix,
16291652
key_sha256,
16301653
ts DESC
16311654
);
1655+
END IF;
1656+
END $$;
16321657
"#,
16331658
r#"
1659+
DO $$
1660+
BEGIN
1661+
IF to_regclass('@db_name.leases') IS NULL THEN
16341662
CREATE TABLE IF NOT EXISTS @db_name.leases (
16351663
id BIGINT NOT NULL,
16361664
ts BIGINT NOT NULL,
16371665
16381666
PRIMARY KEY (id)
16391667
);
1668+
END IF;
1669+
END $$;
16401670
"#,
16411671
r#"
1672+
DO $$
1673+
BEGIN
1674+
IF to_regclass('@db_name.read_only') IS NULL THEN
16421675
CREATE TABLE IF NOT EXISTS @db_name.read_only (
16431676
id BIGINT NOT NULL,
16441677
16451678
PRIMARY KEY (id)
16461679
);
1680+
END IF;
1681+
END $$;
16471682
"#,
16481683
r#"
1684+
DO $$
1685+
BEGIN
1686+
IF to_regclass('@db_name.persistence_globals') IS NULL THEN
16491687
CREATE TABLE IF NOT EXISTS @db_name.persistence_globals (
16501688
key TEXT NOT NULL,
16511689
json_value BYTEA NOT NULL,
16521690
PRIMARY KEY (key)
16531691
);
1654-
"#,
1692+
END IF;
1693+
END $$;
1694+
"#,
16551695
r#"
16561696
INSERT INTO @db_name.leases (id, ts) VALUES (1, 0) ON CONFLICT DO NOTHING;
16571697
"#,

0 commit comments

Comments
 (0)