Skip to content

[Performance]: Value window functions (lag/lead/first_value/last_value/nth_value) have O(n²) memory hotspot #23962

Closed

Description

@ck89119

Is there an existing issue for performance?

  • I have checked the existing issues.

Details of Performance

The current value window function execution model materializes the entire frame into heap for each output row under the default UNBOUNDED PRECEDING ... UNBOUNDED FOLLOWING scenario, causing time and space complexity to approach O(n²).

Core issues:

  1. valueWindowExec stores the complete frame for each output row in frameValues [][]*valueEntry
  2. Varlen types (varchar/text/blob/json) produce massive make+copy allocations
  3. Fill call count approaches n*n (n = partition row count)
  4. Free() does not clean up frameValues, relying mainly on GC
  5. Size() severely underestimates actual memory usage
  6. nth_value semantics not fully implemented (degrades to first_value)

Affected functions: lag, lead, first_value, last_value, nth_value
Especially significant with large partitions, varlen types, and high concurrency.

Activity

  1. self-assigned this
    on Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions