Archive for October 6th, 2009
Answering questions asked on the site.
I am building a music application and need to create a playlist of arbitrary length from the tracks stored in the database.
This playlist should be shuffled and a track can repeat only after at least 10 other tracks had been played.
Is it possible to do this with a single SQL query or I need to create a cursor?
This is in PostgreSQL 8.4
PostgreSQL 8.4 is a wise choice, since it introduces some new features that ease this task.
To do this we just need to keep a running set that would hold the previous 10 tracks so that we could filter on them.
PostgreSQL 8.4 supports recursive CTE‘s that allow iterating the resultsets, and arrays that can be easily used to keep the set of 10 latest tracks.
Here’s what we should do to build the playlist:
- We make a recursive CTE that would generate as many records as we need and just use LIMIT to limit the number
- The base part of the CTE is just a random record (fetched with
ORDER BY RANDOM() LIMIT 1)
- The base part also defines the queue. This is an array which holds 10 latest records selected. It is initialized in the base part with the
idof the random track just selected
- The recursive part of the CTE joins the previous record with the table, making sure that no record from the latest 10 will be selected on this step. To do this, we just use the array operator
- The recursive part adds newly selected record to the queue. The queue should be no more than 10 records long, that’s why we apply array slicing operator to it (
Let’s create a sample table:
Read the rest of this entry »