Index the foreign keys of import receipts and targets

Deleting an import cascades to its receipts once per entry, and deleting
any playlist sets the import targets that point at it to NULL. Without an
index on the referencing columns each of these scanned the whole table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-26 19:27:34 +02:00
1 parent b6c2ec7b39
commit da28e775b3
2 files changed
+32

No files matched your search

+8
View File
@@ -42,6 +42,9 @@ CREATE TABLE IF NOT EXISTS furumusic__library_import_playlist (
);
CREATE INDEX IF NOT EXISTS idx_library_import_playlist_key
ON furumusic__library_import_playlist(source_key);
-- Deleting any playlist sets its import targets to NULL.
CREATE INDEX IF NOT EXISTS idx_library_import_playlist_target
ON furumusic__library_import_playlist(target_playlist_id) WHERE target_playlist_id IS NOT NULL;
CREATE TABLE IF NOT EXISTS furumusic__library_import_item (
id BIGSERIAL PRIMARY KEY,
@@ -85,6 +88,11 @@ CREATE TABLE IF NOT EXISTS furumusic__library_import_publication (
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (target_playlist_id, content_id)
);
-- Deleting an import cascades through these keys once per row.
CREATE INDEX IF NOT EXISTS idx_library_import_publication_item
ON furumusic__library_import_publication(item_id);
CREATE INDEX IF NOT EXISTS idx_library_import_publication_playlist
ON furumusic__library_import_publication(import_playlist_id);
CREATE TABLE IF NOT EXISTS furumusic__library_import_attempt (
id BIGSERIAL PRIMARY KEY,
+24
View File
@@ -417,6 +417,30 @@ async fn library_import_storage_is_isolated_per_user() {
db.drop().await;
}
/// Cascades and `SET NULL` run once per deleted row; without an index on the
/// referencing column each run scans the whole table.
#[tokio::test]
#[ignore = "requires FURU_LIBRARY_IMPORT_TEST_DATABASE_URL pointing to a disposable PostgreSQL database"]
async fn library_import_foreign_keys_have_leading_indexes() {
let _serial = SERIAL.lock().await;
let db = Fixture::new().await;
let unindexed: Vec<String> = sqlx::query_scalar(
"SELECT c.conrelid::regclass::text || '.' || a.attname
FROM pg_constraint c
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1]
WHERE c.contype = 'f' AND cardinality(c.conkey) = 1
AND c.conrelid::regclass::text LIKE 'furumusic__library_import%'
AND NOT EXISTS (SELECT 1 FROM pg_index i
WHERE i.indrelid = c.conrelid AND i.indkey[0] = c.conkey[1])
ORDER BY 1",
)
.fetch_all(&db.pool)
.await
.unwrap();
assert!(unindexed.is_empty(), "{unindexed:?}");
db.drop().await;
}
#[tokio::test]
#[ignore = "requires FURU_LIBRARY_IMPORT_TEST_DATABASE_URL pointing to a disposable PostgreSQL database"]
async fn library_import_rejects_repeated_source_playlists() {