node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Any way to see plv8.elog messages from Postgres in the client?

Open JedI-O opened this issue 8 years ago • 2 comments

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!

JedI-O avatar Feb 14 '17 07:02 JedI-O

It might be coming out on client.on('notice', (msg) => console.log(msg)) ? Have you tried that?

brianc avatar Jun 16 '17 21:06 brianc


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);
});

ljluestc avatar Dec 23 '23 21:12 ljluestc