Read time: ~7 minutes
In PL/pgSQL, SELECT expr INTO v and v := expr do the same thing. Until PostgreSQL 19, one of them was about 5x slower.
This is Part 2 of my series on what’s new in PostgreSQL 19 for application developers. Part 1 covered INSERT … ON CONFLICT DO SELECT. [part1]
This one has no new syntax at all. Your code doesn’t change. It just gets faster, if you write PL/pgSQL the way most migrated code is written.
The Line Every Converted Procedure Has
Open any PL/pgSQL code base that came from Oracle or SQL Server. You’ll find lines like these:
SELECT clock_timestamp() INTO v_now;SELECT v_total + v_line_amount INTO v_total;SELECT upper(trim(p_code)) INTO v_code;
They come from habits that make sense in the source database. Oracle developers write SELECT … INTO … FROM DUAL, and once FROM DUAL goes, SELECT … INTO stays. As the PostgreSQL 19 commit puts it, users coming from SQL Server and T-SQL “are used to writing SELECT … INTO” too.
In PL/pgSQL, every one of those lines could have been written as an assignment:
v_now := clock_timestamp();v_total := v_total + v_line_amount;v_code := upper(trim(p_code));
Same result. Very different cost.
Before PostgreSQL 19: Same Logic, 8x the Time
Let’s create two functions that do exactly the same work, One with assignment, one with SELECT … INTO:
CREATE OR REPLACE FUNCTION bench_assign(n int)RETURNS numeric LANGUAGE plpgsql AS $$DECLARE v numeric := 0; i int;BEGIN FOR i IN 1..n LOOP v := v + i * 2; END LOOP; RETURN v;END $$;CREATE OR REPLACE FUNCTION bench_select_into(n int)RETURNS numeric LANGUAGE plpgsql AS $$DECLARE v numeric := 0; i int;BEGIN FOR i IN 1..n LOOP SELECT v + i * 2 INTO v; END LOOP; RETURN v;END $$;
One million iterations each, on PostgreSQL 18:
postgres=# \timing onpostgres=# SELECT bench_assign(1000000); bench_assign--------------- 1000001000000(1 row)Time: 201.678 mspostgres=# SELECT bench_select_into(1000000); bench_select_into------------------- 1000001000000(1 row)Time: 1639.946 ms (00:01.640)
Same answer. 8x the time.
The Oracle FROM DUAL style shows the same gap. A million SELECT clock_timestamp() INTO d took 956 ms. A million d := clock_timestamp() took 125 ms.
This isn’t new, and it isn’t your hardware. On the hackers list, Pavel Stehule shared the same comparison across releases:
Release:= assignmentSELECT … INTO9.42,900 ms20,800 ms112,041 ms16,243 msmaster (pre-19)534 ms15,438 ms
Assignment got faster with almost every release. SELECT … INTO barely moved. Tom Lane’s reply sums it up: “we’ve sweated a good deal about optimizing plpgsql assignment, but SELECT INTO is always done the hard way.”
Under the Hood: Two Roads Inside PL/pgSQL
PL/pgSQL hands every expression to the SQL engine;
Even v + i * 2 is internally a SELECT v + i * 2.
What matters is which road it takes inside pl_exec.c.
Road 1 – the simple-expression fast path. When you write v := expr, PL/pgSQL checks whether the expression is “simple”. If it is, it caches an expression state and calls the expression evaluator directly. No SPI. No portal. No executor start-up and shut-down per call.
Road 2 – the SPI road. When you write SELECT expr INTO v, PL/pgSQL has always sent it through SPI, the same machinery that runs any SQL statement inside a function. That means executor start-up, a result row, copying it into the variable and executor shut-down for every single call. It is the right road for real queries. For SELECT 1 + 1, it is a toll road.
What counts as “simple”? In the commit’s words, “basically ‘SELECT scalar-expression’ without any input tables, aggregates, qual clauses, etc.”
The rows were never the problem a real query pays for real work. The waste was always in the rows.
What Changed in PostgreSQL 19
The PostgreSQL 19 release notes list it in one line, under General Performance: “Optimize plpgsql syntax SELECT simple-expression INTO var.”
The story behind that line:
January 2026 — Pavel Stehule raises it on pgsql-hackers with the numbers above.
Same day — Tom Lane posts a prototype that routes simple SELECT … INTO through the assignment code path. It “does reclaim nearly all of the performance difference.”
March 20, 2026 — Commit ce8d5fe0e lands for PostgreSQL 19, authored by Tom Lane and reviewed by Pavel Stehule.
October 5, 2026 — A follow-up fix, back-patched to 19, handles an expression that stops being simple after a replan. It was found and fixed before the 19.0 release.
With the patch, Tom Lane ran Pavel’s two test cases on his own machine: about 276 ms for assignment vs 337 ms for SELECT … INTO. Before, on master, Pavel had measured 534 ms vs 15,438 ms.
The rule in PostgreSQL 19 is simple:
If the expression is simple and the INTO target is a single variable, SELECT expr INTO var now takes the same fast path as var := expr. Everything else still goes through SPI, exactly as before.
No new syntax. No setting to turn on. No code change. Upgrade, and the converted code you already have gets the fast path.
“Not Entirely Transparent”
The commit is honest about one side effect. Part of why the fast path is faster is that it skips the hooks pg_stat_statements relies on. In Tom Lane’s words, “the evaluated expression no longer appears in pg_stat_statements output as it did before.”
That only matters if you track nested statements. Here is PostgreSQL 18 with pg_stat_statements.track = 'all', after one call of each function with n = 1000:
postgres=# SELECT calls, query FROM pg_stat_statements WHERE query ILIKE '%i * 2%' OR query ILIKE '%bench%' ORDER BY calls DESC; calls | query-------+------------------------------------------------ 1000 | SELECT v + i * 2 1 | SELECT bench_select_into($1) IS NOT NULL AS ok 1 | SELECT bench_assign($1) IS NOT NULL AS ok(3 rows)
SELECT v + i * 2 shows up 1,000 times. The := version never did. On PostgreSQL 19, the SELECT … INTO version disappears from the view as well.
With the default track = 'top', nested statements weren’t tracked anyway, so most dashboards won’t notice. If yours shows top-N statements with track = 'all', expect those rows to vanish after the upgrade. Nothing is broken. That work just stopped being a statement.
Migration Angle: Watch Out for FROM DUAL (Oracle)
Here’s the gotcha for anyone coming from Oracle. Some converted code keeps FROM dual, either through a compatibility dual table (orafce provides one) or a hand-made one:
SELECT clock_timestamp() INTO d FROM dual;
That statement reads a table. A table means it is not simple, so PostgreSQL 19 still sends it down the SPI road.
So the upgrade gives you the speed-up only where FROM dual is already gone. During conversion:
Drop FROM dual from SELECT … INTO — PostgreSQL never needed it
Prefer v := expr in new code — it is fast on every version, not just 19
If the code must also run on PostgreSQL 16–18, := is still the one to write
Additionally Ora2Pg already does the right thing here: its PL/SQL converter strips FROM DUAL (and FROM SYS.DUAL), so SELECT … INTO … FROM DUAL comes out as a simple SELECT … INTO and gets the PostgreSQL 19 fast path for free. Where FROM dual survives is in code that leans on orafce’s oracle.dual view to stay Oracle-shaped. It works, but that statement reads a view, so it stays on the slow road.
Couple of Things to Remember
-
Only simple expressions, only one target. SELECT … INTO that reads a table, aggregates, filters or fills several variables goes through SPI as before. That is the right road for those statements anyway.
-
pg_stat_statements sees less. With track = 'all', nested SELECT expr INTO var statements stop showing up after the upgrade. Explain that to whoever owns the dashboards before they open a ticket.
-
Same behaviour, by design still test it. The commit says Tom Lane “tried hard to make error reporting look the same”, and also that “there may be some other minor behavioral changes.” One corner case was already caught before release: FOUND and ROW_COUNT could come out wrong after a function called in the expression was redefined as set-returning. That is fixed in 19.0. Run your PL/pgSQL regression tests on 19 anyway, especially code that checks FOUND after SELECT … INTO.
--PostgreSQL 19DO $$DECLARE v int; rc bigint;BEGIN SELECT 1 + 1 INTO v; GET DIAGNOSTICS rc = ROW_COUNT; RAISE NOTICE 'SELECT INTO: v=%, FOUND=%, ROW_COUNT=%', v, FOUND, rc; v := 5; RAISE NOTICE ':= after it: v=%, FOUND=%', v, FOUND;END $$;NOTICE: SELECT INTO: v=2, FOUND=t, ROW_COUNT=1NOTICE: := after it: v=5, FOUND=t DO
- This is not a reason to keep writing SELECT … INTO. v := expr says what you mean, and it is fast on every PostgreSQL version.
Key Takeaways
PostgreSQL 19 makes SELECT simple-expression INTO var as fast as var := expression
No new syntax, no setting, no code change — upgrade and it is there
FROM dual, or any table, keeps the statement on the slow road
Don’t expect those nested statements in pg_stat_statements any more
Write := in new code, and when the same code must run on PostgreSQL 16–18
The Bottom Line
PostgreSQL 19 didn’t add a feature here. It removed a penalty, the one you paid for writing PL/pgSQL with an Oracle or T-SQL accent.
Next in the series: IGNORE NULLS for window functions — “last known value” queries without the self-join gymnastics.
References
PostgreSQL 19 Release Notes
pgsql: plpgsql: optimize “SELECT simple-expression INTO var” — commit ce8d5fe0e, March 20, 2026
pgsql: Fix “SELECT simple_expression INTO” for expression becoming non-simple — commit e3199f314, October 5, 2026
Re: slow SELECT expr INTO var in plpgsql — pgsql-hackers thread with the timings






