Summary
lag is a PostgreSQL built-in window function in the Window Functions group. PostgreSQL’s system catalog (pg_proc) describes it as: “fetch the preceding row value”.
Signature
lag has 3 documented overloaded forms:
lag(anyelement) → anyelement
lag(anyelement, integer) → anyelement
lag(anycompatible, integer, anycompatible) → anycompatible
Argument and return types are taken from the pg_proc catalog; internal type names are shown using their readable SQL spellings (for example int4 is shown as integer). (Derived from the catalog — see the linked reference for the canonical documentation.)
Classification
- Category: Window Functions
- Kind: Window function
- Volatility: IMMUTABLE — Marked IMMUTABLE — it always returns the same result for the same arguments and can be used in indexes and other contexts that require immutability.
- Returns:
anycompatible, anyelement
Example
Illustrative form (replace placeholder values with your own data):
SELECT lag(col) OVER (ORDER BY id) FROM your_table;
The example above is illustrative and is meant to show calling syntax only; consult the linked PostgreSQL documentation for exact semantics, edge cases and accepted argument combinations.
Version applicability
lag is present across the surveyed releases (PostgreSQL 15, 16, 17, 18, 19). On older major versions, behaviour may differ in detail — always check the documentation for the version you run.
Related & references
Reference: PostgreSQL documentation — Window Functions.