On 18th of June, Tom Lane committed patch:

Implement UPDATE tab SET (col1,col2,...) = (SELECT ...), ...
 
This SQL-standard feature allows a sub-SELECT yielding multiple columns
(but only one row) to be used to compute the new values of several columns
to be updated.  While the same results can be had with an independent
sub-SELECT per column, such a workaround can require a great deal of
duplicated computation.
 
The standard actually says that the source for a multi-column assignment
could be any row-valued expression.  The implementation used here is
tightly tied to our existing sub-SELECT support and can't handle other
cases; the Bison grammar would have some issues with them too.  However,
I don't feel too bad about this since other cases can be converted into
sub-SELECTs.  For instance, "SET (a,b,c) = row_valued_function(x)" could
be written "SET (a,b,c) = (SELECT * FROM row_valued_function(x))".

The commit message explains everything nicely, but let me just show it, with one additional info.

For starters – you could have used:

update table set (a,b,c) = (...)

for quite some time now, for example, in 9.1:

$ create table test as select oid, null::text as database_name, null::int4 as encoding from pg_database;
SELECT 5
 
$ update test as t set (database_name, encoding) = (x.datname, x.encoding) from pg_database x where x.oid = t.oid;
UPDATE 5
 
$ select * from test where database_name ~ 'templ';
  oid  | database_name | encoding 
-------+---------------+----------
     1 | template1     |        0
 11910 | template0     |        0
(2 rows)

The new feature in here is that you can use explicit subquery, which previously would have failed:

$ create table test as select oid, null::text as database_name, null::int4 as encoding from pg_database;
SELECT 5
 
$ update test as t set (database_name, encoding) = (select x.datname, x.encoding from pg_database x where x.oid = t.oid );
ERROR:  syntax error at or near "select"
LINE 1: update test as t set (database_name, encoding) = (select x.d...
                                                          ^

and now, in 9.5devel:

$ create table test as select oid, null::text as database_name, null::int4 as encoding from pg_database;
SELECT 5
 
$ update test as t set (database_name, encoding) = (select x.datname, x.encoding from pg_database x where x.oid = t.oid );
UPDATE 5

This will be specifically useful with functions, as described in the commit message.

For example, let's assume we have a function, which returns, for every integer – it's 2nd and third power:

CREATE OR REPLACE FUNCTION powers( IN input INT4, OUT square INT8, OUT cube INT8 ) RETURNS record AS $$
BEGIN
    square := input * input;
    cube := square * input;
    RETURN;
END;
$$ language plpgsql;

Now, we have simple table with some integers:

$ create table test (base int4, p2 int8, p3 int8);
CREATE TABLE
 
$ insert into test (base) select generate_series(2,15, 3);
INSERT 0 5

Previously, we'd have to write something like this:

update test as t
    set p2 = (t2.powers).square,
        p3 = (t2.powers).cube
from (
    select base, powers(base)
    from test
) as t2
where t2.base = t.base;

Now, the query is much simpler and easier to read:

update test as t
    set (p2, p3) = (select square, cube from powers(t.base))

It's somehow similar to LATERAL.

  1. 4 comments

  2. # Vano
    Jun 22, 2014

    Is it possible to use constants in subselect? For instance

    update tab1 set (c1, c2) = (select 7, t2.name from t2)

  3. Jun 22, 2014

    @Vano:
    of course – it’s just normal subselect. The only thing that is important is that it should return just one row.

  4. Jun 22, 2014

    Hi Depesz. I have a off-topic question. Do you know if there are any plans to add mysql like inserts which looks similar to updates?

    insert into tab set
    c1 = ‘aa’,
    c2 = 10

    I must admit that form is very convenient and greatly simplifies creating dynamic queries.

  5. Jun 23, 2014

    @Unodgs:
    I have no information about such work or plans.

Leave a comment