Skip to content

Conversation

@NikolayS
Copy link
Owner

No description provided.

NikolayS and others added 6 commits December 23, 2025 10:19
It is a preliminary commit that tracks the state of temp buffers. The main goal
of this statistic is to provide the optimiser with numbers to compute
the cost estimation of flushing temporary buffers.

Such a flush may be necessary if the optimiser decides to build a plan that
includes a parallel section of the query, which involves scanning
a temporary table.
Change the concept of parallel safety slightly: a query subtree may be
parallel-safe if it includes a temporary table scan, but each buffer of this
temporary table is flushed to disk. In this case, minor changes within
the planner and executor may allow scanning the temporary table in parallel.

By this commit, the optimiser uses the 'parallel_safe' flag to indicate that
the subtree refers to a source with temporary storage.

Path's parallel_safe field may be used in cost-based optimisation, Plan's
parallel_safe field indicates if a Gather or GatherMerge node should flush all
temporary buffers before launching any parallel worker.

We don't make this flag very selective. If different paths of the same
RelOptInfo have various targets, we indicate that each path requires buffer
flushing, even if only one of them actually needs it.
This commit adds a flag to Gather and GatherMerge that indicates whether
the subtree contains temporary tables. Additionally, to prevent multiple flush
attempts, EState has a flag that indicates whether temporary buffers have
already been written to disk.

Employing these two flags, Gather flushes temporary buffers before launching
any parallel worker.

Add some checks to detect accidential scanning of a temp table with not yet
flushed buffers.
Consider the extra cost of flushing temporary tables in partial path
comparisons. With this commit, the optimiser gains a rationale for cost-based
decision on enabling the parallel scan of subtrees that include temporary
tables. It is achieved by adding to the path comparison routine an extra
'flush buffers' weighting factor.

It is trivial to calculate the cost by tracking the number of dirtied temporary
buffers and multiplying it by the write_page_cost parameter.
The functions compare_path_costs and compare_fractional_path_costs
were modified to account for this additional factor.
@NikolayS NikolayS changed the title Claude/rebase postgres fork pm xoe rebased https://github.com/danolivo/pgdev/tree/temp-bufers-stat-master Dec 27, 2025
@NikolayS NikolayS force-pushed the claude/rebase-postgres-fork-pmXOE branch from eb9cb89 to 3dd4f3a Compare December 27, 2025 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants