PostgreSQL 18 Old & New
Learning Structured Query Language can be frustrating when double-checking that what you wanted to have done is actually what was done. PostgreSQL 18 has 'OLD and NEW support for RETURNING clauses in INSERT, UPDATE, DELETE, and MERGE commands'. Now you can get instant feedback.
The addition of the RETURNING clause in the previous version made MERGE much easier to use. Now it makes other commands easier.
To demonstrate, let's create a table with one column that is designated as a unique, primary key integer and insert a value.
create table foo (a int unique primary key);
insert into foo (a) values (1);
Now is the point where many of us would run a SELECT a FROM foo, just to double check that indeed there is a 1 in column a.
There is now an option in PG 18 to use RETURNING, and it provides the previous 'old' value of a column along with the 'new' value.
insert into foo (a) values (2) returning old.a, new.a;
-+-+
|2|
Upserts
insert into foo(a) values (2)
on conflict(a) do update
set a = 99
returning old.a as old, new.a as update
Comments
Post a Comment