Hyperdrive + Postgres.js not caching queries with parameters

For Workers & Pages, what is the name of the domain?

example.com

What is the issue or error you’re encountering

Hyperdrive will not cache queries (even when read-only) as soon as any arguments / parameters are included.

What are the steps to reproduce the issue?

  1. Follow the getting started guide here: Getting started · Hyperdrive docs

  2. Run the following queries:

    Will be cached:

    await sql`select 'hello' as "message";`
    

    Will not be cached (marked as uncacheable):

    await sql`select ${'hello'} as "message";`
    

Is this expected behaviour?

Currently my query is 100% read-only but uses parameters for pagination (limit/offset), and I’d like them to be cached. I do not see any mention of this behaviour in this doc: Query caching · Hyperdrive docs

Can you share your postgres.js configuration? Both queries should be entirely cacheable from my understanding. Are you perhaps using prepare: false? This was needed in the past before Hyperdrive supported prepared statements, but isn’t any longer.

Both queries should be entirely cacheable from my understanding

Yeah I thought so too…

My Postgres.js setup is exactly as described in the getting started docs:

const sql = postgres(
    env.HYPERDRIVE.connectionString,
    {
        max: 5,
        fetch_types: false,
    }
)

I should also mention how I’m “testing” if a query is getting cached. I’m simply comparing the response times of my worker.

Without parameters query: 20-50ms average response time
With parameters query: 200-300ms average response time

The chart in the dashboard doesn’t update very quickly so observing the numbers there is much slower than just checking the responses times. I know it’s not the most scientific method, but the response times are so consistent and different it seems good enough.

This topic was automatically closed after 15 days. New replies are no longer allowed.