Analytical Queries in Postgres at ROSALIND

2024-12-31


During my time at ROSALIND, I managed our 4TB Postgres database for current and historical COVID-19 sequence analysis.

The Database

The Postgres instance has 24 vCPUs, 128GB of RAM, and 4TB of solid-state storage. The database tables of interest include samples, sequences, and mutations:

Samples

Sequences

Mutations

The Queries

The vast majority of queries were analytical queries, which looked something like this:

SELECT
    start,
    indel,
    count(*)
FROM
    mutations
    JOIN sequence_to_mutation ON mutations.id = sequence_to_mutation.mutation_id
    JOIN sample ON sample.sequence_id = sequence_to_mutation.sequence_id
WHERE
    sample.continent = 'North America'
    AND sample.country = 'USA'
    AND sample.state = 'California'
    AND sample.collected >= NOW() - '30 days'::interval
GROUP BY
    start,
    indel;

This query pulled all unique mutations for any samples from California collected within the last 30 days. This data was then used to populate the Genome Browser.

This query would typically return results within 5 seconds, which was acceptable for our use case. The problem was, users would regularly increase the timeframe or remove it altogether. As the number of samples matching our filters increased, so did the number of unique mutations. By August 2023, we had ~20 million samples in our database, with ~50 mutations per sample. Running this query without any filters took >2 minutes to fully complete.

We had a few solutions in mind:

  1. Cache the results once per day. Because we only ingested new data once a day, we could rebuild the cache after that day’s data ingestion. This worked great for existing queries, but didn’t solve the problem for new queries
  2. Denormalize data. We could store the mutations on the samples table in an ARRAY column, saving us the JOINs between tables
  3. Use a different database

We explored all three options. Option 1 was trivial to implement, so we did that first. Option 2 would require a LOT of extra work to denormalize data for different queries, so we pushed that out. That left us with Option 3.

Testing Different Databases

We had a few different databases we wanted to try:

  1. DuckDB, a columnar based database that’s conceptually similar to SQLite
  2. AlloyDB, a Postgres-compatible database from GCP that’s built with analytical queries in mind
  3. BigQuery, a GCP service used for terabyte/petabyte scale data analytics

Unfortunately, the exact tests and results have been lost to time. Maybe one day I’ll replicate this testing, but for now, here’s an overview.

DuckDB

DuckDB was very easy to get started with. We first extracted all of the relevant data into a handful of CSV files. We then transformed those CSV files into Parquet files. Parquet files are similar to CSVs, but encode data in a column-based format instead of a row-based format. We then installed the Python client and tested a handful of queries.

We saw great results with DuckDB. We were pleasantly pleased with how fast the analytical queries were. The downside was that managing a DuckDB service looked moderately challenging. We’d need to export data to Parquet files, make those files accessible to servers running DuckDB, and put an API in front of our DuckDB system. Because of that, we decided to step away from self-hosted systems and look only at managed systems.

AlloyDB

AlloyDB was a bit more challenging to get set up. GCP’s database migration tool didn’t work with AlloyDB at the time, so we used the Foreign Data Wrapper (FDW) extension to copy data over from our main Postgres database. Once data was in AlloyDB, we manually configured the columnar engine for our tables of interest.

While the results were promising, we decided to stop testing with Alloy DB when we realized it wasn’t yet FedRAMP compliant. ROSALIND was a FedRAMP Moderate compliant platform, so we could only use GCP products that were FedRAMP Moderate or High. As of December 2024, Alloy DB is under review for FedRAMP High.

BigQuery

BigQuery was also challenging to get set up. GCP’s Datastream product does allow you to sync data from Cloud SQL Postgres to BigQuery, but we ran into one major issue:

Once data was imported into BigQuery, we could either use a SQLAlchemy plugin or raw queries to query the database. We started with raw queries, as we already had support for that in our backend. IIRC, nearly every query to BigQuery completed in ~10 seconds or less. BigQuery also cached results automatically, and invalidated the cache daily.

While this was great for performance, we saw that BigQuery was scanning ~15GB of data for every query. With some basic math, we found that every unique query was costing us ~$0.09375 (about 9 cents). That’s totally fine for testing, but not something we were okay with for production traffic.

On average, we saw a few hundred queries for the Genome Browser every day. Assuming we get 200 unique Genome Browser queries a day, that equates to ~$19 a day. Not too bad! But we were also looking to add more queries into this system, which would’ve driven up the cost by an order of magnitude. With a total estimated cost of over $200 a day, we just weren’t comfortable paying for yet another database.


End Result

In the end, we decided to denormalize data and fit the denormalized tables to our access patterns. I hated this solution then and I still hate this solution today, but it did solve the problems we were having with query times. Keeping those denormalized tables up-to-date was challenging and took a lot of engineering effort. Now that AlloyDB is (in the process of becoming) FedRAMP compliant, that seems like the obvious choice to redo this project.