@@ -39,18 +39,18 @@ INSERT 0 1000000
39
39
update | delete limit 用法如下
40
40
41
41
```
42
- postgres=# with t1 as (select id from t where id between 1 and 1000 limit 10
42
+ postgres=# with t1 as (select id from t where id between 1 and 1000 limit 10 for update
43
43
)
44
44
delete from t where id in (select * from t1);
45
45
DELETE 10
46
46
47
- postgres=# with t1 as (select id from t where id between 1 and 1000 limit 10
47
+ postgres=# with t1 as (select id from t where id between 1 and 1000 limit 10 for update
48
48
)
49
49
update t set info='new' where id in (select * from t1);
50
50
UPDATE 10
51
51
52
52
postgres=# explain with t1 as (select id from t where id between 1 and 1000
53
- limit 10)
53
+ limit 10 for update )
54
54
postgres-# update t set info='new' where id in (select *
55
55
from t1);
56
56
QUERY
@@ -76,15 +76,15 @@ width=4)
76
76
77
77
```
78
78
postgres=# with t1 as (select ctid from t where id between 1 and 100000
79
- limit 10) delete from t where ctid in (select ctid from t1) and id between 1 and 100000; -- 一定要recheck防止ctid内容在此期间发生变化不符合查询条件, 或者使用RR模式
79
+ limit 10 for update ) delete from t where ctid in (select ctid from t1) and id between 1 and 100000; -- 一定要recheck防止ctid内容在此期间发生变化不符合查询条件, 或者使用RR模式
80
80
DELETE 10
81
81
82
82
postgres=# with t1 as (select ctid from t where id between 1 and 100000
83
- limit 10) update t set info='new' where ctid in (select ctid from t1) and id between 1 and 100000;
83
+ limit 10 for update ) update t set info='new' where ctid in (select ctid from t1) and id between 1 and 100000;
84
84
UPDATE 10
85
85
86
86
postgres=# explain with t1 as (select ctid from t where id between 1 and
87
- 100000 limit 10) update t set info='new' where ctid in (select ctid from t1) and id between 1 and 100000;
87
+ 100000 limit 10 for update ) update t set info='new' where ctid in (select ctid from t1) and id between 1 and 100000;
88
88
QUERY
89
89
PLAN
90
90
-----------------------------------------------------------------------------
@@ -111,10 +111,10 @@ PLAN
111
111
使用如下的写法即可:
112
112
113
113
```
114
- postgres=# with t1 as (select ctid from test limit 10) delete from test where
114
+ postgres=# with t1 as (select ctid from test limit 10 for update ) delete from test where
115
115
ctid = any (array(select ctid from t1)) and id between 1 and 100000;
116
116
DELETE 10
117
- postgres=# with t1 as (select ctid from test limit 10) delete from test where
117
+ postgres=# with t1 as (select ctid from test limit 10 for update ) delete from test where
118
118
ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
119
119
id
120
120
----
@@ -131,7 +131,7 @@ ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
131
131
(10 rows)
132
132
133
133
DELETE 10
134
- postgres=# with t1 as (select ctid from test limit 10) delete from test where
134
+ postgres=# with t1 as (select ctid from test limit 10 for update ) delete from test where
135
135
ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
136
136
id
137
137
----
@@ -149,7 +149,7 @@ ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
149
149
150
150
DELETE 10
151
151
152
- postgres=# explain with t1 as (select ctid from test limit 10) delete from
152
+ postgres=# explain with t1 as (select ctid from test limit 10 for update ) delete from
153
153
test where ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
154
154
QUERY
155
155
PLAN
@@ -174,15 +174,15 @@ create table test(id int, crt_time timestamp);
174
174
insert into test select generate_series(1,1000000), clock_timestamp();
175
175
create index idx_test_1 on test(crt_time);
176
176
177
- postgres=# with t1 as (select ctid from test order by crt_time limit 1000)
177
+ postgres=# with t1 as (select ctid from test order by crt_time limit 1000 for update )
178
178
delete from test where ctid = any (array(select ctid from t1)) and id between 1 and 100000;
179
179
DELETE 1000
180
180
Time: 1.905 ms
181
- postgres=# with t1 as (select ctid from test order by crt_time limit 1000)
181
+ postgres=# with t1 as (select ctid from test order by crt_time limit 1000 for update )
182
182
delete from test where ctid = any (array(select ctid from t1)) and id between 1 and 100000;
183
183
DELETE 1000
184
184
Time: 1.470 ms
185
- postgres=# with t1 as (select ctid from test order by crt_time limit 1000)
185
+ postgres=# with t1 as (select ctid from test order by crt_time limit 1000 for update )
186
186
delete from test where ctid = any (array(select ctid from t1)) and id between 1 and 100000;
187
187
DELETE 1000
188
188
Time: 1.396 ms
0 commit comments