Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Window functions

This slice supports row_number(), rank(), dense_rank(), lag(), lead(), first_value(), and last_value() with OVER:

SELECT group_id, value,
       row_number() OVER (PARTITION BY group_id ORDER BY value DESC NULLS LAST)
FROM measurements;

OVER (), PARTITION BY, and ORDER BY with ASC, DESC, NULLS FIRST, and NULLS LAST are supported. Without ORDER BY, numbering follows the order received by the plan and is not deterministic. The implementation streams compatible partitions within work_mem, spilling them to scratch files when needed, and uses global materialization as a fallback for incompatible expressions.

Named windows can be declared after the filtering clauses:

SELECT row_number() OVER ordered
FROM measurements
WINDOW ordered AS (PARTITION BY group_id ORDER BY value);

This version also accepts ROWS, RANGE, and GROUPS BETWEEN frames with the bounds UNBOUNDED PRECEDING, CURRENT ROW, n PRECEDING/n FOLLOWING, and UNBOUNDED FOLLOWING. The frame is effective for first_value, last_value, and the count, sum, avg, min, max, bool_and, bool_or, bit_and, bit_or, and any_value window aggregates. Aggregates ignore NULL values; count(*) is also supported. For an ordered window without an explicit frame, aggregates use the partition start through the current peer group. FILTER and DISTINCT in window calls are supported for window aggregates; they do not apply to ranking or value functions. EXCLUDE NO OTHERS, string_agg(value, delimiter), var_samp, var_pop, variance, stddev, stddev_samp, and stddev_pop are also supported. Statistical aggregates use their sample/population semantics and return NULL when there are insufficient observations. EXCLUDE CURRENT ROW, EXCLUDE GROUP, and EXCLUDE TIES are supported. lag and lead accept value, an optional offset, and an optional default, in that order. The offset must be a non-negative integer; when no matching row exists, the default is returned, or NULL when no default was supplied. Without an explicit frame, first_value and last_value use the window’s default frame; with an explicit frame, they respect its bounds and EXCLUDE clause.

When one or more windows share the same specification and use direct column keys, the plan shares one sort and processes one partition at a time, charging that partition against work_mem. If it exceeds the limit, the partition uses the scratch file. Incompatible windows or expression keys still use the global materialization path.

Ordered-set aggregates

The first ordered-set aggregate slice also supports percentile_disc(fraction), percentile_cont(fraction), and mode() with WITHIN GROUP (ORDER BY ...):

SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY value),
       percentile_cont(0.5) WITHIN GROUP (ORDER BY value),
       mode() WITHIN GROUP (ORDER BY value)
FROM measurements;

Each call accepts one or more ORDER BY expressions, including ASC, DESC, NULLS FIRST, and NULLS LAST. Null order values are ignored; an input with no non-null values returns NULL. percentile_disc returns an existing ordered value, while percentile_cont interpolates numeric values and returns float8. For a tied mode, the first value in the requested order wins.

The functions work with ordinary GROUP BY, GROUPING SETS, ROLLUP, and CUBE, and can be mixed with normal aggregates in the same query. DISTINCT is supported on the direct argument, for example percentile_disc(DISTINCT 0.5), and deduplicates the first ORDER BY value before calculation. FILTER (WHERE ...) is applied before deduplication and ordering. The implementation materializes groups in memory and partitions groups into scratch files when they exceed work_mem; a single group also uses the shared external sort. Scalar queries follow the same path.