-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00026-memory-usage-with-cursor.js
54 lines (49 loc) · 1.43 KB
/
00026-memory-usage-with-cursor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const pg = require('pg');
const { main } = require('./util/main');
const Cursor = require('pg-cursor');
// Make a Promise wrapper for cursor
const readCursor = (cursor, rowCount) => {
return new Promise((resolve, reject) => {
cursor.read(rowCount, (err, rows) => {
if (err) {
return reject(err);
}
resolve(rows);
});
});
}
main(async () => {
const client = new pg.Client(process.env.DATABASE_URL);
await client.connect();
const printMemory = (title) => {
console.log('Memory Usage - %s', title);
const usage = process.memoryUsage();
Object.keys(usage).forEach((key) => {
console.log(` ${key} ${Math.round(usage[key] / 1024 / 1024 * 100) / 100} MB`);
});
}
const numRows = 10;
const sql = `
SELECT
x,
c.*
FROM information_schema.columns c
, generate_series(1, $1) x`;
const params = [numRows];
printMemory('BEFORE');
// Create the cursor
const cursor = new Cursor(sql, params);
// Submit the query
client.query(cursor);
let count = 0;
// Loop through results
do {
const rows = await readCursor(cursor, 100);
if (rows.length === 0) {
break;
}
count += rows.length;
console.log('Read %s rows (%s total so far)', rows.length, count);
} while (true);
printMemory('AFTER');
});