In PostgreSQL, updating multiple rows simultaneously can encounter challenges when dealing with NULL values. When working with standalone VALUES expressions, PostgreSQL lacks information about data types. As a result, explicit casting is required for non-numeric literals, including NULL values.
To overcome this issue, explore the following solutions:
UPDATE foo SET x=t.x, y=t.y FROM ( (SELECT pkid, x, y FROM foo LIMIT 0) UNION ALL VALUES (1, 20, NULL) -- no type casts here , (2, 50, NULL) ) t -- column names and types are already defined WHERE f.pkid = t.pkid;
UPDATE foo SET x=t.x, y=t.y FROM ( (SELECT pkid, x, y FROM foo LIMIT 0) UNION ALL SELECT 1, 20, NULL UNION ALL SELECT 2, 50, NULL ) t -- column names and types are already defined WHERE f.pkid = t.pkid;
... FROM ( VALUES ((SELECT pkid FROM foo LIMIT 0) , (SELECT x FROM foo LIMIT 0) , (SELECT y FROM foo LIMIT 0)) -- get type for each col individually , (1, 20, NULL) , (2, 50, NULL) ) t (pkid, x, y) -- columns names not defined yet, only types. ...
UPDATE foo f SET x = (t.r).x -- parenthesis needed to make syntax unambiguous , y = (t.r).y FROM ( VALUES ('(1,20,)'::foo) -- columns need to be in default order of table ,('(2,50,)') -- nothing after the last comma for NULL ) t (r) -- column name for row type WHERE f.pkid = (t.r).pkid;
... FROM ( VALUES ((NULL::foo).*) -- decomposed row of values , (1, 20, NULL) -- uniform syntax for all , (2, 50, NULL) ) t(pkid, x, y) -- arbitrary column names (I made them match) ...
The above is the detailed content of How to Efficiently Update Multiple Rows with NULL Values in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!