]> git.ktnx.net Git - mpd-feeder.git/blob - sql/pgsql/init.sql
rework DB update using COPY and a PostgreSQL function for maximum speed
[mpd-feeder.git] / sql / pgsql / init.sql
1 begin transaction;
2
3 create table songs(
4     path text not null primary key,
5     artist text,
6     album text,
7     last_queued timestamp with time zone,
8     generation bigint not null);
9
10 create index songs_artist_idx on songs(artist);
11 create index songs_album_idx on songs(album);
12
13 create table albums(
14     artist text not null,
15     album text not null,
16     last_queued timestamp with time zone,
17     generation bigint not null,
18     primary key(album,artist));
19
20 create table artists(
21     artist text not null primary key,
22     last_queued timestamp with time zone,
23     generation bigint not null);
24
25 create table unwanted_albums(
26     artist text not null,
27     album text not null,
28     generation bigint not null,
29     primary key(album,artist));
30
31 create table unwanted_artists(
32     artist text not null primary key,
33     generation bigint not null
34 );
35
36 create table options(
37     generation bigint not null
38 );
39
40 insert into options(generation) values(0);
41
42 CREATE FUNCTION update_song_data(
43     new_generation bigint
44   , OUT total_songs bigint
45   , OUT total_artists bigint
46   , OUT total_albums bigint
47   , OUT new_songs bigint
48   , OUT new_artists bigint
49   , OUT new_albums bigint)
50 AS
51 $$
52 DECLARE
53     song_data RECORD;
54 BEGIN
55     total_songs = 0;
56     new_songs = 0;
57     new_artists = 0;
58     new_albums = 0;
59
60     FOR song_data IN
61         SELECT path, artist, album
62         FROM tmp_songs
63     LOOP
64         total_songs = total_songs + 1;
65
66         UPDATE songs
67         SET generation = new_generation
68         WHERE path = song_data.path;
69
70         IF ( NOT FOUND ) THEN
71             new_songs = new_songs + 1;
72             INSERT INTO songs(path, artist, album, generation)
73             values(song_data.path, song_data.artist,
74                 song_data.album, new_generation);
75         END IF;
76         -----------------------------
77         UPDATE artists
78         SET generation = new_generation
79         WHERE artist = song_data.artist;
80
81         IF ( NOT FOUND ) THEN
82             new_artists = new_artists + 1;
83             INSERT INTO artists(artist, generation)
84             VALUES(song_data.artist, new_generation);
85         END IF;
86         -----------------------------
87         UPDATE albums
88         SET generation = new_generation
89         WHERE album = song_data.album
90           AND artist = song_data.artist;
91
92         IF ( NOT FOUND ) THEN
93             new_albums = new_albums + 1;
94             INSERT INTO albums(album, artist, generation)
95             VALUES(song_data.album, song_data.artist, new_generation);
96         END IF;
97     END LOOP;
98
99     SELECT COUNT(*) INTO total_artists FROM artists WHERE generation = new_generation;
100     SELECT COUNT(*) INTO total_albums  FROM albums  WHERE generation = new_generation;
101
102     RETURN;
103 END;
104 $$ LANGUAGE plpgsql;
105
106 commit;