Re: Plan caching and serialization for reuse across executions

From: Andrei Lepikhov <lepihov(at)gmail(dot)com>
To: Xuan Chen <cx0222(at)vip(dot)qq(dot)com>, pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Plan caching and serialization for reuse across executions
Date: 2025-08-18 14:09:55
Message-ID: afee378f-df04-4a16-8d00-4debaa92e1a4@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 16/8/2025 14:32, Xuan Chen wrote:
> Hi hackers,
>
> I am currently exploring whether it is possible to cache a plan generated by the PostgreSQL planner and reuse it across later executions.
>
> I understand that there are existing mechanisms like PREPARE/EXECUTE and CachedPlan in plancache.c, but these are tied to prepared statements and session-level usage. My interest is more on the research side:
>
> - Is there any supported (or experimental) way to serialize a Plan or PlannedStmt structure and reload it in a future execution, bypassing the planner?
> - If not, would it make sense to extend the existing CachedPlan mechanism to support serialization/deserialization, or are there known architectural reasons why this approach is discouraged?
> - Any relevant references in the source code (planner, plancache.c, executor) that I should study would be very helpful.
>
> The motivation is research-oriented: I want to experiment with plan persistence as a way to reduce planning overhead and study trade-offs of reusing cached plans.
>
> Thanks a lot for your guidance!
Yes, it is possible. See [1] to find out how it is implemented in the
Postgres architecture.
We implemented 'freezing' of a statement with parameterisation defined
by the user. Each incoming query (constant or parameterised) matches the
QueryId. Then, with matching incoming and the stored parse tree, it
finds the corresponding plan and proves that it may be used for the
query (remember, rewriting rules, indexes, and other factors may change).
Such a 'frozen' plan is serialised into the shared memory and is lazily
transferred to all backends and used across all instances.
There are lots of issues that still exist for me after the finish of
this project. So, you have a whole room of problems that you can address
in your research ;).

[1] https://github.com/danolivo/conf/blob/main/2023-PGDay-Israel/sr-plan.pdf

--
regards, Andrei Lepikhov

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-08-18 14:18:34 Re: Plan caching and serialization for reuse across executions
Previous Message Andrei Lepikhov 2025-08-18 13:59:37 Re: Plan caching and serialization for reuse across executions