typelevel / typelevel/skunk

Idea: Statement Caching

Open
#496 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.7k
Forks
175
Avg merge
5d 14h
Merged PRs (30d)
9

Description

Skunk closes prepared statements after use, which ensures that sessions don't build up an unbounded number of open statements on the server. This is good in principle, but it means that we have to do a Parse exchange for every prepared statement, even if an identical one was previously prepared on the same Session.

An alternative way to do this would be to have an LRU cache from Statement to StatementId on each Session.

  • Parse checks the cache first and if it's there we have no need to do further work.
  • If the statement is not in the cache, do the Parse exchange and insert into the cache on success.

On both access and insert there may be cache evictions, so each Session needs to track this set as well.

  • On return to the pool all pending evictions are processed asynchronously (i.e., we do a bunch of Close exchanges). This means a Session returning to the pool may be unavailable for immediate use. We can pipeline these messages, which should be fast.

A consequence here is that .prepare can now return an F[PreparedCommand[...]] rather than a Resource[F, PreparedCommand[...]] (analogously for queries). This seems like a usability improvement to me but it will break everyone's code. As a first step we could just do Resource.eval to keep the API the same.

Note that the Describe cache is per pool so we don't want to roll it into the Parse cache, which is per session. If we did this we would end up running Describe exchanges for statements we already know are valid.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Session statement handling and the existing per-pool Describe cache, then inspect the Parse and Close exchanges and session return-to-pool behavior. Define how a per-session LRU cache and pending evictions would work without merging the Describe cache, and resolve the proposed prepare API consequences.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgres, scala
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.