node-postgres
node-postgres copied to clipboard
Any way to see plv8.elog messages from Postgres in the client?
I have a Postgres function like
CREATE OR REPLACE FUNCTION public.some_boolean_function()
RETURNS boolean AS
$BODY$
try {
/* Some code prone to failure here ... */
return true;
}
catch(err) {
plv8.elog(WARNING,err);
return false;
}
$BODY$
LANGUAGE plv8 IMMUTABLE
COST 100;
If a run that function from, let's say, pgAdmin I can see the warning triggered by plv8.elog. That's great for developing. However, given the case that this function fails for any reason in production, I'd like to handle that in my NodeJS app. Unfortunately, the elog is not passed to the result object I get with
postgresClient.connectClient(function (err, client, done) {
client.query("SELECT some_boolean_function()", [], function (err, result) {
});
});
So, in the pg client, is there any way to fetch elogs produced by Postgres during a query? If not: That would be a useful feature!
It might be coming out on client.on('notice', (msg) => console.log(msg)) ? Have you tried that?
const { Client } = require('pg');
const connectionString = 'your_connection_string_here';
const client = new Client({
connectionString
});
client.connect(err => {
if (err) {
console.error('Connection error', err.stack);
return;
}
client.query('LISTEN my_custom_channel');
client.on('notification', function(msg) {
console.log('Notification:', msg.payload);
});
});
// Execute your function
client.query('SELECT some_boolean_function()', [], function (err, result) {
if (err) {
console.error('Error executing function', err.stack);
return;
}
console.log('Function result', result);
});