Pg-promise: Display SQL logs

Created on 30 Jan 2017  路  9Comments  路  Source: vitaly-t/pg-promise

Hello

I would like to know if it's possible to returns logs on query (in a batch).
For example :

db.tx(function(t) {
    return t.batch([
      t.none('CREATE SCHEMA IF NOT EXISTS public;'),
      t.none('CREATE TABLE IF NOT EXISTS pg_promise (id integer);'),
    ]);
  })
  .then(function(result) {
    console.log(result);
  })
});
// ['CREATE SCHEMA', 'CREATE TABLE']

I saw mentions about debug but tried with NODE_ENV='development' and did see any logs.

Thanks in advance

invalid / unrelated question

All 9 comments

Are you asking about how to monitor queries being executed by the library?

There is event query for that, and there is pg-monitor module.

See also this example.

Not exactly. I would like to get the numbers of rows inserted in case of an INSERT TO.

Good job for pg-monitor. Didn't know it exists.

Number of rows inserted? You've lost me. Whatever rows you insert, that's the number.

What do you want the library to tell you and in what particular case?

And method .batch resolves with whatever the set of promises/values you provide there.

I'm sorry. For example if I have this query :

INSERT INTO sensor_log_b (location, reading, reading_date)
  SELECT id, location, reading, reading_date
  FROM sensor_log_a
  WHERE reading = 22;

I would like to log the answer (for example: INSERT 0 5230)

How is this relevant to this library? This is a pure SQL question.

Yes you are probably right.
In my case I have an AWS lambda that runs an INSERT INTO query often against Redshift database and sometimes (around 1%) rows are not inserted. I thought displaying this log could have help me find the issue.

That's a very specific case.

If you are inserting inside a transaction, then your .catch will contain the error details. But if you are inserting from another select that fails for one specific value - i'm not even sure how to debug it, rather than use pgAdmin and see where the problem is, since it is all just one query.

Anyhow, the question is not relevant to this library, so i'm closing it.

Thanks a lot @vitaly-t for your answer and your time.

.catch is implemented but didn't catch any errors.

That means your insert is successful, but perhaps your select is simply returning less data than you hoped for, which is a data/business problem, that needs database logic analysis of what is going on there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Juanflugel picture Juanflugel  路  3Comments

illarionvk picture illarionvk  路  3Comments

ForbesLindesay picture ForbesLindesay  路  3Comments

leemhenson picture leemhenson  路  5Comments

paleite picture paleite  路  4Comments