jordan,
It’s not pretty but here is my test code:
const fs = require('fs')
const { Pool } = require('pg')
const pgconfig = {
host: '32.233.966.433',
port: 26257,
user: 'someone',
database: 'time_series',
// max: 100, // The pool size
ssl: {
rejectUnauthorized: false,
ca: fs.readFileSync('./certs/ca.crt').toString(),
key: fs.readFileSync('./certs/client.root.key').toString(),
cert: fs.readFileSync('./certs/client.root.crt').toString()
}
}
const pool = new Pool(pgconfig)
function prepareAggregateQuery (cb) {
const query = `PREPARE aggregate AS
UPSERT INTO aggregates_1
SELECT
source_id,
$1::timestamp with time zone AS "timestamp", -- The end time of the aggregate
avg(value) AS average,
sqrt(variance(value)) AS standard_deviation,
count(value) AS count
FROM series_1
WHERE ("timestamp" >= ($1::timestamp - interval '1 hours')) AND ("timestamp" < $1::timestamp) -- The start to end time of the aggregate
GROUP BY source_id;`
pool.query(query, (err, res) => {
if (err) {
console.log('Error:', err)
cb(err)
} else {
cb(null)
}
})
}
function executeAggregateQuery (timestamp, cb) {
const query = `EXECUTE aggregate ('${timestamp}')`
pool.query(query, (err, res) => {
if (err) {
console.log('Error:', err)
cb(err)
} else {
cb(null, res.rows)
}
})
}
prepareAggregateQuery(function (err) {
if (err) {
console.log('Error: prepareAggregateQuery failed', err)
} else {
console.log('prepareAggregateQuery: OK', err)
executeAggregateQuery('2017-12-04 04:00:00', function (err) {
if (err) {
console.log('Error: executeAggregateQuery', err)
} else {
console.log('executeAggregateQuery: OK', err)
}
})
executeAggregateQuery('2017-12-04 05:00:00', function (err) {
if (err) {
console.log('Error: executeAggregateQuery', err)
} else {
console.log('executeAggregateQuery: OK', err)
}
})
}
})
And here is the result:
prepareAggregateQuery: OK null
Error: { error: prepared statement "aggregate" does not exist
at Connection.parseE (/home/michael/fits-monitor/node_modules/pg/lib/connection.js:546:11)
at Connection.parseMessage (/home/michael/fits-monitor/node_modules/pg/lib/connection.js:371:19)
at TLSSocket.<anonymous> (/home/michael/fits-monitor/node_modules/pg/lib/connection.js:114:22)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at addChunk (_stream_readable.js:266:12)
at readableAddChunk (_stream_readable.js:253:11)
at TLSSocket.Readable.push (_stream_readable.js:211:10)
at TLSWrap.onread (net.js:585:20)
name: 'error',
length: 119,
severity: 'ERROR',
code: '26000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'sql/prepare.go',
line: '339',
routine: 'getPreparedStatementForExecute' }
Error: executeAggregateQuery { error: prepared statement "aggregate" does not exist
at Connection.parseE (/home/michael/fits-monitor/node_modules/pg/lib/connection.js:546:11)
at Connection.parseMessage (/home/michael/fits-monitor/node_modules/pg/lib/connection.js:371:19)
at TLSSocket.<anonymous> (/home/michael/fits-monitor/node_modules/pg/lib/connection.js:114:22)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at addChunk (_stream_readable.js:266:12)
at readableAddChunk (_stream_readable.js:253:11)
at TLSSocket.Readable.push (_stream_readable.js:211:10)
at TLSWrap.onread (net.js:585:20)
name: 'error',
length: 119,
severity: 'ERROR',
code: '26000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'sql/prepare.go',
line: '339',
routine: 'getPreparedStatementForExecute' }
executeAggregateQuery: OK null
Only one of the EXECUTES works.
If I rearrange things such that the second execute only happens when the first has completed all is OK:
$ node query-db.js
prepareAggregateQuery: OK null
executeAggregateQuery: OK null
executeAggregateQuery: OK null