UPDATE SET Row Expression Assigns Two Columns

Assign two columns in one statement with SET (a, b) = row expression, using either a value row constructor or a correlated sub-SELECT snapshot.

SqlData Integrity

Code

snippet
-- SET (a, b) = ... assigns two columns in one row expression(PostgreSQL 9.5+)
UPDATE configs
SET (value, updated_note) = ('10s', 'raised timeout')
WHERE key = 'worker.timeout'
RETURNING key, value, updated_note;

-- 右端也可以是相关子查询:两列来自同一个 SELECT,读到的是一致快照
UPDATE mirror_configs AS m
SET (value, updated_note) = (
    SELECT c.value, 'mirrored from primary'
    FROM configs c
    WHERE c.key = m.key
)
WHERE m.key = 'worker.timeout'
RETURNING m.key, m.value, m.updated_note;

How it works

What runs here is SELECT / UPDATE — configs and mirror_configs are the objects involved.

Breaking the statements into their working parts: a WHERE clause that keeps only the rows a statement is meant to touch; a RETURNING clause that hands the affected rows back to the caller.

Putting it to use

Run the statement against the configs table.

Core lines

UPDATE mirror_configs AS m
SET (value, updated_note) = (
    SELECT c.value, 'mirrored from primary'
    FROM configs c
    WHERE c.key = m.key
)
WHERE m.key = 'worker.timeout'
RETURNING m.key, m.value, m.updated_note;
UPDATE configs
SET (value, updated_note) = ('10s', 'raised timeout')
WHERE key = 'worker.timeout'
RETURNING key, value, updated_note;

Interactive Tools for This Topic

FAQ

Which tables does this file reference?
`configs`, `mirror_configs` — referenced directly, no join needed.