Skip to content
Planet PostgreSQL·· 1d ago

Deepak Mahto: Exploring PostgreSQL 19 – Part 2: SELECT expression INTO in PL/pgSQL

Deepak Mahto: Exploring PostgreSQL 19: A Developer’s Guide to New Features – Part 2: SELECT expression INTO in PL/pgSQL

Brief · Based on the source excerpt

PostgreSQL 19 closes the performance gap between SELECT expr INTO v and v := expr in PL/pgSQL, which was roughly 5x slower before. A 1,000,000-iteration benchmark on PostgreSQL 18 took 201.

Test conditions

Migrated Oracle and SQL Server PL/pgSQL code gains speed without rewrites; after upgrading to PostgreSQL 19, re-benchmark loop-heavy procedures that rely on SELECT ... INTO.

Technical Breakdown · Distilled from the original source

What Changed

  • Before PostgreSQL 19, SELECT expr INTO v and v := expr produced the same result in PL/pgSQL but the former was about 5x slower.
  • No new syntax is introduced; existing code needs no changes to benefit, targeting habits common in code migrated from Oracle and SQL Server.
  • The PostgreSQL 19 commit notes that SQL Server and T-SQL users "are used to writing SELECT … INTO".

Benchmark

  • Two equivalent functions used v := v + i * 2 versus SELECT v + i * 2 INTO v, each looping 1,000,000 times.
  • On PostgreSQL 18, bench_assign(1000000) took 201.678 ms; bench_select_into(1000000) took roughly 1630 ms (the line is truncated in the source).
  • Both returned 1000001000000, confirming identical results with different execution cost.

Migration Context

  • Oracle developers write SELECT … INTO … FROM DUAL; once FROM DUAL is dropped, SELECT … INTO remains.
  • These lines could be written as assignments such as v_now := clock_timestamp(); and v_code := upper(trim(p_code));.
  • This is Part 2 of the series; Part 1 covered INSERT … ON CONFLICT DO SELECT.
Source: Planet PostgreSQL· postgr.es

PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.

Read Official Source

Related PostgreSQL Reference