Skip to content

Free the rows of each run of a LATERAL relation that no tuple uses - #444

Merged
tamnd merged 2 commits into
mainfrom
lateral-memory
Oct 20, 2026
Merged

tamnd merged 2 commits into
mainfrom
lateral-memory

Conversation

@tamnd

@tamnd tamnd commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Summary

A query with a LATERAL relation in a nested loop kept the rows of every run of that relation until the query ended. In the regression tests join and subselect, a self-join of tenk1 ran the relation 10,000 or 100 million times. The memory then grew by about 50 MB each second until the system stopped the server, and the 126 tests after it could not connect. Now the scan frees the rows of a run that no joined tuple uses.

Changes

  • Tables::marks gives the number of rows of each LATERAL relation before a run.
  • Tables::compact runs after the join of each outer row. It keeps only the new rows that the joined tuples use, and it gives those tuples the new row numbers. The memory is then the size of the result of the join, not the size of all the runs.

Known differences

None. The result of each query is the same. Only the memory changes.

Tests

connection/lateral_runs.test is the output of PostgreSQL 19 for the same script in a new database. It has joins that keep a few rows of many runs, an outer join with empty runs, a whole-row reference, a chain of two LATERAL relations and an aggregate. fmt, clippy, the workspace tests and the checks of xtask pass on server1.

@tamnd tamnd added kind/bug It does the wrong thing. area/execution Operators, vectors, morsels, joins, aggregation and spill. labels Oct 20, 2026
@tamnd
tamnd merged commit 695fac9 into main Oct 20, 2026
5 checks passed
@tamnd
tamnd deleted the lateral-memory branch October 20, 2026 07:11
@tamnd tamnd mentioned this pull request Oct 20, 2026
1 of 17 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/execution Operators, vectors, morsels, joins, aggregation and spill. kind/bug It does the wrong thing.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant