Read time: ~7 minutes
“Give me the last known value” is one line of English. Until PostgreSQL 19, it was two levels of window functions.
This is Part 3 of my series on what’s new in PostgreSQL 19 for application developers. Part 1 covered INSERT … ON CONFLICT DO SELECT, and Part 2 the faster SELECT expression INTO variable in PL/pgSQL.
This one is a small clause with a long history: IGNORE NULLS for window functions.
The Last Known Value Problem
Data with gaps is everywhere:
- A price feed that only sends a value when it changes
- A sensor that drops readings
- A status history where most rows don’t touch the status column
The report always asks the same thing. For every row, show the last value we actually knew.
Let’s create a small price feed with gaps:
CREATE TABLE price_ticks ( symbol text, tick_at timestamptz, price numeric ); INSERT INTO price_ticks VALUES ('DCG', '2026-11-20 09:15', 101.50), ('DCG', '2026-11-20 09:16', NULL), ('DCG', '2026-11-20 09:17', NULL), ('DCG', '2026-11-20 09:18', 102.25), ('DCG', '2026-11-20 09:19', NULL), ('DCG', '2026-11-20 09:20', 101.75); Before PostgreSQL 19: The Running-Count Trick
The obvious attempt, lag() and last_value(), just hands the gaps back:
postgres=# SELECT to_char(tick_at,'HH24:MI') AS tick, price, lag(price) OVER w AS prev_price, last_value(price) OVER w AS last_value FROM price_ticks WINDOW w AS (PARTITION BY symbol ORDER BY tick_at) ORDER BY tick_at; tick | price | prev_price | last_value -------+--------+------------+------------ 09:15 | 101.50 | | 101.50 09:16 | | 101.50 | 09:17 | | | 09:18 | 102.25 | | 102.25 09:19 | | 102.25 | 09:20 | 101.75 | | 101.75 (6 rows) Looks close, right? It isn’t. lag() looks exactly one row back even if that row is NULL. last_value() returns the current row’s value, NULL included.
The standard PostgreSQL answer is a two-step trick. First, a running count(price) which only counts non-NULL values, so it stays flat across a gap. That gives every gap the same group number as the last known value. Then first_value() inside each group:
SELECT to_char(tick_at,'HH24:MI') AS tick, price, first_value(price) OVER (PARTITION BY symbol, grp ORDER BY tick_at) AS filled_price FROM ( SELECT *, count(price) OVER (PARTITION BY symbol ORDER BY tick_at) AS grp FROM price_ticks) s ORDER BY tick_at; tick | price | filled_price -------+--------+-------------- 09:15 | 101.50 | 101.50 09:16 | | 101.50 09:17 | | 101.50 09:18 | 102.25 | 102.25 09:19 | | 102.25 09:20 | 101.75 | 101.75 (6 rows) It works. It’s also a subquery, two window passes and a comment block explaining why count() is in there.
And the syntax every Oracle developer already knows? On PostgreSQL 18:
postgres=# SELECT tick_at, price, last_value(price) IGNORE NULLS OVER (PARTITION BY symbol ORDER BY tick_at) AS filled_price FROM price_ticks; ERROR: syntax error at or near "NULLS" LINE 2: last_value(price) IGNORE NULLS OVER (PARTITION BY sym... ^ PostgreSQL 19: IGNORE NULLS and RESPECT NULLS
The release notes put it in one line: “Allow window functions to ignore NULLs with the IGNORE NULLS/RESPECT NULLS clause.”
The window functions page shows where the clause goes — after the argument list, before OVER:
lag ( value [, offset [, default ]] ) [ null treatment ]lead ( value [, offset [, default ]] ) [ null treatment ]first_value ( value ) [ null treatment ]last_value ( value ) [ null treatment ]nth_value ( value, n ) [ null treatment ]null treatment := RESPECT NULLS | IGNORE NULLS Three rules to remember:
- Only these five functions. The docs say the option is “only allowed” for
lag,lead,first_value,last_valueandnth_value. Not forrow_number(),rank()or aggregates used as window functions. RESPECT NULLSis the default. Every query you have today keeps its meaning.IGNORE NULLSskips NULLs when counting.lag(price) IGNORE NULLSis the previous non-NULL price, however many rows back it is.
Function with IGNORE NULLS | Returns |
|---|---|
last_value(x) | Last non-NULL x in the frame – “last known value” |
first_value(x) | First non-NULL x in the frame |
lag(x) | Previous non-NULL x before the current row |
lead(x) | Next non-NULL x after the current row |
nth_value(x, n) | The nth non-NULL x in the frame |
Let’s See It in Action
The running-count trick becomes one window function. While we’re here, lag() and lead() give us the previous and next known prices too:
SELECT to_char(tick_at,'HH24:MI') AS tick, price, last_value(price) IGNORE NULLS OVER w AS filled_price, lag(price) IGNORE NULLS OVER w AS prev_known, lead(price) IGNORE NULLS OVER w AS next_known FROM price_ticks WINDOW w AS (PARTITION BY symbol ORDER BY tick_at) ORDER BY tick_at; No subquery. No count() trick. The SQL now says what the report asks for.
┌───────┬────────┬──────────────┬────────────┬────────────┐│ tick │ price │ filled_price │ prev_known │ next_known │├───────┼────────┼──────────────┼────────────┼────────────┤│ 09:15 │ 101.50 │ 101.50 │ │ 102.25 ││ 09:16 │ │ 101.50 │ 101.50 │ 102.25 ││ 09:17 │ │ 101.50 │ 101.50 │ 102.25 ││ 09:18 │ 102.25 │ 102.25 │ 101.50 │ 101.75 ││ 09:19 │ │ 102.25 │ 102.25 │ 101.75 ││ 09:20 │ 101.75 │ 101.75 │ 102.25 │ │└───────┴────────┴──────────────┴────────────┴────────────┘(6 rows) Migration Angle: Oracle Has Had This for Years
Oracle supports IGNORE NULLS on LAG, LEAD, FIRST_VALUE, LAST_VALUE and NTH_VALUE. Until now, every one of those calls had to be rewritten during conversion usually into the running-count trick above.
One detail matters. Oracle accepts IGNORE NULLS in two places:
-- Oracle: both forms work -- standard form LAST_VALUE(price) IGNORE NULLS OVER (ORDER BY tick_at) -- inside the parentheses LAST_VALUE(price IGNORE NULLS) OVER (ORDER BY tick_at) -- inside, before offset LAG(price IGNORE NULLS, 1) OVER (ORDER BY tick_at) Oracle’s own LAST_VALUE docs say the two forms behave the same and recommend the ANSI one. PostgreSQL 19 documents only the standard form after the closing parenthesis. So the in-parentheses form still needs a small rewrite:
| Oracle source | PostgreSQL ≤ 18 | PostgreSQL 19 |
|---|---|---|
LAST_VALUE(x) IGNORE NULLS OVER (…) | Running-count subquery | Unchanged ![]() |
LAST_VALUE(x IGNORE NULLS) OVER (…) | Running-count subquery | last_value(x) IGNORE NULLS OVER (…) |
LAG(x IGNORE NULLS, n) OVER (…) | Running-count or LATERAL subquery | lag(x, n) IGNORE NULLS OVER (…) |
NTH_VALUE(x, n) FROM LAST IGNORE NULLS | Reverse the ORDER BY | IGNORE NULLS, and still reverse the ORDER BY FROM LAST isn’t implemented |
For conversion tools, that is a mechanical move of three words. For a migration team, it is one less pattern to explain in code review.
Couple of Things to Remember
1. The frame still rules. first_value, last_value and nth_value only look inside the window frame. By default that frame runs from the start of the partition to the current row (and its peers). That is exactly right for “last known value”. For “next known value”, use lead(x) IGNORE NULLS not last_value with a wider frame.
2. Ties need a tie-breaker. With the default RANGE frame, rows with the same ORDER BY value are peers and all sit inside the frame. If two ticks share a timestamp, last_value can pick up its peer’s price. Order by something unique, or use ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
3. Nothing known yet means NULL. If every row so far is NULL, last_value(x) IGNORE NULLS returns NULL. If you want a default, wrap it in COALESCE.
4. It doesn’t apply to aggregates. sum(x) IGNORE NULLS OVER (…) is not a thing. Aggregates already skip NULLs.
Key Takeaways
last_value(x) IGNORE NULLS OVER (…)replaces the running-count trick for “last known value”
lag()/lead()withIGNORE NULLSgive the previous and next known values
Oracle’s standard-form IGNORE NULLSnow converts as-is
Oracle’s in-parentheses form, LAST_VALUE(x IGNORE NULLS), still needs moving outside
nth_value(…) FROM LASTis still not implemented
The Bottom Line
The data still has gaps.
Your SQL doesn’t have to.
Next in the series: JSON in PostgreSQL 19 new jsonpath string methods, and one behaviour change that can surprise your application.