@@ -91,9 +91,22 @@ impl TablePair {
9191 loop {
9292 let start = Instant :: now ( ) ;
9393 let LastVid { last_vid, rows } = conn. transaction ( || {
94+ // The query limits the number of rows we look at in each
95+ // batch, rather than the number of matching rows to make
96+ // the timing for each batch independent of how many rows we
97+ // actually need to copy. This has the downside that it will
98+ // force a sequential scan, but limiting by the number of
99+ // matching rows runs the risk of a batch taking
100+ // unpredictably long if there are only very few matches.
101+ // Strictly speaking, the `vid >= $3` and `limit` clauses on
102+ // the outer query are redundant.
94103 sql_query ( & format ! (
95104 "with cp as (insert into {dst}({column_list}) \
96- select {column_list} from {src} \
105+ select {column_list} from ( \
106+ select {column_list} from {src} \
107+ where vid >= $3 \
108+ order by vid \
109+ limit $4) a \
97110 where lower(block_range) <= $2 \
98111 and coalesce(upper(block_range), 2147483647) > $1 \
99112 and coalesce(upper(block_range), 2147483647) <= $2 \
@@ -153,9 +166,15 @@ impl TablePair {
153166 loop {
154167 let start = Instant :: now ( ) ;
155168 let LastVid { rows, last_vid } = conn. transaction ( || {
169+ // See the comment on the query in `copy_final_entities` for
170+ // why this query is written this way
156171 sql_query ( & format ! (
157172 "with cp as (insert into {dst}({column_list}) \
158- select {column_list} from {src} \
173+ select {column_list} from ( \
174+ select {column_list} from {src} \
175+ where vid >= $2 \
176+ order by vid \
177+ limit $3) a
159178 where coalesce(upper(block_range), 2147483647) > $1 \
160179 and block_range && int4range($1, null) \
161180 and vid >= $2 \
0 commit comments