DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
应该评估所有子查询,就好像它们是在查询开始时拍摄的快照一样。在 Postgres 中无需额外措施即可工作:
CREATE TABLE hopla
( the_id SERIAL NOT NULL PRIMARY KEY
, tralala varchar
);
INSERT INTO hopla(tralala)
SELECT 'tralala_' || gs::text
FROM generate_series(1,4) gs
;
SELECT * FROM hopla;
INSERT INTO hopla(the_id, tralala)
SELECT mx.mx + row_number() OVER (ORDER BY org.the_id)
, org.tralala
FROM hopla org
, (SELECT MAX(the_id) AS mx FROM hopla) mx
;
SELECT * FROM hopla;
结果/输出:
CREATE TABLE
INSERT 0 4
the_id | tralala
--------+-----------
1 | tralala_1
2 | tralala_2
3 | tralala_3
4 | tralala_4
(4 rows)
INSERT 0 4
the_id | tralala
--------+-----------
1 | tralala_1
2 | tralala_2
3 | tralala_3
4 | tralala_4
5 | tralala_1
6 | tralala_2
7 | tralala_3
8 | tralala_4
(8 rows)