Skip to content

Commit 25bb34f

Browse files
gautamg795Convex, Inc.
authored andcommitted
update the indexes schema with new primary key and index (#38876)
GitOrigin-RevId: ad3c96b2b07534b284da58865ccc2f1dbe65970c
1 parent 6ab73d0 commit 25bb34f

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

crates/postgres/src/lib.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,23 +1634,20 @@ BEGIN
16341634
table_id BYTEA NULL,
16351635
/* document_id should be populated iff deleted is false. */
16361636
document_id BYTEA NULL,
1637-
PRIMARY KEY (index_id, key_prefix, key_sha256, ts)
1637+
PRIMARY KEY (index_id, key_sha256, ts)
16381638
);
16391639
END IF;
16401640
END $$;
16411641
"#,
16421642
r#"
16431643
DO $$
16441644
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
1649-
CREATE UNIQUE INDEX IF NOT EXISTS indexes_by_index_id_key_prefix_key_sha256_ts ON @db_name.indexes (
1645+
/* We only want this index created for new instances; existing ones already have `indexes_by_index_id_key_prefix_key_sha256_ts` */
1646+
IF to_regclass('@db_name.indexes_by_index_id_key_prefix_key_sha256_ts') IS NULL AND to_regclass('@db_name.indexes_by_index_id_key_prefix_key_sha256') IS NULL THEN
1647+
CREATE INDEX IF NOT EXISTS indexes_by_index_id_key_prefix_key_sha256 ON @db_name.indexes (
16501648
index_id,
16511649
key_prefix,
1652-
key_sha256,
1653-
ts DESC
1650+
key_sha256
16541651
);
16551652
END IF;
16561653
END $$;
@@ -1816,7 +1813,7 @@ const INSERT_INDEX: &str = r#"INSERT INTO @db_name.indexes
18161813
const INSERT_OVERWRITE_INDEX: &str = r#"INSERT INTO @db_name.indexes
18171814
(index_id, ts, key_prefix, key_suffix, key_sha256, deleted, table_id, document_id)
18181815
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
1819-
ON CONFLICT (index_id, ts, key_prefix, key_sha256) DO UPDATE
1816+
ON CONFLICT ON CONSTRAINT indexes_pkey DO UPDATE
18201817
SET deleted = excluded.deleted, table_id = excluded.table_id, document_id = excluded.document_id
18211818
"#;
18221819

@@ -1862,7 +1859,7 @@ const INSERT_OVERWRITE_INDEX_CHUNK: &str = r#"INSERT INTO @db_name.indexes
18621859
($41, $42, $43, $44, $45, $46, $47, $48),
18631860
($49, $50, $51, $52, $53, $54, $55, $56),
18641861
($57, $58, $59, $60, $61, $62, $63, $64)
1865-
ON CONFLICT (index_id, ts, key_prefix, key_sha256) DO UPDATE
1862+
ON CONFLICT ON CONSTRAINT indexes_pkey DO UPDATE
18661863
SET deleted = excluded.deleted, table_id = excluded.table_id, document_id = excluded.document_id
18671864
"#;
18681865

0 commit comments

Comments
 (0)