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.
Code
-- 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
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Bracket BalancerCheck whether parentheses, square brackets, and braces are balanced, reporting mismatches and maximum nesting depth.Cron Expression ExplainerParse a five-field cron expression and get a field-by-field plain-English explanation plus a one-line human-readable schedule summary.
FAQ
- Which tables does this file reference?
- `configs`, `mirror_configs` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.