Update/Returning + Select 的 UNION ALL 与 UNION?
UNION ALL vs UNION for Update/Returning + Select?
我试图通过将行标记为已更新来使查询幂等。但是,查询规范的一部分是 return 与过滤器匹配的行的 ID。我正在考虑做如下事情:
WITH
prev as (
SELECT id
FROM books
WHERE id = any(::uuid[])
AND updated
),
updated as (
UPDATE books
SET author = || author, updated = true
WHERE id = any(::uuid[])
AND not updated
RETURNING id
)
SELECT id FROM prev
UNION ALL
SELECT id FROM updated
我希望避免重复数据删除步骤使用 UNION
而不是 UNION ALL
所以想知道运算符的语义是否保证第一个查询看不到结果第二个
相关问题:
使用 CTE 进行更新 + select:
有副作用的函数的执行顺序:Does postgres union guarantee order of execution when invoking functions with side effects?
手动重复数据删除:Update a table from a union select statement
PostgreSQL WITH
docs 指定两个 CTE 将在同一个快照中并发执行,因此 UNION ALL
可以安全使用。
The sub-statements in WITH are executed concurrently with each other and with the main query. Therefore, when using data-modifying statements in WITH, the order in which the specified updates actually happen is unpredictable. All the statements are executed with the same snapshot (see Chapter 13), so they cannot “see” one another's effects on the target tables. This alleviates the effects of the unpredictability of the actual order of row updates, and means that RETURNING data is the only way to communicate changes between different WITH sub-statements and the main query.
我试图通过将行标记为已更新来使查询幂等。但是,查询规范的一部分是 return 与过滤器匹配的行的 ID。我正在考虑做如下事情:
WITH
prev as (
SELECT id
FROM books
WHERE id = any(::uuid[])
AND updated
),
updated as (
UPDATE books
SET author = || author, updated = true
WHERE id = any(::uuid[])
AND not updated
RETURNING id
)
SELECT id FROM prev
UNION ALL
SELECT id FROM updated
我希望避免重复数据删除步骤使用 UNION
而不是 UNION ALL
所以想知道运算符的语义是否保证第一个查询看不到结果第二个
相关问题:
使用 CTE 进行更新 + select:
有副作用的函数的执行顺序:Does postgres union guarantee order of execution when invoking functions with side effects?
手动重复数据删除:Update a table from a union select statement
PostgreSQL WITH
docs 指定两个 CTE 将在同一个快照中并发执行,因此 UNION ALL
可以安全使用。
The sub-statements in WITH are executed concurrently with each other and with the main query. Therefore, when using data-modifying statements in WITH, the order in which the specified updates actually happen is unpredictable. All the statements are executed with the same snapshot (see Chapter 13), so they cannot “see” one another's effects on the target tables. This alleviates the effects of the unpredictability of the actual order of row updates, and means that RETURNING data is the only way to communicate changes between different WITH sub-statements and the main query.