pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ?
3+ messages / 3 participants
[nested] [flat]

* AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ?
@ 2026-08-03 15:17 Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
  2026-08-03 16:15 ` Re: AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ? Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Färber, Franz-Josef (StMUK) @ 2026-08-03 15:17 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Yes, the least grave of my items can be worked around by wrapping the function into a subselect, for whatever reason this might be necessary.
(As far as I know there are other places in Postgres where I can substitute a subselect by an expression / a function call, and I am curious: Why not here?)

But what about my other item... what about somehow preventing to write all column names, i. e. changing ...

    SET (col1, col2, ...)

... to something like ...

    SET (myTable.*)

?


My first intention on posting was: Is there some way to acheive that?

If no, my second intention is a general critique on ... I think it is at least similar to what https://carlineng.com/?postid=sql-critique#blog calls " Lack of Orthogonality" in (Postgres-)SQL.


-----Ursprüngliche Nachricht-----
Von: Adrian Klaver <adrian.klaver@aklaver.com> 
Gesendet: Montag, 3. August 2026 17:00
An: Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>; pgsql-general@lists.postgresql.org
Betreff: Re: UPDATE myTable SET (myTable.*) = json_populate_record(...) ?

On 8/3/26 12:09 AM, Färber, Franz-Josef (StMUK) wrote:
> Dear Postgres Community,
> 
> cf. https://www.postgresql.org/docs/current/sql-update.html :
> 
> As far as I understand, I can
> 
> * UPDATE myTable SET col1 = val1, col2 = val2, ...
> * UPDATE myTable SET (col1, col2, ...) = <ROW-or-subselect>
> 
> But I cannot, and I am missing that:
> * UPDATE myTable SET (myTable.*) = <ROW-or-subselect>
> * UPDATE myTable SET (col1, col2, ...) = json_populate_record(NULL::myTable, myJson)   -- this is a function, not a ROW construct and not a subselect

Make it a subselect? Not tested but:

UPDATE myTable SET (col1, col2, ...) = (select * from json_populate_record(NULL::myTable, myJson))

> 
> This would be useful for some of my trigger code.
> 
> 
> Regards,
> fjf2002
> 
> 
> 


--
Adrian Klaver
adrian.klaver@aklaver.com


^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ?
  2026-08-03 15:17 AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ? Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
@ 2026-08-03 16:15 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2026-08-03 16:48   ` Re: AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Adrian Klaver @ 2026-08-03 16:15 UTC (permalink / raw)
  To: Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>



On 8/3/26 8:17 AM, Färber, Franz-Josef (StMUK) wrote:
> Yes, the least grave of my items can be worked around by wrapping the function into a subselect, for whatever reason this might be necessary.
> (As far as I know there are other places in Postgres where I can substitute a subselect by an expression / a function call, and I am curious: Why not here?)

Please provide examples of the cases where you can use a 
function/expression.

As to why it is necessary the example shown here:

https://www.postgresql.org/docs/current/functions-json.html

shows that to extract the columns you need to do the SELECT.

> 
> But what about my other item... what about somehow preventing to write all column names, i. e. changing ...
> 
>      SET (col1, col2, ...)
> 
> ... to something like ...
> 
>      SET (myTable.*)

Best bet is you are seeing the effect of, from here:

https://www.postgresql.org/docs/18/sql-update.html

"Do not include the table's name in the specification of a target column 
— for example, UPDATE table_name SET table_name.col = 1 is invalid."

> 
> ?
> 
> 
> My first intention on posting was: Is there some way to acheive that?
> 
> If no, my second intention is a general critique on ... I think it is at least similar to what https://carlineng.com/?postid=sql-critique#blog calls " Lack of Orthogonality" in (Postgres-)SQL.
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: Adrian Klaver <adrian.klaver@aklaver.com>
> Gesendet: Montag, 3. August 2026 17:00
> An: Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>; pgsql-general@lists.postgresql.org
> Betreff: Re: UPDATE myTable SET (myTable.*) = json_populate_record(...) ?
> 
> On 8/3/26 12:09 AM, Färber, Franz-Josef (StMUK) wrote:
>> Dear Postgres Community,
>>
>> cf. https://www.postgresql.org/docs/current/sql-update.html :
>>
>> As far as I understand, I can
>>
>> * UPDATE myTable SET col1 = val1, col2 = val2, ...
>> * UPDATE myTable SET (col1, col2, ...) = <ROW-or-subselect>
>>
>> But I cannot, and I am missing that:
>> * UPDATE myTable SET (myTable.*) = <ROW-or-subselect>
>> * UPDATE myTable SET (col1, col2, ...) = json_populate_record(NULL::myTable, myJson)   -- this is a function, not a ROW construct and not a subselect
> 
> Make it a subselect? Not tested but:
> 
> UPDATE myTable SET (col1, col2, ...) = (select * from json_populate_record(NULL::myTable, myJson))
> 
>>
>> This would be useful for some of my trigger code.
>>
>>
>> Regards,
>> fjf2002
>>
>>
>>
> 
> 
> --
> Adrian Klaver
> adrian.klaver@aklaver.com







^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ?
  2026-08-03 15:17 AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ? Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
  2026-08-03 16:15 ` Re: AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ? Adrian Klaver <adrian.klaver@aklaver.com>
@ 2026-08-03 16:48   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2026-08-03 16:48 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Adrian Klaver <adrian.klaver@aklaver.com> writes:
> On 8/3/26 8:17 AM, Färber, Franz-Josef (StMUK) wrote:
>> But what about my other item... what about somehow preventing to write all column names, i. e. changing ...
>> SET (col1, col2, ...)
>> ... to something like ...
>> SET (myTable.*)

> Best bet is you are seeing the effect of, from here:
> https://www.postgresql.org/docs/18/sql-update.html

The SQL standard does not allow writing "*" there, and that's
a restriction I agree with.  If you write "SELECT *" and the
output has more or fewer columns, or columns in a different
order, than you expected, no great harm is done to your database.
If we allowed "UPDATE SET (*) = blah, blah" and the same type
of confusion occurred, you might completely destroy your table.
Writing out the target columns explicitly is a small price to
reduce the odds of mistakes.

(I'm too lazy to search for the exact quote, but there's something
in Brooks' classic "The Mythical Man-month" to the effect that,
if a fairy came to a programmer and offered to make all his code
bug-free if he'd agree to type it in three times, any programmer
in the world would instantly take that bargain.)

Having said that, the error checks in transformMultiAssignRef
could probably be ordered better.  For instance:

postgres=# create table tab (a int, b int);
CREATE TABLE

postgres=# update tab set (*) = row(1,2);
ERROR:  syntax error at or near "*"
LINE 1: update tab set (*) = row(1,2);
                        ^

postgres=# update tab set (tab.*) = row(1,2);
ERROR:  number of columns does not match number of values
LINE 1: update tab set (tab.*) = row(1,2);
                                 ^

postgres=# update tab set (tab.*) = row(1);
ERROR:  column "tab" of relation "tab" does not exist
LINE 1: update tab set (tab.*) = row(1);
                        ^
HINT:  SET target columns cannot be qualified with the relation name.

We ought to be complaining that the SET target is inherently invalid
before we start thinking about whether it matches the source value.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2026-08-03 16:48 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-03 15:17 AW: UPDATE myTable SET (myTable.*) = json_populate_record(...) ? Färber, Franz-Josef (StMUK) <Franz-Josef.Faerber@stmuk.bayern.de>
2026-08-03 16:15 ` Adrian Klaver <adrian.klaver@aklaver.com>
2026-08-03 16:48   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox