小编joy*_*eak的帖子

窗口函数中的 SETVAL

我正在尝试使用相同的键字段对多个表执行 INSERT。根据序列操作函数

重要提示:由于序列是非事务性的,如果事务回滚,则 setval 所做的更改不会撤消。

以上让我担心在我的交易过程中其他东西可能会更新序列。

所以这是我当前的查询:

BEGIN;
--[other queries and stuff here]
CREATE TEMPORARY TABLE my_temp_table ON COMMIT DROP AS
SELECT
    (SETVAL('seq_A',(SELECT "last_value" FROM seq_A) + COUNT(*) OVER ()) - (ROW_NUMBER() OVER (ORDER BY some_other_table.field1 DESC))) AS primarykeyid,
    some_other_table.morefields
FROM
    some_other_table;
--[INSERTs into multiple different tables with primarykeyid here]
COMMIT;
Run Code Online (Sandbox Code Playgroud)

是否SETVAL被调用一次中的每一行my_temp_table或者它被调用一次?

在创建之前进行单独的查询以my_temp_table在运行之前“保留”值会更好吗?例如:

BEGIN;
--[other queries and stuff here]

CREATE TEMPORARY TABLE my_temp_sequence_value ON COMMIT DROP AS
SELECT
    (SETVAL('seq_A',(SELECT "last_value" FROM seq_A) …
Run Code Online (Sandbox Code Playgroud)

postgresql sequence postgresql-9.2

3
推荐指数
1
解决办法
372
查看次数

标签 统计

postgresql ×1

postgresql-9.2 ×1

sequence ×1