Commit 60c49c5
committed
[FIX] orm: in recompute_fields, avoid memory error from postgresql
improves 13adede
Queries run through client-side cursors will make postgresql materialze the
whole of the result immediately (which is actually, why `cr.rowcount` is always
available right after `execute` in this case). With server side cursors (named
cursors) on the other hand, tuples are materialized when they are fetched.
This is why running the `query` for ids through the client-side cursor just to
be able to access `cr.rowcount`, can cause an out-of-memory exception from
PostgreSQL.
We fix this by wrapping the query in a `CREATE TABLE AS` statement that inserts
returned ids into a temporary table. We then use a named_cursor to fetch ids
from this table in chunks, server-side.
Another approach would have been to just wrap the query in a `SELECT count(*)`
query and run this once to get the `count`. The approach using `CREATE TABLE
AS` has been chosen over that solution to support queries that include DML
statements (e.g. `UPDATE ... RETURNING`) that affect the results of the
compute, as it allows us to run the query on the main (client) cursor, while
still using a named_cursor for fetching the ids memory-efficiently.
closes #322
Related: odoo/upgrade#8487
Signed-off-by: Christophe Simonis (chs) <chs@odoo.com>1 parent 693590a commit 60c49c5
1 file changed
+16
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| |||
278 | 278 | | |
279 | 279 | | |
280 | 280 | | |
281 | | - | |
| 281 | + | |
| 282 | + | |
282 | 283 | | |
283 | 284 | | |
284 | 285 | | |
| |||
288 | 289 | | |
289 | 290 | | |
290 | 291 | | |
291 | | - | |
292 | | - | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
293 | 296 | | |
294 | | - | |
295 | | - | |
296 | | - | |
| 297 | + | |
297 | 298 | | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
303 | 304 | | |
304 | | - | |
| 305 | + | |
305 | 306 | | |
306 | 307 | | |
307 | 308 | | |
308 | 309 | | |
309 | 310 | | |
| 311 | + | |
310 | 312 | | |
311 | 313 | | |
312 | 314 | | |
| |||
336 | 338 | | |
337 | 339 | | |
338 | 340 | | |
| 341 | + | |
339 | 342 | | |
340 | 343 | | |
341 | 344 | | |
| |||
0 commit comments